Use six library Python2 project completely transferred to the python3

SIX is a library for python2 and python3 compatible.

It exists without modifying the code to have to work together on the Python 2 and Python 3. That being said, but that does not mean in Python 3 in the reference library can easily run Python code 2.

Indeed, SIX is redefined as a function of differences in python2 and 3, for example, capture all the key functions of dict: Python2 is .iterkeys in the ()

Is .keys in Python3 in ()

In SIX is six.iterkeys (dict) (of course, the original version of the corresponding function can also be used)

That left the SIX library if you write code that can not run in Python2 whether or Python3 in. Therefore not eager to pursue compatibility, then you do not need to use the library.

installation:

https://github.com/benjaminp/six/releases

Double-click to unzip steup.py.

 

use

import six

After written according to the syntax provided to write compatible SIX on pyhton2 / 3 code.

 

six.PY2 / six.PY3: Check the compiler version is python2 / 3

 

The following is provided by SIX compatible syntax:

Constant definitions:

six.class_types

Possible types of classes. In Python 2, which contains the old class and the new class. In Python 3, this is just a new class.

 

six.integer_types

Possible type integer. In Python 2, that is a long or int, Python 3 in the same int.

 

six.string_types

Possible types of text data. This is in str end of basestring 3 Python 2 () and Python.

 

six.text_type

It indicates the type (Unicode) text data. This is the unicode () in the 3 str Python 2 and Python (Pyhon3 text data have been integrated, the default is Unicode text data).

 

six.binary_type

Representative of the type of binary data. This is the Python 2 str and in Python 3 bytes.

 

six.MAXSIZE

The maximum size of the list or dict as such containers. This is equivalent to Python 2.6 and later (including 3.x) of sys.maxsize. Please note that this sys.maxint Python 2 is very similar, but not identical. sys.maxint no direct equivalent in Python 3, because the length of the integer type is limited only by memory size.

 

Built-in function definition

1、six.get_unbound_function(meth)

Unbound methods of obtaining meth. In Python 3, the non-binding method does not exist, so this function simply returns meth unchanged. Example usage:

from six import get_unbound_function

 

class X(object):

def method(self):

pass

 

method_function = get_unbound_function(X.method)six.get_method_function(meth)

 

2、six.get_method_self(meth)

The method of obtaining meth self binding.

 

3、six.get_function_closure(func)

It is equivalent to the Python 2.6+ and Python 2.5 func .__ closure__ in func.func_closure

 

4、six.get_function_code(func)

Get the code object associated func. It is equivalent to the Python 2.6+ and Python 2.5 func .__ code__ in func.func_code.

 

5、six.get_function_defaults(func)

Get the default tuples associated with func. It is equivalent to the Python 2.6+ and Python 2.5 func .__ defaults__ in func.func_defaults.

 

6、six.get_function_globals(func)

Get global function. It is equivalent to the Python 2.6+ and Python 2.5 func .__ globals__ in func.func_globals.

 

7、six.next(it) six.advance_iterator(it)

Get the next item in the iterator it. If the iterator reaches the end, it will lead to StopIteration. Equivalent in Python it.next () 2 and the Python 3 next (it) .Python 2.6 and higher have built the next function, and therefore only needs to be compatible version and Python 2.5 or less when the required function.

 

8、six.callable(obj )

Check whether you can call obj. callable mark has been returned in Python 3.2, only support Python 3.0 or 3.1 when using version six of need.

 

9、six.iterkeys(字典,** kwargs )

Returns an iterator over the dictionary keys. Equivalent to dictionary.iterkeys () and the 3 dictionary.keys Python () in 2 Python. kwargs is passed to the underlying methods.

 

10、six.itervalues(字典,** kwargs )

Returns an iterator dictionary value. Equivalent to dictionary.itervalues ​​() and the 3 dictionary.values ​​Python () in 2 Python. kwargs is passed to the underlying methods.

 

11、six.iteritems(字典,** kwargs )

Returns an iterator dictionary project. Equivalent to dictionary.iteritems () and the 3 dictionary.items Python () in 2 Python. kwargs is passed to the underlying methods.

 

12、six.iterlists(字典,** kwargs )

The call dictionary.iterlists Python 2 () or in Python 3 dictionary.lists (). Python is not the original mapping types such equivalent; This method is intended to use such as a multi-value Werkzeug's dictionary. kwargs is passed to the underlying methods.

 

13, six.viewkeys (dictionary)

Returns the view through the dictionary key. It is equivalent to the Python 2.7 dict.viewkeys () and the 3 dict.keys Python ().

 

14, six.viewvalues ​​(dictionary)

Returns a value dictionary view. It is equivalent to the Python 2.7 dict.viewvalues ​​() and the 3 dict.values ​​Python ().

 

15, six.viewitems (dictionary)

Return to view dictionary project. It is equivalent to the Python 2.7 dict.viewitems () and the 3 dict.items Python ().

 

16、six.create_bound_method(func,obj )

Returns a method object wrapper func and bound to obj. On the Python 2 and Python 3, which returns a types.MethodType object. The reason for the existence of a wrapper function that, in Python 2, MethodType constructor requires transfer obj class.

 

17、six.create_unbound_method(func,cls )

A method that returns an object wrapper function unbound. In Python 2, that returns a types.MethodType object. In Python 3, the method does not exist unbound, and this package will simply return func.

 

18、class six.Iterator

For the production of a small iterator class. Its purpose is to provide a subclass and __next__ method. In Python 2, Iterator there is a method: next. It represents __next__. Maybe you can just alias next to __next__. But it will redefine __next__ subclass serious impact. Iterator in the Python 3 is empty. (In fact, it's just been named object.)

 

The function of the SIX described here. To learn more compatible SIX syntax please visit its official website

http://six.readthedocs.io/

The update is available on github and suggestions on SIX library itself. If you encounter a problem with SIX, the wish to put forward the issue on github.

 

 

 
 

SIX is a library for python2 and python3 compatible.

It exists without modifying the code to have to work together on the Python 2 and Python 3. That being said, but that does not mean in Python 3 in the reference library can easily run Python code 2.

Indeed, SIX is redefined as a function of differences in python2 and 3, for example, capture all the key functions of dict: Python2 is .iterkeys in the ()

Is .keys in Python3 in ()

In SIX is six.iterkeys (dict) (of course, the original version of the corresponding function can also be used)

That left the SIX library if you write code that can not run in Python2 whether or Python3 in. Therefore not eager to pursue compatibility, then you do not need to use the library.

installation:

https://github.com/benjaminp/six/releases

Double-click to unzip steup.py.

 

use

import six

After written according to the syntax provided to write compatible SIX on pyhton2 / 3 code.

 

six.PY2 / six.PY3: Check the compiler version is python2 / 3

 

The following is provided by SIX compatible syntax:

Constant definitions:

six.class_types

Possible types of classes. In Python 2, which contains the old class and the new class. In Python 3, this is just a new class.

 

six.integer_types

Possible type integer. In Python 2, that is a long or int, Python 3 in the same int.

 

six.string_types

Possible types of text data. This is in str end of basestring 3 Python 2 () and Python.

 

six.text_type

It indicates the type (Unicode) text data. This is the unicode () in the 3 str Python 2 and Python (Pyhon3 text data have been integrated, the default is Unicode text data).

 

six.binary_type

Representative of the type of binary data. This is the Python 2 str and in Python 3 bytes.

 

six.MAXSIZE

The maximum size of the list or dict as such containers. This is equivalent to Python 2.6 and later (including 3.x) of sys.maxsize. Please note that this sys.maxint Python 2 is very similar, but not identical. sys.maxint no direct equivalent in Python 3, because the length of the integer type is limited only by memory size.

 

Built-in function definition

1、six.get_unbound_function(meth)

Unbound methods of obtaining meth. In Python 3, the non-binding method does not exist, so this function simply returns meth unchanged. Example usage:

from six import get_unbound_function

 

class X(object):

def method(self):

pass

 

method_function = get_unbound_function(X.method)six.get_method_function(meth)

 

2、six.get_method_self(meth)

The method of obtaining meth self binding.

 

3、six.get_function_closure(func)

It is equivalent to the Python 2.6+ and Python 2.5 func .__ closure__ in func.func_closure

 

4、six.get_function_code(func)

Get the code object associated func. It is equivalent to the Python 2.6+ and Python 2.5 func .__ code__ in func.func_code.

 

5、six.get_function_defaults(func)

Get the default tuples associated with func. It is equivalent to the Python 2.6+ and Python 2.5 func .__ defaults__ in func.func_defaults.

 

6、six.get_function_globals(func)

Get global function. It is equivalent to the Python 2.6+ and Python 2.5 func .__ globals__ in func.func_globals.

 

7、six.next(it) six.advance_iterator(it)

Get the next item in the iterator it. If the iterator reaches the end, it will lead to StopIteration. Equivalent in Python it.next () 2 and the Python 3 next (it) .Python 2.6 and higher have built the next function, and therefore only needs to be compatible version and Python 2.5 or less when the required function.

 

8、six.callable(obj )

Check whether you can call obj. callable mark has been returned in Python 3.2, only support Python 3.0 or 3.1 when using version six of need.

 

9、six.iterkeys(字典,** kwargs )

Returns an iterator over the dictionary keys. Equivalent to dictionary.iterkeys () and the 3 dictionary.keys Python () in 2 Python. kwargs is passed to the underlying methods.

 

10、six.itervalues(字典,** kwargs )

Returns an iterator dictionary value. Equivalent to dictionary.itervalues ​​() and the 3 dictionary.values ​​Python () in 2 Python. kwargs is passed to the underlying methods.

 

11、six.iteritems(字典,** kwargs )

Returns an iterator dictionary project. Equivalent to dictionary.iteritems () and the 3 dictionary.items Python () in 2 Python. kwargs is passed to the underlying methods.

 

12、six.iterlists(字典,** kwargs )

The call dictionary.iterlists Python 2 () or in Python 3 dictionary.lists (). Python is not the original mapping types such equivalent; This method is intended to use such as a multi-value Werkzeug's dictionary. kwargs is passed to the underlying methods.

 

13, six.viewkeys (dictionary)

Returns the view through the dictionary key. It is equivalent to the Python 2.7 dict.viewkeys () and the 3 dict.keys Python ().

 

14, six.viewvalues ​​(dictionary)

Returns a value dictionary view. It is equivalent to the Python 2.7 dict.viewvalues ​​() and the 3 dict.values ​​Python ().

 

15, six.viewitems (dictionary)

Return to view dictionary project. It is equivalent to the Python 2.7 dict.viewitems () and the 3 dict.items Python ().

 

16、six.create_bound_method(func,obj )

Returns a method object wrapper func and bound to obj. On the Python 2 and Python 3, which returns a types.MethodType object. The reason for the existence of a wrapper function that, in Python 2, MethodType constructor requires transfer obj class.

 

17、six.create_unbound_method(func,cls )

A method that returns an object wrapper function unbound. In Python 2, that returns a types.MethodType object. In Python 3, the method does not exist unbound, and this package will simply return func.

 

18、class six.Iterator

For the production of a small iterator class. Its purpose is to provide a subclass and __next__ method. In Python 2, Iterator there is a method: next. It represents __next__. Maybe you can just alias next to __next__. But it will redefine __next__ subclass serious impact. Iterator in the Python 3 is empty. (In fact, it's just been named object.)

 

The function of the SIX described here. To learn more compatible SIX syntax please visit its official website

http://six.readthedocs.io/

The update is available on github and suggestions on SIX library itself. If you encounter a problem with SIX, the wish to put forward the issue on github.

Guess you like

Origin www.cnblogs.com/yecanglan/p/11449381.html