try:
from urllib.parse import quote_from_bytes as url_quote
except ImportError:
- from urllib import quote as url_quote
+ from urllib.parse import quote as url_quote
try:
- from thread import allocate_lock
+ from _thread import allocate_lock
except ImportError:
- from dummy_thread import allocate_lock
+ from _dummy_thread import allocate_lock
from collections import deque
-from itertools import imap
+
_word_split_re = re.compile(r'(\s+)')
_punctuation_re = re.compile(
'^(?P<lead>(?:%s)*)(?P<middle>.*?)(?P<trail>(?:%s)*)$' % (
- '|'.join(imap(re.escape, ('(', '<', '<'))),
- '|'.join(imap(re.escape, ('.', ',', ')', '>', '\n', '>')))
+ '|'.join(map(re.escape, ('(', '<', '<'))),
+ '|'.join(map(re.escape, ('.', ',', ')', '>', '\n', '>')))
)
)
_simple_email_re = re.compile(r'^\S+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+$')
# concatenate a list of strings and convert them to unicode.
# unfortunately there is a bug in python 2.4 and lower that causes
# unicode.join trash the traceback.
-_concat = u''.join
+_concat = ''.join
try:
def _test_gen_bug():
raise TypeError(_test_gen_bug)
yield None
_concat(_test_gen_bug())
-except TypeError, _error:
+except TypeError as _error:
if not _error.args or _error.args[0] is not _test_gen_bug:
def concat(gen):
try:
# this hack is needed so that the current frame
# does not show up in the traceback.
exc_type, exc_value, tb = sys.exc_info()
- raise exc_type, exc_value, tb.tb_next
+ raise exc_type(exc_value).with_traceback(tb.tb_next)
else:
concat = _concat
del _test_gen_bug, _error
next = next
except NameError:
def next(x):
- return x.next()
+ return x.__next__()
# if this python version is unable to deal with unicode filenames
# 3.x because compile cannot handle bytes
if sys.version_info < (3, 0):
def _encode_filename(filename):
- if isinstance(filename, unicode):
+ if isinstance(filename, str):
return filename.encode('utf-8')
return filename
else:
FunctionType = type(_func)
GeneratorType = type(_func())
MethodType = type(_C.method)
-CodeType = type(_C.method.func_code)
+CodeType = type(_C.method.__code__)
try:
raise TypeError()
except TypeError:
def internalcode(f):
"""Marks the function as internally used"""
- internal_code.add(f.func_code)
+ internal_code.add(f.__code__)
return f
"""
try:
return open(filename, mode)
- except IOError, e:
+ except IOError as e:
if e.errno not in (errno.ENOENT, errno.EISDIR):
raise
trim_url = lambda x, limit=trim_url_limit: limit is not None \
and (x[:limit] + (len(x) >=limit and '...'
or '')) or x
- words = _word_split_re.split(unicode(escape(text)))
+ words = _word_split_re.split(str(escape(text)))
nofollow_attr = nofollow and ' rel="nofollow"' or ''
for i, word in enumerate(words):
match = _punctuation_re.match(word)
middle = '<a href="mailto:%s">%s</a>' % (middle, middle)
if lead + middle + trail != word:
words[i] = lead + middle + trail
- return u''.join(words)
+ return ''.join(words)
def generate_lorem_ipsum(n=5, html=True, min=20, max=100):
words = LOREM_IPSUM_WORDS.split()
result = []
- for _ in xrange(n):
+ for _ in range(n):
next_capitalized = True
last_comma = last_fullstop = 0
word = None
p = []
# each paragraph contains out of 20 to 100 words.
- for idx, _ in enumerate(xrange(randrange(min, max))):
+ for idx, _ in enumerate(range(randrange(min, max))):
while True:
word = choice(words)
if word != last:
p.append(word)
# ensure that the paragraph ends with a dot.
- p = u' '.join(p)
+ p = ' '.join(p)
if p.endswith(','):
p = p[:-1] + '.'
elif not p.endswith('.'):
result.append(p)
if not html:
- return u'\n\n'.join(result)
- return Markup(u'\n'.join(u'<p>%s</p>' % escape(x) for x in result))
+ return '\n\n'.join(result)
+ return Markup('\n'.join('<p>%s</p>' % escape(x) for x in result))
def unicode_urlencode(obj, charset='utf-8'):
If non strings are provided they are converted to their unicode
representation first.
"""
- if not isinstance(obj, basestring):
- obj = unicode(obj)
- if isinstance(obj, unicode):
+ if not isinstance(obj, str):
+ obj = str(obj)
+ if isinstance(obj, str):
obj = obj.encode(charset)
- return unicode(url_quote(obj))
+ return str(url_quote(obj))
class LRUCache(object):
def iteritems(self):
"""Iterate over all items."""
- return iter(self.items())
+ return iter(list(self.items()))
def values(self):
"""Return a list of all values."""
- return [x[1] for x in self.items()]
+ return [x[1] for x in list(self.items())]
def itervalue(self):
"""Iterate over all values."""
- return iter(self.values())
+ return iter(list(self.values()))
def keys(self):
"""Return a list of all keys ordered by most recent usage."""
"""Returns the current item."""
return self.items[self.pos]
- def next(self):
+ def __next__(self):
"""Goes one item ahead and returns it."""
rv = self.current
self.pos = (self.pos + 1) % len(self.items)
class Joiner(object):
"""A joining helper for templates."""
- def __init__(self, sep=u', '):
+ def __init__(self, sep=', '):
self.sep = sep
self.used = False
def __call__(self):
if not self.used:
self.used = True
- return u''
+ return ''
return self.sep