顯示具有 Toolkit 標籤的文章。 顯示所有文章
顯示具有 Toolkit 標籤的文章。 顯示所有文章

2017年8月7日 星期一

[語言工具箱]Python字串、串列、字典、print、與時間格式的處理

字串處理:

2017/8/8 更新

擷取字串片段
轉載自 : http://snowdaily.pixnet.net/blog/post/81181319-python-%E6%93%B7%E5%8F%96%E5%AD%97%E4%B8%B2%E7%89%87%E6%AE%B5


abc = "This is a string"
print abc[5:]   #從第五個到最後  結果為: is a string
print abc[:5]   #從一開始到第五個  結果為: This
print abc[5:7]  #從第五個到第七個(去頭去尾) 結果為: is
此外,用法類似陣列的存取-abc被隱含轉換為字串陣列(串列)

========================================================


字串分為單引號 '  跟雙引號 " ,建議習慣用雙引號,避免特殊字元衝突!!!

"a" in AAA : 檢查a字串有沒有在AAA字串變數裡面出現,有的話回傳True.

大寫字串轉小寫:
abc = "ABCDE"
abc.lower() #字串ABCDE轉成小寫=> abcde


字串相加:
abc="123"
xyz="456"
hjk = abc[1]+xyz[1]
print(hjk) #會印出 "25" ,若要轉int可用int(hjk)

印出字串長度:
abc = "abcde
print(len(abc))   #字串長度 => 5

字串數字轉換:
int("111")  #字串111轉數字=>111
int(float("11.1")) #字串11.1轉浮點數=> 11.1 再馬上轉成 int => 11

=================================================================
串列(有順序):

在python中沒有所謂的陣列只有list(串列)與tuple
list使用方法
list=[] #宣告一個空陣列,名稱為list
串列可以塞任何東西 thread , string , int , float 甚至連serial物件都能塞,但前提是 只能塞一樣的物件在同一串列
若要像C的多維陣列使用方式來用Python串列的話
我們可以使用下列方法 :
a = ['1','2','3']
b = ['1','2','3']
c = [a,b]

若要列印a的第一個元素'1'的話,可以用print(str(c[0][0]))來顯示
就會印出'1'了

Max : max(list) #回傳串列中最大值

Min : min(list) #回傳串列中最小值

Index : list.index(i) #回傳串列中第一次索引到i值的位置

Length : len(list) #回傳串列的長度

Sum : sum(list) #加總串列

Count : list.count(value) #計算串列中出現某值的次數

Append : list.append(物件) #串列新增

Extend : list.extend(物件) #將物件附加到list 的最後,即擴增。

Insert : list.insert(addr,物件)   #example : list.insert(1,"a") 在位置1插入字串a

Remove : list.remove('a')  #移除串列list中的a值

Sort : 在List中若要排列其中的int元素的話就要使用sort功能,如下例 :
list=[1,2,4,3]

list.sort()
list => 1,2,3,4]
在這邊必須特別提到的
sorted(list)
會變成1,2,3,4
但是如果只是單純使用sorted在呼叫原本的串列
-> list
還是會變回成1,2,4,3

Pop : list.pop[i] #取出list中最後一個數值
Range
count : count 方法 可以計算某值在list出現的次數。


=================================================================
Print

在Python中,print的方式比較像JAVA而非C
然而在Python自己本身卻因為版本的分歧,連print的方式也有了差異
原因在Python在Python3的Unicode的支援度更好,所以用了跟以往Python2不一樣的方式。
例如: 時間格式的處理
1.毫秒數列印:
import datetime

print datetime.datetime.now()
處理結果 : 2016-01-08 11:42:31.804186

2.普通秒數列印:

import time
localtime = time.asctime( time.localtime(time.time()) )
print localtime
處理結果 : Fri Jan  8 09:01:11 2016
3.普通秒數列印的另一種方法
import datetime
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
'2016-03-03 19:25:45'

或是
>>> import datetime
>>> now = datetime.datetime.now()
>>> print unicode(now.replace(microsecond=0))
2011-11-03 11:19:07

4.印一個日曆
import calendar
cal = calendar.month(2008, 1)
print cal;

處理結果 : 
January 2008
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

2017年7月24日 星期一

[語言工具箱] C Linux queue.h之TAILQ序列實作

make_i2c_message->產生訊息並塞到queue.

i2c_tx_check(每300ms檢查一次) -> send_rf_cmd() ->

=============================================================
infoManager.c

static timer g_sendi2c_timer;

TAILQ_HEAD(i2c_tx_queue, i2c_tx_message);
struct i2c_tx_queue *tx_q = NULL;
#define RF_WKUP_MAIN 129//ENET0_MDIO bank4 pin1



int send_rf_cmd(struct i2c_tx_message *msg)
{
    int ret = 1;
    switch(msg->id)
    {
             .....中間略過......
    }
    return ret;
}

.................中間略過.................

void* i2c_tx_check(void* unused)
{
    int ret = 0;
    uint32_t fob_status;
    struct i2c_tx_message *m2 = NULL;
    ret = gpio_get_value(RF_WKUP_MAIN, &fob_status);
    //printf("[Gerald] RF_WKUP_MAIN = %d\n", fob_status);

    if(ret > 0)//errno
    {
        IM_DBG_ERROR("read RF_WKUP_MAIN failed, ret = %d", ret);
        return;
    }
    if((!TAILQ_EMPTY(tx_q)) && (fob_status == 1))//high: ready; low: busy
    {
        m2 = TAILQ_FIRST(tx_q);
        ret = send_rf_cmd(m2);
        TAILQ_REMOVE(tx_q, m2, node);
        free(m2);
    }
}


void start_checker_timer()
 {
    .................中間略過.................
    timer_setup(&g_sendi2c_timer);
     .................中間略過.................
    g_sendi2c_timer.set_timer(&g_sendi2c_timer, 300, true, i2c_tx_check, NULL);
 }


void stop_checker_timer()
 {
    .................中間略過.................
    g_sendi2c_timer.cancel(&g_sendi2c_timer);
 }

void init_i2c_tx_queue()
{
    tx_q = malloc(sizeof(*tx_q));
    TAILQ_INIT(tx_q);
    gpio_export(RF_WKUP_MAIN);
    gpio_dir_in(RF_WKUP_MAIN);
}

void uninit_i2c_tx_queue()
{
    free(tx_q);
}

void make_i2c_message(enum rf_command_table id, char *cmd)
{
    struct i2c_tx_message *m1 = NULL;
    m1 = calloc(1, sizeof(*m1));
    if(m1 == NULL)
    {
        IM_DBG_ERROR("Can not allocate i2c cmd\n");
    }
    else
    {
        m1->id = id;
        m1->cmd = cmd;
        TAILQ_INSERT_TAIL(tx_q, m1, node);
        DKEY_BMW_PROFILE_LOG("cmd_id = %d, value = %s", m1->id, m1->cmd);
    }
}



void initInfoManager() {
      .................中間略過.................
    infoMgr->make_i2c_message = make_i2c_message;
     .................中間略過.................
    init_i2c_tx_queue();
 }

void uninitInfoManager() {
     .................中間略過.................
    uninit_i2c_tx_queue();
     .................中間略過.................
}
==============================================================
infoManager.h

#include <sys/queue.h>


enum rf_command_table {
    MASTER_STATE             = 1,
    ACCESS                   = 2,
    WINDOW                   = 3,
    MISCELLANEOUS            = 4,
    AUTO_DRIVING             = 5,
    VEHICLE_FACILITY_CONTROL = 6,
};

struct i2c_tx_message
{
    enum rf_command_table id;
    char *cmd;
    TAILQ_ENTRY(i2c_tx_message) node;
};

 typedef struct infoManager_s {
     .................中間略過.................
    void (*make_i2c_message)();

 } infoManager;


void add_language_change_event_notify(enum im_evnet_category_t category, int eve
 .................中間略過.................
int send_rf_cmd(struct i2c_tx_message *msg);
========================================================

結論 :























==============================================================

[語言工具箱] C 語言的檔案處理 OPEN / FOPEN


[Open]
#include  //fcntl.h - file control options
Open 有兩種型態 : 
int open(const char *path, int oflags);
int open(const char *path, int oflags, mode_t mode);

以下範例表達 : 
int fd;//宣告int型態的file open狀態容器
char buff[1024];
打開的用法fd = open("filename", 權限);
//filename 在Linux driver中,通常是上層用來做device node的開啟 ->   /sys/..../device_node
假設開啟成功 fd = 0 ,如果失敗 => 小於0 或負數(返回值:成功return 0,出現錯誤return -1並設置errno)

write(fd, );
1 = read(fd1,buff,sizeof(buff))) > 0);
//read 等於1 代表大小大於0‧ 還有值 可以繼續讀取,通常用於while迴圈‧
close(fd); //(返回值:成功return 0,出現錯誤return -1並設置errno)

權限一覽表 : 
O_RDONLY開啟檔案(唯讀).
O_WRONLY開啟檔案(唯寫).
O_RDWR開啟檔案(可被讀跟寫).
O_APPENDAppend new information to the end of the file.
O_TRUNCInitially clear all data from the file.
O_CREAT如果未存在這個檔案,那就建立這個檔案‧
O_EXCLCombined with the O_CREAT option, it ensures that the caller must create the file. If the file already exists, the call will fail.
reference : http://www.thegeekstuff.com/2010/10/linux-error-codes
ERROR CODE TABLE(errno table)
Error numberError CodeError Description
1EPERMOperation not permitted
2ENOENTNo such file or directory
3ESRCHNo such process
4EINTRInterrupted system call
5EIOI/O error
6ENXIONo such device or address
7E2BIGArgument list too long
8ENOEXECExec format error
9EBADFBad file number
10ECHILDNo child processes
11EAGAINTry again
12ENOMEMOut of memory
13EACCESPermission denied
14EFAULTBad address
15ENOTBLKBlock device required
16EBUSYDevice or resource busy
17EEXISTFile exists
18EXDEVCross-device link
19ENODEVNo such device
20ENOTDIRNot a directory
21EISDIRIs a directory
22EINVALInvalid argument
23ENFILEFile table overflow
24EMFILEToo many open files
25ENOTTYNot a typewriter
26ETXTBSYText file busy
27EFBIGFile too large
28ENOSPCNo space left on device
29ESPIPEIllegal seek
30EROFSRead-only file system
31EMLINKToo many links
32EPIPEBroken pipe
33EDOMMath argument out of domain of func
34ERANGEMath result not representable
35EDEADLKResource deadlock would occur
36ENAMETOOLONGFile name too long
37ENOLCKNo record locks available
38ENOSYSFunction not implemented
39ENOTEMPTYDirectory not empty
40ELOOPToo many symbolic links encountered
42ENOMSGNo message of desired type
43EIDRMIdentifier removed
44ECHRNGChannel number out of range
45EL2NSYNCLevel 2 not synchronized
46EL3HLTLevel 3 halted
47EL3RSTLevel 3 reset
48ELNRNGLink number out of range
49EUNATCHProtocol driver not attached
50ENOCSINo CSI structure available
51EL2HLTLevel 2 halted
52EBADEInvalid exchange
53EBADRInvalid request descriptor
54EXFULLExchange full
55ENOANONo anode
56EBADRQCInvalid request code
57EBADSLTInvalid slot
59EBFONTBad font file format
60ENOSTRDevice not a stream
61ENODATANo data available
62ETIMETimer expired
63ENOSROut of streams resources
64ENONETMachine is not on the network
65ENOPKGPackage not installed
66EREMOTEObject is remote
67ENOLINKLink has been severed
68EADVAdvertise error
69ESRMNTSrmount error
70ECOMMCommunication error on send
71EPROTOProtocol error
72EMULTIHOPMultihop attempted
73EDOTDOTRFS specific error
74EBADMSGNot a data message
75EOVERFLOWValue too large for defined data type
76ENOTUNIQName not unique on network
77EBADFDFile descriptor in bad state
78EREMCHGRemote address changed
79ELIBACCCan not access a needed shared library
80ELIBBADAccessing a corrupted shared library
81ELIBSCN.lib section in a.out corrupted
82ELIBMAXAttempting to link in too many shared libraries
83ELIBEXECCannot exec a shared library directly
84EILSEQIllegal byte sequence
85ERESTARTInterrupted system call should be restarted
86ESTRPIPEStreams pipe error
87EUSERSToo many users
88ENOTSOCKSocket operation on non-socket
89EDESTADDRREQDestination address required
90EMSGSIZEMessage too long
91EPROTOTYPEProtocol wrong type for socket
92ENOPROTOOPTProtocol not available
93EPROTONOSUPPORTProtocol not supported
94ESOCKTNOSUPPORTSocket type not supported
95EOPNOTSUPPOperation not supported on transport endpoint
96EPFNOSUPPORTProtocol family not supported
97EAFNOSUPPORTAddress family not supported by protocol
98EADDRINUSEAddress already in use
99EADDRNOTAVAILCannot assign requested address
100ENETDOWNNetwork is down
101ENETUNREACHNetwork is unreachable
102ENETRESETNetwork dropped connection because of reset
103ECONNABORTEDSoftware caused connection abort
104ECONNRESETConnection reset by peer
105ENOBUFSNo buffer space available
106EISCONNTransport endpoint is already connected
107ENOTCONNTransport endpoint is not connected
108ESHUTDOWNCannot send after transport endpoint shutdown
109ETOOMANYREFSToo many references: cannot splice
110ETIMEDOUTConnection timed out
111ECONNREFUSEDConnection refused
112EHOSTDOWNHost is down
113EHOSTUNREACHNo route to host
114EALREADYOperation already in progress
115EINPROGRESSOperation now in progress
116ESTALEStale NFS file handle
117EUCLEANStructure needs cleaning
118ENOTNAMNot a XENIX named type file
119ENAVAILNo XENIX semaphores available
120EISNAMIs a named type file
121EREMOTEIORemote I/O error
122EDQUOTQuota exceeded
123ENOMEDIUMNo medium found
124EMEDIUMTYPEWrong medium type
125ECANCELEDOperation Canceled
126ENOKEYRequired key not available
127EKEYEXPIREDKey has expired
128EKEYREVOKEDKey has been revoked
129EKEYREJECTEDKey was rejected by service
130EOWNERDEADOwner died
131ENOTRECOVERABLEState not recoverable

=============================================
[Fopen]

#include

FOPEN : FILE * fopen(const char * path,const char * mode); 
Example : 
FILE *input;                                                                                                                    
input=fopen(FILE_PATH,"r");
if ( input==NULL ) 
{
    fprintf(stderr, "Open file:%s fail!", FILE_PATH);
    return;
}


FCLOSE : int result fclose(FILE*);
Example : 
input = fopen(FILE_PATH,"r");
fclose(input);


FREAD : fread(void *buffer,int num_byte,int count,FILE *fp);
Example : 
FILE *input;
int result;
char * buffer;
input = fopen(FILE_PATH,"r"); 
if ( input == NULL ) 
{ 
     fprintf(stderr, "Open file:%s fail!", FILE_PATH); 
     return; 
} 
result = fread (buffer,1, Size,input ); 
if (result == -1) 
{
    printf("File read error!!!"); 
    fclose (pFile); 
    return -1; 
} 
fclose (input); 
return 0;

  


FWRITE : fwrite(void *buffer,int num_byte,int count,FILE *fp);
Example : 



FSCANF : fscanf(FILE *fp, constchar* format, [argument ... ]);
用途 : 從檔案讀取數據到宣告的容器中。 
Retrun type : 回傳讀入個數,錯誤回傳-1(EOF)。  
Example :                                                                                                               

int main(void)

{

    char a1[20], a2[20];

    FILE *input;

    input=fopen(FILE_PATH,"r");

    if (input==NULL) 

    {

        fprintf(stderr, "Open file:%s fail!", FILE_PATH);

        return -1;

    }

    fscanf(input, "%s%s", a1, a2);

    fclose(input);

    printf("%s %s\n", a1, a2);

    return 0;

}


FSEEK : 


FPRINTF :  



Extend from : ext2_io.h
Type : Embeded C
OPEN
int write_device_node(char* node, char* value)
{
  int fd = open(node, O_RDWR);
  if(fd<0 nbsp="" p="">  {
          printf("open %s for write failed\n", node);
          return -1;
   }
    int ret = write(fd, value, strlen(value));
    close(fd);
    return ret;
}