Splitting Tree

2012-12-02 Thread subhabangalore
Dear Group,

I am using NLTK and I used the following command,

chunk=nltk.ne_chunk(tag)
print "The Chunk of the Line Is:",chunk


The Chunk of the Line Is: (S
  ''/''
  It/PRP
  is/VBZ
  virtually/RB
  a/DT
  homecoming/NN
  ,/,
  ''/''
  said/VBD
  (PERSON Gen/NNP Singh/NNP)
  on/IN
  arrival/NN)

Now I am trying to split the output preferably by ",/,".

But how would I split a Tree object in python.

If I use command like,
chunk_word=chunk.split()

It is giving me the error as,

File "C:/Python27/docstructure1.py", line 38, in document_structure1
chunk1=chunk.split()
AttributeError: 'Tree' object has no attribute 'split'

If anyone of the learned members of the room can kindly help.

Regards,
Subhabrata. 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Imaging libraries in active development?

2012-12-02 Thread Luis Pedro Coelho

Alasdair McAndrew  on Thu, 29 Nov 2012 wrote:
> Probably the combinations of OpenCV, Scipy.ndimage and scikits-image 
> would cover pretty much all of my needs.


Hi,

All of those (+ mahotas, which is the package I wrote & imread which 
might be useful for microscopy file formats) will work on numpy arrays 
(openCV requires a bit of conversion back and forth, but it will work). 
Therefore, it is not much of a bother to mix&match functions from one or 
the other library::


import mahotas as mah
import imread
import skimage
import pylab

image = imread.imread("my-fancy-image.jpeg")
filtered = mah.gaussian_filter(image, 4.)
segmented = skimage.segmentation.quickshift(filtered)
pylab.imshow(segmented)
...

I just mixed 4 different packages seamlessly in this example. This will 
work flawlessly.


mahotas & skimage are both under very active development, but neither is 
unstable (i.e., we keep adding new features, but the existing code is 
very reliable). scipy.ndimage is sort of dead: I scavenged its code for 
good bits and pieces for mahotas, but nobody is developing ndimage.


If you run into specific issues, the mailing list pythonvision at

https://groups.google.com/forum/#!forum/pythonvision

is a good forum. Plenty of people from different projects lurk there.

HTH,
Luis
--
http://mail.python.org/mailman/listinfo/python-list


setup.py Choosing Older Compiler On Windows

2012-12-02 Thread Ami Tavory
  Hello,

  I'm porting a C++ extension module to a Windows machine that has both VC8
and VC10 installed. Unfortunately, `setup.py build` tries to build using
VC8, which fails (the extension uses C++ standard libraries that VC  didn't
used to have). Is there a way to get setup.py to use VC10 (preferably
externally, without modifying the script)?

  Thanks,

  Ami
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Splitting Tree

2012-12-02 Thread subhabangalore
On Sunday, December 2, 2012 5:39:32 PM UTC+5:30, [email protected] wrote:
> Dear Group,
> 
> 
> 
> I am using NLTK and I used the following command,
> 
> 
> 
> chunk=nltk.ne_chunk(tag)
> 
> print "The Chunk of the Line Is:",chunk
> 
> 
> 
> 
> 
> The Chunk of the Line Is: (S
> 
>   ''/''
> 
>   It/PRP
> 
>   is/VBZ
> 
>   virtually/RB
> 
>   a/DT
> 
>   homecoming/NN
> 
>   ,/,
> 
>   ''/''
> 
>   said/VBD
> 
>   (PERSON Gen/NNP Singh/NNP)
> 
>   on/IN
> 
>   arrival/NN)
> 
> 
> 
> Now I am trying to split the output preferably by ",/,".
> 
> 
> 
> But how would I split a Tree object in python.
> 
> 
> 
> If I use command like,
> 
> chunk_word=chunk.split()
> 
> 
> 
> It is giving me the error as,
> 
> 
> 
> File "C:/Python27/docstructure1.py", line 38, in document_structure1
> 
> chunk1=chunk.split()
> 
> AttributeError: 'Tree' object has no attribute 'split'
> 
> 
> 
> If anyone of the learned members of the room can kindly help.
> 
> 
> 
> Regards,
> 
> Subhabrata.

Sorry to ask this. I converted in string and then splitted it.
-- 
http://mail.python.org/mailman/listinfo/python-list


List problem

2012-12-02 Thread subhabangalore
Dear Group,

I have a list of the following pattern,

[("''", "''"), ('Eastern', 'NNP'), ('Army', 'NNP'), ('Commander', 'NNP'), 
('Lt', 'NNP'), ('Gen', 'NNP'), ('Dalbir', 'NNP'), ('Singh', 'NNP'), ('Suhag', 
'NNP'), ('briefed', 'VBD'), ('the', 'DT'), ('Army', 'NNP'), ('chief', 'NN'), 
('on', 'IN'), ('the', 'DT'), ('operational', 'JJ'), ('preparedness', 'NN'), 
('and', 'CC'), ('the', 'DT'), ('security', 'NN'), ('scenario', 'NN'), ('in', 
'IN'), ('the', 'DT'), ('eastern', 'NN'), ('region', 'NN'), (',', ','), ("''", 
"''"), ('defence', 'NN'), ('spokesperson', 'NN'), ('Group', 'NNP'), ('Capt', 
'NNP'), ('T', 'NNP'), ('K', 'NNP'), ('Singha', 'NNP'), ('said', 'VBD'), 
('here', 'RB')]

Now, as we see it has multiple VBD elements.
I want to recognize,count and index them all.

If any one can kindly suggest.

Regards,
Subhabrata


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List problem

2012-12-02 Thread Lutz Horn
Him

Am 02.12.2012 um 16:03 schrieb [email protected]:
> I have a list of the following pattern,
> 
> [("''", "''"), ('Eastern', 'NNP'), ('Army', 'NNP'), ('Commander', 'NNP'), 
> ('Lt', 'NNP'), ('Gen', 'NNP'), ('Dalbir', 'NNP'), ('Singh', 'NNP'), ('Suhag', 
> 'NNP'), ('briefed', 'VBD'), ('the', 'DT'), ('Army', 'NNP'), ('chief', 'NN'), 
> ('on', 'IN'), ('the', 'DT'), ('operational', 'JJ'), ('preparedness', 'NN'), 
> ('and', 'CC'), ('the', 'DT'), ('security', 'NN'), ('scenario', 'NN'), ('in', 
> 'IN'), ('the', 'DT'), ('eastern', 'NN'), ('region', 'NN'), (',', ','), ("''", 
> "''"), ('defence', 'NN'), ('spokesperson', 'NN'), ('Group', 'NNP'), ('Capt', 
> 'NNP'), ('T', 'NNP'), ('K', 'NNP'), ('Singha', 'NNP'), ('said', 'VBD'), 
> ('here', 'RB')]
> 
> Now, as we see it has multiple VBD elements.
> I want to recognize,count and index them all.

len([x for x in l if x[1] == 'VBD'])

Lutz

-- 
This email is signed with a CAcert certificate. https://www.cacert.org/
Please do not send me Microsoft Office/Apple iWork documents.
Send OpenDocument instead! http://fsf.org/campaigns/opendocument/
https://duckduckgo.com/ | http://donttrack.us/ | http://dontbubble.us/



smime.p7s
Description: S/MIME cryptographic signature
-- 
http://mail.python.org/mailman/listinfo/python-list


New tutorials

2012-12-02 Thread Mitya Sirenef
Hi everyone, I'm making a series of python tutorials and I've just 
finished the introductory part: http://lightbird.net/larks/intro.html


Feedback is appreciated.. - mitya

--
http://mail.python.org/mailman/listinfo/python-list


Re: List problem

2012-12-02 Thread Thomas Bach
On Sun, Dec 02, 2012 at 04:16:01PM +0100, Lutz Horn wrote:
> 
> len([x for x in l if x[1] == 'VBD'])
> 

Another way is

sum(1 for x in l if x[1] == 'VBD')

which saves the list creation.

Regards,
Thomas.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List problem

2012-12-02 Thread subhabangalore
On Sunday, December 2, 2012 9:29:22 PM UTC+5:30, Thomas Bach wrote:
> On Sun, Dec 02, 2012 at 04:16:01PM +0100, Lutz Horn wrote:
> 
> > 
> 
> > len([x for x in l if x[1] == 'VBD'])
> 
> > 
> 
> 
> 
> Another way is
> 
> 
> 
> sum(1 for x in l if x[1] == 'VBD')
> 
> 
> 
> which saves the list creation.
> 
> 
> 
> Regards,
> 
>   Thomas.

Thanks. After I posted I got a solution as,
[x for x, y in enumerate(chunk_word) if "/VB" in y]
but you are smarter.
Thanks.
Regards,
Subhabrata. 
-- 
http://mail.python.org/mailman/listinfo/python-list


Weird exception handling behavior -- late evaluation in except clause

2012-12-02 Thread Roy Smith
This is kind of weird (Python 2.7.3):

try:
print "hello"
except foo:
print "foo"

prints "hello".  The problem (IMHO) is that apparently the except clause 
doesn't get evaluated until after some exception is caught.  Which means 
it never notices that foo is not defined until it's too late.

This just came up in some code, where I was trying to catch a very rare 
exception.  When the exception finally happened, I discovered that I had 
a typo in the except clause (I had mis-spelled the name of the 
exception).  So, instead of getting some useful information, I got an 
AttributeError :-(

Is this a bug, or intended behavior?  It seems to me it would be much 
more useful (if slightly more expensive) to evaluate the names of the 
exceptions in the expect clause before running the try block.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Weird exception handling behavior -- late evaluation in except clause

2012-12-02 Thread Hans Mulder
On 2/12/12 18:25:22, Roy Smith wrote:
> This is kind of weird (Python 2.7.3):
> 
> try:
> print "hello"
> except foo:
> print "foo"
> 
> prints "hello".  The problem (IMHO) is that apparently the except clause 
> doesn't get evaluated until after some exception is caught.  Which means 
> it never notices that foo is not defined until it's too late.
> 
> This just came up in some code, where I was trying to catch a very rare 
> exception.  When the exception finally happened, I discovered that I had 
> a typo in the except clause (I had mis-spelled the name of the 
> exception).  So, instead of getting some useful information, I got an 
> AttributeError :-(
> 
> Is this a bug, or intended behavior?  It seems to me it would be much 
> more useful (if slightly more expensive) to evaluate the names of the 
> exceptions in the expect clause before running the try block.

It's intended behaviour: Python runs your script from top to bottom.
It doesn't peek ahead at except: clauses.  Even it an exception is
raised, the exception names are not eveluated all at once.  Python
begins by evaluating the first exception name.  It the exception at
hand is an instance of that, Python has found a match and the rest
of the names are left unevaluated:

>>> try:
...  1/0
... except ZeroDivisionError:
...  print "hello"
... except 1/0:
...  pass
...
hello

There's probably some lint-like tool that can find this kind of issue.


Hope this helps,

-- HansM
-- 
http://mail.python.org/mailman/listinfo/python-list


Python Noob Question.

2012-12-02 Thread Owatch
Sorry, but I was redirected here via a thread on another forum concerning where 
to find help for Python. (Python-forums.org not working for me)

I wanted to ask if there was any way I could write a simple Python Temperature 
program. Where essentially it somehow (Now sure how, I'm a noob remember) read 
temps off of windows (Providing windows records cpu temps) from their program 
and then rang an alarm if temperatures exceeded a certain value.

I can write the part about the alarm going off. But I have no idea how to have 
it stream or update values it would grab from that program (whatever it might 
be). 

Could you please help, or tell me if its possible. 
Thanks, Owatch.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Weird exception handling behavior -- late evaluation in except clause

2012-12-02 Thread Terry Reedy

On 12/2/2012 12:25 PM, Roy Smith wrote:

This is kind of weird (Python 2.7.3):

try:
 print "hello"
except foo:
 print "foo"

prints "hello".  The problem (IMHO) is that apparently the except clause
doesn't get evaluated until after some exception is caught.  Which means
it never notices that foo is not defined until it's too late.

This just came up in some code, where I was trying to catch a very rare
exception.  When the exception finally happened, I discovered that I had
a typo in the except clause (I had mis-spelled the name of the
exception).  So, instead of getting some useful information, I got an
AttributeError :-(


You would have the same problem if you spelled the exception name right 
but misspelled a name in the corresponding block. This is PyLint, 
PyChecker, xxx territory.


Note that proper checking requires execution of imports.

import mymod

try: pass
except mymod.MymogException: pass  # whoops


Is this a bug, or intended behavior?  It seems to me it would be much
more useful (if slightly more expensive) to evaluate the names of the
exceptions in the expect clause before running the try block.


'try:' is very cheap. Searching ahead any checking names in all the 
except clauses would be much more expensive -- and useless after the 
first time the code is run after being revised.


--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


Re: setup.py Choosing Older Compiler On Windows

2012-12-02 Thread Dave Angel
On 12/02/2012 09:34 AM, Ami Tavory wrote:
>   Hello,
>
>   I'm porting a C++ extension module to a Windows machine that has both VC8
> and VC10 installed. Unfortunately, `setup.py build` tries to build using
> VC8, which fails (the extension uses C++ standard libraries that VC  didn't
> used to have). Is there a way to get setup.py to use VC10 (preferably
> externally, without modifying the script)?
>
>  

I haven't had to do Windows C++ development for many years, but there
used to be a vcvars.bat  in each compiler installation, and you run the
one corresponding to the compiler & libraries you want.



-- 

DaveA

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Weird exception handling behavior -- late evaluation in except clause

2012-12-02 Thread Steven D'Aprano
On Sun, 02 Dec 2012 12:25:22 -0500, Roy Smith wrote:

> This is kind of weird (Python 2.7.3):
> 
> try:
> print "hello"
> except foo:
> print "foo"
> 
> prints "hello".  The problem (IMHO) is that apparently the except clause
> doesn't get evaluated until after some exception is caught.  Which means
> it never notices that foo is not defined until it's too late.

This is exactly the same as, well, everything in Python. Nothing is 
evaluated until needed.

Consider this piece of legal Python code:

Err = None
if condition(x) > 100:
Err = OneException
elif another_condition(x):
Err = AnotherException
try:
spam(a, b, c)
except Err:
recover()


And consider that spam() may very well set the global Err to yet another 
value, or delete it altogether, before raising. How should Python check 
that Err is defined to an actual exception ahead of time?

The names of exceptions are no different from any other names in Python. 
They're merely names, and they're looked up at runtime. If you want to 
boggle/confuse/annoy your friends, shadowing builtins can help:


py> def spam(x):
... global UnicodeEncodeError
... UnicodeEncodeError = ZeroDivisionError
... return 1/x
...
py> try:
... spam(0)
... except UnicodeEncodeError:
... print("Divided by zero")
...
Divided by zero


(By the way, if you ever do this by accident, you can recover with a 
simple "del UnicodeEncodeError" to restore access to the builtin.)

As Terry has said, those sort of pre-runtime checks are the 
responsibility of pylint or pychecker or equivalent. Python is too 
dynamic to allow the sort of compile-time checks you can get from a 
Haskell, C or Pascal, so the Python compiler delegates responsibility to 
third-party applications. There's no point in making an exceptions for 
exceptions.


> This just came up in some code, where I was trying to catch a very rare
> exception.  When the exception finally happened, I discovered that I had
> a typo in the except clause (I had mis-spelled the name of the
> exception).  So, instead of getting some useful information, I got an
> AttributeError :-(

One of the nice features of Python 3:

py> try:
... 1/0
... except ZeroDividingError:
... pass
...
Traceback (most recent call last):
  File "", line 2, in 
ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "", line 3, in 
NameError: name 'ZeroDividingError' is not defined


Very useful for debugging unexpected errors in except clauses. The 
downside is that until Python 3.3, there's no way to turn it off when you 
intentionally catch one exception and raise another in it's place.


> Is this a bug, or intended behavior?  It seems to me it would be much
> more useful (if slightly more expensive) to evaluate the names of the
> exceptions in the expect clause before running the try block.

"Slightly" more expensive? Methinks you are underestimating the level of 
dynamism of Python.

py> def pick_an_exception():
... global x
... if 'x' in globals():
... return TypeError
... x = None
... return NameError
...
py> for i in range(3):
... try:
... print(x + 1)
... except pick_an_exception() as err:
... print("Caught", err)
...
Caught name 'x' is not defined
Caught unsupported operand type(s) for +: 'NoneType' and 'int'
Caught unsupported operand type(s) for +: 'NoneType' and 'int'


The except expression can be arbitrarily expensive, with arbitrary side-
effects.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


loops

2012-12-02 Thread Verde Denim
I'm just getting into py coding, and have come across an oddity in a py
book - while loops that don't work as expected...

import random

MIN = 1
MAX = 6

def main():
again = 'y'

while again == 'y':
print('Rolling...')
print('Values are: ')
print(random.randint(MIN, MAX))
print(random.randint(MIN, MAX))

again = input('Roll again? (y = yes): ')

main()

Produces -
python dice_roll.py
Rolling...
Values are:
5
4
Roll again? (y = yes): y
Traceback (most recent call last):
  File "dice_roll.py", line 17, in 
main()
  File "dice_roll.py", line 15, in main
again = input('Roll again? (y = yes): ')
  File "", line 1, in 
NameError: name 'y' is not defined

This same loop structure appears in many places in this book "Starting
out with Python, 2nd ed, Tony Gaddis), and they all yield the same
error. Is there something I'm missing here?

Thanks for the input...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: loops

2012-12-02 Thread Mitya Sirenef

On 12/02/2012 04:39 PM, Verde Denim wrote:

I'm just getting into py coding, and have come across an oddity in a py
book - while loops that don't work as expected...

import random

MIN = 1
MAX = 6

def main():
 again = 'y'

 while again == 'y':
 print('Rolling...')
 print('Values are: ')
 print(random.randint(MIN, MAX))
 print(random.randint(MIN, MAX))

 again = input('Roll again? (y = yes): ')

main()

Produces -
python dice_roll.py
Rolling...
Values are:
5
4
Roll again? (y = yes): y
Traceback (most recent call last):
   File "dice_roll.py", line 17, in 
 main()
   File "dice_roll.py", line 15, in main
 again = input('Roll again? (y = yes): ')
   File "", line 1, in 
NameError: name 'y' is not defined

This same loop structure appears in many places in this book "Starting
out with Python, 2nd ed, Tony Gaddis), and they all yield the same
error. Is there something I'm missing here?

Thanks for the input...


I believe that should be raw_input, not input . input() evaluates user's 
input

in local scope.  -m
--
http://mail.python.org/mailman/listinfo/python-list


Re: loops

2012-12-02 Thread Chris Angelico
On Mon, Dec 3, 2012 at 8:39 AM, Verde Denim  wrote:
> again = input('Roll again? (y = yes): ')
>
> Roll again? (y = yes): y
> Traceback (most recent call last):
>   File "dice_roll.py", line 17, in 
> main()
>   File "dice_roll.py", line 15, in main
> again = input('Roll again? (y = yes): ')
>   File "", line 1, in 
> NameError: name 'y' is not defined
>
> This same loop structure appears in many places in this book "Starting
> out with Python, 2nd ed, Tony Gaddis), and they all yield the same
> error. Is there something I'm missing here?
>
> Thanks for the input...

The loop isn't the problem, your input() function is. In Python 2,
input() did the strange thing of evaluating what you typed; to simply
ask for a line and return it as a string, use raw_input(). As of
Python 3, the input() function does what raw_input() used to do, which
is what you want here.

You may be able to fix the problem by simply changing the function
name, but if the book is assuming Python 3, you will probably want to
switch to the newer interpreter. Also, the newer Pythons have a large
number of improvements in other areas, so it's well worth switching
anyway.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Noob Question.

2012-12-02 Thread Steven D'Aprano
On Sun, 02 Dec 2012 12:33:58 -0800, Owatch wrote:

> Sorry, but I was redirected here via a thread on another forum
> concerning where to find help for Python. (Python-forums.org not working
> for me)
> 
> I wanted to ask if there was any way I could write a simple Python
> Temperature program. Where essentially it somehow (Now sure how, I'm a
> noob remember) read temps off of windows (Providing windows records cpu
> temps) from their program and then rang an alarm if temperatures
> exceeded a certain value.
> 
> I can write the part about the alarm going off. But I have no idea how
> to have it stream or update values it would grab from that program
> (whatever it might be).
> 
> Could you please help, or tell me if its possible. Thanks, Owatch.

Yes, it's possible, but if you have no idea which program you want to 
read temperature data from, what makes you think we would have any better 
idea? This is a Python forum, not a "Windows programs that give 
temperature data" forum.

I recommend you start by doing some research into how to read the 
temperature data, including the name of the program. Once you have that, 
come back to us for help in using Python.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: loops

2012-12-02 Thread Steven D'Aprano
On Sun, 02 Dec 2012 16:39:07 -0500, Verde Denim wrote:

> I'm just getting into py coding, and have come across an oddity in a py
> book - while loops that don't work as expected...

This error has nothing to do with the while loop. Read the error message 
that Python gives you:

> Traceback (most recent call last):
>   File "dice_roll.py", line 17, in 
> main()
>   File "dice_roll.py", line 15, in main
> again = input('Roll again? (y = yes): ')
>   File "", line 1, in 
> NameError: name 'y' is not defined


In Python 2, there is a serious design flaw with the "input" function, 
fortunately corrected in Python 3. The flaw is that input automatically 
evaluates whatever you type as Python code. So when you type "y", the 
input function tries to evaluate the name y, which doesn't exist.

No while loop required:

py> again = input('Roll again? (y = yes): ')
Roll again? (y = yes): y
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 1, in 
NameError: name 'y' is not defined


The solution is either to use Python 3, where this is no longer an issue, 
or to replace "input" with "raw_input":

py> again = raw_input('Roll again? (y = yes): ')
Roll again? (y = yes): y
py> again
'y'




> This same loop structure appears in many places in this book "Starting
> out with Python, 2nd ed, Tony Gaddis), and they all yield the same
> error. Is there something I'm missing here?
> 
> Thanks for the input...

I see what you did there... *wink*



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Splitting Tree

2012-12-02 Thread Cameron Simpson
On 02Dec2012 07:02, [email protected]  wrote:
| On Sunday, December 2, 2012 5:39:32 PM UTC+5:30, [email protected] wrote:
| > I am using NLTK and I used the following command,
| > chunk=nltk.ne_chunk(tag)
| > 
| > print "The Chunk of the Line Is:",chunk
| > 
| > The Chunk of the Line Is: (S
| >   ''/''
| >   It/PRP
[...]
| > Now I am trying to split the output preferably by ",/,".
[...]
|
| Sorry to ask this. I converted in string and then splitted it.

I'm glad you solved your problem, but I would like to point out that
this is generally a risky way of manipulating data.

The problem arises if the string you're splitting on occurs as a literal
piece of text, but _not_ in the sense you intend. It may be the case
that it will not happen in your particular situation, but in general the
procedure:
  - convert structure to string somehow
  - perfect simple text manipulation
  - unconvert
is at risk of simplistic parsing of the string.

A common example is with CSV data. Supposing you wanted the the third
column from an array of tuples:

  rows = [ (1,2,"A",4),
   (5,6,"B",8),
   (9,10,"C,D",12),
 ]

and you wanted [ "A", "B", "C,D" ]. If one went with the "convert to
text" approach, and decided that converting each tuple to a CSV style
data row was a good idea you might write:

  column_3 = []
  for row in rows:
csv_string = ",".join( str(item) for item in row )
item3 = csv_string.split(",")[2]
column_3.append(item3)

The (simplistic) code above with give you "C" from the third row, not
"C,D". Because it naively assumes there are no commas in the data, and
then does a simplistic textual split to find the third column.

Obviously you woldn't really do that for something this simple; it is to
show the issue. But your situation where manipulating a tree was tricky
and you converted it to a string is very similar conceptually.

Hoping this shows you the issue,
-- 
Cameron Simpson 

I'm not making any of this up you know. - Anna Russell
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: loops

2012-12-02 Thread Verde Denim
On 12/02/2012 04:43 PM, Mitya Sirenef wrote:
> On 12/02/2012 04:39 PM, Verde Denim wrote:
>> I'm just getting into py coding, and have come across an oddity in a py
>> book - while loops that don't work as expected...
>>
>> import random
>>
>> MIN = 1
>> MAX = 6
>>
>> def main():
>>  again = 'y'
>>
>>  while again == 'y':
>>  print('Rolling...')
>>  print('Values are: ')
>>  print(random.randint(MIN, MAX))
>>  print(random.randint(MIN, MAX))
>>
>>  again = input('Roll again? (y = yes): ')
>>
>> main()
>>
>> Produces -
>> python dice_roll.py
>> Rolling...
>> Values are:
>> 5
>> 4
>> Roll again? (y = yes): y
>> Traceback (most recent call last):
>>File "dice_roll.py", line 17, in 
>>  main()
>>File "dice_roll.py", line 15, in main
>>  again = input('Roll again? (y = yes): ')
>>File "", line 1, in 
>> NameError: name 'y' is not defined
>>
>> This same loop structure appears in many places in this book "Starting
>> out with Python, 2nd ed, Tony Gaddis), and they all yield the same
>> error. Is there something I'm missing here?
>>
>> Thanks for the input...
> 
> I believe that should be raw_input, not input . input() evaluates user's
> input
> in local scope.  -m

m
Nicely done! That fixed it! Is that a version feature or should I take
what I find in these books with a grain of salt?

-j
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: loops

2012-12-02 Thread MRAB

On 2012-12-02 22:28, Verde Denim wrote:

On 12/02/2012 04:43 PM, Mitya Sirenef wrote:

On 12/02/2012 04:39 PM, Verde Denim wrote:

I'm just getting into py coding, and have come across an oddity in a py
book - while loops that don't work as expected...


[snip]


This same loop structure appears in many places in this book "Starting
out with Python, 2nd ed, Tony Gaddis), and they all yield the same
error. Is there something I'm missing here?

Thanks for the input...


I believe that should be raw_input, not input . input() evaluates user's
input
in local scope.  -m


m
Nicely done! That fixed it! Is that a version feature or should I take
what I find in these books with a grain of salt?


It's a difference between Python 2 and Python 3.

Normally a lot of care is taken to maintain backwards compatibility,
but it was decided that the language needed to be tidied up and certain
misfeatures removed.

Python 2 has stopped at version 2.7. Python 3 is currently at version
3.3.
--
http://mail.python.org/mailman/listinfo/python-list


Re: loops

2012-12-02 Thread Rhodri James

On Sun, 02 Dec 2012 22:28:48 -, Verde Denim  wrote:


Nicely done! That fixed it! Is that a version feature or should I take
what I find in these books with a grain of salt?


It's a difference between Python 2 and Python 3 (which is almost certainly  
what your book told you to).  The other feature which you will trip over  
is that "print" was a keyword in Python 2, but is a function in Python 3.   
This happens to have made no difference in the example you gave, but try  
this:


Python 3.2 (r32:88445, Oct 20 2012, 14:09:29)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

print(1,2,3)

1 2 3


Python 2.7.1+ (r271:86832, Sep 27 2012, 21:12:17)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

print(1,2,3)

(1, 2, 3)

--
Rhodri James *-* Wildebeest Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Re: Textmining

2012-12-02 Thread alex23
On Dec 1, 7:27 pm, [email protected] wrote:
> http://pypi.python.org/pypi/textmining/1.0
> I am not getting how to download and use it.

Did you look at any of the documentation on its home page?
http://www.christianpeccei.com/projects/textmining/

> The latest version (1.0) is available from the Python Package Index.
> To install, extract the .zip file and run:
> python setup.py install

Immediately after that are usage examples.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: setup.py Choosing Older Compiler On Windows

2012-12-02 Thread Irmen de Jong
On 2-12-2012 22:06, Dave Angel wrote:
> On 12/02/2012 09:34 AM, Ami Tavory wrote:
>>   Hello,
>>
>>   I'm porting a C++ extension module to a Windows machine that has both VC8
>> and VC10 installed. Unfortunately, `setup.py build` tries to build using
>> VC8, which fails (the extension uses C++ standard libraries that VC  didn't
>> used to have). Is there a way to get setup.py to use VC10 (preferably
>> externally, without modifying the script)?
>>
>>  
> 
> I haven't had to do Windows C++ development for many years, but there
> used to be a vcvars.bat  in each compiler installation, and you run the
> one corresponding to the compiler & libraries you want.


Forcing it to use a different compiler than the one that was used to build 
Python
itself, may very well lead to a non-working extension module.

Irmen


-- 
http://mail.python.org/mailman/listinfo/python-list


Using Xpath to parse a Yahoo Finance page

2012-12-02 Thread Jason Hsu
I'm trying to extract the data on "total assets" from Yahoo Finance using 
Python 2.7 and lxml. 

Here is a special test script I set up to work on this issue:

import urllib
import lxml
import lxml.html 

url_local1 = 
"http://www.smartmoney.com/quote/FAST/?story=financials&timewindow=1&opt=YB&isFinprint=1&framework.view=smi_emptyView";
 
result1 = urllib.urlopen(url_local1)
element_html1 = result1.read()
doc1 = lxml.html.document_fromstring (element_html1)
list_row1 = doc1.xpath(u'.//th[div[text()="Total 
Assets"]]/following-sibling::td/text()')
print list_row1

url_local2 = "http://finance.yahoo.com/q/bs?s=FAST"; 
result2 = urllib.urlopen(url_local2)
element_html2 = result2.read()
doc2 = lxml.html.document_fromstring (element_html2)
list_row2 = doc2.xpath(u'.//td[strong[text()="Total 
Assets"]]/following-sibling::td/strong/text()')
print list_row2

I'm able to get the row of data on total assets from the Smartmoney page, but I 
get just an empty list when I try to parse the Yahoo Finance page.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using Xpath to parse a Yahoo Finance page

2012-12-02 Thread MRAB

On 2012-12-03 01:23, Jason Hsu wrote:

I'm trying to extract the data on "total assets" from Yahoo Finance using 
Python 2.7 and lxml.

Here is a special test script I set up to work on this issue:

 import urllib
 import lxml
 import lxml.html

 url_local1 = 
"http://www.smartmoney.com/quote/FAST/?story=financials&timewindow=1&opt=YB&isFinprint=1&framework.view=smi_emptyView";
 result1 = urllib.urlopen(url_local1)
 element_html1 = result1.read()
 doc1 = lxml.html.document_fromstring (element_html1)
 list_row1 = doc1.xpath(u'.//th[div[text()="Total 
Assets"]]/following-sibling::td/text()')
 print list_row1

 url_local2 = "http://finance.yahoo.com/q/bs?s=FAST";
 result2 = urllib.urlopen(url_local2)
 element_html2 = result2.read()
 doc2 = lxml.html.document_fromstring (element_html2)
 list_row2 = doc2.xpath(u'.//td[strong[text()="Total 
Assets"]]/following-sibling::td/strong/text()')
 print list_row2

I'm able to get the row of data on total assets from the Smartmoney page, but I 
get just an empty list when I try to parse the Yahoo Finance page.


The problem is that you're asking it to look for an exact match.

If you look at the HTML itself, you'll see that there's whitespace
around the "Total Assets" part.

This should work:

list_row2 = doc2.xpath(u'.//td[strong[contains(text(),"Total 
Assets")]]/following-sibling::td/strong/text()')


(Although I tested it in Python 3.2.)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Using Xpath to parse a Yahoo Finance page

2012-12-02 Thread Jason Hsu
On Sunday, December 2, 2012 8:25:45 PM UTC-6, MRAB wrote:
> 
> list_row2 = doc2.xpath(u'.//td[strong[contains(text(),"Total 
> 
> Assets")]]/following-sibling::td/strong/text()')
> 
Thanks, MRAB.  Your suggestion works!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Weird exception handling behavior -- late evaluation in except clause

2012-12-02 Thread Chris Angelico
On Mon, Dec 3, 2012 at 8:31 AM, Steven D'Aprano
 wrote:
> Consider this piece of legal Python code:
>
> Err = None
> if condition(x) > 100:
> Err = OneException
> elif another_condition(x):
> Err = AnotherException
> try:
> spam(a, b, c)
> except Err:
> recover()

Legal it may be, but are there times when you actually _need_ this
level of dynamism? It strikes me as a pretty weird way of going about
things.

I agree with the point you're making, but this feels like a contrived
example, and I'm curious as to whether it can be uncontrived.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using Xpath to parse a Yahoo Finance page

2012-12-02 Thread Stefan Behnel
MRAB, 03.12.2012 03:25:
> On 2012-12-03 01:23, Jason Hsu wrote:
>> I'm trying to extract the data on "total assets" from Yahoo Finance using
>> Python 2.7 and lxml.
>>
>> Here is a special test script I set up to work on this issue:
>>
>>  import urllib
>>  import lxml
>>  import lxml.html
>>
>>  url_local1 =
>> "http://www.smartmoney.com/quote/FAST/?story=financials&timewindow=1&opt=YB&isFinprint=1&framework.view=smi_emptyView";
>>
>>  result1 = urllib.urlopen(url_local1)
>>  element_html1 = result1.read()
>>  doc1 = lxml.html.document_fromstring (element_html1)

The last three lines are unnecessarily complicated code. Just use

doc = lxml.html.parse(url_local1)


>>  list_row1 = doc1.xpath(u'.//th[div[text()="Total
>> Assets"]]/following-sibling::td/text()')
>>  print list_row1
>>
>>  url_local2 = "http://finance.yahoo.com/q/bs?s=FAST";
>>  result2 = urllib.urlopen(url_local2)
>>  element_html2 = result2.read()
>>  doc2 = lxml.html.document_fromstring (element_html2)
>>  list_row2 = doc2.xpath(u'.//td[strong[text()="Total
>> Assets"]]/following-sibling::td/strong/text()')
>>  print list_row2
>>
>> I'm able to get the row of data on total assets from the Smartmoney page,
>> but I get just an empty list when I try to parse the Yahoo Finance page.
>>
> The problem is that you're asking it to look for an exact match.
> 
> If you look at the HTML itself, you'll see that there's whitespace
> around the "Total Assets" part.
> 
> This should work:
> 
> list_row2 = doc2.xpath(u'.//td[strong[contains(text(),"Total
> Assets")]]/following-sibling::td/strong/text()')

Something like "contains(text(),"Total Assets")" is better expressed as
"contains(.,"Total Assets")" because it considers the complete text content
instead of just one text node.

Stefan


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Weird exception handling behavior -- late evaluation in except clause

2012-12-02 Thread Steven D'Aprano
On Mon, 03 Dec 2012 16:24:50 +1100, Chris Angelico wrote:

> On Mon, Dec 3, 2012 at 8:31 AM, Steven D'Aprano
>  wrote:
>> Consider this piece of legal Python code:
>>
>> Err = None
>> if condition(x) > 100:
>> Err = OneException
>> elif another_condition(x):
>> Err = AnotherException
>> try:
>> spam(a, b, c)
>> except Err:
>> recover()
> 
> Legal it may be, but are there times when you actually _need_ this level
> of dynamism? It strikes me as a pretty weird way of going about things.
> 
> I agree with the point you're making, but this feels like a contrived
> example, and I'm curious as to whether it can be uncontrived.


Yeah, in hindsight it was a pretty crappy example. But this sort of 
dynamism really is useful:

def testRaises(exc, func, *args):
try:
result = func(*args)
except exc:
return
raise AssertionError("expected exception but didn't get one")


def wrap(func, exc, default=None):
@functools.wraps(func)
def inner(*args):
try:
return func(*args)
except exc:
return default
return inner


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Weird exception handling behavior -- late evaluation in except clause

2012-12-02 Thread Chris Angelico
On Mon, Dec 3, 2012 at 6:30 PM, Steven D'Aprano
 wrote:
> Yeah, in hindsight it was a pretty crappy example. But this sort of
> dynamism really is useful:
>
> def testRaises(exc, func, *args):
> try:
> result = func(*args)
> except exc:
> return
> raise AssertionError("expected exception but didn't get one")
>
>
> def wrap(func, exc, default=None):
> @functools.wraps(func)
> def inner(*args):
> try:
> return func(*args)
> except exc:
> return default
> return inner

Ah, that makes good sense. The 'except' clause takes a parameter, so
it follows logically that you could pass a parameter to something that
wraps an except clause.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list