Skip to content
4 changes: 4 additions & 0 deletions add.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def add(a, b):
print a + b

add(1, -2)
20 changes: 20 additions & 0 deletions dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
D = {}

def makeD(s):
a = s.split(";")
for i in a:
key, val = i.split("=")
D[key] = val

def makeDshort(s):
return dict([i.split("=") for i in s.split(";")])

def dictocs(d):
s = ''
for i in d:
s += str(i) + '=' + str(d[i])
return s

makeD("a=bc;d=efg;h=ijkl;m=n")
print D
print dictocs({'a': 'bc', 'h': 'ijkl', 'm': 'n', 'd': 'efg'})
7 changes: 7 additions & 0 deletions files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
try:
with open('first.txt', 'r') as inp:
with open('second.txt', 'w') as out:
for line in inp:
out.write(line)
except IOError:
print "Wrong file or file path"
60 changes: 60 additions & 0 deletions inherit dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

class ITEMNOTINMENU(Exception):
pass
class WrongPrice(Exception):
pass

class Menu(dict):
def __getitem__(self, i):
if self.get(i) is None:
raise ITEMNOTINMENU
else:
return self.get(i)

class Order(dict):
def __init__(self, m):
self.d = m
dict.__init__(self)

def __setitem__(self, key, item):
if key in self.d:
if self.d[key] == item:
self.update({key : item})
else:
raise WrongPrice
else:
raise ITEMNOTINMENU

def show(self):
print 'order :'
for i in self.items():
print '\t' + str(i[0]) + ' costs ' + str(i[1])


c = Menu()
c['vada'] = 10
c['idly'] = 30
print c['vada']
try:
print c['unknown']
except ITEMNOTINMENU:
print 'not in menu'

o1 = Order(c)
o2 = Order(c)
try:
o1['vada'] = 10
except ITEMNOTINMENU:
print 'not in menu'
except WrongPrice:
print 'wrong price'

try:
o2['idly'] = 30
except ITEMNOTINMENU:
print 'not in menu'
except WrongPrice:
print 'wrong price'

o1.show()
o2.show()
12 changes: 12 additions & 0 deletions menu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Menu:
def __init__(self):
self.d = {'vada' : 10}
def show(self):
for i in self.d.items():
print str(i[0]) + ' costs ' + str(i[1])
def add(self, a, b):
self.d[a] = b

c = Menu()
c.add('idly', 20)
c.show()
76 changes: 76 additions & 0 deletions order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
class ITEMNOTINMENU(Exception):
pass
class WrongPrice(Exception):
pass

class Menu:
def __init__(self):
self.d = {'vada' : 10, 'idly' : 20}

def __contains__(self, key):
return key in self.d

def __getitem__(self, i):
if i in self.d:
return self.d[i]
else:
raise ITEMNOTINMENU

def __setitem__(self, key, item):
self.d[key] = item

def show(self):
print 'menu :'
for i in self.d.items():
print '\t' + str(i[0]) + ' costs ' + str(i[1])

def add(self, a, b):
self.d[a] = b

class Order():
d = Menu()
def __init__(self, m):
self.d = m
self.ord = {}

def __getitem__(self, i):
if i in self.ord:
return self.ord[i]
else:
raise ITEMNOTINMENU

def __setitem__(self, key, item):
if key in self.d:
if self.d[key] == item:
self.ord[key] = item
else:
raise WrongPrice
else:
raise ITEMNOTINMENU

def show(self):
print 'order :'
for i in self.ord.items():
print '\t' + str(i[0]) + ' costs ' + str(i[1])


c = Menu()
c['idly'] = 20
try:
print c['unknown']
except ITEMNOTINMENU:
print 'not in menu'

c.show()
o1 = Order(c)
o2 = Order(c)
try:
o1['vada'] = 10
o2['idly'] = 20
except ITEMNOTINMENU:
print 'not in menu'
except WrongPrice:
print 'wrong price'

o1.show()
o2.show()
78 changes: 78 additions & 0 deletions orderInheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
class ITEMNOTINMENU(Exception):
pass
class WrongPrice(Exception):
pass

class Help(object):
def getitem(self, i, a):
if i in a:
return a[i]
else:
raise ITEMNOTINMENU

def show(self, a):
for i in a.items():
print '\t' + str(i[0]) + ' costs ' + str(i[1])

class Menu(Help):
def __init__(self):
self.d = {'vada' : 10, 'idly' : 20}

def __getitem__(self, i):
return super(Menu, self).getitem(i, self.d)

def __contains__(self, key):
return key in self.d

def __setitem__(self, key, item):
self.d[key] = item

def show(self):
print 'menu :'
super(Menu, self).show(self.d)

def add(self, a, b):
self.d[a] = b

class Order(Help):
def __init__(self, m):
self.d = m
self.ordL = {}

def __getitem__(self, i):
return super(Order, self).getitem(i, self.ordL)

def __setitem__(self, key, item):
if key in self.d:
if self.d[key] == item:
self.ordL[key] = item
else:
raise WrongPrice
else:
raise ITEMNOTINMENU

def show(self):
print 'order :'
super(Order, self).show(self.ordL)


c = Menu()
c['idly'] = 20
try:
print c['unknown']
except ITEMNOTINMENU:
print 'not in menu'

c.show()
o1 = Order(c)
o2 = Order(c)
try:
o1['vada'] = 10
o2['idly'] = 20
except ITEMNOTINMENU:
print 'not in menu'
except WrongPrice:
print 'wrong price'

o1.show()
o2.show()
10 changes: 10 additions & 0 deletions root.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def root(a, b, c):
sq = b * b - 4 * a * c
if sq < 0:
r1 = str(pow(abs(b * b - 4 * a * c), 0.5)) + 'i'
r2 = -b / 2 * a
return str(r2) + ' + ' + r1, str(r2) + ' - ' + r1
else:
return (-b + pow(b * b - 4 * a * c, 0.5)) / (2 * a), (-b - pow(b * b - 4 * a * c, 0.5)) / (2 * a)

print root(2, 3, 5)
6 changes: 6 additions & 0 deletions rootV2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import cmath

def root(a, b, c):
return (-b + cmath.sqrt(b * b - 4 * a * c)) / (2 * a), (-b - cmath.sqrt(b * b - 4 * a * c)) / (2 * a)

print root(2, 3, 5)