I was looking through some of my old code and noticed this old script where I was creating a log of all running processes every 5 minutes. I believe I originally wrote the code to help me diagnose rogue processes that were eating memory or pegging the CPU. I was using the psutil project to get the information I needed, so if you’d like to follow along you will need to download and install it as well.
Tue 21 Oct 2014
Mon 20 Oct 2014
I recently saw someone asking how to bring a window to the front in Windows and I realized I had had some old unreleased code that might help someone with this task. A long time ago, Tim Golden (and possibly some other fellows on the PyWin32 mailing list) showed me how to make windows come to the front on Windows XP, although it should be noted that it also works on Windows 7. If you’d like to follow along, you will need to download and install your own copy of PyWin32.
Mon 20 Oct 2014
Mon 13 Oct 2014
Fri 26 Sep 2014
Tue 23 Sep 2014
I’ve been hearing some buzz about a newish web service called Twilio which allows you to send SMS and MMS messages among other things. There’s a handy Python wrapper to their REST API as well. If you sign up with Twilio, they will give you a trial account without even requiring you to provide a credit card, which I appreciated. You will receive a Twilio number that you can use for sending out your messages. Since you are using a trail account, you do have to authorize any phone numbers you want to send messages to before you can actually send a message. Let’s spend some time learning how this works!
Wed 3 Sep 2014
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.