[Tutor] python parser

2008-12-22 Thread johnf
I've been in the programming business for over 20 years and I have never had a 
need for a parser.  But recently I have need to convert code from one 
language to a my new language python.  What a better way to learn the new 
language python than a new project.  I decided it might be time to learn a 
little something about parsers too.  However, I soon discovered that I was 
walking into the world of compiler writers and theories of computer 
scientist.  I paid for and downloaded a paper from O'Reilly books on what I 
thought was going to be on 'pyparser'.  But that turned out to be mostly 
theory.  And nothing about the use of pyparser. 

So I ask you guys is there a link to a practical tutorial that provides hands 
on information on the use of a python parser.  I'd like to see something that 
demo's converting a real language to python.  When I google parsers I have 
found a few simple code examples of parsing a float.  Not really much help (I 
could have done that using Regex) when you want to parse 'if,then' statements 
that can be recursive.

Thanks in advance.  
-- 
John Fabiani
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Script to take a screenshot of my website.

2008-12-22 Thread Bryan Fodness
I would like to take a screenshot of my website without opening the browser
or importing extra packages.  Is there a way to do this?
I would like to create a function like screenshot('
http://www.scatterworks.com";)

-- 
"The game of science can accurately be described as a never-ending insult to
human intelligence." - João Magueijo
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python parser

2008-12-22 Thread bob gailer

johnf wrote:
I've been in the programming business for over 20 years and I have never had a 
need for a parser.  But recently I have need to convert code from one 
language to a my new language python.  


Pray tell: what is the other language, and why do you want to convert 
programs?


I assume you want to convert so you can then modify / extend the old 
programs. If you just plan to run them in Python "as is" I see no value 
in converting!


It is hard to map programs in other languages to Python because Python 
is so different. A direct translation would not take advantage of 
python's uniqueness.



What a better way to learn the new 
language python than a new project.  I decided it might be time to learn a 
little something about parsers too.  However, I soon discovered that I was 
walking into the world of compiler writers and theories of computer 
scientist.  I paid for and downloaded a paper from O'Reilly books on what I 
thought was going to be on 'pyparser'.  But that turned out to be mostly 
theory.  And nothing about the use of pyparser. 

So I ask you guys is there a link to a practical tutorial that provides hands 
on information on the use of a python parser.  I'd like to see something that 
demo's converting a real language to python.  When I google parsers I have 
found a few simple code examples of parsing a float.  Not really much help (I 
could have done that using Regex) when you want to parse 'if,then' statements 
that can be recursive.
  


FWIW I have written a parser for parts of the CMS Pipelines language. I 
provide a form of BNF for the syntax, generate parsers from that, run a 
"program" through that, which generates lists or dictionaries. I have no 
need to create a program from that.


The Pipelines language is very simple, so I am not dealing with control 
structures, just sequences of words and symbols.



--
Bob Gailer
Chapel Hill NC 
919-636-4239


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


Re: [Tutor] Script to take a screenshot of my website.

2008-12-22 Thread Martin Walsh
Bryan Fodness wrote:
> I would like to take a screenshot of my website without opening the
> browser or importing extra packages.  Is there a way to do this?

Unless you're keen on writing your own html/css/javascript/etc rendering
engine, I'm pretty sure you're going to need extra package(s), or a
browser (even if opened w/o a visible ui).

Perhaps these links will provide inspiration...

http://lapin-blanc.net/09/11/2008/django-website-thumbnail-generator/
http://www.gruppo4.com/~tobia/pythumbnail.shtml

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


Re: [Tutor] python parser

2008-12-22 Thread Andreas Kostyrka
Am Mon, 22 Dec 2008 07:18:41 -0800
schrieb johnf :

> I've been in the programming business for over 20 years and I have
> never had a need for a parser.  But recently I have need to convert
> code from one language to a my new language python.  What a better
> way to learn the new language python than a new project.  I decided
> it might be time to learn a little something about parsers too.
> However, I soon discovered that I was walking into the world of
> compiler writers and theories of computer scientist.  I paid for and
> downloaded a paper from O'Reilly books on what I thought was going to
> be on 'pyparser'.  But that turned out to be mostly theory.  And
> nothing about the use of pyparser. 

Well, scanning and parsing are rather one of the older fields in C.S.

So if I got that correct you want to convert programs from language A
to language Python, and on tops you'll probably want the generated
program to be human-readable and as fast as possible.

Without knowing what A is, it's hard to tell you more, but writing a
parser for almost any programming language is hard, and emitting fast
and human readable code is even harder.

Furthermore, doing it without the theoretical background is not simpler
either.

Btw, possible implementations of a parser depend upon a number of
"theoretical" properties of the grammer you want to implement.

> 
> So I ask you guys is there a link to a practical tutorial that
> provides hands on information on the use of a python parser.  I'd
> like to see something that demo's converting a real language to
> python.  When I google parsers I have found a few simple code
> examples of parsing a float.  Not really much help (I could have done
> that using Regex) when you want to parse 'if,then' statements that
> can be recursive.

Take a look at the examples at the pyparsing website, it even includes
a python parser :)

Andreas
> 
> Thanks in advance.  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python parser

2008-12-22 Thread johnf
On Monday 22 December 2008 09:15:13 am bob gailer wrote:
> johnf wrote:
> > I've been in the programming business for over 20 years and I have never
> > had a need for a parser.  But recently I have need to convert code from
> > one language to a my new language python.
>
> Pray tell: what is the other language, and why do you want to convert
> programs?
>
> I assume you want to convert so you can then modify / extend the old
> programs. If you just plan to run them in Python "as is" I see no value
> in converting!
>
> It is hard to map programs in other languages to Python because Python
> is so different. A direct translation would not take advantage of
> python's uniqueness.
>
> > What a better way to learn the new
> > language python than a new project.  I decided it might be time to learn
> > a little something about parsers too.  However, I soon discovered that I
> > was walking into the world of compiler writers and theories of computer
> > scientist.  I paid for and downloaded a paper from O'Reilly books on what
> > I thought was going to be on 'pyparser'.  But that turned out to be
> > mostly theory.  And nothing about the use of pyparser.
> >
> > So I ask you guys is there a link to a practical tutorial that provides
> > hands on information on the use of a python parser.  I'd like to see
> > something that demo's converting a real language to python.  When I
> > google parsers I have found a few simple code examples of parsing a
> > float.  Not really much help (I could have done that using Regex) when
> > you want to parse 'if,then' statements that can be recursive.
>
> FWIW I have written a parser for parts of the CMS Pipelines language. I
> provide a form of BNF for the syntax, generate parsers from that, run a
> "program" through that, which generates lists or dictionaries. I have no
> need to create a program from that.
>
> The Pipelines language is very simple, so I am not dealing with control
> structures, just sequences of words and symbols.

Well I'm attempting to convert VFP and MsSQL (stored procedures and triggers) 
to Python and Postgres (functions and triggers).  Actually, python is very 
close to VFP and I would convert very well.  MsSQL is another question.

It has been suggested that I just read one line at a time and convert.  I 
could do that but I did want to expand my understanding by using a parser. 
And the one line at a time does not lend it self to control structures.  

Even if I do a get the convert to work there would still be a need for hand 
coding.  But it would provide a good start.

So far no links to tutorials?
-- 
John Fabiani
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Very basic question about lists

2008-12-22 Thread Eduardo Vieira
Hello, I'm trying to teach my self programming with python and there
are some basic things that stumps me:
Given this code:
###
list1 = ['arr', 'bre', 'grau', 'lower', 'tudo']
for item in list1:
if 'arr' in item:
print list1
###
The output is (as expected):
['arr', 'bre', 'grau', 'lower', 'tudo']

If I change my condition to:
if 'arr' or 'bell' in item:
or to this:
if 'arr' or 'grau' in item:

I get this result:
['arr', 'bre', 'grau', 'lower', 'tudo']
['arr', 'bre', 'grau', 'lower', 'tudo']
['arr', 'bre', 'grau', 'lower', 'tudo']
['arr', 'bre', 'grau', 'lower', 'tudo']
['arr', 'bre', 'grau', 'lower', 'tudo']

Why this? I guess I'm not grasping the use of AND and OR

Thanks,

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


Re: [Tutor] python parser

2008-12-22 Thread Alan Gauld


"johnf"  wrote

So I ask you guys is there a link to a practical tutorial that 
provides hands

on information on the use of a python parser.


One place to loook for all things to do with processing text is Mertz' 
Text

Processing in Python. He discuissed parsers in some detail although,
infuriatingly, doesn't spend a lot of time on the standard library 
parsers.
But the principles are common and the modules he uses readily 
available.


Its available online at:

http://gnosis.cx/TPiP/

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



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


Re: [Tutor] Script to take a screenshot of my website.

2008-12-22 Thread Alan Gauld

"Bryan Fodness"  wrote

I would like to take a screenshot of my website without opening the 
browser


I dont really know what you mean by this? The appearance of a web site
depends on many factors, not least the size of the browser window.
But different browsers render pages differently too - and thats not 
due
to bugs its deliberately included in the concept of HTML. - it is not 
a

page layout language.

Indeed if viewed in a text browser like lynx it will look very 
different!
But even something "standard" like IE looks totally different between 
Mac

and PC versions. And thats without taking account of user preferences
over the colours of hyperlinks, font styles and sizes, security 
settings,

etc etc.

So I'm not sure what you think you could capture without knowing what
computer, OS and browser (and the user settings thereof) was going
to be used...

Alan G. 



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


Re: [Tutor] python parser

2008-12-22 Thread bob gailer

johnf wrote:

On Monday 22 December 2008 09:15:13 am bob gailer wrote:
  

johnf wrote:


I've been in the programming business for over 20 years and I have never
had a need for a parser.  But recently I have need to convert code from
one language to a my new language python.
  

Pray tell: what is the other language, and why do you want to convert
programs?

I assume you want to convert so you can then modify / extend the old
programs. If you just plan to run them in Python "as is" I see no value
in converting!

It is hard to map programs in other languages to Python because Python
is so different. A direct translation would not take advantage of
python's uniqueness.



What a better way to learn the new
language python than a new project.  I decided it might be time to learn
a little something about parsers too.  However, I soon discovered that I
was walking into the world of compiler writers and theories of computer
scientist.  I paid for and downloaded a paper from O'Reilly books on what
I thought was going to be on 'pyparser'.  But that turned out to be
mostly theory.  And nothing about the use of pyparser.

So I ask you guys is there a link to a practical tutorial that provides
hands on information on the use of a python parser.  I'd like to see
something that demo's converting a real language to python.  When I
google parsers I have found a few simple code examples of parsing a
float.  Not really much help (I could have done that using Regex) when
you want to parse 'if,then' statements that can be recursive.
  

FWIW I have written a parser for parts of the CMS Pipelines language. I
provide a form of BNF for the syntax, generate parsers from that, run a
"program" through that, which generates lists or dictionaries. I have no
need to create a program from that.

The Pipelines language is very simple, so I am not dealing with control
structures, just sequences of words and symbols.



Well I'm attempting to convert VFP and MsSQL (stored procedures and triggers) 
to Python and Postgres (functions and triggers).  Actually, python is very 
close to VFP and I would convert very well.  MsSQL is another question.
  


I started writing a VFP - Python converter a few years ago. I will see 
if I have it around.


It has been suggested that I just read one line at a time and convert.  I 
could do that but I did want to expand my understanding by using a parser. 
And the one line at a time does not lend it self to control structures.  

Even if I do a get the convert to work there would still be a need for hand 
coding.  But it would provide a good start.


So far no links to tutorials?
  



--
Bob Gailer
Chapel Hill NC 
919-636-4239


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


Re: [Tutor] python parser

2008-12-22 Thread johnf
On Monday 22 December 2008 10:34:12 am Alan Gauld wrote:
> "johnf"  wrote
>
> > So I ask you guys is there a link to a practical tutorial that
> > provides hands
> > on information on the use of a python parser.
>
> One place to loook for all things to do with processing text is Mertz'
> Text
> Processing in Python. He discuissed parsers in some detail although,
> infuriatingly, doesn't spend a lot of time on the standard library
> parsers.
> But the principles are common and the modules he uses readily
> available.
>
> Its available online at:
>
> http://gnosis.cx/TPiP/
>
> HTH,

Interesting - I'll start reading right now.

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


Re: [Tutor] python parser

2008-12-22 Thread Kent Johnson
On Mon, Dec 22, 2008 at 12:39 PM, johnf  wrote:
> I paid for and downloaded a paper from O'Reilly books on what I
> thought was going to be on 'pyparser'.  But that turned out to be mostly
> theory.  And nothing about the use of pyparser.

Was that "Getting Started with Pyparsing" ? I would be very surprised
if that was all theory, it is written by the author of pyparsing and
he likes examples :-) and the ToC looks like it is mostly extended
examples.

> So far no links to tutorials?

David Mertz has written some columns on parsing, see
http://www.ibm.com/developerworks/views/linux/libraryview.jsp?sort_by=Relevance&show_abstract=true&show_all=false&search_flag=true&topic_by=All+topics+and+related+products&type_by=Articles&search_by=charming+python&Submit.x=0&Submit.y=0

You might also try his book:
http://gnosis.cx/TPiP/

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


Re: [Tutor] Very basic question about lists

2008-12-22 Thread Robert Berman




#! /usr/bin/python

list1 = ['arr', 'bre', 'grau', 'lower', 'tudo']
for item in list1:
 if item == 'arr' or item == 'grau':
    print list1

Hopefully, my rewording of one of your tests will make it a bit easier
to see what is happening.
A for statement such as 'for item in list1' walks the whole list.
Therefore, every iteration is tested.
Notice you are doing two tests in one if statement so that each time a
true condition happens during the iterations that list will print. You
have two  iterations that get a true result so list1 prints twice.

That should clear up the confusion. If not please tel us what is still
not clear.

Robert Berman

Eduardo Vieira wrote:

  Hello, I'm trying to teach my self programming with python and there
are some basic things that stumps me:
Given this code:
###
list1 = ['arr', 'bre', 'grau', 'lower', 'tudo']
for item in list1:
if 'arr' in item:
print list1
###
The output is (as expected):
['arr', 'bre', 'grau', 'lower', 'tudo']

If I change my condition to:
if 'arr' or 'bell' in item:
or to this:
if 'arr' or 'grau' in item:

I get this result:
['arr', 'bre', 'grau', 'lower', 'tudo']
['arr', 'bre', 'grau', 'lower', 'tudo']
['arr', 'bre', 'grau', 'lower', 'tudo']
['arr', 'bre', 'grau', 'lower', 'tudo']
['arr', 'bre', 'grau', 'lower', 'tudo']

Why this? I guess I'm not grasping the use of AND and OR

Thanks,

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

  



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


Re: [Tutor] reading output from a c executable.

2008-12-22 Thread Ravi Kondamuru
I tried what Bill Campbell suggested: read the len first and then use that
to populate the structdef length field for the string
len = xstruct.structdef(xstruct.little_endian, [
('len', (xstruct. unsigned_long, 1)),
])

l = len(buf[0:3])

rec = xstruct.structdef(xstruct.little_endian, [
('len', (xstruct. unsigned_long, 1)),
('id', (xstruct.unsigned_long, 1)),
('name', (xstruct.string, (l.len-8))),
])

n = rec(buf[0:l.len])
print n.len, n.id, n.name

On Sat, Dec 20, 2008 at 11:54 AM, Ravi Kondamuru wrote:

> I am trying to use xstruct module to unpack a varaible size record with the
> following structure.
>
> struct nameid {
> u32bits len /* total length */
> u32bits id;
>  char name; /* name variable length */
> }
>
> As can be seen the length of the name = len - (sizeof(len) + sizeof(id)).
>
> How do  I use xstruct or struct to unpack such a structure?
>
> n = xstruct.structdef(xstruct.little_endian, [
> ('len', (xstruct. unsigned_long, 1)),
> ('id', (xstruct.unsigned_long, 1)),
> ('name', (xstruct.string, )),
> ])
>
> xstruct seems to expect the exact len to be specified in structdef. Is
> there a way to tell it to go till the end of the buffer passed?
>
> thanks,
> Ravi.
>
> On Fri, Dec 12, 2008 at 6:30 AM, bob gailer  wrote:
>
>> Ravi Kondamuru wrote:
>>
>>> Denis, These are 32bit, 64bit counters (essentially numbers).
>>> Bob, There are well over 10K counters in the log file that are updated
>>> every 5 secs. If a counter1's graph was requested, log will have to be
>>> parsed once to get the data points. If a user asked for a counter2, now it
>>> needs to be retrieved from the log file. Which essentially means having to
>>> go through the logfile again. This is because i want to try to avoid using a
>>> database to store values after parsing the file.
>>>
>> Here is a little test program I wrote to check timing on a file of
>> *100,000* 64 bit counters.
>>
>> import time, struct
>> ctrs = 10
>> s = time.time()
>> # create a file of 800,000 characters (100,000 64 bit numbers)
>> f = open('ravi.dat', 'wb')
>> for i in range(ctrs):
>>  f.write('abcdefgh') # it does not matter what we write as long as it is 8
>> bytes.
>> print time.time() - s
>>
>> s = time.time()
>> l = []
>> # read the file
>> f = open('ravi.dat', 'rb').read()
>>
>> # unpack each 8 characters into an integer and collect in a list
>> for b in range(0, ctrs, 8):
>>  n = struct.unpack('q', f[b:b+8])
>>  l.append(n)
>> print time.time() - s
>>
>> Writing the file took 0.14 seconds on my computer
>> Reading and unpacking 0.04 seconds.
>> I think you can set performance issues aside.
>>
>> There is a principle in programming that proposes:
>> 1 - get a program running. Preferably in Python as development time is
>> much faster.
>> 2 - check its performance.
>> 3 - refine as needed.
>>
>>
>> --
>> Bob Gailer
>> Chapel Hill NC 919-636-4239
>>
>>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Very basic question about lists

2008-12-22 Thread wesley chun
eduardo,

welcome to programming, and even better, welcome to Python!  you've
done your research and found a list of great people who can help you
out.

with regards to your question, my comment are below...

> list1 = ['arr', 'bre', 'grau', 'lower', 'tudo']
> for item in list1:
>if 'arr' in item:
>print list1
> ###
> The output is (as expected):
> ['arr', 'bre', 'grau', 'lower', 'tudo']

you need to question your output here. sure it is what you *want*, but
i don't think you got it the way you originally thought.

what are you trying to do? are you trying to see whether the string
'arr' is in the entire list, or are you comparing one element at a
time and checking if a string is found in a larger string? (your code
is doing the latter.) here is some code (and output) which will
hopefully make it clear:

>>> list1 = ['arr', 'bre', 'grau', 'lower', 'tudo']
>>> 'arr' in list1
True
>>> 'bell' in list1
False
>>> 'grau' in list1
True

the "in" operator checks if an entire string is in a list or not. you
do not have to loop through it to get an answer. now let's iterate
through each string.

>>> for item in list1:
... print item
...
arr
bre
grau
lower
tudo

notice that item represents each string in the list in each iteration
of the loop. if you are asking whether 'arr' is in item, you are
asking if 'arr' is in 'arr', 'arr' is in 'bre', 'arr' is in 'grau',
etc., so your (single) output is due to the fact that on the first
pass, 'arr' is in 'arr'. in other words, you did this:

>> tmp = 'arr'
>>> 'arr' in tmp
True
>>> 'arr' in 'arr'
True

so that's why your list printed out, because 'arr' in 'arr' returned
True. so, to really illustrate what you're doing, take a look at this
example:

>>> for item in list1:
... if 'owe' in item:
... print list1
...
['arr', 'bre', 'grau', 'lower', 'tudo']

this time, the output *is* expected, because 'owe' is *in* one of the
5 strings in your list... in particular, 'owe' is in 'lower', so
again, this is why list1 is printed, because that statement is True.


> Why this? I guess I'm not grasping the use of AND and OR

i don't think you have a problem with AND and OR... i think your
problem was with IN instead. :-) remember, for strings, IN means
whether a substring is found in a larger string (or not), and for
lists, IN means whether an object is found in the list (or not).

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python parser

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

Hi John,

I hope this message finds you well.

You might be interested in the following:

http://dabodev.com/

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

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

"Background

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

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

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

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


Re: [Tutor] Very basic question about lists

2008-12-22 Thread Kent Johnson
On Mon, Dec 22, 2008 at 1:33 PM, Eduardo Vieira  wrote:

> if 'arr' or 'bell' in item:

The interpreter sees this as
  if ('arr') or ('bell' in item):

'arr' always evaluates to True so the condition is always true. The
correct way to express this condition is
  if 'arr' in item or 'bell' in item:

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


Re: [Tutor] Very basic question about lists

2008-12-22 Thread wesley chun
>> if 'arr' or 'bell' in item:
>
> The interpreter sees this as
>  if ('arr') or ('bell' in item):
>
> 'arr' always evaluates to True so the condition is always true. The
> correct way to express this condition is
>  if 'arr' in item or 'bell' in item:

arrgh. yes, i saw this too but forgot to mention it in my reply. the
reason why it is, as kent has stated is that any non-empty container,
i.e., 'arr', always evaluates to Boolean True. the only string that
has a False value is the empty string, or "".

in addition, i think (and i may be wrong about this) that he really
wanted to do:

if 'arr' in list1 or 'bell' in list1...

"in item" is a substring check and not a list membership query.

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


Re: [Tutor] Very basic question about lists

2008-12-22 Thread spir
Le lundi 22 décembre 2008 à 11:33 -0700, Eduardo Vieira a écrit :
> Hello, I'm trying to teach my self programming with python and there
> are some basic things that stumps me:
> Given this code:
> ###
> list1 = ['arr', 'bre', 'grau', 'lower', 'tudo']
> for item in list1:
> if 'arr' in item:
> print list1
> ###
> The output is (as expected):
> ['arr', 'bre', 'grau', 'lower', 'tudo']
> 
> If I change my condition to:
> if 'arr' or 'bell' in item:
> or to this:
> if 'arr' or 'grau' in item:
> 
> I get this result:
> ['arr', 'bre', 'grau', 'lower', 'tudo']
> ['arr', 'bre', 'grau', 'lower', 'tudo']
> ['arr', 'bre', 'grau', 'lower', 'tudo']
> ['arr', 'bre', 'grau', 'lower', 'tudo']
> ['arr', 'bre', 'grau', 'lower', 'tudo']
> 
> Why this? I guess I'm not grasping the use of AND and OR

Actually, you are right, I guess. There are here two rather subtile
issues. (In addition to the questions about what you really *mean* with
this piece of code):
-1- What python understands reading "if 'arr' or 'bell' in item:"
-2- What logical value python given to several kinds of objects.

About the first point, notice that you write an 'or' in the middle of a
logical expression:

if 'arr' *or* 'bell' in item

Then, the problem for any reader, be it human or mechanical, is to guess
what you intend to relate with 'or'. On left side, no problem, there is
only 'arr'. On right hand, you probably mean to englobe only 'bell', but
how does python know that? To solve this question, any language defines
an operator precedence (priority) order -- which is nearly the same for
all computer languages and inherits from conventional logic. Below the
table for python:

===
Operators and their evaluation order 
Highest
Operator
Comment
  
, [...] {...} `...` 
Tuple, list & dict.
creation; string conv. 
s[i] s[i:j] s.attr
f(...) 
indexing & slicing;
attributes, fct calls 
+x, -x, ~x 
Unary operators 
x**y 
Power 
x*y x/y x%y
mult, division, modulo 
x+y x-y 
addition, substraction 
x<>y 
Bit shifting 
x&y 
Bitwise and 
x^y 
Bitwise exclusive or 
x|y 
Bitwise or 
xy  x>=y
x==y x!=y  x<>y
x is y   x is not y
x in s   x not in s 
Comparison, 
identity, 
membership 
not x 
boolean negation 
x and y 
boolean and 
x or y 
boolean or 
Lowest 
lambda args: expr 
anonymous function
=

As you can see, 'or' is very low, which means that it will be aplied
very late in any logical expression. 'in' comes befores. As you can now
guess, this means that your expression is equal to:

if ('arr') or ('bell' in item)

Lookind at the table, you will understand that, for instance:
if a and b or c <==> if (a and b) or c
if a or b and c <==> if a or (b and c)
Right?

Now, to the second issue -- which is /according to me/ (others will not
agree) a serious default of python: in addition to logical literals
(True and False) and results of logical operators (and, or, in,...),
python gives a boolean logical value to *all* objects. Which means that:

if ( "todu bêm!" and 3 or file("/home/spir/example.txt",'r') )

is perfectly valid in python. Actually, python considers that anything
that is not "nul" (zero, or empty) has a True logical value. So that
'arr' is True for python. Then your expression above is always True
whatever the right side of the 'or' operator. 

for item in list1:
if 'arr' or (whatever you like):
print list1

Your loop will test the condition 5 times, because there are 5 items in
the list. The condition will be True 5 times: the list will print 5
times.
Note that you stepped right from the start on a very tricky trap of
python: most of the language items, and examples and trials you may find
or do will work in a coherent way, meaning the way you expect -- if ever
you have a consistent view of the problem. 

Ecco!

Denis

> Thanks,
> 
> Eduardo
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

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


Re: [Tutor] python parser

2008-12-22 Thread Eike Welk
Hi John! 

I have written a (currently mostly defunct) compiler in Python for a 
specialized programming language. The parser may serve as an example 
for you.

Introduction:
http://freeode.berlios.de/

The parser:
https://svn.berlios.de/wsvn/freeode/trunk/freeode_py/freeode/simlparser.py

The method --- Parser._defineLanguageSyntax --- creates the Pyparsing 
object. There are gigantic amounts of (mostly correct) comments in 
the file. Excuse the bad English and the typos. The style is somewhat 
adapted to the IDE Pydev. 

The parser reads the program text (no scanner) and creates a tree 
structure from it. I call it AST or Parse Tree (I can't decide how to 
call it). This tree is used in subsequent passes of the compiler.

I use a modified version of Pyparsing where I fixed things that I 
considered bugs, and where I implemented some of my wishes:
https://svn.berlios.de/wsvn/freeode/trunk/freeode_py/freeode/third_party/pyparsing.py

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


Re: [Tutor] Very basic question about lists

2008-12-22 Thread Eduardo Vieira
Wesley wrote:
On Mon, Dec 22, 2008 at 1:19 PM, wesley chun  wrote:

>
> in addition, i think (and i may be wrong about this) that he really
> wanted to do:
>
> if 'arr' in list1 or 'bell' in list1...
>

Thanks for all the replies. Yes that was actually what I meant. My
mistake too was that I gave you all a wrong example to explain my
problem, sorry. The following example will explain better:
list1 = ['ar', 'fir', 'wo']
list2 = ['ber', 'gar', 'gt']
list3 = ['hu', 'mo', 'ko', 'tr']
list4 = ['q', 'wer', 'duh']

whole = [list1, list2, list3, list4]
for item in whole:
if 'ar' or 'ko' in item:
print item

So, the unexpected result was that I got all lists printed, when I
expected only list1 and list3. Now, I don't know if I still understand
the explanation given by
spir -- I'll reread more carefully--, but I know that the code to get
what I expect is this instead:
for item in whole:
for word in item:
if word == 'ar' or word == 'ko':
print item

I see I have to do a loop inside a loop and that this the right expression
if word == 'ar' or word == 'ko':

but this is not:
if word == 'ar' or 'ko':
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python parser

2008-12-22 Thread bob gailer

More thoughts on converting VFP to Python:

line-by-line can take into account control structures. One just 
increases or decreases the indent when encountering the start or end of 
a structure.


a bigger challenge is handling the work-area related commands such as 
Scan, Replace, Skip, Set Relation, 


the concept of data sessions

macro substitution

all the GUI stuff would be translated to one of the python GUI packages. 
Not a trivial task!


An outline:

create initial dictionary of all initial special characters, keywords 
including their abbreviations, ...?
create dictionary to hold all user-generated names (variables, classes, 
procedures, windows, ...?)
awareness while scanning of #define and blocks such as text .. endtext 
to ignore

scan file for procedure/function/define

certain initial characters take precedence = ? & # * && @ \
this list may be extended temporarily by commands like text
; is a special final character

assignment statement?
initial word a keyword or function?
if none of the above error?

and on and on.

--
Bob Gailer
Chapel Hill NC 
919-636-4239


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


Re: [Tutor] Very basic question about lists

2008-12-22 Thread wesley chun
> list1 = ['ar', 'fir', 'wo']
> list2 = ['ber', 'gar', 'gt']
> list3 = ['hu', 'mo', 'ko', 'tr']
> list4 = ['q', 'wer', 'duh']
>
> whole = [list1, list2, list3, list4]
> for item in whole:
>if 'ar' or 'ko' in item:
>print item
>
> So, the unexpected result was that I got all lists printed, when I
> expected only list1 and list3. Now, I don't know if I still understand
> the explanation given by spir -- I'll reread more carefully--, but I
> know that the code to get what I expect is this instead:
> for item in whole:
>for word in item:
>if word == 'ar' or word == 'ko':
>print item
>
> I see I have to do a loop inside a loop and that this the right expression
> if word == 'ar' or word == 'ko':
>
> but this is not:
> if word == 'ar' or 'ko':


correct, but i *still* don't think you need to do an inner loop... it
seems you're just trying to see whether a word is in the list, not
checking for substrings. in fact, the lists can be inside another list
too. couple this with *not* looping through each list looking for
something, we have the following:

>>> whole = [
... ['ar', 'fir', 'wo'],
... ['ber', 'gar', 'gt'],
... ['hu', 'mo', 'ko', 'tr'],
... ['q', 'wer', 'duh'],
... ]
>>>
>>> for item in whole:
... if 'ar' in item or 'ko' in item:
... print item
...
['ar', 'fir', 'wo']
['hu', 'mo', 'ko', 'tr']

there... those are list1 and list3 right? again, the secret is in IN. :-)

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Very basic question about lists

2008-12-22 Thread Steve Willoughby



   if 'ar' or 'ko' in item:


This is incorrect.  What you meant to say was:

if 'ar' in item or 'ko' in item:

or something equivalent to that.

"if 'ar' or 'ko' in item" means "if ('ar') is True or ('ko' in item) is 
True".  Since 'ar' is True anyway, you'll get a match every time.

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


[Tutor] more encoding strangeness

2008-12-22 Thread Eric Abrahamsen

Hi there,

I'm configuring a python command to be used by emacs to filter a  
buffer through python markdown, and noticed something strange. If I  
run this command in the terminal:


python -c "import sys,markdown; print  
markdown.markdown(sys.stdin.read().decode('utf-8'))" <  
markdown_source.md


The file (which is encoded as utf-8 and contains Chinese characters)  
is converted and output correctly to the terminal. But if I do this to  
write the output to a file:


python -c "import sys,markdown; print  
markdown.markdown(sys.stdin.read().decode('utf-8'))" <  
markdown_source.md > output.hml


I get a UnicodeEncodeError, 'ascii' codec can't encode character  
u'\u2014'. I'm not sure where exactly this is going wrong, as print  
and sys.stdout.write() and whatnot don't provide encoding parameters.  
What's the difference between this command writing to the terminal,  
and writing to the file?


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