-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
108 lines (83 loc) · 2.93 KB
/
utils.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
import os
import json
from typing import Union
from Classes import person
def clear():
os.system('clear' if os.name == 'posix' else 'cls')
def contact_display(contact: object) -> str:
socials = ""
for social in contact.get_socials():
socials += f"""
==========================
name: {social.get_name()}
url: {social.get_url()}
==========================
"""
return f"""
-------------------------------------
name: {contact.get_name()}
email: {contact.get_email()}
phone: {contact.get_phone()}
location: {contact.get_location()}
id: {contact.get_id()}
socials ->
number of socials -> {str(len(contact.get_socials()))}
{socials}
-------------------------------------
"""
def print_property(prop_name: str, properties: Union[list, tuple]) -> int:
# print properties
for prop in properties:
print(f"{properties.index(prop) + 1}.{prop}")
option: int = int(input(f"Select the {prop_name} that you would like to edit: "))
if option < 1 or option > len(properties):
print("execute")
return -1
return option
def find_index(items: list, func) -> int:
for item in items:
if func(item):
return items.index(item)
return -1
def generate_message(message: str):
return f"""
-----------------------------
{message}
-----------------------------
"""
# storage controller
def read_contacts(contact_list: list) -> None:
try:
json_file = open("./contacts/data.json", "r")
contacts = json.loads(json_file.read())
json_file.close()
for contact in contacts:
person_obj_from_dic: object = person.Person(
contact.get("name"),
contact.get("email"),
contact.get("phone"),
contact.get("location")
)
person_obj_from_dic.set_id(contact.get("id"))
socials: list = contact.get("socials")
if len(socials):
for social_dic in socials:
person_obj_from_dic.set_socials(
social_dic.get("name"),
social_dic.get("url")
)
contact_list.append(person_obj_from_dic)
except FileExistsError:
pass
def write_contacts(contact_list: list) -> None:
parse_dict_contacts: list = []
# object to dictionary
for contact in contact_list:
parse_dict_contacts.append(contact.to_dictionary())
# parse dictionary to json
contacts_json = json.dumps(parse_dict_contacts, indent=3)
# open file in contacts
json_file = open("./contacts/data.json", "w")
# save it to the current path
json_file.write(contacts_json)
json_file.close()