Skip to content

Commit

Permalink
Merge pull request #28 from samv/fix-visitor-lists
Browse files Browse the repository at this point in the history
Fix visitor lists
  • Loading branch information
hearsaydev committed Apr 8, 2014
2 parents 397d3ad + 382d25f commit 006ebcc
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
copyright = u'2014, Sam Vilain, Hearsay Social'

version = '0.1'
release = '0.1.0'
release = '0.1.2'

exclude_patterns = ['sphinx-build']
pygments_style = 'sphinx'
Expand Down
23 changes: 13 additions & 10 deletions normalize/coll.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ def __len__(self):
return len(self.values)

@classmethod
def tuples_to_coll(cls, generator):
def coerce_tuples(cls, generator):
for k, v in generator:
yield k, v if isinstance(v, cls.itemtype) else cls.coerceitem(v)

@classmethod
def tuples_to_coll(cls, generator, coerce=False):
raise exc.CollectionDefinitionError(
property='tuples_to_coll',
coll='Collection',
Expand Down Expand Up @@ -121,8 +126,10 @@ class DictCollection(KeyedCollection):
colltype = dict

@classmethod
def tuples_to_coll(cls, generator):
return cls.colltype(generator)
def tuples_to_coll(cls, generator, coerce=True):
return cls.colltype(
cls.coerce_tuples(generator) if coerce else generator
)

@classmethod
def coll_to_tuples(cls, coll):
Expand Down Expand Up @@ -152,13 +159,9 @@ class ListCollection(KeyedCollection):
colltype = list

@classmethod
def tuples_to_coll(cls, tuples):
itemtype = cls.itemtype
coerceitem = cls.coerceitem
return cls.colltype(
v if isinstance(v, itemtype) else coerceitem(v) for
k, v in tuples
)
def tuples_to_coll(cls, generator, coerce=True):
tuples = cls.coerce_tuples(generator) if coerce else generator
return cls.colltype(v for k, v in tuples)

@classmethod
def coll_to_tuples(cls, coll):
Expand Down
2 changes: 1 addition & 1 deletion normalize/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def reduce_collection(self, result_coll_generator, fs, coll_type):
is a generator, which returns (key, value) pairs (like
Collection.itertuples())
"""
return coll_type.colltype(result_coll_generator)
return coll_type.tuples_to_coll(result_coll_generator, coerce=False)

def reduce_complex(self, record_result, coll_result, fs, value_type):
"""If a Collection has properties that map to something, this
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
packages=find_packages(),
requires=['richenum (>=1.0.0)'],
test_suite="run_tests",
version='0.1.0',
version='0.1.2',
)
2 changes: 2 additions & 0 deletions tests/test_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ class TestVisitor(unittest.TestCase):
def test_simple_dumper(self):
dumper = SimpleDumper()
dumpable = dumper.map(wall_one)
self.assertIsInstance(dumpable['posts'][0], dict)
self.assertEqual(dumpable['posts'][0]['edited'], "2001-09-09T01:46:40")
json.dumps(dumpable)
6 changes: 3 additions & 3 deletions tests/testclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def __init__(self, data):
for character, line in dialogue:
when += 42
id += 3
comments.append(dict(id=id, edited=datetime.fromtimestamp(when),
comments.append(dict(id=id, edited=datetime.utcfromtimestamp(when),
poster=character, content=line))

wall_one = Wall(
Expand All @@ -94,7 +94,7 @@ def __init__(self, data):
{
"comments": copy.deepcopy(comments[0:3]),
"content": "SEY-MOUR!!!",
"edited": datetime.fromtimestamp(1000000000),
"edited": datetime.utcfromtimestamp(1000000000),
"post_id": 1,
"wall_id": 123,
}
Expand All @@ -111,7 +111,7 @@ def __init__(self, data):
{
"comments": comments[1:],
"content": "SEY-MOUR!!!!",
"edited": datetime.fromtimestamp(1000000240),
"edited": datetime.utcfromtimestamp(1000000240),
"post_id": 1,
"wall_id": 123,
}
Expand Down

0 comments on commit 006ebcc

Please sign in to comment.