Considering migrating to Python from Visual Basic 6 for engineering applications

Larry Hudson orgnut at yahoo.com
Sun Feb 21 02:28:05 EST 2016


On 02/20/2016 10:38 AM, wrong.address.1 at gmail.com wrote:
[snip]
> How complicated could this get in Python? Reading the numbers is one thing, and then placing the values in text boxes of the GUI.

If that is the only object of using these values, there is no conversions necessary.  The data 
is read as text (strings), and you could simply write them directly into the text boxes.  Of 
course, this is unlikely -- you surely want the actual numeric values for other purposes.  As 
already pointed out, if you know in advance what the data types are, the necessary conversions 
are trivial.  Just slightly more complex if you don't know the types in advance.

OTOH, the conversion from int or float back to strings is also trivial.  It's as simple as 
writing str(n), where n is an int OR a float -- it works automatically with either.  (Data 
typing in Python is dynamic, you generally don't need to specify the types.)  But if you want 
the strings to be in a specific format, this is also possible with a different syntax that lets 
you specify the output format.  As an example, assume the variable val is 1.97834, and the 
formatting string is "${:.2f}".format(val) -- this will give you the string '$1.98'.  This 
expression breaks down to:
     $   ->  a literal dollar sign
     {}  ->  a placeholder, it is where the formatted data will be put
     :   ->  what follows is formatting code
     .2  ->  round to, and print, two decimal places
     f   ->  the data is a float
     .format(val)  ->  the data to format

BTW, this leaves the variable val unchanged -- it is NOT rounded, it still holds its original 
precision.  It only affects how it is displayed.  You CAN round it if you want, but that's an 
entirely different function.

Naturally, learning all the formatting codes takes some effort, but it allows defining the 
appearance of the resulting strings in a very detailed and complete manner.  [Aside:  this is 
the "new" formatting method.  Python also supports an "old" method, which is very much like the 
way strings are formatted in the C language.]

> Or can I write my own reading subroutines which can then be called like
> ReadVBstyle 8, ND, NIN, NT
> to make the code more readable?

ABSOLUTELY!!  Most Python programming consists of defining the functions and classes needed, 
which definitely makes Python more readable.  (Classes imply Object Oriented Programming. 
Python _allows_ OOP but does not _require_ it -- and is irrelevant here.  Ignore anything I say 
about classes and OOP!)  For your example here, it wouldn't _match_ the BASIC syntax, but would 
be used in an equivalent way.

In fact, if you find yourself writing functions (or classes) that could be general-purpose 
routines, it is a trivial matter to put them into a personal library which you can then use in 
future programs.  You don't need to rewrite them, just use them.

Now, in practice, it takes a little care to write them as library functions.  That is, the 
original version may rely on some details of the original program, so the library version will 
need to be tweaked a bit to remove these 'local' dependencies.  But this is generally easily 
handled through the parameters to the functions.  Also they are probably written with a bit more 
care to handle error conditions (which Python calls exceptions, Python has very extensive 
exception handling).  This capability (a personal library) is enormously convenient.  While I am 
saying 'personal', I really mean library(s) available to everyone involved in a programming 
project.  No need for anyone to re-invent the wheel!  ;-)    Python calls them modules rather 
than libraries, but it's the same thing.

      -=- Larry -=-




More information about the Python-list mailing list