School Commit Init
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
pip-wheel-metadata/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
.python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
#Pipfile.lock
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
@@ -0,0 +1 @@
|
||||
# commit & push your solution to Test 2 to this repository
|
||||
@@ -0,0 +1,28 @@
|
||||
class Address:
|
||||
def __init__(self,id,name,number,x,y) -> None:
|
||||
self.id=id
|
||||
self.name=name
|
||||
self.number=number
|
||||
self.x=x
|
||||
self.y=y
|
||||
|
||||
def get_id(self):
|
||||
return self.id
|
||||
|
||||
def get_name(self):
|
||||
return self.name
|
||||
|
||||
def get_number(self):
|
||||
return self.number
|
||||
|
||||
def get_x(self):
|
||||
return self.x
|
||||
|
||||
def get_y(self):
|
||||
return self.y
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return str(self)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return str(self.id)+","+str(self.name)+","+str(self.number)+","+str(self.x)+","+str(self.y)
|
||||
@@ -0,0 +1,11 @@
|
||||
from ui import UI
|
||||
from repository import Repo
|
||||
from services import Service
|
||||
|
||||
|
||||
repo=Repo("text_file.txt")
|
||||
service=Service(repo)
|
||||
ui=UI(service)
|
||||
|
||||
ui()
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
from domain import Address
|
||||
|
||||
class Repo:
|
||||
def __init__(self,path) -> None:
|
||||
self.path=path
|
||||
self.addresses=[]
|
||||
self.load_file()
|
||||
|
||||
def load_file(self):
|
||||
with open(self.path,"r") as f:
|
||||
for line in f:
|
||||
attributes=line.split(",")
|
||||
self.addresses.append(Address(int(attributes[0]),attributes[1],int(attributes[2]),int(attributes[3]),int(attributes[4])))
|
||||
|
||||
def save_file(self):
|
||||
with open(self.path,"w") as f:
|
||||
for address in self.addresses:
|
||||
string_address = str(address.get_id())+","+str(address.get_name())+","+str(address.get_number())+","+str(address.get_x())+","+str(address.get_y())+"\n"
|
||||
f.write(string_address)
|
||||
|
||||
def add_address(self,address_to_add):
|
||||
for address in self.addresses:
|
||||
if address.get_id() == address_to_add.get_id():
|
||||
raise ValueError("Id of address already exists")
|
||||
self.addresses.append(address_to_add)
|
||||
self.save_file()
|
||||
|
||||
def get_addresses_as_string(self):
|
||||
string_of_addresses=""
|
||||
for address in self.addresses:
|
||||
string_of_addresses+=str(address)
|
||||
string_of_addresses+="\n"
|
||||
return string_of_addresses
|
||||
|
||||
def get_addresses(self):
|
||||
return self.addresses
|
||||
@@ -0,0 +1,77 @@
|
||||
from domain import Address
|
||||
|
||||
class Service:
|
||||
def __init__(self,repo) -> None:
|
||||
self.repo=repo
|
||||
|
||||
def add_address(self,id,name,number,x,y):
|
||||
if not id.isdigit():
|
||||
raise ValueError("Id must be a positive integer")
|
||||
if len(name)<3:
|
||||
raise ValueError("Name must be at least 3 characters")
|
||||
if not number.isdigit() or int(number)>100:
|
||||
raise ValueError("Number must be a positive integer less or equal to 100")
|
||||
try:
|
||||
int(x)
|
||||
except:
|
||||
raise ValueError("X must be an integer")
|
||||
if int(x)>100 or int(x)<-100:
|
||||
raise ValueError("X must be an integer between -100 and 100")
|
||||
try:
|
||||
int(y)
|
||||
except:
|
||||
raise ValueError("Y must be an integer")
|
||||
if int(y)>100 or int(y)<-100:
|
||||
raise ValueError("Y must be an integer between -100 and 100")
|
||||
address=Address(int(id),name,int(number),int(x),int(y))
|
||||
self.repo.add_address(address)
|
||||
|
||||
def get_addresses(self):
|
||||
return self.repo.get_addresses_as_string()
|
||||
|
||||
def get_distance(self,x,y,dist):
|
||||
"""
|
||||
:params:
|
||||
x - str
|
||||
y - str
|
||||
dist - str
|
||||
|
||||
:return:
|
||||
to_return - list of tuples of Address and int
|
||||
|
||||
The function verifies the input and calculates the distance between the given position and all adresses and return the adresses with the distance smaller than the one specify
|
||||
"""
|
||||
try:
|
||||
int(x)
|
||||
except:
|
||||
raise ValueError("X must be an integer")
|
||||
if int(x)>100 or int(x)<-100:
|
||||
raise ValueError("X must be an integer between -100 and 100")
|
||||
try:
|
||||
int(y)
|
||||
except:
|
||||
raise ValueError("Y must be an integer")
|
||||
if int(y)>100 or int(y)<-100:
|
||||
raise ValueError("Y must be an integer between -100 and 100")
|
||||
if not dist.isdigit():
|
||||
raise ValueError("Distance must be a positive integer")
|
||||
data=self.repo.get_addresses()
|
||||
to_return=[]
|
||||
for address in data:
|
||||
distance=((int(x)-address.get_x())**2+(int(y)-address.get_y())**2)**(1/2)
|
||||
if distance<=int(dist):
|
||||
to_return.append((address,distance))
|
||||
return to_return
|
||||
|
||||
def get_coordinates(self):
|
||||
n=0
|
||||
x=0
|
||||
y=0
|
||||
data=self.repo.get_addresses()
|
||||
for address in data:
|
||||
n+=1
|
||||
x+=address.get_x()
|
||||
y+=address.get_y()
|
||||
if n==0:
|
||||
raise ValueError("There are no addresses")
|
||||
return (x/n,y/n)
|
||||
@@ -0,0 +1,19 @@
|
||||
import unittest
|
||||
from services import Service
|
||||
from repository import Repo
|
||||
|
||||
class Test(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.repo=Repo("test_file.txt")
|
||||
self.service=Service(self.repo)
|
||||
return super().setUp()
|
||||
def tearDown(self) -> None:
|
||||
return super().tearDown()
|
||||
def test(self):
|
||||
returned = self.service.get_distance("12","20","1")
|
||||
self.assertEqual(len(returned),1)
|
||||
self.assertEqual(returned[0][0].get_id(),1)
|
||||
self.assertEqual(returned[0][1],0)
|
||||
|
||||
|
||||
unittest.main()
|
||||
@@ -0,0 +1,47 @@
|
||||
class UI:
|
||||
def __init__(self,services) -> None:
|
||||
self.services=services
|
||||
|
||||
def __call__(self):
|
||||
while True:
|
||||
print("""
|
||||
1.Add address
|
||||
2.Display all addresses
|
||||
3.Determine where to put taxi stations
|
||||
4.Determine coordinates
|
||||
5.Exit
|
||||
""")
|
||||
command=input(">")
|
||||
match command:
|
||||
case "1":
|
||||
try:
|
||||
id=input("Input an id:")
|
||||
name=input("Input a name:")
|
||||
number=input("Input a number:")
|
||||
x=input("Input an x coordinate:")
|
||||
y=input("Input an y coordinate:")
|
||||
self.services.add_address(id,name,number,x,y)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
case "2":
|
||||
print(self.services.get_addresses())
|
||||
case "3":
|
||||
try:
|
||||
x=input("Input an x coordinate:")
|
||||
y=input("Input an y coordinate:")
|
||||
distance=input("Input a distance:")
|
||||
addresses=self.services.get_distance(x,y,distance)
|
||||
for address, distance in addresses:
|
||||
print(str(address)+" "+str(distance)+"\n")
|
||||
except Exception as e:
|
||||
print(e)
|
||||
case "4":
|
||||
try:
|
||||
coordinates=self.services.get_coordinates()
|
||||
print("Coordinates: "+str(coordinates[0])+" "+str(coordinates[1]))
|
||||
except Exception as e:
|
||||
print(e)
|
||||
case "5":
|
||||
return
|
||||
case _:
|
||||
print("Invalid command")
|
||||
@@ -0,0 +1 @@
|
||||
1,Theodor Mihail,10,12,20
|
||||
@@ -0,0 +1,10 @@
|
||||
1,Theodor Mihail,10,12,20
|
||||
2,Theodor Mihail,15,16,30
|
||||
3,Mihail Kogalniceanu,1,40,45
|
||||
4,Dorobantilor,20,50,100
|
||||
5,Dorobantilor,22,54,100
|
||||
6,Salciilor,2,2,20
|
||||
7,Salciilor,50,46,18
|
||||
8,Mihai Eminescu,2,90,90
|
||||
9,Mihai Eminescu,3,90,92
|
||||
10,Titulescu,10,10,12
|
||||
@@ -0,0 +1,2 @@
|
||||
2,Theodor Mihail,15,16,30
|
||||
3,Mihail Kogalniceanu,1,40,45
|
||||
Reference in New Issue
Block a user