diff options
author | Brian Dolbec <dolsen@gentoo.org> | 2012-10-13 12:02:10 -0700 |
---|---|---|
committer | Brian Dolbec <dolsen@gentoo.org> | 2012-10-13 13:26:51 -0700 |
commit | 4896f9a6e5d8b09187ffd0a000dbae3bdbcabd30 (patch) | |
tree | 6a318b54c5313a30a507ecac08ea3bc4bbae84b8 | |
parent | fc1de4a933320965e390b2e765bf73a54425e0eb (diff) | |
download | layman-4896f9a6e5d8b09187ffd0a000dbae3bdbcabd30.tar.gz layman-4896f9a6e5d8b09187ffd0a000dbae3bdbcabd30.tar.bz2 layman-4896f9a6e5d8b09187ffd0a000dbae3bdbcabd30.zip |
commit missed compatibility file for py3, py2 functioning.
-rw-r--r-- | layman/compatability.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/layman/compatability.py b/layman/compatability.py new file mode 100644 index 0000000..ea7149e --- /dev/null +++ b/layman/compatability.py @@ -0,0 +1,45 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +""" Copyright 2005 - 2008 Gunnar Wrobel + 2011 - Brian Dolbec + Distributed under the terms of the GNU General Public License v2 +""" + +import sys, types + + +def encode(text, enc="UTF-8"): + """py2, py compatibility function""" + if hasattr(text, 'decode'): + return text.decode(enc) + return str(text) + + +def fileopen(path, mode, enc="UTF-8"): + """py2, py3 compatibility function""" + try: + f = open(path, mode, encoding=enc) + except TypeError: + f = open(path, mode) + return f + + +def cmp_to_key(mycmp): + 'Convert a cmp= function into a key= function' + class K(object): + def __init__(self, obj, *args): + self.obj = obj + def __lt__(self, other): + return mycmp(self.obj, other.obj) < 0 + def __gt__(self, other): + return mycmp(self.obj, other.obj) > 0 + def __eq__(self, other): + return mycmp(self.obj, other.obj) == 0 + def __le__(self, other): + return mycmp(self.obj, other.obj) <= 0 + def __ge__(self, other): + return mycmp(self.obj, other.obj) >= 0 + def __ne__(self, other): + return mycmp(self.obj, other.obj) != 0 + return K |