Cross-Platform


Recently I ran into an issue where an application that calls Python would insert int into Python’s namespace, which overwrites Python’s built-in int function. Since I have to use the tool and I needed to use Python’s int function, I needed a way around this annoyance.

Fortunately, this is fairly easy to fix. All you need to do is import int from __builtin__ and rename it so you don’t overwrite the inserted version:

from __builtins__ import int as py_int

This gives you access to Python’s int function again, although it is now called py_int. You can name it whatever you like as long as you don’t name it int.

The most common circumstance where one shadows builtins or other variables is when the developer imports everything from a package or module:

from something import *

When you do an import like the one above, you don’t always know what all you have imported and you may end up writing your own variable or function that shadows one that you’ve imported. That is the main reason that importing everything from a package or module is so strongly discouraged.

Anyway, I hope you found this little tip helpful. In Python 3, the core developers added a builtins module basically for this very purpose.

Last month we looked at how to create Microsoft Excel (i.e. *.xls) files using the xlwt package. Today we will be looking at how we can read an *.xls/*.xlsx file using a package called xlrd. The xlrd package can be run on Linux and Mac as well as Windows. This is great when you need to process an Excel file on a Linux server.

We will start out by reading the first Excel file we created in our previous article.

Let’s get started! (more…)

Reportlab recently released version 3.1 which now fully supports Python 3 and Python 2.7. They had actually released a Python 3 compatible version about a month or so ago, but this one sounds like they’ve worked the bugs out of that initial release as this version also supports their commercial customers. I find this exciting in that one of my favorite Python packages finally supports Python 3. I haven’t moved to Python 3 myself because I use so many packages that are only available for Python 2 (and also because I have yet to work for a place that uses Python 3). There aren’t really any awesome new features in Reportlab 3.1 as of yet, but you can read through their release notes and decide for yourself.

One cool new feature of Reportlab is that you can now install it with pip or easy_install. They have also introduced Python wheel packages as their primary installation type, although you can still download the source. See PyPI for the open source downloads.

Check it out and let me know what you think!

When you’re first starting out as a Python programmer, you don’t think about how you might need to install an external package or module. But when that need appears, you’ll want to know how to in a hurry! Python packages can be found all over the internet. Most of the popular ones can be found on the Python Package Index (PyPI). You will also find a lot of Python packages on github, bitbucket, and Google code. In this article, we will be covering the following methods of installing Python packages:

  • Install from source
  • easy_install
  • pip
  • Other ways to install packages

(more…)

The lxml.objectify sub-package is extremely handy for parsing and creating XML. In this article, we will show how to create XML using the lxml package. We’ll start with some simple XML and then try to replicate it. Let’s get started! (more…)

Code profiling is an attempt to find bottlenecks in your code. Profiling is supposed to find what parts of your code take the longest. Once you know that, then you can look at those pieces of your code and try to find ways to optimize it. Python comes with three profilers built in: cProfile, profile and hotshot. According to the Python documentation, hotshot “no longer maintained and may be dropped in a future version of Python”. The profile module is a pure Python module, but adds a lot of overhead to profiled programs. Thus we will be focusing on cProfile, which has an interface that mimics the profile module. (more…)

kivy_poster_1

Kivy is an open source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps. The Kivy organization is organizing its second application development contest.! This is a great chance for new and experienced users to show off their skills and compete for prizes. Entries will be judged on a range of criteria accessible to both new and experienced programmers, so don’t be afraid to dive in! For more information, visit http://kivy.org/#contest

The wxPython Google Group was discussing different methods of catching exceptions in wxPython the other day. If you use wxPython a lot, you will soon realize that some exceptions are difficult to catch. The wxPython Wiki explains why. Anyway, the fellows on the list were recommending the use of sys.excepthook. So I took one of the methods they mentioned and created a little example: (more…)

Reportlab is a very flexible PDF creation package for Python. You can layout your documents using absolute positioning or by using Flowable objects, such as a Paragraph, a Table or Frame. You can even mix the two together! In this article, we will be looking at how to create some custom Flowables. For example, what do you do if you need to add a line to demarcate the start of a new section in your document? There isn’t really a built-in Flowable for that, so we’ll design our own. We will also design a flowable that has a line and a box with text inside of it.

Let’s get started! (more…)

There’s a Python book contest going on over on the Bite Sized Python Tips blog. You can get one of 3 copies of the book Tkinter GUI Application Development by Bhaskar Chaudhary. I reviewed this book late last year and found it be a really interesting book. I think it will give you lots of good ideas to try in developing your own GUI applications. Now’s your chance to get a copy of this neat book!

« Previous PageNext Page »