This is now an archive web site. Some is still relevant as at June 2026 but some is purely of historic interest.
Lisa and I (Graham) are now fully retired from IT training.We have made many friends over 30 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Apache, Linux and Solaris/SunOS too. Our training notes are out of date, but with upward compatability some examples remain operational and relevant. You are welcome to make use of them "as seen", at your own risk. We now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We remain active, enjoying the times that we are retired but still healthy enough in mind and body to do things!
I am also active in many other area and still look after a lot of web sites - you can find an index ((here)) |
|
Some gems from an introduction to Python (written 2016-10-29)
This week, I ran a private Introduction to Python 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!
# Using an exception to see in an object exists (in this case within a dict)
# --------------------------------------------------------------------------
try:
counter[website] += 1
except KeyError,e:
counter[website] = 1
print e
# sorting and printing by value
# -----------------------------
def byvalue(that,this):
return cmp(counter[this],counter[that])
domains = counter.keys()
domains.sort(byvalue)
for d in domains:
print "%-8d %s" % (counter[d],d)
# printing documentation within a test harness
# --------------------------------------------
if __name__ == "__main__":
# (main test code here)
print
print person.__doc__
# printing with old and new formatters
# ------------------------------------
print "%8.2f is the value for %-7s today" % (people[person],person)
print "{0:8.2f} is the value for {1:<7s} today".format(people[person],person)
# How to unwrap a collection into a function
# ------------------------------------------
sz = (12,16)
# print getArea(sz[0],sz[1]) ; this is the long-winded way!
print getArea(*sz)
# decorator ... new and old ways
# ------------------------------
@staticmethod
def factory(raw):
# code
return product
# decorator at top of method does the following:
# factory = staticmethod(factory)
# generator ... and list code as comments
# ---------------------------------------
def places():
# small = []
for lyne in fh.xreadlines():
(code on line)
# small.append(fol[6])
print "splash"
yield fol[6]
# return small
# functions that take different parameter patterns
# ------------------------------------------------
def myrange(*inputs):
# passed a list
if isinstance(inputs[0],list):
inputs = inputs[0]
# passed three params
if len(inputs) == 3:
etc
# variety of regular expressions to match a postcode
# --------------------------------------------------
poco = re.compile(r'[A-Z]{1,2}[0-9]{1,2} {1,}[0-9]{1}[A-Z]{2}')
poco = re.compile(r'(([A-Z]{1,2})[0-9]{1,2}) {1,}([0-9]{1}[A-Z]{2})')
poco = re.compile(r'(([A-Z]{1,2})[0-9]{1,2}) {1,}([0-9]{1}[A-Z]{2})$')
poco = re.compile(r'(([A-Z]{1,2})\d[A-Z0-9]?)\s+([0-9][A-Z]{2})',re.IGNORECASE)
# static variable in a function
# -----------------------------
def more():
print "dot"
try:
more.keep += 1
except:
more.keep = 1
print more.keep
# lazy operator, python style with and and or
# -------------------------------------------
def getNet(amount,vset=None):
# vatrate is an external (fallback) variable
vr = (vset == None) and vatrate or vset
# using a slice on a string
# -------------------------
uat = sample.find("u")
sat = sample.find(" ",uat)
print sample[uat:sat] (written 2016-10-29)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles Q110 - Object Orientation and General technical topics - Programming Algorithms [202] Searching for numbers - (2005-02-04) [227] Bellringing and Programming and Objects and Perl - (2005-02-25) [642] How similar are two words - (2006-03-11) [1157] Speed Networking - a great evening and how we arranged it - (2007-04-21) [1187] Updating a page strictly every minute (PHP, Perl) - (2007-05-14) [1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14) [1840] Validating Credit Card Numbers - (2008-10-14) [1949] Nuclear Physics comes to our web site - (2008-12-17) [2189] Matching disparate referencing systems (MediaWiki, PHP, also Tcl) - (2009-05-19) [2259] Grouping rows for a summary report - MySQL and PHP - (2009-06-27) [2509] A life lesson from the accuracy of numbers in Excel and Lua - (2009-11-21) [2586] And and Or illustrated by locks - (2010-01-17) [2617] Comparing floating point numbers - a word of caution and a solution - (2010-02-01) [2894] Sorting people by their names - (2010-07-29) [2951] Lots of way of converting 3 letter month abbreviations to numbers - (2010-09-10) [2993] Arrays v Lists - what is the difference, why use one or the other - (2010-10-10) [3042] Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl - (2010-11-11) [3072] Finding elements common to many lists / arrays - (2010-11-26) [3093] How many toilet rolls - hotel inventory and useage - (2010-12-18) [3102] AND and OR operators - what is the difference between logical and bitwise varieties? - (2010-12-24) [3451] Why would you want to use a Perl hash? - (2011-09-20) [3620] Finding the total, average, minimum and maximum in a program - (2012-02-22) [3662] Finding all the unique lines in a file, using Python or Perl - (2012-03-20) [4325] Learning to program - what are algorithms and design patterns? - (2014-11-22) [4401] Selecting RECENT and POPULAR news and trends for your web site users - (2015-01-19) [4402] Finding sum, minimum, maximum and average in Python (and Ruby) - (2015-01-19) [4410] A good example of recursion - a real use in Python - (2015-02-01) [4652] Testing new algorithms in PHP - (2016-02-20) [4656] Identifying the first and last records in a sequence - (2016-02-26) Y199 - Python - Fast Start [2017] Python - a truly dynamic language - (2009-01-30) [3519] Python - current versions and implementations (CPython, Jython, IronPython etc) - (2011-11-13) [4709] Some gems from Intermediate Python - (2016-10-30) [4712] A reminder of the key issues to consider in moving from Python 2 to Python 3 - (2016-10-30) [4713] Equality (in Python) - (2016-10-30)
Some other Articles
Convering from Python 2 to Python 3 - an update, and the 2to3 utilitySearching a Json or XML structure for a specific key / value pair in PythonScons - a build system in Python - building hello worldSome gems from an introduction to PythonMelksham trial train service is to be made permanentCourse dates - from October 2016 to December 2017Three months in community rail picturesWhat do people use the bus for in Wiltshire - survey interim results.Up and down TransWilts - some pictures off the rails!
|
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. So much so, and it's so long ago
that we are retired
Link to Ezine home page (for reading).
|
|