Commits

Jason R. Coombs  committed 5ab0851

All modules now use unicode literals for Python 3 compatibility.

  • Participants
  • Parent commits 0645a5b
  • Tags 4.1

Comments (0)

Files changed (28)

 
 * Added jaraco.util.dictlib.IdentityOverrideMap
 * Added jaraco.util.itertools.always_iterable
+* All modules now use unicode literals, consistent with Python 3 syntax
 
 4.0
 ~~~

File jaraco/dateutil/__init__.py

 
 "Date time utilities not available in stock python"
 
-from __future__ import division
+from __future__ import division, unicode_literals
 
 import datetime
 import time
 	For example,
 
 	>>> get_date_format_string('month')
-	'%Y-%m'
+	u'%Y-%m'
 	>>> get_date_format_string(3600)
-	'%Y-%m-%d %H'
+	u'%Y-%m-%d %H'
 	>>> get_date_format_string('hour')
-	'%Y-%m-%d %H'
+	u'%Y-%m-%d %H'
 	>>> get_date_format_string(None)
 	Traceback (most recent call last):
 		...

File jaraco/filesystem/__init__.py

 Copyright © 2004, 2011 Jason R. Coombs
 """
 
-from __future__ import division
+from __future__ import division, unicode_literals
 
 import os
 import itertools
 	the extension.
 
 	>>> insert_before_extension('pages.pdf', '-old')
-	'pages-old.pdf'
+	u'pages-old.pdf'
 	"""
 	parts = list(os.path.splitext(filename))
 	parts[1:1] = [content]

File jaraco/lang/python.py

 #!python
 
-from __future__ import print_function, division
+from __future__ import print_function, division, unicode_literals
 
 import collections
 

File jaraco/util/auth.py

-from __future__ import absolute_import
+from __future__ import absolute_import, unicode_literals
 
 import codecs
 import random
 	@staticmethod
 	def random_byte_generator():
 		while True:
-			yield chr(random.randint(0, 255))
+			yield unichr(random.randint(0, 255))
 

File jaraco/util/bitutil.py

-from __future__ import absolute_import
+from __future__ import absolute_import, unicode_literals
 
 from functools import reduce
 
 	application.
 	>>> get_bit_values(0x3, 2)
 	[1, 1]
-	
+
 	>>> get_bit_values(0x3, 4)
 	[0, 0, 1, 1]
 	"""
 	"""
 	Take a sequence of bits, most significant first, and
 	coalesce them into a number.
-	
+
 	>>> coalesce([1,0,1])
 	5
 	"""
 	"""
 	Subclasses should define _names, a list of flag names beginning
 	with the least-significant bit.
-	
-	>>> MyFlags = type('MyFlags', (Flags,), dict(_names=tuple('abc')))
+
+	>>> MyFlags = type(str('MyFlags'), (Flags,), dict(_names=tuple('abc')))
 	>>> mf = MyFlags.from_number(5)
 	>>> mf['a']
 	1
 	"""
 	A metaclass to create a bitmask with attributes. Subclass an int and
 	set this as the metaclass to use.
-	
+
 	>>> class MyBits(int):
 	...   __metaclass__ = BitMask
 	...   a = 0x1
 	...   b = 0x4
 	...   c = 0x3
-	
+
 	>>> b1 = MyBits(3)
 	>>> b1.a, b1.b, b1.c
 	(True, False, True)

File jaraco/util/classutil.py

 Copyright © 2004, 2009, 2011 Jason R. Coombs
 """
 
-from __future__ import absolute_import
+from __future__ import absolute_import, unicode_literals
 
 __author__ = 'Jason R. Coombs <jaraco@jaraco.com>'
 

File jaraco/util/cmp.py

 Based on recipe found at http://code.activestate.com/recipes/510403/
 """
 
+from __future__ import unicode_literals
+
 class RichComparisonMixin(object):
 	"""
 	Define __cmp__, and inherit from this class to provide full rich
 		return self.__cmp__(other)>=0
 
 class KeyedEqualityMixin(object):
-	
+
 	def __eq__(self, other):
 		return self.__key__() == other.__key__()
 	def __ne__(self, other):

File jaraco/util/concurrency.py

-from __future__ import absolute_import
+from __future__ import absolute_import, unicode_literals
 
 import threading
 import functools
 	threads from calling said thread simultaneously.
 	"""
 	lock = lock or threading.RLock()
-	
+
 	@functools.wraps(f)
 	def exec_atomic(*args, **kwargs):
 		lock.acquire()

File jaraco/util/dice.py

 Simple module for simulating dice rolls.
 """
 
-from __future__ import print_function, absolute_import
+from __future__ import print_function, absolute_import, unicode_literals
 
 import random
 import sys

File jaraco/util/dictlib.py

 # -*- coding: utf-8 -*-
 
-from __future__ import absolute_import
+from __future__ import absolute_import, unicode_literals
 
 import re
 import sys
 	Let's create a map that maps 1-3 -> 'a', 4-6 -> 'b'
 	>>> r = RangeMap({3: 'a', 6: 'b'})  # boy, that was easy
 	>>> r[1], r[2], r[3], r[4], r[5], r[6]
-	('a', 'a', 'a', 'b', 'b', 'b')
+	(u'a', u'a', u'a', u'b', u'b', u'b')
 
 	Even float values should work so long as the comparison operator
 	supports it.
 	>>> r[4.5]
-	'b'
+	u'b'
 
 	But you'll notice that the way rangemap is defined, it must be open-ended on one side.
 	>>> r[0]
-	'a'
+	u'a'
 	>>> r[-1]
-	'a'
+	u'a'
 
 	One can close the open-end of the RangeMap by using undefined_value
 	>>> r = RangeMap({0: RangeMap.undefined_value, 3: 'a', 6: 'b'})
 	One can get the first or last elements in the range by using RangeMap.Item
 	>>> last_item = RangeMap.Item(-1)
 	>>> r[last_item]
-	'b'
+	u'b'
 
 	.last_item is a shortcut for Item(-1)
 	>>> r[RangeMap.last_item]
-	'b'
+	u'b'
 
 	Sometimes it's useful to find the bounds for a RangeMap
 	>>> r.bounds()
 		)
 
 	# some special values for the RangeMap
-	undefined_value = type('RangeValueUndefined', (object,), {})()
+	undefined_value = type(str('RangeValueUndefined'), (object,), {})()
 	class Item(int): pass
 	first_item = Item(0)
 	last_item = Item(-1)
 	>>> d = FoldedCaseKeyedDict()
 	>>> d['heLlo'] = 'world'
 	>>> d
-	{'heLlo': 'world'}
+	{u'heLlo': u'world'}
 	>>> d['hello']
-	'world'
+	u'world'
 	>>> FoldedCaseKeyedDict({'heLlo': 'world'})
-	{'heLlo': 'world'}
+	{u'heLlo': u'world'}
 	>>> d = FoldedCaseKeyedDict({'heLlo': 'world'})
 	>>> d['hello']
-	'world'
+	u'world'
 	>>> d
-	{'heLlo': 'world'}
+	{u'heLlo': u'world'}
 	>>> d = FoldedCaseKeyedDict({'heLlo': 'world', 'Hello': 'world'})
 	>>> d
-	{'heLlo': 'world'}
+	{u'heLlo': u'world'}
 	"""
 	def __setitem__(self, key, val):
 		if isinstance(key, basestring):
 
 	>>> import string
 	>>> "lowercase is %(lowercase)s" % DictAdapter(string)
-	'lowercase is abcdefghijklmnopqrstuvwxyz'
+	u'lowercase is abcdefghijklmnopqrstuvwxyz'
 	"""
 	def __init__(self, wrapped_ob):
 		self.object = wrapped_ob
 	Mix-in class to enable a mapping object to provide items as
 	attributes.
 
-	>>> C = type('C', (dict, ItemsAsAttributes), dict())
+	>>> C = type(str('C'), (dict, ItemsAsAttributes), dict())
 	>>> i = C()
 	>>> i['foo'] = 'bar'
 	>>> i.foo
-	'bar'
+	u'bar'
 
 	# natural attribute access takes precedence
 	>>> i.foo = 'henry'
 	>>> i.foo
-	'henry'
+	u'henry'
 
 	# but as you might expect, the mapping functionality is preserved.
 	>>> i['foo']
-	'bar'
+	u'bar'
 
 	# A normal attribute error should be raised if an attribute is
 	#  requested that doesn't exist.
 
 	It also works on dicts that customize __getitem__
 	>>> missing_func = lambda self, key: 'missing item'
-	>>> C = type('C', (dict, ItemsAsAttributes), dict(__missing__ = missing_func))
+	>>> C = type(str('C'), (dict, ItemsAsAttributes), dict(__missing__ = missing_func))
 	>>> i = C()
 	>>> i.missing
-	'missing item'
+	u'missing item'
 	>>> i.foo
-	'missing item'
+	u'missing item'
 	"""
 	def __getattr__(self, key):
 		try:
 	42
 	>>> d['speed'] = 'speedo'
 	>>> d['speed']
-	'speedo'
+	u'speedo'
 	"""
 	def __missing__(self, key):
 		return key

File jaraco/util/editor.py

+from __future__ import unicode_literals
+
 import tempfile
 import os
 import sys

File jaraco/util/exceptions.py

+from __future__ import unicode_literals
+
 def throws_exception(callable, exception=Exception):
 	"""
 	Return True if the callable throws the specified exception

File jaraco/util/filesystem.py

 #!/usr/bin/env python
-from __future__ import division, absolute_import
+from __future__ import division, absolute_import, unicode_literals
 
 import os
 import re
 	"""
 	Encode the name for a suitable name in the given filesystem
 	>>> encode('Test :1')
-	'Test _1'
+	u'Test _1'
 	"""
 	assert system == 'NTFS', 'unsupported filesystem'
 	special_characters = r'<>:"/\|?*' + ''.join(map(chr, range(32)))
 def replace_extension(new_ext, filename):
 	"""
 	>>> replace_extension('.pdf', 'myfile.doc')
-	'myfile.pdf'
+	u'myfile.pdf'
 	"""
 	return os.path.splitext(filename)[0] + new_ext
 
 
 	>>> repl = ExtensionReplacer('.pdf')
 	>>> repl('myfile.doc')
-	'myfile.pdf'
+	u'myfile.pdf'
 	>>> repl('myfile.txt')
-	'myfile.pdf'
+	u'myfile.pdf'
 	>>> repl('myfile')
-	'myfile.pdf'
+	u'myfile.pdf'
 	"""
 	return functools.partial(replace_extension, new_ext)
 

File jaraco/util/functools.py

-from __future__ import absolute_import
+from __future__ import absolute_import, unicode_literals
 from functools import reduce
 
 def compose(*funcs):
 	function.
 
 	>>> import textwrap
-	>>> str.strip(textwrap.dedent(compose.__doc__)) == compose(str.strip, textwrap.dedent)(compose.__doc__)
+	>>> unicode.strip(textwrap.dedent(compose.__doc__)) == compose(unicode.strip, textwrap.dedent)(compose.__doc__)
 	True
 	"""
 
 
 	>>> lower = method_caller('lower')
 	>>> lower('MyString')
-	'mystring'
+	u'mystring'
 	"""
 	def call_method(target):
 		func = getattr(target, method_name)

File jaraco/util/image.py

 Copyright © 2008,2010,2011 Jason R. Coombs
 """
 
-from __future__ import division
+from __future__ import division, unicode_literals
 
 import operator
 from collections import namedtuple

File jaraco/util/input.py

-
 """
 This module currently provides a cross-platform getch function
 """

File jaraco/util/itertools.py

 Copyright © 2008-2011 Jason R. Coombs
 """
 
-from __future__ import absolute_import
+from __future__ import absolute_import, unicode_literals
 
 import operator
 import itertools
 	>>> truthsplit['x']
 	Traceback (most recent call last):
 	...
-	KeyError: 'x'
+	KeyError: u'x'
 	>>> true_items = truthsplit[True]
 	>>> false_items = truthsplit[False]
 	>>> tuple(iter(false_items))
-	('', None)
+	(u'', None)
 	>>> tuple(iter(true_items))
-	('Test', 30)
+	(u'Test', 30)
 
 	>>> every_third_split = GroupbySaved(range(99), lambda n: n%3)
 	>>> zeros = every_third_split[0]
 	>>> sampleData = ['string 1', 'string 2', '', 'string 3', '', 'string 4', '', '', 'string 5']
 	>>> first = itertools.takewhile(LessThanNBlanks(2), sampleData)
 	>>> tuple(first)
-	('string 1', 'string 2', '', 'string 3')
+	(u'string 1', u'string 2', u'', u'string 3')
 	>>> first = itertools.takewhile(LessThanNBlanks(3), sampleData)
 	>>> tuple(first)
-	('string 1', 'string 2', '', 'string 3', '', 'string 4')
+	(u'string 1', u'string 2', u'', u'string 3', u'', u'string 4')
 	"""
 	def __init__(self, nBlanks):
 		self.limit = nBlanks
 	>>> sampleData = ['string 1', 'string 2', '', 'string 3', '', 'string 4', '', '', 'string 5']
 	>>> first = itertools.takewhile(LessThanNConsecutiveBlanks(2), sampleData)
 	>>> tuple(first)
-	('string 1', 'string 2', '', 'string 3', '', 'string 4', '')
+	(u'string 1', u'string 2', u'', u'string 3', u'', u'string 4', u'')
 	"""
 
 	def __init__(self, nBlanks):
 	object that will split a string with the given arguments for each call
 	>>> s = splitter(',')
 	>>> list(s('hello, world, this is your, master calling'))
-	['hello', ' world', ' this is your', ' master calling']
+	[u'hello', u' world', u' this is your', u' master calling']
 	"""
 	def __init__(self, sep = None):
 		self.sep = sep
 	>>> tuple(c)
 	((0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10))
 	"""
-	nofill = type('nofill', (object,), dict())
+	nofill = type(str('nofill'), (object,), dict())
 	value_is_not_nofill = lambda v: v is not nofill
 	remove_nofill = lambda s: tuple(filter(value_is_not_nofill, s))
 	result = grouper(n, iterable, fillvalue = nofill)
 
 	Note this will normally ignore string types as iterables.
 	>>> flatten(['ab', 'c'])
+	[u'ab', u'c']
+
+	Same for bytes
+	>>> flatten([b'ab', b'c'])
 	['ab', 'c']
 	"""
 	return list(iflatten(subject, test))
 def roundrobin(*iterables):
 	"""
 	>>> ' '.join(roundrobin('ABC', 'D', 'EF'))
-	'A D E B F C'
+	u'A D E B F C'
 	"""
 	# Recipe credited to George Sakkis
 	pending = len(iterables)
 	List unique elements, preserving order. Remember only the element just seen.
 
 	>>> ' '.join(unique_justseen('AAAABBBCCDAABBB'))
-	'A B C D A B'
+	u'A B C D A B'
 
-	>>> ' '.join(unique_justseen('ABBCcAD', str.lower))
-	'A B C A D'
+	>>> ' '.join(unique_justseen('ABBCcAD', unicode.lower))
+	u'A B C A D'
 	"""
 	return itertools.imap(
 		next, itertools.imap(
 	if elements remain in the iterable after the first.
 
 	>>> one(['val'])
-	'val'
+	u'val'
 	>>> one(['val', 'other'])
 	Traceback (most recent call last):
 	...
 	>>> always_iterable([1,2,3])
 	[1, 2, 3]
 	>>> always_iterable('foo')
-	('foo',)
+	(u'foo',)
 	>>> always_iterable(None)
 	(None,)
 	>>> always_iterable(xrange(10))

File jaraco/util/logging.py

-from __future__ import absolute_import
+from __future__ import absolute_import, unicode_literals
 
 import logging
 import time

File jaraco/util/meta.py

 Some useful metaclasses.
 """
 
+from __future__ import unicode_literals
+
 class LeafClassesMeta(type):
 	"""
 	A metaclass for classes that keeps track of all of them that

File jaraco/util/numbers.py

+from __future__ import unicode_literals
+
 def ordinalth(n):
 	"""Return the ordinal with 'st', 'th', or 'nd' appended as appropriate.
-	>>> list(map(ordinalth, range(-5, 22)))
+	>>> list(map(str, map(ordinalth, range(-5, 22))))
 	['-5th', '-4th', '-3rd', '-2nd', '-1st', '0th', '1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th', '9th', '10th', '11th', '12th', '13th', '14th', '15th', '16th', '17th', '18th', '19th', '20th', '21st']
 	"""
 	# zero through three map to 'th', 'st', 'nd', 'rd'
 	3L
 
 	>>> coerce('foo')
-	'foo'
+	u'foo'
 	"""
 	result = value
 	for transform in (float, long):

File jaraco/util/properties.py

+from __future__ import unicode_literals
+
 class NonDataProperty(object):
 	"""Much like the property builtin, but only implements __get__,
 	making it a non-data property, and can be subsequently reset.

File jaraco/util/random.py

-from __future__ import absolute_import
+from __future__ import absolute_import, unicode_literals
 
 import random
 import struct

File jaraco/util/stream.py

+from __future__ import unicode_literals
+
 import sys
 
 class Tee(object):

File jaraco/util/string.py

-from __future__ import absolute_import
+from __future__ import absolute_import, unicode_literals
 
 import re
 import inspect
 	a function that performs those substitutions.
 
 	>>> multi_substitution(('foo', 'bar'), ('bar', 'baz'))('foo')
-	'baz'
+	u'baz'
 	"""
 	substitutions = itertools.starmap(substitution, substitutions)
 	# compose function applies last function first, so reverse the
 	substitutions = reversed(tuple(substitutions))
 	return compose(*substitutions)
 
-class FoldedCase(str):
+class FoldedCase(unicode):
 	"""
 	A case insensitive string class; behaves just like str
 	except compares equal when the only variation is case.
 	4
 
 	>>> s.split('O')
-	['hell', ' w', 'rld']
+	[u'hell', u' w', u'rld']
 
 	>>> sorted(map(FoldedCase, ['GAMMA', 'alpha', 'Beta']))
-	['alpha', 'Beta', 'GAMMA']
+	[u'alpha', u'Beta', u'GAMMA']
 	"""
 	def __lt__(self, other):
 		return self.lower() < other.lower()
 
 	>>> a = 3
 	>>> local_format("{a:5}")
-	'    3'
+	u'    3'
 	"""
 	return string.format(**inspect.currentframe().f_back.f_locals)
 
 	"""object that will split a string with the given arguments for each call
 	>>> s = Splitter(',')
 	>>> s('hello, world, this is your, master calling')
-	['hello', ' world', ' this is your', ' master calling']
+	[u'hello', u' world', u' this is your', u' master calling']
 	"""
 	def __init__(self, *args):
 		self.args = args

File jaraco/util/timing.py

+from __future__ import unicode_literals
+
 import datetime
 
 class Stopwatch(object):

File jaraco/util/ui.py

-from __future__ import print_function, absolute_import
+from __future__ import print_function, absolute_import, unicode_literals
 
 from itertools import count
 

File jaraco/util/wallpaper.py

 Assumes Gnome or Windows.
 """
 
-from __future__ import division, print_function
+from __future__ import division, print_function, unicode_literals
 
 import os
 import re
 		return False
 	# Their pages write some script tags through document.write, which was
 	# causing BeautifulSoup to choke
-	html = ''.join(
+	html = b''.join(
 		line for line in html
-		if not 'document.write' in line
+		if not b'document.write' in line
 	)
 	try:
 		soup = BeautifulSoup(html)