-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.py
executable file
·162 lines (145 loc) · 4.67 KB
/
base.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/python3
"""Module base"""
import json
import csv
import turtle
class Base():
"""Defines a base class"""
__nb_objects = 0
def __init__(self, id=None):
"""Method that assign the public instance attribute id
Args:
id(int): integer value to manage id in this project
Return:
Always nothing.
"""
if id is not None:
self.id = id
else:
Base.__nb_objects += 1
self.id = Base.__nb_objects
@staticmethod
def to_json_string(list_dictionaries):
"""Method that returns the JSON
string representation
Args:
list_dictionaries(dict): List of dictionaries
Return:
JSON string
"""
if list_dictionaries is None or list_dictionaries == []:
return "[]"
return json.dumps(list_dictionaries)
@classmethod
def save_to_file(cls, list_objs):
""" Method that writes the JSON string representation
of list_objs to a file
Args:
list_objs(list): List of objects
Return:
Always nothing
"""
obj_list = []
if list_objs:
for obj in list_objs:
json_dict = obj.to_dictionary()
obj_list.append(json_dict)
json_str = cls.to_json_string(obj_list)
with open(f"{cls.__name__}.json", 'w') as json_file:
json_file.write(json_str)
@classmethod
def save_to_file_csv(cls, dict_list):
"""Method that serializes in CSV
Args:
list_objs(list): List of objects
Return:
Always nothing
"""
obj_list = []
for instance in dict_list:
obj_list.append(instance.to_dictionary())
with open(f"{cls.__name__}.csv", 'w') as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=obj_list[0].keys())
writer.writeheader()
writer.writerows(obj_list)
@staticmethod
def from_json_string(json_string):
"""Method that returns the list of the
JSON string representation
Args:
json_string: JSON string
Return:
Python object
"""
if json_string is None or json_string == '':
return []
return json.loads(json_string)
@classmethod
def create(cls, **dictionary):
"""Update the class Base and returns a instance with all
attributes already set
Args:
dictionary: Dictionary with all attributes of the object
Return:
A instance with all attributes already set
"""
if cls.__name__ == 'Rectangle':
dummy = cls(2, 3)
elif cls.__name__ == 'Square':
dummy = cls(2)
dummy.update(**dictionary)
return dummy
@classmethod
def load_from_file(cls):
"""Method that returns a list of instances
- the type of these instances depends on cls
"""
l_instances = []
try:
with open(f"{cls.__name__}.json", 'r') as r_instances:
json_des = cls.from_json_string(r_instances.read())
except FileNotFoundError:
return []
for obj in json_des:
l_instances.append(cls.create(**obj))
return l_instances
@classmethod
def load_from_file_csv(cls):
"""Method that deserializes in CSV
"""
obj_list = []
try:
with open(f"{cls.__name__}.csv") as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
row = dict(row)
for key in row:
row[key] = int(row[key])
instance = cls.create(**row)
obj_list.append(instance)
except FileNotFoundError:
return obj_list
return obj_list
@staticmethod
def draw(list_rectangles, list_squares):
"""Draw Rectangles and Squares using the turtle module.
Args:
list_rectangles (list): A list of Rectangle objects to draw.
list_squares (list): A list of Square objects to draw.
"""
window = turtle.Screen()
pen = turtle.Pen()
figures = list_rectangles + list_squares
for fig in figures:
pen.up()
pen.goto(fig.x, fig.y)
pen.down()
pen.forward(fig.width)
pen.right(90)
pen.forward(fig.height)
pen.right(90)
pen.forward(fig.width)
pen.right(90)
pen.forward(fig.height)
pen.right(90)
window.exitonclick()