Archive
Fingerprint of the operating system/platform
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.
Pycoder’s Jobs
Launch just one instance of a Qt application
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 ([email protected]), 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.
Qt development under Windows with Python using PySide
Install PySide here: http://qt-project.org/wiki/PySideDownloads. Follow the link “Microsoft Windows” and download the installer.
IPython for Windows
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 :)
requests-transition
With the package requests-transition you can use requests 0.x and the shiny new requests 1.x too. Requests 1.x changed some things, so if you don’t want to update your code, requests-transition can be useful.
After installation, you can simply select which version of requests to use:
-
import requests0 as requests
(0.x) -
import requests1 as requests
(1.x)
rlwrap: adding keyboard editing functionalities to your scripts
Problem
You have an interactive script that waits for some user input, similarly to the (Python) shell. In bash, you can use the arrow keys for editing / browsing, but it’s not available in a Python script right away. If you read the user input with raw_input() for instance, you cannot move the cursor back with the left arrow, or you cannot browse previous commands with the up arrow.
And still… How to add such bash-like functionalities to your script? The method must be absolutely painless, of course.
Solution
Use the command “rlwrap“, which magically adds these functionalities to any script. Awesome. So instead of launching your script with “./something.py“, launch it like this:
$ rlwrap something.py
You might have to install rlwrap with “sudo apt-get install rlwrap“. More info about rlwrap: here.
The webbrowser module uses xdg-open
With the webbrowser module you can open webpages in your browser. But how does it work?
Well, under Linux it uses xdg-open. I figured it out accidentally. Start the python shell and try this:
>>> import webbrowser
>>> webbrowser.open_new_tab('')
You will get the usage message of xdg-open :)


You must be logged in to post a comment.