Python

A mundane task: updating a config file to retain old settings

I want to have a hand in creating an excellent personal information manager (PIM) that can be a worthy successor to Ecco Pro. So far, running EccoExt (a clever and expansive hack of Ecco Pro) has been a eminently practical solution.   You can download the most recent version of this actively developed extension from the files section of the ecco_pro Yahoo! group.   I would do so regularly but one of the painful problems with unpacking (using unrar) the new files is that there wasn't an updater that would retain the configuration options of the existing setup.  So a mundane but happy-making programming task of this afternoon was to write a Python script to do exact that function, making use of the builtin ConfigParser library.
"""
compare eccoext.ini files

My goal is to edit the new file so that any overlapping values take on the current value

"""
current_file_path = "/private/tmp/14868/C/Program Files/ECCO/eccoext.ini"
new_file_path = "/private/tmp/14868/C/utils/eccoext.ini"
updated_file = "/private/tmp/14868/C/utils/updated_eccoext.ini"

# extract the key value pairs in both files to compare  the two

# http://docs.python.org/library/configparser.html
import ConfigParser

def extract_values(fname):
    # generate a parsed configuration object, set of (section, options)
    config = ConfigParser.SafeConfigParser()
    options_set = set()

    config.read(fname)
    sections = config.sections()
    for section in sections:
        options = config.options(section)
        for option in options:
            #value = config.get(section,option)
            options_set.add((section,option))

    return (config, options_set)

# process current file and new file

(current_config, current_options) = extract_values(current_file_path)
(new_config, new_options) = extract_values(new_file_path)

# what are the overlapping options
overlapping_options = current_options & new_options

# figure out which of the overlapping options are the values different

for (section,option) in overlapping_options:
    current_value = current_config.get(section,option)
    new_value = new_config.get(section,option)
    if current_value != new_value:
        print section, option, current_value, new_value
        new_config.set(section,option,current_value)

# write the updated config file

with open(updated_file, 'wb') as configfile:
    new_config.write(configfile)

Ecco Pro
Python

Comments (0)

Permalink

Getting started with flickrapi

Based on reading the  Python FlickrAPI, I wrote a simple example using the flickrapi library:

import flickrapi
API_KEY = '[API_KEY]'
API_SECRET = '[API_SECRET]'

if __name__ == '__main__':
    # instantiate the flickr object with the API_KEY, SECRET to return ElementTree entities
    flickr = flickrapi.FlickrAPI(api_key=API_KEY,secret=API_SECRET,format='etree')
    photos = flickr.photos_search(user_id='73509078@N00', per_page='10')

    if photos.attrib['stat']:
        for photo in photos.find('photos').getiterator('photo'):
            id = photo.attrib['id']
            secret = photo.attrib['secret']
            server_id = photo.attrib['server']
            farm_id = photo.attrib['farm']
            user_id = photo.attrib['owner']
            # calculate the thumbnail URL and photo URL
            # http://www.flickr.com/services/api/misc.urls.html
            thumbnail_url = "http://farm%s.static.flickr.com/%s/%s_%s_t.jpg" \
                            % (farm_id, server_id, id, secret)
            photo_url = "http://www.flickr.com/photos/%s/%s"  % (user_id, id)
            print thumbnail_url, photo_url

Flickr
Python

Comments (0)

Permalink

Django hosting options?

Although it seems that you can host Django on dreamhost (Django – DreamHost and JeffCroft.com: Setting up Django on Dreamhost), I might be looking for another host for Django, such as (mt) Media Temple, which is what is used by Django creator Adrian Holovaty for his EveryBlock project.

Python
web hosting

Comments (0)

Permalink

SWIG

Although I plan to be programming primarily  in Python (and JavaScript)  this next year, I don't want to forget about C/C++.  I hope to have an opportunity to use SWIG at some point to connect high-speed code in C/C++ with Python code:

SWIG is an interface compiler that connects programs written in C and C++ with scripting languages such as Perl, Python, Ruby, and Tcl. It works by taking the declarations found in C/C++ header files and using them to generate the wrapper code that scripting languages need to access the underlying C/C++ code. In addition, SWIG provides a variety of customization features that let you tailor the wrapping process to suit your application.

Python

Comments (0)

Permalink

Time to study Python 3.0?

I had been vaguely following Python 3 when Python 3.0 final was released on December 3, 2008. I decide to start coming to terms with what this new and intentionally non-backwards compatible version of Python means by printing out What’s New In Python 3.0 — Python v3.0 documentation and Linux.com :: Python 3.0 makes a big break. My current conclusion from reading the articles: I should learn Python 3.0 in the background but make mastery of Python 2.x the priority for at least the next several years.

Python

Comments (0)

Permalink

listing applications installed in Windows XP with Python

The end of 2008 is approaching, making it an appropriate time to take inventory of my life — which also includes a lot of  my computer-related stuff.   In addition to cleaning up my office, I'm also cleaning up my Lenovo Thinkpad T60p, my primary work computer, whose drive has been close to its 100 GB limit for some time now.  To help me spot the disk hogs, I've been running WinDirStat – Windows Directory Statistics.

I'm also looking through all the programs I've installed over the years, lot of which I've not used and should be uninstalled to save space!  Although I've been using Windows XP's Control Panel's Add/Remove Program, I would also like to have some automated way to inventory my system — hence I started looking for Python code to do so.

I found the following code in List of all installed applications (XP)?:

import win32api, win32con, pywintypes

appKey = win32api.RegOpenKeyEx(win32con.HKEY_LOCAL_MACHINE, \
           'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths', 0, win32con.KEY_READ)
sklist = win32api.RegEnumKeyEx(appKey)

for skey in sklist:
     print skey[0]

     try:
          wPath = win32api.RegQueryValue(appKey, skey[0])
          print '    ' + wPath
     except pywintypes.error,details:
          print '### Error [pywintypes.error]: ' + details[2]

win32api.RegCloseKey(appKey)

The code seems to work. The response to query for a better technique pointed to Tim Golden's Python Stuff: wmi. WMI looks cool and worthwhile following up with, especially because of what seems to be excellent documentation in the form of wmi Tutorial and wmi Cookbook.

Python

Comments (0)

Permalink