Cross-Platform


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…)

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[2], tt[1]-1, tt[0])
     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[2], tt[1]-1, tt[0])
     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!

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.

(more…)

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.

(more…)

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.

(more…)

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…)

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!

(more…)

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:

tkredirect.png

Let’s find out how to do this with Tkinter: (more…)

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:

wxLogin.png (more…)

Last year there was an Indiegogo crowdfunding campaign in support of PyDev, the Python IDE plugin for Eclipse. It was put on by the primary developer of PyDev, Fabio Zadrozny. As a part of that campaign, Fabio also created LiClipse.

Anyway, Fabio is at it again with a new crowdfunding campaign. You can read about it here. Fabio has two targets for this campaign. The first is adding the following features to PyDev:

  • Allow preferences which are currently global to be configured per-project.
  • Provide a proper way to export/import PyDev settings to a new workspace.
  • Suport for namespace packages in code-completion.
  • Provide a way to auto-document types as well as checking existing types in docstrings when doing a test run.
  • Allow running an external pep8 linter (to support newer versions of pep8.py).
  • Show vertical lines for indentation.
  • Attach debugger to running process (although some caveats apply and under certain circumnstances this may not be possible).
  • Other requests to be defined based on community input as funding allows.

The second is, in my opinion, a bit more interesting. Fabio is planning on writing a profiler for PyDev that can also work outside of PyDev using Python and Qt. He has a list of features for the profiler listed in his campaign. It sounds pretty interesting. It should also be noted that PyDev and the proposed profiler will be open source, so you can always check out how the code works behind the scenes.

If you think either of these items sound interesting, then you should consider supporting Fabio in his endeavors.

Next Page »