PyVRML97 2.3.4b3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
vrml/weaklist.py ADDED
@@ -0,0 +1,163 @@
1
+ """list sub-class which holds weak references to objects"""
2
+
3
+ import weakref
4
+ import types
5
+
6
+ class WeakList( list ):
7
+ """list sub-class holding weakrefs to items
8
+
9
+ The weak reference list is intended to allow you
10
+ to store references to a list of objects without
11
+ needing to manage weak references directly.
12
+
13
+ For the most part, the WeakList operates just
14
+ like a list object, in that it allows for all
15
+ of the standard list operations. The difference
16
+ is that the WeakList class only stores weak
17
+ references to its items. As a result, adding
18
+ an object to the list does not necessarily mean
19
+ that it will still be there later on during
20
+ execution (if the referent has been garbage
21
+ collected).
22
+ """
23
+ def __init__( self, sequence=() ):
24
+ """Initialize the list, with an optional sequence of objects
25
+
26
+ The WeakList will store weak references to objects
27
+ within the sequence.
28
+ """
29
+ super( WeakList, self).__init__( map( self.wrap, sequence))
30
+ def wrap( self, item ):
31
+ """Wrap an individual item in a weak-reference
32
+
33
+ If the item is already a weak reference, we store
34
+ a reference to the original item. We use approximately
35
+ the same weak reference callback mechanism as the
36
+ standard weakref.WeakKeyDictionary object.
37
+ """
38
+ if isinstance( item, weakref.ReferenceType ):
39
+ item = item()
40
+ return weakref.ref( item, self.__remover() )
41
+ def unwrap( self, item ):
42
+ """Unwrap an individual item
43
+
44
+ This is a fairly trivial operation at the moment,
45
+ it merely calls the item with no arguments and
46
+ returns the result.
47
+ """
48
+ return item()
49
+
50
+ def get( self ):
51
+ """Get all items as a list of strong references
52
+ """
53
+ return [
54
+ self.unwrap(obj)
55
+ for obj in super( WeakList,self).__getslice__(0,len(self))
56
+ ]
57
+ def __iter__( self ):
58
+ """Iterate over the list, yielding strong references"""
59
+ index = 0
60
+ while index < len(self):
61
+ yield self[index]
62
+ index += 1
63
+
64
+ def __setitem__( self, index, item ):
65
+ """Set the item at the given index"""
66
+ if isinstance( index, slice ):
67
+ item = [self.wrap(x) for x in item]
68
+ else:
69
+ item = self.wrap(x)
70
+ return super( WeakList,self).__setitem__(
71
+ index, item
72
+ )
73
+
74
+ def append( self, item ):
75
+ """Append a single item to the list"""
76
+ return super( WeakList,self).append( self.wrap(item))
77
+ def insert( self, index, item ):
78
+ """Insert an item at the given index"""
79
+ return super( WeakList,self).insert(
80
+ index, self.wrap(item)
81
+ )
82
+ def extend( self, sequence ):
83
+ """Extend this list with another sequence"""
84
+ return super( WeakList, self).extend([
85
+ self.wrap(obj) for obj in sequence
86
+ ])
87
+ __iadd__ = extend
88
+
89
+ def __getitem__( self, index ):
90
+ """Get the item at the given index"""
91
+ return self.unwrap(super (WeakList,self).__getitem__( index))
92
+ def pop( self, index=-1 ):
93
+ """Pop an item from the list, removing it and returning it"""
94
+ return self.unwrap( super(WeakList,self).pop(index))
95
+
96
+ def __contains__( self, item ):
97
+ """Return boolean indicating whether the item is in the list"""
98
+ return item in self.get()
99
+ def count( self, item ):
100
+ """Return integer count of instances of item in list"""
101
+ return self.get().count(item)
102
+ def index( self, item ):
103
+ """Return integer index of item in list"""
104
+ return self.get().index(item)
105
+ def remove( self, item ):
106
+ """Remove the given item from the list"""
107
+ t = self.get()
108
+ result = t.remove( item )
109
+ self[:] = t
110
+ return result
111
+ def sort( self, function = None):
112
+ """Sort the list of objects
113
+
114
+ This sorts the objects referenced,
115
+ then rebuilds the list of references!
116
+ """
117
+ t = self.get()
118
+ if function is not None:
119
+ result = t.sort( function )
120
+ else:
121
+ result = t.sort( )
122
+ self[:] = t
123
+ return result
124
+ def __eq__( self, sequence ):
125
+ """Compare the list to another (==)"""
126
+ return self.get() == sequence
127
+ def __ge__( self, sequence ):
128
+ """Compare the list to another (>=)"""
129
+ return self.get() >= sequence
130
+ def __gt__( self, sequence ):
131
+ """Compare the list to another (>)"""
132
+ return self.get() > sequence
133
+
134
+ def __le__( self, sequence ):
135
+ """Compare the list to another (<=)"""
136
+ return self.get() <= sequence
137
+ def __lt__( self, sequence ):
138
+ """Compare the list to another (<)"""
139
+ return self.get() < sequence
140
+
141
+ def __ne__( self, sequence ):
142
+ """Compare the list to another (!=)"""
143
+ return self.get() != sequence
144
+
145
+ def __repr__( self ):
146
+ """Return a code-like representation of the weak list"""
147
+ return """%s( %s )"""%( self.__class__.__name__, repr(self.get()))
148
+
149
+ def __remover(self):
150
+ """Construct a function callback for eliminating a particular reference"""
151
+ def remove(reference, selfref=weakref.ref(self)):
152
+ """Removes passed reference from the referenced self (selfref)
153
+ Note that the callback does not keep the list alive.
154
+ This approach is taken directly from the WeakKeyDictionary.
155
+ """
156
+ self = selfref()
157
+ if self is not None:
158
+ try:
159
+ super( WeakList, self).remove( reference )
160
+ except (ValueError, TypeError, NameError):
161
+ pass
162
+ return remove
163
+
vrml/weaktuple.py ADDED
@@ -0,0 +1,138 @@
1
+ """tuple sub-class which holds weak references to objects"""
2
+
3
+ import weakref
4
+
5
+ class WeakTuple( tuple ):
6
+ """tuple sub-class holding weakrefs to items
7
+
8
+ The weak reference tuple is intended to allow you
9
+ to store references to a list of objects without
10
+ needing to manage weak references directly.
11
+
12
+ For the most part, the WeakTuple operates just
13
+ like a tuple object, in that it allows for all
14
+ of the standard tuple operations. The difference
15
+ is that the WeakTuple class only stores weak
16
+ references to its items. As a result, adding
17
+ an object to the tuple does not necessarily mean
18
+ that it will still be there later on during
19
+ execution (if the referent has been garbage
20
+ collected).
21
+
22
+ Because WeakTuple's are static (their membership
23
+ doesn't change), they will raise ReferenceError
24
+ when a sub-item is missing rather than skipping
25
+ missing items as does the WeakList. This can
26
+ occur for basically _any_ use of the tuple.
27
+ """
28
+ def __init__( self, sequence=() ):
29
+ """Initialize the tuple
30
+
31
+ The WeakTuple will store weak references to objects
32
+ within the sequence.
33
+ """
34
+ super( WeakTuple, self).__init__( [self.wrap(obj) for obj in sequence])
35
+
36
+ def valid( self ):
37
+ """Explicit validity check for the tuple
38
+
39
+ Checks whether all references can be resolved,
40
+ basically just sees whether calling list(self)
41
+ raises a ReferenceError
42
+ """
43
+ try:
44
+ list( self )
45
+ return 1
46
+ except weakref.ReferenceError:
47
+ return 0
48
+
49
+ def wrap( self, item ):
50
+ """Wrap an individual item in a weak-reference
51
+
52
+ If the item is already a weak reference, we store
53
+ a reference to the original item. We use approximately
54
+ the same weak reference callback mechanism as the
55
+ standard weakref.WeakKeyDictionary object.
56
+ """
57
+ if isinstance( item, weakref.ReferenceType ):
58
+ item = item()
59
+ return weakref.ref( item )
60
+ def unwrap( self, item ):
61
+ """Unwrap an individual item
62
+
63
+ This is a fairly trivial operation at the moment,
64
+ it merely calls the item with no arguments and
65
+ returns the result.
66
+ """
67
+ ref = item()
68
+ if ref is None:
69
+ raise weakref.ReferenceError( """%s instance no longer valid (item %s has been collected)"""%( self.__class__.__name__, item))
70
+ return ref
71
+
72
+ def __iter__( self ):
73
+ """Iterate over the tuple, yielding strong references"""
74
+ index = 0
75
+ while index < len(self):
76
+ yield self[index]
77
+ index += 1
78
+
79
+ def __getitem__( self, index ):
80
+ """Get the item at the given index"""
81
+ return self.unwrap(super (WeakTuple,self).__getitem__( index ))
82
+ def __getslice__( self, start, stop ):
83
+ """Get the items in the range start to stop"""
84
+ return [
85
+ self.unwrap(obj)
86
+ for obj in super (WeakTuple,self).__getslice__( start, stop)
87
+ ]
88
+ def __contains__( self, item ):
89
+ """Return boolean indicating whether the item is in the tuple"""
90
+ for node in self:
91
+ if item is node:
92
+ return 1
93
+ return 0
94
+ def count( self, item ):
95
+ """Return integer count of instances of item in tuple"""
96
+ count = 0
97
+ for node in self:
98
+ if item is node:
99
+ count += 1
100
+ return count
101
+ def index( self, item ):
102
+ """Return integer index of item in tuple"""
103
+ count = 0
104
+ for node in self:
105
+ if item is node:
106
+ return count
107
+ count += 1
108
+ return -1
109
+
110
+ def __add__(self, other):
111
+ """Return a new path with other as tail"""
112
+ return tuple(self) + other
113
+
114
+ def __eq__( self, sequence ):
115
+ """Compare the tuple to another (==)"""
116
+ return list(self) == sequence
117
+ def __ge__( self, sequence ):
118
+ """Compare the tuple to another (>=)"""
119
+ return list(self) >= sequence
120
+ def __gt__( self, sequence ):
121
+ """Compare the tuple to another (>)"""
122
+ return list(self) > sequence
123
+
124
+ def __le__( self, sequence ):
125
+ """Compare the tuple to another (<=)"""
126
+ return list(self) <= sequence
127
+ def __lt__( self, sequence ):
128
+ """Compare the tuple to another (<)"""
129
+ return list(self) < sequence
130
+
131
+ def __ne__( self, sequence ):
132
+ """Compare the tuple to another (!=)"""
133
+ return list(self) != sequence
134
+
135
+ def __repr__( self ):
136
+ """Return a code-like representation of the weak tuple"""
137
+ return """%s( %s )"""%( self.__class__.__name__, super(WeakTuple,self).__repr__())
138
+