As Python is a dynamic language it offers many possibilities how to manipulate object instances during runtime. We may for instance inject methods from another class, achieving similar results as if this instance has this calls as a mix-in super class. This approach could be useful to hack some existing libraries with extra functionality, in case we have to work with created instances.
This function will inject methods from mixin_class into instance assuring that method is properly bound to the instance.
import types
def mixin(instance, mixin_class):
"""Dynamic mixin of methods into instance - works only for new style classes"""
for name in mixin_class.__dict__:
if name.startswith('__') and name.endswith('__') \
or not type(mixin_class.__dict__[name])==types.FunctionType:
continue
instance.__dict__[name]=mixin_class.__dict__[name].__get__(instance)
Django framework provides integrated tests runner, which can be started by ./manage/py test (see docs for more details, key advantage of this runner is that it’ll create new empty database for tests, so they do not interfere with each other or your development instance). This tests runner runs unit tests from tests.py or models.py modules in active projects. However in larger projects we would like to have other organization of tests code – have for instance special test package within project that contains many testing modules, each focused on particular aspect of the application. Continue reading Django tests auto-discover→
Some types of web applications require to start long running tasks – like import of file, compilation etc., but user still needs to have real time updates about progress of the task, eventually some error messages, warnings from the task (cannot import particular line, compilation error). There are existing robust solutions like Celery, but it is aways fun to reinvent the wheel 🙂 In this case we focus on simple solution, without need for request broker in middle, which enables immediate/ real time updates on running tasks to client browser.
Recently I’ve updated TheTool to support also AppIndicator3 interface so it can show icon in Unity panel. Although the documentation is not saying it, you can use absolute path to PNG image as icon-name ( apart of stock icon name). I was trying to add application specific stock icons, but is somehow did not work well (only possibility is to use xdg-icon-resource install --novendor --size 32 picture.png icon-name, but python code for creating application specific icons with Gtk.IconFactory was not working for me – see this post on stackoverflow). Continue reading AppIndicator3 – how to use custom icons→
GSetttings is the standard way how Gnome 3 applications store their configuration. GSettings is the front-end interface for application, actual values are stored by back-end – standard one is called dconf. We can then use the tool dconf-editor to easily browse all stored configurations for all applications. Thanks to GObject introspection we can also work easily with GSettings from python. Continue reading GSettings – Flexible Configuration System→
TheTool v.02 is available – this small handy tools now enables networks detection and setting proxy according to which network computer (e.g. notebook) is just connected. Plus has now very flexible system for defining other actions. More on it’s own page.
Recently I’ve been converting a batch of ebooks into epub and mobi formats. I used python tools, parts of my project MyBookShelf, which uses calibre and LibreOffice for all the hard work. The conversion tool enables to run several conversion in parallel – in separate processes. I wondered how the conversion will speed up with adding more processes. I ran it on my notebook with core i5 processor – two physical cores, each core can run two physical threads, 8GB memory. Graph below shows results for conversion of about 10 books into both formats.
Interesting thing for me is that only notable speed up is between 1 and 2 processes. Not very much gain with running 3 or 4 – looks like full utilization of HW threads is held back by I/O or memory speed limits?
Many Latin alphabets (like my native Czech) contain characters with diacritical marks (or can be called accent marks). For some application in computers (like searching, cross systems compatible file names etc.) we would like to remove diacritics and translate to string containing just ASCII characters. Common approach for this is to use UNICODE character decomposition.
It utilizes fact, that unicode has two ways how to represent characters with diacritics – for instance character á (LATIN SMALL LETTER A WITH ACUTE) normaly has code 225, but this character can be decomposed into two unicode characters code 97 (LATIN SMALL LETTER A) and character code 769 (COMBINING ACUTE ACCENT). This process will work for majority of common ‘special’ Latin characters, however there are still few left, for which unicode does not have decomposition defined – these include characters like ø (LATIN SMALL LETTER O WITH STROKE) used in Norwegian language or ł (LATIN SMALL LETTER L WITH STROKE) used in Polish language. A special handling is needed for these characters – basically a transcription table to map these into some basic Latin characters (it could be 1 to many mapping – for instance æ (LATIN SMALL LETTER AE) should map to ‘ae’).
Characters decomposition is defined in unicode standard and all common computer languages contains libraries which contain unicode definitions and can decompose characters. Below Ishow how this can be done in python. Continue reading Removing Diacritics Marks from Strings→
Visual Python or vpython is python library for simple 3D animation, especially useful for animation of simple physical scenes – like pendulum, ball on a spring, movement in gravitational field etc. It is fairly simple to use and gives quite nice animations, which can demonstrate some laws of physics. I actually spot this package, when I saw an article about physics in popular mobile game “Angry Birds – Space”. In that article they had been arguing about physics laws in the game and also referred to vpython, where they made some experiments. I told myself I have to try it myself, so I’ve created small program using vpython. Continue reading Having 3D fun with Visual Python→