Skip to content

Commit 3492b60

Browse files
authored
Merge pull request faif#161 from duboviy/master
Add six library usage for cross-version compatibility; Resolve issue faif#117
2 parents 81caecd + 055f962 commit 3492b60

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ cache:
1818

1919
install:
2020
- travis_retry pip install -q coveralls codecov
21-
- pip install flake8 # eventually worth
21+
- pip install flake8 six # eventually worth
2222

2323
script:
2424
# Run tests

flyweight.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,31 @@
55

66
import weakref
77

8+
from six import add_metaclass
9+
810

911
class FlyweightMeta(type):
12+
1013
def __new__(mcs, name, parents, dct):
1114
"""
15+
Set up object pool
1216
1317
:param name: class name
1418
:param parents: class parents
1519
:param dct: dict: includes class attributes, class methods,
1620
static methods, etc
1721
:return: new class
1822
"""
19-
20-
# set up instances pool
2123
dct['pool'] = weakref.WeakValueDictionary()
2224
return super(FlyweightMeta, mcs).__new__(mcs, name, parents, dct)
2325

2426
@staticmethod
2527
def _serialize_params(cls, *args, **kwargs):
26-
"""Serialize input parameters to a key.
28+
"""
29+
Serialize input parameters to a key.
2730
Simple implementation is just to serialize it as a string
28-
2931
"""
30-
args_list = map(str, args)
32+
args_list = list(map(str, args))
3133
args_list.extend([str(kwargs), cls.__name__])
3234
key = ''.join(args_list)
3335
return key
@@ -65,20 +67,15 @@ def __repr__(self):
6567
return "<Card: %s%s>" % (self.value, self.suit)
6668

6769

70+
@add_metaclass(FlyweightMeta)
6871
class Card2(object):
69-
__metaclass__ = FlyweightMeta
7072

7173
def __init__(self, *args, **kwargs):
7274
# print('Init {}: {}'.format(self.__class__, (args, kwargs)))
7375
pass
7476

7577

7678
if __name__ == '__main__':
77-
import sys
78-
if sys.version_info[0] > 2:
79-
sys.stderr.write("!!! This example is compatible only with Python 2 ATM !!!\n")
80-
raise SystemExit(0)
81-
8279
# comment __new__ and uncomment __init__ to see the difference
8380
c1 = Card('9', 'h')
8481
c2 = Card('9', 'h')

0 commit comments

Comments
 (0)