-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlesson28.py
63 lines (42 loc) · 1.19 KB
/
lesson28.py
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
import os
class Reader(object):
def __init__(self, filename):
self.filename = filename
def read(self):
print "read from this {}".format(self.__class__.__name__)
def set_filename(self, filename):
self.filename = filename
return self
def set_chunk_size(self, size):
self.size = size
return self
class TextReader(Reader):
pass
# def read(self):
# #TODO reading from text
# print "read from this {}".format(self.__class__.__name__)
#
class BinaryReader(Reader):
pass
def fab_method(filename):
_, ext = os.path.splitext(filename)
if ext == '.bin':
return BinaryReader
elif ext == '.txt':
return TextReader
reader = fab_method('name.bin')
read_instance = reader('name.bin')
read_instance.read()
class Singleton(type):
instances = {}
# TODO implement singleton
def __call__(self, *args, **kwargs):
return self
class Config(object):
__metaclass__ = Singleton
def __init__(self, a):
self.a = a
print "Initialized config"
singleton = Config(2) # instance of Singleton will be returned
singleton1 = Config(5)
print singleton, singleton1