Re: [Tutor] Tutor Digest, Vol 49, Issue 78

2008-03-26 Thread Tony Cappellini
>>Draft notes for the next Kent's Korner presentation are available at
>>http://personalpages.tds.net/~kent37/kk/00010.html

>>Comments welcome.
I vote Kent move out of the korner and into the front of the classroom!

Nice color scheme, easy to look at, good layout, font, size, and small
chunks (paragraphs?) of info to digest without getting overwhelmed by
a page of content.

The page looks great.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] SOAPpy and ZSI

2008-03-26 Thread Dan Thomas-Paquin
Hi,
I've been tasked with setting up a basic SOAP client and I'm its been the most
frustrating python experience.

Here's the code:
from LoginService_services import *
import sys
from SOAPpy import SOAPProxy

# get a port proxy instance
loc = LoginServiceLocator()
port = loc.getLogin(in0='pbs_uen', in1='TDapi', in2='3dcarapi')

# create a new request
req = LoginRequest()

# call the remote method
resp = port.Login(req)

lr = resp.LoginReturn
lri0 = lr.get_element_items()[0]
service_url = lri0.Value

n = 'http://DefaultNamespace'
server = SOAPProxy(service_url, namespace=n)
server.config.dumpSOAPOut = 1
server.config.dumpSOAPIn = 1
server.config.dumpHeadersOut = 1
server.config.dumpHeadersIn = 1
test = server.greeting()

Notice I use ZSI (LoginService_services) for the Login and then SOAPpy for the 
post
log-in stuff (ZSI's wsdl2py script broke on the post log-in WSDL).
The login part works fine. It returns a URL with a session id appended. 

server.greeting() creates this envelope which I can see because of the dumps:
http://schemas.xmlsoap.org/soap/encoding/";
  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/";
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/";
>

http://DefaultNamespace"; SOAP-ENC:root="1">




but then this error follows right after:

  File "generic_usage.py", line 34, in 
test = server.greeting()
  File
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/SOAPpy/Client.py",
line 470, in __call__
return self.__r_call(*args, **kw)
  File
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/SOAPpy/Client.py",
line 492, in __r_call
self.__hd, self.__ma)
  File
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/SOAPpy/Client.py",
line 363, in __call
config = self.config)
  File
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/SOAPpy/Client.py",
line 252, in call
raise HTTPError(code, msg)
SOAPpy.Errors.HTTPError: 

line 252 of Client.py is:

 if code == 500 and not \
   ( startswith(content_type, "text/xml") and message_len > 0 ):
raise HTTPError(code, msg)


I can't tell if the server is returning the 500 error or if the transport is. I
honestly don't know where to go from here. I wouldn't doubt that the envelope 
isn't
correct for the service but I don't have access to any more info on that. Help 
much
appreciated.

Dan









"All men dream: but not equally. Those who dream by night in the dusty recesses 
of their minds wake in the day to find that it was vanity: but the dreamers of 
the day are dangerous men, for they may act their dreams with open eyes, to 
make it possible." -T. E. Lawrence -
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with logic while extracting data from binary file

2008-03-26 Thread bob gailer
Please always reply to the list not just me.

Bryan Fodness wrote:
>
> Thanks Bob,
>  
> I was having trouble with that loop from the start.  Could you tell me 
> what a=3 is doing, I cannot seem to figure it out.
I accidentally left that in. It was a place for me to set a breakpoint 
in the debugger.

I also failed to point out that I made the creation of the dictionary 
"search" straightforward rather than collecting all the keys and values. 
However neither your code or mine handles the multi-value cases (e.g. 
\n0\x82). To solve that I changed search to be of type defaultdict so 
each new element is a list and we can use append() to add values to the 
lists. The printed output now looks like lists but that is easy to alter.

In this version I also "import time", and changed the names start and 
time so as not to conflict (even in appearance) with other names.

Also in this version I modified parseDataElement to be a generator (the 
quickest way I could think of to solve the while loop problem) and added 
a for loop in parseDICOM to access the generator. This also required 
adding "start = pos" at the end of the while loop.

BUT ... on further inspection of your program and data I note that there 
is NOTHING that looks for \n0\x82. It sits in the middle of a block of 
data of length 544. There needs to be some code to examine the contents 
of that block and look for \n0\x82.

Also discovered and fixed "name 'LeafJawPostions' is not defined".

Code follows 
--

import struct
import time
import collections
print "Creating Variables...\n"
startTime = time.clock()
rawData = open('file.dcm', 'rb').read()
# Need to get Transfer Syntax UID (0002,0010) for encoding type

def parseDICOM(data):
search = collections.defaultdict(list)
try:
preamble, next = parsePreamble(data)
except NotAPreambleException:
preamble, next = None, 0
while next < len(data):
for element, next, value, length in parseDataElement(data, next):
if element.startswith('\n0') and element.endswith('\x00') 
and element[2] in ('\x10', '@', 'p', '\xb0'):
start = 0
while  start < length:
element, start, svalue = parseSequence(value, start)
search[element].append(svalue)
else:
search[element].append(value)
return search

def parsePreamble(data):
preamble = data[:128]
dicm = data[128:132]
if dicm == 'DICM':
return preamble, 132
else:
raise NotAPreambleException

def parseMetaElementGL(data):
vl_field = data[138:140]
length = struct.unpack('h', vl_field)[0]
value = struct.unpack('hh',data[140:(140+length)])[0]
return value
  
def parseDataElement(data, start):
if start < (144 + parseMetaElementGL(rawData)):
group_num = data[start:start+2]
element_num = data[start+2:start+4]
vr_field = data[start+4:start+6]
if vr_field in ('UN', 'SQ', 'OB','OW'):
unused = data[start+6:start+8]
vl_field = data[start+8:start+12]
length = struct.unpack('hh', vl_field)[0]   # 4-byte
value = data[start+12:(start+12+length)]
pos = start+12+length
element = (group_num+element_num)
yield element, pos, value, length
else:
vl_field = data[start+6:start+8]
length = struct.unpack('h', vl_field)[0]# 2-byte
value = data[start+8:(start+8+length)]
pos = start+8+length
element = (group_num+element_num)
yield element, pos, value, length
else:
while start < len(data):
group_num = data[start:start+2]
element_num = data[start+2:start+4]
vl_field = data[start+4:start+8]   
length = struct.unpack('hh', vl_field)[0]
value = data[start+8:(start+8+length)]
pos = start+8+length
element = (group_num+element_num)
yield element, pos, value, length
start = pos
else:
print "End of File"

def parseSequence(data, start):
group_num = data[start:start+2]
element_num = data[start+2:start+4]
vl_field = data[start+4:start+8]
length = struct.unpack('hh', vl_field)[0]
value = data[start+8:(start+8+length)]
pos = start+8+length
element = (group_num+element_num)
if element == '\xfe\xff\x00\xe0':
start = start+8
group_num = data[start:start+2]
element_num = data[start+2:start+4]
vl_field = data[start+4:start+8]
length = struct.unpack('hh', vl_field)[0]
value = data[start+8:(start+8+length)]
pos = start+8+length
element = (group_num+element_num)
if element == '\xfe\xff\x00\xe0':
start = start+8
group_num = data[start:start+2]
element_num = data[start+2:start+4]
   

Re: [Tutor] Library for Disk Usage (UNIX)

2008-03-26 Thread Chris Fuller
On Wednesday 26 March 2008 09:11, Tom Tucker wrote:
> Hello all. I'm looking for a builtin Python library capable of providing
> similar output to what the unix df command provides.  Obviously, I'm trying
> to avoid a system call if possible.  I'm looking for the following fields
> at a mimimum, total size, used, and /path. Suggestions?  I was looking at
> os.stat(/path)[WXYZ}, and os.path.getsize, but they are lacking.
>
> Thanks for the help,
>
> Tom

You need to know the size of the blocks that the filesystem uses.  Use the 
statvfs module and the os.statvfs function.

Cheers


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


[Tutor] Library for Disk Usage (UNIX)

2008-03-26 Thread Tom Tucker
Hello all. I'm looking for a builtin Python library capable of providing
similar output to what the unix df command provides.  Obviously, I'm trying
to avoid a system call if possible.  I'm looking for the following fields at
a mimimum, total size, used, and /path. Suggestions?  I was looking at
os.stat(/path)[WXYZ}, and os.path.getsize, but they are lacking.

Thanks for the help,

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


Re: [Tutor] Table like array in Python

2008-03-26 Thread Roel Schroeven
Gloom Demon schreef:
> Hello :-)
> 
> I am reading Ivan van Leiningem "Learn Python in 24 hours" and I am 
> having problems understanding the way arrays work in Python. I used to 
> know Pascal and arrays there were tablelike.
> 
> Example (cost of something in different countries by different years)
> 
> Record1 US 2006 22.10
> Record2 US 2007 23.45
> Record3 UK 2007 22.90
> ..
> RecordN 

That is an array of records. In Pascal you can also have e.g. an array 
of integers, and it is a sequential list just as in Python. What makes 
it table-like is that you have an array not of scalars but of records.

> However in Python, if I understand correctly this example would look 
> like this:
> 
> US 2006 22.10 US 2007 23.45 UK 2007 22.90 

You could do it like that, but there are better ways. You could make a 
list of tuples, which would be more or less equivalent to your Pascal 
array of records. A simple example, presuming you read the values from a 
file:

lst = []
for line in countryfile:
   country, year, amount = line.split()
   year = int(year)
   amount = float(amount)
   lst.append((country, year, amount))

That would look like:

[
   ('US', 2006, 22.10),
   ('US', 2007, 23.45)
   ...
]

Then you could scan through it like this:

for record in lst:
   if record[0] == 'US':
 ...


-- 
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
   -- Isaac Asimov

Roel Schroeven

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


Re: [Tutor] Table like array in Python

2008-03-26 Thread Kent Johnson
Gloom Demon wrote:

> Example (cost of something in different countries by different years)
> 
> Record1 US 2006 22.10
> Record2 US 2007 23.45
> Record3 UK 2007 22.90
> ..
> RecordN 
> 
> So I could read the record, see if the name of the country in the first 
> cell was what I was looking for and either continue working with the 
> record or resume searching.

Where are the records coming from? A file or database?
> 
> However in Python, if I understand correctly this example would look 
> like this:
> 
> US 2006 22.10 US 2007 23.45 UK 2007 22.90 

If the data is in a file, and you read the file using file.read(), then 
you will get the full contents in memory in a single string.
> 
> This means that I have to keep a lot of unnesessary information in RAM, 
> not to mention that I would have to scan through the whole array instead 
> of scanning just the required cell. 
> 
> Could anyone please direct me to a good description of working with 
> arrays in Python?

Arrays are called lists in Python, they are covered in every tutorial.
But I think maybe it is files that you need help with.

You can read files line by line. One way is to use a for loop, for example,

f = open('records.txt')
for line in f: # the contents of each line in turn will be put in 'line'
   data = line.split() # split the line at spaces
   if data[1] == 'US':
 # process line for US

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


Re: [Tutor] Table like array in Python

2008-03-26 Thread Michael Connors
On 26/03/2008, Gloom Demon <[EMAIL PROTECTED]> wrote:
>
> Hello :-)
>
> Example (cost of something in different countries by different years)
>
> Record1 US 2006 22.10
> Record2 US 2007 23.45
> Record3 UK 2007 22.90
> ..
> 
>
>
In Python a list is similiar to an array in Pascal.
You define a list like this: my_list = [1, 2, 3, 4]

However I dont think you want an array here, if I was doing something like
this I would use a dictionary. A dictionary stores keys and values.
So you could do something like this:

records = {'US': {'2007': 22.5, '2008': 44.8}, 'UK': {'2008': 3.4, '2007':
2.6}}

You can now access a particular record as follows:

In: print records['UK']['2007']
Out: 2.6

Regards,
-- 
Michael Connors
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Table like array in Python

2008-03-26 Thread Gloom Demon
Hello :-)

I am reading Ivan van Leiningem "Learn Python in 24 hours" and I am having
problems understanding the way arrays work in Python. I used to know Pascal
and arrays there were tablelike.

Example (cost of something in different countries by different years)

Record1 US 2006 22.10
Record2 US 2007 23.45
Record3 UK 2007 22.90
..
RecordN 

So I could read the record, see if the name of the country in the first cell
was what I was looking for and either continue working with the record or
resume searching.

However in Python, if I understand correctly this example would look like
this:

US 2006 22.10 US 2007 23.45 UK 2007 22.90 

This means that I have to keep a lot of unnesessary information in RAM, not
to mention that I would have to scan through the whole array instead
of scanning just the required cell.

Could anyone please direct me to a good description of working with arrays
in Python?
And I have problems understanding what dictionaries are for :-(

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