From 3edd2ea8f011bff83d48dfbef853228a9151ecd7 Mon Sep 17 00:00:00 2001 From: Aure Date: Thu, 22 Oct 2020 11:08:24 +0100 Subject: [PATCH 1/8] first commit --- group.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/group.py b/group.py index e2ec347..10e1623 100644 --- a/group.py +++ b/group.py @@ -2,4 +2,25 @@ # Your code to go here... -my_group = +my_group = [{'name':Jill, 'age':26, } + +class Person(): + def __init__(self, name, age, job, connection=[] ): + self.name = name + self.age = age + self.job =job + self.connection = connection + + def add_connection(name, relationship) + connection= self.connection.append({'name':name, 'relationship':relationship}) + return connection + + + + +jill=Person('Jill', 26, 'biologist') +zalika = Person('Zalika', 28, 'artist') + + +Person('John', 27, 'writer') +Person('Nash', 34, 'chef') \ No newline at end of file From 10d9d1b8334ae0450813018be043b8b7f5dd9b54 Mon Sep 17 00:00:00 2001 From: Aure Date: Thu, 22 Oct 2020 11:38:04 +0100 Subject: [PATCH 2/8] Added connection function. Still to add: 2-way relationship --- group.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/group.py b/group.py index 10e1623..06795ba 100644 --- a/group.py +++ b/group.py @@ -2,25 +2,28 @@ # Your code to go here... -my_group = [{'name':Jill, 'age':26, } -class Person(): - def __init__(self, name, age, job, connection=[] ): +class Person: + def __init__(self, name, age, job, connection={} ): self.name = name self.age = age self.job =job self.connection = connection - def add_connection(name, relationship) - connection= self.connection.append({'name':name, 'relationship':relationship}) - return connection - - + def add_connection(self, friend, relationship): + self.connection.update({friend.name:relationship}) + if relationship=='friend' or relationship=='partner': + friend.connection.update({self.name:relationship}) jill=Person('Jill', 26, 'biologist') zalika = Person('Zalika', 28, 'artist') +john=Person('John', 27, 'writer') +nash=Person('Nash', 34, 'chef') +jill.add_connection(zalika, 'friend') +jill.add_connection(john, 'partner') +print(zalika.connection) +print(jill.connection) +#zalika.add_connection(jill, 'friend') -Person('John', 27, 'writer') -Person('Nash', 34, 'chef') \ No newline at end of file From fdf5ff68ea6af716e83f4d0051bc6235994f70a3 Mon Sep 17 00:00:00 2001 From: Iani Date: Tue, 27 Oct 2020 14:14:16 +0000 Subject: [PATCH 3/8] Commenting out changes --- group.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/group.py b/group.py index 06795ba..4252f8c 100644 --- a/group.py +++ b/group.py @@ -19,7 +19,7 @@ def add_connection(self, friend, relationship): jill=Person('Jill', 26, 'biologist') zalika = Person('Zalika', 28, 'artist') john=Person('John', 27, 'writer') -nash=Person('Nash', 34, 'chef') +#nash=Person('Nash', 34, 'chef') jill.add_connection(zalika, 'friend') jill.add_connection(john, 'partner') @@ -27,3 +27,4 @@ def add_connection(self, friend, relationship): print(jill.connection) #zalika.add_connection(jill, 'friend') + From 5dcb5b86540714b6e081be55813dffb081f023a7 Mon Sep 17 00:00:00 2001 From: Iani Date: Tue, 27 Oct 2020 14:54:24 +0000 Subject: [PATCH 4/8] Attempt 2 just using dictionary of dictionaries --- group.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/group.py b/group.py index 4252f8c..db96299 100644 --- a/group.py +++ b/group.py @@ -1,19 +1,23 @@ """An example of how to represent a group of acquaintances in Python.""" -# Your code to go here... - +""" +# ATTEMPT 1: USING CLASSES BUT IT GOT TOO COMPLICATED WITH SELF +#Clearing out variables +jill = [] +zalika = [] +# Your code to go here... class Person: - def __init__(self, name, age, job, connection={} ): + def __init__(self, name, age, job, connection= [] ): self.name = name self.age = age self.job =job self.connection = connection def add_connection(self, friend, relationship): - self.connection.update({friend.name:relationship}) - if relationship=='friend' or relationship=='partner': - friend.connection.update({self.name:relationship}) + #Problem: This also updates the connection of the friend using self of person and person's friend + #self.connection.update({friend.name: relationship}) + self.connection.append({friend.name : relationship}) jill=Person('Jill', 26, 'biologist') @@ -23,8 +27,43 @@ def add_connection(self, friend, relationship): jill.add_connection(zalika, 'friend') jill.add_connection(john, 'partner') -print(zalika.connection) -print(jill.connection) -#zalika.add_connection(jill, 'friend') +print("Jill's connection: ", jill.connection) +zalika.add_connection(jill, 'friend') +print("Zalika's connection: ", zalika.connection) +""" + +# Attempt 2: Using dictionary of dictionaries +group = { + "Jill": { + "age": 26, + "job": "biologist", + "relations": { + "Zalika": "friend", + "John": "partner" + } + }, + "Zalika": { + "age": 28, + "job": "artist", + "relations": { + "Jill": "friend" + } + }, + "John": { + "age": 27, + "job": "writer", + "relations": { + "Jill": "partner" + } + }, + "Nash": { + "age": 34, + "job": "chef", + "relations": { + "John": "cousin", + "Zalika": "landlord" + } + } +} From fa1cc07478bea9a3fe510fe3c21c82320260e516 Mon Sep 17 00:00:00 2001 From: Iani Date: Tue, 27 Oct 2020 15:21:04 +0000 Subject: [PATCH 5/8] Printing out values from group --- group.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/group.py b/group.py index db96299..7803916 100644 --- a/group.py +++ b/group.py @@ -1,7 +1,7 @@ """An example of how to represent a group of acquaintances in Python.""" -""" # ATTEMPT 1: USING CLASSES BUT IT GOT TOO COMPLICATED WITH SELF +""" #Clearing out variables jill = [] zalika = [] @@ -32,7 +32,7 @@ def add_connection(self, friend, relationship): print("Zalika's connection: ", zalika.connection) """ -# Attempt 2: Using dictionary of dictionaries +# ATTEMPT 2: Using dictionary of dictionaries group = { "Jill": { @@ -67,3 +67,23 @@ def add_connection(self, friend, relationship): } } +#Unpacking things in nested dictionary of dictionaries : member name is string, member is actual dictionary value with its items +[print(member['age']) for member_name, member in group.items()] + +# 1. Max age of people in the group +ages = [] +[ages.append(member['age']) for member_name, member in group.items()] +print("Maximum age of group: ", max(ages)) + +# 2. Average (mean) number of relations among members of the group +no_relations = [] +[no_relations.append(len(member['relations'])) for member_name, member in group.items()] +avg_relations = sum(no_relations) / len(no_relations) +print("Average number of relations: " , avg_relations) + +# 3. The maximum age of people in the group that have at least one relation +ages_1relation = [] +[ages_1relation.append(member['age']) for member_name, member in group.items() if len(member['relations']) > 0 ] +print("Max age of people in the group that have at least one relation: ", max(ages_1relation)) + +#4. Maximum age of people in the group that have at least one friend From c74ca15aeca6213ba622f2ead0c3119bc4dde129 Mon Sep 17 00:00:00 2001 From: Iani Date: Tue, 27 Oct 2020 15:45:49 +0000 Subject: [PATCH 6/8] Printed additional info --- group.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/group.py b/group.py index 7803916..deabda6 100644 --- a/group.py +++ b/group.py @@ -87,3 +87,8 @@ def add_connection(self, friend, relationship): print("Max age of people in the group that have at least one relation: ", max(ages_1relation)) #4. Maximum age of people in the group that have at least one friend +print('friend' in group['Jill']['relations'].values()) #Test if friend is in relations.values +ages_wfriends = [] +[ages_wfriends.append(member['age']) for member_name, member in group.items() if ('friend' in member['relations'].values())] +print('Maximum age of people w/at least one friend: ', max(ages_wfriends)) + From be6eb94df420f1f3857b9ecf47deafd9984995c3 Mon Sep 17 00:00:00 2001 From: Iani Date: Tue, 27 Oct 2020 17:19:41 +0000 Subject: [PATCH 7/8] Adding json files --- group.json | 32 ++++++++++++++++++++++++++++++++ group.py | 22 ++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 group.json diff --git a/group.json b/group.json new file mode 100644 index 0000000..6ad1f9e --- /dev/null +++ b/group.json @@ -0,0 +1,32 @@ +{ + "Jill": { + "age": 26, + "job": "biologist", + "relations": { + "Zalika": "friend", + "John": "partner" + } + }, + "Zalika": { + "age": 28, + "job": "artist", + "relations": { + "Jill": "friend" + } + }, + "John": { + "age": 27, + "job": "writer", + "relations": { + "Jill": "partner" + } + }, + "Nash": { + "age": 34, + "job": "chef", + "relations": { + "John": "cousin", + "Zalika": "landlord" + } + } +} \ No newline at end of file diff --git a/group.py b/group.py index deabda6..75ddac6 100644 --- a/group.py +++ b/group.py @@ -92,3 +92,25 @@ def add_connection(self, friend, relationship): [ages_wfriends.append(member['age']) for member_name, member in group.items() if ('friend' in member['relations'].values())] print('Maximum age of people w/at least one friend: ', max(ages_wfriends)) +""" Working with JSON files """ +import json + +# Writing group dictionary to json file format +with open('group.json', 'w') as json_file: + + #Writes dictionary group into json format to be placed inside file : ie string + json_format = json.dumps(group, indent = 3) + + #Writes json formatted text to the json file + json_file.write(json_format) + +#Reading json file +with open('group.json', 'r') as json_file_read: + string_data = json_file_read.read() +print(string_data) + +# Turning the read data into json format +group_read = json.loads(string_data) + +#Access jill contents +print(group_read['Jill']) From db9e8c962391a3cd0fe9814b37b564485b22f0c1 Mon Sep 17 00:00:00 2001 From: Iani Date: Fri, 30 Oct 2020 11:38:35 +0000 Subject: [PATCH 8/8] Correcting comprehensions --- group.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/group.py b/group.py index 75ddac6..73ae217 100644 --- a/group.py +++ b/group.py @@ -71,25 +71,26 @@ def add_connection(self, friend, relationship): [print(member['age']) for member_name, member in group.items()] # 1. Max age of people in the group -ages = [] -[ages.append(member['age']) for member_name, member in group.items()] -print("Maximum age of group: ", max(ages)) +#Not proper comprehensions --> Comprehensions make a list already +#ages = [] +#[ages.append(member['age']) for member_name, member in group.items()] + +#Making use of proper comprehensions +ages = [person["age"] for person in group.values()] +print("Maximum age of group: ", max(ages)) # 2. Average (mean) number of relations among members of the group -no_relations = [] -[no_relations.append(len(member['relations'])) for member_name, member in group.items()] +no_relations = [len(member['relations']) for member_name, member in group.items()] avg_relations = sum(no_relations) / len(no_relations) print("Average number of relations: " , avg_relations) # 3. The maximum age of people in the group that have at least one relation -ages_1relation = [] -[ages_1relation.append(member['age']) for member_name, member in group.items() if len(member['relations']) > 0 ] +ages_1relation = [member['age'] for member_name, member in group.items() if len(member['relations']) > 0 ] print("Max age of people in the group that have at least one relation: ", max(ages_1relation)) #4. Maximum age of people in the group that have at least one friend print('friend' in group['Jill']['relations'].values()) #Test if friend is in relations.values -ages_wfriends = [] -[ages_wfriends.append(member['age']) for member_name, member in group.items() if ('friend' in member['relations'].values())] +ages_wfriends = [member['age'] for member_name, member in group.items() if ('friend' in member['relations'].values())] print('Maximum age of people w/at least one friend: ', max(ages_wfriends)) """ Working with JSON files """ @@ -102,15 +103,14 @@ def add_connection(self, friend, relationship): json_format = json.dumps(group, indent = 3) #Writes json formatted text to the json file - json_file.write(json_format) - + json_file.write(json_format) #or could've done: json_file.write(json.dumps(group, indent =3 ) #Reading json file with open('group.json', 'r') as json_file_read: string_data = json_file_read.read() -print(string_data) +#print(string_data) # Turning the read data into json format group_read = json.loads(string_data) #Access jill contents -print(group_read['Jill']) +#print(group_read['Jill'])