Skip to content

Commit

Permalink
Merge pull request dsc#6 from vmalloc/python3
Browse files Browse the repository at this point in the history
Adds Python3 support.
  • Loading branch information
dsc committed Mar 12, 2012
2 parents 1812c22 + 999659e commit 34eedf5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.pyc
*.egg-info
.tox
19 changes: 10 additions & 9 deletions bunch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

__all__ = ('Bunch', 'bunchify','unbunchify',)

from .python3_compat import *

class Bunch(dict):
""" A dictionary that provides attribute-style access.
Expand All @@ -47,13 +48,13 @@ class Bunch(dict):
A Bunch is a subclass of dict; it supports all the methods a dict does...
>>> b.keys()
>>> sorted(b.keys())
['foo', 'hello']
Including update()...
>>> b.update({ 'ponies': 'are pretty!' }, hello=42)
>>> print repr(b)
>>> print (repr(b))
Bunch(foo=Bunch(lol=True), hello=42, ponies='are pretty!')
As well as iteration...
Expand Down Expand Up @@ -195,14 +196,14 @@ def __repr__(self):
""" Invertible* string-form of a Bunch.
>>> b = Bunch(foo=Bunch(lol=True), hello=42, ponies='are pretty!')
>>> print repr(b)
>>> print (repr(b))
Bunch(foo=Bunch(lol=True), hello=42, ponies='are pretty!')
>>> eval(repr(b))
Bunch(foo=Bunch(lol=True), hello=42, ponies='are pretty!')
(*) Invertible so long as collection contents are each repr-invertible.
"""
keys = self.keys()
keys = list(iterkeys(self))
keys.sort()
args = ', '.join(['%s=%r' % (key, self[key]) for key in keys])
return '%s(%s)' % (self.__class__.__name__, args)
Expand Down Expand Up @@ -248,7 +249,7 @@ def bunchify(x):
nb. As dicts are not hashable, they cannot be nested in sets/frozensets.
"""
if isinstance(x, dict):
return Bunch( (k, bunchify(v)) for k,v in x.iteritems() )
return Bunch( (k, bunchify(v)) for k,v in iteritems(x) )
elif isinstance(x, (list, tuple)):
return type(x)( bunchify(v) for v in x )
else:
Expand All @@ -273,7 +274,7 @@ def unbunchify(x):
nb. As dicts are not hashable, they cannot be nested in sets/frozensets.
"""
if isinstance(x, dict):
return dict( (k, unbunchify(v)) for k,v in x.iteritems() )
return dict( (k, unbunchify(v)) for k,v in iteritems(x) )
elif isinstance(x, (list, tuple)):
return type(x)( unbunchify(v) for v in x )
else:
Expand Down Expand Up @@ -355,11 +356,11 @@ def to_yaml(dumper, data):
>>> yaml.dump(b, default_flow_style=True)
'!bunch.Bunch {foo: [bar, !bunch.Bunch {lol: true}], hello: 42}\\n'
"""
return dumper.represent_mapping(u'!bunch.Bunch', data)
return dumper.represent_mapping(u('!bunch.Bunch'), data)


yaml.add_constructor(u'!bunch', from_yaml)
yaml.add_constructor(u'!bunch.Bunch', from_yaml)
yaml.add_constructor(u('!bunch'), from_yaml)
yaml.add_constructor(u('!bunch.Bunch'), from_yaml)

SafeRepresenter.add_representer(Bunch, to_yaml_safe)
SafeRepresenter.add_multi_representer(Bunch, to_yaml_safe)
Expand Down
22 changes: 22 additions & 0 deletions bunch/python3_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import platform

_IS_PYTHON_3 = (platform.version() >= '3')

identity = lambda x : x

# u('string') replaces the forwards-incompatible u'string'
if _IS_PYTHON_3:
u = identity
else:
import codecs
def u(string):
return codecs.unicode_escape_decode(string)[0]

# dict.iteritems(), dict.iterkeys() is also incompatible
if _IS_PYTHON_3:
iteritems = dict.items
iterkeys = dict.keys
else:
iteritems = dict.iteritems
iterkeys = dict.iterkeys

6 changes: 4 additions & 2 deletions bunch/test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys

def test():
import bunch
import doctest
doctest.testmod(bunch)
returned = doctest.testmod(bunch)
return returned.failed

if __name__ == '__main__':
test()
sys.exit(test())
7 changes: 7 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[tox]
envlist = py26,py27,py32

[testenv]
commands=
python bunch/test.py

0 comments on commit 34eedf5

Please sign in to comment.