[Tutor] Moving my C++ code to Python?

2010-10-15 Thread Paul
Hi there,

I've been using C/C++ for many years (python, just reading about it).

I have a software written in C/C++ but considering porting most of it
to python, as it seems like it's a better choice for decision making
portion of the code.  I'm also thinking about having a 'matlab' like
interface for reading, processing, and writing.

In my current C++ code, I would read data into a vector of structs
(which contains other simple vectors, strings, and structs) and it can
be as large as 500MB to 2GB.  The data gets processed (requires random
access).  The result is then written out.

I would like to make modules for python.  The problem is that the
vector of structs that is very large.  First, is it possible to pass
such structures around to and from python and C/C++?  What would be
the overhead cost of having a large structure that needs to be passed
to and from the C/C++ modules?

# I imagine I'd use the newly written software this way:
>>> import c_stuff  # my C/C++ module
>>> largestuff = c_stuff.read(file)  # read from disk
>>> c_stuff.process(largestuff, someparams1)  # processing, requires random 
>>> access to the vector
>>> c_stuff.process(largestuff, someparams2)
...
>>> c_stuff.process(largestuff, someparams1) # the whole thing may take a 
>>> few minutes to days
>>>
>>> import python_stuff # some module written in python to process the data as 
>>> well
>>>
>>> python_stuff.process(largestuff, otherparams1)  # It's important that this 
>>> data can be read (and I hope written) by python code
>>> python_stuff.process(largestuff, otherparams2)
>>>
>>> c_stuff.write(largestuff) #write result

Thank you in advance,
Paul
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Moving my C++ code to Python?

2010-10-15 Thread Paul
On Fri, Oct 15, 2010 at 3:07 PM, David Hutto  wrote:
> This isn't an answer to your question, but I'm doing about the same in
> reverse(it sounds like) as you're doing. More directly just processing
> data for 2-d/3d graphs using matplotlib and wxpython. So, I was

I'm afraid it is a different problem.  However, matplotlib and
xwpython seem like good tools I may use, once I know my porting is
possible (and hopefully easy).

> wondering if you think it's similar to those ends, and good enough to
> let someone else look at yet?

Not sure what you mean by "someone else look at yet?"

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


Re: [Tutor] Moving my C++ code to Python?

2010-10-17 Thread Paul
Thank you all for your kind suggestions.  It seem that cython and
boost.python both look like a good suggestions; SWIG seems like a good
tool, but may not meet my requirements.
Quick google seems to suggest that boost.python is 7 times slower [1],
however.  Any thoughts about speed performance between the two?

[1] 
http://blog.chrischou.org/2010/02/28/simple-benchmark-between-cython-and-boost-python/

Thank you,
Paul

And a link I found today.
[2] 
http://koichitamura.blogspot.com/2008/06/various-ways-to-integrate-python-and-c.html

On Sun, Oct 17, 2010 at 1:56 PM, Sithembewena Lloyd Dube
 wrote:
> Also have a look at Boost.Python:
>
> http://www.boost.org/doc/libs/1_44_0/libs/python/doc/index.html
>
> On Fri, Oct 15, 2010 at 11:29 PM, Paul  wrote:
>>
>> Hi there,
>>
>> I've been using C/C++ for many years (python, just reading about it).
>>
>> I have a software written in C/C++ but considering porting most of it
>> to python, as it seems like it's a better choice for decision making
>> portion of the code.  I'm also thinking about having a 'matlab' like
>> interface for reading, processing, and writing.
>>
>> In my current C++ code, I would read data into a vector of structs
>> (which contains other simple vectors, strings, and structs) and it can
>> be as large as 500MB to 2GB.  The data gets processed (requires random
>> access).  The result is then written out.
>>
>> I would like to make modules for python.  The problem is that the
>> vector of structs that is very large.  First, is it possible to pass
>> such structures around to and from python and C/C++?  What would be
>> the overhead cost of having a large structure that needs to be passed
>> to and from the C/C++ modules?
>>
>> # I imagine I'd use the newly written software this way:
>> >>> import c_stuff  # my C/C++ module
>> >>> largestuff = c_stuff.read(file)  # read from disk
>> >>> c_stuff.process(largestuff, someparams1)  # processing, requires
>> >>> random access to the vector
>> >>> c_stuff.process(largestuff, someparams2)
>> ...
>> >>> c_stuff.process(largestuff, someparams1) # the whole thing may
>> >>> take a few minutes to days
>> >>>
>> >>> import python_stuff # some module written in python to process the
>> >>> data as well
>> >>>
>> >>> python_stuff.process(largestuff, otherparams1)  # It's important that
>> >>> this data can be read (and I hope written) by python code
>> >>> python_stuff.process(largestuff, otherparams2)
>> >>>
>> >>> c_stuff.write(largestuff) #write result
>>
>> Thank you in advance,
>> Paul
>> ___
>> Tutor maillist  -  tu...@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>
>
>
> --
> Regards,
> Sithembewena Lloyd Dube
> http://www.lloyddube.com
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] From Newbie

2008-06-22 Thread paul
hi Danny,As far as i am aware you must declare your variable first, something like a=0The same would go for shope that helpspaulOn Sun Jun 22 10:45 , Danny Laya  sent:Hi ! I have learned wiki tutor for non-programmer and I found some hill that stopping me. In  Non-Programmer's Tutorial for Python/Count to 10, wiki ask me to write this code :a = 1s = 0print 'Enter Numbers to add to the sum.'print 'Enter 0 to quit.'while a != 0:print 'Current Sum:', sa = int(raw_input('Number? '))s = s + aprint 'Total Sum =', sBut when i write while a != 0: and then i press enter, python terminal tell me :>>> while a ! = 0:  File "", line 1while a ! = 0:^SyntaxError: invalid syntaxCan you
 find my mistake, guys ? Sorry to bother you, I try to find the answer in google, but I can't found the answer.Please help me soon guys, whatever your answer. If you don'twant to answer my question, please give me some site that couldanswer this newbie question. Thank's.  

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


[Tutor] Classes

2008-07-11 Thread paul
hi,I have two questions please:1. What is the difference between a classmethod and a staticmethod, and when would i use either?2. I want to create an object, presumably a class, to handle attributes of an open gl object.For example, if i wanted to create a 2d square i need 4 points, each with an x,y co-ord, and each point could also have an optional colour, in the form (1.0, 1.0, 1.0), each of these items is then passed to the 'drawing' function to actually draw the square or polygon etc.My question is how do i design the appropriate class to do this, i have coded these with lists at the moment, egdef vertex_position():
    vp_r = raw_input('Please enter the co-ordinate for each vertex ')
    vp_m = map(int, vp_r.split(','))
    return vp_m

and simply call vp_m[i] for the various vertices, however i think that the class method should be simpler.My main problem is that i need, pairs of co-ords for the vertices and tuples for the colour and am not sure how to code/return this using classes, the other issue realting to this is that the open gl uses the same attribute name for each, ie glVertex2i and glColor3f, and am not sure how to pass this correctly, i have included my open gl below to show how i reference it using the lists at the moment,    glColor3f(1.0, 0.0, 0.0)
    glVertex2i(l[0], l[1])
    glColor3f(0.0, 1.0, 0.0)
    glVertex2i((l[0] + width), l[1])
    glColor3f(0.0, 0.0, 1.0)
    glVertex2i((l[0] + width), (l[1] + height))
    glColor3f(1.0, 1.0, 1.0)
    glVertex2i(l[0], (l[1] + height))
    glEnd() This is based on a square, but i would like to change the number of vertices using a for loop, i am just unsure how to get the variables into the correct places.thankspaul   
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] about real-time audio synthesis and algorithmic composition

2015-07-04 Thread Paul
Hi !
I'm a rookie to programming, and just graduated from a conservatory of music 
last year. I'm interested in real-time audio synthesis and algorithmic 
composition, I can write a little bit SuperCollider and Django. I just buy a 
book called "Python Programming for the Absolute Beginner" (Chinese version), 
and try to learn Python via it.

I have a query, Is there have a way (e.g. import some library) to synthesis 
sound in Python as SuperCollider? (synthesis sound via write some code in a 
file, and run it will hear the result immediately.)

I'm realy like Python's style, and want to learn Programming from Python. If I 
can learn it through synthesis sound, I will be very happy. :-) In addition, I 
think if I can synthesis sound via Python Code, I will have more chance to 
cooperate with other programer. :-) 

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


[Tutor] Searching in a file

2010-01-13 Thread Paul Melvin
Hi,

I have a file generated from a webpage.

I want to search that file for a specific keyword, in my case 'NEW'.

Once I have found that keyword I want to retrieve information below it, e.g.
web link, size of file etc.

When I have this information I move off until I find another 'NEW' and the
process starts all over.

Please can someone give me some pointers on how to do this.

I can find the line containing 'NEW' which I get using a simple for loop on
every line in the file, but how do I then traverse the next x amount of
lines, taking what I want until either the next 'NEW' or eof.

e.g.

for line in file:
if re.findall('NEW', line)  # or search
list.append(line)   # to do something to later

I cannot 'get' to the following lines because I would need to get out of the
loop.

I did use enumerate and was wondering if I could use the line number, or
maybe I could use an re iterator.

Any advice would be much appreciated.

Thanks

paul
 

__ Information from ESET Smart Security, version of virus signature
database 4767 (20100113) __

The message was checked by ESET Smart Security.

http://www.eset.com
 

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


[Tutor] Searching in a file

2010-01-15 Thread Paul Melvin
Hi,

Thanks very much to all your suggestions, I am looking into the suggestions
of Hugo and Alan.

The file is not very big, only 700KB (~2 lines), which I think should be
fine to be loaded into memory?

I have two further questions though please, the lines are like this:


Revenge
(2011)



5 days 


65 minutes 

Etc with a chunk (between each NEW) being about 60 lines, I need to extract
info from these lines, e.g. /browse/post/5354361/ and Revenge (2011) to pass
back to the output, is re the best option to get all these various bits,
maybe a generic function that I pass the search strings too?

And if I use the split suggestion of Alan's I assume the last one would be
the rest of the file, would the next() option just let me search for the
next /browse/post/5354361/ etc after the NEW? (maybe putting this info into
a list)

Thanks again

paul
 

__ Information from ESET Smart Security, version of virus signature
database 4773 (20100114) __

The message was checked by ESET Smart Security.

http://www.eset.com
 

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


[Tutor] Still searching in html files

2010-01-21 Thread Paul Melvin
Hi,

 

I am still looking for information in these files and have a sort of
'clunky' solution that I would like feedback on please.

 

The code is at http://python.codepad.org/S1ul2bh7 and the bit I would like
some advice on is how to get the sorted data and where to put it.

 

Currently I use a temporary list to store the data before writing it to
another, is this OK? (lines 45 onwards)

 

I want to expand the program to give some output which the user can interact
with, e.g. 'which of this information do you want me to get?' and the user
would choose.  Does anyone have any tips on this? Can I just use things like
raw_input and lists?

 

Thanks

 

paul

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


[Tutor] Python 3.0

2010-02-20 Thread Paul Whittaker
Using the following script, the (Windows 7) PC echoes the key presses but
doesn't quit (ever!)

 

import msvcrt

 

print('Press SPACE to stop...')

 

while True:

k = msvcrt.getch() # Strangely, this echoes to the IDLE window

if k == ' ':   # Not recognized

break

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


[Tutor] Having a return when subprocess.Popen finishes

2010-07-08 Thread Paul VanGundy
Hi All,

I'm trying to get data from subprocess.Popen. To be specific, I am
trying to read a ping echo and take the output and assign it to a
variable like below:

ping = subprocess.Popen("ping -c 5 %s" % (server),
stdout=subprocess.PIPE, shell=True)

However, when I run the command the output that gets assigned to my ping
variable is something along the lines of '' and am not returned to >>> prompt. I know that is the proper
output but I need to be able to capture the ping replies and assign
those to a variable. I tried adding a \r and \n at the end of my cmd.
Any help would be greatly appreciated. I'm open to improvements,
different ways of doing it and corrections. :) If more info is needed
let me know. Thanks.

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


Re: [Tutor] Picking up citations

2009-02-10 Thread Paul McGuire
Dinesh and Kent -

I've been lurking along as you run this problem to ground.  The syntax you
are working on looks very slippery, and reminds me of some of the issues I
had writing a generic street address parser with pyparsing
(http://pyparsing.wikispaces.com/file/view/streetAddressParser.py).  Mailing
list companies spend beaucoup $$$ trying to parse addresses in order to
filter duplicates, to group by zip code, street, neighborhood, etc., and
this citation format looks similarly scary.  

Congratulations on getting to a 95% solution using PLY.

-- Paul

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


Re: [Tutor] extracting phrases and their memberships from syntax

2009-02-13 Thread Paul McGuire
Pyparsing has a built-in helper called nestedExpr that fits neatly in with
this data.  Here is the whole script:

from pyparsing import nestedExpr

syntax_tree = nestedExpr()
results = syntax_tree.parseString(st_data)

from pprint import pprint
pprint(results.asList())


Prints:

[[['S',
   ['NP-SBJ-1',
['NP', ['NNP', 'Rudolph'], ['NNP', 'Agnew']],
[',', ','],
['UCP',
 ['ADJP', ['NP', ['CD', '55'], ['NNS', 'years']], ['JJ', 'old']],
 ['CC', 'and'],
 ['NP',
  ['NP', ['JJ', 'former'], ['NN', 'chairman']],
  ['PP',
   ['IN', 'of'],
   ['NP',
['NNP', 'Consolidated'],
['NNP', 'Gold'],
['NNP', 'Fields'],
['NNP', 'PLC'],
[',', ',']],
   ['VP',
['VBD', 'was'],
['VP',
 ['VBN', 'named'],
 ['S',
  ['NP-SBJ', ['-NONE-', '*-1']],
  ['NP-PRD',
   ['NP', ['DT', 'a'], ['JJ', 'nonexecutive'], ['NN', 'director']],
   ['PP',
['IN', 'of'],
['NP',
 ['DT', 'this'],
 ['JJ', 'British'],
 ['JJ', 'industrial'],
 ['NN', 'conglomerate']]],
   ['.', '.'

If you want to delve deeper into this, you could, since the content of the
() groups is so regular.  You in essence reconstruct nestedExpr in your own
code, but you do get some increased control and visibility to the parsed
content.

Since this is a recursive syntax, you will need to use pyparsing's mechanism
for recursion, which is the Forward class.  Forward is sort of a "I can't
define the whole thing yet, just create a placeholder" placeholder.

syntax_element = Forward()
LPAR,RPAR = map(Suppress,"()")
syntax_tree = LPAR + syntax_element + RPAR

Now in your example, a syntax_element can be one of 4 things:
- a punctuation mark, twice
- a syntax marker followed by one or more syntax_trees
- a syntax marker followed by a word
- a syntax tree

Here is how I define those:

marker = oneOf("VBD ADJP VBN JJ DT PP NN UCP NP-PRD "
"NP NNS NNP CC NP-SBJ-1 CD VP -NONE- "
"IN NP-SBJ S")
punc = oneOf(", . ! ?")

wordchars = printables.replace("(","").replace(")","")

syntax_element << (
punc + punc | 
marker + OneOrMore(Group(syntax_tree)) | 
marker + Word(wordchars) |
syntax_tree )

Note that we use '<<' operator to "inject" the definition of a
syntax_element - we can't use '=' or we would get a different expression
than the one we used to define syntax_tree.

Now parse the string, and voila!  Same as before.

Here is the entire script:

from pyparsing import nestedExpr, Suppress, oneOf, Forward, OneOrMore, Word,
printables, Group

syntax_element = Forward()
LPAR,RPAR = map(Suppress,"()")
syntax_tree = LPAR + syntax_element + RPAR

marker = oneOf("VBD ADJP VBN JJ DT PP NN UCP NP-PRD "
"NP NNS NNP CC NP-SBJ-1 CD VP -NONE- "
"IN NP-SBJ S")
punc = oneOf(", . ! ?")

wordchars = printables.replace("(","").replace(")","")

syntax_element << (
punc + punc | 
marker + OneOrMore(Group(syntax_tree)) | 
marker + Word(wordchars) |
syntax_tree )

results = syntax_tree.parseString(st_data)
from pprint import pprint
pprint(results.asList())

-- Paul


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


Re: [Tutor] extracting phrases and their memberships from syntax

2009-02-14 Thread Paul McGuire
-Original Message-
From: Emad Nawfal (عماد نوفل) [mailto:emadnaw...@gmail.com] 
Sent: Saturday, February 14, 2009 8:59 AM
To: Paul McGuire
Cc: tutor@python.org
Subject: Re: [Tutor] extracting phrases and their memberships from syntax


Thank you so much Paul, Kent, and Hoftkamp.
I was asking what the right tools were, and I got two fully-functional
scripts back. Much more than I had expected.

I'm planning to use these scripts instead of the Perl one. I've also started
with PyParsing as it seems to be a little easier to understand than PLY.


---
Glad to help - actually, I sent 2 working scripts, and Kent sent 1, so I
count THREE fully-functional scripts! ;)  Also, Kent's recursive example
code for traversing the returned parse tree should also work with the data
structure that the pyparsing example returns.

Please make use of the pyparsing samples and documentation available at the
pyparsing wiki - pyparsing.wikispaces.com.  Also, if you use the Windows
binary or easy_install installations, you don't get the help and example
directories - download the source ZIP distribution, and you get a lot more
helpful information.

-- Paul

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


Re: [Tutor] Looking for ConfigObj Documentation

2009-02-20 Thread Paul McGuire
Has anyone already mentioned the article in Python Magazine, May, 2008?

-- Paul

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


Re: [Tutor] regex help

2009-02-23 Thread Paul McGuire
I second Alan G's appreciation for a well-thought-through and well-conveyed
description of your text processing task.  (Is "Alan G" his gangsta name, I
wonder?)

This pyparsing snippet may point you to some easier-to-follow code,
especially once you go beyond the immediate task and do more exhaustive
parsing of your syllable syntax.


from pyparsing import *

LT,GT = map(Suppress,"<>")
lower = oneOf(list(alphas.lower()))
H = Suppress("H")

# have to look ahead to only accept lowers if NOT followed by H
patt = LT + H + ZeroOrMore(lower + ~H)("body")  + lower + H + GT

tests = """\
a b c h
a b c
a b c""".splitlines()

for t in tests:
print t
print sum((list(p.body)
for p in patt.searchString(t) if p.body), [])
print

Prints:

a b c h
['d', 'e', 'f', 'i', 'j']

a b c
[]

a b c
['d']

There is more info on pyparsing at http://pyparsing.wikispaces.com.

-- Paul



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


Re: [Tutor] Extract strings from a text file

2009-02-27 Thread Paul McGuire
Emad wrote: 
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Since I'm learning Pyparsing, this was a nice excercise. I've written this
elementary script which does the job well in light of the data we have

from pyparsing import *
ID_TAG = Literal("")
FULL_NAME_TAG1 = Literal("")
END_TAG = Literal("', 'Joseph', '', 'Joseph', 'Smith', '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Welcome to the world of pyparsing!  Your program is a very good first cut at
this problem.  Let me add some suggestions (more like hints toward more
advanced concepts in your pyparsing learning):
- Look into Group, as in Group(OneOrMore(word)), this will add organization
and structure to the returned results.
- Results names will make it easier to access the separate parsed fields.
- Check out the makeHTMLTags and makeXMLTags helper methods - these do more
than just wrap angle brackets around a tag name, but also handle attributes
in varying order, case variability, and (of course) varying whitespace - the
OP didn't explicitly say this XML data, but the sample does look suspicious.

If you only easy_install'ed pyparsing or used the binary windows installer,
please go back to SourceForge and download the source .ZIP or tarball
package - these have the full examples and htmldoc directories that the
auto-installers omit.

Good luck in your continued studies!
-- Paul

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


Re: [Tutor] modular program

2009-03-01 Thread Paul McGuire
You can look at a site called UtilityMill (http://utilitymill.com/) that
hosts user-defined Python code, and wraps it in an API to be used
interactively through an HTML form, or programmatically over HTTP.  I'm
pretty sure that the author makes the source code available for this site.

Also, you could look at a particular utility on UtilityMill, the Trading
Strategy Evaluator
(http://utilitymill.com/utility/trading_strategy_evaluator).  This accepts a
Python function in an HTML form text input field, and then runs that
function as part of a trading simulation.  (Since this a utility hosted
within UtilityMill, the source code is publicly viewable.)

-- Paul

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


Re: [Tutor] combining Python 2.x and Python 3

2009-03-18 Thread Paul McGuire
These are very interesting links, and I just downloaded the evoque code to
see how they handled the one "impossible" case that has stymied me in
supporting pyparsing under Python 2.x (pre-2.6) and Python 3.0 with a single
code base: exception handling.

For those new to this topic, here is the problem.  Python 2.x uses this
syntax (code in []'s is optional, syntax-wise):

try:
... body of try block ...
except exception_type[, exception_var]:
... body of except block ...

In Python 3.0, this has become:

try:
... body of try block ...
except exception_type[as exception_var]:
... body of except block ...


Searching through evoque's code, I found that nearly all excepts are of the
form:

except exception_type:

(3 are bare except:'s - eek!)

Unfortunately, pyparsing makes *heavy* use of exceptions for managing the
navigation through the parsing alternatives defined in the users parser
expression.  I have already removed all cases of "except exc_type, exc_var:"
where the exc_var is not used in the exception handling block, but there are
still some cases where I *must* have the actual exception that was raised.

I posted to Stephan Deibel's blog a comment asking how he resolved this -
here is his reply:
>>>
Instead of the version-specific except clause forms I use just 'except
SomeException:' or 'except:' and then use sys.exc_info() inside the except
block to get at the exception information. That returns a three-value tuple,
the second element of which is the same thing you would get for 'e' in
'except SomeException, e:' or 'except SomeException as e:'.
>>>

Well, this is certainly promising, but pyparsing already has performance
challenges, and adding another function call in an exception handler to work
around this syntax issue is not that attractive.  My rough timing shows that
calling exc_info to extract the exception variable incurs a ~30% performance
penalty over using "except exc_type, exc_var:".  Note that adding this code
would impact not only the Python 3.0 users, but would also degrade
performance for Python 2.x users using this code base.  I can certainly use
this trick in those parts of pyparsing that are not performance-critical
(when building the parser to begin with for instance, as opposed to the
actual parsing time).  But in the parse-time code, I'm unwilling to add 30%
to my users' parsing time.

-- Paul

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


Re: [Tutor] Here's something to talk about

2009-04-15 Thread Paul McGuire
1. Python is not Java (see Philip Eby's blog entry
http://dirtsimple.org/2004/12/python-is-not-java.html).  Let go of your
concepts that only Items can go into an ItemCollection - Python already has
some perfectly good collection builtins.  Instead of writing a custom
ItemCollection, why not write a generic Exporter that takes any Python
sequence (such as a list, set, tuple, etc., anything that supports iter)?

2. SQLExporter does not use recommended form for substituting values into an
SQL statement.  It is preferred that one use the 2-argument form of execute,
in which the first argument contains '?' or '%s' placeholders, and the
second argument is a tuple of values.  That is, instead of:

first, last = "Bobby", "Tables"
cursor.execute("INSERT into STUDENT (firstname, lastname) values
('"+first+"','"+last+"')")

Do:

first, last = "Bobby", "Tables"
cursor.execute("INSERT into STUDENT (firstname, lastname) values (?,?)",
(first,last))

No need for wrapping in quotes, already handles values with embedded quotes,
and no SQL injection jeopardy (http://xkcd.com/327/).   This slightly
complicates your SQL exporter, it would have to return a tuple containing
the INSERT statement and the tuple of substitution values, instead of just a
string to be passed to execute.

3. The Pythonic way to create a comma-separated string of values from a list
of strings is:  ','.join(value_list).  If you have a list of tuples and want
two comma-separated strings, do: keystring, valstring = (','.join(s) for s
in zip(*key_value_tuple_list))

4. While I am not a slave to PEP8, your mixed case method names with leading
capitals look too much like class names and/or .Net method names.

In general, I find your approach too intrusive into the objects you would
like to export or load.  I would prefer to see a Loader and/or Exporter
class that takes my own application object, configured with table name and
field names, and then creates the proper XML or SQL representation.

-- Paul


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


Re: [Tutor] Here's something to talk about

2009-04-15 Thread Paul McGuire
Ronald -

I really encourage you to try to embrace some of the basic Python idioms as
part of your Java->Python journey:

1. Iterators

for item in list_of_items:
# do something with item

Is all that is needed to visit each item in a Python list.  Your verbose
MoveFirst, MoveNext, if more <> None, etc. is completely subsumed into the
built-in machinery Python has for iterators. (4-space source indent is also
the norm.)


2. Static typing

Change that snippet to:

def visit_each_item(collection):
for item in collection:
# do something with item, like printing its name

And this code works for lists, tuples, and sets of items, or dicts with
items as keys (assuming an item is hashable).  (The old method would accept
non-lists, too, but naming a parameter 'list_of_items' sounds like only
lists are intended.)  And the items in the list/tuple/set/whatever don't
even have to be all the same type, as long as they all support the concept
of "doing something".  Such as:

def print_each_items_name(collection):
for item in collection:
print item.name

The Java approach is to define an interface called Nameable (or INameable?),
with methods GetName and SetName.

You want to talk about self-maintaining code?  I wrote code like this before
sets were even added to the language definition, and when this new data type
came along, my code library handled it without changing a single line!  Good
thing that I didn't try to hack in some hare-brained run-time type safety
like:

if isinstance(collection,(list,tuple,dict)):
for item in collection:
... blah blah...

Or I would have to chase down all such code and add 'set' to the list.


3. Interfaces

Interfaces are there to allow compilers to verify that a static data type
will comply with an interface's requirements at runtime.  Python skips this
and just does the runtime check - if the object does not have the necessary
attribute (and methods are just callable attributes), then an AttributeError
gets thrown.  If you have a design that calls for inheritance from a pure
virtual abstract base class, then in Python, that base class is completely
unnecessary.  Sometimes in my code I will still include such a base class,
for purposes of documentation or to help me remember later what the heck was
I thinking?  But such a thing is for my benefit, not Python's.


4. Getters and Setters

This is such a fundamental dogma for Java developers that it may be
difficult for you to let go.  But do so, please!  Assume that rootTag in
your XMLExporter is a public variable, which you later must change to a
method call (maybe to ensure that the given tag is actually a kosher XML tag
string, and not something like "123...@^^!!!") - this is where the Python
property allows you to map set and get access to an attribute through a pair
of methods, while external code continues to access the property by its
public name, "rootTag".  No additional refactoring or recoding or
recompiling necessary.  Meanwhile, you can trim significant fat of "just in
case I need to make this a method someday" code from your classes, and just
use the attributes directly.


5. New concepts in Python

Python has some exciting features related to its built-in support for
collections that makes creating and working with them a dream.  Read up on
list comprehensions, generator expressions, iterators, and generators.  You
won't need them right away, but the sooner you get them, the sooner you'll
be able to dump verbose (and slow!) code like:

return_list = []
for blah in somecollection:
return_list.append(something_about(blah))

and write:

return_list = [something_about(blah) for blah in somecollection]

Not only is this more compact and concise, it also runs faster.  Also, learn
about zip and itertools - these will make your programs just fly.


Please try to hold off on the Newbie's Hubris.  The code that you posted
doesn't really present anything novel, or particularly awesome.  Instead,
think about this:

class ApplicationClass(object):
# objects of this class have no attributes - yet!
pass

def makeExportable(obj, exporter):
obj.exporter = exporter

a = ApplicationClass()
makeExportable(a, SQLExporter)

Now I have added exportability to an instance, without changing the class at
all.  This might even be a class from a 3rd party lib, whose source code I
don't even have!

Welcome to Python, but check your Java baggage at the door!

-- Paul

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


Re: [Tutor] Looping

2009-04-20 Thread Paul McGuire
> 
> xrange was a kludge to improve on range's memory efficiency
> but it is a horrible name that obscures the code.
> 
> Also it does not exist in v3 so if you use it you will need to change
> the code for v3. It is as well to be as consistent with v3 as possible
> IMHO
> 
> Alan G

I have felt this way also (i.e., "xrange was a kludge" and "it is a horrible
name"), and have resisted the general adoption of xrange in my code.
Fortunately, I rarely use range, but iterate over sequences directly; or if
I absolutely need the item's index, I iterate over enumerate(seq).

But even if you use "range" exclusively, you may need to change code when
migrating to Python 3.  In Py2, range returns a list; in Py3 range returns
an iterator (a la Py2-xrange's behavior).  If you have code that uses the
value returned from range as a list, then in Py3 you will need to explicitly
convert it to a list using "list(range(n))".

I concur with Alan's suggestion that you code to Py3 semantics, but there
are certainly cases where xrange makes sense vs. range (such as doing some
sort of repetitive test 1e8 times, there is no sense in creating a
100-million entry list just to iterate over it).  So I'll propose some
usages for those who use range:

1. For Py2-Py3 range-xrange compatibility, add this code to the top of your
Python scripts:

try:
range = xrange
except NameError:
pass

In this way, your code under Py2 will use xrange whenever you call range,
and you will adopt the future-compatible range behavior.  (Hmm, maybe this
would have been a good option to add to the "future" module...)

2. In all cases where you really must use the result returned from range as
a list, ALWAYS write this as "list(range(n))", as in:

nonnegative_numbers_up_to_but_not_including_10 = list(range(10))
even_numbers_up_to_but_not_including_20 = list(range(0,20,2))

Using the "list(range(n))" form when you actually need a list will prepare
you for the change in idiom when you upgrade to Py3, and is fully Py2
compatible (even if you don't first bind xrange to range as in suggestion 1
- it simply copies the original list).  It also makes it very explicit that
you REALLY WANT THE RANGE AS A LIST, and not just as something for counting
up to n.

-- Paul


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


Re: [Tutor] Tokenizing Help

2009-04-23 Thread Paul McGuire
For the given test case, this pyparsing sample parses the data, without
having to anticipate all the possible 2-letter keys.  

from pyparsing import *

integer = Word(nums)
DASH = Literal('-').suppress()
LT = Literal('<').suppress()
GT = Literal('>').suppress()

entrynum = LT + integer + GT
keycode = Word(alphas.upper(),exact=2)
key = GoToColumn(1).suppress() + keycode + DASH
data = Group(key("key") + Empty() + SkipTo(key | entrynum |
StringEnd())("value"))
entry = entrynum("refnum") + OneOrMore(data)("data")

for e in entry.searchString(test):
print e.refnum
for dd in e.data:
print dd.key,':', dd.value
print


Prints:

['567']
['AU'] : Bibliographical Theory and Practice - Volume 1 - The AU  - Tag 
and its applications
['AB'] : Texts in Library Science

['568']
['AU'] : Bibliographical Theory and Practice - Volume 2 - The
['AB'] : Tag and its applications
['AB'] : Texts in Library Science

['569']
['AU'] : AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU  -
['AU'] : AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU
['AB'] : AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU  -
['AU'] : AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU  - AU
['ZZ'] : Somewhat nonsensical case


If you find that you have to also accept keycodes that consist of a capital
letter followed by a numeric digit (like "B7"), modify the keycode
definition to be:

keycode = Word(alphas.upper(), alphanums.upper(), exact=2)


-- Paul


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


Re: [Tutor] Iterating over a long list with regular expressions and changing each item?

2009-05-04 Thread Paul McGuire
Original:
 'case_def_gen':['case_def','gen','null'],
 'nsuff_fem_pl':['nsuff','null', 'null'],
 'abbrev': ['abbrev, null, null'],
 'adj': ['adj, null, null'],
 'adv': ['adv, null, null'],}

Note the values for 'abbrev', 'adj' and 'adv' are not lists, but strings
containing comma-separated lists.

Should be:
 'case_def_gen':['case_def','gen','null'],
 'nsuff_fem_pl':['nsuff','null', 'null'],
 'abbrev': ['abbrev', 'null', 'null'],
 'adj': ['adj', 'null', 'null'],
 'adv': ['adv', 'null', 'null'],}

For much of my own code, I find lists of string literals to be tedious to
enter, and easy to drop a ' character.  This style is a little easier on the
eyes, and harder to screw up.

 'case_def_gen':['case_def gen null'.split()],
 'nsuff_fem_pl':['nsuff null null'.split()],
 'abbrev': ['abbrev null null'.split()],
 'adj': ['adj null null'.split()],
 'adv': ['adv null null'.split()],}

Since all that your code does at runtime with the value strings is
"\t".join() them, then you might as well initialize the dict with these
computed values, for at least some small gain in runtime performance:

 T = lambda s : "\t".join(s.split())
 'case_def_gen' : T('case_def gen null'),
 'nsuff_fem_pl' : T('nsuff null null'),
 'abbrev' :   T('abbrev null null'),
 'adj' :  T('adj null null'),
 'adv' :  T('adv null null'),}
 del T

(Yes, I know PEP8 says *not* to add spaces to line up assignments or other
related values, but I think there are isolated cases where it does help to
see what's going on.  You could even write this as:

 T = lambda s : "\t".join(s.split())
 'case_def_gen' : T('case_def  gen  null'),
 'nsuff_fem_pl' : T('nsuff null null'),
 'abbrev' :   T('abbrevnull null'),
 'adj' :  T('adj   null null'),
 'adv' :  T('adv   null null'),}
 del T

and the extra spaces help you to see the individual subtags more easily,
with no change in the resulting values since split() splits on multiple
whitespace the same as a single space.)

Of course you could simply code as:

 'case_def_gen' : T('case_def\tgen\t null'),
 'nsuff_fem_pl' : T('nsuff\tnull\tnull'),
 'abbrev' :   T('abbrev\tnull\tnull'),
 'adj' :  T('adj\tnull\tnull'),
 'adv' :  T('adv\tnull\tnull'),}

But I think readability definitely suffers here, I would probably go with
the penultimate version.

-- Paul


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


Re: [Tutor] Generating deck combinations

2009-06-20 Thread Paul McGuire
If you are looking for all possible 60-card deals of this deck, then you
also probably want to filter out duplicate deals caused by equivalent cards
exchanging places.  That is, say you had a deck of 3 cards: 2 Mountain, and
1 Vial, and you want to deal out al 3 in various order of cards.  Using the
recursive solutions, you will get:

(hmm, couldn't get either of the previous submissions to work..., well
something like this)

['Mountain', 'Mountain', 'Vial']
['Mountain', 'Vial', 'Mountain']
['Mountain', 'Mountain', 'Vial']
['Mountain', 'Vial', 'Mountain']
['Vial', 'Mountain', 'Mountain']
['Vial', 'Mountain', 'Mountain']


That is, because you have 2 Mountain cards, recursively shuffling this list
*looks* like you have two different list elements to process, but in fact,
they are equivalent so you will get duplicate deals.

Now imagine you have up to 150 different types of cards, in quantities of
1-10 of each, and this problem magnifies considerably.

Try this version (mildly tested):

def deal(cards, size):
if size > len(cards):
raise ValueError, "cannot deal more cards than are in the deck"

if size == 1:
for cd in cards:
yield [cd]
else:
for i,cd in enumerate(cards):
remainder = cards[:i] + cards[i+1:]
for d in deal(remainder,size-1):
yield [cd]+d


cardlist = [('Mountain', 2), ('Vial', 6), ]
allcards = sum(([card,]*qty for card,qty in cardlist), [])

# generate all unique deals of 'n' cards from allcards
# use the seenalready set to avoid reporting duplicate deals
n = 4
seenalready = set()

for d in deal(allcards,n):
newdeck = tuple(d)
if newdeck not in seenalready:
print d
seenalready.add(newdeck)

Here are all the 4-card deals from this pack of 8 cards:

['Mountain', 'Mountain', 'Vial', 'Vial']
['Mountain', 'Vial', 'Mountain', 'Vial']
['Mountain', 'Vial', 'Vial', 'Mountain']
['Mountain', 'Vial', 'Vial', 'Vial']
['Vial', 'Mountain', 'Mountain', 'Vial']
['Vial', 'Mountain', 'Vial', 'Mountain']
['Vial', 'Mountain', 'Vial', 'Vial']
['Vial', 'Vial', 'Mountain', 'Mountain']
['Vial', 'Vial', 'Mountain', 'Vial']
['Vial', 'Vial', 'Vial', 'Mountain']
['Vial', 'Vial', 'Vial', 'Vial']

(If order is not significant, then you could simplify this algorithm
accordingly, and you would get these results:
['Mountain', 'Mountain', 'Vial', 'Vial']
['Mountain', 'Vial', 'Vial', 'Vial']
['Vial', 'Vial', 'Vial', 'Vial']
)

This is definitely a brute-force approach (brute-force is actually my
favorite first approach), generating all possible deals and then filtering
duplicates using a set of "already been seen" deals.  A smarter algorithm
would generate only the unique deals.  If someone were paying me to be that
smart, maybe I'd work on it.

I'm guessing this is an attempt to optimize a deck for playing a card game
like Magic or Pokemon, etc.  Your plan is, given a set of 'n' cards in your
collection (presumbaly n>60, or there would be no problem to solve), to
compute all possible decks of 60 cards.  Then you have some function to
evaluate this deck, or perhaps simulate playing it against another deck.
You will then exhaustively run this simulation function against each deck to
find which deck from your collection of 100's of cards is the "best".

By the time your program has finished running, I suspect the sun will be a
cold, dark cinder.  But have fun with it.

-- Paul

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


Re: [Tutor] thesaurus - translation, actually

2009-07-10 Thread Paul McGuire
My favorite rendition of this is in the related field of text-to-speech.  In
early Object Management Group days, OMG conference proceedings were
available as transcripts, but there was substantial delay in getting these
out.  An attempt to automate this with TTS software of the time was
discarded quickly - the first conference had a session track on
configuration management, and every mention of "version control" was
transcribed as "virgin control" - a dubious prospect in any event.

-- Paul


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


Re: [Tutor] find and replace relative to an nearby search string

2009-07-13 Thread Paul McGuire
This is a good little pyparsing exercise.  Pyparsing makes it easy to define
the structure of a "Write { ( )* }" block, and use the names
given to the parsed tokens to easily find the "name" and "file" entries.


from pyparsing import (Literal, Word, alphanums, empty, restOfLine, dictOf)

# make up grammar for expression of a Write block (tiny bit of
# pyparsing magic: define value as "empty + restOfLine" instead
# of just plain restOfLine, because empty will advance the 
# parser past any whitespace before starting the capture of
# restOfLine)
WRITE = Literal("Write")
key = Word(alphanums+"_")
value = empty + restOfLine
write_block = WRITE + "{" + dictOf(key,value)("data") + "}"

# SIMPLE VERSION
# if the order of the output key-value pairs is not significant, 
# just modify the "file" entry of tokens.data, and iterate
# over tokens.data.items() to create the output
def modify_write1(tokens):
if tokens.data["name"] == "Write1":
tokens.data["file"] = NEW_FILE_VALUE

def tokens_as_string(tokens):
return ("Write {\n" + 
"\n".join(" %s %s" % (k,v) 
for k,v in tokens.data.items()) + "\n" +
"}")

# SLIGHTLY MORE COMPLICATED VERSION
# if the order of the key-value pairs must be preserved, then
# you must enumerate through tokens.data's underlying list, 
# to replace the value associated with "file"
def modify_write1(tokens):
if tokens.data["name"] == "Write1":
for i,(k,v) in enumerate(tokens.data):
if k == "file":
tokens.data[i][1] = NEW_FILE_VALUE
break

def tokens_as_string(tokens):
return ("Write {\n" + 
"\n".join(" %s %s" % (k,v) 
for k,v in tokens.data) + "\n" +
"}")

# Assign parse actions: for each write_block found, modify the 
# file entry if name is "Write1", and then reformat the parsed 
# tokens into a string of the right form (since the parser will 
# have turned the tokens into a data structure of keys and 
# values)
write_block.setParseAction(modify_write1, tokens_as_string)

# define NEW_FILE_VALUE, and use transformString to parse
# through the input text, and process every Write block found
NEW_FILE_VALUE = "/Volumes/raid0/Z353_002_comp_v27.%04d.cin"
print write_block.transformString(text)



If you assign your input test string to the variable 'text', you'll get this
output:

Write {
 file /Volumes/raid0/Z353_002_comp_v27.%04d.cin
 file_type cin
 name Write1
 xpos 13762
 ypos -364
}



Write {
 file /Volumes/raid0/Z353_002_comp_v27.%04d.cin
 colorspace linear
 raw true
 file_type exr
 name Write1
 selected true
 xpos -487
 ypos -155
}


Find out more about pyparsing at http://pyparsing.wikispaces.com.
Cheers,
-- Paul

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


Re: [Tutor] pyparsing complex search

2009-07-16 Thread Paul McGuire
Pedro -

If you are trying to extract a simple pattern like a numeric word followed
by an alpha word, I would suggest using one of the scanString or
searchString methods.  scanString is probably the better choice, since you
seem to need not only the matching tokens, but also the location within the
input string where the match occurs.

scanString(sourcestring) is a generator function.  For every match,
scanString returns:
- tokens - the matched tokens themselves (as a ParseResults object)
- startloc - starting location of the matched tokens
- endloc - ending location of the matched tokens

Here is your test program, rewritten to use scanString (I inserted the
number '75' in the source string so you can see the results of having
multiple matches):


from pyparsing import *

data = """23 different size pry bars
hammer the 75 pitchfork
pound felt paper staple the felt paper every to inches staple hammer"""

numbrword = Word(nums)
alphaword = Word(alphas)

for match,locn,endloc in (numbrword+alphaword).scanString(data):
num,wrd = match
st = data
print "Found '%s/%s' on line %d at column %d" % \
(num,wrd, lineno(locn , st ), col( locn , st ))
print "The full line of text was:"
print "'%s'" % line( locn , st )
print (" "*col( locn , st ))+"^"
print


With this output:

Found '23/different' on line 1 at column 1
The full line of text was:
'23 different size pry bars'
 ^

Found '75/pitchfork' on line 2 at column 12
The full line of text was:
'hammer the 75 pitchfork'
^

Look at the difference between this program and your latest version.  The
pattern you parsed for is simply "OneOrMore(Word(alphanums))" - to debug a
parse action, try using the decorator that comes with pyparsing,
traceParseAction, like this:

@traceParseAction
def reportACertainWord( st , locn , toks ):

and it will print out the tokens passed into and out of the parse action.
It would have shown you that you weren't just matching ['23', 'different'],
but the entire input string.


Using scanString is a little simpler than writing a parse action.  Also,
since scanString is a generator, it will return matches as it finds them.
So if you have this file:

123 paper clips
... 10Gb of intervening non-matching text...
456 paper planes

scanString will return the match for '123 paper' right away, before
processing the intervening 10Gb of non-matching text.

Best of luck in your pyparsing efforts!

-- Paul


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


Re: [Tutor] pyparsing complex search

2009-07-16 Thread Paul McGuire
> Hi Paul. Thanks. I would like to get better at pyparsing. Can you
> recommend a good resource for learning. I am finding that what info I
> have found on the subject (on the web) isn't really explaining it from
> the ground up as much as I would like.

Hrmmm, not sure what to recommend - I might just point you back to what you
have already tried.  Where are you looking and what are you finding?

The pyparsing source package comes with a directory of examples and htmldoc
class documentation.  If you just used the easy_install or the Win32 install
package, these directories get omitted - try downloading the source package
from SourceForge.

There is also quite a bit of info at the pyparsing wiki, at
http://pyparsing.wikispaces.com.  The Discussion tab on the home page has
over 1200 posted messages in various discussion threads of people seeking
help.  You can also visit the Documentation page for some links to
presentations that have been given on pyparsing.

For basic getting-started type stuff, I'd recommend:
- the PyCon'06 presentations I gave - 
Introduction to Pyparsing - http://wiki.python.org/moin/PyCon2006/Talks#1
An Interactive Adventure Game Engine Built Using Pyparsing -
http://wiki.python.org/moin/PyCon2006/Talks#4

- the OnLAMP article (free, but a little old now) -
http://onlamp.com/pub/a/python/2006/01/26/pyparsing.html

- the 3 articles I wrote for Python Magazine in the past year (not free,
you'll have to buy back-issues)
Writing a simple Interpreter/Compiler with Pyparsing -
http://pymag.phparch.com/c/issue/view/74
Advanced Pyparsing: Implementing a JSON Parser Using Results Names -
http://pymag.phparch.com/c/issue/view/79
Create Your Own Domain-Specific Language in Python With Imputil and
Pyparsing - http://pymag.phparch.com/c/issue/view/96

- the O'Reilly e-book I wrote (also not free, costs $10) - Getting Started
with Pyparsing - http://oreilly.com/catalog/9780596514235/

Does this help?

-- Paul

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


Re: [Tutor] how to get blank value

2009-07-28 Thread Paul McGuire
Ok, I've seen various passes at this problem using regex, split('='), etc.,
but the solutions seem fairly fragile, and the OP doesn't seem happy with
any of them.  Here is how this problem looks if you were going to try
breaking it up with pyparsing:
- Each line starts with an integer, and the string "ALA"
- "ALA" is followed by a series of "X = 1.2"-type attributes, where the
value part might be missing.

And to implement (with a few bells and whistles thrown in for free):

data = """48 ALA H = 8.33 N = 120.77 CA = 55.18 HA = 4.12 C = 181.50
104 ALA H = 7.70 N = 121.21 CA = 54.32 HA = 4.21 C =
85 ALA H = 8.60 N =  CA =  HA = 4.65 C =""".splitlines()


from pyparsing import *

# define some basic data expressions
integer = Word(nums)
real = Combine(Word(nums) + "." + Word(nums))

# use parse actions to automatically convert numeric 
# strings to actual numbers at parse time
integer.setParseAction(lambda tokens:int(tokens[0]))
real.setParseAction(lambda tokens:float(tokens[0]))

# define expressions for 'X = 1.2' assignments; note that the
# value might be missing, so use Optional - we'll fill in
# a default value of 0.0 if no value is given
keyValue = Word(alphas.upper()) + '=' + \
Optional(real|integer, default=0.0)

# define overall expression for the data on a line
dataline = integer + "ALA" + OneOrMore(Group(keyValue))("kvdata")

# attach parse action to define named values in the returned tokens
def assignDataByKey(tokens):
for k,_,v in tokens.kvdata:
tokens[k] = v
dataline.setParseAction(assignDataByKey)

# for each line in the input data, parse it and print some of the data
fields
for d in data:
print d
parsedData = dataline.parseString(d)
print parsedData.dump()
print parsedData.CA
print parsedData.N
print


Prints out:

48 ALA H = 8.33 N = 120.77 CA = 55.18 HA = 4.12 C = 181.50
[48, 'ALA', ['H', '=', 8.3301], ['N', '=', 120.77], ['CA', '=',
55.18], ['HA', '=', 4.1201], ['C', '=', 181.5]]
- C: 181.5
- CA: 55.18
- H: 8.33
- HA: 4.12
- N: 120.77
- kvdata: [['H', '=', 8.3301], ['N', '=', 120.77], ['CA', '=',
55.18], ['HA', '=', 4.1201], ['C', '=', 181.5]]
55.18
120.77

104 ALA H = 7.70 N = 121.21 CA = 54.32 HA = 4.21 C =
[104, 'ALA', ['H', '=', 7.7002], ['N', '=', 121.20],
['CA', '=', 54.32], ['HA', '=', 4.21], ['C', '=', 0.0]]
- C: 0.0
- CA: 54.32
- H: 7.7
- HA: 4.21
- N: 121.21
- kvdata: [['H', '=', 7.7002], ['N', '=', 121.20],
['CA', '=', 54.32], ['HA', '=', 4.21], ['C', '=', 0.0]]
54.32
121.21

85 ALA H = 8.60 N =  CA =  HA = 4.65 C =
[85, 'ALA', ['H', '=', 8.5996], ['N', '=', 0.0], ['CA', '=',
0.0], ['HA', '=', 4.6504], ['C', '=', 0.0]]
- C: 0.0
- CA: 0.0
- H: 8.6
- HA: 4.65
- N: 0.0
- kvdata: [['H', '=', 8.5996], ['N', '=', 0.0], ['CA', '=',
0.0], ['HA', '=', 4.6504], ['C', '=', 0.0]]
0.0
0.0


Learn more about pyparsing at http://pyparsing.wikispaces.com.

-- Paul


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


Re: [Tutor] how to get blank value

2009-07-28 Thread Paul McGuire
> -Original Message-
> From: amr...@iisermohali.ac.in [mailto:amr...@iisermohali.ac.in]
> Sent: Wednesday, July 29, 2009 12:13 AM
> To: Paul McGuire
> Cc: tutor@python.org
> Subject: Re: [Tutor] how to get blank value
> 
> Thanks for help Sir but with these commands it is showing error:-
> 
> ph08...@sys53:~> python trip.py
> Traceback (most recent call last):
>   File "trip.py", line 6, in 
> from pyparsing import *
> ImportError: No module named pyparsing
> 

Wow.  You are on your own from here, you will have to do *some* work for
yourself.  This problem should definitely be within the abilities of a
"Research Fellow" to solve.

Did you even see the link I added at the bottom of my earlier post?

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


[Tutor] need to hide process

2005-01-10 Thread Paul Tremblay
I am trying to hide a process from a python script and don't know if I
can do so.

The process is a pax command, and my unsucesful code is below.

I am trying to read from a pax archive (similar to a tar archive) to get
a list of files. As long as the pax archive is valid, the code below
works fine.

The problem arises when the pax archive is not valid. In this case, the
pax commnad gives this message:

ATTENTION! pax archive volume change required.
Ready for archive volume: 1
Input archive name or "." to quit pax.
Archive name > 

pax then waits for the user to type in a command.

I would like to supress both the message and the need for user input,
because whenever this message appears, it always means the pax archive
is corrupt. So ther is no need to see this confusing and misleading
message.

However, no matter what I have tried, I can't get away from pax both
displaying the message and demanding the user type in a command. Do I
need to fork a process? I have never done this, and the documentation is
confusing to me. 

Thanks

Paul



def make_verify_list(self, archive, write_obj):
if self.__main.verbose:
sys.stdout.write('Making file from archive...\n')
if self.__main.platform == 'darwin':
command = 'hfspax -f %s ' % archive
elif self.__main.platform == 'linux':
command = 'pax -f %s' % archive


junk_obj, read_obj, error_obj = os.popen3(command )
errors = []
line = 1
while 1:
line = read_obj.readline()
if line:
#  add a leading /, which was stripped away
line = '/%s' % line
line = line.replace('//', '/')
index = line.find('\/..namedfork\/rsrc$')
if index > -1:
continue
index = line.find('\/..namedfork\/finf$')
if index > -1:
continue
write_obj.write(line)
else:
break



# test for errors
error_result = error_obj.readlines()
read_obj.close()
junk_obj.close()
error_obj.close()
if error_result:
return 1

-- 


*Paul Tremblay *
[EMAIL PROTECTED]

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


Re: [Tutor] Time script help sought!

2005-01-11 Thread Paul Tremblay
Hi Kevin

Is all your data in one big text file? Or do you have to type it in by
hand and want some type of script to do the calcluations? 

I have to run right now. I'll be back in a few hours and can give you
some more help then, if no one else has offered.

Paul


On Tue, Jan 11, 2005 at 12:18:15PM -0500, kevin parks wrote:
> From: kevin parks <[EMAIL PROTECTED]>
> Date: Tue, 11 Jan 2005 12:18:15 -0500
> To: tutor@python.org
> Cc: Kevin Parks <[EMAIL PROTECTED]>
> Subject: [Tutor] Time script help sought!
> 
> I am kind of in a bit of a jam  (okay a big jam) and i was hoping that 
> someone here could give me a quick hand. I had a few pages of time 
> calculations to do. So, i just started in on them typing them in my 
> time calculator and writing them in by hand. Now i realize, that i 
> really need a script to do this because:
> 
> 1. It turns out there are hundreds of pages of this stuff.
> 2. I have to do something similar in again soon.
> 3. By doing it by hand i am introducing wonderful new errors!
> 4. It all has to be typed up anyway (which means weeks of work and even 
> more typos!)
> 
> The input would like so:
> 
> Item_1TAPE_1100:238:23
> 
> Item_2TAPE_128:239:41
> 
> Item_3TAPE_139:4110:41
> Item_3TAPE_1410:4711:19
> Item_3TAPE_1511:2111:55
> Item_3TAPE_1611:5812:10
> Item_3TAPE_1712:1512:45Defect in analog tape sound.
> Item_3TAPE_1812:5824:20Defect in analog tape sound.
> 
> Item_4TAPE_1924:33
> Item_4TAPE_11025:48
> Item_4TAPE_11129:48
> Item_4TAPE_11231:46
> Item_4TAPE_11334:17Electronic sounds.
> Item_4TAPE_11435:21
> Item_4TAPE_11536:06
> Item_4TAPE_11637:0137:38
> 
> These are analog tapes that were digitized (on to CD or a digital tape) 
> that have now been exported as individual files that are meant to be 
> part of an on-line audio archive. The timings refer to the time display 
> on the CD or digital tape. The now all have to adjusted so that each 
> item starts at 0.00 since they have all been edited out of their 
> context and are now all individual items that start at 00:00. So Item_1 
> which was started at 00:23 on the tape and ended at 8:23 needs to have 
> 23 seconds subtracted to it so that it says:
> 
> Item_1TAPE_1100:0008:00
> 
> Item_2TAPE_1208:2309:41
> 
> would change to:
> 
> Item_2TAPE_1200:0001:18
> 
> etc.
> 
> but as always you may notice a wrinkle some items have many times 
> (here 6) indicated:
> 
> Item_3TAPE_139:4110:41
> Item_3TAPE_1410:4711:19
> Item_3TAPE_1511:2111:55
> Item_3TAPE_1611:5812:10
> Item_3TAPE_1712:1512:45Defect in analog tape sound.
> Item_3TAPE_1812:5824:20Defect in analog tape sound.
> 
> This is all a single sound file and these separate times mark where 
> there was a break, defect, or edit in the individual item. These have 
> to be adjusted as well to show where these events would appear in the 
> new sound file which now starts at 00:00.
> 
> Item_3TAPE_1300:0001:00
> Item_3TAPE_1401:0001:38
> Item_3TAPE_1501:3802:14
> Item_3TAPE_1602:1402:29
> Item_3TAPE_1702:2903:04Defect in analog tape sound.
> Item_3TAPE_1803:0414:39Defect in analog tape sound.
> 
> Further wrinkles: Some have start and end times indicated, some only 
> start times. I suppose that the output would ideally have both some 
> have comments and others don't ... and I need these comments echo-ed or
> since i probably need to make a database or table eventually non 
> comments just have some place holder.
> 
> I'd have a lot of similar type calculations to do... I was hoping and 
> praying that some one here was feeling generous and show me the way and 
> then, of course i could modify that to do other tasks... Usually i am 
> happy to take the long road and all but i'll be honest, i am in a big 
> jam here and this huge task was just dumped on me. I am frankly a 
> little desperate for help on this and hoping someone is feeling up to 
> spoon feeding me a clear modifiable example that works. Sorry.
> 
> cheers,
> 
> kevin
> k p 8 'at ' m a c 'dot' c o m
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 


*Paul Tremblay *
[EMAIL PROTECTED]

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


[Tutor] style question: when to "hide" variable, modules

2005-01-14 Thread Paul Tremblay
During the recent discussion on jython, a poster
brought up the good point that one should hide
variables and modules to indicate that they are
not for public use:

self.__for_internal_purposes = 5

__internal_stuff.py
"""
This module only makes sense for use with 
the parent module.
"""

So one could write several modules using these
guidelines. One could then issue the command 
from a shell pydoc internal_stuff, and sure enough,
one gets a nice listing of all the methods with the 
__methods listed first, signalling to someone who 
hasn't written the program (or to the author who 
returns to it a few years later), that one shouldn't 
look at these methods when looking what is useful
from the script.

My question is, how far should one take these guidlines?

I am working on an OO script with several modules that 
backs up a hardrive. There will be about 10 modules, but 
really only *one* of them, called backup, would be used 
as a pubilc interface. Should I name the rest of them things
like __handle_archive.py, __file_system.py, and so on? That
seems odd, since I have never seen this done before. However,
it does have an elegance of clearly telling someone how to 
hook into the modules.

Also, maybe more importantly, I want to know how to handle
attributes that need to be shared. Imagine that there are around
20 attributes, such as the level the program runs at, and the number 
of the disk being copied that need to be shared with different 
modules.  Right now, my setup looks like this:

# this module is called backup.py

class Backup:

  __init__(self, verbose, level ):
   self.__make_objects()
   self.verbose = verbose
   self.level = level
 

  def __make_objects(self):
self.burn_obj = paxbac.burn.Burn(self)
self.archive_obj = paxbac.archive.Archive(self)

  def get_disk(self):
return self.__disk

   def set_disk(self, the_num):
  self.__disk = the_num

  def backup(self):
 archive_obj.archive()
 burn_obj.burn()

*

#this is aother module called burn.py

class Burn:
  def __init__(self, main):
  self.__main = main

  def burn(self):
 cd_num = self.main.get_disk()
 if self__main.level > 3:
sys.stdout.write('Burning disk %s\n' %cd_num)



The main module backukp provides a number of public 
methods that are really not public. For examle, no 
one is going to want to use this module to find 
out what disk the method is on. If I wanted to 
be very strict with public and private variables
and methods, I would have to:

1. change burn.py to __burn.py

2. create a module called __attributes.py, create an
object in burn.py called self.__attributes_obj, and pass
this object to each method.

These two steps seem to take things a bit far. On the other 
hand, one could look at the set of modules and immediately
know how to use them.

Thanks

Paul


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


Re: [Tutor] style question: when to "hide" variable, modules

2005-01-18 Thread Paul Tremblay
Thanks for your input (and others, too!).


On Fri, Jan 14, 2005 at 08:11:32PM -0500, Kent Johnson wrote:
> Date: Fri, 14 Jan 2005 20:11:32 -0500
> From: Kent Johnson <[EMAIL PROTECTED]>
> User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206)
> Cc: tutor@python.org
> Subject: Re: [Tutor] style question: when to "hide" variable, modules
> 
> A few thoughts:
> - you might want to make a configuration object that you can pass around, 
> this is probably better than passing around an instance of the main Burn 
> class.

I actually pass around many instances. Maybe this is bad programming,
but I can't think of a better way to share attributes across modules.
For example, one class splits lists. I want both my message class and my
burn class to have access to the current state of the split list.


> - typical Python style is *not* to define setter and getter functions. If 
> you need to mediate attribute access you can do it later using properties.

I treid to figure out how this works with no luck.  It seems that when
you use property, you don't necessarily update the attribute. It seems I
want the setattr() and getattr() functions? I always thought it was bad
programming to alter an attribute directly in OO programming, but it
seems I am mistaken?

class Backup:

  __init__(self):
self.some_var = 5

 burn_obj = Burn(self)

class Burn:

  __init__(self, main):
  setattr(main, 'some_var', 6)


???


> - you might want to consolidate your classes into a small number of 
> modules. This is the style of much of the Python standard library. Look at 
> optparse.py for an example (picked more-or-less at random).

I couldn't find the optparse.py module. But looking at other packages
and modules, it seems that the one module shouldn't run more than 500
lines. I * could* consolidate many of my classes in one file, but that
would make very long files.

Thanks

Paul

> - if one class has a helper class or function that isn't used anywhere 
> else, put the helper into the same module as the client and name the helper 
> starting with _.
> - I tend to worry more about naming with _ in a module that is likely to be 
> reused. For a module that will probably be only be used once, I don't use _ 
> at all. In a module that has a public API I try to use _ to distinguish the 
> implementation details from the public stuff.
> 
> HTH
> Kent
> 
> Paul Tremblay wrote:
> >During the recent discussion on jython, a poster
> >brought up the good point that one should hide
> >variables and modules to indicate that they are
> >not for public use:
> >
> >self.__for_internal_purposes = 5
> >
> >__internal_stuff.py
> >"""
> >This module only makes sense for use with 
> >the parent module.
> >"""
> >
> >So one could write several modules using these
> >guidelines. One could then issue the command 
> >from a shell pydoc internal_stuff, and sure enough,
> >one gets a nice listing of all the methods with the 
> >__methods listed first, signalling to someone who 
> >hasn't written the program (or to the author who 
> >returns to it a few years later), that one shouldn't 
> >look at these methods when looking what is useful
> >from the script.
> >
> >My question is, how far should one take these guidlines?
> >
> >I am working on an OO script with several modules that 
> >backs up a hardrive. There will be about 10 modules, but 
> >really only *one* of them, called backup, would be used 
> >as a pubilc interface. Should I name the rest of them things
> >like __handle_archive.py, __file_system.py, and so on? That
> >seems odd, since I have never seen this done before. However,
> >it does have an elegance of clearly telling someone how to 
> >hook into the modules.
> >
> >Also, maybe more importantly, I want to know how to handle
> >attributes that need to be shared. Imagine that there are around
> >20 attributes, such as the level the program runs at, and the number 
> >of the disk being copied that need to be shared with different 
> >modules.  Right now, my setup looks like this:
> >
> ># this module is called backup.py
> >
> >class Backup:
> >
> >  __init__(self, verbose, level ):
> >   self.__make_objects()
> >   self.verbose = verbose
> >   self.level = level
> > 
> >
> >  def __make_objects(self):
> >self.burn_obj = paxbac.burn.Burn(self)
> >self.archive_obj = paxbac.archive.Archive(self)
> >
> >  def get_disk(self):
> >return self.__disk
> >
> >   def set_disk(self, the_num):
> >  self.__disk = the_

[Tutor] Presentation

2005-01-31 Thread Paul Hartley



I am trying to get Python established here in the 
Philippines. Currently I am in charge of operations at a business based in 
Manila and I have asked my IT staff to start using Python (with some 
success).
 
A local university has now asked that I give a talk 
to their IT people on Python - so here is an opportunity to spread the word and 
get some more converts and maybe introduce python into their computer science 
courses.
 
Any help I can get would be much appreciated - such 
as language comparisons, companies and systems that use python, debates about 
what python is good for (almost everything), examples of elegant 
code..
 
When I was a member of the Forth Interest Group in 
the USA we learned that Forth was used on the buggy that went to mars, that it 
started life controlling huge radio telescopes which only had 4k (yes 4k) of 
memory for both language and application.
 
Anything like the above concerning python would be 
useful.
 
Any other suggestions would help also, the audience 
will have computers, so a demonstration of small workshop would also be good - 
the presentation/session is for 4 hours on the 10th February.
 
Paul
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Presentation

2005-02-01 Thread Paul Hartley



Thank you for all your feedback on this - 
powerpoint presentations and all!!
 
Not only is the language amazing but the community 
is fantastic!!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] getting a webpage via python

2005-03-08 Thread Paul Tremblay
Is there a simple way to get a web page with python? I have done no
network programming with python before. 

My router (a Linksys 54G model) stores the IP/MAC addresss in a web
page. There is no way for me to access them except through the web. 

Righ now, I am using this code:

command = 'lynx -reload -auth %s:%s -source http://%s/DHCPTable.asp > %s' % (
 self.__log_name, self.__log_password, self.__router_address, 
temp_out1)
(exit_status, out_text) = commands.getstatusoutput(command)

The lynx terminal browser dumps the raw HTML page to the out_text, and I
can then parse it.

I would like to make the code independent of lynx, if possible.

Thanks

Paul

-- 

****
*Paul Tremblay *
[EMAIL PROTECTED]*

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


Re: [Tutor] getting a webpage via python

2005-03-08 Thread Paul Tremblay
On Tue, Mar 08, 2005 at 11:50:12AM -0500, Kent Johnson wrote:
> 
> Paul Tremblay wrote:
> 
> You can use urllib2 to do this. It is a little work to set it up to use 
> Basic authentication. Here is an example (slightly modified from the 
> urllib2 example page):
> 
> import urllib2
> 
> # Create an OpenerDirector with support for Basic HTTP Authentication...
> auth_handler = urllib2.HTTPBasicAuthHandler()
> auth_handler.add_password('realm', '127.0.0.1', 'username', 'password')
> opener = urllib2.build_opener(auth_handler)
> # ...and install it globally so it can be used with urlopen.
> urllib2.install_opener(opener)
> print urllib2.urlopen('http://127.0.0.1/my/protected/page.html').read()
> 
> Kent
> 

This is giving me 401 error, authorization required. Here's what I have:


auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password('realm', '127.0.0.1', 'myname', 'password')
opener = urllib2.build_opener(auth_handler)
# ...and install it globally so it can be used with urlopen.
urllib2.install_opener(opener)
print urllib2.urlopen('http://nnn.nnn.n.n').read()

This is almost the literal code, except I changed my log in name and
passowrd.

What am I supposed to put for realm? I checked the docs on line, and it
doens't mention what realm is.

Also, I assume I am supposed to use the loopback address when I set up
the handler?

Also, I know I am going to have another question once I solve this one,
and since it is directly related, I'll ask it now. How can I use a
password system that is not hardcoded in my text? Should I just setup a
password file? 

Thanks

Paul



-- 


*Paul Tremblay *
[EMAIL PROTECTED]*

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


Re: [Tutor] getting a webpage via python

2005-03-08 Thread Paul Tremblay


On Tue, Mar 08, 2005 at 01:42:40PM -0500, Kent Johnson wrote:
> Date: Tue, 08 Mar 2005 13:42:40 -0500
> From: Kent Johnson <[EMAIL PROTECTED]>
> User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206)
> Cc: python tutor 
> Subject: Re: [Tutor] getting a webpage via python
> 
> Paul Tremblay wrote:
> >This is giving me 401 error, authorization required. Here's what I have:
> >
> >
> >auth_handler = urllib2.HTTPBasicAuthHandler()
> >auth_handler.add_password('realm', '127.0.0.1', 'myname', 'password')
> >opener = urllib2.build_opener(auth_handler)
> ># ...and install it globally so it can be used with urlopen.
> >urllib2.install_opener(opener)
> >print urllib2.urlopen('http://nnn.nnn.n.n').read()
> >
> >This is almost the literal code, except I changed my log in name and
> >passowrd.
> >
> >What am I supposed to put for realm? I checked the docs on line, and it
> >doens't mention what realm is.
> 
> The realm name is part of the web server authentication configuration. If 
> you browse to the web page in your browser it will show the realm in the 
> auth dialog. For example my browser (Firefox) shows
> 
> Enter user name and password for "Curriculum Builder" at http://localhost
> 
> The realm name is "Curriculum Builder"


Thanks. I got it to work.

I tired to open the page in Koqueror web browser and got this message:

Authorization Dialog


You need to supply a username and password to access this site.

Site: WRT54G at nnn.nnn.n.n

I tried useing "Authorizaiton Dialog" with no success. So I tried opening
the same page with lynx with the trace option on. I found that lynx used
WRT54G as the realm, so I tried it and it worked.

> 
> >Also, I assume I am supposed to use the loopback address when I set up
> >the handler?
> 
> No, use your server name, in your example nnn.nn.n.n
> 
> >Also, I know I am going to have another question once I solve this one,
> >and since it is directly related, I'll ask it now. How can I use a
> >password system that is not hardcoded in my text? Should I just setup a
> >password file? 
> 
> Yes, something like that. You will have to store the password in plaintext 
> (or a reversible cipher) since that is what HTTPBasicAuthHandler needs.
> 

So I just make a file called /etc/router_passwords  and include
something like 

WRT54G username password

Then parse the file, and supply the info to the password handler? This
is easy to do, and I guess it is secure. I am networking on a home
system, so security is not so big a concern. However, I have seen a lot
of people struggle with getting router addresses from the WRT54G,so I
thought I might offer my script, and I wanted to do things a secure,
normal way.

Thanks

Paul

-- 


*Paul Tremblay *
[EMAIL PROTECTED]*

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


Re: [Tutor] getting a webpage via python

2005-03-09 Thread Paul Tremblay
On Wed, Mar 09, 2005 at 08:29:46AM -0500, Kent Johnson wrote:
> 
> Kent Johnson wrote:
> >Paul Tremblay wrote:
> >
> >>So I just make a file called /etc/router_passwords  and include
> >>something like
> >>WRT54G username password
> >>
> >>Then parse the file, and supply the info to the password handler? This
> >>is easy to do, and I guess it is secure. 
> 
> The book "Foundations of Python Network Programming" has a sample program 
> that uses a custom urllib2.HTTPPasswordMgr to ask the user for the username 
> and password, maybe you would like to use that?
> 
> The examples can be downloaded from the book Web site at
> http://www.apress.com/book/bookDisplay.html?bID=363
> 
> Look for the one called dump_info_auth.py
> 

Thanks. There are lots of good examples in the free download examples.

Paul

-- 


*Paul Tremblay *
[EMAIL PROTECTED]*

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


Re: [Tutor] and once i have learned how to program ?

2005-03-11 Thread Paul Tremblay
On Fri, Mar 11, 2005 at 02:26:48PM -0500, [EMAIL PROTECTED] wrote:
> From: [EMAIL PROTECTED]
> Date: Fri, 11 Mar 2005 14:26:48 EST
> To: tutor@python.org
> Subject: [Tutor] and once i have learned how to program ?
> 
> Once i have learned how to program what can i do with the programs can they 
> run out side of the python shell like on My OS or a game? My OS is window XP 
> home



Yes, python will act like any other program, such as C++. If you want to
use python to run a gui (graphical user interface), then you would use
Tinker. Just to give you an idea, I just downloaded a program called
skencil. I get in the shell and type "skencil". I then get a window for
to draw in. I take my mouse and make lines and drawings. I point my
mouse to the file window and pull down the menu for save to save my
document. So the application works exactly like, say, Adobe Draw, with
small difference that I had to start it form my shell. I'm pretty sure
there are ways to start applications without a shell. That isn't too
hard. The hard part is writing a program with a graphical interface.
This is always hard, in any languge. 

Paul

-- 


*Paul Tremblay *
[EMAIL PROTECTED]*

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


Re: [Tutor] CGI authentication

2005-03-14 Thread Paul Tremblay
On Mon, Mar 14, 2005 at 08:04:10AM -0500, Kent Johnson wrote:
> Date: Mon, 14 Mar 2005 08:04:10 -0500
> From: Kent Johnson <[EMAIL PROTECTED]>
> User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206)
> To: Python Tutor 
> Subject: [Tutor] CGI authentication
> 
> There was a question on this list recently about how to authenticate users 
> of a web server from a simple CGI script. I just came across a module that 
> might help:
> http://www.voidspace.org.uk/python/logintools.html
> 
> Kent
> 

That would be for me! This looks pretty nice. On problem I have with the
python url2lib is the dfficulty of determining the realm. For example,
bot lynx and curl (command line url tools) don't need a realm to work.
This seems to mean that lynx and curl provide more flexibility. 

Paul

-- 


*Paul Tremblay *
[EMAIL PROTECTED]*

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


[Tutor] distutils problem

2005-04-16 Thread Paul Tremblay
I have written a script that is available at:

https://sourceforge.net/projects/rtf2xml/

This script has gotten about 1600 downloads and I have not got many
bug complaints. However, recently I got the bug complaint listed
below. 

This bug--if it is one--seems to be related to the person't version
and setup of python rather than my script. Can anyone make sense of
it?

Thanks 

Paul

forwarded message
=

I tried to set this up according to the documentation at

  http://rtf2xml.sourceforge.net/docs/installation.html#install-rtf2xml-module

But when I try to run

   python setup.py install

(both as user and as root)

 I get

  xps8250:/home/chuck/rtf2xml-1.0a# python setup.py install

  running install

  error: invalid Python installation: unable to open 
/usr/lib/python2.3/config/Makefile (No such file or directory)

According to apt-get, I do have Python correctly installed. I also did
this:

xps8250:/home/chuck/rtf2xml-1.0a# python

 Python 2.3.5 (#2, Mar 26 2005, 17:32:32)

 [GCC 3.3.5 (Debian 1:3.3.5-12)] on linux2

However, in /usr/lib/python2.3/   there is no subdirectory for
/config/Makefile, nor any Makefile in any python subtree. I've looked
around the Python online documentation, but haven't found any
clues. Any suggestions?


-- 

****
*Paul Tremblay *
[EMAIL PROTECTED]*

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


Re: [Tutor] distutils problem

2005-04-16 Thread Paul Tremblay
On Sat, Apr 16, 2005 at 12:58:33PM -0400, Paul Tremblay wrote:
> I have written a script that is available at:
> 
> https://sourceforge.net/projects/rtf2xml/
> 
> This script has gotten about 1600 downloads and I have not got many
> bug complaints. However, recently I got the bug complaint listed
> below. 
> 
> This bug--if it is one--seems to be related to the person't version
> and setup of python rather than my script. Can anyone make sense of
> it?
> 
> Thanks 
> 
> Paul
> 
> forwarded message
> =
> 
> I tried to set this up according to the documentation at
> 
>   http://rtf2xml.sourceforge.net/docs/installation.html#install-rtf2xml-module
> 
> But when I try to run
> 
>python setup.py install
> 
> (both as user and as root)
> 
>  I get
> 
>   xps8250:/home/chuck/rtf2xml-1.0a# python setup.py install
> 
>   running install
> 
>   error: invalid Python installation: unable to open 
> /usr/lib/python2.3/config/Makefile (No such file or directory)
> 
> According to apt-get, I do have Python correctly installed. I also did
> this:
> 
> xps8250:/home/chuck/rtf2xml-1.0a# python
> 
>  Python 2.3.5 (#2, Mar 26 2005, 17:32:32)
> 
>  [GCC 3.3.5 (Debian 1:3.3.5-12)] on linux2
> 
> However, in /usr/lib/python2.3/   there is no subdirectory for
> /config/Makefile, nor any Makefile in any python subtree. I've looked
> around the Python online documentation, but haven't found any
> clues. Any suggestions?
> 
> 

Okay, I just did a web search (which I should have done before I
posted this!) and discovered that the problem is probably due to the
user not having the development package of python installed as well. I
posted him and will wait to hear from him.

Paul

-- 


*Paul Tremblay *
[EMAIL PROTECTED]*

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


[Tutor] Python and Web Pages?

2005-04-23 Thread Paul Tader
I have a couple programs/scripts that I want to write that need to be 
web-based.  Can Python (and a little HTML) accomplish this?  Or are 
other languages like PHP, or Perl better suited?

A little more detail:
One project is to make a web page that users login to with a name and 
password, choose from a list of "canned" directory structure (ie. how 
many subdirectories, top-level name, maybe permissions, etc), hit a "GO" 
button and then the directories are made.

Another example is a web-based form that users interface with a mySQL 
database.  Simple additions/changes/deletions.  Basic stuff.

Thanks,
Paul

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


[Tutor] Parsing problem

2005-07-21 Thread Paul McGuire
Liam, Kent, and Danny -

It sure looks like pyparsing is taking on a life of its own!  I can see I no
longer am the only one pitching pyparsing at some of these applications!

Yes, Liam, it is possible to create dictionary-like objects, that is,
ParseResults objects that have named values in them.  I looked into your
application, and the nested assignments seem very similar to a ConfigParse
type of structure.  Here is a pyparsing version that handles the test data
in your original post (I kept Danny Yoo's recursive list values, and added
recursive dictionary entries):

--
import pyparsing as pp

listValue = pp.Forward()
listSeq = pp.Suppress('{') + pp.Group(pp.ZeroOrMore(listValue)) +
pp.Suppress('}')
listValue << ( pp.dblQuotedString.setParseAction(pp.removeQuotes) | 
pp.Word(pp.alphanums) | listSeq )

keyName = pp.Word( pp.alphas )

entries = pp.Forward()
entrySeq = pp.Suppress('{') + pp.Group(pp.OneOrMore(entries)) +
pp.Suppress('}')
entries << pp.Dict( 
pp.OneOrMore( 
pp.Group( keyName + pp.Suppress('=') + (entrySeq |
listValue) ) ) )
--


Dict is one of the most confusing classes to use, and there are some
examples in the examples directory that comes with pyparsing (see
dictExample2.py), but it is still tricky.  Here is some code to access your
input test data, repeated here for easy reference:

--
testdata = """\
country = {
tag = ENG
ai = {
flags = { }
combat = { DAU FRA ORL PRO }
continent = { }
area = { }
region = { "British Isles" "NorthSeaSea" "ECAtlanticSea" "NAtlanticSea"
"TagoSea" "WCAtlanticSea" }
war = 60
ferocity = no
}
}
"""
parsedEntries = entries.parseString(testdata)

def dumpEntries(dct,depth=0):
keys = dct.keys()
keys.sort()
for k in keys:
print ('  '*depth) + '- ' + k + ':',
if isinstance(dct[k],pp.ParseResults):
if dct[k][0].keys():
print
dumpEntries(dct[k][0],depth+1)
else:
print dct[k][0]
else:
print dct[k]

dumpEntries( parsedEntries )

print
print parsedEntries.country[0].tag
print parsedEntries.country[0].ai[0].war
print parsedEntries.country[0].ai[0].ferocity
--

This will print out:

--
- country:
  - ai:
- area: []
- combat: ['DAU', 'FRA', 'ORL', 'PRO']
- continent: []
- ferocity: no
- flags: []
- region: ['British Isles', 'NorthSeaSea', 'ECAtlanticSea',
'NAtlanticSea', 'TagoSea', 'WCAtlanticSea']
- war: 60
  - tag: ENG

ENG
60
No
--

But I really dislike having to dereference those nested values using the
0'th element.  So I'm going to fix pyparsing so that in the next release,
you'll be able to reference the sub-elements as:

print parsedEntries.country.tag
print parsedEntries.country.ai.war
print parsedEntries.country.ai.ferocity

This *may* break some existing code, but Dict is not heavily used, based on
feedback from users, and this may make it more useful in general, especially
when data parses into nested Dict's.

Hope this sheds more light than confusion!
-- Paul McGuire

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


Re: [Tutor] Parsing problem

2005-07-23 Thread Paul McGuire
Liam -

Glad you are sticking with pyparsing through some of these idiosyncracies!

One thing that might simplify your life is if you are a bit more strict on
specifying your grammar, especially using pp.printables as the character set
for your various words and values.  Is this statement really valid?

Lw)r*)*dsflkj = sldjouwe)r#jdd

According to your grammar, it is.  Also, by using printables, you force your
user to insert whitespace between the assignment target and the equals sign.
I'm sure your users would like to enter a quick "a=1" once in a while, but
since there is no whitespace, it will all be slurped into the left-hand side
identifier.

Let's create two expressions, LHS and RHS, to dictate what is valid on the
left and right-hand side of the equals sign.  (Well, it turns out I create a
bunch of expressions here, in the process of defining LHS and RHS, but
hopefullly, this will make some sense):

EQUALS = pp.Suppress("=")
LBRACE = pp.Suppress("{")
RBRACE = pp.Suppress("}")
identifier = pp.Word(pp.alphas, pp.alphanums + "_")
integer = pp.Word(pp.nums+"-+", pp.nums)
assignment = pp.Forward()
LHS = identifier
RHS = pp.Forward().setName("RHS")
RHS << ( pp.dblQuotedString ^ identifier ^ integer ^ pp.Group( LBRACE +
pp.OneOrMore(assignment) + RBRACE ) )
assignment << pp.Group( LHS + EQUALS + RHS )

I leave it to you to flesh out what other possible value types can be
included in RHS.

Note also the use of the Group.  Try running this snippet with and without
Group and see how the results change.  I think using Group will help you to
build up a good parse tree for the matched tokens.

Lastly, please note in the '<<' assignment to RHS that the expression is
enclosed in parens.  I originally left this as

RHS << pp.dblQuotedString ^ identifier ^ integer ^ pp.Group( LBRACE +
pp.OneOrMore(assignment) + RBRACE )

And it failed to match!  A bug! In my own code!  The shame...

This fails because '<<' has a higher precedence then '^', so RHS only worked
if it was handed a quoted string.  Probably good practice to always enclose
in quotes the expression being assigned to a Forward using '<<'.

-- Paul


-Original Message-
From: Liam Clarke [mailto:[EMAIL PROTECTED] 
Sent: Saturday, July 23, 2005 9:03 AM
To: Paul McGuire
Cc: tutor@python.org
Subject: Re: [Tutor] Parsing problem

*sigh* I just read the documentation more carefully and found the difference
between the 
| operator and the ^ operator. 

Input - 

j = { line = { foo = 10 bar = 20 } }

New code

sel = pp.Forward()
values = ((pp.Word(pp.printables) + pp.Suppress("=") +
pp.Word(pp.printables)) ^ sel)
sel << (pp.Word(pp.printables) + pp.Suppress("=") + pp.Suppress("{") +
pp.OneOrMore(values) + pp.Suppress("}"))

Output - 

(['j', 'line', 'foo', '10', 'bar', '20'], {})

My apologies for the deluge. 

Regards, 

Liam Clarke


On 7/24/05, Liam Clarke <[EMAIL PROTECTED]> wrote:

Hmmm... just a quick update, I've been poking around and I'm
obviously making some error of logic. 

Given a line - 

 f = "j = { line = { foo = 10 bar = 20 } }"

And given the following code - 

select = pp.Forward()
select << 
pp.Word(pp.printables) + pp.Suppress("=") + pp.Suppress("{") + 
pp.OneOrMore( (pp.Word(pp.printables) + pp.Suppress("=") + 
pp.Word(pp.printables) ) | select ) + pp.Suppress("}")

sel.parseString(f) gives - 

(['j', 'line', '{', 'foo', '10', 'bar', '20'], {})

So I've got a bracket sneaking through there. Argh. My brain hurts. 

Is the | operator an exclusive or? 

Befuddled, 

Liam Clarke



On 7/23/05, Liam Clarke <[EMAIL PROTECTED] > wrote:

Howdy, 

I've attempted to follow your lead and have started from
scratch, I could just copy and paste your solution (which works pretty
well), but I want to understand what I'm doing *grin*

However, I've been hitting a couple of ruts in the path to
enlightenment. Is there a way to tell pyparsing that to treat specific
escaped characters as just a slash followed by a letter? For the time being
I've converted all backslashes to forwardslashes, as it was choking on \a in
a file path.

But my latest hitch, takes this form (apologies for large
traceback)

Traceback (most recent call last):
  File &qu

Re: [Tutor] Parsing problem

2005-07-25 Thread Paul McGuire
Liam -

Great, this sounds like it's coming together.  Don't be discouraged, parsing
text like this has many forward/backward steps.

As far as stopping after one assignent, well, you might kick yourself over
this, but the answer is that you are no longer parsing just a single
assignment, but a list of them.  You cannot parse more than one assignment
with assignment as you have it, and you shouldn't.  Instead, expand the
scope of the parser to correspond to the expanded scope of input, as in:


listOfAssignments = OneOrMore( assignment )  


Now listOfAssignments is your root BNF, that you use to call parseString
against the contents of the input file.

Looking at your code, you might prefer to just enclose the contents inside
the braces inside an Optional, or a ZeroOrMore.  Seeing the other possible
elements that might be in your braces, will this work?  ZeroOrMore will take
care of the empty option, and recursively nesting RHS will avoid having to
repeat the other "scalar" entries.


RHS << ( pp.dblQuotedString.setParseAction(pp.removeQuotes) ^
 identifier ^
 integer ^
 pp.Group( LBRACE + pp.ZeroOrMore( assignment ^ RHS ) + RBRACE ) )


-- Paul



-Original Message-
From: Liam Clarke [mailto:[EMAIL PROTECTED] 
Sent: Sunday, July 24, 2005 10:21 AM
To: Paul McGuire
Cc: tutor@python.org
Subject: Re: [Tutor] Parsing problem

Hi Paul, 

That is fantastic. It works, and using that pp.group is the key with the
nested braces. 

I just ran this on the actual file after adding a few more possible values
inside the group, and it parsed the entire header structure rather nicely.

Now this will probably sound silly, but from the bit 

header = {...
...
}

it continues on with 

province = {...
} 

and so forth. 

Now, once it reads up to the closing bracket of the header section, it
returns that parsed nicely. 
Is there a way I can tell it to continue onwards? I can see that it's
stopping at one group.

Pyparsing is wonderful, but boy... as learning curves go, I'm somewhat over
my head.

I've tried this - 

Code http://www.rafb.net/paste/results/3Dm7FF35.html
Current data http://www.rafb.net/paste/results/3cWyt169.html

assignment << (pp.OneOrMore(pp.Group( LHS + EQUALS + RHS ))) 

to try and continue the parsing, but no luck.

I've been running into the 

 File "c:\python24\Lib\site-packages\pyparsing.py", line 1427, in parseImpl
raise maxException
pyparsing.ParseException: Expected "}" (at char 742), (line:35, col:5) 

hassle again. From the CPU loading, I'm worried I've got myself something
very badly recursive going on, but I'm unsure of how to use validate()

I've noticed that a few of the sections in between contain values like this
- 

foo = { BAR = { HUN = 10 SOB = 6 } oof = { HUN = { } SOB = 4 } }

and so I've stuck pp.empty into my RHS possible values. What unintended side
effects may I get from using pp.empty? From the docs, it sounds like a
wildcard token, rather than matching a null.

Using pp.empty has resolved my apparent problem with empty {}'s causing my
favourite exception, but I'm just worried that I'm casting my net too wide.

Oh, and, if there's a way to get a 'last line parsed' value so as to start
parsing onwards, it would ease my day, as the only way I've found to get the
whole thing parsed is to use another x = { ... } around the whole of the
data, and now, I'm only getting the 'x' returned, so if I could parse by
section, it would help my understanding of what's happening. 

I'm still trial and error-ing a bit too much at the moment.

Regards, 

Liam Clarke





On 7/24/05, Paul McGuire <[EMAIL PROTECTED]> wrote:

Liam -

Glad you are sticking with pyparsing through some of these
idiosyncracies!

One thing that might simplify your life is if you are a bit more
strict on
specifying your grammar, especially using pp.printables as the
character set
for your various words and values.  Is this statement really valid?

Lw)r*)*dsflkj = sldjouwe)r#jdd

According to your grammar, it is.  Also, by using printables, you
force your
user to insert whitespace between the assignment target and the
equals sign. 
I'm sure your users would like to enter a quick "a=1" once in a
while, but
since there is no whitespace, it will all be slurped into the
left-hand side
identifier.

Let's create two expressions, LHS and RHS, to dictate what is valid
on the 
left and right-hand side of the equals sign.  (Well, it turns out I
create a
bunch of expressions here, in the process of defining LHS and RHS,
but
hopefullly, this will make some sense):

EQUALS = pp.Suppress ("=")
LBRACE = pp.Suppress("{&

Re: [Tutor] Parsing problem

2005-07-25 Thread Paul McGuire
Liam -

I just uploaded an update to pyparsing, version 1.3.2, that should fix the
problem with using nested Dicts.  Now you won't need to use [0] to
dereference the "0'th" element, just reference the nested elements as a.b.c,
or a["b"]["c"].

-- Paul 


-Original Message-
From: Liam Clarke [mailto:[EMAIL PROTECTED] 
Sent: Sunday, July 24, 2005 10:21 AM
To: Paul McGuire
Cc: tutor@python.org
Subject: Re: [Tutor] Parsing problem

Hi Paul, 

That is fantastic. It works, and using that pp.group is the key with the
nested braces. 

I just ran this on the actual file after adding a few more possible values
inside the group, and it parsed the entire header structure rather nicely.

Now this will probably sound silly, but from the bit 

header = {...
...
}

it continues on with 

province = {...
} 

and so forth. 

Now, once it reads up to the closing bracket of the header section, it
returns that parsed nicely. 
Is there a way I can tell it to continue onwards? I can see that it's
stopping at one group.

Pyparsing is wonderful, but boy... as learning curves go, I'm somewhat over
my head.

I've tried this - 

Code http://www.rafb.net/paste/results/3Dm7FF35.html
Current data http://www.rafb.net/paste/results/3cWyt169.html

assignment << (pp.OneOrMore(pp.Group( LHS + EQUALS + RHS ))) 

to try and continue the parsing, but no luck.

I've been running into the 

 File "c:\python24\Lib\site-packages\pyparsing.py", line 1427, in parseImpl
raise maxException
pyparsing.ParseException: Expected "}" (at char 742), (line:35, col:5) 

hassle again. From the CPU loading, I'm worried I've got myself something
very badly recursive going on, but I'm unsure of how to use validate()

I've noticed that a few of the sections in between contain values like this
- 

foo = { BAR = { HUN = 10 SOB = 6 } oof = { HUN = { } SOB = 4 } }

and so I've stuck pp.empty into my RHS possible values. What unintended side
effects may I get from using pp.empty? From the docs, it sounds like a
wildcard token, rather than matching a null.

Using pp.empty has resolved my apparent problem with empty {}'s causing my
favourite exception, but I'm just worried that I'm casting my net too wide.

Oh, and, if there's a way to get a 'last line parsed' value so as to start
parsing onwards, it would ease my day, as the only way I've found to get the
whole thing parsed is to use another x = { ... } around the whole of the
data, and now, I'm only getting the 'x' returned, so if I could parse by
section, it would help my understanding of what's happening. 

I'm still trial and error-ing a bit too much at the moment.

Regards, 

Liam Clarke





On 7/24/05, Paul McGuire <[EMAIL PROTECTED]> wrote:

Liam -

Glad you are sticking with pyparsing through some of these
idiosyncracies!

One thing that might simplify your life is if you are a bit more
strict on
specifying your grammar, especially using pp.printables as the
character set
for your various words and values.  Is this statement really valid?

Lw)r*)*dsflkj = sldjouwe)r#jdd

According to your grammar, it is.  Also, by using printables, you
force your
user to insert whitespace between the assignment target and the
equals sign. 
I'm sure your users would like to enter a quick "a=1" once in a
while, but
since there is no whitespace, it will all be slurped into the
left-hand side
identifier.

Let's create two expressions, LHS and RHS, to dictate what is valid
on the 
left and right-hand side of the equals sign.  (Well, it turns out I
create a
bunch of expressions here, in the process of defining LHS and RHS,
but
hopefullly, this will make some sense):

EQUALS = pp.Suppress ("=")
LBRACE = pp.Suppress("{")
RBRACE = pp.Suppress("}")
identifier = pp.Word(pp.alphas, pp.alphanums + "_")
integer = pp.Word(pp.nums+"-+", pp.nums)
assignment = pp.Forward()
LHS = identifier
RHS = pp.Forward().setName("RHS")
RHS << ( pp.dblQuotedString ^ identifier ^ integer ^ pp.Group(
LBRACE +
pp.OneOrMore(assignment) + RBRACE ) )
assignment << pp.Group( LHS + EQUALS + RHS )

I leave it to you to flesh out what other possible value types can
be
included in RHS.

Note also the use of the Group.  Try running this snippet with and
without
Group and see how the results change.  I think using Group will help
you to 
build up a good parse tree for the matched tokens.

Lastly, please note in the '<<' assignment to RHS

Re: [Tutor] Parsing problem

2005-07-25 Thread Paul McGuire
Liam -

Could you e-mail me your latest grammar?  The last version I had includes
this definition for RHS:

RHS << ( pp.dblQuotedString.setParseAction(pp.removeQuotes) ^
 identifier ^
 integer ^
 pp.Group( LBRACE + pp.ZeroOrMore( assignment ^ RHS ) + RBRACE ) )

What happens if you replace the '^' operators with '|', as in:

RHS << ( pp.dblQuotedString.setParseAction(pp.removeQuotes) |
 identifier |
 integer |
 pp.Group( LBRACE + pp.ZeroOrMore( assignment | RHS ) + RBRACE ) )

I think earlier on, you needed to use '^' because your various terms were
fairly vague (you were still using Word(pp.printables), which would accept
just about anything).  But now I think there is little ambiguity between a
quoted string, identifier, etc., and simple '|' or MatchFirst's will do.
This is about the only optimization I can think of.

-- Paul
 

-Original Message-
From: Liam Clarke [mailto:[EMAIL PROTECTED] 
Sent: Monday, July 25, 2005 7:38 AM
To: Paul McGuire
Cc: tutor@python.org
Subject: Re: [Tutor] Parsing problem

Hi Paul, 

Well various tweaks and such done, it parses perfectly, so much thanks, I
think I now have a rough understanding of the basics of pyparsing. 

Now, onto the fun part of optimising it. At the moment, I'm looking at 2 - 5
minutes to parse a 2000 line country section, and that's with psyco. Only
problem is, I have 157 country sections...

I am running a 650 MHz processor, so that isn't helping either. I read this
quote on http://pyparsing.sourceforge.net.

"Thanks again for your help and thanks for writing pyparser! It seems my
code needed to be optimized and now I am able to parse a 200mb file in 3
seconds. Now I can stick my tongue out at the Perl guys ;)"

I'm jealous, 200mb in 3 seconds, my file's only 4mb.

Are there any general approaches to optimisation that work well?

My current thinking is to use string methods to split the string into each
component section, and then parse each section to a bare minimum key, value.
ie - instead of parsing 

x = { foo = { bar = 10 bob = 20 } type = { z = { } y = { } }}

out fully, just parse to "x":"{ foo = { bar = 10 bob = 20 } type = { z = { }
y = { } }}"

I'm thinking that would avoid the complicated nested structure I have now,
and I could parse data out of the string as needed, if needed at all.

Erk, I don't know, I've never had to optimise anything. 

Much thanks for creating pyparsing, and doubly thank-you for your assistance
in learning how to use it. 

Regards, 

Liam Clarke

On 7/25/05, Liam Clarke <[EMAIL PROTECTED]> wrote:

Hi Paul, 

My apologies, as I was jumping into my car after sending that email,
it clicked in my brain. 
"Oh yeah... initial & body..."

But good to know about how to accept valid numbers.

Sorry, getting a bit too quick to fire off emails here.

Regards, 

Liam Clarke


On 7/25/05, Paul McGuire < [EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]> > wrote:


Liam -

The two arguments to Word work this way:
- the first argument lists valid *initial* characters
- the second argument lists valid *body* or subsequent
characters

For example, in the identifier definition, 

identifier = pp.Word(pp.alphas, pp.alphanums + "_/:.")

identifiers *must* start with an alphabetic character, and
then may be
followed by 0 or more alphanumeric or _/: or . characters.
If only one 
argument is supplied, then the same string of characters is
used as both
initial and body.  Identifiers are very typical for 2
argument Word's, as
they often start with alphas, but then accept digits and
other punctuation. 
No whitespace is permitted within a Word.  The Word matching
will end when a
non-body character is seen.

Using this definition:

integer = pp.Word(pp.nums+"-+.", pp.nums)

It will accept "+123", "-345", "678", and ".901".  But in a
real number, a 
period may occur anywhere in the number, not just as the
initial character,
as in "3.14159".  So your bodyCharacters must also include a
".", as in:

integer = pp.Word(pp.nums+"-+.", pp.nums+".")

Let me say, though, that this is a very permissive
definition of integer -
for one thing, we reall

Re: [Tutor] Parsing problem

2005-07-25 Thread Paul McGuire
Liam -

The two arguments to Word work this way:
- the first argument lists valid *initial* characters
- the second argument lists valid *body* or subsequent characters

For example, in the identifier definition, 

identifier = pp.Word(pp.alphas, pp.alphanums + "_/:.")

identifiers *must* start with an alphabetic character, and then may be
followed by 0 or more alphanumeric or _/: or . characters.  If only one
argument is supplied, then the same string of characters is used as both
initial and body.  Identifiers are very typical for 2 argument Word's, as
they often start with alphas, but then accept digits and other punctuation.
No whitespace is permitted within a Word.  The Word matching will end when a
non-body character is seen.

Using this definition:

integer = pp.Word(pp.nums+"-+.", pp.nums)

It will accept "+123", "-345", "678", and ".901".  But in a real number, a
period may occur anywhere in the number, not just as the initial character,
as in "3.14159".  So your bodyCharacters must also include a ".", as in:

integer = pp.Word(pp.nums+"-+.", pp.nums+".")

Let me say, though, that this is a very permissive definition of integer -
for one thing, we really should rename it something like "number", since it
now accepts non-integers as well!  But also, there is no restriction on the
frequency of body characters.  This definition would accept a "number" that
looks like "3.4.3234.111.123.3234".  If you are certain that you will only
receive valid inputs, then this simple definition will be fine.  But if you
will have to handle and reject erroneous inputs, then you might do better
with a number definition like:

number = Combine( Word( "+-"+nums, nums ) + 
  Optional( point + Optional( Word( nums ) ) ) )

This will handle "+123", "-345", "678", and "0.901", but not ".901".  If you
want to accept numbers that begin with "."s, then you'll need to tweak this
a bit further.

One last thing: you may want to start using setName() on some of your
expressions, as in:

number = Combine( Word( "+-"+nums, nums ) + 
  Optional( point + Optional( Word( nums ) ) )
).setName("number")

Note, this is *not* the same as setResultsName.  Here setName is attaching a
name to this pattern, so that when it appears in an exception, the name will
be used instead of an encoded pattern string (such as W:012345...).  No need
to do this for Literals, the literal string is used when it appears in an
exception.

-- Paul


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


Re: [Tutor] Parsing problem

2005-07-25 Thread Paul McGuire
One other thing, Liam.  Download the latest version of pyparsing (1.3.2),
and make this change to the assignment statement:

assignment << pp.Dict( pp.Group( LHS + EQUALS + RHS ) )


Now you can write clean-looking code like:

test = """j = { line = { foo = 10 bar = 20 } } }"""
res = assignment.parseString(test)
print res
print res.j
print res.j.line
print res.j.line.foo
print res.j.line.bar


And get:

[['j', [['line', [['foo', '10'], ['bar', '20']]
[['line', [['foo', '10'], ['bar', '20'
[['foo', '10'], ['bar', '20']]
10
20


-- Paul
 

-Original Message-
From: Liam Clarke [mailto:[EMAIL PROTECTED] 
Sent: Monday, July 25, 2005 7:38 AM
To: Paul McGuire
Cc: tutor@python.org
Subject: Re: [Tutor] Parsing problem

Hi Paul, 

Well various tweaks and such done, it parses perfectly, so much thanks, I
think I now have a rough understanding of the basics of pyparsing. 

Now, onto the fun part of optimising it. At the moment, I'm looking at 2 - 5
minutes to parse a 2000 line country section, and that's with psyco. Only
problem is, I have 157 country sections...

I am running a 650 MHz processor, so that isn't helping either. I read this
quote on http://pyparsing.sourceforge.net.

"Thanks again for your help and thanks for writing pyparser! It seems my
code needed to be optimized and now I am able to parse a 200mb file in 3
seconds. Now I can stick my tongue out at the Perl guys ;)"

I'm jealous, 200mb in 3 seconds, my file's only 4mb.

Are there any general approaches to optimisation that work well?

My current thinking is to use string methods to split the string into each
component section, and then parse each section to a bare minimum key, value.
ie - instead of parsing 

x = { foo = { bar = 10 bob = 20 } type = { z = { } y = { } }}

out fully, just parse to "x":"{ foo = { bar = 10 bob = 20 } type = { z = { }
y = { } }}"

I'm thinking that would avoid the complicated nested structure I have now,
and I could parse data out of the string as needed, if needed at all.

Erk, I don't know, I've never had to optimise anything. 

Much thanks for creating pyparsing, and doubly thank-you for your assistance
in learning how to use it. 

Regards, 

Liam Clarke

On 7/25/05, Liam Clarke <[EMAIL PROTECTED]> wrote:

Hi Paul, 

My apologies, as I was jumping into my car after sending that email,
it clicked in my brain. 
"Oh yeah... initial & body..."

But good to know about how to accept valid numbers.

Sorry, getting a bit too quick to fire off emails here.

Regards, 

Liam Clarke


On 7/25/05, Paul McGuire < [EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]> > wrote:


Liam -

The two arguments to Word work this way:
- the first argument lists valid *initial* characters
- the second argument lists valid *body* or subsequent
characters

For example, in the identifier definition, 

identifier = pp.Word(pp.alphas, pp.alphanums + "_/:.")

identifiers *must* start with an alphabetic character, and
then may be
followed by 0 or more alphanumeric or _/: or . characters.
If only one 
argument is supplied, then the same string of characters is
used as both
initial and body.  Identifiers are very typical for 2
argument Word's, as
they often start with alphas, but then accept digits and
other punctuation. 
No whitespace is permitted within a Word.  The Word matching
will end when a
non-body character is seen.

Using this definition:

integer = pp.Word(pp.nums+"-+.", pp.nums)

It will accept "+123", "-345", "678", and ".901".  But in a
real number, a 
period may occur anywhere in the number, not just as the
initial character,
as in "3.14159".  So your bodyCharacters must also include a
".", as in:

integer = pp.Word(pp.nums+"-+.", pp.nums+".")

Let me say, though, that this is a very permissive
definition of integer -
for one thing, we really should rename it something like
"number", since it
now accepts non-integers as well!  But also, there is no
restriction on the 
frequency of body characters.  Th

Re: [Tutor] Parsing problem

2005-07-25 Thread Paul McGuire
Liam -

I made some changes and timed them, I think this problem is solvable.  (All
timings are done using your data, on a P4 800MHz laptop.)

1. Baseline, the current state, in the parser code you sent me:

bracketGroup << ( pp.Group( LBRACE + ( pp.empty ^ pp.OneOrMore(assignment) ^
pp.OneOrMore(identifier) ^ pp.OneOrMore(pp.dblQuotedString) ^
pp.OneOrMore(number) ^ pp.OneOrMore(bracketGroup) ) + RBRACE ) )

Time: 02:20.71 (mm:ss.sss)

Just for general edification, '^' means Or, and it will evaluate all the
alternatives and choose the longest match (in regexp docs, this is sometimes
referred to as "greedy" matching); '|' means MatchFirst, and it will only
evaluate alternatives until it finds a match (which I refer to as "eager"
matching).  In the past, I've had only slight results converting '^' to '|',
but since this is a recursive expression, evaluating all of the possible
alternatives can involve quite a bit of processing before selecting the
longest.

2. Convert to '|', replace empty with ZeroOrMore:

bracketGroup << ( pp.Group( LBRACE + pp.ZeroOrMore( assignment | identifier
| pp.dblQuotedString | number | bracketGroup ) + RBRACE ) )

Time: 00:14.57

This is getting us somewhere!  Replaced empty and OneOrMore's with a single
ZeroOrMore, and changed from '^' to '|'.  Since there is no overlap of the
various alternatives *in their current order*, it is safe to use '|'. (This
would not be the case if assignment came after identifier - this should be a
hint on how to resolve the 'empty' problem.)  One problem with this
expression is that it will permit mixed bracket groups, such as { "A" 10
b=1000 {} }.

3. Go back to baseline, change '^' to '|', *and put empty at the end*

bracketGroup << ( pp.Group( LBRACE + ( pp.OneOrMore(assignment) |
pp.OneOrMore(identifier) | pp.OneOrMore(pp.dblQuotedString) |
pp.OneOrMore(number) | pp.OneOrMore(bracketGroup) | pp.empty ) + RBRACE ) )

Time: 00:12.04

Best solution yet!  This is faster than #2, since, once a match is made on
the first term within  a bracketGroup, all others in the group are expected
to be the same type.  Since '|' means "take first match", we resolve empty's
"accept anything" behavior simply by putting it at the end of the list.

4. Make change in #3, also convert '^' to '|' in RHS.

RHS << ( pp.dblQuotedString | identifier | number | bracketGroup )

Time: 00:01.15

Voila!  I'm happy to say, this is the first time I've seen a 100X
improvement, mostly by replacing '^' by '|'.  While this is not *always*
possible (see the CORBA IDL parser in the examples directory), it is worth
the effort, especially with a recursive expression.

The one item to be wary of when using '|' is when expressions mask each
other.  The easiest example is when trying to parse numbers, which may be
integers or reals.  If I write the expression as (assuming that integers
will match a sequence of digits, and reals will match digits with a decimal
point and some more digits):

number = (integer | real)

I will never match a real number! The integer expression "masks" the real,
and since it occurs first, it will match first.  The two solutions are:

number = (integer ^ real)
Or
number = (real | integer)

That is, use an Or, which will match the longest, or reorder the MatchFirst
to put the most restrictive expression first.

Welcome to pyparsing, please let me know how your project goes!

-- Paul


-Original Message-
From: Liam Clarke [mailto:[EMAIL PROTECTED] 
Sent: Monday, July 25, 2005 8:31 AM
To: Paul McGuire
Subject: Re: [Tutor] Parsing problem

Hi Paul, 

I've attached the latest version. It includes my sample data within the
file. The sample data came in at 8 minutes 32 seconds without Pysco, 5
minutes 25 with, on a  650MHz Athlon.

I was pondering whether breaking the test data down into separate bits via
some preprocessing and feeding the simpler data structures in would help at
all.

Unfortunately, as I'm using pp.empty to deal with empty bracket sets (which
were causing my 'expected "}" ' exceptions), using | matches to pp.empty
first. 

I'm not sure how to get around the empty brackets without using that.

I also get the feeling that pyparsing was more designed for making parsing
small complex expressions easy, as opposed to my data churning. That said, I
can think of about ten different projects I'd played with before giving up
because of a problem that pyparsing handles elegantly.
Usually it was regexes that got me. So if I have to attack this another way,
at least I know the basics of a good module now. :)

Much thanks for your time and energy, having read your listing on the c2
wiki (I searched for pyparsing on the offchan

Re: [Tutor] Parsing problem

2005-07-26 Thread Paul McGuire
> Also interesting is that our processors, which aren't overly 
> far apart in clock speed, vary so greatly in processing this 
> problem. Maybe Intel is better *grin*

Urp, turns out I have an "Athlon Inside" label right here on the deck of my
laptop!  Maybe the difference is my 1.2Gb of RAM.  It's an Athlon XP-M
3000+, which runs about 800MHz to save battery power, but can ratchet up to
1.6GHz when processing.

Cheers!
-- Paul

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


[Tutor] More class questions

2007-09-08 Thread Paul McGuire
Ara -

I found your question about the Pyparsing-based adventure game that I wrote.
You can find more info on this from the presentation I made at PyCon'06,
(http://www.python.org/pycon/2006/papers/4/).  This link opens up at the
title page, there are navigation controls in the lower right corner of the
screen if you move your mouse over them.

This program uses the following classes:
- Room
- Item
- Player
- Command

The Room and Item instances are created during the game setup.  Each Room
contains pointers to neighboring rooms to the north, south, east, and west.
Rooms also have a list of items that are currently in the room.  As the game
ensues, items can be picked up and dropped, so this list will change over
time.  I guess you could change the room connections also - wouldn't be hard
- perhaps as a result of using a special Item while in the Room.

Items are fairly passive, free-standing objects, containing some attributes,
and a possible useAction.  They don't have much behavior, they don't know
what room they are in, they can be picked up, dropped, and used, and they
have a name that describes them when you look in a room, or list your
player's inventory.

Player is the "status" object of the game.  Player has an inventory of
Items, and has a reference to the Room the player is currently in.  I think
an easy mistake when writing a game is to make the Player status and
attributes global variables.  This will work okay, but by keeping this info
in an object, the game could easily extend to having multiple players, just
by adding a second instance, and adding support for the players to take
turns giving commands.

Command is the class that actually makes things happen.  Command itself is
an "abstract" class, that defines the basic form of what different commands
can do, and how they are created.  There are several subclasses of Command:
- TakeCommand
- DropCommand
- InventoryCommand
- UseCommand
- LookCommand
- DoorsCommand
- MoveCommand
- HelpCommand
- QuitCommand

Commands are created based on the input that the game player types in at the
game prompt (this is where pyparsing comes in).  The pyparsing grammar
parses the input string, and if it is a valid command, the grammar's parse
actions create the appropriate Command subclass.  For instance, typing in
"help" will create a HelpCommand instance.  Typing in "take shovel" will
create a TakeCommand, with the target object of "shovel".  After the Command
is created, it is executed against the Player object.  The results of the
Command can:
- have the Player take something from the current Room
- have the Player drop something in his inventory, and leave it in the
current Room
- list the Player's inventory
- etc.
The MoveCommand will move the player to an adjoining room.

To tie it all together, the game engine runs in a basic loop:

# create a player, let's call him Bob
player = Player("Bob")

# give Bob the sword for protection
player.take( Item.items["sword"] )

# read commands, and then invoke them on Bob (and his surroundings)
while not player.gameOver:
cmdstr = raw_input(">> ")
cmd = parser.parseCmd(cmdstr)
if cmd is not None:
cmd.command( player )

And that's it.  All of the logic about the moving from room to room is
captured in the N,S,E,W references between Room objects.  Moving Bob from
room to room is done by MoveCommands, as they are dynamically created based
on user input.  

I hope that gives you a little more idea of how the pyparsing adventure game
works.

-- Paul

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


[Tutor] How do I add uvs to each vert line if a texture/bump map is used?

2007-09-14 Thread Paul Coones
C3DModel
4
2
0,50
Cube
1
1
8,1,12,0
v1
1.00,0.500,0.500,0,1,NULL.TIF
"null.bump.tif"
-7.028856,3.349522,4.785803,0.23,0.46, 
-0.46,0.00,0.00
-7.028856,1.349522,4.785803,0.816492,-0.408246, 
-0.408246,0.00,0.00
-9.028855,1.349522,4.785803,-0.577349,-0.577349, 
-0.577349,0.00,0.00
-9.028855,3.349522,4.785803,-0.577349,0.577349, 
-0.577349,0.00,0.00
-7.028855,3.349521,6.785803,0.46,0.23,0.46,0.00,0.00
-7.028856,1.349521,6.785803,0.408246, 
-0.816492,0.408246,0.00,0.00
-9.028855,1.349522,6.785803,-0.408246, 
-0.408246,0.816492,0.00,0.00
-9.028855,3.349522,6.785803, 
-0.46,0.46,0.23,0.00,0.00
  4, 0, 7
  0, 3, 7
  2, 6, 7
  2, 7, 3
  1, 5, 2
  5, 6, 2
  0, 4, 1
  4, 5, 1
  4, 7, 6
  4, 6, 5
  0, 1, 2
  0, 2, 3
Plane
1
1
4,1,2,0
v1
1.00,0.500,0.500,0,1,NULL.TIF
"null.bump.tif"
0.991230,-2.242427,0.00,0.00,0.00,1.00,0.00,0.00
0.991230,-4.242427,0.00,0.00,0.00,1.00,0.00,0.00
-1.008770, 
-4.242427,0.00,0.00,0.00,1.00,0.00,0.00
-1.008770, 
-2.242426,0.00,0.00,0.00,1.00,0.00,0.00
  0, 3, 2
  0, 2, 1

NOTE: each vert line contains x,y,z,nx,ny,nz,u,v
Location of smf_export.py file on savefile:
http://www.savefile.com/files/1054095

Thanks, Paul ( upretirementman )

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


[Tutor] writing the correct map names

2007-09-14 Thread Paul Coones

# Test for maps
#if faceUV = 0:

texture_map = "NULL.TIF"

if me.hasVertexUV():
print "must be a 
texture map!"
texture_map = 
current_obj.name
texture_map = texture_map 
+ ".TIF"



smf_file.write(str(texture_map 
+ '\n'))

#if not map:
bump_map = '"null.bump.tif"'
if me.hasVertexUV():
print"must be a bump 
map!"
bump_map = 
current_obj.name
bump_map = bump_map + 
"_bump.TIF"


smf_file.write(str(bump_map + 
'\n'))

1.00,0.500,0.500,0,1,NULL.TIF
"null.bump.tif"
-7.028856,3.349522,4.785803,0.23,0.46, 
-0.46,0.00,0.00


When I do have a mapped object, the script is not putting in the map  
name nor the bump map name.
What am I missing? The python script is for the 3D graphics program  
Blender, which is free.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beat me over the head with it

2007-12-11 Thread Paul Schewietzek
Theyain schrieb:
> I'm not sure if this is really the place to do this, but I will ask anyways.
> 
> Hello everyone, names Theyain.  I want to learn Python.  But I am one of 
> those people who needs some one to "Beat me over the head" to actually 
> learn something.  I can't get myself to actually sit down and read a 
> tutorial on Python because no one is making me.  So can someone help me 
> with this?  I really want to learn python, but I can't do it with out help.
> 
> 
> So help?
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

Reminds me of the all-queen problem... :)

If you know chess, you could try the following:

Step 1: Arrange 8 queens on a chessboard in a way that no queen is 
threatened by any other.

Step 2: Write a script that prints out all possible solutions to this 
problem. Can be printed in text-mode so you don't have to worry about 
real graphics when starting, like this:

 ---
| | | | | | | | |
 ---
| | | | | | | | |
 ---
| | | |X| | | | |
 ---
| | | | | | | | |
 ---
| |X| | | | | | |
 ---
| | | | | | | | |
 ---
| | | | | |X| | |
 ---
| | | | | | | | |
 ---

where X is a queen - of course there shall be eight (8) X'es :)

Step 3: Make the board scalable (6 queens on a 6x6 board, 12 queens on a 
12x12 board, n queens on a n x n board) - Hint: 4 is the minimum, 
smaller setups have no solution.

This exercise isn't really meant to learn a specific language, but 
rather to learn how to solve a problem algorythmically - However it WILL 
give you a feeling for Python, tracing back the bugs you will encounter :)

If you feel it is to hard, write back and I'll give you hints how to 
structure the script (logically - I definitively have no time for syntax 
questions. And by the way, I am relatively new to Python myself :))

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


[Tutor] Handling MySQLdb exceptions

2007-12-19 Thread Paul Schewietzek
Hi there!

I'm writing a script that inserts data from a .csv file into a 
MySQL-Database.

Actually, it works fine (the data make it into the database correctly), 
however everytime it runs it raises an exception coming from the 
MySQLdb-module.

Here's the code:

--
#!/usr/bin/env python

import MySQLdb
import sys

if len(sys.argv) <> 2:
 print("""Usage: put_data_into_pool 
 Columns need to be:
 title, firstname, lastname, street, number of 
house, postal code, city, phone number""")
 sys.exit(0)

tabelle = open(sys.argv[1], "r")

db = MySQLdb.connect(host="localhost", user="user", passwd="", 
db="db")

cursor = MySQLdb.cursors.Cursor(db)

line = tabelle.readline()

while line <> "":
#try:
 cursor.execute('INSERT INTO pool (titel, vorname, 
nachname, strasse, hausnummer, plz, ort, rufnummer, datum) VALUES (' + 
line + ');')
 line = tabelle.readline()
#except(_mysql_exceptions.OperationalError):
#pass

tabelle.close()
--

The exception goes like this:

--
Traceback (most recent call last):
   File "bin/auftragserfassung/put_data_into_pool.py", line 22, in 
 cursor.execute('INSERT INTO pool (titel, vorname, nachname, 
strasse, hausnummer, plz, ort, rufnummer, datum) VALUES (' + line + ');')
   File "/var/lib/python-support/python2.5/MySQLdb/cursors.py", line 
166, in execute
 self.errorhandler(self, exc, value)
   File "/var/lib/python-support/python2.5/MySQLdb/connections.py", line 
35, in defaulterrorhandler
 raise errorclass, errorvalue
_mysql_exceptions.OperationalError: (1136, "Column count doesn't match 
value count at row 1")
---

Is there any way to handle this exception? As you can see, I already 
tried it with _mysql_exceptions.OperationalError (the lines that are 
commented out), but _mysql_exceptions is not defined to Python

Just so you don't need to wonder: The .csv-file I give to the script for 
testing is absolutely OK.

On a side note, would you think I should post this somewhere else? If 
so, where?

Any help is appreciated - I'll answer tomorrow (have to go now).

Kindest regards, Paul
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Handling MySQLdb exceptions

2007-12-20 Thread Paul Schewietzek
Joshua Simpson schrieb:
> On Dec 19, 2007 10:14 AM, Paul Schewietzek <[EMAIL PROTECTED] 
> <mailto:[EMAIL PROTECTED]>> wrote:
> 
> 
> Is there any way to handle this exception? As you can see, I already
> tried it with _mysql_exceptions.OperationalError (the lines that are
> commented out), but _mysql_exceptions is not defined to Python
> 
> 
> "OperationalError" is contained in the MySQLdb module and thus 
> namespace, so you'll have to reference it like so:
> 
> except MySQLdb.OperationalError:
> 
> 
> -- 
> -
> http://stderr.ws/
> "Insert pseudo-insightful quote here." - Some Guy

Thanks a lot! It works 8D

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


Re: [Tutor] Handling MySQLdb exceptions

2007-12-20 Thread Paul Schewietzek
Kent Johnson schrieb:
> A more robust solution would be to read the file with the csv module and 
> use cursor.execute() with a proper parameter list. This lets the csv and 
> database modules correctly (un)escape the data values.
> 
> Kent
> 

WOW thanks! I never thought about that there might be a csv-module! I'm 
on my way exploring it ;)

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


Re: [Tutor] Careful Dictionary Building

2007-12-29 Thread Paul McGuire
1. Don't name your dict 'dict' or your list 'list', as this then masks the
builtin dict and list types.
2. Your application is a textbook case for defaultdict:

from collections import defaultdict

recordDict = defaultdict(list)
for record in recordList:
recordDict[record[0]].append(record)

Voila!  No key checking, no keeping of separate key lists (wrong for many
other reasons, too), just let defaultdict do the work.  If the key does not
exist, then defaultdict will use the factory method specified in its
constructor (in this case, list) to initialize a new entry, and then the new
record is appended to the empty list.  If the key does exist, then you
retreive the list that's been built so far, and then the new record gets
appended.

-- Paul

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


Re: [Tutor] parsing html.

2008-01-16 Thread Paul McGuire
Here is a pyparsing approach to your question.  I've added some comments to
walk you through the various steps.  By using pyparsing's makeHTMLTags
helper method, it is easy to write short programs to skim selected data tags
from out of an HTML page.

-- Paul


from pyparsing import makeHTMLTags, SkipTo

html = """
Table of Contents
.
Preface
"""

# define the pattern to search for, using pyparsing makeHTMLTags helper
# makeHTMLTags constructs a very tolerant mini-pattern, to match HTML
# tags with the given tag name:
# - caseless matching on the tag name
# - embedded whitespace is handled
# - detection of empty tags (opening tags that end in "/")
# - detection of tag attributes
# - returning parsed data using results names for attribute values
# makeHTMLTags actually returns two patterns, one for the opening tag
# and one for the closing tag
aStart,aEnd = makeHTMLTags("A")
bStart,bEnd = makeHTMLTags("B")
pattern = aStart + aEnd + bStart + SkipTo(bEnd)("text") + bEnd

# search the input string - dump matched structure for each match
for pp in pattern.searchString(html):
print pp.dump()
print pp.startA.name, pp.text

# parse input and build a dict using the results
nameDict = dict( (pp.startA.name,pp.text) for pp in
pattern.searchString(html) )
print nameDict


The last line of the output is the dict that is created:

{'5': 'Preface', '4': 'Table of Contents'}




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


Re: [Tutor] Saving Objects

2008-01-17 Thread Paul McGuire
>>>
My initial tests using pickle and a simple class system (shown below) have
failed. The method shown below fails with a AttributeError: 
'FakeModule' object has no attribute 'Spod', so when I create a an empty
class Spod in the new session, it generates an IndexError:(list index out of
range)

Is there a better way to do this?
>>>

I'm assuming that "# New session" marks the beginning of a separate Python
file.  The problem is that the Spod class definition is not included in the
pickle file, so you must import that as well, before loading back the
pickled fish object.  Do this;

1. Put Spod in its own module, let's call it spod.py.  (While you are at it,
have Spod derive from object, so that you get a new-style class.)
2. Create test1.py to pickle a Spod.  Have test1.py import spod, and then
have your code that creates spod.Spod("andy") and pickles it to test.pickle.
3. Create test2.py to unpickle a Spod.  Have test2.py *also* import spod,
and then have the rest of your code that follows "# New session".  Modify
the pickle.load statement to save the result to a variable, and then you can
verify that its name is "andy", type is "Spod", etc.

-- Paul


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


[Tutor] web gallery

2005-08-15 Thread paul . hendrick
Hi All,
I'd like to write some web gallery software from scratch for my
girlfriend. I've looked at the PIL (python imaging library) and I'm
using it to generate thumbnails from given images.
Something else i'd like to get done now though, is building an index of
images.
Are there any libs suited for this? basically I want an text index of
images, which contains the main image name, the thumbnail name, and a
description.

a new image can be added to the index either by adding it via ftp, or
through an html upload form with the comment in the form.

So basically, i'm after advice on indexes.

thanks for reading,
Paul

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


Re: [Tutor] Using superclass __init__ method

2005-09-22 Thread paul brian
There are several areas this seems to touch upon, most of them well
covered by Guido himself in http://www.python.org/2.2/descrintro.html

firstly to call a super class' methods you get the subclass to refer
to the *superclass* then the method (note not to the superclass
instance)

from newstyle tutorial (comment mine):

class B:
def m(self):
print "B here"

class C(B):
def m(self):
print "C here"
B.m(self)   #refer to the class definition not the
instance of C via self.


Secondly to run __init__ as you have described is normal behaviour
because the __call__ function that every new instance must go through
makes sure that __init__ is called, and as there is not one defined in
subclass it uses the one in superclass.

This should have the same effect, but is more controllable:

class Base:
   def __init__(self):
   print "hello"

class Child(Base):
   def __init__(self):
   Base.__init__(self)

produces::

>>> c = Child()
hello


This might also help make things clearer

class Base:
   def __init__(self):
   print "hello"

class Child(Base):
   def __init__(self):
   Base.__init__(self)

   def foo(self):
  print "foo"

c = Child()
b = Base()

x = Child()
x.foo()
Child.foo(x) #1
Child.foo(b) #2


The class defintion of Child is an object (since 2.2) and can be
called with parameters. at #1 we see foo printed out because we have
called the Child object method foo with an object of type Child (this
is essentially self)

Of course we can try passing in another object (#2) but it will barf.


This issue is frankly just muddy. Not because of bad design or poor
documentation but because it is a awkward subject.  Read the link
above - most of us mortals have had to read it several times. Things
also get a little more complex with __new__ but its all in there


However I have not found a really good explanation of the whole
instance creation thing - perhaps this list could make one?

--
Paul Brian
m. 07875 074 534
t. 0208 352 1741
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] time challange

2005-09-22 Thread paul brian
You are fairly close

>>> t1 = today()
>>> t1

>>> t2 = today() + RelativeDateTime(hours=20)
>>> t2

>>> t3 = t2 - t1
>>> t3.hours
20.0
>>> slice = t3/20
>>> slice


t3 is a "Delta" - that is an abstract representation
of time - it is not the 20 hours since midnight, just 20 hours
at any time in the universe.
slice is just 1/20th of that same abstract time. But because of the munificence
of Marc we can add that abstract hour to a real fixed time (ignoring
Einstein of course)

>>> t1 + slice


And so that is a "real" datetime 1/20th of the way forward from t1

so a simple loop will get you your 20 evenly spaced time periods,
which is what i think you were asking for.

cheers



On 9/22/05, nephish <[EMAIL PROTECTED]> wrote:
> Hey there,
>
> i use mx.DateTime.RelativeDateTimeDiff to get the difference between
> date_x and date_y.
> what i need to do divide this amount of time into 20 different times
> that spaced out between the date_x and the date_y.
>
> so if the difference between date_x and date_y is 20 hours, i need 20
> DateTimes that are one hour apart from each other. If the difference is
> 40 minutes, i need the 20 DateTimes to be spaced out 2 minutes from each
> other..
>
> what would be a way to pull this off? i have looked at the docs for
> mxDateTime
> http://www.egenix.com/files/python/mxDateTime.html
> and there seems to be a divide operation, but i dont quite know what it
> is talking about
> with the deltas.
>
> anyone have a good start point?
>
> thanks
> shawn
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


--
--
Paul Brian
m. 07875 074 534
t. 0208 352 1741
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python DB

2005-09-22 Thread paul brian
I would look quite seriously at SQLObject

It is a bit rough round the edges but given a class defintion, it then
creates the underlying SQL tables and handles the CRUD (create update
delete) for you.

I do not like this approach and prefer the other but equally nice
ability it has to look at a database and create appropriate classes,
which you can then use.
It is a class of Object Relational mapper, of which there are a few
Java production level ones but I do not know of any "real world" uses
of SQL Object. Perhaps others do.

Backups and access from other applications imply you want a fairly
serious admin load afterwarss - in which case I would think very
carefully before using anything other than a "real" RDBMS (Oracle,
MySQL, Postgres MSAccess even). Backups and synchronous access are
what they are designed to do.

However are you quite sure a normal db table will not suffice - I tend
to find that the underlying data structure is never as fluid as one
suspects.
Patient - disease - treatment sounds a fairly striaghtforward domain. 
Perhaps if you shared a few things that make you worried it will
change dramatically someone might be able to suggest workable
structures.

On 9/22/05, Matt Williams <[EMAIL PROTECTED]> wrote:
> Dear List,
>
> Thanks for all the advice! Obviously, I'm still a bit torn, but some of
> the ideas looked good.
>
> In terms of spec, the DB can be fairly simple (single access, etc.).
> Lower dependency on other libraries is good. Also, it needs to be cross-
> platform.
>
> The problem (I have) with SQL-type DB is that I cannot be sure ahead of
> time of the exact data structure. The DB will be about patients, who
> have diseases, and also have treatments.Clearly, I can't specify now
> the exact structure of the table. The advantage of SQL is that you can
> (in general) do things like constrain types for fields, and give
> enumerated options, which makes the data more consistent.
>
> The thing I liked about KirbyBase was that it used text files. This is a
> real advantage, as it means that I can access the data from other
> application easily, and also makes it easier to back-up (can just copy a
> few files). The same would seem to be true of the XML-based options. The
> advantage of ZODB was that the "object" structure seemed to map well to
> the concept of patients, with diseases, with treatments, etc. (and
> Shelve would work at least as a trial implementation)
>
> The final thing is that I needs to have a simple GUI frontend. The nice
> thing about ZODB is that I could just map the eventhandlers to functions
> on objects..
>
> If people have more comments in the light of the bigger spec. above, I'd
> still love to hear them...
>
> Matt
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


--
--
Paul Brian
m. 07875 074 534
t. 0208 352 1741
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] simplifying with string-formatting operator

2005-09-23 Thread paul brian
I would suggest you use the built in datetime modules or egenix mx.DateTime.

for example

>>> import datetime
>>> year = "05"; month="09";day="23"
>>> dateobj = datetime.date(int(year)+2000, int(month), int(day))
>>> dateobj.strftime("%A %B %y")
'Friday September 05'
>>>

obviously you need to look at how the incoming 2 digit strings are
checked and so on.

By the way the strftime("%A %B %y") tells python to print out the date
as %A which is locale weekday etc etc. with the %A or %B being
replaced in a similar way to %s in normal string formatting.

strftime is well documented in the time module docs.

cheers


On 9/23/05, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> Hello
>
> Does anyone have any idea on how i could simplify the following program by
> using strings?
>
> # dateconvert2.py
> #Converts day month and year numbers into two date formats
>
> import string
>
> def main():
># get the day month and year
>day, month, year = input("Please enter the day, month and year numbers:
> ")
>
>date1 = str(month)+"/"+str(day)+"/"+str(year)
>
>months = ["January", "February", "March", "April",
>  "May", "June", "July", "August",
>  "September", "October", "November", "December"]
>    monthStr = months[month-1]
>date2 = monthStr+" " + str(day) + ", " + str(year)
>
>print "The date is", date1, "or", date2
>
> main()
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>


--
--
Paul Brian
m. 07875 074 534
t. 0208 352 1741
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with pi and the math module.

2005-09-26 Thread paul brian
it might be a little clearer if you look at sys.modules


In the sys module is a useful dictionary called "modules"
This dictionary maps all the names of modules we import, to the objects
that are those modules.

for example (I am only importing pprint to make it easier to read)

>>> import sys, pprint
>>> pprint.pprint( sys.modules )
{'UserDict': ,
 '__builtin__': ,
 '__main__': ,
 'copy_reg': ,
 ...
 'sys': ,
 'types': ,
 'warnings': ,
 'zipimport': }


So how does an import change this "namespace"?

Well, lets try one. I am going to choose the math module, becasue it has an
easy to spot constant in there called pi.

>>> import math
>>> pprint.pprint( sys.modules )
...
 'math': ,
...

aha - that was not there earlier.

>>> import sys, pprint
>>> import math
>>> sys.modules['__main__'].__dict__['pi']
Traceback (most recent call last):
  File "", line 1, in ?
KeyError: 'pi'
>>> sys.modules['__main__'].__dict__['math']

>>> math.pi
3.1415926535897931


So in the namespace of __main__ (where we run the interpreter) there
exists a module named 'math', and this module holds in its namespace a constant
called pi.

Now lets restart our python interepreter and try again

>>> import sys, pprint
>>> from math import *
>>> import sys, pprint
>>> pprint.pprint( sys.modules )
...
 'math': 
...

There it is again. ?

Now if i have a look at the __main__ namespace (its __dict__)

>>> sys.modules['__main__'].__dict__['math']
Traceback (most recent call last):
  File "", line 1, in ?
KeyError: 'math'
>>> sys.modules['__main__'].__dict__['pi']
3.1415926535897931

math is not in the namespace, but pi is directly there.
Thats the difference between

import math which imports a module into the current namespace

and

from math import *
 which imports all the contents of math into the namespace

one last thing

>>> import random as offthewall
>>> pprint.pprint( sys.modules )
...
 'pprint': ,
 'random': ,
...
>>> random.randint(1,10)
Traceback (most recent call last):
  File "", line 1, in ?
NameError: name 'random' is not defined

Whoops - but it is clearly shown in the sys.modules. That is correct - because
we have imported the module (file) called random.  However when I am in
the __main__ namespace and do random.randint(1,10), Python tries to find
"random" in the __main__namespace.

>>> sys.modules['__main__'].__dict__['offthewall']

>>> sys.modules['__main__'].__dict__['random']
Traceback (most recent call last):
  File "", line 1, in ?
KeyError: 'random'


We have imported the module random but with a name of offthewall
so

>>> offthewall.randint(1,10)
1
>>> offthewall.randint(1,10)
8

works fine.



On 9/24/05, Pujo Aji <[EMAIL PROTECTED]> wrote:
> hi,
>
> if you use : import math
> you can type: diameter * math.pi
>
> if you use from math import *
> you can type: diameter * pi
>
> Cheers,
> pujo
>
>
> On 9/24/05, Nathan Pinno <[EMAIL PROTECTED]> wrote:
> >
> >
> > Hi all,
> >
> > I need help with pi and the math module. I had import math at the top of a
> program, but when it came to diameter*pi, it said that pi was not defined.
> >
> > How do I use it correctly?
> >
> > Thanks,
> > Nathan Pinno
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
> >
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>


--
--
Paul Brian
m. 07875 074 534
t. 0208 352 1741
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] HTTP GET Request

2005-09-27 Thread paul brian
> Basicall, what I need is a stand alone CGI.  Instead of the program passing
> the data off to a CGI, I want it to parse and handle the request directly.

instead of which program ?

Http requests are served by a web server (ie Apache), which depending
on the type of request passes the request to wherever.

As such any HTTP request *must* be handled first by a web server, and
cgi scripts traditionally lived in cgi-bin directory on the server so
a URL would look like http://www.example.com/cgi-bin/myscript.py

I think you have 3 options

1. use the cgi module in python to create scripts like the one above.
They will not be fast but it gives you a lowlevel access to the request
However cgi was out of date about 8 years ago - it has some
seriouslimitations mostly on speed/capacity.

2. use a system like mod_python. This is better than cgi for lots of reasons,
mostly to do with speed. Here you also have access to the request 
 objects,   but there is a bit of a learning curve.

3. Zope - higher level than even mod_python and still more of a learning curve

(there is a multitude of python based cgi repalcements, Django,
webware and others spring to mind.  But there is no clear "winner"
amoungst the community)

I would recommend that you look at taking a weekend to install apache,
and play with both the cgi module and mod_python.  mod_python is
pretty good and fairly well documented, as well as being pretty low
level.

I think there is a lot to do here - perhaps if you tell us exactly
what you need we can point you at a solution. Some web hosters provide
mod_python or zope hosting and that might be a way to get up and
running faster.





On 9/27/05, Jerl Simpson <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I have been looking through some of the HTTP projects and haven't quite
> found what I'm looking for.
> Basicall, what I need is a stand alone CGI.  Instead of the program passing
> the data off to a CGI, I want it to parse and handle the request directly.
>
> The part I'm having trouble with is actually getting the request and parsing
> it.
>
> Let's say I have a URI that looks like:
> ?var1=val1&var2=val2&...varn=valn
>
> I'd like to find a way to get these into some datastructure so I can use
> them to generate my output.
>
> It seems like a simple thing, but as I'm new to python, I don't know where
> to start.
>
> Thank you for any help you can give.
>
>
> Jerl
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>


--
--
Paul Brian
m. 07875 074 534
t. 0208 352 1741
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] find data in html file

2005-09-30 Thread paul brian
> But to get the data like price,bidders,shipment etc without the official
> eBayAPI is hard.
> Maybe anyone has a solution made ?

 Ebay specifically change around their HTML codes, tags and formatting
 (in quite a clever way) to stop people doing exactly what you are
 trying to do. I think it changes every month.

 Like people say, use the API - You need to become an "ebay developer"
 (signup) and can use your own code or the python-ebay thing for free
 in "the sandbox", but must pay $100 or so to have your code verified
 as "not likey to scrunch our servers" before they give you a key for
 the real world.

 Its a bit of a pain, so i just hacked turbo-ebay a while back and made
 do.  Worked quite well really.


--
--
Paul Brian
m. 07875 074 534
t. 0208 352 1741
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beautiful soup

2005-10-04 Thread paul brian
How did you change it to look at the file on your PC?
You appear to have told urllib2 to use "FRE_word_list.htm", it cannot
find that online so tried to look for it on your local disk at
'\\C:\\Python24\\FRE_word_list.htm

I would suggest that you either put your local html on a web server
and send in that local URL or replace html =
urllib2.urlopen(url).read() with
html = open(r'c:\myfolder\myfile.html').read()  and see where that takes you.

cheers




On 10/4/05, David Holland <[EMAIL PROTECTED]> wrote:
> I tried to use this script which I found on the web :-
> import urllib2, pprint
> from BeautifulSoup import BeautifulSoup
>
>
> def cellToWord(cell):
>   """Given a table cell, return the word in that
> cell."""
>   # Some words are in bold.
>   if cell('b'):
>  return cell.first('b').string.strip()  #
> Return the bold piece.
>   else:
>  return cell.string.split('.')[1].strip()   #
> Remove the number.
>
>
> def parse(url):
>   """Parse the given URL and return a dictionary
> mapping US words to
>   foreign words."""
>
>
>   # Read the URL and pass it to BeautifulSoup.
>   html = urllib2.urlopen(url).read()
>   soup = BeautifulSoup()
>   soup.feed(html)
>
>
>   # Read the main table, extracting the words from
> the table cells.
>   USToForeign = {}
>   mainTable = soup.first('table')
>   rows = mainTable('tr')
>   for row in rows[1:]:# Exclude the first
> (headings) row.
>  cells = row('td')
>  if len(cells) == 3:  # Some rows have a
> single colspan="3" cell.
> US = cellToWord(cells[0])
> foreign = cellToWord(cells[1])
> USToForeign[US] = foreign
>
>
>   return USToForeign
>
>
> if __name__ == '__main__':
>   url =
> 'http://msdn.microsoft.com/library/en-us/dnwue/html/FRE_word_list.htm'
>
>   USToForeign = parse(url)
>   pairs = USToForeign.items()
>   pairs.sort(lambda a, b: cmp(a[0].lower(),
> b[0].lower()))  # Web page order
>   pprint.pprint(pairs)
>
> and it works well.  However I change it to get it to
> look at a file on my PC, then I get this message :-
> Traceback (most recent call last):
>  File "C:\Python24\beaexp2", line 43, in -toplevel-
>USToForeign = parse(url)
>  File "C:\Python24\beaexp2", line 20, in parse
>html = urllib2.urlopen(url).read()
>  File "C:\Python24\lib\urllib2.py", line 130, in
> urlopen
>return _opener.open(url, data)
>  File "C:\Python24\lib\urllib2.py", line 358, in open
>response = self._open(req, data)
>  File "C:\Python24\lib\urllib2.py", line 376, in
> _open
>'_open', req)
>  File "C:\Python24\lib\urllib2.py", line 337, in
> _call_chain
>result = func(*args)
>  File "C:\Python24\lib\urllib2.py", line 1119, in
> file_open
>return self.open_local_file(req)
>  File "C:\Python24\lib\urllib2.py", line 1135, in
> open_local_file
>stats = os.stat(localfile)
> OSError: [Errno 2] No such file or directory:
> '\\C:\\Python24\\FRE_word_list.htm
> Any idea how to solve it ?  The file is on my PC.
>
> I am using Python 2.4 on Win XP.
>
> Thanks in advance.
>
> David
>
>
>
> ___
> How much free photo storage do you get? Store your holiday
> snaps for FREE with Yahoo! Photos http://uk.photos.yahoo.com
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


--
--
Paul Brian
m. 07875 074 534
t. 0208 352 1741
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] need help to understand terms for desinging a program

2005-10-05 Thread paul brian
This is a pretty big question, and it would be easier to answer if you
can give us more details about what you want to do.

It seems you want to inspect outgoing mails (possibly rewrite their
addresses?) before the mails are sent out.

If you are looking to send out emails to a list then I suggest looking
at mailman - a python mailing list manager.

If you want to use python to examine mail messages and do not care
what mailserver (MTA) you use you could try exim instead of qmail and
then try exim-python. 
http://botanicus.net/dw/exim-python/exim-4.32py1.html





On 10/5/05, Hameed U. Khan <[EMAIL PROTECTED]> wrote:
> Hi,
>  I need to make a program which will accomplish following. I dont
> need the code. I want to make it myself. But I need guidance from you
> people because I am not a good programmer. I need help in
> understanding following terms.
>
>   " qmail-queue  reads  a  mail message from descriptor 0.  It
>   then reads envelope information  from  descriptor  1.   It
>   places  the  message  into  the  outgoing queue for future
>   delivery by qmail-send.
>
>   The envelope information is  an  envelope  sender  address
>   followed  by  a list of envelope recipient addresses.  The
>   sender address is preceded by the letter F and  terminated
>   by  a  0  byte.  Each recipient address is preceded by the
>   letter T and terminated by a 0 byte.  The list of  recipi-
>   ent addresses is terminated by an extra 0 byte.  If qmail-
>   queue sees end-of-file before the extra 0 byte, it  aborts
>   without placing the message into the queue."
>
>  I want to inspect messages before passing them to actuall qmail-queue
> program.
> Thanks in advance for your help.
> --
> Regards,
> Hameed U. Khan
> Registered Linux User #: 354374
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


--
--
Paul Brian
m. 07875 074 534
t. 0208 352 1741
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] need help to understand terms for desinging a program

2005-10-05 Thread paul brian
I would suggest that you look firstly at qmails own built in ruleset.
something like that must be achieveable without writing a special plugin.

qmail control files are usually in /var/qmail/control. Nothing obvious
in the man pages i am afraid.

Good luck


On 10/5/05, Hameed U. Khan <[EMAIL PROTECTED]> wrote:
> On 10/5/05, paul brian <[EMAIL PROTECTED]> wrote:
> > This is a pretty big question, and it would be easier to answer if you
> > can give us more details about what you want to do.
> >
> > It seems you want to inspect outgoing mails (possibly rewrite their
> > addresses?) before the mails are sent out.
> >
> > If you are looking to send out emails to a list then I suggest looking
> > at mailman - a python mailing list manager.
> >
> > If you want to use python to examine mail messages and do not care
> > what mailserver (MTA) you use you could try exim instead of qmail and
> > then try exim-python.
> > http://botanicus.net/dw/exim-python/exim-4.32py1.html
> >
> >
> >
> >
> >
> > On 10/5/05, Hameed U. Khan <[EMAIL PROTECTED]> wrote:
> > > Hi,
> > >  I need to make a program which will accomplish following. I dont
> > > need the code. I want to make it myself. But I need guidance from you
> > > people because I am not a good programmer. I need help in
> > > understanding following terms.
> > >
> > >   " qmail-queue  reads  a  mail message from descriptor 0.  It
> > >   then reads envelope information  from  descriptor  1.   It
> > >   places  the  message  into  the  outgoing queue for future
> > >   delivery by qmail-send.
> > >
> > >   The envelope information is  an  envelope  sender  address
> > >   followed  by  a list of envelope recipient addresses.  The
> > >   sender address is preceded by the letter F and  terminated
> > >   by  a  0  byte.  Each recipient address is preceded by the
> > >   letter T and terminated by a 0 byte.  The list of  recipi-
> > >   ent addresses is terminated by an extra 0 byte.  If qmail-
> > >   queue sees end-of-file before the extra 0 byte, it  aborts
> > >   without placing the message into the queue."
> > >
> > >  I want to inspect messages before passing them to actuall qmail-queue
> > > program.
> > > Thanks in advance for your help.
> > > --
> > > Regards,
> > > Hameed U. Khan
> > > Registered Linux User #: 354374
> > > ___
> > > Tutor maillist  -  Tutor@python.org
> > > http://mail.python.org/mailman/listinfo/tutor
> > >
> >
> >
> > --
> > --
> > Paul Brian
> > m. 07875 074 534
> > t. 0208 352 1741
> >
>
> Thanks paul for you quick reply but I have to stick with qmail. I want
> to check envelopes when user attempts to send message. Because our
> company want to restrict some users to send message to selected
> domains only. I've been given this task. Anyway thanks again for your
> time.
> --
> Regards,
> Hameed U. Khan
> Registered Linux User #: 354374
> -
> *Computer without Linux is just like the world without computer.*
>


--
--
Paul Brian
m. 07875 074 534
t. 0208 352 1741
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Accessing Variables

2005-10-05 Thread paul brian
Sort ofnot really

When a module is imported the following things happen

import ONE

a module object (dict essentially) named ONE is created. This is the
module ONE.py's namespace.
The module object is added to sys.modules
the code in the object is executed inside the ONE dict (namespace)

now if ONE has at the top
import os

ONE is able to use the line
os.path.join (mydir, "foo.txt")

If however we do not have the line import os we will get an error
as the namespace of one does not have a reference to the module object of os

as an example import your one.py and run

pprint.pprint(sys.modules['one'].__dict__)

You will see what a virgin namesapce looks like
- there is a lot in there but it boils down to
__builtins__, __doc__, __file__, __name__ and whatever you define (_-dict__)

care notes
---
You can easily get into circular references with imports, simply
because code at the module level (ie not in a function or class) will
be executed at the first import - and if it calls code that is in the
next module which waits on code in the first etc etc.

So as you said, put everything into classes or functions or be very
careful with your imports.

(I am not sure I answered the question but it is late now...:-)

yrs



On 10/5/05, Matt Williams <[EMAIL PROTECTED]> wrote:
> Dear List,
>
> I'm trying to clarify something about accessing variables.
>
> If I have ONE.py file with some variable a, and ONE imports TWO, which
> has a variable b, can TWO access variable a (I don't think so, but I
> just thought I'd check).
>
> I guess the way round this is just to make some classes & objects, and
> then they can easily pass parameters to each other, but I just thought
> I'd check.
>
> Matt
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


--
--
Paul Brian
m. 07875 074 534
t. 0208 352 1741
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] and-or precedence

2005-10-10 Thread paul brian
 has a higher priority than  (as shown on the page you
referenced -its "ascending priority"). Perhaps that could be clearer.

I find that brackets always make life easier in these cases (a rare
statement in the Python world :-), and on a personal note I would
always always comment heavily boolean operations that are not
immediately obvious. it saves brain ache later on.

>>> ((1 or 2) and 3)
3

However 1 or 2 and 3 is <1> if  is evaluated first.

cheers


On 10/10/05, Krishna <[EMAIL PROTECTED]> wrote:
> >>> 1 or 2 and 3
> 1
>
> Why does the above expression return 1? As per my understanding of
> boolean operations, this is what should have happaned:
>
> 1 or 2 => 1 and then
> 1 and 3 => 3
>
> The library reference also suggests that 'or' has higher priority than 'and'.
> http://docs.python.org/lib/boolean.html
>
> Thanks
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


--
--
Paul Brian
m. 07875 074 534
t. 0208 352 1741
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Walk a dictionary recursively

2005-10-11 Thread paul brian
Firstly are you using this to store or alter data regarding Microsoft
Active Directory?. If so I suggest you look at some of their ADSI /
WMI interfaces for COM (if you use win32com from Mark Hammond or
activeState life becomes a lot easier. Well the Windows programming
part of it does)

As for the particulars of your question, you might find life simpler
if you created a "parentID" (and/or childID) for each unique entry in
the tree.

As you are going to be growing the data list in size one other area to
look at is generators - this will enable  you to walk arbitrarily
large trees but with a far lower memory footprint and hence a lot
faster.  A generator class returns an object that will walk through an
iteration set (like a for loop) but at the end of every step will
"disappear"  from the stack and when it is called again it starts
exactly where it left off.

So I would suggest you create generaotr based classes to store your
data, using an explicit parent/child relationship rather than relying
on the implicit relationships of which dictionary is stored inside
which dictionary.

It is still a far chunk of work.  I suggest you start on the parent
child thing first.
Think about having a single point of entry that creates a new object
and then "hangs" it on the tree.

I hope that helps and do please come back to the list with how you are
gettng on.



On 10/11/05, Negroup - <[EMAIL PROTECTED]> wrote:
> Hi tutors, in my application I found convenient to store all the data
> in a data structure based on a dictionary containing a lot of keys,
> and each of them host other dictionary with lists and dictionaries
> nested inside and so on...
>
> First of all I'd like to know if it is normal to use so complex data
> structures in which store data, or if it possible in some way to
> organize them using smaller "organizational units".
>
> This is instead the problem I should solve as soon as possible: I
> should apply a function, exactly the string's method decode('utf-8'),
> to  each key and value of the above descripted dictionary. Consider
> that the keys are integers or strings, and if a key is a list, I need
> to decode each contained element. Is there a way to walk recursively
> the dictionary, or should I write my own walk function?
>
> Thanks in advance,
> negroup
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


--
--
Paul Brian
m. 07875 074 534
t. 0208 352 1741
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: if-else statements

2005-10-14 Thread paul brian
My apologies - I keep failing to reply-all.

-- Forwarded message --
From: paul brian <[EMAIL PROTECTED]>
Date: Oct 14, 2005 10:04 AM
Subject: Re: [Tutor] if-else statements
To: Andre Engels <[EMAIL PROTECTED]>


I would also suggest you look at either datetime module or the
mx.DateTime modules (from egenix). They are both very good and contain
plenty of error checking for just such things, aas well as nicely
overriding operators like + (so you can add 2 weeks to a date easily).

>>> import datetime

We can try an invlaid date and trap the error (using try: Except: statements)
>>> datetime.date(2005,02,30)
Traceback (most recent call last):
 File "", line 1, in ?
ValueError: day is out of range for month

A valid date
>>> d = datetime.date(2005,02,27)

And shown in a easy to read format
>>> d.strftime("%A %B %d %Y")
'Sunday February 27 2005'

cheers

On 10/14/05, Andre Engels <[EMAIL PROTECTED]> wrote:
> 2005/10/14, [EMAIL PROTECTED] <[EMAIL PROTECTED]>:
> > Hello
> >
> > I'm having some trouble with my if, else statements. For some reason, the
> > months that have 31 days work fine, but the months that have 28/30 do not
> > work. Am I doing something wrong? it is supposed to take a date as an
> > input like 9/31/1991 and then say that the date is not valid because
> > september only has 30 days.
>
> First hint:
> Where is 'month' used in your program? The answer is: nowhere. It
> should be used. Why?
>
> Second hint:
> Currently, the program checks each date first for validity with
> respect to January, then throws the outcome away to do the same with
> February, etcetera upto December. Thus, the final outcome says whether
> the date is correct in December, not whether it is correct in the
> given month.
>
> (actual code below)
>
> > import string
> >
> > def main():
> > # get the day month and year
> > month, day, year = input("Please enter the mm, dd, : ")
> > date1 = "%d/%d/%d" % (month,day,year)
> >
> > months = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
> >
> > if day <= months[1]:
> > d = "valid"
> > else:
> > n = "not valid"
> >
> > if day <= months[2]:
> > d = "valid"
> > else:
> > d = "not valid"
> >
> > if day <= months[3]:
> > d = "valid"
> > else:
> > d = "not valid"
> >
> > if day <= months[4]:
> > d = "valid"
> > else:
> > n = "not valid"
> >
> > if day <= months[5]:
> > d = "valid"
> > else:
> > d = "not valid"
> >
> > if day <= months[6]:
> > d = "valid"
> > else:
> > d = "not valid"
> >
> > if day <= months[7]:
> > d = "valid"
> > else:
> > d = "not valid"
> >
> > if day <= months[8]:
> > d = "valid"
> > else:
> > d = "not valid"
> >
> > if day <= months[9]:
> > d = "valid"
> > else:
> > d = "not valid"
> >
> > if day <= months[10]:
> > d = "valid"
> > else:
> > d = "not valid"
> >
> > if day <= months[11]:
> > d = "valid"
> > else:
> > d = "not valid"
> >
> > if day <= months[12]:
> > d = "valid"
> > else:
> > d = "not valid"
> >
> > print "The date you entered", date1, "is", d +"."
> >
> > main()
>
> Correct and shortened code:
>
> Instead of the whole line "if day <= months[1]"... series of
> if-then-else statements, use only one statement, namely:
>
> if day <= months[month]:
>d = "valid"
> else:
>d = "not valid"
>
> --
> Andre Engels, [EMAIL PROTECTED]
> ICQ: 6260644  --  Skype: a_engels
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


--
--
Paul Brian
m. 07875 074 534
t. 0208 352 1741


--
--
Paul Brian
m. 07875 074 534
t. 0208 352 1741
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] passing variable to python script

2005-10-14 Thread paul brian
There are several approaches ranging from the simple but limited to
rather advanced.

raw_input is the simplest but it is to be deprecated, and more
importantly it limits you to entering commands after the script is
running. automation becomes harder

$ myscript  

Those args are put into a list called sys.argv where sys.argv[0] is
the name of the script you ran and any subsequent ones are sys.argv[1]
{2] etc etc

However my favourite is optparse module, which allows you to set up
what options you like (for example $ myscript -q
--outfile=/home/foo.txt) and will carefully check their validity,
provide defaults, convert to floats etc where needed, and there is a
cool easygui addon.



On guido's Artima weblog there is a good article on using special main
cases - it is pretty "deep" so do not worry if you cannot put it all
to use - at least you know where you can get to.


The above $ myscript  is the best way to call a script, because
you can run it from the command line automatically, manually, and if
you use the convention of wrapping the script in a main() function,
then putting this at the bottom of the file

if __name__ == '__main__':
  main()

(ie you have a function addTwoNumbers(a,b), and main takes args[1] and
args[2] and passes them to addTwoNumbers)

Then main() will only be run when the script is called from the
command line, meaning the same code can be import'ed and addTwoNumbers
can be called from any other python program.

HTH


On 10/14/05, Alan Gauld <[EMAIL PROTECTED]> wrote:
> > i want to pass an argument (a number) to a python
> > script when running it:
> >> python script.py 
> >
> > i want to be able to use  within script.py
> > as a parameter.
> >
> > how do i set this up?
>
> This is covered in the 'talking to the user' topic in my tutorial.
>
> The short answer is use sys.argv
>
> Alan G
> Author of the learn to program web tutor
> http://www.freenetpages.co.uk/hp/alan.gauld
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


--
--
Paul Brian
m. 07875 074 534
t. 0208 352 1741
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to create GUI for Python progs

2005-10-14 Thread paul brian
It seems to depend on what you want

easygui
---
Very simple, very easy to set up, but is NOT event driven (ie the
program uses it much like it would use raw_input but with drop boxes.

All other guis are event driven - that is the gui has the main loop,
and when it detects (a button click) it fires off a request to the
rest of the program to do something.

The main frameworks (ie they provide widgets like buttons so you do
not have to write your own button code)

pyQT
---
Extrememly well suported and mature, Has a good drag and drop
developer. Is free for Linux / open source use. Commercially is a bit
more confused.

tkinter

The default python Gui but it might be changing  - see frederick
Lundh's IDE for what can be done in it.

WxPython
-
Simialr to pyQt, widgets bigger than tkinter

Wrappers around the above frameworks for ease

PythonCard
---
As recommended above.
There are a plethora of gui interfaces
(http://wiki.python.org/moin/GuiProgramming)
Almost as many as web frameworks !

I suggest starting with easygui if you just want pop up dialogs,
otherwise take a day or two to get to grips with QT.  with a drag and
drop editor it is surprisingly good. There is a tutorial by a B Rempt
somewhere on line.



On 10/13/05, Olexiy Kharchyshyn <[EMAIL PROTECTED]> wrote:
>
> I'm really confused on the issue how to create windows, forms, etc. in
> Python & can't find any manual for that.
> Could you possibly advise me smth useful?
> --
> Best regards,
>
> Olexiy Kharchyshyn
> 
> [EMAIL PROTECTED]
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


--
--
Paul Brian
m. 07875 074 534
t. 0208 352 1741
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] IDLE will not appear under Win95 (Python 2.4.2) (fwd)

2005-10-17 Thread paul brian
I would suggest that you use
"add Remove Programs" in the control panel
and remove the python and win32 installations that you have installed

Then visit
www.activestate.com and download the package for activePython

(http://activestate.com/Products/Download/Download.plex?id=ActivePython)

You may need to download the latest MSI (MS installer) files from
microsoft as Win95 does not understand them

Activestate has a nice installer and you would be up and running then.

cheers

On 10/17/05, Danny Yoo <[EMAIL PROTECTED]> wrote:
> [Keeping tutor in CC]
>
> -- Forwarded message --
> Date: Mon, 17 Oct 2005 22:03:22 +1300
> From: Dave Shea <[EMAIL PROTECTED]>
> To: Danny Yoo <[EMAIL PROTECTED]>
> Subject: Re: [Tutor] IDLE will not appear under Win95 (Python 2.4.2)
>
> Hi Danny,
>
> Thanks for your note. I tried your original suggestion of using the
> C:\Python24 as the home directory but to no avail. So now I am a bit stuck
> on your and Alan's next suggestion which is to use PythonWin.
>
> I downloaded:
> http://optusnet.dl.sourceforge.net/sourceforge/pywin32/pywin32-204.win32-py2
> .4.exe
>
> and it installed fine.
>
> However, it is listed as "Python for Windows extensions" so I assume that
> installing this whilst still having Pythin24 installed was the way to go.
> The installer seemed to be OK about this but when I went to start PythonWin
> I could not actually find anything to start. I think I may be missing
> something here. Is PythonWin a separate installation of Python with an
> IDE/GUI ? Or is PythonWin simply something to sit over the top of a (any)
> Python installation.
>
> I'm a bit lost, as you may tell so any help would be greatly accepted.
>
> Many thanks.
>
> Dave Shea
> Wellington
> New Zealand.
> - Original Message -
> From: "Danny Yoo" <[EMAIL PROTECTED]>
> To: "Dave Shea" <[EMAIL PROTECTED]>
> Cc: 
> Sent: Monday, October 17, 2005 5:17 PM
> Subject: Re: [Tutor] IDLE will not appear under Win95 (Python 2.4.2)
>
>
> >
> >
> > On Sat, 15 Oct 2005, Dave Shea wrote:
> >
> > > Python installed without a complaint. However, when I fire up IDLE,
> > > there is an hour glass shown for a couple of seconds, then nothing else
> > > happens. When I fire up Python (command line) no problem, the DOS box
> > > opens and Python starts. I've tried re-starting, using Explorer instead
> > > of the Start menu, all the usual voodoo but still the IDLE refuses to
> > > start.
> >
> > Hi Dave,
> >
> > According to:
> >
> > http://python.org/2.4.2/bugs.html
> >
> > IDLE might not work well if Python has been installed into Program Files.
> >
> > Do you know if you've done this?  If so, try reinstalling and just leave
> > the installation path at the defaults (Python should install under
> > "C:\PYTHON24", I think.)
> >
> > IDLE has unfortunately been a bit problematic in the Python 2.4 release,
> > so if you continue to run into issues, I'd second Alan's suggestion about
> > trying Pythonwin instead.
> >
> >
> > Good luck!
> >
> >
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


--
--
Paul Brian
m. 07875 074 534
t. 0208 352 1741
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] setting

2005-10-19 Thread paul brian
Did you install from the python.org download or from activestate. If
you have a .chm file I am guessing activestate. For some reason the
normal docs that you get with the python.org distributin (the
"official" one) are only found as a chm file.

I suggest you get the python.org installer and carefully install to a
temp directory, and copy across the html files in Doc, or remove
activestate, install python.org and then add in the win32all
extensions. I would suggest the first option for sheer ease. (in fact
on windows i would suggest keeping the .chm and using that, cmd.exe is
quite poor in command line reading and copying so that .chm is my
favourtie way of looking up documentation)

HTH.




On 10/19/05, Shi Mu <[EMAIL PROTECTED]> wrote:
> I have installed Python 2.3 and I type "help()" and then "Keywords".
> I get a list of words. And it says that I can enter any of the words
> to get more help.  I enter
> "and" and I get the following error message:
> "Sorry, topic and keyword documentation is not available because the Python
> HTML documentation files could not be found.  If you have installed them,
> please set the environment variable PYTHONDOCS to indicate their location."
>
> but I have set both the environment variable, with the path to be
> C:\Python23\Doc which includes python23.chm
> Why I still got the above error message?
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


--
--
Paul Brian
m. 07875 074 534
t. 0208 352 1741
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] problem detecting files

2005-12-01 Thread Paul Hendrick
Hi there,
I've got a problem with getting the difference between 2 directories,
but only recognising directories and not files.
I'm trying to find directories in /home/svn that aren't
in /usr/local/trac/projects.

The list returned by listdir in /home/svn is:
['.bash_logout', '.bash_profile', '.bashrc', '.emacs', 'dir1', 'dir2']

the problem is I can't get the script to detect that .bash_profile
and .emacs aren't directories, it always comes back in my list of
directories.

I'll include my code below - any help would be greatly appreciated.
http://pastebin.ca/32033

Cheers,
Paul

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


[Tutor] Multi-Dimensional Dictionary that contains a 12 element list.

2005-12-28 Thread Paul Kraus
I am trying to build a data structure that would be a dictionary of a 
dictionary of a list.

In Perl I would build the structure like so $dictionary{key1}{key2}[0] = X
I would iterate like so ...
foreach my $key1 ( sort keys %dictionary ) {
foreach my $key2 ( sort keys %{$dictionary{$key1}} ) {
foreach my $element ( @{$dictionary{$key1}{$key2} } ) {
print "$key1 => $key2 => $element\n";
}
}
}

Sorry for the Perl reference but its the language I am coming from. I use data 
structures like this all the time. I don't always iterate them like this but 
If i can learn to build these and move through them in python then a good 
portion of the Perl apps I am using can be ported.

Playing around I have come up with this but have no clue how to iterate over 
it or if its the best way. It seems "clunky" but it is most likely my lack of 
understanding.

dictionary[(key1,key2)]=[ a,b,c,d,e,f,g ... ]

This i think gives me a dictionary with two keys ( not sure how to doing 
anything usefull with it though) and a list.

Now I am not sure how I can assign one element at a time to the list.

here is the pseudo code.
read text file.
split line from text file into list of fields.
One of the fields contains the date. Split the date into two fields Year and 
Month/Period. Build data structure that is a dictionary based on year, based 
on period, based on item code then store/increment the units sold based on 
period.

dictionary[(year,period)] = [ jan, feb, mar, apr, may, jun, july, aug, sep, 
oct, nov ,dec]

I would prefer to have the months just be an array index 0 through 11 and when 
it reads the file it increments the number contained there.

TIA,
-- 
Paul Kraus
=-=-=-=-=-=-=-=-=-=-=
PEL Supply Company
Network Administrator
216.267.5775 Voice
216.267.6176 Fax
www.pelsupply.com
=-=-=-=-=-=-=-=-=-=-=
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Multi-Dimensional Dictionary that contains a 12 element list.

2005-12-28 Thread Paul Kraus
On Wednesday 28 December 2005 10:18 am, Paul Kraus wrote:
> I am trying to build a data structure that would be a dictionary of a
> dictionary of a list.
>
> In Perl I would build the structure like so $dictionary{key1}{key2}[0] = X
> I would iterate like so ...
> foreach my $key1 ( sort keys %dictionary ) {
>   foreach my $key2 ( sort keys %{$dictionary{$key1}} ) {
>   foreach my $element ( @{$dictionary{$key1}{$key2} } ) {
>   print "$key1 => $key2 => $element\n";
>   }
>   }
> }
>
Here is the code that I used. Its functional and it works but there has got to 
be some better ways to do a lot of this. Transversing the data structure 
still seems like I have to be doing it the hard way.

The input data file has fixed width fields that are delimited by pipe.
So depending on the justification for each field it will either have leading 
or ending whitespace.

TIA,
Paul


#!/usr/bin/python
#
## Paul D. Kraus - 2005-12-27
## parse.py - Parse Text File
## Pipe deliminted '|'
#
## Fields: CustCode[0]
##   : OrdrType[1]
##   : OrdrReturn  [2]
##   : State   [3]
##   : QtyShipped  [4]
##   : VendCode[5]
##   : InvoiceDate [7] 
#
import re 
import string
results = {}
def format_date(datestring):
(period,day,year) = map(int,datestring.split('/') )
period += 2
if period == 13: period = 1; year += 1
if period == 14: period = 2; year += 1
if year > 80:
year = '19%02d' % year
else:
year = '20%02d' % year
return (year,period)

def format_qty(qty,credit,oreturn):
qty = float(qty)
if credit == 'C' or oreturn == 'Y':
return qty * -1
else:
return qty

textfile = open('orders.txt','r')
for line in textfile:
fields = map( string.strip, line.split( '|' ) )
fields[4] = format_qty(fields[ 4 ],fields[ 1 ], fields[ 2 ] )
(year, period) = format_date( fields[7] )
for count in range(12):
if count == period:
if results.get( ( year, fields[6], count), 0):
results[ year,fields[6], count] += fields[4]
else:
results[ year,fields[6],count] = fields[4]

sortedkeys = results.keys()
sortedkeys.sort()

for keys in sortedkeys:
res_string = keys[0]+'|'+keys[1]
for count in range(12):
if results.get((keys[0],keys[1],count),0):
res_string += '|'+str(results[keys[0],keys[1],count])
else:
res_string += '|0'
print res_string

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


[Tutor] Multiple Assignment from list.

2005-12-28 Thread Paul Kraus
How do I code this in python. Assuming fields is a list of 3 things.

(myfielda, myfieldb, myfieldc) = fields

When i try that I get 
ValueError: need more than 1 value to unpack.
If i print fields it is in need printed as
['somestring','somestring','somestring']

TIA,
-- 
Paul Kraus
=-=-=-=-=-=-=-=-=-=-=
PEL Supply Company
Network Administrator
216.267.5775 Voice
216.267.6176 Fax
www.pelsupply.com
=-=-=-=-=-=-=-=-=-=-=
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Multiple Assignment from list.

2005-12-28 Thread Paul Kraus
Never mind. i figured this out. the top line of a file i was reading in and 
splitting only had 1 char so "fields" on that line was not a list. I fixed 
this.
On Wednesday 28 December 2005 3:12 pm, Paul Kraus wrote:
> How do I code this in python. Assuming fields is a list of 3 things.
>
> (myfielda, myfieldb, myfieldc) = fields
>
> When i try that I get
> ValueError: need more than 1 value to unpack.
> If i print fields it is in need printed as
> ['somestring','somestring','somestring']
>
> TIA,

-- 
Paul Kraus
=-=-=-=-=-=-=-=-=-=-=
PEL Supply Company
Network Administrator
216.267.5775 Voice
216.267.6176 Fax
www.pelsupply.com
=-=-=-=-=-=-=-=-=-=-=
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Multi-Dimensional Dictionary that contains a 12 element list.

2005-12-29 Thread Paul Kraus
On Wednesday 28 December 2005 11:30 am, Kent Johnson wrote:
> Python lists don't create new elements on assignment (I think Perl lists
> do this?) so for example
> dictionary[(key1, key2)][10] = X
ok so assuming I had a dictionary with 1key that contained a list like so...
dictionary[key1][0]

How would I increment it or assign it if it didn't exist. I assumed like this.
dict[key1][0] = dictionary.get(key1[0],0) + X

-- 
Paul Kraus
=-=-=-=-=-=-=-=-=-=-=
PEL Supply Company
Network Administrator
216.267.5775 Voice
216.267.6176 Fax
www.pelsupply.com
=-=-=-=-=-=-=-=-=-=-=
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Multi-Dimensional Dictionary that contains a 12 element list.

2005-12-30 Thread Paul Kraus
> That is the approach Paul took originally (see the other fork of this
> thread). He is accumulating a sparse 3d matrix where the keys are year,
> field6 and month. (He hasn't said what field6 represents.) The problem
> is that he wants to print out counts corresponding to all the existing
> year and field6 values and every possible month value. To do this I
> think a two-level data structure is appropriate, such as the dict[
> (year, field6) ][month] approach you outlined.

Field6 is just an arbitrary field that represents some generic key. So for 
clarity lets say in this instance it represents a customer code.

so dict(2005,12130)[0..11]
would hold sales by month for customer number 12130 in 2005.

This problem has evolved since it started and I have created a class that lets 
me build rolling 24 month data structures.I need to create a bunch of reports 
that will be run monthly that will show a rolling 24 month total for 
different things. Sales by customer, sales by vendor, purchases by vendor.

So by making a class that on construction takes the current year and month it 
will build the structure I need. I then have a method that lets me fill the 
monthly buckets. All i do is pass it the arbitrary key (customercode) year, 
month, and amount and it will increment that bucket.

So now for all my reports all I have to write are little 5 or 6 line scripts 
that take a text file split the fields and format them before basing them off 
into my custom object. Very slick and this is my first python project. Its 
cluttered and messy but for about 1 hours worth of work on a brand new 
language I am impressed with the usability of this language.

Now I have to find a way to take the output at the end and pipe it out to an 
external Perl program that creates an excel spreadsheet ( no real clean easy 
way to do this in python but hey each tool has its usefullness). I wish I 
could hide this in the object though so that I could call a "dump" method 
that would then create the spreadsheet. I will have to play with this later. 

Current Script - Critique away! :)
=-=-=-=-=--=-=
#!/usr/bin/python
import string
import re

class Tbred_24Months:
def __init__(self,currentyear,currentmonth): ### Takes Ending Year and 
Ending Month Inits List
guide = []
self.results = {}
self.guide = {}
self.end_month = currentmonth
self.end_year  = currentyear
self.start_month = currentmonth
self.start_year = currentyear
for count in range(24):
guide.append((self.start_month,self.start_year))
self.start_month -= 1
if self.start_month < 1:
self.start_month = 12
self.start_year -= 1
guide.reverse()
count = 0
for key in guide:
self.guide[key[1],key[0]]=count
count += 1
self.sortedkeys = self.guide.keys()
self.sortedkeys.sort()

def insert(self,key,year,month,number):
if self.guide.has_key((year,month)):
if self.results.has_key(key):
seq = self.guide[(year,month)]
self.results[key][seq] += number
else:
self.results[key] = []
for x in range(24):self.results[key].append(0)

def splitfields(record):
fields = []
datestring=''
### Regular Expr.
re_negnum = re.compile('(\d?)\.(\d+)(-)')
re_date   = re.compile('(\d\d)/(\d\d)/(\d\d)')
for element in record.split('|'):
element=element.strip() # remove leading/trailing whitespace
### Move Neg Sign from right to left of number
negnum_match = re_negnum.search( element )
if negnum_match: 
if negnum_match.group(1):element = "%s%d.%02d" 
%(negnum_match.group(3),int(negnum_match.group(1)),int(negnum_match.group(2)))
else:element = "%s0.%02d" 
%(negnum_match.group(3),int(negnum_match.group(2)))
### Format Date
date_match = re_date.search(element)
if date_match:
(month,day,year) = 
(date_match.group(1),date_match.group(2),date_match.group(3))
### Convert 2 year date to 4 year
if int(year) > 80:year = "19%02d" %int(year)
else:year = "20%02d" %int(year)
element = (year,month,day)
if element == '.000': element = 0.00
fields.append( element )
return fields


### Build Vendor Sales 
sales = Tbred_24Months(2005,11)
vendorsales = open('vendorsales.txt','r')
for line in vendorsales:
fields = splitfields( line )
if len(fields) == 7:
(vendor,otype,oreturn,discountable,discperc,amount,date) = fields
amount = float(amount);discperc = float(discperc)
#if discperc and discountable == 'Y': amount = amount - ( amount * 
(discperc/100) )
i

Re: [Tutor] Open file error

2006-01-17 Thread Paul Kraus
On Tuesday 17 January 2006 12:11 pm, andy senoaji wrote:
> I am starting to pull my hair here. There were some postings in the past,
> similar to my problem, but the response was not clear enough. Sorry if you
> thingk I am reposting this.
>
> I am trying to run (on an XP box) a simple open file using this:
> f = open(r'C:\Test.txt', 'r')Newbie here but shouldn't it be.

Newbie Here

f = open( r'C:\\Test.txt','r')

I think you are escaping the T with \T.

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


Re: [Tutor] Controling my loops and redundant code?!?

2006-01-26 Thread Paul Kraus
What book are you working through? That is a pretty interesting exercise.

Paul
On Thursday 26 January 2006 12:52 pm, Bob Gailer wrote:
> At 08:44 AM 1/25/2006, Jon Moore wrote:
>
> Hi,
>
> I have written the program below as an exercise from a book I am working my
> way through.
>
> Objective from book:
> Write a character creator program for a role-playing-game. The player
> should be given a pool of 30 points to spend on four attributes: strength,
> health, wisdom and dexterity. The player should be able to spend points
> from the pool on any attribute and should be also be able to take points
> from an attribute and put them back in the pool.
>
> Although the program meets the aim of the exercise set out in the book ,
> there are a couple of things I am not happy with!
>
> 1. I am sure I have written far more code than required. Where could I have
> made some shorcuts?
>
> 2. Should the user enter a value greater than what is available, the
> program kicks the user all the way back to the main menu. How could I tidy
> this up to just loop round to ask the user to try a new value?
>
> choice = None
>
> # Set max number of available points
> POINTS_POOL = 30
>
> # store attribute values
> attributes = [["Strength", 0], ["Health", 0], ["Wisdom", 0], ["Dexterity",
> 0]]
>
>
> Some ideas to chew on as you develop skills and understanding of
> programming.
> Separate the "essential data" from the code. The essential data are
> attributes, associated points and max_points. So
>
> attributes = ["Strength", "Health", "Wisdom", "Dexterity"]
> points = [0, 0, 0, 0]
> MAX_POINTS = 30
>
> In this model the relationship between attributes and points is by
> position. Later you will study and use classes; then the points list will
> be replaced by a list of class instances, one per attribute.
>
> Develop code that operates on these lists to accomplish the various
> objectives. The code itself will never refer to any attribute by name!
>
> For example - to list the attributes with their points:
>
> for x in range(len(attributes)):
>   print "\t",attributes[x], points[x]
>
> To assign values. Note I separated getting input from converting it to
> integer so we can see if the user's entry is convertible.:
>
> available_points = MAX_POINTS - sum(points)
> print "You have " + available_points + " available."
> for x in range(len(attributes)):
>  cvalue = raw_input("How much would you like to assign to " + attributes[x]
> + " ?: "))
>   if cvalue.isdigit():
> value = int(cvalue)
>   else:
> print "Number expected"
>
> [snip]
> --
> Bob Gailer
> 510-978-4454

-- 
Paul Kraus
=-=-=-=-=-=-=-=-=-=-=
PEL Supply Company
Network Administrator
216.267.5775 Voice
216.267.6176 Fax
www.pelsupply.com
=-=-=-=-=-=-=-=-=-=-=
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Documentation

2006-02-05 Thread Paul Kraus
I have been working through a couple of books and learning alot. However I 
can't seem to find any easy way to learn more about different methods and 
commands.

For instance what are all of the methods that can be applied to lists and what 
do they do and how do they work. Or in TKinter what are all the different 
things can I do with a button besides just setting the command and the text 
and such. These are just examples.

I can't find an easy way to do this.

I know there has to be i have tried the pydoc gui but I found nothing when i 
searched list. *shrug*
-- 
Paul Kraus
=-=-=-=-=-=-=-=-=-=-=
PEL Supply Company
Network Administrator
216.267.5775 Voice
216.267.6176 Fax
www.pelsupply.com
=-=-=-=-=-=-=-=-=-=-=
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Documentation

2006-02-05 Thread Paul Kraus
> >>> help(Tkinter.Button.configure)
>
> Help on method configure in module Tkinter:
>
> configure(self, cnf=None, **kw) unbound Tkinter.Button method
> Configure resources of a widget.
>
> The values for resources are specified as keyword
> arguments. To get an overview about
> the allowed keyword arguments call the method keys.
>
>
> Try it on your own!  As a rule, stay away from methods that start with
> an underscore.
Thanks for the fast response. This was exactly what I was looking for. One 
last question how would I 'call the method keys'. from the example above.
-- 
Paul Kraus
=-=-=-=-=-=-=-=-=-=-=
PEL Supply Company
Network Administrator
216.267.5775 Voice
216.267.6176 Fax
www.pelsupply.com
=-=-=-=-=-=-=-=-=-=-=
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] GUI Development - Which toolkit

2006-02-06 Thread Paul Kraus
I am developing applications that need to run without work on both windows and 
linux and was wondering what gui toolkits everyone uses and why.

I have been looking at wxpython and tkinter.

Thanks in advance,
-- 
Paul Kraus
=-=-=-=-=-=-=-=-=-=-=
PEL Supply Company
Network Administrator
216.267.5775 Voice
216.267.6176 Fax
www.pelsupply.com
=-=-=-=-=-=-=-=-=-=-=
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] IDE - Editors - Python

2006-02-06 Thread Paul Kraus
Which editors does everyone use and why. Please keep the discussion to IDE's 
rather then any editors. I am well versed on Emacs and VI so anything beyond 
them would be appreciative. Why you like the editor and how it helps reduce 
your development time would be productive and helpfull.

TIA,
-- 
Paul Kraus
=-=-=-=-=-=-=-=-=-=-=
PEL Supply Company
Network Administrator
216.267.5775 Voice
216.267.6176 Fax
www.pelsupply.com
=-=-=-=-=-=-=-=-=-=-=
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] First Gui App - Please scrutinize

2006-02-06 Thread Paul Kraus
This is my first gui application. It just takes a percentage and then based on 
some options calculates a new price.

---
#!/usr/bin/python
from Tkinter import *

class Application( Frame ):
""" Price Calc GUI """
def __init__( self, master):
Frame.__init__( self, master )
self.grid()
self.costprice = 0.0
self.listprice = 0.0
self.margmarkup = 0.0
self.formula = ''
self.mmradial = IntVar()
self.costlistradial = IntVar()
self.create_widgets()

def create_widgets( self ):
self.marginormarkup()
self.entryboxes()
self.costlist()

# Calculate Button
self.b_calc = Button( self )
self.b_calc[ 'text'] = 'Calculate'
self.b_calc[ 'command' ] = self.calculate
self.b_calc.grid(row=10,column=0)

def marginormarkup(self):
# Calc using margin or markup
Label( self, text = 'Margin or 
Markup...').grid(row=0,column=0,sticky=W)
Radiobutton( self, text = 'Margin',  variable = self.mmradial, command 
= self.setstatusmm, value = 1).grid(row=1,column=0,sticky=W)
Radiobutton( self, text = 'Mark-Up', variable = self.mmradial, command 
= self.setstatusmm, value = 0).grid(row=2,column=0,sticky=W)
self.e_marginmarkup = Entry( self )
self.e_marginmarkup.grid(row=3,column=0,sticky=W)
Label( self, text = 'Percentage').grid(row=3,column=1,sticky=W)
self.mmradial.set(1)

def costlist(self):
# From Radial
Label( self, text = 'Calculations Based 
On...').grid(row=4,column=0,sticky=NW)
self.test = Radiobutton( self, text = 'Cost', variable = 
self.costlistradial, value = 1, command = 
self.setstatus).grid(row=5,column=0,sticky=W)
self.test = Radiobutton( self, text = 'List', variable = 
self.costlistradial, value = 0, command = 
self.setstatus).grid(row=6,column=0,sticky=W)
self.costlistradial.set(1)
self.setstatus()

def entryboxes(self):
 # Entry Boxes Cost List Result
self.e_cost = Entry( self )
self.e_list = Entry( self )

Label(self, text='Cost').grid(row=7,column=1,sticky=W)
self.e_cost.grid(row=7,column=0,sticky=W)

Label(self, text='List').grid(row=8,column=1,sticky=W)
self.e_list.grid(row=8,column=0,sticky=W)

def setstatusmm( self ):
if ( not self.costlistradial.get()):
self.mmradial.set(0)

def setstatus( self ):
self.delentryboxes()
if (self.costlistradial.get()):
self.e_list['state'] = 'disabled'
self.e_cost['state'] = 'normal'
else:
self.e_cost['state'] = 'disabled'
self.e_list['state'] = 'normal'
self.mmradial.set(0)
self.e_list.delete(0,END)
self.e_cost.delete(0,END)
self.e_marginmarkup.delete(0,END)

def delentryboxes( self ):
boxes = [ self.e_list, self.e_cost, self.e_marginmarkup ]
for ebox in boxes:
ebox['state'] = 'normal'
ebox.delete(0,END)


def calculate( self ):
equation = None
target = None
costa = float(self.e_cost.get())
lista = float(self.e_lista.get())
mma   = float(self.e_marginmarkup.get())/100
if (self.mmradial.get()): ### If Margin
if ( self.costlistradial.get() ):
equation = costa / ( 1 - mma)
target = self.e_list
else: ### If Markup
if ( self.costlistradial.get() ):
equation = cost + ( cost * mma )
target = self.e_list
else:
equation = list - (list * mma)
target = self.e_cost
target['state'] = 'normal'
target.delete(0,END)   
target.insert(0,equation)
target['state'] = 'readonly'


root = Tk()
root.title('Price Calculations')
root.geometry( '200x225' )
pricecalcgui = Application(root)
root.mainloop()
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python - Embedded mysql server

2006-02-07 Thread Paul Kraus
Anyone know where I can find information about how to embedd mysql into a 
python application. I need my hand held. I have worked with mysql a lot. I 
understand how to use python to manipulate a mysql database. What I want to 
know is how to embedded a database into my application.

-- 
Paul Kraus
=-=-=-=-=-=-=-=-=-=-=
PEL Supply Company
Network Administrator
216.267.5775 Voice
216.267.6176 Fax
www.pelsupply.com
=-=-=-=-=-=-=-=-=-=-=
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Build Python 2.4 SCO OpenServer 5

2006-02-14 Thread Paul Kraus
Everything configures fine but when I build it dies here...
 (If this is not the right place for this please point me to where I should be 
posting.)

gcc  -Wl,-Bexport -o python \
Modules/python.o \
libpython2.4.a -lsocket -lnsl -ldl  -lpthread   -lm
case $MAKEFLAGS in \
*-s*)  CC='gcc' LDSHARED='gcc -Wl,-G,-Bexport' OPT='-DNDEBUG -g -O3 -Wall 
-Wstrict-prototypes -m486 -DSCO5' ./python -E ./setup.py -q build;; \
*)  CC='gcc' LDSHARED='gcc -Wl,-G,-Bexport' OPT='-DNDEBUG -g -O3 -Wall 
-Wstrict-prototypes -m486 -DSCO5' ./python -E ./setup.py build;; \
esac
/bin/sh: 9056 Memory fault - core dumped
make: *** [sharedmods] Error 139

-- 
Paul Kraus
=-=-=-=-=-=-=-=-=-=-=
PEL Supply Company
Network Administrator
216.267.5775 Voice
216.267.6176 Fax
www.pelsupply.com
=-=-=-=-=-=-=-=-=-=-=
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


  1   2   3   >