"Fossies" - the Fresh Open Source Software Archive

Member "NZMATH-1.2.0/nzmath/compatibility.py" (19 Nov 2012, 962 Bytes) of package /linux/misc/old/NZMATH-1.2.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "compatibility.py" see the Fossies "Dox" file reference documentation.

    1 """
    2 compatibility between Python version
    3 """
    4 
    5 # __builtins__.set is in >=2.4, sets.Set is in >=2.3.
    6 # Be careful that the compatibility is not perfect.
    7 try:
    8     set, frozenset
    9 except NameError:
   10     import sets
   11     __builtins__["set"] = sets.Set
   12     __builtins__["frozenset"] = sets.ImmutableSet
   13     del sets
   14 
   15 # __builtins__.cmp is only in <3.0.
   16 # Be careful that the compatibility is not perfect.
   17 try:
   18     cmp
   19 except NameError:
   20     cmp = lambda x,y: (x>y) - (x<y)
   21     __builtins__["cmp"] = cmp
   22     del cmp
   23 
   24 # builtin len() raises OverflowError when the result > sys.maxint.
   25 # I'm not sure this restriction will go away in the future.
   26 # The following function card() ought to be used instead of len()
   27 # for obtaining cardinality of sets or set-like objects.
   28 def card(virtualset):
   29     """
   30     Return cardinality of the virtualset.
   31     """
   32     if hasattr(virtualset, "card"):
   33         return virtualset.card()
   34     return len(virtualset)
   35 
   36 __builtins__["card"] = card