def seed(db): zoos = [ Zoo(id=1, name="Bronx Zoo",address="Bronx"), Zoo(id=2, name="Central Park Zoo", address="Manhattan") ] species = [ Species(id=1, colloquial_name="cow", scientific_name="bos taurus", sound="moo"), Species(id=2, colloquial_name="pig", scientific_name="sus scrofa", sound="oink oink"), Species(id=3, colloquial_name="dog", scientific_name="canis familiaris", sound="bark") ] animals = [ Animal(name="Bessie", birthday=datetime.date(2020, 1, 1), zoo_id=1, species_id=1), Animal(name="Betty", birthday=datetime.date(2020, 1, 1), zoo_id=1, species_id=1), Animal(name="Stanley", birthday=datetime.date(2020, 1, 1), zoo_id=2, species_id=1), Animal(name="Manly", birthday=datetime.date(2020, 1, 1), zoo_id=2, species_id=1), Animal(name="Wilbur", birthday=datetime.date(2020, 1, 1), zoo_id=1, species_id=2), Animal(name="Wendy", birthday=datetime.date(2020, 1, 1), zoo_id=1, species_id=2), Animal(name="Candy", birthday=datetime.date(2020, 1, 1), zoo_id=2, species_id=2), Animal(name="Dandy", birthday=datetime.date(2020, 1, 1), zoo_id=2, species_id=2), Animal(name="Shiloh", birthday=datetime.date(2020, 1, 1), zoo_id=1, species_id=3), Animal(name="Sheepdog", birthday=datetime.date(2020, 1, 1), zoo_id=2, species_id=3) ] db.session.add_all(zoos) db.session.add_all(species) db.session.add_all(animals) db.session.commit()
def test_unicode_string_and_repr_SpeciesQuerySet(self): query_set = SpeciesQuerySet(items=[ Species(name='species1'), Species(name='species2'), ]) self.assertEqual(repr(query_set), '<SpeciesQuerySet - 2>') self.assertEqual(str(query_set), '<SpeciesQuerySet - 2>') self.assertEqual(u"%s" % query_set, '<SpeciesQuerySet - 2>')
def species_json(): """ List species objects in JSON format """ species_list = [] for species in SpeciesDB.objects: species_list.append(Species(**species.to_mongo())) return species_list
def test_species_model(self): species_to_test = Species(name='myspeciesname') self.assertEqual(species_to_test.name, 'myspeciesname') self.assertEqual(repr(species_to_test), '<Species - myspeciesname>') self.assertEqual(str(species_to_test), '<Species - myspeciesname>') self.assertEqual(u"%s" % species_to_test, '<Species - myspeciesname>')
def test_too_young(self): animal = Animal() species = Species() species.minimum_breeding_age = 1 simulation_step = SimulationStep() simulation_step.month = 0 self.assertFalse(can_breed(animal, species, simulation_step))
def test(self): animal = Animal() species = Species() species.minimum_breeding_age = 1 simulation_step = SimulationStep() simulation_step.month = MONTHS_IN_YEAR self.assertTrue(can_breed(animal, species, simulation_step))
def test_finish_breeding(self): simulation_step = SimulationStep() animal = Animal() animal.gender = GENDER_FEMALE animal.gestation_months = 1 species = Species() species.gestation_months = 1 new_animals = tuple(breed_animals([animal], species, simulation_step)) self.assertEqual(0, animal.gestation_months) self.assertEqual(1, len(new_animals))
def species_from_config(config): attributes = config['attributes'] species = Species() species.name = config['name'] species.life_span = attributes['life_span'] species.monthly_food_consumption = attributes['monthly_food_consumption'] species.monthly_water_consumption = attributes['monthly_water_consumption'] species.minimum_temperature = attributes['minimum_temperature'] species.maximum_temperature = attributes['maximum_temperature'] species.gestation_months = attributes['gestation_period'] species.minimum_breeding_age = attributes['minimum_breeding_age'] return species
def insert_test_species(clean=False): if clean: clean_species() iucn_cat = IucnRedListCategory.objects.get(code='LC') with reversion.create_revision(): element_species = ElementSpecies() element_species.other_code = "lontra_cnd" element_species.iucn_red_list_category = iucn_cat element_species.save() species = Species() species.tsn = 180549 species.first_common = 'North American river otter' species.name_sci = 'Lontra canadensis' species.element_species = element_species species.family = 'Mustelidae' species.family_common = 'Mustelids' species.phylum = 'Chordata' species.phylum_common = 'Chordates' species.save() element_species = ElementSpecies() element_species.other_code = "bl_bear" element_species.iucn_red_list_category = iucn_cat element_species.save() species = Species() species.tsn = 180544 species.first_common = 'American black bear' species.name_sci = 'Ursus americanus' species.element_species = element_species species.family = 'Ursidae' species.family_common = 'Bears' species.phylum = 'Chordata' species.phylum_common = 'Chordate' species.save()
def postSpecies(request): if request.method == 'POST': body = json.loads(request.body) common_name = body['common_name'] newSpecies = Species(common_name=common_name) newSpecies.save() if body.has_key("flower_color"): flower_color = Color.objects.get(color="yellow-green") newSpecies.flower_color.add(flower_color) newSpecies.save() species_serialized = json.dumps(newSpecies.as_dict()) return HttpResponse(species_serialized) return HttpResponse("No body")
def get_species(): count = get_count('species') for species_id in range(1, count + 1): url = SWAPI_URL.format('species') + '{}/'.format(species_id) species = None try: species = DB.open().query(Species).filter( Species.api_url == url).one() except: obj = get_request(url) if 'name' in obj: species = Species() species.parse_json(obj) DB.save(species) if species: print(species.name) print('+++++++++++++++++')
def test_old_age(self): animal = Animal() # Males cannot reproduce, so we can use them to test if they will die # correctly. simulation_step = SimulationStep() simulation_step.month = 13 simulation_step.animals = [animal] species = Species() species.life_span = 1 habitat = Habitat() next_step = advance(simulation_step, species, habitat) self.assertEqual(0, len(next_step.animals)) self.assertIn(DEATH_OLD_AGE, next_step.deaths)
def test_thirst(self): animal = Animal() # Males cannot reproduce, so we can use them to test if they will die # correctly. simulation_step = SimulationStep() simulation_step.month = 3 simulation_step.animals = [animal] species = Species() species.life_span = 100 species.monthly_water_consumption = 1 habitat = Habitat() habitat.monthly_water = 0 next_step = advance(simulation_step, species, habitat) self.assertEqual(0, len(next_step.animals)) self.assertIn(DEATH_THIRST, next_step.deaths)
def test_feed(self): animals = [Animal(), Animal()] # Males cannot reproduce, so we can use them to test if they will die # correctly. simulation_step = SimulationStep() simulation_step.month = 4 simulation_step.animals = animals species = Species() species.life_span = 1 species.monthly_food_consumption = 1 habitat = Habitat() habitat.monthly_food = 1 next_step = advance(simulation_step, species, habitat) # One animal should have died due to starvation from not enough food self.assertEqual(1, len(next_step.animals)) self.assertIn(DEATH_STARVATION, next_step.deaths)
def add_species(): """ POST /species?token=uEam0vbBtaK8slCuk-RDakZSvtxDuUIfuQs0 Creates a new species or returns bad request in case of invalid object """ request_data = request.get_json() if Validator(required_request_input).validate(request_data): new_species = Species(name=request_data['name'], description=request_data['description'], price=request_data['price']) db.session.add(new_species) db.session.commit() return make_response(jsonify({}), 201) else: return make_response( jsonify({ 'error': 'Invalid species object passed in request', 'helpString': "Data passed in similar to this {'name': 'name', " "'description': 'description', 'price': 5.88}" }), 400)
def test_too_cold(self): animal = Animal() animal.consecutive_cold_months = 1 # Males cannot reproduce, so we can use them to test if they will die # correctly. simulation_step = SimulationStep() simulation_step.animals = [animal] species = Species() species.life_span = 100 species.minimum_temperature = 100 species.maximum_temperature = 200 habitat = Habitat() habitat.monthly_food = 1 habitat.monthly_water = 1 habitat.average_temperatures[SEASON_SPRING] = -1000 next_step = advance(simulation_step, species, habitat) self.assertEqual(0, len(next_step.animals)) self.assertIn(DEATH_TOO_COLD, next_step.deaths)
def parse_taxonomy(Document): f = csv.reader(Document, delimiter='\t') f.next() for line in f: subbed = re.sub(r'(\(.*?\)|k__|p__|c__|o__|f__|g__|s__)', '', line[2]) taxon = subbed.split(';') if not Kingdom.objects.filter(kingdomName=taxon[0]).exists(): kid = uuid4().hex record = Kingdom(kingdomid=kid, kingdomName=taxon[0]) record.save() k = Kingdom.objects.get(kingdomName=taxon[0]).kingdomid if not Phyla.objects.filter(kingdomid_id=k, phylaName=taxon[1]).exists(): pid = uuid4().hex record = Phyla(kingdomid_id=k, phylaid=pid, phylaName=taxon[1]) record.save() p = Phyla.objects.get(kingdomid_id=k, phylaName=taxon[1]).phylaid if not Class.objects.filter( kingdomid_id=k, phylaid_id=p, className=taxon[2]).exists(): cid = uuid4().hex record = Class(kingdomid_id=k, phylaid_id=p, classid=cid, className=taxon[2]) record.save() c = Class.objects.get(kingdomid_id=k, phylaid_id=p, className=taxon[2]).classid if not Order.objects.filter( kingdomid_id=k, phylaid_id=p, classid_id=c, orderName=taxon[3]).exists(): oid = uuid4().hex record = Order(kingdomid_id=k, phylaid_id=p, classid_id=c, orderid=oid, orderName=taxon[3]) record.save() o = Order.objects.get(kingdomid_id=k, phylaid_id=p, classid_id=c, orderName=taxon[3]).orderid if not Family.objects.filter(kingdomid_id=k, phylaid_id=p, classid_id=c, orderid_id=o, familyName=taxon[4]).exists(): fid = uuid4().hex record = Family(kingdomid_id=k, phylaid_id=p, classid_id=c, orderid_id=o, familyid=fid, familyName=taxon[4]) record.save() f = Family.objects.get(kingdomid_id=k, phylaid_id=p, classid_id=c, orderid_id=o, familyName=taxon[4]).familyid if not Genus.objects.filter(kingdomid_id=k, phylaid_id=p, classid_id=c, orderid_id=o, familyid_id=f, genusName=taxon[5]).exists(): gid = uuid4().hex record = Genus(kingdomid_id=k, phylaid_id=p, classid_id=c, orderid_id=o, familyid_id=f, genusid=gid, genusName=taxon[5]) record.save() g = Genus.objects.get(kingdomid_id=k, phylaid_id=p, classid_id=c, orderid_id=o, familyid_id=f, genusName=taxon[5]).genusid if not Species.objects.filter(kingdomid_id=k, phylaid_id=p, classid_id=c, orderid_id=o, familyid_id=f, genusid_id=g, speciesName=taxon[6]).exists(): sid = uuid4().hex record = Species(kingdomid_id=k, phylaid_id=p, classid_id=c, orderid_id=o, familyid_id=f, genusid_id=g, speciesid=sid, speciesName=taxon[6]) record.save()
def csvToDB(request): if request.method == 'POST': file = request.FILES['file'] reader = csv.DictReader(file) result = [] for row in reader: newSpecies = Species( common_name=row['Common Name'], scientific_name=row['Scientific Name'], usda_symbol=row['Symbol'], base_age_height=strToFloat( row['Height at Base Age, Maximum (feet)']), mature_height=strToFloat(row['Height, Mature (feet)']), fire_resistant=yesNoToBool(row['Fire Resistance']), flower_conspicuous=yesNoToBool(row['Flower Conspicuous']), allelopathic=yesNoToBool(row['Known Allelopath']), leaf_retentive=yesNoToBool(row['Leaf Retention']), resproutable=yesNoToBool(row['Resprout Ability']), ) newSpecies.save() if row['County']: counties = row['County'].split(", ") for county in counties: newCounty = County.objects.get(county=county) newSpecies.county.add(newCounty) if row['Flower Color']: newFlowerColor = Color.objects.get( color=row['Flower Color'].lower()) newSpecies.flower_color.add(newFlowerColor) if row['Shape and Orientation']: newShape = Shape.objects.get( shape=row['Shape and Orientation'].lower()) newSpecies.shape.add(newShape) if row['Category']: newCategory = Category.objects.get( category=row['Category'].lower()) newSpecies.category.add(newCategory) if row['Duration']: durations = row['Duration'].split(", ") for duration in durations: newDuration = Duration.objects.get( duration=duration.lower()) newSpecies.duration.add(newDuration) if row['Active Growth Period']: if (row['Active Growth Period'] == "Year Round"): row['Active Growth Period'] = "Spring, Summer, Fall, Winter" replaced = row['Active Growth Period'].replace(" and ", ", ") growth_periods = replaced.split(", ") for growth_period in growth_periods: newActiveGrowthPeriod = ActiveGrowthPeriod.objects.get( active_growth_period=growth_period.lower()) newSpecies.active_growth_period.add(newActiveGrowthPeriod) if row['Growth Habit']: growth_habits = row['Growth Habit'].split(", ") if 'and' in row['Growth Habit']: growth_habits = row['Growth Habit'].split(" and ") for growth_habit in growth_habits: newGrowthHabit = GrowthHabit.objects.get( growth_habit=growth_habit.lower()) newSpecies.growth_habit.add(newGrowthHabit) if row['Growth Form']: newGrowthForm = GrowthForm.objects.get( growth_form=row['Growth Form'].lower()) newSpecies.growth_form.add(newGrowthForm) if row['Growth Rate']: newGrothRate = GrowthRate.objects.get( growth_rate=row['Growth Rate'].lower()) newSpecies.growth_rate.add(newGrothRate) if row['Lifespan']: newLifespan = Lifespan.objects.get( lifespan=row['Lifespan'].lower()) newSpecies.lifespan.add(newLifespan) if row['Toxicity']: newToxicity = Toxicity.objects.get( toxicity=row['Toxicity'].lower()) newSpecies.toxicity.add(newToxicity) newSpecies.save() result.append(newSpecies) return JsonResponse(result, safe=False)