-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasepart.py
123 lines (110 loc) · 4.38 KB
/
basepart.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""Base part of language set"""
import collections
import random
DEFAULT_STORAGE = 'name.storage'
class NamingLibException(Exception):
"""Basic exception class for naming"""
pass
class AutoMemberSetType(type):
"""This metaclass automatically set the arguments and change it into list"""
def __call__(cls, *args, **kwargs):
# Initialize
elem = type.__call__(cls, *args, **kwargs)
name_arg = elem.__init__.__func__.__code__.co_varnames[1:]
defaults = elem.__init__.__func__.__defaults__
# Write the default arguments and user-defined arguments with *args
for (name, value) in zip(name_arg, args + defaults[len(args):]):
setattr(elem, name, value)
# Write the user-defined arguments with **kwargs
for (name, value) in kwargs.items():
setattr(elem, name, value)
return elem
class AutoMemberListSetType(AutoMemberSetType):
"""This metaclass automatically set the arguments and change it into list"""
def __call__(cls, *args, **kwargs):
# Initialize
elem = type.__call__(cls, *args, **kwargs)
name_arg = elem.__init__.__func__.__code__.co_varnames[1:]
defaults = elem.__init__.__func__.__defaults__
# Write the default arguments and user-defined arguments with *args
buf_dict = collections.OrderedDict()
if defaults is None:
defaults = tuple()
for (name, value) in zip(name_arg, args + defaults[len(args):]):
buf_dict[name] = cls.__list(value)
# Write the user-defined arguments with **kwargs
for (name, value) in kwargs.items():
buf_dict[name] = cls.__list(value)
setattr(elem, 'element', list(buf_dict.values()))
return elem
#@staticmethod
def __list(cls, elem):
if isinstance(elem, tuple) or isinstance(elem, list):
return list(elem)
elif isinstance(elem, str) or isinstance(elem, int):
return [elem]
elif elem is None:
return list()
else:
raise NamingLibException('Wrong type for __list')
class SimplifiedElementBase(metaclass=AutoMemberSetType):
"""Base element class"""
element = None
def __init__(self, element=None):
pass
def __repr__(self):
return '<{}: {}>'.format('SElement', self.element)
class ListBase(SimplifiedElementBase, metaclass=AutoMemberListSetType):
"""Base class of customed list class"""
element = list()
def digitize(self, list_compare):
"""Digitize the list"""
# pylint: disable=unused-variable
for (idx, elem) in enumerate(self.element[0]):
if elem in list_compare:
self.element[0][idx] = list_compare.index(elem)
return True
def characterize(self, list_compare):
# pylint: disable=unused-variable
"""Characterize the list"""
for (idx, elem) in enumerate(self.element[0]):
if elem in range(len(list_compare)):
self.element[0][idx] = list_compare[idx]
return True
def check_element_index(self, list_compare):
"""Check whether number in list is out of range"""
for elem in (self.element)[0]:
if not elem in range(len(list_compare)):
raise NamingLibException('Character index - out of range')
return True
def choice(self, list_input):
"""Basic method of choose"""
pass
class IncludeList(ListBase):
"""Use this class to include"""
def choice(self, list_input=None):
"""Choose one from list. You don't have to use list_input argument"""
return random.choice(self.element[0])
class ExcludeList(ListBase):
"""Use this class to exclude"""
def choice(self, list_input):
"""Choose one and exclude elements from list_compare"""
if self.element[0] == list(range(len(list_input))):
raise NamingLibException('All letters are excluded')
while True:
elem = random.randrange(len(list_input))
if elem in self.element[0]:
continue
else:
break
return elem
class ComposerElementBase(metaclass=AutoMemberSetType):
"""Base class of element composer"""
# Please revise with metaclass
character = None
argument = tuple()
result = None
#@abc.abstractmethod
def compose(self):
"""Compose the name"""
pass