Often it is necessary to develop applications for the corporate system that should have functioned yesterday, without requiring strict compliance with corporate standards. Such applications can submit cms to sites, gui for services under * nix systems, simply by the application of the accounting system. Developing applications of this kind in scripting languages
has a theme! usually optimal in terms of speed of execution. Under the cat, an example of implementing an application in python + PyQt4, the functions of the application parsing and loading data from xls files ...
In order to
cook scrambled eggs I used:
- Python2.5
- PyQt-4.4.3
- kinterbasdb3 (DB KIS - Firebird)
- xlrd-0.6
As an editor, I recommend to everyone emacs python-mode (this also applies to
wind users):

1. We take the dishes out of the cabinet (storage settings)
In general, for scripting languages ​​this question is not worth it, as you can get a settings file like settings.py and quietly run it at the code level:
# -*- coding: cp1251 -*-
host='localhost/3051:intur'
user='SYSDBA'
path='d:\\dlogs\\scanner\\xls'
oprts = (
u'',
u'',
u'',
u'',
u'',
)
Getting the variables in the program looks like this:
mod = __import__(modName)
host = getattr(mod, 'host', 'horn:intur')
user = getattr(mod, 'user', 'SYSDBA')
2. Drawing of forms
Qt Software is generally handsome in terms of documentation, or if you compare tools for drawing GUIs, here they are also great. I drew the main window of the program in QtDesigner + marked the signals from the widgets that need to be processed.

In order to quickly assemble the forms in the designer, you certainly need to gain a little understanding of the mechanisms of formation of Layout and Spacers. There are two options for raising the window in the application, or you generate the file for the programming language you need, or use the Qt special modules to build classes from the description xml files. I usually generate a file, I can not say that this is something better, it just seems more convenient to me. For python, PyQt supplies pyuic.bat which you just need to copy to your project directory and generate window handler classes as you work in the designer.
I have it for the main window looks like this:
')
pyuic.bat -o mwnd.py mwnd.ui
Unlike wx Qt, it is not the heirs of the main window classes that generate, but the modifier classes that take the base class of the window as an argument and “hang” your controls on it. When generating on c ++, Qt uses multiple inheritance the class of your application becomes the heir of the base window class and the modifier class. It is interesting that when generating files, pyuic slots are hung on the base class, hence the discrepancy - signal handlers are in the heir of the base class of the window, and references to controls of the same window are found in the modifier class of the generated pyuic.
def _initApp(self): self.mwnd, self.mwnd.ui = MainWnd(), Uwnd() self.mwnd.ui.setupUi(self.mwnd)
I acted simply: I connected the class-modifier with the heir of the base class of the window, it turned out just when I needed to access the controls, I communicate to them as - self.ui. "name". It seems to be an obvious thing, but how often it happens it occurred to me not the first time.
! It is also interesting that python does not digest keywords in the names of attributes and methods of classes, so you can not create the self.pass variable or self.exec (). Hence the funny inscription in every PyQt application sys.exit (app.exec_ ())
3. Put the pan on the stove, or launch the application
Here is the main.py code - its tasks start, load the settings file, request a password and, in case of a successful connection, launch the main application window
main.py4. Do not forget to put eggs in the fried eggs.
The heir to the main window of the application (MainWnd) performs the functions of tracking user behavior; here, by the way, the slots that we generated in the designer are visible, here the main task of the code is the most transparent and simple window control functions for the main class of the application loading the file to the database.
The class that performs the main function of the application - parsing and loading data (MainModel) does not make sense without the main window, so we will make it an attribute of the window. To parse xls files in python, there is a trivial one to use, and, therefore, the wonderful xlrd library works with the file directly without lifting up bulky OLE machines, and therefore is also faster. The application allows you to download only modified files, so the part when the MainModel, is taken away to compare the modification dates and the last download that is stored in the database. So, apart from the main working class of the application, no one communicates with the database by giving it a small class (dbConn), which stores the MainModel instructions in "terms" sql.
WndModel.pyIn general, according to science, there is a certain clumsiness of the program due to pulling variables (for example, app) from the Container class to the classes of the 2nd hierarchy level. In large programs, there are a little more than
9000 such variables, and this problem is solved with the help of a singleton (next time I will consider this).
As a result, we received an application template that you can use for your own purposes, throw away WndModel.py, write your successor to the base window, your class implementing your tasks - and everything seems to be now even working.
PS Full source code
hereUPD. delivered the code to an external codeguard, if someone tells me how to properly highlight the python code on Habré I will be grateful.