[pythonvis] How I program in Python

  • From: "Jeffrey Thompson" <jthomp@xxxxxxxxxxx>
  • To: <pythonvis@xxxxxxxxxxxxx>
  • Date: Sat, 10 May 2014 00:10:43 -0400

 

Hi (for beginners)

 

                                I did not have the ideas and

experience of years programming Python as

is the case for a number of posters and responders who have been making
posts to the list.

I am slightly jealous that Idid not have this kind of support

and instruction that you beginners have here and now.

I sympathize that some of these posts are sometimes hard to understand.

 

Here I describe my program development process.

There may be better ways to do it,

 

but I was able to make it work for me.

                1)In your text editor for programming,

you would have opened the code file that you are working on.

 

The way I work is to have a separate program from the text editor.

That is the command line window.

On the Macs it is called the Terminal program.

The purpose of these applications is to

run the code and see if it works or not.

The command line program that provides utilities originally created by
Microsoft, before Windows was created.

All the commands in the command line are launched by typing the command that
you want to perform.

There are some things that you can do in the command line that are harder to
do than in Windows.

It is rather primitive, but it gets the job done in our first attempts to
test our Python programs.

 

Ideally the program you have written works perfectly and no debugging is
required.Realistically, your program will have some errors

If there any errors, the programmer must fix them.

Often the command line commands are rather repetitive, since you are usually
repeatedly changing your open file in your text editor.

To help me save time, I created for the command line a Batch file.

The idea is that a batch file can do a number of thingsin their proper
order.

This can save a lot of time which would otherwise be wasted doing 

unnecessary typing and even more if the programmer makes typing mistakes

The batch file has commands that the command line can perform.

So a batch file is like a program.

But the lines in a batch file  only do the actions that the command line can


perform.

However, a batch file can have some control structures which cannot be run
from the command line, but rather only in a batch file.

 

First we see a batch file that is usually performed at the beginning of a
coding session.

Here is a batch file which will change the directory (folder) we are going
to operate in.

The folder is chosen to that which contains the program to test and any test
data files which the program might need to run certain tests.

 

initUseful.bat

cd "C:\Users\Jeffrey\Apperian\usefulWorkingFolder"

 

CD is shorthand for change directory.

A directory is the same as a folder.

When looking for a file to launch, edit, etc.,

The computer system will start looking first in the currentfolder,

The path to the files we are going to work withwill start with the volume,
which in this case is C:

Inside the C: directory is the folder/directory "Users".

That means C: has the folder Users.

Each successive folder in the list is the name of a folder previous in the
line of  "nested" directories.

In other words volume C contains the folder "Users".

and folder "users" contains the folder "Jeffrey", etc. 

A listing of directories contained in one another is called a path.

So in this case we are telling the computer to start looking for a file in
the path folder at the end of the path listing.

One should have the program in that directory,

as well as any other files you need to work with.

Should you have a file shared by several programmers, then 

you might not want to have a copy of the file

because the file may be updated while you are testing your program.

If you occasionly access a file in a shared directory,

that you may not make a copy for your working folder

then you might choose to use the full path name for the shared folder,

instead of adding the occasionly accessed folder to the path list,

which is long to begin with.

The longer you make the path list, the more work the computer must do before
finding your file or calling it quits

This file would be run once at the beginning of a programming session.

Here is another short batch file that was used :

python.exe calcGretchensPensionValues.py >
"C:\Users\Jeffrey\Desktop\Apperian related stuff 5-7-12
folder\statsreportOutput.txt" 2>&1

type beep.tx

 

So I will explain how this short batch file works:

The first line starts by launching the Python.exe file. The path to this
program is already on the path list, so the command control can find it just
by looking through all the folders in the paths list.

until it finds the desired file.If the same file is located in two different
folders, the first one encountered is used.

The second word is the path to our program that we are running or testing.

If it is in the current folder, which is the one we set in our first
command: cd ...

So the computer finds it rather quickly.

 

after that word there is the symbol ">".

In a batch file, the ">" symbol means that the output of the program will
will be sent to the word to the right of the ">" symbol.

As you can hear, it is a ".txt" file.Python programs can produce many
different file types: whatever the programmer wants.

You can think as an arrowhead pointing to the output file.

At the end of the line is the characters: 

"2>&1"."1" stands for the general output  from the program.

"2" stands for the error messages that were generated, if there were any.

This word puts both of these into one file, , the output file.

 

When you merge the 2 files the output is combined with the error messages
fileas the program produces them.

In other words you might have some normal output followed by the line where
the error was detected.

Below that will be the error message.

This can turn out to be useful,

because the mistake could be in the erroroneous line or the previous line.

If you have print statements around the line with the error, that can be
useful information about what is causing the error,

so you can fix it.

 

Later on we might learn how to use a debugger.

A debugger is a fancy program that you can use to more quickly find where an
error is occurring and get some relevant information about what the program
is doing wrong or right.

 

                The debugger is a somewhat advanced topic, but is very
helpful in fixing some problems.

 

The next line in the batch file : type beep.txt"

The command "type" is used to write out the contents of a file.

It is smart enough to produce sounds if the file contains sounds.

In this case I searched for the ascii character "beep", which I then mad two
successive beeps in a row. Then I saved and closed beep.txt.

So when the batch file gets to that line it prints (sounds) the two beeps.

If you have a small quick-running program these will not help much.

However on programs that last more than a couple of minutes will give you
some time to grab something to eat or do some other work while you are
waiting for the program to complete.

 

This procedure is close to what you may have read previously in this list.

But they have run the program in a window of the editor program.

The difference between is small,

but the version that works within the text editor has to switch from the
source code window to the shell.bat window and then to the output window.

The way I have found in Edsharp text editor is to press F4 to get a small
window with a list of open files in the text editor.

You then press enter on the one you want to switch to.

My solution requires one to use alt-tab to go from the command line program
running outside the text editor.

This requires the programmer to switch back and forth between the text
editor and re-running the presumably fixed program.

 

But Windows is a little smart:

if you switch from program1 to program 2 and back, then Windows puts the 2
programs together in the list.

In other words once you have plowed through other open programs to get to
one you want,

then Windows puts both of these together in the list so one only has to type
alt-tab once.

If you are doing coding for a whilethis makes the routine seem to go faster.

 

I tried using the run command from the text editor but it could get it to
work for me.

That was a while ago so maybe they have fixed it or the knowledgable
programmers have figured out how to do it right.

 

                Jet (Jeffrey Thompson)

Other related posts:

  • » [pythonvis] How I program in Python - Jeffrey Thompson