Archive - Originally posted on "The Horse's Mouth" - 2016-10-30 07:59:25 - Graham Ellis
Last week, I ran a private Python Intermediate course - using Python 2.7 at customer request - and here are some of the code snippets I wrote in front of the class - little things I may not have blogged about in the past. Complete examples being emailed to the delegates!
# with keyword to perform open and closure on object
# -------------------------------------------------
with table.rectangularTable("Bob","3",2000,100) as ourparty:
print ourparty
print ourparty.getSeats()
print ourparty
# defining your own "with"
# -----------------------
def __enter__(this,*t,**tt):
print "We have a nice table by the window"
print t,tt
return this
def __exit__(this,*t,**tt):
print "Can we have a cleaner over here please?"
print t,tt
# map, filter and reduce examples
# -------------------------------
# Python 2 and 3 compatible printing
# ----------------------------------
print("Here are the remote IP visitors to your images")
for k in range(len(found)):
sep = ((k+1)%4 and k+1 != len(found)) and " " or "\n"
sys.stdout.write("%16s%s"%(found[k],sep))