[Tutor] XML parsing when elements contain foreign characters

2014-01-09 Thread Garry Bettle
Howdy all,

Have you hear the news? Happy New Year!

Hope someone can help. I know this is a tutor list so please feel free to
send me somewhere else.

I'm trying to parse some XML and I'm struggling to reference elements that
contain foreign characters.

Code so far:

# -*- coding: utf-8 -*-

from xml.dom import minidom

xmldoc = minidom.parse('Export.xml')
products = xmldoc.getElementsByTagName('product')
print '%s Products' % len(products)

row_cnt = 0
titles = {}
stocklevel = {}
for product in products:
  row_cnt+=1
  title=product.getElementsByTagName('Titel')[0].firstChild.nodeValue
  stock=product.getElementsByTagName('AntalPåLager')[0].firstChild.nodeValue
  if title not in titles:
titles[title]=1
  else:
titles[title]+=1
  if stock not in stocklevel:
stocklevel[stock]=1
  else:
stocklevel[stock]+=1

Traceback (most recent call last):
  File C:\Python27\Testing Zizzi.py, line 16, in module

stock=product.getElementsByTagName('AntalPÃ¥Lager')[0].firstChild.nodeValue
IndexError: list index out of range

I've tried to encode the string before giving it to getElementsByTagName
but no joy.

Any ideas?

Many thanks!

Cheers,

Garry
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Question regarding xml.dom.minidom: How do you send an unsignedByte in an wsdl request

2011-07-22 Thread Garry Bettle
Howdy all,

Hope this message finds everyone well - roll on the weekend!

I'm trying some calls to an wsdl API I've subscribed to.

But I'm struggling to know what they want when sending an unsignedByte in a
request.

I'm using xml.dom.minidom so to start with I have:
from xml.dom.minidom import Document, parseString
import httplib, urlparse

And later on I create a doc with:
doc=Document()

And I have a using a little helper to add a text element:
def add_text_element(doc, parent, name, value):
element=doc.createElement(name)
element.appendChild(doc.createTextNode(str(value)))
parent.appendChild(element)

Should I have a separate class for an unsignedByte? i.e. def
add_byte_element

What should it look like?

This is what their API helpdesk have said:

In this instance, the PriceFormat is defined as an unsignedByte.



 *xs:complexType name=GetOddsLadderRequest*

*xs:attribute name=PriceFormat type=xs:unsignedByte / *

*/xs:complexType*

Many thanks!

Cheers,

Garry
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Dictionary Question

2010-12-22 Thread Garry Bettle
Howdy all,

Hope this message finds everyone well.

I have dictionary of keys and a string of values.

i.e.

8 Fixtures:

Swin1828 1844 1901 1916 1932 1948 2004 2019 2036 2052 2107 2122
HGrn1148 1204 1218 1232 1247 1304 1319 1333 1351
Newc1142 1157 1212 1227 1242 1258 1312 1327 1344 1403
Yarm1833 1849 1906 1922 1937 1953 2009 2024 2041 2057 2112
BVue1418 1437 1457 1517 1538 1558 1618 1637 1657 1717 1733 1747 1804 181
Hove1408 1427 1447 1507 1528 1548 1608 1627 1647 1707 1722 1738 1756 181
Romfd   1930 1946 2003 2019 2035 2053 2109 2125 2141 2157 2213 2230
Sund1839 1856 1911 1927 1943 1958 2014 2031 2047 2102 2117

I print that with the following:

f = open(SummaryFile, 'a')
header = %d Fixtures, %d Races:\n % len(FixtureDict.keys())
print header

f.write(header)
f.write(\n)
for fixture, racetimes in FixtureDict.iteritems():
line = %s\t%s  % (fixture,  .join(racetimes))
print line
f.write(line + \n)
f.write(\n)
f.close()

What I'd like to is add the number of values to the Header line.

So how would I get i.e.

8 Fixtures, 93 Races

I tried

header = %d Fixtures, %d Races:\n % (len(FixtureDict.keys()),
len(FixtureDict.values()))

But I get

print header
 8 Fixture, 8 Races

Any ideas?

Cheers,

Garry
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dictionary Question

2010-12-22 Thread Garry Bettle
On Wed, 22 Dec 2010 23:31:39 +1100, Steven D'Aprano wrote:
 In this case, you need to sum the number of races for all the fixtures:

 num_races = sum(len(racetimes) for racetimes in FixtureDict.values())

Many thanks Steven for your explanation and final golden nugget of code.

On Wed, 22 Dec 2010 10:11:25 -0500, bob gailer wrote:

 Also note: len(dict.keys()) == len(dict.values()) == len(dict)

Yup, thanks Bob.

Cheers,

G.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with a Dictionary

2010-01-11 Thread Garry Bettle
Many thanks to Alan Gauld, Gerard Flanagan, Lie Ryan and spir for your replies.

All systems are go!

Cheers,

Garry
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Help with a Dictionary

2010-01-07 Thread Garry Bettle
Howdy all,

I hope this message finds you all well.

I have a list that I output in the following order:

2010-01-07 1103 Sund A7 450m
2010-01-07  Sheff A7 500m
2010-01-07 1119 Sund A6 450m
2010-01-07 1128 Sheff A6 500m
2010-01-07 1134 Sund A5 450m
2010-01-07 1142 Sheff A7 500m
2010-01-07 1148 Sund A5 450m
2010-01-07 1157 Sheff A6 500m
2010-01-07 1204 Sund A4 450m
2010-01-07 1212 Sheff A5 500m
2010-01-07 1218 Sund A4 450m
2010-01-07 1227 Sheff A3 500m
2010-01-07 1232 Sund A4 450m
2010-01-07 1242 Sheff A4 500m
2010-01-07 1247 Sund A4 450m
2010-01-07 1258 Sheff A3 500m
2010-01-07 1304 Sund A3 450m
2010-01-07 1312 Sheff A4 500m
2010-01-07 1319 Sund HC 640m
2010-01-07 1327 Sheff A5 500m
2010-01-07 1333 Sund A3 450m
2010-01-07 1344 Sheff A3 500m
2010-01-07 1351 Sund A2 450m
2010-01-07 1403 Sheff A2 500m
2010-01-07 1408 Romfd A7 400m
2010-01-07 1418 Crayfd S8 540m
2010-01-07 1427 Romfd A6 400m
2010-01-07 1437 Crayfd H3 380m
... etc.

The above are RaceDate + RaceTime + Fixture + RaceDetails, and are
output in RaceTime order.

What I'd like to do, is output a transposed-like summary of just the
Fixture + RaceTime.

i.e.

Sund   1103 1119 1134 1148 1204 1218 1232 1247 1304 1319 1333 1351
Sheff    1128 1142 1157 1212 1227 1242 1258 1312 1327 1344 1403
Romfd 1408 1427 ...
Crayfd 1418 1437 ...

Do I need to use a Dictionary?

Many thanks!

Cheers,

Garry
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with a Dictionary

2010-01-07 Thread Garry Bettle
On Thu, Jan 7, 2010 at 15:26,  Garry Bettle wrote:

 Howdy all,

 I hope this message finds you all well.

 I have a list that I output in the following order:

 2010-01-07 1103 Sund A7 450m
 2010-01-07  Sheff A7 500m
 2010-01-07 1119 Sund A6 450m
 2010-01-07 1128 Sheff A6 500m
 2010-01-07 1134 Sund A5 450m
 2010-01-07 1142 Sheff A7 500m
 2010-01-07 1148 Sund A5 450m
 2010-01-07 1157 Sheff A6 500m
 2010-01-07 1204 Sund A4 450m
 2010-01-07 1212 Sheff A5 500m
 2010-01-07 1218 Sund A4 450m
 2010-01-07 1227 Sheff A3 500m
 2010-01-07 1232 Sund A4 450m
 2010-01-07 1242 Sheff A4 500m
 2010-01-07 1247 Sund A4 450m
 2010-01-07 1258 Sheff A3 500m
 2010-01-07 1304 Sund A3 450m
 2010-01-07 1312 Sheff A4 500m
 2010-01-07 1319 Sund HC 640m
 2010-01-07 1327 Sheff A5 500m
 2010-01-07 1333 Sund A3 450m
 2010-01-07 1344 Sheff A3 500m
 2010-01-07 1351 Sund A2 450m
 2010-01-07 1403 Sheff A2 500m
 2010-01-07 1408 Romfd A7 400m
 2010-01-07 1418 Crayfd S8 540m
 2010-01-07 1427 Romfd A6 400m
 2010-01-07 1437 Crayfd H3 380m
 ... etc.

 The above are RaceDate + RaceTime + Fixture + RaceDetails, and are
 output in RaceTime order.

 What I'd like to do, is output a transposed-like summary of just the
 Fixture + RaceTime.

 i.e.

 Sund   1103 1119 1134 1148 1204 1218 1232 1247 1304 1319 1333 1351
 Sheff    1128 1142 1157 1212 1227 1242 1258 1312 1327 1344 1403
 Romfd 1408 1427 ...
 Crayfd 1418 1437 ...

 Do I need to use a Dictionary?

 Many thanks!

 Cheers,

 Garry


This is what I've come up with.  Sorry, python is something I touch on
occasionally:  must do more!

As the races are output, I build a dictionary of key=FixtureName and
value=RaceTimes:

RaceTime = marketResp.market.displayTime.time()
cRaceTime = RaceTime.strftime(%H%M)
FixtureName = MarketSummary.marketName.strip()
MarketName = marketResp.market.name
if FixtureName not in FixtureList:
FixtureList[FixtureName] = cRaceTime
else:
FixtureList[FixtureName]+=   + cRaceTime

And then, when I want the summary to be printed:

for fixture in FixtureList:
print fixture.ljust(6), FixtureList[fixture]

It works, but have I done it the python way?  Can't I unpack both
the key and value from FixtureList?
This doesn't work:

for fixture, racetimes in FixtureList:
print fixture, racetimes

Traceback (most recent call last):
  File pyshell#3, line 1, in module
for fixture, racetimes in FixtureList:
ValueError: too many values to unpack

Another thing I'd like to change is the ljust().  I believe it's
depreciated later in 3.0+, so I should really find an alternative.

Cheers,

G.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Trouble with a Recipe ...

2009-08-25 Thread Garry Bettle
Hi,

Hope this email finds everyone well - roll on the weekend.

I'm trying to run http://code.activestate.com/recipes/576824/

I'm in IDLE and I try:

 email_it_via_gmail( {To: garry.bet...@gmail.com, Subject: Testing, 
 From: garry.bet...@gmail.com}, text=Testing)

but I get the following error:

Traceback (most recent call last):
  File pyshell#17, line 1, in module
email_it_via_gmail( {To: garry.bet...@gmail.com, Subject:
Testing, From: garry.bet...@gmail.com}, text=Testing)
  File C:\Documents and Settings\Garry\Desktop\recipe-576824-1.py,
line 50, in email_it_via_gmail
+ headers.get(Bcc, [])
TypeError: cannot concatenate 'str' and 'list' objects

Sorry, but I according to the recipe I don't need a Bcc.

Sorry, again, for such a simple question!

Cheers,

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


Re: [Tutor] Trouble with a Recipe ...

2009-08-25 Thread Garry Bettle
Many, many thanks Vern.

For the life of me, I couldn't remember [] are for lists:  I was trying with ().

Guess I picked a bad day to stop sniffing glue!

Cheers,

Garry

On Tue, Aug 25, 2009 at 15:55, Vern Cedervce...@canterburyschool.org wrote:
 At the top in the docstring it says, 'To, Cc and Bcc values must be
 *lists*'.

 That means instead of To:  garry.bet...@gmail.com, you need To:
 [garry.bet...@gmail.com] i.e. a list containing the destination address.
 That's so that you could send to, cc and bcc more than one address.

 HTH,

 Vern


 Garry Bettle wrote:

 Hi,

 Hope this email finds everyone well - roll on the weekend.

 I'm trying to run http://code.activestate.com/recipes/576824/

 I'm in IDLE and I try:

 email_it_via_gmail( {To: garry.bet...@gmail.com, Subject:
 Testing, From: garry.bet...@gmail.com}, text=Testing)

 but I get the following error:

 Traceback (most recent call last):
  File pyshell#17, line 1, in module
    email_it_via_gmail( {To: garry.bet...@gmail.com, Subject:
 Testing, From: garry.bet...@gmail.com}, text=Testing)
  File C:\Documents and Settings\Garry\Desktop\recipe-576824-1.py,
 line 50, in email_it_via_gmail
    + headers.get(Bcc, [])
 TypeError: cannot concatenate 'str' and 'list' objects

 Sorry, but I according to the recipe I don't need a Bcc.

 Sorry, again, for such a simple question!

 Cheers,

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

 --
 This time for sure!
   -Bullwinkle J. Moose
 -
 Vern Ceder, Director of Technology
 Canterbury School, 3210 Smith Road, Ft Wayne, IN 46804
 vce...@canterburyschool.org; 260-436-0746; FAX: 260-436-5137

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


[Tutor] Normal Distribution

2009-08-14 Thread Garry Bettle
Sorry!  Now with an appropriate subject line... G.

Howdy all,

Hope this email finds you all well - roll on the weekend.

I've been tasked with replicating an Excel spreadsheet.

The spreadsheet contains cells that use Excels' NORMDIST() function.

I have all 3 variables required - value, mean and standard deviation -
but does anyone already have equivalent python code for this function?

Many thanks!

Cheers,

Garry

PS:  Excel Help:

NORMDIST Returns the normal distribution for the specified mean and
standard deviation. This function has a very wide range of
applications in statistics, including hypothesis testing.

Syntax

NORMDIST(x,mean,standard_dev,cumulative)

X   is the value for which you want the distribution.

Mean   is the arithmetic mean of the distribution.

Standard_dev   is the standard deviation of the distribution.

Cumulative   is a logical value that determines the form of the
function. If cumulative is TRUE, NORMDIST returns the cumulative
distribution function; if FALSE, it returns the probability mass
function.

Remarks

If mean or standard_dev is nonnumeric, NORMDIST returns the #VALUE!
error value.
If standard_dev ≤ 0, NORMDIST returns the #NUM! error value.
If mean = 0, standard_dev = 1, and cumulative = TRUE, NORMDIST returns
the standard normal distribution, NORMSDIST.
When cumulative = TRUE, the formula is the integral from negative
infinity to x of the given formula.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Quick question regarding Parsing a Delimited string

2009-07-08 Thread Garry Bettle
Hi,

I've been programming for over 20 yrs, but only the last few in python
and then only in dribs and drabs.

I'm having a difficult time parsing a delimited string.

e.g.

100657641~GBP~ACTIVE~0~1~~true~5.0~1247065352508~:
3818854~0~24104.08~4.5~~22.1~false|4.4~241.67~L~1~4.3~936.0~L~2~4.2~210.54~L~3~|4.5~19.16~B~1~4.6~214.27~B~2~4.7~802.13~B~3~:
3991404~1~19974.18~4.7~~21.7~false|4.6~133.01~L~1~4.5~124.83~L~2~4.4~319.33~L~3~|4.7~86.61~B~1~4.8~247.9~B~2~4.9~142.0~B~3~:
4031423~2~15503.56~6.6~~15.1~false|6.6~53.21~L~1~6.4~19.23~L~2~6.2~53.28~L~3~|6.8~41.23~B~1~7.0~145.04~B~2~7.2~37.23~B~3~

That is just a selection of the full string - and I've broken it up
for this email.  It's delimited by : and then by ~ and finally, in
some cases, | (a pipe).

If the string is called m, I thought I could create a list with
m.split(:).  I would like to then first of all find in this list the
entry beginning with e.g. 3991404.

I thought I could pop each item in the list and compare that seems
pretty long winded.

When the ItemFound is now =
'3991404~1~19974.18~4.7~~21.7~false|4.6~133.01~L~1~4.5~124.83~L~2~4.4~319.33~L~3~|4.7~86.61~B~1~4.8~247.9~B~2~4.9~142.0~B~3~:'

I would like to return the 3rd item delimited with ~, which in this case, is 4.7

Can anyone help?

Many thanks!

Cheers,

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


Re: [Tutor] Quick question regarding Parsing a Delimited string

2009-07-08 Thread Garry Bettle
On Wed, Jul 8, 2009 at 21:37, Kent Johnsonken...@tds.net wrote:
 On Wed, Jul 8, 2009 at 1:22 PM, Rich Lovelyroadier...@googlemail.com wrote:

 If you really want to speed up the search, you could turn the list of lists
 into a dict, using the first value in each sublist as a key:

 dct = dict((i[0], i[1:]) for i in lst)

 Then you can access it using the normal dictionary interface.
 dct[3991404][3]

 I'm suspicious of this claim if there is only one lookup needed. You
 iterate the whole list and incur the overhead of constructing a dict.
 If there will be multiple lookups it may well be a win but as always
 there is no substitute for measurement if you want to know what is
 faster.

 Kent


Hi Kent,

No, I think the dict will be fine:  I have to find every one that is
in my original list.

Many thanks to both you and Rich for all the help.  I'll let you know
how I get on.

Cheers,

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


Re: [Tutor] python parser

2008-12-22 Thread Garry Bettle
On Mon, 22 Dec 2008 09:39:37 -0800, johnf wrote:
 Well I'm attempting to convert VFP and MsSQL (stored procedures and triggers)
 to Python and Postgres (functions and triggers).  Actually, python is very
 close to VFP and I would convert very well.  MsSQL is another question.

Hi John,

I hope this message finds you well.

You might be interested in the following:

http://dabodev.com/

Dabo is a 3-tier, cross-platform application development framework,
written in Python atop the wxPython GUI toolkit. And while Dabo is
designed to create database-centric apps, that is not a requirement.
Lots of people are using Dabo for the GUI tools to create apps that
have no need to connect to a database at all.

NB:  The VFP-angle is well and truely catered for:

Background

Dabo's authors, Ed Leafe and Paul McNett, have strong backgrounds in
database application development using the awesome and underrated
Microsoft Visual FoxPro development environment.

While Visual FoxPro shines at developing data-centric applications, it
has one limitation that cannot be ignored: it only runs on Microsoft
Windows, and Ed and Paul both have clients that want their
applications to run on Linux and Macintosh. We are sure we are not
alone in this regard: it is a multi-platform world with more diverse
needs than one vendor can fulfill.

Ed and Paul got to talking one day: Paul had been researching various
multiplatform GUI toolkits for about 18 months, and Ed has lots of
experience developing the Visual FoxPro Codebook framework. We decided
to work together to make a framework for developing robust
data-centric applications for multi-platform deployment. We've come up
with a design that is simple, flexible, and robust, and we've begun
developing our own client applications using the Dabo framework.

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


[Tutor] Pycron - Does anyone have experience with this?

2008-01-16 Thread Garry Bettle
Howdy all,

Hope this finds everyone well - roll on the weekend.

I've been using pycron for months now with no trouble at all.

It's a great scheduling program.

I'm now trying to schedule a file copy from a network share.

However, Pycron chokes, as it requires the share to fully qualified.

Instead of:

s:\subdir1\subdir2\file.ext

it wants:

\\servername\share\subdir1\subdir2\file.ext

But no matter how I try to do the file copy - batch file, WSH and
Win32API calls - it still refuses to run.

It seems a shame to have to look for another scheduling solution.

Can anyone help?

Many thanks!

Cheers,

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