Archive for QuickNote

[QN] How to convert month name to month number in Python

For a short script I was writing I needed to convert a string representation of the month (“month name”) into a numerical value (“month number”). A quick google led to many solutions to the inverse problem, which to me seems a lot easier. Here is a quick and dirty solution that uses the calendar module.

import calendar
list(calendar.month_name).index(month_name)

I chose to use the calendar module array month_name because I would rather not have to create a list of month names myself.

Comments (1)

Allow ssh access to a Fedora 10 machine (VM)

I have been setting up several Virtual Machine’s lately , to test deployment in various linux environments. I must say I have been frustrated with the way linux handles shared libraries. The DLL hell that many people complain about on Windows machines is nothing compared to the interdependencies of shared libraries! (Statically linking everything was not an option, as I was unable to successfully statically compile Python + wxWidgets + wxPython + Numpy)

Once a Fedora 10 VM has been set up, I need to do the following:

  • system-config-firewall   Turn off the firewall
  • chkconfig –list sshd   Confirm whether sshd is running or not
  • Assuming it isnt running in modes 3 & 5,
    • chkconfig sshd on
    • service sshd start

Comments off

QN: Python: Looping through sequence and indices simultaneously

I started programming in python from the days of 1.5.2, and have not kept up to date with some of the new features. Two features that caught me by surprise:

enumerate()  # builtin function
"...".format() # String method

enumerate() takes a list an returns an interator tuple (index,value) as it goes along the sequence. Very handy and obviates the need for the very messy:

for i in range(len(L)):
    print "I need the index {0} and the item {1} at the same time".format(i,L[i])

This illustrates the second new feature I just realized, the .format() method. This apparently replaces the ‘%’ operator and allows formatted strings to include positional replacement strings using {idx} notation.

Comments off