Skip to content
Open

Dane #24

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Day2/.DS_Store
Binary file not shown.
Binary file added Day2/intro-flask/.DS_Store
Binary file not shown.
Binary file added Day2/intro-flask/data/listings.db
Binary file not shown.
56 changes: 56 additions & 0 deletions Day2/intro-flask/models/listing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import sqlite3

class Listing:

tablename = "listings"
dbpath = "..data/listings.db"

def __init__(self, name, quantity, price, location, pk=None):
self.pk = pk
self.name = name
self.quantity = quantity
self.price = price
self.location = location


def insert(self):
with sqlite3.connect(self.dbpath) as conn:
cursor = conn.cursor()
cursor.execute("INSERT INTO listing VALUES(name, quantity, price, location)")


def update(self):
with sqlite3.connect(self.dbpath) as conn:
cursor = conn.cursor()
cursor.execute("""UPDATE listing SET name = self.name WHERE pk = self.pk""")


def delete(self):
pass


@classmethod
def select_all(cls):
with sqlite3.connect(cls.dbpath) as conn:
cursor = conn.cursor()
# TODO: prevent sql injection
sql = f"""SELECT * from {cls.tablename}"""
cursor.execute(sql)
return cursor.fetchall()


@classmethod
def select_one(cls):
with sqlite3.connect(cls.dbpath) as conn:
cursor = conn.cursor()
# TODO: prevent sql injection
sql = f"""SELECT * from {cls.tablename}"""
cursor.execute(sql)
return cursor.fetchone()


if __name__ == "__main__":
listings = Listing.select_all()
print(listings)
new_item = Listing("name", 5, 12.99, "test location")
new_item.insert()
7 changes: 7 additions & 0 deletions Day2/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os
from models.listing import Listing

PATH = os.path.dirname(__file__)
Listing.dbpath = os.path.join(PATH, "data", "listings.db")

print(Listing.select_all())

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Code your solution here

answer = 'a'
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Code your solution here

answer = 'd'
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Code your solution here

answer = 'c'
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# Code your solution here
# Code your solution here
answer = 'b'
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
ONE = 'man'

# Assign the correct strings below
TWO =
THREE =
FOUR =
FIVE =
SIX =
SEVEN =
TWO = 'ls'
THREE = 'mkdir'
FOUR = 'touch'
FIVE = 'cp'
SIX = 'mv'
SEVEN = 'rm'

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Assign the correct strings below

ONE =
TWO =
THREE =
FOUR =
FIVE =
SIX =
SEVEN =
ONE = 'cd exercises'
TWO = 'pwd'
THREE = 'cat textfile.txt'
FOUR = 'tail textfile.txt'
FIVE = 'ls -a'
SIX = 'head textfile.txt'
SEVEN = 'cp stackdata.json stockdata.json'
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Assign the correct strings below

ONE =
TWO =
THREE =
FOUR =
FIVE =
SIX =
ONE = 'grep elephant book.txt'
TWO = 'mv notes.txt newdir'
THREE = 'grep -r string testdir'
FOUR = 'rm -d empty'
FIVE = 'cd'
SIX = 'rm -r Downloads'
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

# Assign the correct strings below
ONE =
TWO =
THREE =
FOUR =
FIVE =
ONE = 'mkdir testdir && cd testdir'
TWO = 'cat textfile.txt | grep and'
THREE = 'ls >> contents.txt'
FOUR = 'cd current || mkdir current'
FIVE = 'pwd > current_path.txt'
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Vector3D:

def __init__(self, x=0, y=0, z=0):
self.x = x
self.y = y
self.z = z


vector1 = Vector3D(1, 2, 3)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Vector3D:

def __init__(self, x=0, y=0, z=0):
self.x = x
self.y = y
self.z = z

def __str__(self):
return f'x = {self.x}, y = {self.y}, z = {self.z}'


vector1 = Vector3D(3, 4, 2)
print(vector1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Vector3D:

def __init__(self, x=0, y=0, z=0):
self.x = x
self.y = y
self.z = z

def __add__(self, other):
x = self.x + other.x
y = self.y + other.y
z = self.z + other.z
return Vector3D(x, y, z)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import math


class Vector3D:
def __init__(self, x=0, y=0, z=0):
self.x = x
self.y = y
self.z = z

def __it__(self, other):
if self.x < other.x:
return math.sqrt(x)
elif self.y < other.y:
return True
else:
self.z < other.z
return True

def __gt__(self, other):
if self.x > other.x:
return True
elif self.y > other.y:
return True
else:
self.z > other.z
return True


print(vec1=math.sqrt((1, 5, 3)))
print(vec2=math.sqrt((4, 4, 1)))
print(vec3=math.sqrt((1, 5, 2)))
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Employee:

def __init__(self, name, employee_id, salary, years_at_company):
self.name = name
self.employee_id = employee_id
self.salary = salary
self.years_at_company = years_at_company


cosette = Employee("Cosette Rodger", 1, 100000, 4)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Employee:

def __init__(self, name, employee_id, salary, years_at_company):
self.name = name
self.employee_id = employee_id
self.salary = salary
self.years_at_company = years_at_company


def give_raises(employee_lst):
for emp in employee_lst:
if emp.years_at_company <= 5:
emp.salary = emp.salary + 5000
elif emp.years_at_company > 5 and emp.years_at_company < 10:
emp.salary = emp.salary + 8000
else:
emp.years_at_company >= 10
emp.salary = emp.salary + 10000
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Employee:

def __init__(self, name, employee_id, salary, years_at_company):
self.name = name
self.employee_id = employee_id
self.salary = salary
self.years_at_company = years_at_company


def sort_employees(employee_list):
names = []
for employee in employee_list:
names.append(employee.name)
return sorted(names)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Person:

def __init__(self, name, age, spouse, children):
self.name = name
self.age = age
self.spouse = spouse
self.children = children


class Child(Person):
def __init__(self, name, age, spouse, children, parents):
Person.__init__(self, name, age, spouse, children)
self.parents = parents


jim = Person('Jim Brown', 45, None, [])
suzy = Person('Suzy Brown', 42, None, [])
martha = Child('Martha Brown', 18, None, [], [])

jim.spouse = suzy
suzy.spouse = jim
jim.children.append(martha)
suzy.children.append(martha)
martha.parents.append(jim)
martha.parents.append(suzy)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Person:

def __init__(self, name, age, spouse, children):
self.name = name
self.age = age
self.spouse = spouse
self.children = children


class Child(Person):

def __init__(self, name, age, spouse, children, parents):
Person.__init__(self, name, age, spouse, children)
self.parents = parents

def get_siblings(self, children):
self.child_by_age = []
children.sort(key=self.child_by_age)
for self.child in children:
return self.children.age
10 changes: 10 additions & 0 deletions introduction_to_python/functions/1_below_100/solution/solution.py
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
# Write your solution here

num = int()
def below_100(num):
if num < 100:
return("less than 100")
else:
return("greater than 100")

below_100(50)
below_100(200)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def concat_input(val_1, val_2):
return val_1 + val_2

concat_input(val_1 = "hello", val_2 = "world")
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def hello(name):
return("Hello " + name)

hello("Dorothy")
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# Write your solution here

def max_val(a, b, c):
return max(a, b, c)

print(max_val(1, 4, 2))
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def shut_down(x):
if x == True:
return "SHUTDOWN"
else:
return "SHUTDOWN ABORTED"

#print(shut_down(x=0))
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def concat_args(*args):
return "" + ""

print(concat_args("Hello ", "There"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def count_even(*args):
for num in args:
if num % 2 == 0:
return num

count_even(1, 2, 3, 4) == 2
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
# Write your code here

def divisible_by_3(*args):
div_3 = []
for num in args:
if num % 3 == 0:
return div_3.append(num)

print(divisible_by_3(2,3,5,6,9))