School Commit Init

This commit is contained in:
2024-08-31 12:07:21 +03:00
commit 0b130ee18c
2801 changed files with 4720552 additions and 0 deletions
@@ -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")