From d4071070802a89c5f5dc53d8939952a59ee5ad1b Mon Sep 17 00:00:00 2001 From: Sayfudin90 Date: Sun, 2 Mar 2025 15:50:41 +0300 Subject: [PATCH] solving freevies errors --- lib/__pycache__/models.cpython-38.pyc | Bin 0 -> 4553 bytes lib/freebies.db | Bin 20480 -> 24576 bytes lib/models.py | 128 ++++++++++++++++++++++++-- lib/seed.py | 91 +++++++++++++++++- 4 files changed, 211 insertions(+), 8 deletions(-) create mode 100644 lib/__pycache__/models.cpython-38.pyc diff --git a/lib/__pycache__/models.cpython-38.pyc b/lib/__pycache__/models.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..1e77e1a9e593a8d3d203e62788cf8e1bad566c7e GIT binary patch literal 4553 zcmb7ITaVku73Pq-P+Cd5cGoVDrtS0+Gy-fXsheIli`2;)YxEL7)D8*|EeI}Wv`a5V z$}`k%BtEDaeQN03#jF1eA}qmTYs@@0wmp07c+R-y)y8$N&gh#RH^xn` zId(nQ^qsghZhLLCYoZ=8Z%fwYmS{X>UPrcNM>Nsu-nWD+TAx{>CA(LS*F(Q8w$R@) z{cZF+qKkgl^mjz>q1E61H>9!p^}KzUDj6Lozmc=NagfH-agsNGm1Oc*s=Va+3F#24PXnF1 zc=cltX?aXoo-LT?$eOHQLBYa#YI_aznpf5p^W0(_t(I()lA&B1YHo`rl*+*0dRp^3 zg+jfq>=mj6|F-Cf?awT4N9;hI*Yf5;I-Uf{EN_LT5lQ{%Im@>uDjElB=AS~p%|j*S zQ6%$b!7~Fxj}+)n(#aHBl{$Cs$+O%Y6^#11lee_Q;RV)EyEs(to<;IJP2yQdp0#-D zXwcyg@M_}AtPA_XxnLLYxTm$t+C>dw+Cj`LtIf;jKuOw@2RxDIycCW{iOzx~lswQn z4I^*}eja5fJUfXrFD2(eA}}ENF|3_%A`+#?;CRd8qy z=h1qNJIdtPxX2za9FZ$IQBsp5B*$iAsi+@mPR_M9`z(m3a*vn3{8dv8=i1z=^E;VM zRr10q9^u^3SUlsQ(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50ao z>&ULTK(w$z5A8MzAZl-mEzvR827oPouZeBM%T58UlvfI50k1?gjkd`o%Pro4+^$Ba0iHy}c7{*THT7ZnyF7DY&q z(G39Q0TofE1`@b_MKL1!D4k}jL9`)GH@bdN&F^vCJvT0YW>EIlA`bZq2-3Zy(86rJ zT`<_%0t$2@0)eyzw?CV?A7q_TUHW9MDm)RN3E&-N#<6)xVK-zY55be&KXv0ekdn}4rll0rfsb$Iy*KZD!)!grTW|5>spL39 zNSAfEv?sGy(&OmT_0I|Tg(Ai_f&{!VhvuZQy>p)!S0CU=G(A0yAFc$n?y**%u{vji z!>;FD+p5~6YUN5cIY6IU=9wW6ShwcWE+e-rH@{%H#3@&avr;O}l6Y*BdK-)sovK~1 zk26iSS>w&WQVr4VQUC70=w-LKWVvhProBcoL*C4WLHV*%sk)X-BCSsCaO~PBC2RvS zUv}+c!3pnWMtGLE_Pp)dPTsDNKbp+{?T#j^jkf$6J8pcWIA(sJmf|oWp52`^zt0#N z>?8OI-{2GM?P!C;aGpP&9N-S>lbp7>HI7eP!T~ zE(ozM#UB&$t2Nj|@B_ZW2iTK-FW`1UQ_;)~>Qj>Ii#z@)RYi08Htl+)Gq9Zg`27ldeXmeJItuA^xQI3i$mAv$qYI~~!{MG2K?FAzOa@fh1=XH_Ms zjifo+==5Y=R@9WLqYE-(l?JR&av}&k21-f^Q4+(IfHyjlSeELDVqzUl;aIFXS84Np GkopY)=hG_y delta 87 zcmZoTz}T>Wae}lUD+2=q2*UvLL>*&MRtCLzSzi7h3@m(74E((OJNfGQq&5o*EaTlQ Y#rH~)i9rC!xz9gwf;1a1NF$sJ0HVYZR{#J2 diff --git a/lib/models.py b/lib/models.py index 2681bee5a..8a62b4291 100644 --- a/lib/models.py +++ b/lib/models.py @@ -6,24 +6,140 @@ "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", } metadata = MetaData(naming_convention=convention) - Base = declarative_base(metadata=metadata) class Company(Base): __tablename__ = 'companies' - id = Column(Integer(), primary_key=True) name = Column(String()) founding_year = Column(Integer()) - + + # Relationship to Freebie + freebies = relationship('Freebie', back_populates='company') + + # Relationship to Dev through Freebie (many-to-many) + devs = relationship( + 'Dev', + secondary='freebies', + back_populates='companies', + viewonly=True + ) + + def give_freebie(self, dev, item_name, value): + """ + Creates a new Freebie instance associated with this company and the given dev. + + Args: + dev: An instance of the Dev class + item_name: String representing the freebie's name + value: Integer representing the freebie's value + + Returns: + The newly created Freebie instance + """ + freebie = Freebie( + item_name=item_name, + value=value, + dev=dev, + company=self + ) + return freebie + + @classmethod + def oldest_company(cls): + """ + Returns the Company instance with the earliest founding year. + + Returns: + Company: The company with the earliest founding year + """ + from sqlalchemy.orm import Session + from sqlalchemy import create_engine + + engine = create_engine('sqlite:///freebies.db') + session = Session(engine) + + oldest = session.query(cls).order_by(cls.founding_year).first() + session.close() + + return oldest + def __repr__(self): return f'' + class Dev(Base): __tablename__ = 'devs' - id = Column(Integer(), primary_key=True) - name= Column(String()) - + name = Column(String()) + + # Relationship to Freebie + freebies = relationship('Freebie', back_populates='dev') + + # Relationship to Company through Freebie (many-to-many) + companies = relationship( + 'Company', + secondary='freebies', + back_populates='devs', + viewonly=True + ) + + def received_one(self, item_name): + """ + Checks if any of the freebies associated with the dev has the given item_name. + + Args: + item_name: String representing the item name to check for + + Returns: + Bool: True if the dev has a freebie with the given item_name, False otherwise + """ + for freebie in self.freebies: + if freebie.item_name == item_name: + return True + return False + + def give_away(self, dev, freebie): + """ + Gives a freebie to another dev if the freebie belongs to this dev. + + Args: + dev: An instance of the Dev class who will receive the freebie + freebie: An instance of the Freebie class to be given away + + Returns: + None + """ + if freebie in self.freebies: + freebie.dev = dev + def __repr__(self): return f'' + + +class Freebie(Base): + __tablename__ = 'freebies' + + id = Column(Integer(), primary_key=True) + item_name = Column(String(), nullable=False) + value = Column(Integer(), nullable=False) + + # Foreign keys + dev_id = Column(Integer(), ForeignKey('devs.id'), nullable=False) + company_id = Column(Integer(), ForeignKey('companies.id'), nullable=False) + + # Relationships + dev = relationship('Dev', back_populates='freebies') + company = relationship('Company', back_populates='freebies') + + def print_details(self): + """ + Returns a formatted string with details about the freebie. + + Returns: + String: Formatted string with dev name, freebie item_name, and company name + """ + return f"{self.dev.name} owns a {self.item_name} from {self.company.name}." + + def __repr__(self): + return f'' \ No newline at end of file diff --git a/lib/seed.py b/lib/seed.py index b16becbbb..6dd987ce3 100644 --- a/lib/seed.py +++ b/lib/seed.py @@ -1,3 +1,90 @@ -#!/usr/bin/env python3 +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker +from models import Base, Company, Dev, Freebie -# Script goes here! + +engine = create_engine('sqlite:///freebies.db') + + +Base.metadata.create_all(engine) + + +Session = sessionmaker(bind=engine) +session = Session() + + +session.query(Freebie).delete() +session.commit() + + +companies_data = [ + {"name": "Google", "founding_year": 1998}, + {"name": "Microsoft", "founding_year": 1975}, + {"name": "Amazon", "founding_year": 1994}, + {"name": "Apple", "founding_year": 1976} +] + +companies = {} +for company_data in companies_data: + company = session.query(Company).filter_by(name=company_data["name"]).first() + if not company: + company = Company(**company_data) + session.add(company) + companies[company_data["name"]] = company + + +devs_data = [ + {"name": "Alice Smith"}, + {"name": "Bob Johnson"}, + {"name": "Charlie Williams"}, + {"name": "Dana Brown"} +] + +devs = {} +for dev_data in devs_data: + dev = session.query(Dev).filter_by(name=dev_data["name"]).first() + if not dev: + dev = Dev(**dev_data) + session.add(dev) + devs[dev_data["name"]] = dev + +session.commit() + +# Creating freebies +freebies_data = [ + {"item_name": "T-Shirt", "value": 20, "dev": devs["Alice Smith"], "company": companies["Google"]}, + {"item_name": "Sticker Pack", "value": 5, "dev": devs["Alice Smith"], "company": companies["Microsoft"]}, + {"item_name": "Water Bottle", "value": 15, "dev": devs["Bob Johnson"], "company": companies["Google"]}, + {"item_name": "Backpack", "value": 50, "dev": devs["Bob Johnson"], "company": companies["Amazon"]}, + {"item_name": "Laptop Sleeve", "value": 30, "dev": devs["Charlie Williams"], "company": companies["Microsoft"]}, + {"item_name": "Notebook", "value": 10, "dev": devs["Charlie Williams"], "company": companies["Apple"]}, + {"item_name": "Hoodie", "value": 40, "dev": devs["Dana Brown"], "company": companies["Amazon"]}, + {"item_name": "Coffee Mug", "value": 12, "dev": devs["Dana Brown"], "company": companies["Google"]} +] + +for freebie_data in freebies_data: + freebie = Freebie(**freebie_data) + session.add(freebie) + +session.commit() + +print("Database seeded successfully!") + + +print("\nSample data verification:") +print(f"Total companies: {session.query(Company).count()}") +print(f"Total devs: {session.query(Dev).count()}") +print(f"Total freebies: {session.query(Freebie).count()}") + + +alice = devs["Alice Smith"] +google = companies["Google"] + +print(f"\nAlice's freebies: {[f.item_name for f in alice.freebies]}") +print(f"Companies Alice has freebies from: {[c.name for c in alice.companies]}") +print(f"Google's freebies: {[f.item_name for f in google.freebies]}") +print(f"Devs with Google freebies: {[d.name for d in google.devs]}") + +import ipdb + +ipdb.set_trace(context=10) \ No newline at end of file