__init__.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/env python
  2. #
  3. # Author: Mike McKerns (mmckerns @caltech and @uqfoundation)
  4. # Copyright (c) 2008-2016 California Institute of Technology.
  5. # Copyright (c) 2016-2024 The Uncertainty Quantification Foundation.
  6. # License: 3-clause BSD. The full license text is available at:
  7. # - https://github.com/uqfoundation/dill/blob/master/LICENSE
  8. # author, version, license, and long description
  9. try: # the package is installed
  10. from .__info__ import __version__, __author__, __doc__, __license__
  11. except: # pragma: no cover
  12. import os
  13. import sys
  14. parent = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))
  15. sys.path.append(parent)
  16. # get distribution meta info
  17. from version import (__version__, __author__,
  18. get_license_text, get_readme_as_rst)
  19. __license__ = get_license_text(os.path.join(parent, 'LICENSE'))
  20. __license__ = "\n%s" % __license__
  21. __doc__ = get_readme_as_rst(os.path.join(parent, 'README.md'))
  22. del os, sys, parent, get_license_text, get_readme_as_rst
  23. from ._dill import (
  24. dump, dumps, load, loads, copy,
  25. Pickler, Unpickler, register, pickle, pickles, check,
  26. DEFAULT_PROTOCOL, HIGHEST_PROTOCOL, HANDLE_FMODE, CONTENTS_FMODE, FILE_FMODE,
  27. PickleError, PickleWarning, PicklingError, PicklingWarning, UnpicklingError,
  28. UnpicklingWarning,
  29. )
  30. from .session import (
  31. dump_module, load_module, load_module_asdict,
  32. dump_session, load_session # backward compatibility
  33. )
  34. from . import detect, logger, session, source, temp
  35. # get global settings
  36. from .settings import settings
  37. # make sure "trace" is turned off
  38. logger.trace(False)
  39. objects = {}
  40. # local import of dill._objects
  41. #from . import _objects
  42. #objects.update(_objects.succeeds)
  43. #del _objects
  44. # local import of dill.objtypes
  45. from . import objtypes as types
  46. def load_types(pickleable=True, unpickleable=True):
  47. """load pickleable and/or unpickleable types to ``dill.types``
  48. ``dill.types`` is meant to mimic the ``types`` module, providing a
  49. registry of object types. By default, the module is empty (for import
  50. speed purposes). Use the ``load_types`` function to load selected object
  51. types to the ``dill.types`` module.
  52. Args:
  53. pickleable (bool, default=True): if True, load pickleable types.
  54. unpickleable (bool, default=True): if True, load unpickleable types.
  55. Returns:
  56. None
  57. """
  58. from importlib import reload
  59. # local import of dill.objects
  60. from . import _objects
  61. if pickleable:
  62. objects.update(_objects.succeeds)
  63. else:
  64. [objects.pop(obj,None) for obj in _objects.succeeds]
  65. if unpickleable:
  66. objects.update(_objects.failures)
  67. else:
  68. [objects.pop(obj,None) for obj in _objects.failures]
  69. objects.update(_objects.registered)
  70. del _objects
  71. # reset contents of types to 'empty'
  72. [types.__dict__.pop(obj) for obj in list(types.__dict__.keys()) \
  73. if obj.find('Type') != -1]
  74. # add corresponding types from objects to types
  75. reload(types)
  76. def extend(use_dill=True):
  77. '''add (or remove) dill types to/from the pickle registry
  78. by default, ``dill`` populates its types to ``pickle.Pickler.dispatch``.
  79. Thus, all ``dill`` types are available upon calling ``'import pickle'``.
  80. To drop all ``dill`` types from the ``pickle`` dispatch, *use_dill=False*.
  81. Args:
  82. use_dill (bool, default=True): if True, extend the dispatch table.
  83. Returns:
  84. None
  85. '''
  86. from ._dill import _revert_extension, _extend
  87. if use_dill: _extend()
  88. else: _revert_extension()
  89. return
  90. extend()
  91. def license():
  92. """print license"""
  93. print (__license__)
  94. return
  95. def citation():
  96. """print citation"""
  97. print (__doc__[-491:-118])
  98. return
  99. # end of file