Archive

Archive for January, 2013

Fingerprint of the operating system/platform

January 30, 2013 Leave a comment

Problem
I have a script that I want to run in different environments: on Linux, on Windows, on my home machine, at my workplace, in virtualbox, etc. In each environment I want to use different configurations. For instance the temp. directory on Linux would be /tmp, on Windows c:\temp, etc. When the script starts, I want to test the environment and load the corresponding configuration settings. How to get an OS-independent fingerprint of the environment?

Solution
I came up with the following solution. For my purposes the short fingerprint is enough.

import platform as p
import uuid
import hashlib

def get_fingerprint(md5=False):
    """
    Fingerprint of the current operating system/platform.
    If md5 is True, a digital fingerprint is returned.
    """
    sb = []
    sb.append(p.node())
    sb.append(p.architecture()[0])
    sb.append(p.architecture()[1])
    sb.append(p.machine())
    sb.append(p.processor())
    sb.append(p.system())
    sb.append(str(uuid.getnode())) # MAC address
    text = '#'.join(sb)
    if md5:
        md5 = hashlib.md5()
        md5.update(text)
        return md5.hexdigest()
    else:
        return text

def get_short_fingerprint(length=6):
    """
    A short digital fingerprint of the current operating system/platform.
    Length should be at least 6 characters.
    """
    assert 6 <= length <= 32
    #
    return get_fingerprint(md5=True)[-length:]

The up-to-date version of the source is available here as part of my jabbapylib library.

Advertisements

100,000 hits

January 23, 2013 2 comments

Whoa! Today the blog had its 100,000th visit. Thanks!

100000

And it took 29 months only :)

Categories: python Tags: , ,

Python’s GIL (Global Interpreter Lock)

January 16, 2013 Leave a comment
Categories: python Tags:

Pycoder’s Jobs

January 11, 2013 Leave a comment
Categories: python Tags:

Launch just one instance of a Qt application

January 10, 2013 Leave a comment

Problem
I have a Qt application that I only want to run in one instance. If I forget that it’s launched and I start it again, I want the system to put the focus on the already running instance.

Solution
For more details, see this post where the used Unix commands are explained.

#!/usr/bin/env python

"""
Launch just one instance of an application.

If the application is running, the focus is put on its window.

Requires the xdotool package (sudo apt-get install xdotool).
Tested under Ubuntu GNU/Linux 12.10.

Author: Laszlo Szathmary (jabba.laci@gmail.com), 2013
"""

__author__ = 'Jabba Laci'
__appname__ = "The One and Only"
__version__ = "0.1"

import os
import sys
from PySide.QtGui import *
from PySide.QtCore import *
from subprocess import Popen, PIPE, STDOUT
import shlex


def get_simple_cmd_output(cmd, stderr=STDOUT):
    """Execute a simple external command and get its output.

    The command contains no pipes. Error messages are
    redirected to the standard output by default.
    """
    args = shlex.split(cmd)
    return Popen(args, stdout=PIPE, stderr=stderr).communicate()[0]


class OneDialog(QDialog):
    def __init__(self, parent=None):
        super(OneDialog, self).__init__(parent)
        self.setWindowTitle("{} {}".format(__appname__, __version__))
        self.resize(300,150)

####################

if __name__ == "__main__":
    wid = get_simple_cmd_output('xdotool search --name "{n}"'.format(n=__appname__)).strip()
    if wid:
        os.system('xdotool windowactivate {}'.format(wid))
    else:
        app = QApplication(sys.argv)
        form = OneDialog()
        form.show()
        app.exec_()

If you have a better solution, let me know.

Categories: python, Qt, ubuntu

Qt development under Windows with Python using PySide

January 9, 2013 Leave a comment

Install PySide here: http://qt-project.org/wiki/PySideDownloads. Follow the link “Microsoft Windows” and download the installer.

Categories: python, windows Tags: , ,

IPython for Windows

January 9, 2013 Leave a comment

Installation instructions: http://ipython.org/ipython-doc/stable/install/install.html#windows.

The important thing is to install IPython as administrator. This way Start menu shortcuts will be created and you can launch IPython easily. The IPython shortcut calls ‘C:\Python27\python.exe “C:\Python27\scripts\ipython-script.py”‘ in my case and I don’t want to type all that each time I want to launch IPython :)

Categories: python, windows Tags: