Python Common module Part1

Source: Internet
Author: User
Tags file copy shuffle stdin string format

1. SYS module

The SYS module includes a very useful set of services, including a number of function methods and variables, to handle Python runtime configuration and resources so that it can interact with system environments outside of the previous program
1.1 SYS.ARGV parameters are passed externally to the program

sys.argv[number]
    • 1
    • 2

In general, number 0 is the name of this script, ... The arguments passed under the command line

Example (there is a python file named classoop.py in D:\github\pycharmDevelopment)

import sysdef testSys(): print(sys.argv[0]) print(‘The first arg is: ‘, sys.argv[1]) print(‘The second arg is: ‘, sys.argv[2])testSys()
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
D:\github\pycharmDevelopment>python classoop.py halo hiFile name is:  classoop.pyThe first arg is:  haloThe second arg is:  hi
    • 1
    • 2
    • 3
    • 4
    • 5

1.2 Sys.platform Platform View

>>> sys.platform‘win32‘
    • 1
    • 2
    • 3

1.3 Sys.path View Python path

What happens inside Python when we execute the import module_name? To put it simply, search for module_name. Search for Module.name According to Sys.path's path

>>> sys.path[‘‘, ‘E:\\python35\\python35.zip‘, ‘E:\\python35\\DLLs‘, ‘E:\\python35\\lib‘, ‘E:\\python35‘, ‘E:\\python35\\lib\\site-packages‘, ‘E:\\python35\\lib\\site-packages\\setuptools-27.3.0-py3.5.egg‘]
    • 1
    • 2
    • 3
    • 4
    • 5

Everyone will be able to write the module can be placed in one of the above directory, it can be correctly searched. Of course, you can also add your own module path. Sys.path.append ("Mine module Path").

1.4 Sys.stdin,sys.stdout,sys.stderr

stdin, stdout, and stderr variables contain stream objects that correspond to standard I/O streams. If you need more control over the output, and print does not meet your requirements, they are what you need. You can also replace them, so you can redirect output and input to other devices, or handle them in a non-standard way

1.5 Sys.builtin_module_names

Sys.builtin_module_names returns a list containing the names of the built-in modules

1.6 Sys.exit (N)

Calling Sys.exit (n) can exit the program halfway, and when the argument is not 0 o'clock, a Systemexit exception is thrown, allowing the exception to be caught in the main program.

The 1.7 sys.version string gives you the version information for the Python installation. The Sys.version_info tuple provides an easier way for your program to have the Python version requirements feature.

>>> sys.version‘3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)]‘>>> sys.version_infosys.version_info(major=3, minor=5, micro=2, releaselevel=‘final‘, serial=0)>>>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

1.8 sys.getdefaultencoding (): Gets the system's current encoding, which is generally ASCII by default.
Sys.setdefaultencoding (): Set system default encoding, do not see this method when executing dir (SYS), do not pass in interpreter, can execute reload (SYS), execute setdefaultencoding (' UTF8 ' ), the system default encoding is set to UTF8. (See Set system default encoding)
Sys.getfilesystemencoding (): Gets the file system using encoding, returns ' MBCS ' under Windows, and returns ' Utf-8 ' under Mac.

>>> sys.getdefaultencoding()‘utf-8‘>>> sys.getfilesystemencoding()‘mbcs‘
    • 1
    • 2
    • 3
    • 4
    • 5
2. JSON module

JSON (JavaScript Object Notation) is a lightweight data interchange format. Easy for people to read and write. It is also easy for machine parsing and generation.

The data format can be easily understood as a collection of key-value pairs (A collection of name/value pairs). In different languages, it is understood as objects (object), record (record), structure (struct), Dictionary (dictionary), hash table (hash table), keyed list (keyed list), or associative array (associative Array).
The sequence of values (an ordered list of values). In most languages, it is understood as an array (array).

2.1 Json.dumps ()

The function of dump is to encode the Python object into a JSON object, an encoding process. Note that the JSON module provides the Json.dumps and Json.dump methods, except that the dump is directly to the file, and dumps to a string, where s can be understood as a string.

>>>data = [ { ‘a‘:‘A‘, ‘b‘:(2, 4), ‘c‘:3 } ]>>>print(‘data:‘, repr(data))>>>data: [{‘c‘: 3, ‘b‘: (2, 4), ‘a‘: ‘A‘}]>>>data_string = json.dumps(data)>>>print(‘json:‘, data_string)json: [{"c": 3, "b": [2, 4], "a": "A"}]>>>type(data)<class ‘list‘>>>>type(data_string)<class ‘str‘>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

2.2 Json.dump ()

Not only can you encode a Python object as a string, but you can also write to a file. Because we can't write the Python object directly to the file, it will give an error typeerror:expected a string or other character buffer object, we need to serialize it before we can.

with open(‘json-dump.json‘,‘w‘) as fp: json.dump(data,fp)
    • 1
    • 2

2.3 Json.loads ()

From Python built-in object dump to JSON object we know how to do it, so how do we decode it from JSON object decode to the object that Python can recognize? Yes, with the Json.loads method, of course this is string-based, and if it is a file, we can use the Json.load method.

decoded_json = json.loads(data_string)
    • 1
    • 2

2.4 Json.load ()

You can load the file directly.

with open(‘output.json‘) as fp:    print type(fp) loaded_json = json.load(fp)
    • 1
    • 2
    • 3
3 pickle module (compared with JSON module)

The following methods may be used:

‘bytes_types‘, ‘codecs‘, ‘compatible_formats‘, ‘decode_long‘, ‘dispatch_table‘, ‘dump‘, ‘dumps‘, ‘encode_long‘, ‘format_version‘, ‘io‘, ‘islice‘, ‘load‘, ‘loads‘, ‘maxsize‘, ‘pack‘, ‘re‘, ‘sys‘, ‘unpack‘, ‘whichmodule‘
    • 1
    • 2

3.1 Dumps (object) returns a string that contains an object in the pickle format, loads (string) returns the object contained in the pickle string, and dump (object, file) writes the object to a file, which can be the actual physical file, but it can also be any file-like object that has the write () method, which can accept a single string parameter; Load (file) returns the object contained in the pickle file.

By default, dumps () and dump () use printable ASCII representations to create pickle. Both have a final parameter (optional), and if true, the parameter specifies a faster and smaller binary representation to create the pickle. The loads () and load () functions automatically detect whether pickle is in binary or text format.

>>>data_string = pickle.dumps(data)>>>type(data_string)>>><class ‘bytes‘>>>>data = pickle.loads(data_string)>>>type(data)>>><class ‘list‘>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

Using the binary pickle format does not show much efficiency in space-saving. However, in a system that actually uses complex objects, you'll see that using a binary format can bring significant improvements in size and speed.

3.2 Dump () and load ()

objects that use files and similar files. The operations of these functions are very similar to the dumps () and loads () that we have just seen, except that they have another capability-dump () function can dump several objects to the same file one after the other. The load () is then called to retrieve the objects in the same order.

>>>fo = open(‘halo.txt‘,‘wb‘)>>>pickle.dump(data,fo,True)>>>fo.close()>>>fo1 = open(‘halo.txt‘,‘rb‘)>>>load = pickle.load(fo1)>>>load[{‘c‘: 3, ‘b‘: (2, 4), ‘a‘: ‘A‘}]>>>type(fo1)<class ‘_io.BufferedReader‘>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

3.3 Retrieving supported formats

>>>pickle.format_version‘4.0‘>>>pickle.compatible_formats[‘1.0‘, ‘1.1‘, ‘1.2‘, ‘1.3‘, ‘2.0‘, ‘3.0‘, ‘4.0‘]
    • 1
    • 2
    • 3
    • 4
    • 5

The basic functions of pickle are as above, but there are many high-order usages such as maintenance of object reference, circular reference and recursive reference, etc.

4. Shutil

Advanced files, folders, Compression pack processing modules
Shutil is defined as a high-level file manipulation module in Python, with more powerful functions than the OS module, which may be used in the following ways:

‘chown‘, ‘collections‘, ‘copy‘, ‘copy2‘, ‘copyfile‘, ‘copyfileobj‘, ‘copymode‘, ‘copystat‘, ‘copytree‘, ‘disk_usage‘, ‘errno‘, ‘fnmatch‘, ‘get_archive_formats‘, ‘get_terminal_size‘, ‘get_unpack_formats‘, ‘getgrnam‘, ‘getpwnam‘, ‘ignore_patterns‘, ‘make_archive‘, ‘move‘, ‘nt‘, ‘os‘, ‘register_archive_format‘, ‘register_unpack_format‘, ‘rmtree‘, ‘stat‘, ‘sys‘, ‘tarfile‘, ‘unpack_archive‘, ‘unregister_archive_format‘, ‘unregister_unpack_format‘, ‘which‘]
    • 1
    • 2

4.1 Copy ()

chutil.copy(source, destination)
    • 1
    • 2

The Shutil.copy () function implements the file copy function, copying the source file into the destination folder, and two parameters are in string format. If destination is a file name, it is used as the copied file name, which equals copy + rename.

Copy2 ()
Copy2 () works like copy (), but the metadata that is copied to the new file contains the access and modification times.

4.2 CopyFile ()

CopyFile () copies the contents of the source to the target and generates IOERROR if no permission is written to the target file
CopyFile () is actually using the underlying function copyfileobj (). The argument to CopyFile () is the file name, and the argument to Copyfileobj () is a handle to the open document. The third parameter is optional and is used to read the buffer length of a block.

4.3 copymode () Copying file metadata

Creating a new file on Unix will accept permissions based on the current user's umask. To copy permissions from one file to another, you can use Copymode ().

To copy additional metadata, you can use Copystat ().
Using Copystat () copies only the permissions and dates associated with the file.

4.4 Copytree () processing the directory tree
Shutil contains three functions to handle the directory tree. To copy a directory from one location to another, use Copytree (). This recursively iterates through the source tree and copies the files to the destination.
Copytree () can take the current implementation as a starting point, make it more robust before use, and add features such as a progress bar.

The Symlinks parameter controls whether the symbolic link is copied as a link or a file. By default, the content is copied to a new file, and if the option is true, a new symbolic link is created in the target.

4.5 Rmtree ()
To delete a directory and its contents, you can use Rmtree ().

4.6 Move ()
To move a file or directory from one location to another, you can use Move ().

4.7 shutil.make_archive (base_name, format, ...)

创建压缩包并返回文件路径,例如: zip、tarbase_name: 压缩包的文件名,也可以是压缩包的路径。文件名是保存到当前路径,路径时保存到指定路径format: 压缩包种类, zip, tar, bztar, gztarroot_dir: 要压缩的文件夹路径(默认当前目录)owner: 用户, 默认当前用户group: 组,默认当前组logger: 用户于记录日志,通常是logging.Logger对象
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    #将当前目录打包放到当前目录下    ret = shutil.make_archive(‘dq‘, ‘gztar‘)
    • 1
    • 2
    #将指定目录打包放到当前目录    root_dir = ‘D:/temp/python/zuoye/day6‘    ret = shutil.make_archive(‘dq‘, ‘gztar‘, root_dir=rootdir)
    • 1
    • 2
    • 3
5. Random

In addition to the random number generation, the random module has many uses, the following is the possible method:

‘betavariate‘, ‘choice‘, ‘expovariate‘, ‘gammavariate‘, ‘gauss‘, ‘getrandbits‘, ‘getstate‘, ‘lognormvariate‘, ‘normalvariate‘, ‘paretovariate‘, ‘randint‘, ‘random‘, ‘randrange‘, ‘sample‘, ‘seed‘, ‘setstate‘, ‘shuffle‘, ‘triangular‘, ‘uniform‘, ‘vonmisesvariate‘, ‘weibullvariate‘
    • 1
    • 2

5.1 random.random (): Returns the floating-point number between [0.0,1]

 >>>import random >>>random.random() 0.265150807792064
    • 1
    • 2
    • 3
    • 4

5.2 Random.uniform (A, B): Returns the floating-point number between [A, a and a]

5.3 Random.randint (A, B): Returns an integer between [A, a and a]

5.4 Random.randrange ([start], stop[, step]): From within the specified range, increments by the specified cardinality

random.randrange(10, 100, 2)在结果上与 random.choice(range(10, 100, 2) 等效。
    • 1
    • 2

5.5 Random.choice (sequence). The parameter sequence represents an ordered type. Here's a note: sequence is not a specific type in Python, but a series of types. list, tuple, string all belong to sequence.

5.6 Random.shuffle (x[, random]), used to disrupt elements in a list

5.7 Random.sample (sequence, k), randomly fetches a fragment of the specified length from the specified sequence. The sample function does not modify the original sequence.

slice = random.sample(list, 5) #从list中随机获取5个元素,作为一个片断返回
喜欢的朋友可以加QQ群813622576,群内有免费资料供大家一起交流学习

Python Common module Part1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.