Exercises, examples and other material relating to training module Y115. This topic is presented on public courses
Python is a wide-ranging language, so you won't have learnt everything about it on the early stages of your course. In this module, we assume fundamentals knowledge, and we go on to cover some of the more advanced features of the language that you'll need as you go on to write larger applications of the type that Python's so well suited to.
Articles and tips on this subject | updated |
4709 | Some gems from Intermediate Python 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 keyw ... | 2016-10-30 |
4593 | Command line parameter handling in Python via the argparse module A new example from the Python course just completed - looking at command line parameters through the argparse module from the standard Python library (2.7 and 3.2 onwards), and XML handling through the xml.etree.ElementTree module (2.5 onwards)
Here's code setting up an argument parser:
parser ... | 2015-12-09 |
4536 | Json load from URL, recursive display, Python 3.4 Reading a json object from a remote URL (or it that's not available from a local file), displaying the contents of the object, modifying it, and printing it out. A Python 3.4 example from today's Python course. See [here] for full source code.
Note that in Python 2, you would have used urllib2 which ... | 2015-10-14 (short) |
4451 | Running an operating system command from your Python program - the new way with the subprocess module Python's subprocess module has replaced a series of other modules, and if you're writing new code which calls operating system commands you should use this module in perferance to the older stuff. Easy examples at [here].
The "interesting bit" in various languages is not so much how you cal other ... | 2015-03-07 |
4439 | Json is the new marshall, pickle and cPickle / Python Conversion of objects into serial data, such that it can be stored in a file or passed over a network, and restoring it when read back, is a vital topic within any serious object oriented application. It's all very well working with an object on the heap (i.e. in memory while your program runs), but ... | 2015-02-22 |
4298 | Python - an interesting application Python's in use in a very wide variety of applications ... one of the more noteworthy that we got involved in recently was in robot programming .... here's our Python Course delegate with his robot.
Python is an ideal language for tailoring C / C++ applications such as real time control systems as ... | 2014-09-18 |
4211 | Handling JSON in Python (and a csv, marshall and pickle comparison) JSON - JavaScript Object Notation - has become the data encoding standard of choice for many applications far removed from the "Javascript" of its name. It's a compact, readable format which allows collections within a language (that would be lists and dicts in Python, for example) to be encoded into ... | 2013-11-19 |
4085 | JSON from Python - first principles, easy example Here's a short example of how to pick up a JSON feed from a URL in Python. All the examples I came across looked very complicated, so I thought I would write one that's really straightforward:
Open a remote URL feed that provides a JSON object:
response = urllib2.urlopen('http://www.wellho.net/services/pix.json')
Read ... | 2013-05-18 |
3469 | Teaching dilemma - old tricks and techniques, or recent enhancements? Where there's something that's a frequent requirement on one of the subjects we teach, but can be hard to achieve, we'll spend more than just a minute or two covering it on our courses. After all, the tips and techniques of how to make the most of a programming language are every bit as important as ... | 2011-10-08 |
3442 | A demonstration of how many Python facilities work together Many of our demonstrations on the Well House Consultants site show individual features of a language - taken in isolaion to show you how they work. However, during a course we usually write further examples to show you how features work in combination to give a total result / solution to an application.
On ... | 2011-09-16 (longest) |
463 | Splitting the difference Perl's split function takes a string of text, and divides it up at a delimiter of your choice into a list of shorter strings ... it's one of the "power tool" functions of Perl and a vital part of the language. So how come that you can write a Tcl program and use its version of split - or omit the split ... | 2011-03-01 |
3089 | Python regular expressions - repeating, splitting, lookahead and lookbehind If you're looking for part of a string that's repeated again later in the string, you can capture the first occurrence and then use a back reference (\1, \2 etc) to refer to "same again". In Python, you can also name the element that you want to repeat - examples [here].
If you have a number of fields ... | 2010-12-17 |
2790 | Joining a MySQL table from within a Python program Python is a big subject.
MySQL is a big subject.
Yet the two fin so well together that they can be firmly linked, and work well together, using the tiniest drops of glue.
I've provided examples here before, but in answer to a request from a recent delegate, I've just added a fresh example onto our ... | 2010-06-02 |
2786 | Factory methods and SqLite in use in a Python teaching example I've just uploaded a neat little example - [here] from last week's Python course - it's amazing what you can do in a very few lines of code.
The application scenario is that we have a database of customers - some are residents at our hotel and others are delegates on our courses. Being mercenary folks, ... | 2010-06-02 |
2721 | Regular Expressions in Python I took advantage of the lack of a whiteboard yesterday to write notes on the screen - in an edit window - while I was teaching a class about regular expressions, and how they're used in Python. Which has resulted in a rather nice example that's now online [here].
Regular expressions can frighten newcomers. ... | 2010-05-16 |
2765 | Running operating system commands from your Python program As from Python 2.6, os.popen and friends are deprecated methods and you should use the subprocess module for subprocesses. So that's the way to go if you want to run operating system commands. New example showing this - [here]
Starting with:
from subprocess import *
I can run a process and allow ... | 2010-05-15 |
2764 | Python decorators - your own, staticmethod and classmethod Python Decorators are wrappers that you may apply around methods. So they're rather like the bread around a sandwich.
There are a number of standard decorators provided with Python - such as @classmethod and @staticmethod (see [here]) which allow you to turn methods into unbound (static, class) ones. ... | 2010-05-14 |
2746 | Model - View - Controller demo, Sqlite - Python 3 - Qt4 The Model - View - Controller approach to application design keeps the Graphic User Interface (the view) separate from the database (the model), with the controller in between the two, describing how the data is displayed, and how changes to the data are saved away in the database. By separating the ... | 2010-04-29 |
2745 | Connecting Python to sqlite and MySQL databases For developing a Python application, and for light use, the SQLite database may be a good choice ... but then you may want to be able to expand the system to use a central database with a more classic server such as MySQL at a later date.
Using Polymorphism in Python, and with a careful choice of which ... | 2010-04-28 |
2655 | Python - what is going on around me? If you want to find out what's going on around you in a Python program, you can uses your system's environment variables via the os.environ module - the keys() method telling you which environment variables are available to you. There's an example [here] from our Python Programming training course.
Python ... | 2010-03-03 |
2462 | Python - how it saves on compile time Python is interpreted every time you run the code ... or that's the simple first story you're told. But really it's not that simple ... and not that inefficient.
When you run a Python program, it is not interpreted line by line as you run it - it is interpreted all at once before it's run at all, ... | 2009-10-20 |
2435 | Serialization - storing and reloading objects In most of the languages we teach, data is held in memory on a "heap" with a "symbol table" holding the names of the variables, where they are stored, and what type of information the (currently) contain. When you write simple variables out to a file (or the screen) functions like print or puts (Tcl) ... | 2009-10-04 |
2407 | Testing code in Python - doctest, unittest and others The doctest and unittest modules of Python allow you to provide test harnesses for your classes / packages. Designing applications from the bottom up, you'll want to ensure that each of your code levels works and works well. You'll want to provide an example of what it should do for the next level ... | 2009-09-17 |
1136 | Buffering output - why it is done and issues raised in Tcl, Perl, Python and PHP When you go to board a plane at Heathrow, do the ground staff admit the passengers one at a time, ensuring that each is seated before the next boards, and sealing and re-opening the main doors between each? What a stupid and inefficient system that would be!!
The same thing applies when you're programming ... | 2009-08-31 |
901 | Python - listing out the contents of all variables You can often guess which course I'm giving by the topic of the my daily writing ....
"How do I list out the contents of all variables with names starting v-a-r ... a question from yesterday.
The immediate answer:
# print all variables starting with "var"
sum = 9
var3 = 78
var6 = 99
varsity = 56
variable ... | 2009-01-01 |
1876 | Python Regular Expressions Python supports string pattern matching to regular expressions, using Perl style regular expressions.
The re module - loaded via
import re
brings the appropriate elements into your program ... ready to use.
Patterns that you want to match against are called Regular Expressions and are created ... | 2008-11-08 |
1149 | Turning objects into something you can store - Pickling (Python) If you're working with objects in an OO language and you want to transfer them to another computer ... or simple save them from the current application for reloading into that application or another one on the same computer later on, you need to serialize the objects.
Data within OO programming languages ... | 2008-10-11 |
239 | What and why for the epoch The Epoch occurred on 1st January 1970 ... at midnight, at the start of the day, GMT. It's an important concept in many programming languages, which work with times and dates before and after the epoch - it means that you can compare dates and times easily, even where month ends and different timezones ... | 2008-05-16 |
1337 | A series of tyre damages Warwick, 11 a.m., Saturday, for later posting. I left Maastricht yesterday evening at quarter past four, was at my overnight stop in Kent from 11 p.m. to 5:30 a.m., and should have reached Birmingham at around 08:00 ready for the Python Conference - Pycon.
So want went wrong? Debris on the motorway! ... | 2007-09-11 |
1336 | Ignore case in Regular Expression Do you want to ignore case in a regular expression? There are a variety of ways of doing it ... depending on the language you're writing. Here are some hints:
/abcd/i Perl - an i after the regular expression
eregi PHP - use eregi rather than ereg
re.I or re.IGNORECASE Python - extra parameters ... | 2007-09-07 |
1305 | Regular expressions made easy - building from components There seems to be a certain macho desire in many programmer's minds to write a single complicated regular expression to match against an input line, ignorning the structured approach that everyone accepts quite cheerfully in almost every other case. Have a look at this Python line:
wholeline = r"\d\d-...-\d\d\d\d\s+(\d\d):(\d\d):(\d\d.\d\d),\s+(-?\d+\.\d+),\s+(-?\d+\.\d+),(-?\d+\.\d+),\s+(-?\d+\.\d+),(-?\d+\.\d+),\s+(-?\d+\.\d+)"
Impressive, ... | 2007-08-16 |
1043 | Sending an email from Python Good question - came up on the course yesterday and all the examples I could find were longwinded or obfursacted to avoid them abusing the email addresses of the people named.
At the risk of getting lots of people trying this out and filling my mail box, here is a sample piece of code that emails grom ... | 2007-01-18 |
753 | Python 3000 - the next generation There is no definite schedule for Python 3 (a.k.a. Python 3000 or Py3K) but a Python Enhancement Proposal (PEP) that details plans exists. There's a guiding principle to reduce duplication by removing old ways of doing things which will break version 2 compatibility. (Heard it before ... Perl 5 to Perl ... | 2006-06-09 |
183 | The elegance of Python If I need a little utility and I'm running a Perl course, then I'll write it in Perl. Training in Tcl ... then that's the scripting language to use for other requirements that day (two languages in a day is too much). And of course if I'm running Python Programming it has to be .....
MY GOODNESS - ... | 2006-06-05 |
again | Looking for part of a string that repeats later |
bench.py | Find largest 5 files in or below a directory |
class_v_static.py | Decorators - your own, plus @classmethod and @staticmethod |
cli.py | Report on command line parameters |
cpick | Store an object via cPickle |
cunpick | recover a pickled object |
curr27 | From Python 2.7 - currency alternative |
currency.py | Formatting a currency using Regular Expression |
emma | Regular expression then split |
eo2.py | Scanning log file for instability |
flist.py | All the files in a directory - which is largest? |
gad1.py | Create a Gadfly (SQL) database and insert data |
gad2.py | Running an SQL query - Gadfly |
get_os.py | Get Operating System information |
hostcounter | Extract data from huge flow. Save and restore via Json |
hostfilter.py | Extract host computer information - strip comments with regular expressions |
logs.json | Json data for http://www.wellho.net/resources/ex.php?item=y115/hostcounter |
mar_in.py | Loading in an object saved via the marshal module |
mar_out.py | Marshal converts an object to a string |
myuser.py | class for use in pickle / unpickle demo |
penv.py | Python environment variables |
pocohunter | Looking for postcodes in a string |
psplit | Splitting at a string v splitting at a regular expression |
py_mysql.py | MySQL to Python using MySQL Connector |
pysql_another | Using a MySQL database - select across multiple tables |
python_mysql_web.py | Python, MySQL, CGI (Web) board |
re1.py | Regular expression - first example |
re2.py | Stripping spaces |
re3.py | Find email addresses in line of text |
re4.py | Regular expression split over several lines |
refun | evaluating a net list in EQN format |
relib.py | Some common regular expressions |
retest.py | Regular Expression test engine |
sql1.py | Hello MySQL World in Python |
sql1a.py | MySQL to Python using MySQLdb |
sqlite.py | Sqlite3 / Python 3 example |
subp | Subprocess module - replacement for os.popen |
syscommand | Running an operating system command in Python |
tim | Some time handlers |
timestuff.py | Some time handling |
Is Python compiled or interpreted?.
Regular expressions in Python.
Elements of a Python-style regular expression.
Anchors (Assertions).
Literal characters.
Character groups.
Counts.
Alternation.
Grouping.
Modifiers.
Some examples.
Methods available on Python regular expressions.
Some more examples and features of regular expressions.
Some popular regular expressions.
Running Python and system commands from within.
Handling dates and times in Python.
Pickling and shelving.
Accessing the environment from Python.
Talking to SQL databases from Python.
If you are looking for a complete course and not just a information on a single subject, visit our
Listing and schedule page.
Well House Consultants specialise in training courses in
Ruby,
Lua,
Python,
Perl,
PHP, and
MySQL. We run
Private Courses throughout the UK (and beyond for longer courses), and
Public Courses at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if
you live in a different
country or continent to us.
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre.