[Tutor] Reading binary files #2

2009-02-09 Thread etrade . griffiths
Hi

following last week's discussion with Bob Gailer about reading unformatted 
FORTRAN files, I have attached an 
example of the file in ASCII format and the equivalent unformatted version.  
Below is some code that works OK until 
it gets to a data item that has no additional associated data, then seems to 
have got 4 bytes ahead of itself.  I 
though I had trapped this but it appears not.  I think the issue is asociated 
with newline characters or the 
unformatted equivalent.

Thanks in advance

Alun Griffiths
==
# Test function to write/read from unformatted files

import sys
import struct

# Read file in one go

in_file = open(test.bin,rb)
data = in_file.read()
in_file.close()

# Initialise

nrec = len(data)
stop = 0
items = []

# Read data until EOF encountered

while stop  nrec:

# extract data structure

start, stop = stop, stop + struct.calcsize('4s8si4s8s')
vals = struct.unpack('4s8si4s8s', data[start:stop])
items.extend(vals)
print stop, vals

# define format of subsequent data

nval = int(vals[2])

if vals[3] == 'INTE':
fmt_string = 'i'
elif vals[3] == 'CHAR':
fmt_string = '8s'
elif vals[3] == 'LOGI':
fmt_string = 'i'
elif vals[3] == 'REAL':
fmt_string = 'f'
elif vals[3] == 'DOUB':
fmt_string = 'd'
elif vals[3] == 'MESS':
fmt_string = '%dd' % nval
else:
print Unknown data type ... exiting
print items
sys.exit(0)

# extract data

for i in range(0,nval):
start, stop = stop, stop + struct.calcsize(fmt_string)
vals = struct.unpack(fmt_string, data[start:stop])
items.extend(vals)

# trailing spaces

if nval  0:
start, stop = stop, stop + struct.calcsize('4s')
vals = struct.unpack('4s', data[start:stop])

# All data read so print items

print items


-
Visit Pipex Business: The homepage for UK Small Businesses

Go to http://www.pipex.co.uk/business-services

 'IFLAGS  '  12 'INTE'
  -1   2   1   10500   3   10500
   2  -1 100   1 105   10500
 'QFLAGS  '  10 'LOGI'
  F  T  F  F  F  T  F  F  F  F
 'NAMES   '   3 'CHAR'
 'ORTHO   ' 'META' 'PARA'
 'DATABEGI'   0 'MESS'
 'TIME'   1 'REAL'
   0.00
 'DISTANCE'  10 'DOUB'
   0.00D+00   1.00D+00   2.00D+00
   3.00D+00   4.00D+00   5.00D+00
   6.00D+00   7.00D+00   8.00D+00
   9.00D+00   
 'CONC'  10 'REAL'
   0.10495859E+02   0.10480556E+02   0.10465680E+02   0.10451221E+02
   0.10437166E+02   0.10423511E+02   0.10410242E+02   0.10397354E+02
   0.10384837E+02   0.10372684E+02   
 'DATAEND '   0 'MESS'


test.bin
Description: Binary data
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reading binary files

2009-02-03 Thread etrade . griffiths
Sorry, still having problems 


   I am trying to read data from a file that has format
  item_name  num_items  item_type  items 
  
  eg
  
  TIME  1  0.0
  DISTANCE 10  0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
 
 Where is the item_type?
Ooops, the data format should look like this:

TIME  1  F  0.0
DISTANCE 10  F  0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0

F=float, D=double, L=logical, S=string etc

 
  I can read this if the data are in ASCII format using
  
 in_file = open(my_file.dat,r)
 data1 = in_file.read()
 tokens = data1.split()
 
 It might be easier to process line by line using readline 
 or readlines rather than read but otherwise, ok so far...
 
  and then stepping through the resulting list but the data 
  also appear in the same format in a binary file.  
 
 When you say a binary file do you mean an ASCII file 
 encoded into binary using some standard algorithm?
 Or do you mean the data is binary so that, for example, 
 the number 1 would appear as 4 bytes? If so do you 
 know how strings (the name) are delimited? Also 
 how many could be present - is length a single or 
 multiple bytes? and are the reors fixed length or 
 variable? If variable what is the field/record separator?

Sorry, no idea what the difference is.  All I know is that the data
were written by a FORTRAN program using the UNFORMATTED argument
in the WRITE statement and that if they had been written FORMATTED
then we would get  afile that looks something like the example above

 
 You may need to load the file into a hex editor of debugger 
 to determine the answers...
 
 Having done that the struct module will allow you to read 
 the data.
 
 You can see a basic example of using struct in my 
 tutorial topic about handling files.

The first part of the file should contain a string (eg TIME),
an integer (1) and another string (eg F) so I tried using

import struct
in_file = open(file_name+.dat,rb)
data = in_file.read()
items = struct.unpack('sds', data)

Now I get the error

error: unpack requires a string argument of length 17

which has left me completely baffled! 

 
 
 --
 
 Message: 4
 Date: Mon, 02 Feb 2009 14:53:59 -0700
 From: Bernd Prager be...@prager.ws
 Subject: [Tutor] question about mpmath product expression
 To: tutor@python.org
 Message-ID: ac7e7f56dc4bc0903dc7df8861f9b...@prager.ws
 Content-Type: text/plain; charset=UTF-8
 
 Does anybody know if there is a precision difference when I use mpmath and
 take an expression:
 
 from mpmath import *
 mp.dps = 100
 mu0 = [mpf('4') * pi * power(10, -7)
 
 rather then:
 
 mu0 = fprod([mpf('4'), pi, power(10, -7)])
 
 ?
 
 Thanks,
 -- Bernd
 
 
 --
 
 Message: 5
 Date: Mon, 2 Feb 2009 14:46:18 -0800 (PST)
 From: Bernard Rankin beranki...@yahoo.com
 Subject: [Tutor] regex: not start with FOO
 To: Tutor@python.org
 Message-ID: 528538.84097...@web112218.mail.gq1.yahoo.com
 Content-Type: text/plain; charset=us-ascii
 
 Hello,
 
 
 I'd like to match any line that does not start with FOO.  (Using just a
 reg-ex rule)
 
 1) What is the effective difference between:
 
 (?!^FOO).*
 
 ^(?!FOO).*
 
 2) Is there a better way to do this?
 
 
 Thanks,
 :)
 
 
 
   
 
 
 
 --
 
 Message: 6
 Date: Mon, 02 Feb 2009 15:50:18 -0800
 From: WM. wfergus...@socal.rr.com
 Subject: [Tutor] newton's sqrt formula
 To: tutor@python.org
 Message-ID: 498786ba.6090...@socal.rr.com
 Content-Type: text/plain; charset=ISO-8859-1; format=flowed
 
 # program to find square root
 square = input ('Please enter a number to be rooted, ')
 square = square * 1.0
 guess = input('Please guess at the root, ')
 guess = guess * 1.0
 newguess = 0.
 
 while guess**2 != square:
  # Newton's formula
  newguess = guess - (guess * guess - square) / (guess * 2)
  guess = newguess
  guess**2 - square
 print
 print
 print guess, ' is the square root of ', square
 print
 print
 print 'bye'
 Last month there was a square root program discussed. I wondered if the 
 tide of my ignorance had receded enough that I could take a whack at 
 messing with it.
 I offer this rewrite for your critique. Can it be terser, faster, prettier?
 Thank you.
 
 
 
 
 --
 
 Message: 7
 Date: Tue, 3 Feb 2009 00:44:27 -
 From: Alan Gauld alan.ga...@btinternet.com
 Subject: Re: [Tutor] newton's sqrt formula
 To: tutor@python.org
 Message-ID: gm841b$l9...@ger.gmane.org
 Content-Type: text/plain; format=flowed; charset=iso-8859-1;
   reply-type=response
 
 WM. wfergus...@socal.rr.com wrote
 
  square = input ('Please enter a number to be rooted, ')
  square = square * 1.0
 
 Use raw_input() instead of input() and don't multiply
 by 1.0 - instead convert to float using float():
 
 square = float( raw_input ('Please enter a number to be rooted, '))
 
  guess = input('Please guess at the root, ')
  guess = guess * 1.0
  newguess = 0.
 
  while guess**2 != square:
  # 

[Tutor] reading binary files

2009-02-02 Thread etrade . griffiths
Hi

I am trying to read data from a file that has format

item_name  num_items  item_type  items 

eg

TIME  1  0.0
DISTANCE 10  0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
TIME  1  1.0
DISTANCE 10  1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0

I can read this if the data are in ASCII format using

in_file = open(my_file.dat,r)
data1 = in_file.read()
tokens = data1.split()

and then stepping through the resulting list but the data 
also appear in the same format in a binary file.  I tried 
converting the binary file to an ASCII file using

ifile = open(my_file.dat,rb)
ofile = open(new_file.dat,w)
base64.decode(ifile, ofile)

but that gave the error Error: Incorrect padding.  I imagine
that there is a straightforward way of doing this but haven't
found it so far.  Would be grateful for any suggestions!

Thanks

Alun Griffiths








-
Visit Pipex Business: The homepage for UK Small Businesses

Go to http://www.pipex.co.uk/business-services

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How many loops does break jump out of?

2007-02-22 Thread etrade . griffiths
Am trying to convert a C program with a GOTO in it to Python and was wondering 
how many loops a Python break jumps out of.  Here is the C pseudo code

if (test_1) {
for (;;) {
if (test_2) {
do_stuff();
} else if (test_2) {
for (ip=m1+m2+1;ip=m;ip++) {
if (test_3) {
do_more_stuff();
if (test_4)
goto one;
}
}
for (i=m1+1;i=m1+m2;i++)
do_even_more_stuff();

do_yet_more_stuff();
}

do_final_stuff();

one:continue_program();

Python version 1 - assumes break jumps out of outer enclosing loop

if (test_1):
while 1:
if (test_2):
do_stuff()
elif (test_2):
for ip in range(m1+m2+1, m):
if (test_3):
do_more_stuff()
if (test_4):
break
for i in range(m1+1, m1+m2):
do_even_more_stuff()

do_yet_more_stuff()
}

do_final_stuff()

continue_program()# one (break jumps out of while loop to here)

Python version 2 - assumes break jumps out of inner loop only.

if (test_1):
while 1:
if (test_2):
do_stuff()
elif (test_2):
for ip in range(m1+m2+1, m):
if (test_3):
do_more_stuff()
if (test_4):
quit_loop = true
break# jump out of for loop

if quit_loop:
break:   # jump out of if test

for i in range(m1+1, m1+m2):
do_even_more_stuff()

do_yet_more_stuff()
}

# break jumps out of if loop to here

if quit_loop:
break# jump out of while loop

do_final_stuff()

continue_program()# one
   
Python version 3 - assumes break jumps out of for loop only.

if (test_1):
while 1:
if (test_2):
do_stuff()
elif (test_2):
for ip in range(m1+m2+1, m):
if (test_3):
do_more_stuff()
if (test_4):
quit_loop = true
break# jump out of for loop

for i in range(m1+1, m1+m2):
do_even_more_stuff()

do_yet_more_stuff()
}

# break jumps out of for loop to here

if quit_loop:
break# jump out of while loop

do_final_stuff()

continue_program()# one

Unfortunately, can't test the Python script against the C program 'cos I don't 
have a C compiler.  All commments gratefully received
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Alternatives to PY2EXE

2006-11-16 Thread etrade . griffiths
Thanks for the various advice(s) re PY2EXE alternatives.  I considered 
installing Python on the target machines but though it would be simpler to 
install the EXE as I has done with C++ apps.  Boy, how naive was that?  Using 
PY2EXE is not straightforward and the documentation is fairly poor IMHO.  
pyInstaller sounds promising so I will check this out. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Use of dateutil.relativedata

2006-11-15 Thread Etrade Griffiths
Hi

trying to use dateutil to calculate the number of months between two 
dates.  here is the script

import datetime
import dateutil

x=datetime.date.today()
y=x+datetime.timedelta(days=366)
z=y-x

print x,y,z

a=dateutil.relativedelta(x,y)
print a


and here is the output

 
2006-11-15 2007-11-16 366 days, 0:00:00

Traceback (most recent call last):
   File C:/Projects/completion_dates, line 10, in -toplevel-
 a=dateutil.relativedelta(x,y)
AttributeError: 'module' object has no attribute 'relativedelta'
 

There's problem here that's really basic but just can't see it!!!

Thanks

Alun Griffiths 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Use of dateutil.relativedata

2006-11-15 Thread Etrade Griffiths
Sorry, problem solved

import datetime
import dateutil.relativedelta

x=datetime.date.today()
y=x+datetime.timedelta(days=366)
z=y-x

print x,y,z

a=dateutil.relativedelta.relativedelta(x,y)
print a

seems to do the trick


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Alternatives to PY2EXE

2006-11-14 Thread Etrade Griffiths
Hi

just finished developing my first app with wxPython and matplotlib and now 
trying to create an EXE file using PY2EXE for distribution.  However, this 
is proving to be an extremely frustrating experience and I am making very 
little progress.  Are there any simple alternatives to PY2EXE for 
shipping Python apps to Windows machines?

Thanks

Alun Griffiths


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Getting the type of a variable

2006-10-27 Thread Etrade Griffiths
Hi

I want to check the type of a variable so that I know which format to use 
for printing eg

def print_correct_format(a):

if type(a) == 'int':
print a is an integer, value %i % (a)
elif type(a) == 'float':
print a is a float, value %f % (a)
else:
print a is unknown type

The comparison type(a) == 'int' etc does not work - I'm sure there's a 
simple way to fix this but can't see it at the moment - any suggestions?


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Getting the type of a variable

2006-10-27 Thread Etrade Griffiths


Thanks, guys - like so many things, easy when you know how!
Alun Griffiths

At 14:27 27/10/2006, you wrote:
Use this:

if type(a) == type(1):
 print a is int %d
elif type(a) == type(1.1):
 ...

HTH..
Regards,
Asrarahmed Kadri


On 10/27/06, Etrade Griffiths
[EMAIL PROTECTED]
wrote: 


Hi

I want to check the type of a variable so that I know which format to
use

for printing eg

def print_correct_format(a):

 if type(a) == 'int':


print a is an integer, value %i % (a)

 elif type(a) == 'float':


print a is a float, value %f % (a) 

 else:


print a is unknown type

The comparison type(a) == 'int' etc does not work - I'm sure there's
a

simple way to fix this but can't see it at the moment - any
suggestions? 


___

Tutor maillist -
Tutor@python.org

http://mail.python.org/mailman/listinfo/tutor




-- 
To HIM you shall return. 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Sharing data between modules

2006-10-19 Thread etrade . griffiths
Hi

just starting to get to grips with writing GUIs in Python using wxPython and 
not being a computer scientist, have a philosophical question about the best 
way to pass data between various modules.  For example, I anticipate having 
one module to look after the GUI stuff where users can input data (either from 
the keyboard or file), another to process the data and (probably) a third to 
display the calculation results.  Just not sure what the most efficient way of 
passing data between the various modules.  I can think of 4 ways to do this, 
of which 1 is not possible in Python (I think).

(1) via temp files: modules read to and write from temporary files
(2) via function arguments: data passed from one module to another via 
argument lists.  This seems the most logical but I have an irrational dislike 
of long argument lists.  Could pass data objects (eg. C type structs) or 
dictionaries via the argument list but I don't think this gets around 
the problem of long argument lists and we now need pack/unpack type 
functions.  Maybe this isn't a problem anyway?
(3) via globals: AFAIK this is not recommended so we will move swiftly on
(4) indirectly: thinking here of something along the FORTRAN COMMON blocks 
which (I think) Python doesn't have

So, looks like it's a choice between (1) and (2) - or have I missed something 
obvious?  What is the most pythonic way of doing this?  Thanks in advance 
for any suggestions
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Traversing Excel Columns

2006-09-12 Thread Etrade Griffiths
Chris

are you looking for something like this?

xlSht=xlApp.Worksheets(Sheet1)

irow=1
XL_row_has_data=1

while XL_row_has_data:
 xlRng=xlSht.Range(xlSht.Cells(irow,1),xlSht.Cells(irow,256))
 ncell=xlApp.WorksheetFunction.CountA(xlRng)

 if ncell ==0:
 # Cells in current row are all empty
 XL_row_has_data=0
 else:
 # Look in next row
 irow=irow+1

print first row with empty cells is row +str(irow)

HTH

Alun Griffiths


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Running DOS jobs in batch

2006-09-07 Thread Etrade Griffiths
Hi

I am trying to write a PYTHON script that automates the running of an 
application program MX200510.EXE under XP Pro via a DOS window.  This file 
is in directory

C:\Program Files\CMG\IMEX\2005.10\EXE

The command line arguments are

MX200510.EXE -f temp.dat -wd C:\Projects\Vitol\New 
business\Octon\simulation\full field model\0608

My Python script is as follows:

#
# Test Python script to submit IMEX jobs
#

import os

#
# Start of MAIN program
#

# Initialisation

work_dir=r'C:\Projects\Vitol\New business\Octon\simulation\full field 
model\0608'
imex_dir=r'C:\Program Files\CMG\IMEX\2005.10\EXE'
imex_fil='temp.dat'

imex_args=('mx200510.exe','-f',imex_fil,'-wd',''+work_dir+'')

nscen=2

# Check IMEX directory and files exist

os.chdir(imex_dir)
L=os.listdir(imex_dir)

for item in L:
 print item

# Change directory to working directory

os.chdir(work_dir)

# Loop over N scenarios

for n in range(1,nscen):

 # Spawn IMEX job and wait for completion

 os.spawnv(os.P_WAIT, imex_dir, imex_args)

The output from this script is

ck9700.dll
libguide40.dll
mx200510.exe
mx200510en.chm
mx200510sp.chm

Traceback (most recent call last):
   File C:/Projects/Vitol/New business/Octon/simulation/full field 
model/0608/test_imex.py, line 39, in -toplevel-
 os.spawnv(os.P_WAIT, imex_dir, imex_args)
OSError: [Errno 2] No such file or directory

I tried following the ARGS into the os module using the debugger but this 
did not help much, presumably because OS is precompiled.  Not sure which 
file or directory Python is unhappy about - LISTDIR shows that the EXE file 
and path exist so presumably it's something in the way I
set up the argument list.  All suggestions gratefully received!

Thanks

Alun Griffiths


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Running DOS jobs in batch

2006-09-07 Thread Etrade Griffiths


Thanks for that Kent. I did
imex_exe=r'C:\Program Files\CMG\IMEX\2005.10\EXE\mx200510.exe'
imex_args=('mx200510.exe','-f',imex_fil,'-wd',''+work_dir+'')
for n in range(1,nscen):
os.spawnv(os.P_WAIT,
imex_exe, imex_args)
and it seems to work. Not sure why I need to provide the name of
the application twice though: the lib pages for spawn* say In
either case, the arguments to the child process must start with the name
of the command being run but hey, it works!
Thanks again
Alun


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Confused about globals

2006-06-09 Thread Etrade Griffiths
Hi

I have a series of python programs that plot stuff using PYX.  Common to 
these is the need to read in a list of (well) locations in (X,Y) coords so 
I put that code in a separate module called shared_funcs.py.  The coords 
are stored in dictionaries which I want to use later in the main program 
- every time I find a well with data, I can get the well's (x,y) coords by 
looking up the well name in the dictionary.  The code snippet looks like this:

=== main.py 

# define coord dictionaries for global use

DE={}
DN={}

import shared_funcs()

shared_funcs.get_xy_data() # Get coords from file

  ... do plotting stuff


=== shared_funcs.py ===

def get_xy_data():
in_file=open(well_xy.txt,r)
for line in in_file
L=line.split()
well=L[0]
x=L[1]
y=L[2]

DE[well]=x
DN[well]=y
in_file.close()

The problem is that DE and DN appear not to be accessible to get_xy_data - 
presumably this is because shared_funcs.py and main.py don't share the same 
scope.  So, is there any way to let get_xy_data change DE and DN?  I guess 
the obvious way is to pass them to get_xy_data as arguments - but is there 
a more pythonic method?  Thanks in advance!


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] unpacking PyTime

2006-05-17 Thread Etrade Griffiths
Hi

I am using the Win32com library to pick up data from an EXCEL spreadsheet 
but am having trouble with dates.  I want to convert a date read from the 
XL sheet into a float using this snippet

from win32com.client import dispatch
import time

xlFile=test.xls
xlApp=Dispatch(Excel.Application)
xlApp.Workbooks.Open(xlFile)
xlSht=xlApp.Worksheets(data)

# OK so far but the problem comes now ...

curr_date=xlSht.Cells(1,3).Value # returns PyTime

Not how to get the date (as either yy,mm,dd or a single number aka 
XL).  The ASPN ActivePython site suggests using

x=curr_date.__float__()

but Python gives the error AttributeError: __float__

I also tried

x=float(curr_date)

but Python gives the error TypeError: float() argument must be string or a 
number.  All suggestions gratefully received!  


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] unpacking PyTime

2006-05-17 Thread Etrade Griffiths
Bob

I was looking at

http://aspn.activestate.com/ASPN/docs/ActivePython/2.4/pywin32/PyTime.html

To my untrained eye it looks like there are a number of functions for 
PyTime objects (eg __int__, __hash__ etc).  However, I tried int(curr_date) 
and it seems to work OK - thanks!

At 19:11 17/05/2006, Bob Gailer wrote:
Etrade Griffiths wrote:
Hi

I am using the Win32com library to pick up data from an EXCEL spreadsheet 
but am having trouble with dates.  I want to convert a date read from the 
XL sheet into a float using this snippet

from win32com.client import dispatch
import time

xlFile=test.xls
xlApp=Dispatch(Excel.Application)
xlApp.Workbooks.Open(xlFile)
xlSht=xlApp.Worksheets(data)

# OK so far but the problem comes now ...

curr_date=xlSht.Cells(1,3).Value # returns PyTime

Not how to get the date (as either yy,mm,dd or a single number aka 
XL).  The ASPN ActivePython site suggests using

It appears that the ActivePython docs may be wrong, as the only method I 
see for a PyTime object is Format. Looking up Format leads to 
curr_date.Format(%y,%m,%d) to get yy,mm,dd.

Or you can, as the ActivePython docs suggest, use int(curr_date) to get an 
integer value which you can then float.

--
Bob Gailer
510-978-4454


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looping over lists of objects

2006-04-24 Thread Etrade Griffiths
Ed

the problem is that my original code did not have the closing brackets for 
the method calls get_a and get_b whereas the code snippet I posted 
did.  That's probably why your version works and mine failed.  Thanks for 
your help!

Alun

At 17:19 24/04/2006, Ed Singleton wrote:
On 24/04/06, Etrade Griffiths [EMAIL PROTECTED] wrote:
   Hi
 
   just feeling my way into Python with a  small app that reads data from
  file, creates objects using that data, stores the objects in a list, loops
  over the list doing comparison tests to filter out various 
 objects.  Here is
  a code snippet:
 
[snip]
 
   Trying to debug this using IDLE.  The calls x.get_a and x.get_b always
  return zero so something is going wrong somewhere.  I think I'm either not
  storing the objects correctly or retrieving them correctly but no idea why!
  All suggestions gratefully received!!!

I added some test input to give the code below, and it works fine for
me.  Can you give us some test input that fails for you?  Can you also
show us your test() function as it may the code in there that is
failing.

Ed

class myObj:
 def __init__(self,a,b):
 self.a=a
 self.b=b

 def get_a(self):
 return self.a

 def get_b(self):
 return self.b


input = [1 2 3, 4 5 6]

L1=[]
nobj=0

for line in input:
 L0=line.split()
 a=L0[1]
 b=L0[2]
 nobj=nobj+1

 an_obj=myObj(a,b)

 L1.append(an_obj)

# Filter data

for i in range(1,nobj):
 for x in L1:# ... loop over all objects in list
 print a - , x.get_a() # ... get value
of a from current object
 print b - , x.get_b()


It returns:

a -  2
b -  3
a -  5
b -  6


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] First steps with Tkinter

2006-01-27 Thread Etrade Griffiths


Catherine
I'm a Python newbie too but have done some programming with C++ Builder
so have a little knowledge of GUIs etc
Best regards
Alun

At 22:41 26/01/2006, catherine curley wrote:
Alan

As a matter of interest, did you have much knowledge of Python before you
tried TKinter? I'm only a python beginner at present.

Catherine

On 1/26/06,
[EMAIL PROTECTED]
[EMAIL PROTECTED]  wrote: 


Hi!

Just started trying to get to grips with Python and Tkinter. Have Frederick

Lundh's tutorial and am on program hello2.py which looks like this

# File: hello2.py

from Tkinter import *

class App:

 def __init__(self, master):

 frame = Frame(master)

 frame.pack()

 self.button = Button(frame, text=QUIT, fg=red, command=frame.quit)

 self.button.pack(side=LEFT)

 self.hi_there = Button(frame, text=Hello, command=self.say_hi)

 self.hi_there.pack (side=LEFT)

 def say_hi(self):

 print hi there, everyone!

root = Tk()

app = App(root)

root.mainloop()

I am running from inside Pythonwin 2.4 IDE under XP Pro and every time I run 

hello2.py it freezes when I press QUIT. The only way to kill it is through

Alt-Ctrl-Del but this crashes Pythonwin. Any workaround for this so that I

can use Tkinter from inside the IDE? BTW the same thing happend with IDLE 

Thanks in advance

Alun Griffiths

___

Tutor maillist - Tutor@python.org

http://mail.python.org/mailman/listinfo/tutor



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] First steps with Tkinter

2006-01-26 Thread etrade . griffiths
Hi!

Just started trying to get to grips with Python and Tkinter.  Have Frederick 
Lundh's tutorial and am on program hello2.py which looks like this

# File: hello2.py

from Tkinter import *

class App:

def __init__(self, master):

frame = Frame(master)
frame.pack()

self.button = Button(frame, text=QUIT, fg=red, command=frame.quit)
self.button.pack(side=LEFT)

self.hi_there = Button(frame, text=Hello, command=self.say_hi)
self.hi_there.pack(side=LEFT)

def say_hi(self):
print hi there, everyone!

root = Tk()

app = App(root)

root.mainloop()

I am running from inside Pythonwin 2.4 IDE under XP Pro and every time I run 
hello2.py it freezes when I press QUIT.  The only way to kill it is through 
Alt-Ctrl-Del but this crashes Pythonwin.  Any workaround for this so that I 
can use Tkinter from inside the IDE?  BTW the same thing happend with IDLE

Thanks in advance

Alun Griffiths
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor