Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Week09 exercises #169 #177

Open
wants to merge 20 commits into
base: week09
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
# rse-classwork-2020

We will use this repository to follow up and review all the exercises done in class for the
[MPHY0021: Research Software Engineering With Python course](http://github-pages.ucl.ac.uk/rsd-engineeringcourse/).
This branch is about testing on week5
05/11/2020
67 changes: 33 additions & 34 deletions week09/refactoring/initial_global.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
def average_age():
def average_age(group):
"""Compute the average age of the group's members."""
all_ages = [person["age"] for person in group.values()]
return sum(all_ages) / len(group)


def forget(person1, person2):
def forget(person1, person2, group):
"""Remove the connection between two people."""
group[person1]["relations"].pop(person2, None)
group[person2]["relations"].pop(person1, None)


def add_person(name, age, job, relations):
def add_person(name, age, job, relations, group):
"""Add a new person with the given characteristics to the group."""
new_person = {
"age": age,
Expand All @@ -19,43 +19,42 @@ def add_person(name, age, job, relations):
}
group[name] = new_person


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"
if __name__ == "__main__":
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_relations = {
"John": "cousin",
"Zalika": "landlord"
}
nash_relations = {
"John": "cousin",
"Zalika": "landlord"
}

add_person("Nash", 34, "chef", nash_relations)
add_person("Nash", 34, "chef", nash_relations, group)

forget("Nash", "John")
forget("Nash", "John", group)

if __name__ == "__main__":
assert len(group) == 4, "Group should have 4 members"
assert average_age() == 28.75, "Average age of the group is incorrect!"
assert average_age(group) == 28.75, "Average age of the group is incorrect!"
assert len(group["Nash"]["relations"]) == 1, "Nash should only have one relation"
print("All assertions have passed!")
18 changes: 13 additions & 5 deletions week09/refactoring/initial_person_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ def add_connection(self, person, relation):

def forget(self, person):
"""Removes any connections to a person"""
pass

if person not in self.connections:
raise ValueError(f"I don't know about {person.name}")
self.connections.pop(person)

def average_age(group):
"""Compute the average age of the group's members."""
Expand All @@ -28,11 +29,18 @@ def average_age(group):
if __name__ == "__main__":
# ...then create the group members one by one...
jill = Person("Jill", 26, "biologist")

zalika = Person("Zalika", 28, "artist")
john = Person("John", 27, "writer")
nash = Person("Nash", 34, "chef")
# ...then add the connections one by one...
# Note: this will fail from here if the person objects aren't created
jill.add_connection(zalika, "friend")

zalika.add_connection(jill, "friend")
jill.add_connection(john, "partner")
john.add_connection(john, "partner")
nash.add_connection(john, "cousin")
john.add_connection(nash, "cousin")
nash.add_connection(zalika, "Landlord")
# Note: this will fail from here if the person objects aren't created
# ... then forget Nash and John's connection
nash.forget(john)
# Then create the group
Expand Down
31 changes: 27 additions & 4 deletions week09/refactoring/initial_two_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self):

def size(self):
"""Return how many people are in the group."""
pass
return len(self.members)

def contains(self, name):
"""Check whether the group contains a person with the given name.
Expand All @@ -32,17 +32,34 @@ def add_person(self, name, age, job):

def number_of_connections(self, name):
"""Find the number of connections that a person in the group has"""
pass
if not self.contains(name):
raise ValueError(f"I don't know about {name}.")
return sum(1 for (first, second) in self.connections if first == name)

def connect(self, name1, name2, relation, reciprocal=True):
"""Connect two given people in a particular way.
Optional reciprocal: If true, will add the relationship from name2 to name 1 as well
"""
pass
if not self.contains(name1):
raise ValueError(f"I don't know who {name1} is.")
if not self.contains(name2):
raise ValueError(f"I don't know who {name2} is.")
if (name1, name2) in self.connections:
raise ValueError(f"{name1} already knows {name2} as a "
f"{self.connections[(name1, name2)]}")
self.connections[(name1, name2)] = relation
if reciprocal:
self.connections[(name2, name1)] = relation

def forget(self, name1, name2):
"""Remove the connection between two people."""
pass
if not self.contains(name1):
raise ValueError(f"I don't know who {name1} is.")
if not self.contains(name2):
raise ValueError(f"I don't know who {name2} is.")
if (name1, name2) not in self.connections:
raise ValueError(f"{name1} does not know {name2} yet.")
self.connections.pop((name1, name2))

def average_age(self):
"""Compute the average age of the group's members."""
Expand All @@ -55,8 +72,14 @@ def average_age(self):
my_group = Group()
# ...then add the group members one by one...
my_group.add_person("Jill", 26, "biologist")
my_group.add_person("Zalika", 28, "artist")
my_group.add_person("John", 27, "writer")
my_group.add_person("Nash", 34, "chef")
# ...then their connections
my_group.connect("Jill", "Zalika", "friend")
my_group.connect("Jill", "John", "partner")
my_group.connect("Nash", "John", "cousin", reciprocal=False)
my_group.connect("Nash", "Zalika", "landlord", reciprocal=False)
# ... then forget Nash and John's connection
my_group.forget("Nash", "John")

Expand Down