def main(args): """Main entry point allowing external calls Args: args ([str]): command line parameter list """ args = parse_args(args) setup_logging(args.loglevel) shelter = Shelter() shelter.run() return os.EX_OK
def load(str1, str2=None): if str1 is None: return None if type(str2) == str: shelter = Shelter() r1 = parse(str1) r2 = parse(str2) for r in r1: shelter.animals.append(load_animal(r)) for r in r2: shelter.foster_parents.append(load_parent(r)) ## Are you sure this is correct? ## I see 2 problems. ## You are doing the check only from animal side, but you have to do it ## from the parent side too. ## The check below will raise RuntimeError every time you have a foster ## parent that fosters 2 or more animals. # Check fosters for a in shelter.animals: for f in a.fosters: for pf in f.parent.fosters: if pf.animal != a: raise RuntimeError return shelter r = parse(str1) ## You cannot use get() on a list if r.get("species") is not None: return load_animal(r) return load_parent(r)
def load(str1, str2=None): if str1 is None: return None if type(str2) == str: shelter = Shelter() r1 = parse(str1) r2 = parse(str2) for r in r1: shelter.animals.append(load_animal(r)) for r in r2: shelter.foster_parents.append(load_parent(r)) # Check fosters for a in shelter.animals: for f in a.fosters: for pf in f.parent.fosters: if pf.animal != a: raise RuntimeError return shelter r = parse(str1) if r.get("species") is not None: return load_animal(r) return load_parent(r)
def create_shelter(): start_x = 0 for shelt in range(3): start_x = (shelt * 160) - 200 for n in range(4): for i in range(4): new_brick = Shelter(x_pos=start_x + i * 20, y_pos=-150 + n * 20) shelter.append(new_brick)
def load(str1, str2=None): if str1 is None: return None if type(str2) == str: shelter = Shelter() r1 = parse(str1) r2 = parse(str2) for r in r1: shelter.animals.append(load_animal(r)) for r in r2: shelter.foster_parents.append(load_parent(r)) # Check fosters ## too many for nesting to my liking but... ## it seems at least acceptable when considering ## that it is direct and simple (no complex conditions...) for a in shelter.animals: for f in a.fosters: for pf in f.parent.fosters: if pf.animal != a: ## please, never raise an exception without ## description - do you know how hard is to ## search for the problem source? ## imagine the wednesday test suite will ## give you message: ## test failed: RuntimeError ## VS ## test failed: RuntimeError("what where went wrong") raise RuntimeError return shelter r = parse(str1) if r.get("species") is not None: return load_animal(r) return load_parent(r)
def load(id, *, db): shelter = Shelter(id=id) foster_parents = {} shelter_parents = db.execute(query_select_shelter_parents, (shelter.id, )).fetchall() for (pid, parent_id, max_animals) in shelter_parents: (name, address, phone_number) = db.execute(query_select_foster_parent, (parent_id, )).fetchone() parent = FosterParent( id=pid, name=name, address=address, phone_number=phone_number, max_animals=max_animals, ) foster_parents[parent.id] = parent shelter.foster_parents.append(parent) shelter_animals = db.execute(query_select_shelter_animals, (shelter.id, )).fetchall() for (aid, animal_id, date_of_entry) in shelter_animals: (name, year_of_birth, gender, species, breed) = db.execute(query_select_animal, (animal_id, )).fetchone() animal = Animal( id=aid, name=name, year_of_birth=year_of_birth, gender=gender, date_of_entry=datetime.strptime(date_of_entry, "%Y-%m-%d").date(), species=species, breed=breed, ) exams = db.execute(query_select_exams, (aid, )).fetchall() for (exam_id, vet, date, report) in exams: exam = Exam( id=exam_id, vet=vet, date=datetime.strptime(date, "%Y-%m-%d").date(), report=report, ) animal.exams.append(exam) fosters = db.execute(query_select_fosters, (aid, )).fetchall() for (foster_id, parent_id, start_date, end_date) in fosters: parent = foster_parents[parent_id] foster = Foster( id=foster_id, parent=parent, animal=animal, start_date=datetime.strptime(start_date, "%Y-%m-%d").date(), end_date=None if end_date == None else datetime.strptime( end_date, "%Y-%m-%d").date(), ) animal.fosters.append(foster) parent.fosters.append(foster) adoption = db.execute(query_select_adoption, (aid, )).fetchone() if adoption is not None: (adopter_name, adopter_address, date) = adoption animal.adoption = Adoption( id=aid, date=datetime.strptime(date, "%Y-%m-%d").date(), adopter_name=adopter_name, adopter_address=adopter_address, ) shelter.animals.append(animal) return shelter
from shelter import Shelter from dog import Dog from box import Box # Main entry point is here if __name__ == '__main__': shelter = Shelter('PIESKOWY RAJ') shelter.print_info() dog = Dog('Robi', 'male', 2, True) dog2 = Dog('Zuzia', 'female', 4, False) box = Box(1, 5) box.add_dog(dog) box.add_dog(dog2) print('in box {} there are {}'.format(box.id, box.dogs)) box.remove_dog(dog) print('in box {} there are {}'.format(box.id, box.dogs)) box.remove_dog(dog2) print('in box {} there are {}'.format(box.id, box.dogs))
def test_shelter(): s1 = Shelter() s1d = Shelter() s2 = Shelter() now = date.today() # add_animal s1.add_animal(name="Doz", year_of_birth=2000, gender="Male", date_of_entry=now - timedelta(days=200), species="Dog", breed="Staff") s1.add_animal(name="Doz2", year_of_birth=1992, gender="Female", date_of_entry=now - timedelta(days=200), species="Doge", breed="Staffe") s1d.add_animal(name="Doz", year_of_birth=2000, gender="Male", date_of_entry=now - timedelta(days=200), species="Dog", breed="Staff") s1d.add_animal(name="Doz2", year_of_birth=1992, gender="Female", date_of_entry=now - timedelta(days=200), species="Doge", breed="Staffe") s2.add_animal(name="Doz", year_of_birth=2000, gender="Male", date_of_entry=now - timedelta(days=100), species="Dog", breed="Staff") s2.add_animal(name="Doz2", year_of_birth=1992, gender="Female", date_of_entry=now - timedelta(days=100), species="Doge", breed="Staffe") # add_foster_parent fp1 = s1.add_foster_parent(name="Kek", address="Bur 1337", phone_number="13371337", max_animals=1) s1d.add_foster_parent(name="Kek", address="Bur 1337", phone_number="13371337", max_animals=1) fp2 = s2.add_foster_parent(name="Kek2", address="Bur", phone_number="13371337", max_animals=2) s2.add_foster_parent(name="Kek", address="Bur 1337", phone_number="13371337", max_animals=2) for a in s1.animals: a.fosters = [make_foster(parent=fp1, animal=a)] fp1.fosters = [make_foster(parent=fp1, animal=a)] a.exams = [make_exam()] a.adoption = make_adoption() for a in s1d.animals: a.fosters = [make_foster(parent=fp1, animal=a)] fp1.fosters = [make_foster(parent=fp1, animal=a)] a.exams = [make_exam()] a.adoption = make_adoption() for a in s2.animals: a.fosters = [make_foster(parent=fp2, animal=a)] fp2.fosters = [make_foster(parent=fp2, animal=a)] a.exams = [make_exam()] # === SQL === db = sqlite3.connect(':memory:') db.execute("PRAGMA foreign_keys = on") store(s1, db=db) store(s1d, db=db, deduplicate=True) store(s2, db=db) assert s1 == s1d assert s1.id == s1d.id s1l = load(s1.id, db=db) assert s1 == s1l db.close() # === JSON === a_jsons = [] for a in s1.animals: j = shelter_json.store(a) res = shelter_json.load(j) a_jsons.append(shelter_json.parse(j)) assert res == a p_jsons = [] for p in s1.foster_parents: j = shelter_json.store(p) res = shelter_json.load(j) p_jsons.append(shelter_json.parse(j)) assert res == p s_res = shelter_json.load(shelter_json.format(a_jsons), shelter_json.format(p_jsons)) assert s_res == s1
def test_shelter(): s = Shelter() now = date.today() # add_animal res = s.add_animal(name="Doz", year_of_birth=2000, gender="Male", date_of_entry=now - timedelta(days=200), species="Dog", breed="Staff") assert type(res) == Animal assert len(s.animals) == 1 res = s.add_animal(name="Doz2", year_of_birth=1992, gender="Female", date_of_entry=now - timedelta(days=200), species="Doge", breed="Staffe") assert type(res) == Animal assert len(s.animals) == 2 # list_animals assert len(s.list_animals(date=now, )) == 2 assert len(s.list_animals(date=now, name="Doz")) == 1 assert len(s.list_animals(date=now, name="Doz2")) == 1 assert len(s.list_animals(date=now, name="Dozzzz")) == 0 assert len(s.list_animals(date=now, year_of_birth=2000)) == 1 assert len(s.list_animals(date=now, year_of_birth=1992)) == 1 assert len(s.list_animals(date=now, year_of_birth=1337)) == 0 assert len(s.list_animals(date=now, gender="Male")) == 1 assert len(s.list_animals(date=now, gender="Female")) == 1 assert len(s.list_animals(date=now, gender="Hybrid")) == 0 assert len( s.list_animals(date=now, date_of_entry=now - timedelta(days=200))) == 2 assert len(s.list_animals(date=now, date_of_entry=now)) == 0 assert len(s.list_animals(date=now, species="Dog")) == 1 assert len(s.list_animals(date=now, species="Doge")) == 1 assert len(s.list_animals(date=now, species="Dog3")) == 0 assert len(s.list_animals(date=now, breed="Staff")) == 1 assert len(s.list_animals(date=now, breed="Staffe")) == 1 assert len(s.list_animals(date=now, breed="Staff3")) == 0 doz = s.list_animals(date=now, name="Doz")[0] doz.adopt(date=now, adopter_name="Kek", adopter_address="Bur 1337") assert len(s.list_animals(date=now - timedelta(days=1))) == 2 assert len(s.list_animals(date=now)) == 1 # add_foster_parent p = s.add_foster_parent(name="Kek", address="Bur 1337", phone_number="13371337", max_animals=1) assert type(p) == FosterParent assert len(s.foster_parents) == 1 # available_foster_parents assert len(s.available_foster_parents(date=now)) == 1 p.fosters = [make_foster(start_date=now)] assert len(s.available_foster_parents(date=now)) == 0
def test(): shelter = Shelter() now = date.today() day = timedelta(days=1) # add_animal a1 = shelter.add_animal(name="Doz", year_of_birth=2000, gender="Male", date_of_entry=now - day * 200, species="Dog", breed="Staff") a1.add_exam(vet="Kekega", date=now - day, report="OK") a1.add_exam(vet="Kekega", date=now, report="OK") res = shelter.add_animal(name="Doz", year_of_birth=2000, gender="Male", date_of_entry=now - day * 200, species="Dog", breed="Staff") assert a1 == res, f"{a1} == {res}" assert a1 is res, f"{a1} is {res}" a2 = shelter.add_animal(name="Doz2", year_of_birth=1992, gender="Female", date_of_entry=now - day * 200, species="Doge", breed="Staffe") a2.add_exam(vet="Kekega", date=now - day, report="OK") a2.add_exam(vet="Kekega", date=now, report="OK") a3 = shelter.add_animal(name="Doz3", year_of_birth=1992, gender="Female", date_of_entry=now - day * 200, species="Dogee", breed="Staffe") a3.add_exam(vet="Kekega", date=now - day, report="OK") # add_foster_parent fp1 = shelter.add_foster_parent(name="Kek", address="Bur 1337", phone_number="13371337", max_animals=3) a1.start_foster(date=now - day * 2, parent=fp1) a1.end_foster(date=now - day) a1.adopt(date=now, adopter_name="Kek", adopter_address="Lol") res = shelter.add_foster_parent(name="Kek", address="Bur 1337", phone_number="13371337", max_animals=3) assert fp1 == res, f"{fp1} == {res}" assert fp1 is res, f"{fp1} is {res}" fp2 = shelter.add_foster_parent(name="Kek2", address="Bur 1337", phone_number="13371337", max_animals=3) a2.start_foster(date=now - day, parent=fp2) a3.start_foster(date=now - day, parent=fp2) # === SQL === db = sqlite3.connect(':memory:') db.execute("PRAGMA foreign_keys = on") id_ = store(shelter, db=db) shelter_2 = load(id_, db=db) id_2 = store(shelter_2, db=db, deduplicate=True) id_3 = store(shelter, db=db, deduplicate=True) assert id_ == id_2, f"{id_} == {id_2}" assert id_ == id_3, f"{id_} == {id_3}" # === JSON === res = sj.load(sj.store(a1)) assert res == a1, f"{res} == {a1}" res = sj.load(sj.store(fp1)) assert res == fp1, f"{res} == {fp1}" a_jsons = [] for a in shelter.animals: j = sj.store(a) res = sj.load(j) a_jsons.append(sj.parse(j)) assert res == a, f"{res} == {a}" p_jsons = [] for p in shelter.foster_parents: j = sj.store(p) res = sj.load(j) p_jsons.append(sj.parse(j)) assert res == p, f"{res} == {p}" sj.store(shelter.animals) sj.store(shelter.foster_parents) res = sj.load(sj.format(a_jsons), sj.format(p_jsons)) assert res == shelter, f"{res} == {shelter}" # === e2e === res = load(store(res, db=db, deduplicate=True), db=db) assert res == shelter, f"{res} == {shelter}" print("OK")