Python


Python comes with a fun module called functools. One of its classes is the partial class. You can use it create a new function with partial application of the arguments and keywords that you pass to it. You can use partial to “freeze” a portion of your function’s arguments and/or keywords which results in a new object. Another way to put it is that partial creates a new function with some defaults. Let’s look at an example!

>>> from functools import partial
>>> def add(x, y):
...     return x + y
... 
>>> p_add = partial(add, 2)
>>> p_add(4)
6

Here we create a simple adding function that returns the result of adding its arguments, x and y. Next we create a new callable by creating an instance of partial and passing it our function and an argument for that function. In other words, we are basically defaulting the x parameter of our add function to the number 2. Finally we call our new callable, p_add, with the argument of the number 4 which results in 6 because 2 + 4 = 6.

(more…)

The Python 101 Screencast has been finished for a little over a month now and I am now releasing it for general consumption.

mousecovertitlejpg_sm_title

The Python 101 Screencast is based on my book, Python 101. I went through all 44 chapters of the book and turned each of them into a standalone screencast. In other words, the screencast covers 44 videos. You can view the first eleven videos on my blog’s youTube page.

What you get:

A minimum of 44 episodes + you will receive a PDF, MOBI and EPUB version of the Python 101 book with your purchase. You will also receive a PDF copy formatted to A4 size.

Part One

The first part is the beginner section. In it you will learn all the basics of Python. From Python types (strings, lists, dictionaries) to conditional statements to loops. You will also learn about comprehensions, functions and classes and everything in between!

Part Two

This section is a curated tour of the Python Standard Library. The intent isn’t to cover everything in it, but instead it is to show the reader that you can do a lot with Python right out of the box. We’ll be covering the modules I find the most useful in day-to-day programming tasks, such as os, sys, logging, threads, and more.

Part Three

An all intermediate section covering lambda, decorators, properties, debugging, testing and profiling.

Part Four

Now things get really interesting! In part four, we will be learning how to install 3rd party libraries (i.e. packages) from the Python Package Index and other locations. We will cover easy_install and pip. This section will also be a series of tutorials where you will learn how to use the packages you download. For example, you will learn how to download a file, parse XML, use an Object Relational Mapper to work with a database, etc.

Part Five

The last section of the series will cover how to share your code with your friends and the world! You will learn how to package it up and share it on the Python Package Index (i.e. how to create an egg or wheel). You will also learn how to create executables using py2exe, bb_freeze, cx_freeze and PyInstaller. Finally you will learn how to create an installer using Inno Setup

Buy Now

This week we welcome Chris Withers as our PyDev of the Week! Chris has been using Python for quite a while. I think the first packages I used of his were the excellent xlrd and xlwt packages, used for reading and writing Excel files. You can get an idea of his contributions to Python on his github profile. Let’s take some time and get to know him better.

Can you tell us a little about yourself (hobbies, education, etc):

I grew up in Zimbabwe and now like in the UK, but have ended up with two passports, both for countries I’ve never lived in.

When I’m not coding, I’m either umpiring for field hockey matches or doing things with dance music.

(more…)

Earlier this week, I came across another fun package called datefinder. The idea behind this package is that it can take any string with dates in it and transform them into a list of Python datetime objects. I would have loved this package at my old job where I did a lot of text file and database query parsing as there were many a time when finding the date and getting it into a format I could easily use was quite the nuisance.

Anyway, to install this handy package all you need to do is this:


pip install datefinder

I should note that when I ran this, it ended up also installing the following packages:

  • PyYAML-3.11
  • dateparser-0.3.2
  • jdatetime-1.7.2
  • python-dateutil-2.4.2
  • pytz-2015.7
  • regex-2016.1.10
  • six-1.10.0
  • umalqurra-0.2

Because of all this extra stuff, you might want to install this package into a virtualenv first. Let’s take a look at some code. Here’s a quick demo I tried:

>>> import datefinder
>>> data = '''Your appointment is on July 14th, 2016. Your bill is due 05/05/2016'''
>>> matches = datefinder.find_dates(data)
>>> for match in matches:
... 	print(match)
... 
2016-07-14 00:00:00
2016-05-05 00:00:00

As you can see, it worked quite well with these two common date formats. Another format that I used to have to support was the fairly typical ISO 8601 date format. Let’s see how datefinder behaves with that.

>>> data = 'Your report is due: 2016-02-04T20:16:26+00:00'
>>> matches = datefinder.find_dates(x)
>>> for i in matches: 
...     print(i)
... 
2016-02-04 00:00:00
2016-02-04 20:16:26

Interestingly, this particular version of the ISO 8601 format causes datefinder to return two matches. The first is just the date while the second has both the date and the time. Anyway, hopefully you’ll find this package useful in your projects. Have fun!

Webucator recently contacted me to let me know that they have finished up an Introduction to Python Training that they are allowing people to take for free for the month of February. The course is made up of videos, exercises, readings, and quizzes. You can get it free by using the following code when you register: PYTHON

I thought they did a good job when they did a video based on my context manager article.

The other day, I came across this interesting experimental package called import_from_github_com. The package uses the new import hooks provided in PEP 302 to basically allow you to import a package from github. What the package actually appears to do is install the package and add it to locals. Anyway, you need Python 3.2 or greater, git and pip to use this package.

Once it’s installed, you can do the following:

>>> from github_com.zzzeek import sqlalchemy
Collecting git+https://github.com/zzzeek/sqlalchemy
  Cloning https://github.com/zzzeek/sqlalchemy to /tmp/pip-acfv7t06-build
Installing collected packages: SQLAlchemy
  Running setup.py install for SQLAlchemy ... done
Successfully installed SQLAlchemy-1.1.0b1.dev0
>>> locals()
{'__builtins__': <module 'builtins' (built-in)>, '__spec__': None,
 '__package__': None, '__doc__': None, '__name__': '__main__', 
'sqlalchemy': <module 'sqlalchemy' from '/usr/local/lib/python3.5/site-packages/sqlalchemy/__init__.py'>,
 '__loader__': <class '_frozen_importlib.BuiltinImporter'>}

One important note that isn’t mentioned on the package’s github page is that you need to run Python as an administrator or it won’t be able to install its packages. At least, this was true for me on Xubuntu. Anyway I thought this was a neat little package and demonstrates some of neat little import hooks that you can add to Python 3.

This week we welcome Oliver Schoenborn as our PyDev of the Week. He is the author of the PyPubSub project, a version of which is included with wxPython. He has been an active contributor on the wxPython mailing list where I have always appreciated his insights. You might find his Dr. Dobbs article interesting as well, even though it’s a bit old. Let’s spend a few moments getting to know him better!

Can you tell us a little about yourself (hobbies, education, etc):

I’m a Senior Consultant at CAE Inc in Ottawa, where I engineer simulation systems for a variety of applications.

I started programming on an Apple IIe in 1982 when I was 13 years old. I bought it used, with my own money that I had saved for a few years for “some day when I would want something really big”. I discovered Assembly programming on that machine, with peeks and pokes and interrupts and registers, and was hooked. I moved on to Basic and Pascal and Prolog. I created my first simulation in my last year of high-school for a programming course project. Discovering C++ in the mid-90’s was a revelation, I found the object-oriented approach so intuitive, and I’m still a stickler for clean interfaces and refactoring. During my high-school years I thought that Physics was my passion and I received my Physics PhD in 1998 from University of Toronto, but I came to realize that programming was my real passion and have made that the focus of my professional career.

I haven’t worked in Physics since my degree, but during my PhD I developed many valuable skills such as problem solving, bug finding, testing, approximations, process modeling, and Unix development. As such, I have been fortunate to work on some very fun and challenging industrial projects, including: crane operation trainer in a fully immersive virtual environment (with a real crane cab and controls, and surround display etc);a Search and Rescue trainer which allows an instructor to challenge a student to spot and alert against threats on a military aircraft; an Avionics maintenance trainer that allows a classroom of students to each troubleshoot defects on a modern aircraft using virtual tools and virtual cockpit and work areas and a Human Resources planner that allows an Operational Research department to conduct “what-if” analyses of 100,000 employees evolving over the span of 20-30 years in the future.

Other than an obsession for programming, I love snowboarding, and playing the harmonica (blues and folk, although I don’t have much time anymore to learn anything new). If you are middle-aged and want to learn how to snowboard without breaking your rear-end, let me know and I’ll share the tricks that allowed me to enjoy this wonderful sport.

(more…)

Every so often you will find yourself needing to write code that traverse a directory. They tend to be one-off scripts or clean up scripts that run in cron in my experience. Anyway, Python provides a very useful method of walking a directory structure that is aptly called os.walk. I usually use this functionality to go through a set of folders and sub-folders where I need to remove old files or move them into an archive directory. Let’s spend some time learning about how to traverse directories in Python!

(more…)

The other day, I came across an interesting project called sh, which I believe refers to the shell (or terminal). It used to be the pbs project, but they renamed it for reasons I haven’t figured out. Regardless, the sh package is a wrapper around subprocess that allows the developer to call executables a little more simply. Basically it will map your system programs to Python functions. Note that sh only supports linux and mac, whereas pbs supported Windows too.

Let’s look at a couple of examples.

>>> from sh import ls
>>> ls
<command '/bin/ls'/>
>>> ls('/home')
user_one  user_two

(more…)

Python 3.5 added an interesting new library called typing. This adds type hinting to Python. Type hinting is kind of declaring your functions arguments to have a certain type. However the type hinting is not binding. It’s just a hint, so there’s nothing preventing the programmer from passing something they shouldn’t. This is Python after all. You can read the type hinting specification in PEP 484 or you can just read the theory behind it in PEP 483.

Let’s take a look at a simple example:

>>> def some_function(number: int, name: str) -> None:
	print("%s entered %s" % (name, number))
 
 
>>> some_function(13, 'Mike')
Mike entered 13

This means that some_function expects two arguments where the first is an integer and the second is a string. It should also be noted that we have hinted that this function returns None.

(more…)

Next Page »