Skip to content

Commit e4fdbaa

Browse files
committed
[System] minor refractoring
1 parent e18659d commit e4fdbaa

13 files changed

+106
-167
lines changed

__init__.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from pymfony.component.system.oop import abstract;
1515
from pymfony.component.system.oop import interface;
1616

17-
if sys.version_info[0] == 2:
17+
if sys.version_info < (3,):
1818
from pymfony.component.system.py2 import *;
1919
else:
2020
from pymfony.component.system.py3 import *;
@@ -166,7 +166,7 @@ def __init__(self, instanceOrig):
166166
setattr(self, name, cloneMethod);
167167
else:
168168
setattr(self, name, value);
169-
except AttributeError:
169+
except Exception:
170170
pass;
171171

172172
for classType in instance.__class__.__mro__:
@@ -251,7 +251,7 @@ def isAbstract(cls, obj):
251251

252252
@classmethod
253253
def isCallable(cls, closure):
254-
if isinstance(closure, AbstractString):
254+
if isinstance(closure, basestring):
255255
if '.' in closure:
256256
# Static class method call
257257
try:
@@ -269,7 +269,7 @@ def isCallable(cls, closure):
269269
if len(closure) != 2:
270270
return False;
271271

272-
if not isinstance(closure[1], AbstractString):
272+
if not isinstance(closure[1], basestring):
273273
return False;
274274

275275
if not isinstance(closure[0], object):
@@ -376,7 +376,7 @@ def stripcslashes(cls, string):
376376

377377
class ReflectionClass(Object):
378378
def __init__(self, argument):
379-
if isinstance(argument, AbstractString):
379+
if isinstance(argument, basestring):
380380
qualClassName = argument;
381381
try:
382382
argument = ClassLoader.load(argument);

exception.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ def __createTrace(self):
7070
name = f.f_code.co_name;
7171
argcount = f.f_code.co_argcount;
7272
allLines = linecache.getlines(filename, f.f_globals);
73-
line = allLines[lineno - 1].strip();
73+
if allLines:
74+
line = allLines[lineno - 1].strip();
75+
else:
76+
line = "";
7477

7578
upAndDown = 5;
7679

json.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
import sys;
1212

13-
if sys.version_info <= (2, 6):
14-
from pymfony.component.system.py26.json import *;
15-
elif sys.version_info < (3, 0):
13+
if sys.version_info < (3,):
1614
from pymfony.component.system.py2.json import *;
1715
else:
1816
from pymfony.component.system.py3.json import *;

py2/__init__.py

-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
__all__ = [
2222
'OOPObject',
23-
'AbstractString',
2423
'AbstractCloneBuilder',
2524
];
2625

@@ -38,13 +37,6 @@ def __subclasshook__(cls, subclass):
3837
class OOPObject(object, Abstract):
3938
__metaclass__ = OOPMeta;
4039

41-
class AbstractString(str, OOPObject):
42-
@classmethod
43-
def __subclasshook__(cls, subclass):
44-
if issubclass(subclass, basestring):
45-
return True;
46-
return NotImplemented;
47-
4840
class AbstractCloneBuilder(OOPObject):
4941
TYPES_MAP = {
5042
'int': int,

py2/json.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88

99
from __future__ import absolute_import;
1010

11-
from json import decoder;
12-
from collections import OrderedDict;
11+
import sys
1312

1413
"""
1514
"""
@@ -18,10 +17,18 @@
1817
'AbstractJSONDecoderOrderedDict',
1918
];
2019

21-
class AbstractJSONDecoderOrderedDict(decoder.JSONDecoder):
22-
def __init__(self, parse_float=None, parse_int=None, parse_constant=None,
23-
strict=True):
24-
decoder.JSONDecoder.__init__(self, object_hook=None, parse_float=parse_float, parse_int=parse_int, parse_constant=parse_constant, strict=strict, object_pairs_hook=self.__object_pairs_hook);
20+
if sys.version_info <= (2, 6):
21+
from pymfony.component.system.py2.minor6.json import AbstractJSONDecoderOrderedDict;
22+
else:
23+
from json import JSONDecoder;
24+
from pymfony.component.system.types import OrderedDict
2525

26-
def __object_pairs_hook(self, seq):
27-
return OrderedDict(seq);
26+
class AbstractJSONDecoderOrderedDict(JSONDecoder):
27+
def __init__(self, parse_float=None, parse_int=None,
28+
parse_constant=None, strict=True):
29+
JSONDecoder.__init__(self, object_hook=None, parse_float=parse_float,
30+
parse_int=parse_int, parse_constant=parse_constant,
31+
strict=strict, object_pairs_hook=self.__object_pairs_hook);
32+
33+
def __object_pairs_hook(self, seq):
34+
return OrderedDict(seq);
File renamed without changes.

py26/json.py py2/minor6/json.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from json import decoder;
1212
from json import scanner;
1313

14-
from pymfony.component.system.py26.types import OrderedDict;
14+
from pymfony.component.system.types import OrderedDict;
1515

1616
"""
1717
"""

py26/types.py py2/minor6/types.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
# -*- coding: utf-8 -*-
2+
# This file is part of the pymfony package.
3+
#
4+
# (c) Alexandre Quercia <[email protected]>
5+
#
6+
# For the full copyright and license information, please view the LICENSE
7+
# file that was distributed with this source code.
8+
9+
from __future__ import absolute_import;
10+
11+
from UserDict import DictMixin;
12+
13+
"""
14+
"""
15+
16+
__all__ = [
17+
'AbstractOrderedDict',
18+
];
19+
20+
class AbstractOrderedDict(dict, DictMixin):
221
# Copyright (c) 2009 Raymond Hettinger
322
#
423
# Permission is hereby granted, free of charge, to any person
@@ -20,11 +39,6 @@
2039
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2140
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2241
# OTHER DEALINGS IN THE SOFTWARE.
23-
24-
from UserDict import DictMixin
25-
26-
class OrderedDict(dict, DictMixin):
27-
2842
def __init__(self, *args, **kwds):
2943
if len(args) > 1:
3044
raise TypeError('expected at most 1 arguments, got %d' % len(args))

py2/types.py

+22-117
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,33 @@
11
# -*- coding: utf-8 -*-
2-
# Copyright (c) 2009 Raymond Hettinger
2+
# This file is part of the pymfony package.
33
#
4-
# Permission is hereby granted, free of charge, to any person
5-
# obtaining a copy of this software and associated documentation files
6-
# (the "Software"), to deal in the Software without restriction,
7-
# including without limitation the rights to use, copy, modify, merge,
8-
# publish, distribute, sublicense, and/or sell copies of the Software,
9-
# and to permit persons to whom the Software is furnished to do so,
10-
# subject to the following conditions:
4+
# (c) Alexandre Quercia <[email protected]>
115
#
12-
# The above copyright notice and this permission notice shall be
13-
# included in all copies or substantial portions of the Software.
14-
#
15-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16-
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17-
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18-
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19-
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20-
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21-
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22-
# OTHER DEALINGS IN THE SOFTWARE.
23-
24-
from UserDict import DictMixin
25-
26-
class OrderedDict(dict, DictMixin):
27-
28-
def __init__(self, *args, **kwds):
29-
if len(args) > 1:
30-
raise TypeError('expected at most 1 arguments, got %d' % len(args))
31-
try:
32-
self.__end
33-
except AttributeError:
34-
self.clear()
35-
self.update(*args, **kwds)
36-
37-
def clear(self):
38-
self.__end = end = []
39-
end += [None, end, end] # sentinel node for doubly linked list
40-
self.__map = {} # key --> [key, prev, next]
41-
dict.clear(self)
42-
43-
def __setitem__(self, key, value):
44-
if key not in self:
45-
end = self.__end
46-
curr = end[1]
47-
curr[2] = end[1] = self.__map[key] = [key, curr, end]
48-
dict.__setitem__(self, key, value)
6+
# For the full copyright and license information, please view the LICENSE
7+
# file that was distributed with this source code.
498

50-
def __delitem__(self, key):
51-
dict.__delitem__(self, key)
52-
key, prev, end = self.__map.pop(key)
53-
prev[2] = end
54-
end[1] = prev
9+
from __future__ import absolute_import;
5510

56-
def __iter__(self):
57-
end = self.__end
58-
curr = end[2]
59-
while curr is not end:
60-
yield curr[0]
61-
curr = curr[2]
11+
import sys;
6212

63-
def __reversed__(self):
64-
end = self.__end
65-
curr = end[1]
66-
while curr is not end:
67-
yield curr[0]
68-
curr = curr[1]
13+
from pymfony.component.system import Object;
6914

70-
def popitem(self, last=True):
71-
if not self:
72-
raise KeyError('dictionary is empty')
73-
if last:
74-
key = reversed(self).next()
75-
else:
76-
key = iter(self).next()
77-
value = self.pop(key)
78-
return key, value
15+
if sys.version_info <= (2, 6):
16+
from pymfony.component.system.py2.minor6.types import AbstractOrderedDict;
17+
else:
18+
from collections import OrderedDict as AbstractOrderedDict;
7919

80-
def __reduce__(self):
81-
items = [[k, self[k]] for k in self]
82-
tmp = self.__map, self.__end
83-
del self.__map, self.__end
84-
inst_dict = vars(self).copy()
85-
self.__map, self.__end = tmp
86-
if inst_dict:
87-
return (self.__class__, (items,), inst_dict)
88-
return self.__class__, (items,)
20+
"""
21+
"""
8922

90-
def keys(self):
91-
return list(self)
92-
93-
setdefault = DictMixin.setdefault
94-
update = DictMixin.update
95-
pop = DictMixin.pop
96-
values = DictMixin.values
97-
items = DictMixin.items
98-
iterkeys = DictMixin.iterkeys
99-
itervalues = DictMixin.itervalues
100-
iteritems = DictMixin.iteritems
101-
102-
def __repr__(self):
103-
if not self:
104-
return '%s()' % (self.__class__.__name__,)
105-
return '%s(%r)' % (self.__class__.__name__, self.items())
106-
107-
def copy(self):
108-
return self.__class__(self)
23+
__all__ = [
24+
'AbstractString',
25+
'AbstractOrderedDict',
26+
];
10927

28+
class AbstractString(str, Object):
11029
@classmethod
111-
def fromkeys(cls, iterable, value=None):
112-
d = cls()
113-
for key in iterable:
114-
d[key] = value
115-
return d
116-
117-
def __eq__(self, other):
118-
if isinstance(other, OrderedDict):
119-
if len(self) != len(other):
120-
return False
121-
for p, q in zip(self.items(), other.items()):
122-
if p != q:
123-
return False
124-
return True
125-
return dict.__eq__(self, other)
126-
127-
def __ne__(self, other):
128-
return not self == other
30+
def __subclasshook__(cls, subclass):
31+
if issubclass(subclass, basestring):
32+
return True;
33+
return NotImplemented;

py3/__init__.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
__all__ = [
1818
'OOPObject',
19-
'AbstractString',
19+
'basestring',
2020
'AbstractCloneBuilder',
2121
];
2222

23+
basestring = str;
24+
2325
class Abstract():
2426
__abstractclass__ = None;
2527
__isfinalclass__ = False;
@@ -33,13 +35,6 @@ def __subclasshook__(cls, subclass):
3335
class OOPObject(Abstract, metaclass=OOPMeta):
3436
pass;
3537

36-
class AbstractString(str, OOPObject):
37-
@classmethod
38-
def __subclasshook__(cls, subclass):
39-
if issubclass(subclass, str):
40-
return True;
41-
return NotImplemented;
42-
4338
class AbstractCloneBuilder(OOPObject):
4439
TYPES_MAP = {
4540
'int': int,
@@ -56,4 +51,3 @@ class AbstractCloneBuilder(OOPObject):
5651
'dict': dict,
5752
'bool': bool,
5853
};
59-

py3/json.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
# For the full copyright and license information, please view the LICENSE
77
# file that was distributed with this source code.
88

9-
from __future__ import absolute_import;
9+
from json import JSONDecoder;
1010

11-
from json import decoder;
12-
from collections import OrderedDict;
11+
from pymfony.component.system.types import OrderedDict
1312

1413
"""
1514
"""
@@ -18,10 +17,10 @@
1817
'AbstractJSONDecoderOrderedDict',
1918
];
2019

21-
class AbstractJSONDecoderOrderedDict(decoder.JSONDecoder):
20+
class AbstractJSONDecoderOrderedDict(JSONDecoder):
2221
def __init__(self, parse_float=None, parse_int=None, parse_constant=None,
2322
strict=True):
24-
decoder.JSONDecoder.__init__(self, object_hook=None, parse_float=parse_float, parse_int=parse_int, parse_constant=parse_constant, strict=strict, object_pairs_hook=self.__object_pairs_hook);
23+
JSONDecoder.__init__(self, object_hook=None, parse_float=parse_float, parse_int=parse_int, parse_constant=parse_constant, strict=strict, object_pairs_hook=self.__object_pairs_hook);
2524

2625
def __object_pairs_hook(self, seq):
2726
return OrderedDict(seq);

0 commit comments

Comments
 (0)