Skip to content
Snippets Groups Projects
Commit 4504be78 authored by Rushel Silvester's avatar Rushel Silvester
Browse files

Merge branch 'master' into 'main'

add: create_dog()

See merge request !4
parents db621876 590588d1
No related branches found
No related tags found
1 merge request!4add: create_dog()
......@@ -8,4 +8,6 @@
/dataSources.local.xml
/ext
/ext/*
/.idea
\ No newline at end of file
/.idea
/images/*
/images
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
import requests
import csv
import argparse
import random
import shutil
import pathlib as Path
URL = "https://data.stadt-zuerich.ch/dataset/sid_stapo_hundenamen_od1002/download/KUL100OD1002.csv"
DATA_URL = (
"https://data.stadt-zuerich.ch/dataset/"
"sid_stapo_hundenamen_od1002/download/KUL100OD1002.csv"
)
DOG_IMAGE_URL = "https://random.dog/woof.json"
def get_parser():
para = argparse.ArgumentParser()
para.add_argument('--year', action='store', default='2015')
# parser = para.add_mutually_exclusive_group()
para.add_argument('-find', nargs='?')
para.add_argument('-create', nargs='?')
para.add_argument('-stats', action='store_true')
para.add_argument("-o", "--output", help="Output file name", default="images")
para.add_argument("--year", action="store", help="set search year", default="2015")
# parser = para.add_mutually_exclusive_group()
para.add_argument("-find", help="find dog", nargs="?")
para.add_argument("-create", help="create new Dog", action="store_true")
para.add_argument("-stats", help="show stat", action="store_true")
return para
def show_stats():
def show_stats(dog_list):
print("stats")
def find_dog(arg, dog_list):
for dog in dog_list:
if dog["StichtagDatJahr"] == arg.year and dog["HundenameText"] == arg.find:
print("name: " + dog["HundenameText"])
print("birth year: " + dog["GebDatHundJahr"])
print("sex: " + dog["SexHundLang"] + ",\n")
def get_image_url(url):
response = requests.get(url)
res = response.json()
return res["url"]
def find_dog(arg):
list = requestHandler(URL)
for l in list:
if(l['\ufeff"StichtagDatJahr"'] == arg.year and l['HundenameText'] == arg.find):
print("name: " + l['HundenameText'])
print("birth year: " + l['GebDatHundJahr'])
print("sex: " + l['SexHundLang'] + ",\n")
def create_dog(create):
print(create)
def create_image(path):
image_url = get_image_url(DOG_IMAGE_URL)
end = image_url.split(".")[-1]
response = requests.get(image_url, stream=True)
with open(path.with_suffix("." + end), "wb") as out_file:
shutil.copyfileobj(response.raw, out_file)
print(
f"The image of the new dog can be found here: " f"{path.with_suffix('.'+ end)}"
)
def create_dog(path, dog_list):
path = Path.Path(path)
hunde_name = dog_list[random.randint(0, len(dog_list))]["HundenameText"]
geb_date = dog_list[random.randint(0, len(dog_list))]["GebDatHundJahr"]
sex_hund = random.choice(["Male", "Female"])
path = path / f"{hunde_name.replace(' ', '_')}_{geb_date}"
print("Here's your new dog!")
print("Name: " + hunde_name)
print("Yirth year: " + geb_date)
print("Sex: : " + sex_hund)
create_image(path)
def parser_handler(args):
def parser_handler(args, dog_list):
if args.stats:
show_stats()
elif (args.find is not None):
find_dog(args)
elif (args.create is not None):
create_dog(args)
show_stats(dog_list)
elif args.find is not None:
find_dog(args, dog_list)
elif args.create:
create_dog(args.output, dog_list)
def requestHandler(url):
with requests.Session() as s:
download = s.get(url)
decoded_content = download.content.decode('utf-8')
cr = csv.DictReader(decoded_content.splitlines(), delimiter=',')
# download.encoding = "utf-8-sig"
decoded_content = download.content.decode("utf-8-sig")
cr = csv.DictReader(decoded_content.splitlines(), delimiter=",")
my_list = list(cr)
return my_list
def main(args=None):
parser = get_parser()
parsed = parser.parse_args(["-find", "Bob","--year" , "2020"])
#parsed = get_parser().parse_args(args)
parser_handler(parsed)
if __name__ == '__main__':
parsed = parser.parse_args(["-create"])
# parsed = get_parser().parse_args(args)
dog_list = requestHandler(DATA_URL)
parser_handler(parsed, dog_list)
if __name__ == "__main__":
main()
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment