File: repository.py

package info (click to toggle)
pyepl 1.1.0-3.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,120 kB
  • sloc: cpp: 7,986; python: 6,026; makefile: 360; ansic: 132
file content (90 lines) | stat: -rw-r--r-- 2,349 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# PyEPL: repository.py
#
# Copyright (C) 2003-2005 Michael J. Kahana
# Authors: Ian Schleifer, Per Sederberg, Aaron Geller, Josh Jacobs
# URL: http://memory.psych.upenn.edu/programming/pyepl
#
# Distributed under the terms of the GNU Lesser General Public License
# (LGPL). See the license.txt that came with this file.

"""
The repository module.
"""

import weakref

nopickle = []

class WeakRef:
    """
    An extension of the ref class that can be pickled.
    """
    def __init__(self, *targs, **dargs):
        """
        """
        self.r = weakref.ref(*targs, **dargs)
    def __getattr__(self, name):
        """
        """
        return getattr(self.r, name)
    def __getstate__(self):
        """
        """
        return self.r()
    def __setstate__(self, state):
        """
        """
        self.r = weakref.ref(state)

class WeakKeyDictionary(weakref.WeakKeyDictionary):
    """
    An extension of the WeakKeyDictionary class that can be pickled.
    """
    def __getstate__(self):
        """
        Returns a regular dictionary version of the WeakKeyDictionary.
        """
        global nopickle
        d = dict(self)
        for key, value in d.items():
            if type(key) in nopickle:
                del d[key]
        return d
    def __setstate__(self, state):
        """
        Constructs WeakKeyDictionary from state dictionary.
        """
        weakref.WeakKeyDictionary.__init__(self, state)

class WeakValueDictionary(weakref.WeakValueDictionary):
    """
    An extension of the WeakValueDictionary class that can be pickled.
    """
    def __getstate__(self):
        """
        Returns a regular dictionary version of the WeakKeyDictionary.
        """
        global nopickle
        d = dict(self)
        for key, value in d.items():
            if type(value) in nopickle:
                del self[key]
        return dict(self)
    def __setstate__(self, state):
        """
        Constructs WeakKeyDictionary from state dictionary.
        """
        weakref.WeakValueDictionary.__init__(self, state)

class MethodCallback:
    """
    """
    def __init__(self, m):
        """
        """
        self.name = m.im_func.__name__
        self.obj = m.im_self
    def __call__(self, *targs, **dargs):
        """
        """
        getattr(self.obj, self.name)(*targs, **dargs)