from collections import namedtuple
Shape = namedtuple("Poly", "Faces Vertexes Edges")
The name Shape
now points to an object in memory that serves as a callable, a kind of factory, for making any number of Shape things (a subclass of tuple, one may think of it).
tetra = Shape(4, 4, 6)
tetra
Poly(Faces=4, Vertexes=4, Edges=6)
cube = Shape(Vertexes=8, Edges=12, Faces=6) # named args
cube
Poly(Faces=6, Vertexes=8, Edges=12)
"Shape things" represent themselves with whatever string is provided, as the leftmost argument.
octa = Shape(Faces=8, Vertexes=6, Edges=12)
icosa = Shape(Faces=20, Vertexes=12, Edges=30)
dodeca = Shape(12, 12, 24)
icosa
Poly(Faces=20, Vertexes=12, Edges=30)
If you're following the action, you'll see we have created a few Shape type namedtuples. Now we can get the data in namedtuple s by saying s.Edges
instead of s[2]
.
all_shapes = icosa, cube, tetra, octa, dodeca
for s in all_shapes:
# V + F == E + 2 (Euler's Law for Polyhedra with No Holes)
try:
assert s.Edges + 2 == (s.Vertexes + s.Faces)
except AssertionError:
print("Tsk Tsk, you broke Euler's rule")
Tsk Tsk, you broke Euler's rule
getattr(tetra, 'Faces')
4
hasattr(icosa, 'Edges')
True
tetra[0], tetra[1], tetra[2]
(4, 4, 6)
*icosa, *tetra
(20, 12, 30, 4, 4, 6)
icosa, tetra
(Poly(Faces=20, Vertexes=12, Edges=30), Poly(Faces=4, Vertexes=4, Edges=6))
You realize future designs to add a database on the back end necessitate each Shape having an additional attribute, namely a Name. The Name would be the exact type of Polyhedron being defined, such as "Octahedron" or "Tetrahedron".
Make a new Shape below, that has this additional attribute and make it do tricks.
Note on style: blank lines cost only a single byte apiece, yet add a lot to readability. Separate methods with a space.
class Dog:
def __init__(me, name):
me.name = name # use of 'me' versus 'self' nonstandard!
def bark(me, n=1):
"bark n times"
print(me.name, "says", "Bark! " * n)
the_dog = Dog("Rover")
the_dog.bark(5)
Rover says Bark! Bark! Bark! Bark! Bark!
Dog.bark(the_dog, 5)
Rover says Bark! Bark! Bark! Bark! Bark!
Inheritance is a standard part of the Object Model, in any object oriented language. An Animal class, more general than its more specialized child classes, such as Dog and Cat, provides a common core or ancestor.
The builtin super() figures out who the ancestor will be, according to a kind of search algorithm which traces a "method resolution order" (path through ancestor classes).
When initializing a new Dog, invoke the ancestral constructor as well, as that's where an empty stomach gets added, for use with an eat() method.
class Animal:
def __init__(self):
self.stomach = [ ] # self is standard
def eat(self, food):
self.stomach.append(food)
class Dog(Animal): # does everything an Animal might do, and more
def __init__(me, name):
me.name = name # use of 'me' versus 'self' nonstandard!
super().__init__() # lets get us a stomach
def bark(me, n=1):
"bark n times"
print(me.name, "says", "Bark! " * n)
der_dog = Dog("Rover")
der_dog.eat("marshmellow")
der_dog.stomach
['marshmellow']
Dog.__mro__ # the order in which to search for a method name
(__main__.Dog, __main__.Animal, object)
Here's another example of named tuples in action.
# %%writefile make_links_v4.py
"""
Created on Thu Jan 28 21:27:07 2016
@author: kurner
A namedtuple is a subclass of tuple allowing for named
columns and therefore dot notation access, treating
elements as named attributes.
Bookmark(place='Anaconda.org', url='http://anaconda.org', tags='tool')
"""
from collections import namedtuple
from context1 import DB
PRINT = True # make true if you wish screen noise (echo of bookmarks)
Bmk = namedtuple('Bookmark', 'place url tags')
# tuple of tuples
tuples = (
("Anaconda.org",
"http://anaconda.org",
"tool"),
("Python.org",
"http://python.org",
"source"),
("Python Docs",
"https://docs.python.org/3/",
"docs"),
("''New Math'' by Tom Lehrer (animated)",
"https://youtu.be/UIKGV2cTgqA",
"comedy"),
("Spaghetti Code",
"http://c2.com/cgi/wiki?SpaghettiCode",
"glossary"),
("Structured Programming",
"http://c2.com/cgi/wiki?StructuredProgramming",
"glossary, history"),
("Map of Languages",
"http://archive.oreilly.com/pub/a/oreilly//news/languageposter_0504.html",
"docs"),
("XKCD", "http://xkcd.com", "comedy"),
("Will Geeks Rule? CBS News (world domination meme)",
"http://www.cbsnews.com/news/will-geeks-rule-the-world/",
"glossary, comedy"),
("Django","https://docs.djangoproject.com/", "source"),
("PythonAnywhere","https://www.pythonanywhere.com/", "tool"),
("CodeAcademy: Python",
"https://www.codecademy.com/learn/python",
"docs"),
("Unicode on Youtube",
"https://www.youtube.com/watch?v=Z_sl99D2a18",
"docs, glossary"),
("In Defense of Ada",
"http://www.grunch.net/synergetics/adaessay.html", "comedy, history"),
("Grace Hopper on Letterman",
"https://www.youtube.com/watch?v=1-vcErOPofQ", "source"),
("The Mind of a Genius: John von Neumann",
"https://www.youtube.com/watch?v=XZ9tt72feL8", "docs"),
("World Domination meme",
"https://www.google.com/search?q=linux+world+domination&safe=off&source=lnms&tbm=isch",
"glossary, comedy, culture"),
("Warriors of the Net",
"https://www.youtube.com/watch?v=PBWhzz_Gn10",
"docs, glossary, comedy"),
("LAMP stack",
"https://www.google.com/search?q=lamp+stack&safe=off&biw=787&bih=535&source=lnms&tbm=isch",
"glossary"),
("LAMP stack (Wikipedia)",
"https://en.wikipedia.org/wiki/LAMP_(software_bundle)",
"glossary"),
("Find string in string in SQLite",
"https://stackoverflow.com/questions/3498844/sqlite-string-contains-other-string-query",
"docs"),
("What Python IDE is best?", "https://www.quora.com/What-is-your-favorite-IDE-for-Python-programming-and-why",
"tool"),
("More Help with Python", "https://medium.com/@kirbyurner/more-help-with-python-9afe2d0affe8",
"docs"),
("Microsoft Azure", "https://azure.microsoft.com/en-us/", "cloud"),
("AWS", "https://aws.amazon.com/", "cloud")
)
# lets make these namedtuples instead OK?
# *tup explodes each tuple into two positionals, what Bmk expects
def bumpy():
"""
if you expect problems in the input
"""
the_list = []
for tup in tuples:
try:
the_list.append(Bmk(*tup))
except TypeError:
print("Ill-formed tuple:", tup)
return the_list
def smooth():
"""
if you expect smooth sailing i.e. no problems with tuples
"""
the_list = [Bmk(*tup) for tup in tuples] # list comprehension!
return the_list
def printall():
for bmk in bookmarks:
# Bookmark(place='Anaconda.org', url='http://anaconda.org')
print(bmk) # notice format of output: __repr__
print("-")
if __name__ == "__main__":
bookmarks = bumpy()
# bookmarks = smooth()
if PRINT:
printall()
# login
with DB("bookmarks.db") as db:
# https://www.sqlite.org/lang_droptable.html
# DB API ???
db.curs.execute("""DROP TABLE IF EXISTS Bookmarks""")
db.curs.execute("""CREATE TABLE Bookmarks
(bk_place text PRIMARY KEY,
bk_url text,
bk_tags text)""")
for bmk in sorted(bookmarks):
query = ("INSERT INTO Bookmarks "
"(bk_place, bk_url, bk_tags) "
"VALUES ('{}', '{}', '{}')".format(bmk.place, bmk.url, bmk.tags))
# print(query)
db.curs.execute(query)
db.conn.commit()
Bookmark(place='Anaconda.org', url='http://anaconda.org', tags='tool') - Bookmark(place='Python.org', url='http://python.org', tags='source') - Bookmark(place='Python Docs', url='https://docs.python.org/3/', tags='docs') - Bookmark(place="''New Math'' by Tom Lehrer (animated)", url='https://youtu.be/UIKGV2cTgqA', tags='comedy') - Bookmark(place='Spaghetti Code', url='http://c2.com/cgi/wiki?SpaghettiCode', tags='glossary') - Bookmark(place='Structured Programming', url='http://c2.com/cgi/wiki?StructuredProgramming', tags='glossary, history') - Bookmark(place='Map of Languages', url='http://archive.oreilly.com/pub/a/oreilly//news/languageposter_0504.html', tags='docs') - Bookmark(place='XKCD', url='http://xkcd.com', tags='comedy') - Bookmark(place='Will Geeks Rule? CBS News (world domination meme)', url='http://www.cbsnews.com/news/will-geeks-rule-the-world/', tags='glossary, comedy') - Bookmark(place='Django', url='https://docs.djangoproject.com/', tags='source') - Bookmark(place='PythonAnywhere', url='https://www.pythonanywhere.com/', tags='tool') - Bookmark(place='CodeAcademy: Python', url='https://www.codecademy.com/learn/python', tags='docs') - Bookmark(place='Unicode on Youtube', url='https://www.youtube.com/watch?v=Z_sl99D2a18', tags='docs, glossary') - Bookmark(place='In Defense of Ada', url='http://www.grunch.net/synergetics/adaessay.html', tags='comedy, history') - Bookmark(place='Grace Hopper on Letterman', url='https://www.youtube.com/watch?v=1-vcErOPofQ', tags='source') - Bookmark(place='The Mind of a Genius: John von Neumann', url='https://www.youtube.com/watch?v=XZ9tt72feL8', tags='docs') - Bookmark(place='World Domination meme', url='https://www.google.com/search?q=linux+world+domination&safe=off&source=lnms&tbm=isch', tags='glossary, comedy, culture') - Bookmark(place='Warriors of the Net', url='https://www.youtube.com/watch?v=PBWhzz_Gn10', tags='docs, glossary, comedy') - Bookmark(place='LAMP stack', url='https://www.google.com/search?q=lamp+stack&safe=off&biw=787&bih=535&source=lnms&tbm=isch', tags='glossary') - Bookmark(place='LAMP stack (Wikipedia)', url='https://en.wikipedia.org/wiki/LAMP_(software_bundle)', tags='glossary') - Bookmark(place='Find string in string in SQLite', url='https://stackoverflow.com/questions/3498844/sqlite-string-contains-other-string-query', tags='docs') - Bookmark(place='What Python IDE is best?', url='https://www.quora.com/What-is-your-favorite-IDE-for-Python-programming-and-why', tags='tool') - Bookmark(place='More Help with Python', url='https://medium.com/@kirbyurner/more-help-with-python-9afe2d0affe8', tags='docs') - Bookmark(place='Microsoft Azure', url='https://azure.microsoft.com/en-us/', tags='cloud') - Bookmark(place='AWS', url='https://aws.amazon.com/', tags='cloud') -
# %load bookmarks.py
#!/usr/bin/env python3
"""
Created on Thu Oct 11 11:38:26 2018
@author: Kirby Urner
How to use LIKE keyword in SQLite:
https://stackoverflow.com/questions/3498844/sqlite-string-contains-other-string-query
"""
import sqlite3 as sql
class DB:
"""
I connect you to a database, use me as a
context manager
"""
def __init__(self, target):
# target to dial
self.source = target
def connect(self):
"""
dial the target and connect
"""
# we'll be needing these
self.conn = sql.connect(self.source)
self.curs = self.conn.cursor()
def __enter__(self):
"""
upon entering the context, I do my job
"""
self.connect() # delegating...
return self # I am valuable
def __exit__(self, *oops):
if oops:
return False
return True
def tag_filter(the_tag):
with DB("bookmarks.db") as db:
# see Stackoverflow link in module's __doc__
db.curs.execute("SELECT * FROM "
"Bookmarks WHERE " # whoah, this OK?
"bk_tags LIKE ? ", (the_tag,))
results = list(db.curs.fetchall())
# database closes through __exit__, yahoo!
return results
if __name__ == "__main__":
# try %comedy% %docs% %tool% and %glossary% too
for record in tag_filter("%cloud%"):
print( record )
('AWS', 'https://aws.amazon.com/', 'cloud') ('Microsoft Azure', 'https://azure.microsoft.com/en-us/', 'cloud')