From 64c7fc91a3db07ee7ddbff45fd4183b672a6427e Mon Sep 17 00:00:00 2001 From: Bill Brown Date: Wed, 12 Apr 2023 16:04:06 -0700 Subject: [PATCH 1/3] moc challenge of python --- lib/company.py | 83 ++++++++++++++++++++++++++++++++++++++++++ lib/debug.py | 54 ++++++++++++++++++++++++++-- lib/dev.py | 81 +++++++++++++++++++++++++++++++++++++++++ lib/freebie.py | 94 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/freebies.db | Bin 20480 -> 24576 bytes lib/models.py | 29 --------------- lib/seed.py | 3 -- 7 files changed, 309 insertions(+), 35 deletions(-) create mode 100644 lib/company.py create mode 100644 lib/dev.py create mode 100644 lib/freebie.py delete mode 100644 lib/models.py delete mode 100644 lib/seed.py diff --git a/lib/company.py b/lib/company.py new file mode 100644 index 000000000..fcffe5363 --- /dev/null +++ b/lib/company.py @@ -0,0 +1,83 @@ +import sqlite3 +from freebie import Freebie + +CONN = sqlite3.connect("./lib/freebies.db") +CURSOR = CONN.cursor() + +class Company(): + def __init__(self, name, founding_year, id=None): + self.name = name + self.founding_year = founding_year + self.id = id + + # Create a table that companies get saved into: + @classmethod + def create_table(cls): + sql = """ + CREATE TABLE IF NOT EXISTS companies ( + id INTEGER PRIMARY KEY, + name TEXT, + founding_year INTEGER + ) + """ + CURSOR.execute(sql) + + # Create a drop table to refresh every time code is ran + @classmethod + def drop_table(cls): + sql = "DROP TABLE IF EXISTS companies" + CURSOR.execute(sql) + + # Create option to save information to Table once created: + def save(self): + sql = """ + INSERT INTO companies (name, founding_year) + VALUES (?, ?) + """ + CURSOR.execute(sql, (self.name, self.founding_year)) + CONN.commit() + self.id = CURSOR.lastrowid + + #! Create a freebies property for company + def get_freebies(self): + sql = """ + SELECT * FROM freebies + WHERE comp_id = ? + """ + found_freebies = CURSOR.execute(sql, (self.id,)).fetchall() + return found_freebies + + freebies = property(get_freebies) + + #! Create a devs property for company + def get_devs(self): + sql = """ + SELECT devs.id, devs.name + FROM devs + INNER JOIN freebies + ON devs.id = freebies.dev_id + WHERE freebies.comp_id = ? + """ + found_devs = CURSOR.execute(sql, (self.id,)).fetchall() + return found_devs + + devs = property(get_devs) + + + #*Aggregate Methods + def give_freebie(self,Dev,item_name,value): + item_name = Freebie.create(item_name,value,self.id,Dev.id) + return item_name + + @classmethod + def oldest_company(cls): + sql = """ + SELECT * + FROM companies + ORDER BY founding_year + ASC LIMIT 1 + """ + oldest_comp = CURSOR.execute(sql).fetchone() + return oldest_comp + + \ No newline at end of file diff --git a/lib/debug.py b/lib/debug.py index 4f922eb69..4137e5e0f 100644 --- a/lib/debug.py +++ b/lib/debug.py @@ -1,9 +1,57 @@ #!/usr/bin/env python3 -from sqlalchemy import create_engine +# from sqlalchemy import create_engine + +# from models import Company, Dev, Freebies + +from freebie import Freebie +from company import Company +from dev import Dev + +#! Drop all tables when first started to see if it works: +Company.drop_table() +Dev.drop_table() +Freebie.drop_table() + +#! Create all (3) tables to exist in the database: +Company.create_table() +Dev.create_table() +Freebie.create_table() + + +#! Create some dev instances +ollie = Dev("ollie") +ollie.save() +wally = Dev("Wally") +wally.save() +bill = Dev("Bill") +bill.save() + +#! Create some company instances + +docusign = Company("DocuSign", 1999) +docusign.save() +gitHub = Company("GitHub", 2002) +gitHub.save() +walmart = Company("Walmart", 1804) +walmart.save() + +#! Create some Freebie instances +koozie = Freebie("Koozie", 10, gitHub.id, bill.id) +koozie.save() +hat = Freebie("Hat", 20, gitHub.id, wally.id) +hat.save() +pen = Freebie("Pen", 1, docusign.id, bill.id) +pen.save() + + + + + + + -from models import Company, Dev if __name__ == '__main__': - engine = create_engine('sqlite:///freebies.db') + # engine = create_engine('sqlite:///freebies.db') import ipdb; ipdb.set_trace() diff --git a/lib/dev.py b/lib/dev.py new file mode 100644 index 000000000..291c61aa8 --- /dev/null +++ b/lib/dev.py @@ -0,0 +1,81 @@ +import sqlite3 + +CONN = sqlite3.connect("./lib/freebies.db") +CURSOR = CONN.cursor() + +from freebie import Freebie + +#* All Dev Table in this color +class Dev(): + def __init__(self, name, id=None): + self.name = name + self.id = id + + #* Create a table that devs get saved into: + @classmethod + def create_table(cls): + sql = """ + CREATE TABLE IF NOT EXISTS devs ( + id INTEGER PRIMARY KEY, + name TEXT + ) + """ + CURSOR.execute(sql) + + #* Create a drop table to refresh every time code is ran + @classmethod + def drop_table(cls): + sql = "DROP TABLE IF EXISTS devs" + CURSOR.execute(sql) + + #* Create option to save information to Table once created: + def save(self): + sql = """ + INSERT INTO devs (name) + VALUES (?) + """ + CURSOR.execute(sql, (self.name,)) #* leave comma in when one item + CONN.commit() + self.id = CURSOR.lastrowid + + #! Create a freebies property for devs + def get_freebies(self): + sql = """ + SELECT * FROM freebies + WHERE dev_id = ? + """ + found_freebies = CURSOR.execute(sql, (self.id,)).fetchall() + return found_freebies + + freebies = property(get_freebies) + + #! Create a devs property for company + def get_companies(self): + sql = """ + SELECT companies.id, companies.name + FROM companies + INNER JOIN freebies + ON companies.id = freebies.comp_id + WHERE freebies.dev_id = ? + """ + found_companies = CURSOR.execute(sql, (self.id,)).fetchall() + return found_companies + + companies = property(get_companies) + + #* Aggregate Methods + def received_one(self, item_name): + all_freebies = [freebie[1].lower() for freebie in self.freebies] + if item_name.lower() in all_freebies: + return True + else: + return False + + def give_away(self, Dev, Freebie): + if self.id == Freebie.dev_id: + Freebie.update_dev_id(Dev.id) + return (f"{Freebie.item_name} now belongs to {Dev.name}") + else: + return "Freebie Doesn't Belong to you" + + \ No newline at end of file diff --git a/lib/freebie.py b/lib/freebie.py new file mode 100644 index 000000000..b9928c613 --- /dev/null +++ b/lib/freebie.py @@ -0,0 +1,94 @@ +import sqlite3 + +CONN = sqlite3.connect("./lib/freebies.db") +CURSOR = CONN.cursor() + +#! Create Freebies Class +class Freebie(): + def __init__(self, item_name, value, comp_id=None, dev_id=None, id=None): + self.id = id + self.item_name = item_name + self.value = value + self.comp_id = comp_id + self.dev_id = dev_id + + #! Create a table that freebies get saved into: + @classmethod + def create_table(cls): + sql = """ + CREATE TABLE IF NOT EXISTS freebies ( + id INTEGER PRIMARY KEY, + item_name TEXT, + value INTEGER, + comp_id INTEGER, + dev_id INTEGER + ) + """ + + CURSOR.execute(sql) + + #! Create a drop table to refresh every time code is ran + @classmethod + def drop_table(cls): + sql = "DROP TABLE IF EXISTS freebies" + CURSOR.execute(sql) + + #! Create option to save information to Table once created: + def save(self): + sql = """ + INSERT INTO freebies (item_name, value, comp_id, dev_id) + VALUES (?, ?, ?, ?) + """ + CURSOR.execute(sql, (self.item_name, self.value, self.comp_id, self.dev_id)) + CONN.commit() + self.id = CURSOR.lastrowid + #! Create function that makes instance and saves to database + @classmethod + def create(cls,item_name, value, comp_id, dev_id): + item_name = cls(item_name, value, comp_id, dev_id) + item_name.save() + return item_name + + + #! Create a dev property for frisby. + def get_dev(self): + sql = """ + SELECT * FROM devs + WHERE id = ? + LIMIT 1 + """ + found_dev = CURSOR.execute(sql, (self.dev_id,)).fetchone() + return found_dev + + dev = property(get_dev) + + #! Create a company property for frisby + def get_company(self): + sql = """ + SELECT * FROM companies + WHERE id = ? + LIMIT 1 + """ + found_company = CURSOR.execute(sql, (self.comp_id,)).fetchone() + return found_company + + company = property(get_company) + + + #! Create an update freebie + def update_dev_id(self,new_dev_id): + sql = """ + UPDATE freebies + SET dev_id = ? + WHERE id = ? + """ + CURSOR.execute(sql, (new_dev_id, self.id)) + CONN.commit() + + + + #*Aggregate Methods + def print_details(self): + print(f"{self.dev[1]} owns a {self.item_name} from {self.company[1]}") + + \ No newline at end of file diff --git a/lib/freebies.db b/lib/freebies.db index 12beb1c963e832db481e7a7493e3029e691ac4dc..610ef2cde8a3ecfaf42f49cbd77833b11a2bf91e 100644 GIT binary patch literal 24576 zcmeI&O-~a+7zgl~-K9&LmNC($A*Nw2Bs36TibO6Z7Tf@#7FbpRFPm*Ug^l}y?Uo2P z@KgB(yqajdnkZKjjR)h=X{Fe9<4I$L{3qS)Gc((nd4AnJOxw+kvKtBB2%EkW@mX?( zP)hD|PKY`~2uaA}tk69fXuX;|)4upOp&a9eozJlEWF-BSunc=S_9XLOhA0q#00bZa z0SG_<0uX=z1pa}*u{)yY@_G7VGIFY(Xf%bWx}w!PYm25)vJ7sO7Rm}N?(kJ(XC{7KBz!w?e8DYa+lu!eI9^-yhKhIBLVw@>HCOzCx;W_T zADF&yr0KbAmL6|)Q2x;9vJ|DE6eHTH_I&+G$xCj%4+KmY;|fB*y_009U<00Izz00jP1fsB?Q zSxRYZFBC;pNvR2PHJPOAB2d$cjAhrd6J1jjMiXRQ)>gyt$Q3C??d<a%kg}t8R_Y?qLFcB$&HrVRsGEv)kt1> z5Z2n8?rxyJ{<;4@CF~pf#7-|pf>Wae|Z(11kdq7y~IVV4kRBBnA}Gi' - -class Dev(Base): - __tablename__ = 'devs' - - id = Column(Integer(), primary_key=True) - name= Column(String()) - - def __repr__(self): - return f'' diff --git a/lib/seed.py b/lib/seed.py deleted file mode 100644 index b16becbbb..000000000 --- a/lib/seed.py +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env python3 - -# Script goes here! From 034dda24fd1370c9c3b5620531e002bedb17dc6d Mon Sep 17 00:00:00 2001 From: Bill Brown Date: Thu, 13 Apr 2023 16:58:02 -0700 Subject: [PATCH 2/3] Bill's version --- lib/__pycache__/company.cpython-38.pyc | Bin 0 -> 3371 bytes lib/__pycache__/dev.cpython-38.pyc | Bin 0 -> 2661 bytes lib/__pycache__/freebie.cpython-38.pyc | Bin 0 -> 2841 bytes lib/company.py | 28 ++++++++++++++++++++++++- lib/debug.py | 3 --- lib/dev.py | 12 +++++++++++ lib/freebie.py | 25 ++++++++++++++++++++++ lib/freebies.db | Bin 24576 -> 24576 bytes 8 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 lib/__pycache__/company.cpython-38.pyc create mode 100644 lib/__pycache__/dev.cpython-38.pyc create mode 100644 lib/__pycache__/freebie.cpython-38.pyc diff --git a/lib/__pycache__/company.cpython-38.pyc b/lib/__pycache__/company.cpython-38.pyc new file mode 100644 index 0000000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25E*-*vF@c*$B7j?nOae~#wsl1MmoYwH;~h{cuTD!!5$+m) E1M4=~3;+NC literal 0 HcmV?d00001 diff --git a/lib/__pycache__/dev.cpython-38.pyc b/lib/__pycache__/dev.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..c538266560e5759290f51b479224ae340155bb9a GIT binary patch literal 2661 zcmbtW&2HO95Z+x<5+zHvoc^FG+G1O@XcWX!(I7w$X;R0gQ>6|SNOGG9CJ&#!33oVDddQ|2A}j2s2THl-aV*||0)9UUZ^o9SmeSxO!dp^MBJ5t&!4 zlM}Q>D%rR2HL=F))tcMeuN^2U_j}T>iv0==b35_WL7nuIoP7YRl60sb9VV^_CbTo! z(WD-q^NMydFwTnH8G&(D7$Sd0;N*f>fwv*^(t!1XD2fuyS75#Z^P*T4YcMZ~vRH@r zYGmnqp|&lLEeFq@KR9O4wxIcC=q7YRY%*ccgH3>sS``-RgKl4jtS2g3sQc1AP&(+U z40LaZ%E9ZOz1p5Hm0#QMx$eI5P6jnNOuF8%F9$)*@r0}mpLKn?akJL<9NTef!_(lI zH`o~7+?bKJ5vZ>7T&jpjA&;xW)6lT(-k=xQHu}=XQwhz|aS=AJ&P5Kz<=KO15LJZe z3Je5&Ndxfegia|1KLaLajejff)U(wzH`^v}Z{FWEd3}dljW#!*)LZQq2RnRzZAlwH z>6P+c!mgN_ZvkD?{y7@i68PhfHxIi{YM zY{KGTtuLW%nY-pzn_uTU&Bh}>PdG~|KmTAhO{BtxIQ@>_ON4Lq6lA=

ZGs=ULZv zE7}5HxO(7?2Ev|iei3f*PzH(cI&9-3GYI>A8i!$8;s1gED={A3I0FwJ8g>VmXM5rX zU(C?&OZpK^w=B554;yvs-6S@w)W#8yb2_THAyB)tUCeo+@(%LVhrm&q=FVD7 zb7J2`o=Zq$BE6L&ItN6rV{|QPIGFwB4~AKi+v5u+*cpM#MYf@uKzPw}I)z;SJgd0S zT2R}dVSdb5OD@ArFkV~G{EwhJgiU@azl4-3$W?MqrsQ|_0zR(Lx0s5d@MT=@L@KBp ztoluun~`!6UEz+~^8uA(V8_b^MQB5_n}e_4t*YYeEjK7{L{d< zJ?Rg=4*2~@h_X{}RLMliD9pwI7M4asf%PDss?WiYD3{IS3{6z%!HeMYhrq7gJ?Wl4 z1nDeq6#@AbHZDbxHJ9T*z#3L!WYo69f^GLbF>-NSwCxw8t{Y1f{AQ7G#eux(_a#(! zq41wVtz-8Ac2`ls7(%&_DmYaf*RW)C@=27dFL3cI>=tDv8liI(;=yC0Ks0C}!)T!Z zEn^f6!_bTsXt_wMVuU#;pb!f;!<^#{2GR)>{t-ZeA1U8caVq=_cZ~>&sCvhlp>Ze1 Qdl#cSV#;6zYOtdAFN8%w8UO$Q literal 0 HcmV?d00001 diff --git a/lib/__pycache__/freebie.cpython-38.pyc b/lib/__pycache__/freebie.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..013b68d3a3e28e49db98a85a6d3394fe29e06e82 GIT binary patch literal 2841 zcmbtW&2!sC6yMdCWyem^GzAJ}5X>-Bb&`0PcHjb(G*yd-#O~M*G~%F9v}PYSgDi<4DZ1=YzJn4<6?2h zu=oLQT~b@((uI6UY~?9oGE>e8Q_dt?Wwb?1^(~wr^~sH$s^2}X zb_D0gT^{VPV-FdNW2QvFE+;l+#Fp45lbC!?ZJ8@fVd^=K2=vasN7ciCi=cYk_5EWpd^M;(<|o`A zj{1BMR=pwP)zJ$#;M@1B{h{Z0UUhUDz8DU+NB6f=+_yvFdcSkA=!>1vX{0+&chC(T z2bBhKEKn`|o2;j(>|{b71*vQxGU&M&oeh{U`kICi8dG{nDFh1yfmEOTQ!*CDUenrb zTSj~L(Y|HWo*4B;+pwP3TJ4sRMTl`XcLEQ0nNh2^t*2Jgc-E{P>^6^#pRA*;t8y9S zM%#MczRHjNkp0wEcIuyZ3nYHa_&N3FZ1qNpw7tV-tI>=K{15JpLk?TA=LeA-y!2z2 zCS(Obi^`thZpfX`J@)xJ>;&j~A}OWGW`_Tw$ou%<)5p!mvpMKXfQt&~6R3LGwh|M= zQDWI=z|JhAla0)>xxf{>FqY%kcmR`mY)nZHGN%W5(}S$(sU77~q9oKV3YP@lq^CnJ z6|?9W%wWYhsMT9mvyJ#S7Q$tgXj?|?;4LF{z8i#)kX;t%t0{>h9>io=hEfUK6aFPI0?af*73B`q z>7<+iiEIxwW&`K=#1MWAlPMM+EI7)U@*0xtf*@d1sI>yq(4^u+e5v9Fysk5T*(5Cq zs~r#z;<6{LqDiZnNeMgWS5$=I85?y6CV9Vh=w;jxUQ1Ic$^OF}^et=O+G`v4j3>>; z0Xiqhv48o|YFe1=-x&{cjQ!d{t!;cY+cT8~HzA&p-r=DKC6Al(+y{2CGaL_?1B#aK zUigiNxc@aAPA*Rsb(2okvdgFZ|95+PW-&%@{`MH7?f^PsPUig}h}*_(xJvH!mE`t` zqSIvrR`7-?@6i$pHy2SzWjw6+*}yoM1t)e!hyy9E{StP^p45@ztd=hn4ALx{k8*cC zeD)Z7K_~q($njcMJ5Amz!Eh}^5cc!L#RwI*kbD3n#l$WR_$zcdblz(-v@#wsEX0XO z15VBYV}nj^Wsu~ga^Zo^aaQ#1!6Jm5?NLT=lRpW=q9?%{3!j|q{iUAK6da}g7LQMK z1KN9FxW+^SV1|9eR3ddGx`Qy1F^m!PR!<2@QuO5O;o;# z1j~>32nm*}NFPD-;37N~*gM20NDL%cH^k>i=7z^Jv|ga8h+=?ZIxXoEEv>?%>P4Uh zy{OCZD0(@TG&Q>bO%+w=R{a GF8>GiOH0K7 literal 0 HcmV?d00001 diff --git a/lib/company.py b/lib/company.py index fcffe5363..76b6e5668 100644 --- a/lib/company.py +++ b/lib/company.py @@ -6,9 +6,35 @@ class Company(): def __init__(self, name, founding_year, id=None): - self.name = name + self.set_name(name) self.founding_year = founding_year self.id = id + + #!Validate the properties + def get_name(self): + return self._name + + def set_name(self, new_name): + if type(new_name) == str and len(new_name) > 0: + self._name = new_name + else: + print("name is not a string") + raise Exception() + + name = property(get_name, set_name) + + #!Another way to make a property + @property + def founding_year(self): + return self._founding_year + + @founding_year.setter + def founding_year(self, new_year): + if type(new_year) == int and new_year > 0: + self._founding_year = new_year + else: + raise Exception("Incorrect Year") + # Create a table that companies get saved into: @classmethod diff --git a/lib/debug.py b/lib/debug.py index 4137e5e0f..38ccbaead 100644 --- a/lib/debug.py +++ b/lib/debug.py @@ -49,9 +49,6 @@ - - - if __name__ == '__main__': # engine = create_engine('sqlite:///freebies.db') import ipdb; ipdb.set_trace() diff --git a/lib/dev.py b/lib/dev.py index 291c61aa8..6392ae877 100644 --- a/lib/dev.py +++ b/lib/dev.py @@ -10,6 +10,18 @@ class Dev(): def __init__(self, name, id=None): self.name = name self.id = id + + @property + def name(self): + return self._name + + @name.setter + def name(self,new_name): + if type(new_name) == str: + self._name = new_name + else: + print("Name entered is not a string") + #* Create a table that devs get saved into: @classmethod diff --git a/lib/freebie.py b/lib/freebie.py index b9928c613..351ebd198 100644 --- a/lib/freebie.py +++ b/lib/freebie.py @@ -12,6 +12,29 @@ def __init__(self, item_name, value, comp_id=None, dev_id=None, id=None): self.comp_id = comp_id self.dev_id = dev_id + @property + def item_name(self): + return self._item_name + @item_name.setter + def item_name(self,new_item_name): + if type(new_item_name)==str: + self._item_name = new_item_name + else: + raise Exception("Bad Item Name...") + + @property + def dev_id(self): + return self._dev_id + @dev_id.setter + def dev_id(self, dev_id): + all_devs = CURSOR.execute("SELECT id from devs").fetchall() + all_ids = [row[0] for row in all_devs] + if dev_id in all_ids: + self._dev_id = dev_id + else: + raise Exception("No Dev with id provided") + + #! Create a table that freebies get saved into: @classmethod def create_table(cls): @@ -90,5 +113,7 @@ def update_dev_id(self,new_dev_id): #*Aggregate Methods def print_details(self): print(f"{self.dev[1]} owns a {self.item_name} from {self.company[1]}") + + \ No newline at end of file diff --git a/lib/freebies.db b/lib/freebies.db index 610ef2cde8a3ecfaf42f49cbd77833b11a2bf91e..f85ad1c9097ff5ac369f2cb74bf80b0ca736a036 100644 GIT binary patch delta 56 zcmZoTz}Rqrae}nq0tN;KHXw!q#u*cJj2RbfOjx4N#ms+|f&V@KL;kCq1qF`tZ@y}; GtpEUl Date: Thu, 13 Apr 2023 21:30:32 -0700 Subject: [PATCH 3/3] final code --- lib/__pycache__/company.cpython-38.pyc | Bin 3371 -> 0 bytes lib/__pycache__/dev.cpython-38.pyc | Bin 2661 -> 0 bytes lib/__pycache__/freebie.cpython-38.pyc | Bin 2841 -> 0 bytes lib/alembic.ini | 105 ------------------ lib/company.py | 7 ++ lib/debug.py | 4 +- lib/freebie.py | 3 +- lib/freebies.db | Bin 24576 -> 16384 bytes lib/migrations/README | 1 - lib/migrations/env.py | 79 ------------- lib/migrations/script.py.mako | 24 ---- .../5f72c58bf48c_create_companies_devs.py | 39 ------- .../versions/7a71dbf71c64_create_db.py | 24 ---- 13 files changed, 11 insertions(+), 275 deletions(-) delete mode 100644 lib/__pycache__/company.cpython-38.pyc delete mode 100644 lib/__pycache__/dev.cpython-38.pyc delete mode 100644 lib/__pycache__/freebie.cpython-38.pyc delete mode 100644 lib/alembic.ini delete mode 100644 lib/migrations/README delete mode 100644 lib/migrations/env.py delete mode 100644 lib/migrations/script.py.mako delete mode 100644 lib/migrations/versions/5f72c58bf48c_create_companies_devs.py delete mode 100644 lib/migrations/versions/7a71dbf71c64_create_db.py diff --git a/lib/__pycache__/company.cpython-38.pyc b/lib/__pycache__/company.cpython-38.pyc deleted file mode 100644 index 77b42545fc29d99690843bffe6000c630474c6e6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3371 zcmbtWL37(g6yB9&TX7sWNz>AnQZxetHIu{x<S8 zk~SXqLi#(H_?R0P{s|{tIq?@b@!ra^C8rz!&1ki&w{Lgf_rCphcNZ3l1fJh_?y_I! z3HbvX=|_deH}D2`pKAz>QJy&)`js+a{v z+oY2J6TC^ILZiY1!TG+!2TSW!*V(Tg#LZxx?c3NV{Y>Wm1^#kkQbtULU1thY-%wMH zXE8OFXN5P!%<(+exSW&cna<|kkW*^vTtA#+#YwBk=2=Nv=6R7Vu(GU|*d?|IV=S;G zwhZ4gj8z8jOYAaRf%YP_7ookxuCS}nUIyQH*fr^USw?sxDs1|_e%Cu`*jg$GtYaLT z@CG-a7?LwGq$5hnP#F@74pk@>M%v_@HY#eQ2RyXAZjVO`2mX=A9Pg=h!n-0;99GdH zE#U4!q+thb~Q;`@(kr zVXr40Y>LM5gLQmm_~YD_tWT!gt(@-y%YxT-b;u6VG5 z7#mJtcz$SfjUW_|ib_7x!jnFaRA@)4%RR9GougbItYI`)du8)}==femkugQO$6v}I zXQC51a9e^mz|aZ3Ms-C`g_p44-=JBxGK>+3+w8QMgLmPL@l*y^lJsR<{~6TV;F|*V z<}d~nzDQ$F4^;$o2Fh~Xvwb1B9U6N;<%%X2VVFn-$i!8si4`cuh?x>GWc=Hf{yub! z*{7@kVi|NW@hZGB`O8pY^fe918I8A_Os>)x{8ltl&t|K((Wx1ojr-d*qyE5XG&@G^ zNxj`^8!`JG9vHVXF6iwrqu%J$9@bjM<5qoVqqS#zSKC{?&`)xxQ+v|6XvzRS8O_KH zQJJ*Pl_QbeI(GG3Uj5*c~N2E~NZv zLHC${0sqDdp=rQ)Iw&O=p*e#Nav!rS(YXngEIJ2tq@1ZE@>&@xAak%29%ayI*S2e$ z9pe+@L94lAB7R}^6)#XCok^OS9c%V#~KcZ(!n)4?auoKQ_e#!(R z?p+VWKVxdPwjj&*_p<7c)$EP-rmtLDJHC!S+25E*-*vF@c*$B7j?nOae~#wsl1MmoYwH;~h{cuTD!!5$+m) E1M4=~3;+NC diff --git a/lib/__pycache__/dev.cpython-38.pyc b/lib/__pycache__/dev.cpython-38.pyc deleted file mode 100644 index c538266560e5759290f51b479224ae340155bb9a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2661 zcmbtW&2HO95Z+x<5+zHvoc^FG+G1O@XcWX!(I7w$X;R0gQ>6|SNOGG9CJ&#!33oVDddQ|2A}j2s2THl-aV*||0)9UUZ^o9SmeSxO!d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t_wMVuU#;pb!f;!<^#{2GR)>{t-ZeA1U8caVq=_cZ~>&sCvhlp>Ze1 Qdl#cSV#;6zYOtdAFN8%w8UO$Q diff --git a/lib/__pycache__/freebie.cpython-38.pyc b/lib/__pycache__/freebie.cpython-38.pyc deleted file mode 100644 index 013b68d3a3e28e49db98a85a6d3394fe29e06e82..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2841 zcmbtW&2!sC6yMdCWyem^GzAJ}5X>-Bb&`0PcHjb(G*yd-#O~M*G~%F9v}PYSgDi<4DZ1=YzJn4<6?2h zu=oLQT~b@((uI6UY~?9oGE>e8Q_dt?Wwb?1^(~wr^~sH$s^2}X zb_D0gT^{VPV-FdNW2QvFE+;l+#Fp45lbC!?ZJ8@fVd^=K2=vasN7ciCi=cYk_5EWpd^M;(<|o`A zj{1BMR=pwP)zJ$#;M@1B{h{Z0UUhUDz8DU+NB6f=+_yvFdcSkA=!>1vX{0+&chC(T z2bBhKEKn`|o2;j(>|{b71*vQxGU&M&oeh{U`kICi8dG{nDFh1yfmEOTQ!*CDUenrb zTSj~L(Y|HWo*4B;+pwP3TJ4sRMTl`XcLEQ0nNh2^t*2Jgc-E{P>^6^#pRA*;t8y9S zM%#MczRHjNkp0wEcIuyZ3nYHa_&N3FZ1qNpw7tV-tI>=K{15JpLk?TA=LeA-y!2z2 zCS(Obi^`thZpfX`J@)xJ>;&j~A}OWGW`_Tw$ou%<)5p!mvpMKXfQt&~6R3LGwh|M= zQDWI=z|JhAla0)>xxf{>FqY%kcmR`mY)nZHGN%W5(}S$(sU77~q9oKV3YP@lq^CnJ z6|?9W%wWYhsMT9mvyJ#S7Q$tgXj?|?;4LF{z8i#)kX;t%t0{>h9>io=hEfUK6aFPI0?af*73B`q z>7<+iiEIxwW&`K=#1MWAlPMM+EI7)U@*0xtf*@d1sI>yq(4^u+e5v9Fysk5T*(5Cq zs~r#z;<6{LqDiZnNeMgWS5$=I85?y6CV9Vh=w;jxUQ1Ic$^OF}^et=O+G`v4j3>>; z0Xiqhv48o|YFe1=-x&{cjQ!d{t!;cY+cT8~HzA&p-r=DKC6Al(+y{2CGaL_?1B#aK zUigiNxc@aAPA*Rsb(2okvdgFZ|95+PW-&%@{`MH7?f^PsPUig}h}*_(xJvH!mE`t` zqSIvrR`7-?@6i$pHy2SzWjw6+*}yoM1t)e!hyy9E{StP^p45@ztd=hn4ALx{k8*cC zeD)Z7K_~q($njcMJ5Amz!Eh}^5cc!L#RwI*kbD3n#l$WR_$zcdblz(-v@#wsEX0XO z15VBYV}nj^Wsu~ga^Zo^aaQ#1!6Jm5?NLT=lRpW=q9?%{3!j|q{iUAK6da}g7LQMK z1KN9FxW+^SV1|9eR3ddGx`Qy1F^m!PR!<2@QuO5O;o;# z1j~>32nm*}NFPD-;37N~*gM20NDL%cH^k>i=7z^Jv|ga8h+=?ZIxXoEEv>?%>P4Uh zy{OCZD0(@TG&Q>bO%+w=R{a GF8>GiOH0K7 diff --git a/lib/alembic.ini b/lib/alembic.ini deleted file mode 100644 index 953863ddd..000000000 --- a/lib/alembic.ini +++ /dev/null @@ -1,105 +0,0 @@ -# A generic, single database configuration. - -[alembic] -# path to migration scripts -script_location = migrations - -# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s -# Uncomment the line below if you want the files to be prepended with date and time -# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file -# for all available tokens -# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s - -# sys.path path, will be prepended to sys.path if present. -# defaults to the current working directory. -prepend_sys_path = . - -# timezone to use when rendering the date within the migration file -# as well as the filename. -# If specified, requires the python-dateutil library that can be -# installed by adding `alembic[tz]` to the pip requirements -# string value is passed to dateutil.tz.gettz() -# leave blank for localtime -# timezone = - -# max length of characters to apply to the -# "slug" field -# truncate_slug_length = 40 - -# set to 'true' to run the environment during -# the 'revision' command, regardless of autogenerate -# revision_environment = false - -# set to 'true' to allow .pyc and .pyo files without -# a source .py file to be detected as revisions in the -# versions/ directory -# sourceless = false - -# version location specification; This defaults -# to migrations/versions. When using multiple version -# directories, initial revisions must be specified with --version-path. -# The path separator used here should be the separator specified by "version_path_separator" below. -# version_locations = %(here)s/bar:%(here)s/bat:migrations/versions - -# version path separator; As mentioned above, this is the character used to split -# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep. -# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas. -# Valid values for version_path_separator are: -# -# version_path_separator = : -# version_path_separator = ; -# version_path_separator = space -version_path_separator = os # Use os.pathsep. Default configuration used for new projects. - -# the output encoding used when revision files -# are written from script.py.mako -# output_encoding = utf-8 - -sqlalchemy.url = sqlite:///freebies.db - - -[post_write_hooks] -# post_write_hooks defines scripts or Python functions that are run -# on newly generated revision scripts. See the documentation for further -# detail and examples - -# format using "black" - use the console_scripts runner, against the "black" entrypoint -# hooks = black -# black.type = console_scripts -# black.entrypoint = black -# black.options = -l 79 REVISION_SCRIPT_FILENAME - -# Logging configuration -[loggers] -keys = root,sqlalchemy,alembic - -[handlers] -keys = console - -[formatters] -keys = generic - -[logger_root] -level = WARN -handlers = console -qualname = - -[logger_sqlalchemy] -level = WARN -handlers = -qualname = sqlalchemy.engine - -[logger_alembic] -level = INFO -handlers = -qualname = alembic - -[handler_console] -class = StreamHandler -args = (sys.stderr,) -level = NOTSET -formatter = generic - -[formatter_generic] -format = %(levelname)-5.5s [%(name)s] %(message)s -datefmt = %H:%M:%S diff --git a/lib/company.py b/lib/company.py index 76b6e5668..566341799 100644 --- a/lib/company.py +++ b/lib/company.py @@ -34,6 +34,13 @@ def founding_year(self, new_year): self._founding_year = new_year else: raise Exception("Incorrect Year") + + #!new from db + @classmethod + def new_from_db(cls, row): + company_row = cls(name=row[1], founding_year=row[2], id = row[0]) + return company_row + # Create a table that companies get saved into: diff --git a/lib/debug.py b/lib/debug.py index 38ccbaead..83b6b77f8 100644 --- a/lib/debug.py +++ b/lib/debug.py @@ -13,12 +13,13 @@ Dev.drop_table() Freebie.drop_table() -#! Create all (3) tables to exist in the database: +# #! Create all (3) tables to exist in the database: Company.create_table() Dev.create_table() Freebie.create_table() + #! Create some dev instances ollie = Dev("ollie") ollie.save() @@ -50,5 +51,4 @@ if __name__ == '__main__': - # engine = create_engine('sqlite:///freebies.db') import ipdb; ipdb.set_trace() diff --git a/lib/freebie.py b/lib/freebie.py index 351ebd198..38632e9fa 100644 --- a/lib/freebie.py +++ b/lib/freebie.py @@ -93,7 +93,8 @@ def get_company(self): LIMIT 1 """ found_company = CURSOR.execute(sql, (self.comp_id,)).fetchone() - return found_company + from company import Company + return Company.new_from_db(found_company) company = property(get_company) diff --git a/lib/freebies.db b/lib/freebies.db index f85ad1c9097ff5ac369f2cb74bf80b0ca736a036..5c61b51f18c963236ba5987d030b0555b7a52b0e 100644 GIT binary patch delta 120 zcmZoTz}V2hI6+E?p@M;dfdzx0h0~-)S0ppB`Iz|#eQN4IsUj82ptUR9>_<8wv@Tfn|Po$BSU6hN@_)MVNPaAYJ6g8Nj{h#kLI3uLo@+q zU{G>^f|D1B8Tmgj0MTY4gNytVCx}S#F*2!|rkNWhn_48LnOG!q!PRUQG`Iv(BEZkA L%89IGk$?dJez|{( diff --git a/lib/migrations/README b/lib/migrations/README deleted file mode 100644 index 98e4f9c44..000000000 --- a/lib/migrations/README +++ /dev/null @@ -1 +0,0 @@ -Generic single-database configuration. \ No newline at end of file diff --git a/lib/migrations/env.py b/lib/migrations/env.py deleted file mode 100644 index c7aab9656..000000000 --- a/lib/migrations/env.py +++ /dev/null @@ -1,79 +0,0 @@ -from logging.config import fileConfig - -from sqlalchemy import engine_from_config -from sqlalchemy import pool - -from alembic import context - -# this is the Alembic Config object, which provides -# access to the values within the .ini file in use. -config = context.config - -# Interpret the config file for Python logging. -# This line sets up loggers basically. -if config.config_file_name is not None: - fileConfig(config.config_file_name) - -# add your model's MetaData object here -# for 'autogenerate' support -# from myapp import mymodel -# target_metadata = mymodel.Base.metadata -from models import Base -target_metadata = Base.metadata - -# other values from the config, defined by the needs of env.py, -# can be acquired: -# my_important_option = config.get_main_option("my_important_option") -# ... etc. - - -def run_migrations_offline() -> None: - """Run migrations in 'offline' mode. - - This configures the context with just a URL - and not an Engine, though an Engine is acceptable - here as well. By skipping the Engine creation - we don't even need a DBAPI to be available. - - Calls to context.execute() here emit the given string to the - script output. - - """ - url = config.get_main_option("sqlalchemy.url") - context.configure( - url=url, - target_metadata=target_metadata, - literal_binds=True, - dialect_opts={"paramstyle": "named"}, - ) - - with context.begin_transaction(): - context.run_migrations() - - -def run_migrations_online() -> None: - """Run migrations in 'online' mode. - - In this scenario we need to create an Engine - and associate a connection with the context. - - """ - connectable = engine_from_config( - config.get_section(config.config_ini_section), - prefix="sqlalchemy.", - poolclass=pool.NullPool, - ) - - with connectable.connect() as connection: - context.configure( - connection=connection, target_metadata=target_metadata, render_as_batch=True, - ) - - with context.begin_transaction(): - context.run_migrations() - - -if context.is_offline_mode(): - run_migrations_offline() -else: - run_migrations_online() diff --git a/lib/migrations/script.py.mako b/lib/migrations/script.py.mako deleted file mode 100644 index 55df2863d..000000000 --- a/lib/migrations/script.py.mako +++ /dev/null @@ -1,24 +0,0 @@ -"""${message} - -Revision ID: ${up_revision} -Revises: ${down_revision | comma,n} -Create Date: ${create_date} - -""" -from alembic import op -import sqlalchemy as sa -${imports if imports else ""} - -# revision identifiers, used by Alembic. -revision = ${repr(up_revision)} -down_revision = ${repr(down_revision)} -branch_labels = ${repr(branch_labels)} -depends_on = ${repr(depends_on)} - - -def upgrade() -> None: - ${upgrades if upgrades else "pass"} - - -def downgrade() -> None: - ${downgrades if downgrades else "pass"} diff --git a/lib/migrations/versions/5f72c58bf48c_create_companies_devs.py b/lib/migrations/versions/5f72c58bf48c_create_companies_devs.py deleted file mode 100644 index c191bb2f9..000000000 --- a/lib/migrations/versions/5f72c58bf48c_create_companies_devs.py +++ /dev/null @@ -1,39 +0,0 @@ -"""create companies, devs - -Revision ID: 5f72c58bf48c -Revises: 7a71dbf71c64 -Create Date: 2023-03-15 15:06:20.944586 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = '5f72c58bf48c' -down_revision = '7a71dbf71c64' -branch_labels = None -depends_on = None - - -def upgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - op.create_table('companies', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('name', sa.String(), nullable=True), - sa.Column('founding_year', sa.Integer(), nullable=True), - sa.PrimaryKeyConstraint('id') - ) - op.create_table('devs', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('name', sa.String(), nullable=True), - sa.PrimaryKeyConstraint('id') - ) - # ### end Alembic commands ### - - -def downgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - op.drop_table('devs') - op.drop_table('companies') - # ### end Alembic commands ### diff --git a/lib/migrations/versions/7a71dbf71c64_create_db.py b/lib/migrations/versions/7a71dbf71c64_create_db.py deleted file mode 100644 index 23e0a655b..000000000 --- a/lib/migrations/versions/7a71dbf71c64_create_db.py +++ /dev/null @@ -1,24 +0,0 @@ -"""create db - -Revision ID: 7a71dbf71c64 -Revises: -Create Date: 2023-03-15 15:05:55.516631 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = '7a71dbf71c64' -down_revision = None -branch_labels = None -depends_on = None - - -def upgrade() -> None: - pass - - -def downgrade() -> None: - pass