lazy_compat Module

Compatibility tools to keep the same source working in both Python 2 and 3

Summary of module contents:

Name Description
orange Old Python 2 range (returns a list), working both in Python 2 and 3.
PYTHON2 bool(x) -> bool
builtins Built-in functions, exceptions, and other objects.
xrange range(stop) -> range object range(start, stop[, step]) -> range object
xzip zip(iter1 [,iter2 [...]]) –> zip object
xzip_longest zip_longest(iter1 [,iter2 [...]], [fillvalue=None]) –> zip_longest object
xmap map(func, *iterables) –> map object
xfilter filter(function or None, iterable) –> filter object
STR_TYPES tuple() -> empty tuple tuple(iterable) -> tuple initialized from iterable’s items
INT_TYPES tuple() -> empty tuple tuple(iterable) -> tuple initialized from iterable’s items
SOME_GEN_TYPES tuple() -> empty tuple tuple(iterable) -> tuple initialized from iterable’s items
NEXT_NAME str(object=’‘) -> str str(bytes_or_buffer[, encoding[, errors]]) -> str
iteritems Function to use the generator-based items iterator over built-in dictionaries in both Python 2 and 3.
itervalues Function to use the generator-based value iterator over built-in dictionaries in both Python 2 and 3.
im_func Gets the function from the method in both Python 2 and 3.
meta Allows unique syntax similar to Python 3 for working with metaclasses in both Python 2 and Python 3.
orange(*args, **kwargs)[source]

Old Python 2 range (returns a list), working both in Python 2 and 3.

xrange

alias of range

xzip

alias of zip

xzip_longest

alias of zip_longest

xmap

alias of map

xfilter

alias of filter

iteritems(dictionary)[source]

Function to use the generator-based items iterator over built-in dictionaries in both Python 2 and 3.

itervalues(dictionary)[source]

Function to use the generator-based value iterator over built-in dictionaries in both Python 2 and 3.

im_func(method)[source]

Gets the function from the method in both Python 2 and 3.

meta(*bases, **kwargs)[source]

Allows unique syntax similar to Python 3 for working with metaclasses in both Python 2 and Python 3.

Examples:
>>> class BadMeta(type): # An usual metaclass definition
...   def __new__(mcls, name, bases, namespace):
...     if "bad" not in namespace: # A bad constraint
...       raise Exception("Oops, not bad enough")
...     value = len(name) # To ensure this metaclass is called again
...     def really_bad(self):
...       return self.bad() * value
...     namespace["really_bad"] = really_bad
...     return super(BadMeta, mcls).__new__(mcls, name, bases, namespace)
...
>>> class Bady(meta(object, metaclass=BadMeta)):
...   def bad(self):
...     return "HUA "
...
>>> class BadGuy(Bady):
...   def bad(self):
...     return "R"
...
>>> issubclass(BadGuy, Bady)
True
>>> Bady().really_bad() # Here value = 4
'HUA HUA HUA HUA '
>>> BadGuy().really_bad() # Called metaclass ``__new__`` again, so value = 6
'RRRRRR'