Advanced python bis (module) of the python import module [2-1]

The import module python

To use a module, we must first import the module. Python import statement using a module. For example, a module built into the system math:

You can think math is a pointer to the variable module has been imported by the variables, we can access all public functions, variables and class math module defined:

Math.pow >>> (2, 0.5) # POW is a function 
1.4142135623730951 

>>> Math.PI # PI is variable 
3.141592653589793

If we only want to import math module used in a number of functions, not all the functions, you can use the following statement:

from math import pow, sin, log

If you encounter a name conflict how to do? For example, math module has a log function, logging module also has a log function, if used at the same time, how to resolve name conflicts?

If you use import to import the module name, because the name of the function must be referenced by the module name, so there is no conflict:

. 1  Import math, logging
 2  Print Math.log (10)    # call log function is the math 
. 3 logging.log (10, ' something ' )    # call log of the logging function is

If you are using from ... import Import log function, it is bound to cause conflict. In this case, since a "Alias" to the function to avoid conflicts:

. 1  from math Import log
 2  from logging Import log AS Logger    # logging log now becomes the Logger 
. 3  Print log (10)    # call is a math log 
. 4 Logger (10, ' Import from logging ' )    # call is logging the log

task

Python's os.path module provides isdir () and isfile () function, import the module, and call the function to determine whether the specified directory and file exists.

note: 

1. As the operating environment is the platform server, so the test is also a server folders and files, there is / data / webroot / resource / python folders and files on the server /data/webroot/resource/python/test.txt, we can test the next.

2. Of course, you can test whether there is a corresponding folders and files on this machine.

import os
print os.path.isdir(r'C:\Windows')
print os.path.isfile(r'C:\Windows\notepad.exe')
import os
print os.path.isdir(r'/data/webroot/resource/python')
print os.path.isfile(r'/data/webroot/resource/python/test.txt')

 

 

Dynamic import module python

If you import the module does not exist, Python interpreter will report ImportError error:

1 >>> import something
2 Traceback (most recent call last):
3   File "<stdin>", line 1, in <module>
4 ImportError: No module named something

Sometimes, two different module provides the same functionality, such as StringIO and cStringIO StringIO provides this function.

This is because Python is a dynamic language, interpreted, therefore Python code runs slow.

If you want to increase the operating speed of Python code, the easiest way is to rewrite some of the key functions in C language, so that we can greatly improve the speed of execution.

The same function, StringIO is written in pure Python code, and cStringIO part of the function is written in C, so cStringIO run faster.

Use ImportError wrong, we often Dynamically importing modules in Python:

1 try:
2     from cStringIO import StringIO
3 except ImportError:
4     from StringIO import StringIO

The code introduced from cStringIO first try, if it fails (such cStringIO not installed), and then try to import from StringIO. Thus, if cStringIO module is present, then we will get faster speed, if cStringIO does not exist, at most, the code will run slower, but it will not affect the normal execution of the code.

Try to capture action is wrong, and execute the statement except when capturing the specified error.

task

Using the import ... as ..., you may also be introduced into the dynamic modules of different names.

Python 2.6 / 2.7 provides a json module, but Python 2.5 and earlier versions do not json module, but you can install a simplejson module, function signature and functions of these two modules provide exactly the same.

Write the code into json module can be operable at Python 2.5 / 2.6 / 2.7.

1 try:
2     import json
3 except ImportError:
4     import simplejson as json
5 
6 print json.dumps({'python':2.7})

 

The use python __future__

The new version of Python will introduce new features, but in fact these functions on an older version already exists. To "try" some new features, can be achieved by importing certain functions __future__ module.

For example, Python integer division integer result is still 2.7:

>>> 10 / 3
3

However, Python 3.x has improved integer division operation, "/" will be in addition to float, "//" In addition it still is an integer:

>>> 10 / 3
3.3333333333333335
>>> 10 // 3
3

3.x division rule to be introduced in Python 2.7, introducing the __future__ division:

>>> from __future__ import division
>>> print 10 / 3
3.3333333333333335

When a new version of a feature is not compatible with older versions, this feature will be added in the old version to __future__ so old code to test the new features in the old version.

 

task

In Python 3.x, the Unicode string is uniform, no need to add a prefix u, and stored in bytes must str prefixed b. Please use __future__ of unicode_literals write unicode string in Python 2.7.

1 from __future__ import unicode_literals
2 
3 s = 'am I an unicode?'
4 print isinstance(s, unicode)

 

Guess you like

Origin www.cnblogs.com/ucasljq/p/11622312.html