##// END OF EJS Templates
Merge pull request #909 from minrk/nosqlite...
Merge pull request #909 from minrk/nosqlite Allow IPython to run without sqlite3. History across sessions is not supported, but IPython will run and readline history within a session will work.

File last commit:

r4872:34c10438
r5154:b59cc86b merge
Show More
pickleutil.py
153 lines | 4.2 KiB | text/x-python | PythonLexer
MinRK
prep newparallel for rebase...
r3539 # encoding: utf-8
"""Pickle related utilities. Perhaps this should be called 'can'."""
__docformat__ = "restructuredtext en"
#-------------------------------------------------------------------------------
# Copyright (C) 2008 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Imports
#-------------------------------------------------------------------------------
MinRK
Improvements to dependency handling...
r3607 import copy
MinRK
update API after sagedays29...
r3664 import sys
from types import FunctionType
MinRK
Improvements to dependency handling...
r3607
MinRK
codeutil into zmq, to prevent IPython.kernel import
r3557 import codeutil
MinRK
prep newparallel for rebase...
r3539
MinRK
Improvements to dependency handling...
r3607 #-------------------------------------------------------------------------------
# Classes
#-------------------------------------------------------------------------------
MinRK
prep newparallel for rebase...
r3539 class CannedObject(object):
MinRK
added dependency decorator
r3546 def __init__(self, obj, keys=[]):
self.keys = keys
MinRK
Improvements to dependency handling...
r3607 self.obj = copy.copy(obj)
MinRK
added dependency decorator
r3546 for key in keys:
MinRK
Improvements to dependency handling...
r3607 setattr(self.obj, key, can(getattr(obj, key)))
Bernardo B. Marques
remove all trailling spaces
r4872
MinRK
added dependency decorator
r3546 def getObject(self, g=None):
if g is None:
g = globals()
for key in self.keys:
setattr(self.obj, key, uncan(getattr(self.obj, key), g))
return self.obj
MinRK
add Reference object
r3643 class Reference(CannedObject):
"""object for wrapping a remote reference by name."""
def __init__(self, name):
if not isinstance(name, basestring):
raise TypeError("illegal name: %r"%name)
self.name = name
Bernardo B. Marques
remove all trailling spaces
r4872
MinRK
add Reference object
r3643 def __repr__(self):
return "<Reference: %r>"%self.name
Bernardo B. Marques
remove all trailling spaces
r4872
MinRK
add Reference object
r3643 def getObject(self, g=None):
if g is None:
g = globals()
try:
return g[self.name]
except KeyError:
raise NameError("name %r is not defined"%self.name)
Bernardo B. Marques
remove all trailling spaces
r4872
MinRK
added dependency decorator
r3546
MinRK
prep newparallel for rebase...
r3539 class CannedFunction(CannedObject):
Bernardo B. Marques
remove all trailling spaces
r4872
MinRK
prep newparallel for rebase...
r3539 def __init__(self, f):
Bernardo B. Marques
remove all trailling spaces
r4872 self._checkType(f)
MinRK
prep newparallel for rebase...
r3539 self.code = f.func_code
MinRK
reflect revised apply_bound pattern
r3655 self.defaults = f.func_defaults
MinRK
update API after sagedays29...
r3664 self.module = f.__module__ or '__main__'
MinRK
Improvements to dependency handling...
r3607 self.__name__ = f.__name__
Bernardo B. Marques
remove all trailling spaces
r4872
MinRK
prep newparallel for rebase...
r3539 def _checkType(self, obj):
assert isinstance(obj, FunctionType), "Not a function type"
Bernardo B. Marques
remove all trailling spaces
r4872
MinRK
add Reference object
r3643 def getObject(self, g=None):
MinRK
update API after sagedays29...
r3664 # try to load function back into its module:
if not self.module.startswith('__'):
try:
__import__(self.module)
except ImportError:
pass
else:
g = sys.modules[self.module].__dict__
Bernardo B. Marques
remove all trailling spaces
r4872
MinRK
prep newparallel for rebase...
r3539 if g is None:
g = globals()
MinRK
reflect revised apply_bound pattern
r3655 newFunc = FunctionType(self.code, g, self.__name__, self.defaults)
MinRK
prep newparallel for rebase...
r3539 return newFunc
MinRK
Improvements to dependency handling...
r3607 #-------------------------------------------------------------------------------
# Functions
#-------------------------------------------------------------------------------
MinRK
prep newparallel for rebase...
r3539 def can(obj):
MinRK
update API after sagedays29...
r3664 # import here to prevent module-level circular imports
MinRK
organize IPython.parallel into subpackages
r3673 from IPython.parallel import dependent
MinRK
add Reference object
r3643 if isinstance(obj, dependent):
MinRK
added dependency decorator
r3546 keys = ('f','df')
return CannedObject(obj, keys=keys)
MinRK
add Reference object
r3643 elif isinstance(obj, FunctionType):
return CannedFunction(obj)
MinRK
prep newparallel for rebase...
r3539 elif isinstance(obj,dict):
return canDict(obj)
elif isinstance(obj, (list,tuple)):
return canSequence(obj)
else:
return obj
def canDict(obj):
if isinstance(obj, dict):
newobj = {}
for k, v in obj.iteritems():
newobj[k] = can(v)
return newobj
else:
return obj
def canSequence(obj):
if isinstance(obj, (list, tuple)):
t = type(obj)
return t([can(i) for i in obj])
else:
return obj
def uncan(obj, g=None):
MinRK
add Reference object
r3643 if isinstance(obj, CannedObject):
MinRK
added dependency decorator
r3546 return obj.getObject(g)
MinRK
prep newparallel for rebase...
r3539 elif isinstance(obj,dict):
MinRK
fix/test pushed function globals
r3638 return uncanDict(obj, g)
MinRK
prep newparallel for rebase...
r3539 elif isinstance(obj, (list,tuple)):
MinRK
fix/test pushed function globals
r3638 return uncanSequence(obj, g)
MinRK
prep newparallel for rebase...
r3539 else:
return obj
def uncanDict(obj, g=None):
if isinstance(obj, dict):
newobj = {}
for k, v in obj.iteritems():
newobj[k] = uncan(v,g)
return newobj
else:
return obj
def uncanSequence(obj, g=None):
if isinstance(obj, (list, tuple)):
t = type(obj)
return t([uncan(i,g) for i in obj])
else:
return obj
def rebindFunctionGlobals(f, glbls):
return FunctionType(f.func_code, glbls)