Skip to content

Commit

Permalink
Merge pull request #3 from JoshuaOndieki/develop
Browse files Browse the repository at this point in the history
PEP8
  • Loading branch information
JoshuaOndieki authored Apr 13, 2017
2 parents eaa7c25 + daf744f commit d250586
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 144 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__pycache__
venv
.pyc
.idea
139 changes: 72 additions & 67 deletions models/dojo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,120 +3,125 @@
from models.livingspace import LivingSpace
from models.office import Office
import random
from modules.ui import error,success
from modules.ui import error, success
from modules.validators import *


class Dojo():
def __init__(self):
self.offices={}
self.livingspaces={}
self.fellows=[]
self.staff=[]
self.all_rooms={}
self.all_people=[]
self.offices = {}
self.livingspaces = {}
self.fellows = []
self.staff = []
self.all_rooms = {}
self.all_people = []

def create_room(self, name,room_type):
if room_type.lower() not in ['office','livingspace']:
return(error('Only offices and livingspaces allowed!'))
if not isinstance(name,str):
return(error('Room names can only be strings!'))
def create_room(self, name, room_type):
if room_type.lower() not in ['office', 'livingspace']:
return (error('Only offices and livingspaces allowed!'))
if not isinstance(name, str):
return (error('Room names can only be strings!'))
if name in self.all_rooms:
return(error('Room %s exists!'%(name)))
return (error('Room %s exists!' % (name)))
else:
if room_type.lower()=='office'.lower():
#create an office
self.room=Office(name)
self.offices[name]=self.room.members
self.all_rooms[name]=[room_type,self.room.members]
return(success('An office called %s has been created successfully!'%(name)))
elif room_type.lower()=='livingspace'.lower():
#create a livingspace
self.room=LivingSpace(name)
self.livingspaces[name]=self.room.members
self.all_rooms[name]=[room_type,self.room.members]
return(success('A Livingspace called %s has been created successfully!'%(name)))
if room_type.lower() == 'office'.lower():
# create an office
self.room = Office(name)
self.offices[name] = self.room.members
self.all_rooms[name] = [room_type, self.room.members]
return (success('An office called %s has been created successfully!' % (name)))
elif room_type.lower() == 'livingspace'.lower():
# create a livingspace
self.room = LivingSpace(name)
self.livingspaces[name] = self.room.members
self.all_rooms[name] = [room_type, self.room.members]
return (success('A Livingspace called %s has been created successfully!' % (name)))

def add_person(self, firstname,surname,person_type,wants_accomodation='N'):
if person_type.lower() not in ['fellow','staff']:
return(error('Only fellow and staff allowed!'))
if not isinstance(firstname,str) or not isinstance(surname,str):
return(error('People names can only be strings!'))
if firstname+' '+surname in self.all_people:
return(error('%s %s exists!'%(firstname,surname)))
def add_person(self, firstname, surname, person_type, wants_accomodation='N'):
if person_type.lower() not in ['fellow', 'staff']:
return (error('Only fellow and staff allowed!'))
if not isinstance(firstname, str) or not isinstance(surname, str):
return (error('People names can only be strings!'))
if firstname + ' ' + surname in self.all_people:
return (error('%s %s exists!' % (firstname, surname)))
else:
if represents_int(firstname) or represents_int(surname): return error('Names can not be or contain integers!')
if person_type.lower()=='fellow':
#create a fellow
fellow=Fellow(firstname,surname)
self.fellows.append(firstname +' '+ surname)
self.all_people.append(firstname +' '+ surname)
if represents_int(firstname) or represents_int(surname):
return error('Names can not be or contain integers!')
if person_type.lower() == 'fellow':
# create a fellow
fellow = Fellow(firstname, surname)
self.fellows.append(firstname + ' ' + surname)
self.all_people.append(firstname + ' ' + surname)
if self.offices:
all_offices = list(self.offices.keys())
checked_offices = []
while True:
office=random.choice(list(self.offices))
office = random.choice(list(self.offices))
if len(self.offices[office]) < 6:
self.offices[office].append(firstname +' '+ surname)
print(success('Fellow %s %s has been assigned office %s!'%(firstname,surname,office)))
self.offices[office].append(firstname + ' ' + surname)
print(success('Fellow %s %s has been assigned office %s!' % (firstname, surname, office)))
break
if office not in checked_offices: checked_offices.append(office)
if office not in checked_offices:
checked_offices.append(office)
if checked_offices == all_offices:
print(error('All offices are full at the moment!'))
break
else:
print(error('No office to assign!'))
if wants_accomodation=='Y' and self.livingspaces:
if wants_accomodation == 'Y' and self.livingspaces:
all_livingspaces = list(self.livingspaces.keys())
checked_livingspaces = []
while True:
room=random.choice(list(self.livingspaces))
room = random.choice(list(self.livingspaces))
if len(self.livingspaces[room]) < 4:
self.livingspaces[room].append(firstname +' '+ surname)
print(success('Fellow %s %s has been assigned livingspace %s!'%(firstname,surname,room)))
self.livingspaces[room].append(firstname + ' ' + surname)
print(
success('Fellow %s %s has been assigned livingspace %s!' % (firstname, surname, room)))
break
if room not in checked_livingspaces: checked_livingspaces.append(room)
if room not in checked_livingspaces:
checked_livingspaces.append(room)
if checked_livingspaces == all_livingspaces:
print(error('All livingspaces are full at the moment!'))
break
return(success('Fellow %s %s has been added successfully!'%(firstname,surname)))
elif person_type.lower()=='staff':
#create a staff member
return (success('Fellow %s %s has been added successfully!' % (firstname, surname)))
elif person_type.lower() == 'staff':
# create a staff member
if wants_accomodation == 'Y':
print(error('Staff can not be allocated livingspace!'))
staff=Staff(firstname,surname)
self.staff.append(firstname +' '+ surname)
self.all_people.append(firstname +' '+ surname)
staff = Staff(firstname, surname)
self.staff.append(firstname + ' ' + surname)
self.all_people.append(firstname + ' ' + surname)
if self.offices:
office=random.choice(list(self.offices))
self.offices[office].append(firstname +' '+ surname)
print(success('Staff %s %s has been assigned office %s!'%(firstname,surname,office)))
office = random.choice(list(self.offices))
self.offices[office].append(firstname + ' ' + surname)
print(success('Staff %s %s has been assigned office %s!' % (firstname, surname, office)))
else:
print(error('No office to assign!'))
return(success('Staff %s %s has been added successfully!'%(firstname,surname)))
def print_room(self,name):
return (success('Staff %s %s has been added successfully!' % (firstname, surname)))

def print_room(self, name):
if name in self.all_rooms:
room_members = {k:v[1] for k,v in self.all_rooms.items() if k==name}
room_members = {k: v[1] for k, v in self.all_rooms.items() if k == name}
return room_members
return (error("Room %s does not exist!"%(name)))
return (error("Room %s does not exist!" % (name)))

def print_allocations(self,filename=None):
allocations={k: v[1] for k, v in self.all_rooms.items() if v[1]}
def print_allocations(self, filename=None):
allocations = {k: v[1] for k, v in self.all_rooms.items() if v[1]}
for room in allocations:
print(room)
print('```````````````````````````````````````````')
members = ''
room_members = allocations[room]
for member in room_members:
members = members + ' '+ member
members = members + ' ' + member
print(members)
print('')
if filename is not None:
#save to file
# save to file
with open(filename, 'w') as file:
file.writelines(room_members)


def print_unallocations(self,filename=None):
def print_unallocations(self, filename=None):
allocated = []
for room in self.all_rooms:
members = self.all_rooms[room]
Expand All @@ -125,7 +130,7 @@ def print_unallocations(self,filename=None):
unallocated_people = []
for person in self.all_people:
if person not in allocated:
unallocated_people.append(person+ '\n')
unallocated_people.append(person + '\n')
if filename is None:
for person in unallocated_people:
print(person)
Expand Down
5 changes: 3 additions & 2 deletions models/fellow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from models.person import Person


class Fellow(Person):
def __init__(self,firstname,surname):
# super().__init__(self,firstname,surname)
self.firstname=firstname
self.surname=surname
self.firstname = firstname
self.surname = surname
7 changes: 4 additions & 3 deletions models/livingspace.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from models.room import Room


class LivingSpace(Room):
def __init__(self,name):
# super().__init__(self,name)
self.name=name
self.capacity=4
self.members=[]
self.name = name
self.capacity = 4
self.members = []
7 changes: 4 additions & 3 deletions models/office.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from models.room import Room


class Office(Room):
def __init__(self,name):
# super().__init__(self,name)
self.name=name
self.capacity=6
self.members=[]
self.name = name
self.capacity = 6
self.members = []
2 changes: 1 addition & 1 deletion models/person.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Person():
def __init__(self, firstname,surname):
def __init__(self, firstname, surname):
self.firstname = firstname
self.surname = surname
4 changes: 2 additions & 2 deletions models/room.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

class Room():
def __init__(self,name):
self.name=name
def __init__(self, name):
self.name = name
7 changes: 4 additions & 3 deletions models/staff.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from models.person import Person


class Staff(Person):
def __init__(self,firstname,surname):
def __init__(self, firstname, surname):
# super().__init__(self,firstname,surname)
self.firstname=firstname
self.surname=surname
self.firstname = firstname
self.surname = surname
6 changes: 4 additions & 2 deletions modules/ui.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from termcolor import colored


def error(text):
return colored('\t\t\t\t'+text,'red',attrs=['bold'])
return colored('\t\t\t\t'+text, 'red', attrs=['bold'])


def success(text):
return colored('\t\t'+text,'green',attrs=['bold'])
return colored('\t\t'+text, 'green', attrs=['bold'])
5 changes: 3 additions & 2 deletions tests/test_add_fellow.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import unittest
from models.fellow import Fellow


class TestFellow(unittest.TestCase):
def test_creates_fellow_instance(self):
self.fellow=Fellow('Joshua','Ondieki')
self.assertTrue('Joshua'==self.fellow.firstname and 'Ondieki'==self.fellow.surname)
self.fellow = Fellow('Joshua', 'Ondieki')
self.assertTrue('Joshua' == self.fellow.firstname and 'Ondieki' == self.fellow.surname)

if __name__ == '__main__':
unittest.main()
40 changes: 21 additions & 19 deletions tests/test_add_person.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,39 @@
from models.dojo import Dojo
from modules.ui import *


class TestAddPerson(unittest.TestCase):
def setUp(self):
self.dojo=Dojo()
self.dojo = Dojo()
self.initial_people = len(self.dojo.all_people)

def test_create_people_successfully(self):
self.fellow = self.dojo.add_person('Joshua','Ondieki', 'Fellow')
self.staff = self.dojo.add_person('Annette','Kamau', 'Staff')
self.fellow = self.dojo.add_person('Joshua', 'Ondieki', 'Fellow')
self.staff = self.dojo.add_person('Annette', 'Kamau', 'Staff')
new_people = len(self.dojo.all_people)
self.assertEqual(new_people - self.initial_people, 2)

def test_it_fails_with_existing_person(self):
exist_person = self.dojo.add_person('Joshua','Ondieki', 'Fellow')
try_overwrite_f = self.dojo.add_person('Joshua','Ondieki', 'Fellow')
exist_person = self.dojo.add_person('Evans','Musomi', 'Staff')
try_overwrite_s = self.dojo.add_person('Evans','Musomi', 'Staff')
self.assertTrue(try_overwrite_f == error('Joshua Ondieki exists!') and try_overwrite_s == error('Evans Musomi exists!'))
exist_person = self.dojo.add_person('Joshua', 'Ondieki', 'Fellow')
try_overwrite_f = self.dojo.add_person('Joshua', 'Ondieki', 'Fellow')
exist_person = self.dojo.add_person('Evans', 'Musomi', 'Staff')
try_overwrite_s = self.dojo.add_person('Evans', 'Musomi', 'Staff')
self.assertTrue(
try_overwrite_f == error('Joshua Ondieki exists!') and try_overwrite_s == error('Evans Musomi exists!'))

def test_it_fails_with_invalid_person_type(self):
invalid_type = self.dojo.add_person('Loice','Andia','BFA')
self.assertEqual(error('Only fellow and staff allowed!'),invalid_type)
invalid_type = self.dojo.add_person('Loice', 'Andia', 'BFA')
self.assertEqual(error('Only fellow and staff allowed!'), invalid_type)

def test_fails_with_person_name_not_string(self):
invalid_person = self.dojo.add_person(['Oj'],'Josh','Fellow')
invalid_person1 = self.dojo.add_person({'Oj':'Josh'},{},'Fellow')
invalid_person2 = self.dojo.add_person(['Oj'],['Josh'],'Staff')
invalid_person3 = self.dojo.add_person({'Oj':'Josh'},{},'Staff')
self.assertEqual(invalid_person,error('People names can only be strings!'))
self.assertEqual(invalid_person1,error('People names can only be strings!'))
self.assertEqual(invalid_person2,error('People names can only be strings!'))
self.assertEqual(invalid_person3,error('People names can only be strings!'))

invalid_person = self.dojo.add_person(['Oj'], 'Josh', 'Fellow')
invalid_person1 = self.dojo.add_person({'Oj': 'Josh'}, {}, 'Fellow')
invalid_person2 = self.dojo.add_person(['Oj'], ['Josh'], 'Staff')
invalid_person3 = self.dojo.add_person({'Oj': 'Josh'}, {}, 'Staff')
self.assertEqual(invalid_person, error('People names can only be strings!'))
self.assertEqual(invalid_person1, error('People names can only be strings!'))
self.assertEqual(invalid_person2, error('People names can only be strings!'))
self.assertEqual(invalid_person3, error('People names can only be strings!'))


if __name__ == '__main__':
Expand Down
5 changes: 3 additions & 2 deletions tests/test_add_staff.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import unittest
from models.staff import Staff


class TestStaff(unittest.TestCase):
def test_creates_staff_instance(self):
self.staff=Staff('James','Ndiga')
self.assertTrue('James'==self.staff.firstname and 'Ndiga'==self.staff.surname)
self.staff = Staff('James', 'Ndiga')
self.assertTrue('James' == self.staff.firstname and 'Ndiga' == self.staff.surname)


if __name__ == '__main__':
Expand Down
7 changes: 4 additions & 3 deletions tests/test_create_livingspace.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import unittest
from models.livingspace import LivingSpace


class TestLivingSpace(unittest.TestCase):
def test_creates_livingspace_instance(self):
self.livingspace=LivingSpace('Amity')
self.assertEqual(self.livingspace.capacity,4)
self.assertEqual(self.livingspace.members,[])
self.livingspace = LivingSpace('Amity')
self.assertEqual(self.livingspace.capacity, 4)
self.assertEqual(self.livingspace.members, [])

if __name__ == '__main__':
unittest.main()
7 changes: 4 additions & 3 deletions tests/test_create_office.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import unittest
from models.office import Office


class TestOffice(unittest.TestCase):
def test_creates_office_instance(self):
self.office=Office('Spire')
self.assertEqual(self.office.capacity,6)
self.assertEqual(self.office.members,[])
self.office = Office('Spire')
self.assertEqual(self.office.capacity, 6)
self.assertEqual(self.office.members, [])

if __name__ == '__main__':
unittest.main()
Loading

0 comments on commit d250586

Please sign in to comment.