My first book, Python 101 is on sale this month. You can get it on Amazon US for $2.99 or you can buy it for 50% off via my website. To get 50% off, just enter the following offer code at checkout: fall2014
Wed 3 Sep 2014
Recently I wrote about the arrow project and one of my readers mentioned that another datetime related project known as Delorean. So in this article, we’ll spend some time going over the delorean project. This will be a high level article as there is no reason to rewrite the delorean’s documentation. (more…)
Wed 27 Aug 2014
The wxPython GUI toolkit includes its own date / time capabilities. Most of the time, you can just use Python’s datetime and time modules and you’ll be fine. But occasionally you’ll find yourself needing to convert from wxPython’s wx.DateTime objects to Python’s datetime objects. You may encounter this when you use the wx.DatePickerCtrl widget.
Fortunately, wxPython’s calendar module has some helper functions that can help you convert datetime objects back and forth between wxPython and Python. Let’s take a look:
def _pydate2wxdate(date): import datetime assert isinstance(date, (datetime.datetime, datetime.date)) tt = date.timetuple() dmy = (tt, tt-1, tt) return wx.DateTimeFromDMY(*dmy) def _wxdate2pydate(date): import datetime assert isinstance(date, wx.DateTime) if date.IsValid(): ymd = map(int, date.FormatISODate().split('-')) return datetime.date(*ymd) else: return None
You can use these handy functions in your own code to help with your conversions. I would probably put these into a controller or utilities script. I would also rewrite it slightly so I wouldn’t import Python’s datetime module inside the functions. Here’s an example:
import datetime import wx def pydate2wxdate(date): assert isinstance(date, (datetime.datetime, datetime.date)) tt = date.timetuple() dmy = (tt, tt-1, tt) return wx.DateTimeFromDMY(*dmy) def wxdate2pydate(date): assert isinstance(date, wx.DateTime) if date.IsValid(): ymd = map(int, date.FormatISODate().split('-')) return datetime.date(*ymd) else: return None
You can read more about this topic on this old wxPython mailing thread. Have fun and happy coding!
Wed 13 Aug 2014
The other day, I saw an interesting question on StackOverflow where the author asked if there was a way to serialize a Python dictionary into a human-readable format. The answer that was given was to use a package called jsonpickle, which will serialize complex Python objects to and from JSON. This article will give you a quick overview of how to use the project.
Thu 7 Aug 2014
The other day, I came across an article about a fork of the pbs package called sh. These packages are wrappers for Python’s subprocess module. Basically sh allows you to import and use shell commands directly from Python. This article will go over a few examples to show you how to use this fun little library.
Note that at the time of writing, the sh package only supports Linux and Mac. If you need Windows support, then you should try the pbs project.
Tue 5 Aug 2014
The arrow project is an attempt to wrap Python’s time and datetime modules into a single API. It also claims to plug gaps in functionality in those modules, such as time spans, ISO-8601 and humanization. You can kind of think of arrow as a drop-in replacement for Python’s datetime and time modules, much like the requests project can be used instead of Python’s urllib. Arrow supports Python 2.6, 2.7 and 3.3 at the time of this writing.
Mon 21 Jul 2014
The Pony ORM project is another object relational mapper package for Python. They allow you to query a database using generators. They also have an online ER Diagram Editor that is supposed to help you create a model. They are also one of the only Python packages I’ve seen with a multi-licensing scheme where you can develop using a GNU license or purchase a license for non-open source work. See their website for additional details.
In this article, we will spend some time learning the basics of this package. (more…)
Thu 17 Jul 2014
I thought it would be fun to try out a few different Python object relational mappers (ORMs) besides SQLAlchemy. I recently stumbled across a project known as peewee. For this article, we will take the examples from my SQLAlchemy tutorial and port it to peewee to see how it stands up. The peewee project supports sqlite, postgres and MySQL out of the box, which isn’t as flexible as SQLAlchemy, but it’s not bad either. You can also use peewee with the Flask web framework, via a handy Flask-peewee plugin.
Anyway, let’s start playing around with this fun little library!
Mon 14 Jul 2014
Redirecting stdout seems to be a pretty common request on the wxPython users group, so I decided to see how easy it would be to do it with Tkinter. The typical use case for redirecting stdout or stderr is that you are calling some other process (like ping or tracert) and you want to catch what it’s doing to put it into your UI. Usually you can just use Python’s subprocess module and call its communicate() method to access the data. Then you can just print it to stdout and it will magically appear in your UI’s widget of choice.
Our finished user interface will look something like the following:
Let’s find out how to do this with Tkinter: (more…)
Fri 11 Jul 2014
I’ve been using wxPython for quite a while now and I see certain questions come up on a fairly frequent basis. One of the popular ones is how to ask the user for their credentials before loading up the rest of the application. There are several approaches to this, but I am going to focus on a simple solution as I believe this solution can be used as the basis for more complex solutions.
Basically what we want to happen is for the user to see a login dialog where they have to enter their username and password. If they enter it correctly, then the program will continue to load and they’ll see the main interface. You see this a lot on websites with a common use case being web email clients. Desktop applications don’t include this functionality as often, although you will see it for Stamps.com’s application and for law enforcement software. We will be creating a dialog that looks like this: