python practical skills summary (b)

Thirteen, remove specific character string

 

  Method a: string strip (), rstrip (), lstrip () remove both ends or either end of characters

  Method two: string sections plus splicing

  Method three: replace string () method of a regular expression or the re.sub () delete any substring

  Four: string translate () method, you can delete many different characters at the same time

 

 

Fourteen, python text file operations

 

  Open file: File Object Name = open (name [, mode [, buffering]])

 

    name: a string value containing the name of the file you want to access.

    mode: mode determines the open file modes: read-only, write, append and so on. See a complete list of all possible values ​​as follows. This parameter is not mandatory, the default file is read-only access mode (r).

    buffering :

      If the value of buffering is set to 0, there will be storage. If the value of buffering take 1, the line will register to access the file.

      If the value is an integer greater than 1 of buffering, indicating that this is the buffer size of the storage area. If negative, the buffer size for the system default parking zone.

  Close file: the file name of the object .close ()

    The difference between flush and close

      flush (): after the data in the buffer brush to the destination, the flow may be used.

 

      close (): after the data in the buffer brush to the destination, the flow shut down, the process is mainly used for the underlying resource ending call. This action must be done.

 

Fifth, set the file buffer

  Full Buffer: buffering function is set to open an integer greater than 1, the number of buffer size

  Line buffer: buffering function is set open (text mode can) 1

  Unbuffered: buffering function is set open 0

 

 

XVI map the file into memory

  purpose:

    1. file mapped into memory to achieve random access to binary files

    Register 2. Some embedded devices is addressed to the memory address space, you can go to access these registers by mapping

    3. Multiple processes are mapped to the same file in order to achieve the purpose of the communication process

  solution

    Use standard library mmap.mmap () function will map the file to the memory address space

      m=mmap.mmap(fileno, length[, flags[, prot[, access[, offset]]]])

        fileno: file encoding, may be () obtained by file object .fileno

        length: file descriptor, the object may be fileno file () method, or from os.open ()

          To map the size of the file parts (bytes), this value is 0, then the entire file is mapped, if the current size is larger than the file size, the file is extended.

        flags:

          MAP_PRIVATE: This memory-mapped only available in this process;

          mmap.MAP_SHARED: the shared memory mapping and other processes, the process of mapping all the same file, which will be able to see a change made;  

        prot: mmap.PROT_READ, mmap.PROT_WRITE and mmap.PROT_WRITE | mmap.PROT_READ. Meaning the last one is at the same time readable and writable.

 

        access: access has a value in the optional parameters have mmap

 

          ACCESS_READ: read access.

 

          ACCESS_WRITE: write access by default.

 

          ACCESS_COPY: copy visit, will not change writes to the file, the file is written using flush the change.

 

 

 

 

 

 

XVII uses temporary files

 

  Without naming temporary files, it will be automatically deleted after you close, less access to the file system

  Solutions standard library tempfile.TemporaryFile (anonymous), NamedRemporaryFile (named) difference between the two:

    The difference is that with TemporaryFile, NamedTemporaryFile will generate a genuine document

 

    TemporaryFile not generate real files on the hard disk, but is written in the memory

 

 

 

 

  Examples

    from tempfile import TemporaryFile  , NamedTemporaryFile

 

    1. Read #

    f = TemporaryFile(mode="w+")

    # Parameters:

    # 1). Mode = "w +" allow opening mode, the default mode is w + b

    # W write mode

    # W + write mode

    # W + b mode write Bytes

    # 2). Buffering = -1 buffer size is not limited -1

    # 3). Encoding = character encoding of files read None

 

    f.write("abcdefg\nhijkmlm\nopqist\nuvwxyz")  # 写入

    f.seek (0) # switch the cursor to the beginning

 

    # Line = f.readlines () # read each line in accordance with

    line = f.read () # Read All

    print(line)

 

 

 

    When there is a parameter # delete = True, when the variable is cleared, the resulting file will be deleted, delete = False, the file is not deleted

    from tempfile import NamedTemporaryFile

 

    1. Read #

    f = NamedTemporaryFile(mode="w+", dir=r"D:\data\01_RPA_Project")

    # Parameters:

    # 1). Mode = "w +" allow opening mode, the default mode is w + b

    # W write mode

    # W + write mode

    # W + b mode write Bytes

    # 2). Buffering = -1 buffer size is not limited -1

    # 3). Encoding = character encoding of files read None

    # 4). Dir = None temporary file storage location of the file

    # 5). Delete = True variable will delete files to delete, delete = False, the variables are not deleted when deleting

 

    print (f.name) # print the file name

 

    f.write("abcdefg\nhijkmlm\nopqist\nuvwxyz")  # 写入

    f.seek (0) # switch the cursor to the beginning

 

    # Line = f.readlines () # read each line in accordance with

    line = f.read () # Read All

    print(line)

 

 

 

 

 

XVIII instance properties do type checking

  

  Objective: To want a statically typed language like that, their instance properties do type checking

  Solution: Use Descriptor

 

 

Ninth, commonly decorator

 

  @property

    The method calls the class as a reference field properties in the same class. The modified method of characteristics, internal processing logic can be achieved, but the external call to provide a unified way. Follow the principle of unified access.

 

    In addition, @ property can be used to achieve similar to the set and get methods in Java

 

  @staticmethod

    The methods in the class decorative static method, that is, classes without the need to create an instance of the class can be directly referenced by name. The function-arrival Example unbundling effect.

 

 

 

  @classmethod

    The first parameter is a class class method, as is the method of operating the class itself. Which class class method is called, was introduced to the class which operates as the first argument.

 

 

XX, how variable the same name into a string

 

  solution:

    Dictionary of then extracted key name

    This method can not be abstracted into functions

  Examples

    >> abc = 376         

     # The first step: Dictionary of

    >> dict (abc = abc) # This is the best show of the step

    >> { 'abc': 376} # The resulting dictionary 

    # Step two: key to take a dictionary

    >> dict(abc = abc).keys()

    >> dict_keys ([ 'abc']) # key to obtain a version of the dictionary string

    # The third step: list of

    >> list(dict(abc = abc).keys())

    >> [ 'abc'] # obtain a list

    # At last

    >> list(dict(abc = abc).keys())[0]

    >> 'abc' # you want something: a string of variable name

  

 

 

XXI ring data structures in memory management

 

  Problems: for example, the presence of an annular circular linked list data structures, etc. FIG referenced variable or between each reference cycle will not be automatically recovered when not needed, resulting in memory-intensive

  Solution: Use weak references weakref module, reference can be a weak reference object, but does not increase the reference count, if all variables are between weak reference recovery system will be automatically

 

 

 

XXII, by calling the method instance method name string

  Option One: calling getattr (object attribute name) (parameters)

  Option II: methodcaller operator module (method name, parameters) (Method Object belongs)

Guess you like

Origin www.cnblogs.com/burningcarbon/p/12432581.html