All examples work on Python 3.

This commit is contained in:
Thomas Kluyver
2012-01-06 00:11:51 +00:00
parent 1ea4c5afdc
commit b16b2ce723
7 changed files with 36 additions and 14 deletions
+5
View File
@@ -14,6 +14,11 @@ COMMA = ','
CONJUNCTION = 'and'
SPACE = ' '
try:
unicode
except NameError:
unicode = str
def join(l, conj=CONJUNCTION, im_a_moron=MORON_MODE, seperator=COMMA):
"""Joins lists of words. Oxford comma and all."""
+13 -5
View File
@@ -14,6 +14,8 @@ from __future__ import absolute_import
import re
import sys
PY3 = sys.version_info[0] >= 3
from ..packages import colorama
__all__ = (
@@ -53,12 +55,18 @@ class ColoredString(object):
def __repr__(self):
return "<%s-string: '%s'>" % (self.color, self.s)
def __str__(self):
return self.__unicode__().encode('utf8')
def __unicode__(self):
return self.color_str
if PY3:
__str__ = __unicode__
else:
def __str__(self):
return unicode(self).encode('utf8')
def __iter__(self):
return iter(self.color_str)
def __add__(self, other):
return str(self.color_str) + str(other)
@@ -68,8 +76,8 @@ class ColoredString(object):
def __mul__(self, other):
return (self.color_str * other)
def split(self, x=' '):
return map(self._new, self.s.split(x))
def split(self, sep=None):
return [self._new(s) for s in self.s.split(sep)]
def _new(self, s):
return ColoredString(self.color, s)
+4
View File
@@ -16,6 +16,10 @@ import os.path
from os import makedirs
from glob import glob
try:
basestring
except NameError:
basestring = str
def expand_path(path):
"""Expands directories and globs in given path."""
+3 -1
View File
@@ -1,6 +1,8 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
import os
@@ -13,4 +15,4 @@ text = 'THIS TEXT IS COLORED %s!'
if __name__ == '__main__':
for color in colored.COLORS:
print getattr(colored, color)(text % color.upper())
print(getattr(colored, color)(text % color.upper()))
+1 -1
View File
@@ -17,7 +17,7 @@ colors = [
colored.magenta('magenta')
]
colors = map(str, colors)
colors = [str(cs) for cs in colors]
puts('Smart:')
+7 -5
View File
@@ -1,6 +1,8 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
import os
@@ -13,16 +15,16 @@ resources.init('kennethreitz', 'clint')
lorem = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
print '%s created.' % resources.user.path
print('%s created.' % resources.user.path)
resources.user.write('lorem.txt', lorem)
print 'lorem.txt created'
print('lorem.txt created')
assert resources.user.read('lorem.txt') == lorem
print 'lorem.txt has correct contents'
print('lorem.txt has correct contents')
resources.user.delete('lorem.txt')
print 'lorem.txt deleted'
print('lorem.txt deleted')
assert resources.user.read('lorem.txt') == None
print 'lorem.txt deletion confirmed'
print('lorem.txt deletion confirmed')
+3 -2
View File
@@ -19,5 +19,6 @@ if __name__ == '__main__':
col = 60
puts(columns([(colored.red('Column 1')), col], [(colored.green('Column Two')), None], [(colored.magenta('Column III')), col]))
puts(columns(['hi there my name is kenneth and this is a columns', col], [lorem, None], ['kenneths', col]))
puts(columns([(colored.red('Column 1')), col], [(colored.green('Column Two')), None],
[(colored.magenta('Column III')), col]))
puts(columns(['hi there my name is kenneth and this is a columns', col], [lorem, None], ['kenneths', col]))