Skip to content

Commit

Permalink
Unify Part of the Pickle and Dill code
Browse files Browse the repository at this point in the history
  • Loading branch information
hgrecco committed Jan 28, 2016
1 parent ad47ef9 commit bb8122f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 32 deletions.
27 changes: 2 additions & 25 deletions serialize/dill.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,17 @@
"""

from . import all
from . import pickle

try:
import dill
import copyreg
except ImportError:
all.register_unavailable('dill', pkg='dill')
raise


class DispatchTable:

def __getitem__(self, item):
if item in all.CLASSES:
return lambda obj: (all.CLASSES[item].from_builtin,
(all.CLASSES[item].to_builtin(obj),),
None, None, None)

return copyreg.dispatch_table[item]

def __setitem__(self, key, value):
copyreg.dispatch_table[key] = value

def get(self, key, default=None):
if key in all.CLASSES:
return lambda obj: (all.CLASSES[key].from_builtin,
(all.CLASSES[key].to_builtin(obj),),
None, None, None)

return copyreg.dispatch_table.get(key, default)


class MyPickler(dill.Pickler):

dispatch_table = DispatchTable()
dispatch_table = pickle.DispatchTable()


def dump(obj, fp):
Expand Down
17 changes: 10 additions & 7 deletions serialize/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
:license: BSD, see LICENSE for more details.
"""

import collections

from . import all

try:
Expand All @@ -21,7 +23,7 @@
raise


class DispatchTable:
class DispatchTable(collections.MutableMapping):

def __getitem__(self, item):
if item in all.CLASSES:
Expand All @@ -34,13 +36,14 @@ def __getitem__(self, item):
def __setitem__(self, key, value):
copyreg.dispatch_table[key] = value

def get(self, key, default=None):
if key in all.CLASSES:
return lambda obj: (all.CLASSES[key].from_builtin,
(all.CLASSES[key].to_builtin(obj),),
None, None, None)
def __delitem__(self, key):
del copyreg.dispatch_table[key]

def __iter__(self):
return copyreg.dispatch_table.__iter__()

return copyreg.dispatch_table.get(key, default)
def __len__(self):
return copyreg.dispatch_table.__len__()


class MyPickler(pickle.Pickler):
Expand Down

0 comments on commit bb8122f

Please sign in to comment.