Mercurial > genshi > genshi-test
comparison genshi/util.py @ 914:c5faa881d87f experimental-py3k
py3k branch: add compatibility functions for dealing with python 3; factor existing compatibility functions out from genshi utils.
author | hodgestar |
---|---|
date | Sun, 24 Oct 2010 21:38:19 +0000 |
parents | fbe34d12acde |
children |
comparison
equal
deleted
inserted
replaced
913:263919318d52 | 914:c5faa881d87f |
---|---|
13 | 13 |
14 """Various utility classes and functions.""" | 14 """Various utility classes and functions.""" |
15 | 15 |
16 import htmlentitydefs as entities | 16 import htmlentitydefs as entities |
17 import re | 17 import re |
18 | |
19 from compat import any, all, stringrepr | |
18 | 20 |
19 __docformat__ = 'restructuredtext en' | 21 __docformat__ = 'restructuredtext en' |
20 | 22 |
21 | 23 |
22 class LRUCache(dict): | 24 class LRUCache(dict): |
244 :param text: the string to remove tags from | 246 :param text: the string to remove tags from |
245 :return: the text with tags removed | 247 :return: the text with tags removed |
246 """ | 248 """ |
247 return _STRIPTAGS_RE.sub('', text) | 249 return _STRIPTAGS_RE.sub('', text) |
248 | 250 |
249 | |
250 def stringrepr(string): | |
251 ascii = string.encode('ascii', 'backslashreplace') | |
252 quoted = "'" + ascii.replace("'", "\\'") + "'" | |
253 if len(ascii) > len(string): | |
254 return 'u' + quoted | |
255 return quoted | |
256 | |
257 | |
258 # Compatibility fallback implementations for older Python versions | |
259 | |
260 try: | |
261 all = all | |
262 any = any | |
263 except NameError: | |
264 def any(S): | |
265 for x in S: | |
266 if x: | |
267 return True | |
268 return False | |
269 | |
270 def all(S): | |
271 for x in S: | |
272 if not x: | |
273 return False | |
274 return True |