Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3f9c59e

Browse files
committed
Restructured code a little.
1 parent5e8334e commit3f9c59e

File tree

10 files changed

+573
-556
lines changed

10 files changed

+573
-556
lines changed

‎abstract/abstract.py‎

Lines changed: 5 additions & 325 deletions
Original file line numberDiff line numberDiff line change
@@ -1,326 +1,6 @@
11
"""Provides immutable abstract data structures for use in python."""
2-
importcopy
3-
4-
fromimmutableimportImmutable
5-
6-
7-
COLLECTIONS= (list,set)
8-
9-
10-
classQueue(Immutable):
11-
"""Immutable queue.
12-
13-
Attributes:
14-
_list (list): queue's implementation.
15-
"""
16-
def__init__(self,queue=NotImplemented,*args):
17-
super(Queue,self).__init__()
18-
19-
iftype(queue)notinCOLLECTIONSandqueueisnotNotImplemented:
20-
self._list= [queue]+list(args)
21-
22-
else:
23-
self._list=copy.copy(queue)ifqueueisnotNotImplementedelse []
24-
25-
defenqueue(self,obj):
26-
returnQueue(self._list+ [obj])
27-
28-
defdequeue(self):
29-
returnQueue(self._list[1:])
30-
31-
deftop(self):
32-
returnself._list[0]
33-
34-
def__len__(self):
35-
returnlen(self._list)
36-
37-
def__str__(self):
38-
returnstr(self._list)
39-
40-
def__iter__(self):
41-
returniter(self._list)
42-
43-
44-
classStack(Immutable):
45-
"""Immutable stack.
46-
47-
Attributes:
48-
_list (list): stacks's implementation.
49-
"""
50-
def__init__(self,stack=NotImplemented,*args):
51-
super(Stack,self).__init__()
52-
iftype(stack)notinCOLLECTIONSandstackisnotNotImplemented:
53-
self._list= [stack]+list(args)
54-
55-
else:
56-
self._list=copy.copy(stack)ifstackisnotNotImplementedelse []
57-
58-
defpush(self,obj):
59-
returnStack([obj]+self._list)
60-
61-
defpop(self):
62-
returnStack(self._list[1:])
63-
64-
defhead(self):
65-
returnself._list[0]
66-
67-
def__len__(self):
68-
returnlen(self._list)
69-
70-
def__str__(self):
71-
returnstr(self._list)
72-
73-
def__iter__(self):
74-
returniter(self._list)
75-
76-
77-
classList(Immutable):
78-
"""Immutable list.
79-
80-
Attributes:
81-
_list (list): list's implementation.
82-
"""
83-
def__init__(self,_list=NotImplemented,*args):
84-
super(List,self).__init__()
85-
iftype(_list)notinCOLLECTIONSand_listisnotNotImplemented:
86-
self._list= [_list]+list(args)
87-
88-
else:
89-
self._list=copy.copy(_list)if_listisnotNotImplementedelse []
90-
91-
defappend(self,p_object):
92-
returnList(self._list+ [p_object])
93-
94-
defcount(self,value):
95-
returnself._list.count(value)
96-
97-
defindex(self,value,start=None,stop=None):
98-
forvar,indexinenumerate(self._list[start:stop]):
99-
ifvar==value:
100-
returnindex
101-
102-
definsert(self,index,p_object):
103-
returnList(self._list[:index]+ [p_object]+self._list[index:])
104-
105-
defpop(self,index=None):
106-
index=indexifindexisnotNoneelse0
107-
returnList([itemfor (idx,item)inenumerate(self._list)ifidx!=
108-
index])
109-
110-
defremove(self,value):
111-
returnList([itemforiteminself._listifitem!=value])
112-
113-
defextend(self,iterable):
114-
returnList(self._list+list(iterable))
115-
116-
defreverse(self):
117-
returnList(self._list[::-1])
118-
119-
defsort(self,compareable=None,key=None,reverse=False):
120-
returnList(sorted(self._list),compareable,key,reverse)
121-
122-
def__iter__(self):
123-
returniter(self._list)
124-
125-
def__add__(self,other):
126-
returnList(self._list+list(other))
127-
128-
def__mul__(self,other):
129-
returnList(self._list*other)
130-
131-
def__contains__(self,item):
132-
returniteminself._list
133-
134-
def__getitem__(self,item):
135-
returnself._list[item]
136-
137-
def__len__(self):
138-
returnlen(self._list)
139-
140-
def__str__(self):
141-
returnstr(self._list)
142-
143-
144-
classDictionary(Immutable):
145-
"""Immutable dictionary.
146-
147-
Attributes:
148-
_dict (dict): dictionary's implementation.
149-
"""
150-
def__init__(self,var=NotImplemented):
151-
super(Dictionary,self).__init__()
152-
iftype(var)isdict:
153-
self._dict=var.copy()
154-
155-
elifisinstance(var, (list,tuple,List)):
156-
# We'll assume we've got a list of tuples
157-
self._dict=dict(list(var))
158-
159-
elifvarisNotImplementedorvarisNone:
160-
self._dict=dict()
161-
162-
else:
163-
raiseTypeError("Couldn't instansiate Dictionary object with "
164-
"variable of class %s "%type(var).__name__)
165-
166-
defclear(self):
167-
returnDictionary()
168-
169-
defcopy(self):
170-
returnDictionary(copy.deepcopy(self._dict))
171-
172-
# noinspection PyMethodOverriding
173-
@classmethod
174-
deffromkeys(cls,keys,values=None):
175-
returncls(dict.fromkeys(keys,values))
176-
177-
defget(self,key,default=None):
178-
returnself._dict.get(key,default)
179-
180-
defhas_key(self,key):
181-
returnkeyinself._dict
182-
183-
defitems(self):
184-
returncopy.deepcopy(self._dict).items()
185-
186-
defitervalues(self):
187-
returniter(copy.deepcopy(self._dict).values())
188-
189-
defiteritems(self):
190-
returncopy.deepcopy(self._dict).iteritems()
191-
192-
defiterkeys(self):
193-
returniter(copy.deepcopy(self._dict).keys())
194-
195-
defto_list(self):
196-
return [(k,v)for (k,v)inself._dict.iteritems()]
197-
198-
def__iter__(self):
199-
returnself.iterkeys()
200-
201-
def__len__(self):
202-
returnlen(self._dict.keys())
203-
204-
def__getitem__(self,item):
205-
returnself.get(item)
206-
207-
def__contains__(self,item):
208-
returnself.has_key(item)
209-
210-
def__add__(self,other):
211-
ifisinstance(other,Dictionary):
212-
returnDictionary(self._dict.copy()).update(other)
213-
214-
elifisinstance(other,dict):
215-
returnDictionary(self._dict.copy()).update(other)
216-
217-
else:
218-
raiseTypeError("Cannot add Dictionary to %s"%
219-
type(other).__name__)
220-
221-
def__str__(self):
222-
returnstr(self._dict)
223-
224-
defappend(self,key,value):
225-
returnDictionary(self._dict).update(Dictionary({key:value}))
226-
227-
defupdate(self,other=None):
228-
iftype(other)isdict:
229-
cpy=self._dict.copy()
230-
cpy.update(other)
231-
returnDictionary(cpy)
232-
233-
eliftype(other)isDictionary:
234-
returnself.update(dict(other.items()))
235-
236-
else:
237-
raiseTypeError("cannot update Dictionary and %s"%type(other))
238-
239-
defviewvalues(self):
240-
returnself._dict.viewvalues()
241-
242-
defviewkeys(self):
243-
returnself._dict.viewkeys()
244-
245-
defviewitems(self):
246-
returnself._dict.viewitems()
247-
248-
defsetdefault(self,key,default=None):
249-
ifkeyinself:
250-
returnself
251-
252-
else:
253-
returnDictionary(self._dict).append(key,default)
254-
255-
defpop(self,k):
256-
returnDictionary(dict([(key,value)for (key,value)in
257-
self._dict.items()ifkey!=k]))
258-
259-
260-
classSet(Immutable):
261-
"""Immutable set.
262-
263-
Attributes:
264-
_list (list): set's implementation.
265-
"""
266-
def__init__(self,_list=NotImplemented,*args):
267-
super(Set,self).__init__()
268-
iftype(_list)notinCOLLECTIONSand_listisnotNotImplemented:
269-
self._list=list(set([_list]+list(args)))
270-
271-
else:
272-
self._list=list(set(copy.copy(_list)
273-
if_listisnotNotImplementedelse []))
274-
275-
defadd(self,p_object):
276-
returnSet(list(set(self._list+ [p_object])))
277-
278-
defcount(self,value):
279-
returnself._list.count(value)
280-
281-
defpop(self):
282-
index=0
283-
returnSet([itemfor (idx,item)inenumerate(self._list)
284-
ifidx!=index])
285-
286-
defremove(self,value):
287-
ifvaluenotinself:
288-
raiseKeyError
289-
290-
returnself.discard(value)
291-
292-
defdiscard(self,value):
293-
returnSet([itemforiteminself._listifitem!=value])
294-
295-
defupdate(self,iterable):
296-
returnSet(list(set(self._list+list(iterable))))
297-
298-
def__iter__(self):
299-
returniter(self._list)
300-
301-
def__add__(self,other):
302-
returnSet(list(set(self._list+list(other))))
303-
304-
def__contains__(self,item):
305-
returniteminself._list
306-
307-
def__getitem__(self,item):
308-
returnself._list[item]
309-
310-
def__len__(self):
311-
returnlen(self._list)
312-
313-
def__str__(self):
314-
returnstr(self._list)
315-
316-
def__xor__(self,other):
317-
returnSet(set(self._list).symmetric_difference(other))
318-
319-
def__or__(self,other):
320-
returnself.update(other)
321-
322-
def__and__(self,other):
323-
returnSet(set(self._list).intersection(other))
324-
325-
def__sub__(self,other):
326-
returnSet(set(self._list).difference(other))
2+
fromsetimportSet
3+
fromlistimportList
4+
fromstackimportStack
5+
fromqueueimportQueue
6+
fromdictionaryimportDictionary

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp