How to use the dill.source.getimportable function in dill

To help you get started, we’ve selected a few dill examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github uqfoundation / dill / tests / test_source.py View on Github external
def test_numpy():
  try:
    from numpy import array
    x = array([1,2,3])
    assert getimportable(x) == 'from numpy import array\narray([1, 2, 3])\n'
    assert getimportable(array) == 'from %s import array\n' % array.__module__
    assert getimportable(x, byname=False) == 'from numpy import array\narray([1, 2, 3])\n'
    assert getimportable(array, byname=False) == 'from %s import array\n' % array.__module__
  except ImportError: pass
github uqfoundation / dill / tests / test_source.py View on Github external
def test_importable():
  assert getimportable(add) == 'from %s import add\n' % __name__
  assert getimportable(squared) == 'from %s import squared\n' % __name__
  assert getimportable(Foo) == 'from %s import Foo\n' % __name__
  assert getimportable(Foo.bar) == 'from %s import bar\n' % __name__
  assert getimportable(_foo.bar) == 'from %s import bar\n' % __name__
  assert getimportable(None) == 'None\n'
  assert getimportable(100) == '100\n'

  assert getimportable(add, byname=False) == 'def add(x,y):\n  return x+y\n'
  assert getimportable(squared, byname=False) == 'squared = lambda x:x**2\n'
  assert getimportable(None, byname=False) == 'None\n'
  assert getimportable(Bar, byname=False) == 'class Bar:\n  pass\n'
  assert getimportable(Foo, byname=False) == 'class Foo(object):\n  def bar(self, x):\n    return x*x+x\n'
  assert getimportable(Foo.bar, byname=False) == 'def bar(self, x):\n  return x*x+x\n'
  assert getimportable(Foo.bar, byname=True) == 'from %s import bar\n' % __name__
  assert getimportable(Foo.bar, alias='memo', byname=True) == 'from %s import bar as memo\n' % __name__
  assert getimportable(Foo, alias='memo', byname=True) == 'from %s import Foo as memo\n' % __name__
  assert getimportable(squared, alias='memo', byname=True) == 'from %s import squared as memo\n' % __name__
  assert getimportable(squared, alias='memo', byname=False) == 'memo = squared = lambda x:x**2\n'
  assert getimportable(add, alias='memo', byname=False) == 'def add(x,y):\n  return x+y\n\nmemo = add\n'
  assert getimportable(None, alias='memo', byname=False) == 'memo = None\n'
  assert getimportable(100, alias='memo', byname=False) == 'memo = 100\n'
  assert getimportable(add, explicit=True) == 'from %s import add\n' % __name__
github uqfoundation / dill / tests / test_source.py View on Github external
def test_importable():
  assert getimportable(add) == 'from %s import add\n' % __name__
  assert getimportable(squared) == 'from %s import squared\n' % __name__
  assert getimportable(Foo) == 'from %s import Foo\n' % __name__
  assert getimportable(Foo.bar) == 'from %s import bar\n' % __name__
  assert getimportable(_foo.bar) == 'from %s import bar\n' % __name__
  assert getimportable(None) == 'None\n'
  assert getimportable(100) == '100\n'

  assert getimportable(add, byname=False) == 'def add(x,y):\n  return x+y\n'
  assert getimportable(squared, byname=False) == 'squared = lambda x:x**2\n'
  assert getimportable(None, byname=False) == 'None\n'
  assert getimportable(Bar, byname=False) == 'class Bar:\n  pass\n'
  assert getimportable(Foo, byname=False) == 'class Foo(object):\n  def bar(self, x):\n    return x*x+x\n'
  assert getimportable(Foo.bar, byname=False) == 'def bar(self, x):\n  return x*x+x\n'
  assert getimportable(Foo.bar, byname=True) == 'from %s import bar\n' % __name__
  assert getimportable(Foo.bar, alias='memo', byname=True) == 'from %s import bar as memo\n' % __name__
  assert getimportable(Foo, alias='memo', byname=True) == 'from %s import Foo as memo\n' % __name__
  assert getimportable(squared, alias='memo', byname=True) == 'from %s import squared as memo\n' % __name__
  assert getimportable(squared, alias='memo', byname=False) == 'memo = squared = lambda x:x**2\n'
  assert getimportable(add, alias='memo', byname=False) == 'def add(x,y):\n  return x+y\n\nmemo = add\n'
  assert getimportable(None, alias='memo', byname=False) == 'memo = None\n'
  assert getimportable(100, alias='memo', byname=False) == 'memo = 100\n'
  assert getimportable(add, explicit=True) == 'from %s import add\n' % __name__
  assert getimportable(squared, explicit=True) == 'from %s import squared\n' % __name__
  assert getimportable(Foo, explicit=True) == 'from %s import Foo\n' % __name__
  assert getimportable(Foo.bar, explicit=True) == 'from %s import bar\n' % __name__
  assert getimportable(_foo.bar, explicit=True) == 'from %s import bar\n' % __name__
  assert getimportable(None, explicit=True) == 'None\n'
  assert getimportable(100, explicit=True) == '100\n'
github uqfoundation / dill / tests / test_source.py View on Github external
def test_numpy():
  try:
    from numpy import array
    x = array([1,2,3])
    assert getimportable(x) == 'from numpy import array\narray([1, 2, 3])\n'
    assert getimportable(array) == 'from %s import array\n' % array.__module__
    assert getimportable(x, byname=False) == 'from numpy import array\narray([1, 2, 3])\n'
    assert getimportable(array, byname=False) == 'from %s import array\n' % array.__module__
  except ImportError: pass
github uqfoundation / dill / tests / test_source.py View on Github external
def test_importable():
  assert getimportable(add) == 'from %s import add\n' % __name__
  assert getimportable(squared) == 'from %s import squared\n' % __name__
  assert getimportable(Foo) == 'from %s import Foo\n' % __name__
  assert getimportable(Foo.bar) == 'from %s import bar\n' % __name__
  assert getimportable(_foo.bar) == 'from %s import bar\n' % __name__
  assert getimportable(None) == 'None\n'
  assert getimportable(100) == '100\n'

  assert getimportable(add, byname=False) == 'def add(x,y):\n  return x+y\n'
  assert getimportable(squared, byname=False) == 'squared = lambda x:x**2\n'
  assert getimportable(None, byname=False) == 'None\n'
  assert getimportable(Bar, byname=False) == 'class Bar:\n  pass\n'
  assert getimportable(Foo, byname=False) == 'class Foo(object):\n  def bar(self, x):\n    return x*x+x\n'
  assert getimportable(Foo.bar, byname=False) == 'def bar(self, x):\n  return x*x+x\n'
  assert getimportable(Foo.bar, byname=True) == 'from %s import bar\n' % __name__
  assert getimportable(Foo.bar, alias='memo', byname=True) == 'from %s import bar as memo\n' % __name__
  assert getimportable(Foo, alias='memo', byname=True) == 'from %s import Foo as memo\n' % __name__
  assert getimportable(squared, alias='memo', byname=True) == 'from %s import squared as memo\n' % __name__
  assert getimportable(squared, alias='memo', byname=False) == 'memo = squared = lambda x:x**2\n'
  assert getimportable(add, alias='memo', byname=False) == 'def add(x,y):\n  return x+y\n\nmemo = add\n'
  assert getimportable(None, alias='memo', byname=False) == 'memo = None\n'
  assert getimportable(100, alias='memo', byname=False) == 'memo = 100\n'
  assert getimportable(add, explicit=True) == 'from %s import add\n' % __name__
  assert getimportable(squared, explicit=True) == 'from %s import squared\n' % __name__
  assert getimportable(Foo, explicit=True) == 'from %s import Foo\n' % __name__
  assert getimportable(Foo.bar, explicit=True) == 'from %s import bar\n' % __name__
  assert getimportable(_foo.bar, explicit=True) == 'from %s import bar\n' % __name__
github uqfoundation / dill / tests / test_source.py View on Github external
def test_importable():
  assert getimportable(add) == 'from %s import add\n' % __name__
  assert getimportable(squared) == 'from %s import squared\n' % __name__
  assert getimportable(Foo) == 'from %s import Foo\n' % __name__
  assert getimportable(Foo.bar) == 'from %s import bar\n' % __name__
  assert getimportable(_foo.bar) == 'from %s import bar\n' % __name__
  assert getimportable(None) == 'None\n'
  assert getimportable(100) == '100\n'

  assert getimportable(add, byname=False) == 'def add(x,y):\n  return x+y\n'
  assert getimportable(squared, byname=False) == 'squared = lambda x:x**2\n'
  assert getimportable(None, byname=False) == 'None\n'
  assert getimportable(Bar, byname=False) == 'class Bar:\n  pass\n'
  assert getimportable(Foo, byname=False) == 'class Foo(object):\n  def bar(self, x):\n    return x*x+x\n'
  assert getimportable(Foo.bar, byname=False) == 'def bar(self, x):\n  return x*x+x\n'
  assert getimportable(Foo.bar, byname=True) == 'from %s import bar\n' % __name__
  assert getimportable(Foo.bar, alias='memo', byname=True) == 'from %s import bar as memo\n' % __name__
  assert getimportable(Foo, alias='memo', byname=True) == 'from %s import Foo as memo\n' % __name__
  assert getimportable(squared, alias='memo', byname=True) == 'from %s import squared as memo\n' % __name__
  assert getimportable(squared, alias='memo', byname=False) == 'memo = squared = lambda x:x**2\n'
  assert getimportable(add, alias='memo', byname=False) == 'def add(x,y):\n  return x+y\n\nmemo = add\n'
  assert getimportable(None, alias='memo', byname=False) == 'memo = None\n'
  assert getimportable(100, alias='memo', byname=False) == 'memo = 100\n'
  assert getimportable(add, explicit=True) == 'from %s import add\n' % __name__
  assert getimportable(squared, explicit=True) == 'from %s import squared\n' % __name__
  assert getimportable(Foo, explicit=True) == 'from %s import Foo\n' % __name__
  assert getimportable(Foo.bar, explicit=True) == 'from %s import bar\n' % __name__
  assert getimportable(_foo.bar, explicit=True) == 'from %s import bar\n' % __name__
  assert getimportable(None, explicit=True) == 'None\n'
  assert getimportable(100, explicit=True) == '100\n'
github uqfoundation / klepto / klepto / _archives.py View on Github external
pik.dump(value, f, **kwd)
                    if input:
                        with open(_args, mode) as f:
                            pik.dump(key, f, **kwd)
            else: # try to get an import for the object
                try: memo = getimportable(value, alias='memo', byname=False)
                except AttributeError: #XXX: HACKY... get classes by name
                    memo = getimportable(value, alias='memo')
                #XXX: class instances and such fail... abuse pickle here?
                from .tools import _b
                with open(_file, 'wb') as f:
                    f.write(_b(memo))
                if input:
                    try: memo = getimportable(key, alias='memo', byname=False)
                    except AttributeError:
                        memo = getimportable(key, alias='memo')
                    from .tools import _b
                    with open(_args, 'wb') as f:
                        f.write(_b(memo))
        except OSError:
            "failed to populate directory for '%s'" % str(key)
        # move the results to the proper place
        try: #XXX: possible permissions issues here
            self._rmdir(key) #XXX: 'key' must be a suitable dir name
            os.renames(self._getdir(_key), self._getdir(key))
#       except TypeError: #XXX: catch key that isn't converted to safe filename
#           "error in populating directory for '%s'" % str(key)
        except OSError: #XXX: if rename fails, may need cleanup (_rmdir ?)
            "error in populating directory for '%s'" % str(key)