Skip to content

Commit

Permalink
Refactor RecursiveDefaultDict default behavior. Fixes #10
Browse files Browse the repository at this point in the history
Don't use collections.defaultdict with a default_factory function
b/c that has problems with pickle.loads which triggers #10. Instead
refactor __getitem__ and create the default values for missing keys
by hand.
  • Loading branch information
atodorov committed Aug 10, 2016
1 parent b1730c9 commit 3bf8b55
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
11 changes: 7 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,32 @@ Changelog
=========

* master
* Merge chartit_tests/ with demoproject/
* Merge ``chartit_tests/`` with ``demoproject/``
* Load test DB with real data to use during testing
* Add more tests
* Update the path to demoproject.settings when building docs. Fixes
a problem which caused some API docs to be empty
* Fix ValueError: not enough values to unpack (expected 2, got 0)
with PivotChart when the QuerySet returns empty data
* Dropped requirement on `simplejson`
* Dropped requirement on ``simplejson``
* Properly handle unicode data in Pivot charts. Fixes
`#5 <https://github.com/chartit/django-chartit/issues/5>`_
* Demo project updated with Chart and PivotChart examples of
rendering DateField values on the X axis
* Allow charting of extra() or annotate() fields. Fixes
* Allow charting of ``extra()`` or ``annotate()`` fields. Fixes
`#8 <https://github.com/chartit/django-chartit/issues/8>`_ and
`#12 <https://github.com/chartit/django-chartit/issues/12>`_
* Refactor ``RecursiveDefaultDict`` to allow chart objects to be
serialized to/from cache. Fixes
`#10 <https://github.com/chartit/django-chartit/issues/10>`_

* 0.2.5 (August 3, 2016)
* Workaround Python 3 vs. Python 2 list sort issue which breaks
charts with multiple data sources displayed on the same axis!
* Make demoproject/ compatible with Django 1.10

* 0.2.4 (August 2, 2016)
* Fix for `get_all_field_names()` and `get_field_by_name()` removal
* Fix for ``get_all_field_names()`` and ``get_field_by_name()`` removal
in Django 1.10. Fixes
`#39 <https://github.com/chartit/django-chartit/issues/39>`_
* Updated for django.db.sql.query.Query.aggregates removal
Expand Down
19 changes: 12 additions & 7 deletions chartit/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from collections import defaultdict


def _convert_to_rdd(obj):
"""Accepts a dict or a list of dicts and converts it to a
RecursiveDefaultDict."""
Expand All @@ -18,18 +15,26 @@ def _convert_to_rdd(obj):
return obj


class RecursiveDefaultDict(defaultdict):
"""The name says it all.
class RecursiveDefaultDict(dict):
"""
Behaves exactly the same as a collections.defaultdict
but works with pickle.loads. Fixes #10.
"""
def __init__(self, data=None):
self.default_factory = type(self)
if data is not None:
self.data = _convert_to_rdd(data)
self.update(self.data)
del self.data

def __getitem__(self, key):
return super(RecursiveDefaultDict, self).__getitem__(key)
# create a default object if this key
# isn't in the dictionary
if key not in self.keys():
item = self.__class__()
self[key] = item
return item
else:
return super(RecursiveDefaultDict, self).__getitem__(key)

def __setitem__(self, key, item):
if not isinstance(item, RecursiveDefaultDict):
Expand Down

0 comments on commit 3bf8b55

Please sign in to comment.