Difference between revisions of "Russ Python Tips and Techniques"

From OpenCircuits
Jump to navigation Jump to search
Line 39: Line 39:
== Use Threading  ==
== Use Threading  ==
= Calling across Threads =

Revision as of 08:58, 1 February 2017


Just starting Feb 2017 check history to see if progress is being made. Most of this material relates to applications with a GUI.

Parameters for Applications

There are various ways of storing start up parameters for applications. In the old days almost all windows programs had a "ini" file. Linux had/has "config" files. In both cases these are text files that are easily edited with a simple text editor. More recently xml files ( which i tend to hate ) have come into vogue. Finally some applications give you a gui for editing configuration values and then save them any way they want, perhaps in the "registry".

Using a gui is the easiest way for the user in particular the naive user. They are also a lot of work to implement and maintain. So I favor a flat, non-xml file. But what file? For Python my answere is a python.py file that has pretty much one class, Parameters, that is implemented as a singleton ( in what ever way you want to implement singletons ). These are easy to implement, easy to maintain, and very flexible. They can also do clever things like detecting what operating system they are running on and make automatic adjustments. There are no issues of converting type into strings for the file. Comments are easily included. Loading can print the progress of initialization.

I will put a brief sample here soon so you can see what a file looks like.

code comming here


I use the logging class that is part of standard python. I provide a button to view and edit the file. Why invent your own method. Here is some code:

code comming


When an application ( here I am thinking about GUI applications ) starts there can be a considerable delay as all the modules are imported. Typically a restart is done after an exception or when parameters are changed. So I include a restart button on the GUI to quickly restart the application. In my case applications are usually started by constructing some application class. So what I do is break up the __init__ method into 2 parts, only the second needs to be run to restart the application. Here is some sample code:

code coming

Threading with Tkinker

Tkinker wants to own the processing of the application. If you make a button start a long running operation then the GUI blocks and becomes unresponsive. So you need some sort of threading. Here are some tips.

Polling withing Tkinker

Use Threading

Calling across Threads