Cross-Platform


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.

eGenix announced this week that they were releasing an “open source, one file, no installation version of Python”. You can read their entire announcement in their press release here. If you want to check out the actual product, you can get it at the following URL: http://www.egenix.com/products/python/PyRun/.

This is a Unix-based Python and they state that they only provide Linux, FreeBSD and Mac OS X, as 32- and 64-bit versions, so if you’re a Windows guy, you’re out of luck. I think one of the coolest aspects of this tool is that it’s 11MB for Python 2 and 13MB for Python 3, but it still supports most Python applications and code that you’ve written. If you write a lot of code that depends on 3rd party utilities, than I don’t think PyRun will be very helpful to you. But if you depend mostly on Python’s standard library, than I can see this as a very handy tool.

An alternative to PyRun would be Portable Python which provides a large number of 3rd party libraries that are baked in with the standard library. I’ve used Portable Python in several applications of my own where I didn’t want to install Python itself. Anyway, I hope you find this information useful in your own endeavors.

Every once in a while, I run into a situation where I need dummy data to test my code against. If you need to do tests on a new database or table, you will run into the need for dummy data often. I recently came across an interesting package called Faker. Faker’s sole purpose is to create semi-random fake data. Faker can create fake names, addresses, browser user agents, domains, paragraphs and much more. We will spend a few moments in this article demonstrating some of Faker’s capabilities. (more…)

Next Page »