Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Monday, April 9, 2012

Interactive Python Eats CPU!

For the last few months I have stopped using the interactive Python shell to try things out while developing my django apps. It was limiting but I could get around the inconvenience...

The reason I stopped using the interactive shell was simple - it used all my system CPU and my laptop ground to a halt; now that was a major inconvenience!

At first I assumed it was a broken installation of Python, or perhaps a memory issue when using Eclipse (ie Java) and Python at the same time, even though that has been my usual working environment for some time.

At last I have found the problem. I had enabled a history file for the interactive shell (so I could avoid re-typing some of the long djano database queries), and that history file was now 388Mb in size. Python was thrashing away reading a history of everything I had typed since late 2011.

The first solution was to removed the file, and the shell became snappy and responsive again.

The second solution is to use logrotate to manage the file.
So this is the simple configuration file, stored as $HOME/.logrotate.conf
Update: oops, forgot to actually trim the file afetr making the backup...

# Global 
options 
compress 
weekly 
rotate=4 missingok 
# log files to rotate 
/home/james/.pythonhistory {
  postrotate
    tail -50 /home/james/.pythonhistory >/tmp/pythonhistory.$$
    cp /tmp/pythonhistory.$$ /home/james/.pythonhistory
    rm /tmp/pythonhistory.$$
  endscript


and a simple addition to my personal crontab to rotate personal files at 1am

# run logrotate for personal files 0 11 * * * /usr/sbin/logrotate -s /home/james/.logrotate.status /home/james/.logrotate.conf

Problem solved!

Friday, February 10, 2012

plan2ics

This is a quick note about a small Python script I wrote to translate my home calendar into the iCalendar format.

I use an "old" calendar system called plan (sometimes "netplan" which is the server program, and easier to find on Google). It is fast, simple, and I have been using it since 1997, so I don't see any reason to change.

Then I bought an Android phone... which loves to sync with the Google Calendar, and can be convinced to sync with most iCal sources, but has no support for my system.

So what I decided to not do was to write an Android content provider to connect to my netplan server and get the calendar directly. This is the most elegant solution, but I didn't have the time to learn how to do it. (But when I do have time, I should look at the aCal app to see what needs doing...)

Instead I wrote a conversion script. plan2ics is on Launchpad.net, and it takes the netplan file as input and creates an iCal output. I allow the iCal file to be served by a webserver, then tell Google Calendar to import the calendar from an external URL. Once Google has it, the calendar gets synced down to my Android phone.
Oh, and I can also use the calendar URL for email clients like Evolution or Thunderbird.

So it works, but are there any problems?

Well yes, thanks for asking.

First, it is a READ-ONLY solution. So no updating the entries on the web or phone and expecting them to appear in netplan - for this I need to make my own client (see above).

Second, I'm not handling the UID for events properly. At the moment I have a fudge that will break when events are moved around, then I get duplicates in the calendar. To fix this I need to borrow a trick from some old tools to sync netplan with Palm Pilots, and that is to write the UID back into the netplan file as a comment, then reuse the UID in the next export.