# [FAQTS] Python Knowledge Base Update -- May 25th, 2000

Fiona Czuczman fiona at sitegnome.com
Thu May 25 11:22:50 EDT 2000

```Hi All:

Yet another group of entries entered into http://python.faqts.com

cheers,

Fiona Czuczman

## New Entries #################################################

-------------------------------------------------------------
Searching a comparision between the Python and Visual Basic languages? Mathematical comparison specifically.
http://www.faqts.com/knowledge-base/view.phtml/aid/3243
-------------------------------------------------------------
Fiona Czuczman
Janko Hauser, Erik Wilsher, Travis Oliphant

Look into the topic guide at http://www.python.org/topics/scicomp/
There you will find links to many tools for number crunching and I'm
sure that NumPy can withstand a comparison against Visual Basic,
although I would also be interested what is lacking.

You can also have a look at:

http://www.python.org/doc/Comparisons.html    (general)
http://lheawww.gsfc.nasa.gov/users/barrett/IDAE/table.1.html
(specific).

-------

I have been programming in both (although I stopped with VB after I
discovered Python:-), so here is a short summary.

If we look at the bare facts we have:

Py         VB
Int*2          N          Y
Int*4          Y[1]       Y
Long[2]        Y          N
Real*4         N          Y
Real*8         Y          Y
Currency       N          Y

Array arith.   Y[3]       N[4]

Callbacks      Y[5]       Y
Modules        Y          Well, sort of
Dynamic        YES        N
Interactive    Y          Not very good

complex (not in VB) and exceptions (poor in VB)

1: Can be Int*8 on 64 bit machines
2: Long is abitary precision integer values
3: Through NumPy
4: Not that I know of
5: From within python only

[Insert : Through NumPy you have Int*2, and Real*4.]

Looking at the above, it can appear that the two languages are
1) You probably don't need real*4, real*8 is just as fast
2) You don't need int*2
3) VB makes some things extremly tedious. Compare

py>>> FiltConst = [1., 2., 1.5, 3.2, 6.2, 4.3]

VB:   Dim FiltConst(6) as Double
FiltConst(1) = 1.
FiltConst(2) = 2.
etc....

4) The prototyping offered through an interactive environment
(=Python) speeds up developement quite a lot.  The fact that you can
include test code in your modules for easy testing lets you develop
your application in a modular fashion and test each part
before "assembly".

5) The speed issue is difficult:
a) If you run lots of matrix math you can use NumPy, and it is
*fast*.  There is no alternative to NumPy in VB (to my knowledge)

[Insert:  You can use NumPy for lots of things besides just "matrix"
math.  You can store large amounts of data in a small space and do rapid
operations on that data as well.  As a result, image processing can be
done very well using the NumPy object.]

b) If you on the other hand run lots of "itty nitty" numeric
calculations VB is approx 15x faster than Python (highly informal
test).

The question you need to ask yourselves on speed is "how fast is fast
enough".  At present Python is (unfortunatly) not fast enough for our
line of work (real time calculation of large ODE's), but the speed is
sufficent for most other tasks.  If you find that Python is fast
enough (execution speed), it offers great advantages in development
speed!!

Have you tried multipack?  It contains a module that links the
FORTRAN integration routines in ODEPACK.  It is rather fast, but because
it calls a Python function at each step it is not "as fast as machinely
possible."  Still, a combination of Python/(C,FORTRAN) is very fast.

-------------------------------------------------------------
How do I create a binary file and write to it?
http://www.faqts.com/knowledge-base/view.phtml/aid/3237
-------------------------------------------------------------
Fiona Czuczman
Travis Oliphant

The idea is to write a string to the file where the string is just a
collection of bytes representing the data.  There are lot's of ways to
proceed here.

(1) Use the struct module to create a string with the machine
representation of the integer in question and write that out:

import struct
n = 1066
bfile.write(struct.pack('i',n))

(2) Use the *default* array module to store the numbers and then use
it's tofile() method or its tostring() method

import array
numbers = array.array('i',[1066,1035,1032,1023])
bfile.write(numbers.tostring())

## OR

numbers.tofile(bfile)

(3) Use Numerical Python's array module to store the numbers and then
use the tostring() method of the NumPy array.

import Numeric
numbers = Numeric.array([1,2,3,4,5,6],'i')
bfile.write(numbers.tostring())

(4) Use Numerical Python and mIO.py which is part of signaltools
(http://oliphant.netpedia.net).  This will let you write a binary file
directly from a NumPy array (without the intervening copy-to-string
which can save a lot of memory if you have *a lot* of numbers).

import mIO, Numeric

bfile = mIO.fopen('somefile','w')
numbers = Numeric.array([1,2,3,4,5,6],'i')
bfile.fwrite(numbers)
bfile.close()

# You could also say for example
# bfile.fwrite(numbers,'float')  to write them as floats

-------------------------------------------------------------
How do I get IDLE to run a script on startup?
http://www.faqts.com/knowledge-base/view.phtml/aid/3241
-------------------------------------------------------------
Fiona Czuczman
Shae Erisson

Problem:

I've created a script called \$IDLESTARTUP.py in the IDLE directory, and
I'm running idle.pyw with the -s switch, but it doesn't appear to be
working.  (Running IDLE 0.5 on Win98.)

Solution:

\$IDLESTARTUP actually means "read the value of the environment variable
IDLESTARTUP"
That means if you add a line in your autoexec.bat that says:
SET IDLESTARTUP="C:\Program Files\Python\Tools\idle\autoidle.py"

Then the -s switch will work (as long as you have a file name
autoidle.py in the correct path that's a python file that is :)

You can of course change the value of that variable to start with any
file you prefer. For shell variables, you don't need to surround them
with double quotes if there aren't any spaces in the path you're giving
it.
ex:
SET IDLESTARTUP=C:\Python\Tools\idle\idlesetup.py
that will work fine since there aren't any spaces (like "Program Files")
in the directory specification.

-------------------------------------------------------------
Is there any way on NT that I can use a function such as "flock()"? On unix, it seems "fcntl" module is available.
http://www.faqts.com/knowledge-base/view.phtml/aid/3242
-------------------------------------------------------------
Fiona Czuczman
Lyle Johnson, Mark Hammond, Jeff Kunce

There is a function _locking() which I haven't used, but which sounds
like it does the same thing as flock() under Unix. The prototype is:

_locking(int handle, int mode, long nbytes)

Look it up in the MSDN documentation for more details.

Also, you will find it in the builtin msvcrt module...

--------

I extended the posixfile file locking protocol to work on NT. I never
used it in production, so test it well before using.

See:
ntposixfile.py
at:
http://starship.python.net/crew/jjkunce/python/ntposixfile.py

-------------------------------------------------------------
Is there a simple script somewhere I can look at that sends and retrieves cookies via CGI?
http://www.faqts.com/knowledge-base/view.phtml/aid/3238
-------------------------------------------------------------
Fiona Czuczman
Oleg Broytmann, Michal Wallace

# ----- create -----
#! /usr/local/bin/python -O

import time
t = time.gmtime(time.time())
t = (t[0] + 10,) + t[1:] # add 10 years :)

cookie["id"]["expires"] = time.strftime("%a, %d-%b-%Y %T GMT", t)

try:
print "Content-type: text/html"
print "Location: test_c.py"

except:
#print exception

# ----- test -----
#! /usr/local/bin/python -O

if c:
print c
else:

try:
print "Content-type: text/html"
print

except:
#print exception

If you use weblib (http://weblib.sourceforge.net/ - get the cvs version,
not the snapshot):
-----------------------

import weblib

## show cookies from the browser:

print "<ul>"

print "<li><b>%s</b> - %s</li>", (c, weblib.request.cookie[c])

print "</ul>"