Re: A Unique XML Parsing Problem

2010-10-24 Thread Stefan Behnel

Devon, 24.10.2010 01:40:

I must quickly and efficiently parse some data contained in multiple
XML files in order to perform some learning algorithms on the data.

I have thousands of files, each file corresponds to a single song.
Each XML file contains information extracted from the song (called
features). Examples include tempo, time signature, pitch classes, etc.

> [...]

I am a statistician and therefore used to data being stored in CSV-
like files, with each row being a datapoint, and each column being a
feature. I would like to parse the data out of these XML files and
write them out into a CSV file.  Any help would be greatly appreciated.
Mostly I am looking for a point in the right direction. I have heard
about Beautiful Soup but never used it. I am currently reading Dive
Into Python's chapters on HTML and XML parsing.


That chapter is mostly out of date, and BeautifulSoup is certainly not the 
right tool for dealing with XML, both for performance and compliance 
reasons. If you need performance, as you stated above, look at cElementTree 
in the stdlib.




And I am also more
concerned about how to use the tags in the XML files to build feature
names so I do not have to hard code them. For example, the first
feature given by the above code would be "track duration" with a value
of 29.12331


If the rules are as simple as that (i.e. tag name + attribute name), it'll 
be easy going with ElementTree. Don't put too much effort into separating 
the data from the XML format, though. XML parsing is fast and has the clear 
advantage over CSV files that the data is safely stored in a well defined, 
expressive format, including character encoding and named data fields.


Stefan

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


Interface centering

2010-10-24 Thread Jah_Alarm
sorry 4 the sillu question.

I've designed a GUI. How can I center on the screen? (i.e. it's always
launched in the center of the screen)

cheers,

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


Re: Interface centering

2010-10-24 Thread Chris Rebert
On Sun, Oct 24, 2010 at 12:21 AM, Jah_Alarm  wrote:
> sorry 4 the sillu question.
>
> I've designed a GUI. How can I center on the screen? (i.e. it's always
> launched in the center of the screen)

Which GUI toolkit did you use?

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


newbie to python

2010-10-24 Thread Hrishikesh
I want to start python, I have just downloaded python compiler, Can
somebody please tell me what python really is and explain its
architecture, in what problems it is used and from where should I
start?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Interface centering

2010-10-24 Thread Jah_Alarm
hi, I'm using Tkinter

cheers,

Alex

On 24 окт, 20:26, Chris Rebert  wrote:
> On Sun, Oct 24, 2010 at 12:21 AM, Jah_Alarm  wrote:
> > sorry 4 the sillu question.
>
> > I've designed a GUI. How can I center on the screen? (i.e. it's always
> > launched in the center of the screen)
>
> Which GUI toolkit did you use?
>
> Cheers,
> Chris

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


Re: newbie to python

2010-10-24 Thread Chris Rebert
On Sun, Oct 24, 2010 at 12:29 AM, Hrishikesh  wrote:
> I want to start python, I have just downloaded python compiler,

Most consider it an interpreter (though it does compile the source
code into high-level bytecode).

> Can
> somebody please tell me what python really is

A programming language. What's a question like that even mean?

> and explain its
> architecture,

Define "architecture" in this context.

> in what problems it is used

Various and sundry; it's a general-purpose language.
But see http://www.python.org/about/apps/

> and from where should I
> start?

The official tutorial of course.
If you downloaded version 2.x: http://docs.python.org/tutorial/
If you downloaded version 3.x: http://docs.python.org/py3k/

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


Re: newbie to python

2010-10-24 Thread Arnaud Delobelle
Hrishikesh  writes:

> I want to start python, I have just downloaded python compiler, Can
> somebody please tell me what python really is and explain its
> architecture, in what problems it is used and from where should I
> start?

I would start by looking at

http://www.python.org

To learn Python, go through the tutorial:

http://docs.python.org/tutorial/

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


Re: newbie to python

2010-10-24 Thread Hrishikesh
On Oct 24, 12:48 am, Arnaud Delobelle  wrote:
> Hrishikesh  writes:
> > I want to start python, I have just downloaded python compiler, Can
> > somebody please tell me what python really is and explain its
> > architecture, in what problems it is used and from where should I
> > start?
>
> I would start by looking at
>
>    http://www.python.org
>
> To learn Python, go through the tutorial:
>
>    http://docs.python.org/tutorial/
>
> --
> Arnaud

Thanks guys,

I knew its an interpreter, mistakenly put compiler, I am reading the
docs, any further programming issues I will post soon!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie to python

2010-10-24 Thread Arnaud Delobelle
Hrishikesh  writes:

> Thanks guys,
>
> I knew its an interpreter, mistakenly put compiler [...]

Oops!  You just started a flame war ;)

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


Re: Exception Handling in Python 3

2010-10-24 Thread Martin v. Loewis
Am 24.10.2010 07:01, schrieb Steve Holden:
> I was somewhat surprised to discover that Python 3 no longer allows an
> exception to be raised in an except clause (or rather that it reports it
> as a separate exception that occurred during the handling of the first).

I think you are misinterpreting what you are seeing. The exception being
raised actually *is* an attribute error, and it actually is the
attribute error that gets reported. It's only that reporting an
exception that has a __context__ first reports the context, then reports
the actual exception.

You may now wonder whether it is possible to set __context__ to None
somehow. See PEP 3134:

Open Issue: Suppressing Context

As written, this PEP makes it impossible to suppress '__context__',
since setting exc.__context__ to None in an 'except' or 'finally'
clause will only result in it being set again when exc is raised.

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


Re: Interface centering

2010-10-24 Thread Peter Otten
Jah_Alarm wrote:

> hi, I'm using Tkinter
> 
> cheers,
> 
> Alex
> 
> On 24 окт, 20:26, Chris Rebert  wrote:
>> On Sun, Oct 24, 2010 at 12:21 AM, Jah_Alarm  wrote:
>> > sorry 4 the sillu question.
>>
>> > I've designed a GUI. How can I center on the screen? (i.e. it's always
>> > launched in the center of the screen)
>>
>> Which GUI toolkit did you use?
>>
>> Cheers,
>> Chris

First hit googling for 'tkinter center' is

http://www.daniweb.com/forums/thread66181.html

With minor stylistic changes this becomes:

from Tkinter import *
 
def center_window(w, h):
# get screen width and height
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
# calculate position x, y
x = (ws//2) - (w//2) 
y = (hs//2) - (h//2)
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
 
root = Tk() 
center_window(500, 300) 
root.mainloop()

A good basic source for Tkinter programming is

http://infohost.nmt.edu/tcc/help/pubs/tkinter/

Geometry strings are explained here:

http://infohost.nmt.edu/tcc/help/pubs/tkinter/std-attrs.html#geometry

Peter

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


*** Fundamental Right of Every Child ****

2010-10-24 Thread neha shena
*** Fundamental Right of  Every Child 

Education is a human right with immense power to transform. On its
foundation rest the cornerstones of freedom, democracy and sustainable
human development."Education is an act or experience that has a
formative effect on the mind, character or physical potential of an
individual. A vital element to balance the societal factors, education
contributes to the economic development of a country. Countries
without educated population cannot foresee and implement the best
policies necessary for the growth of an otherwise developing country.

http://childschooledu.blogspot.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Tools for turning Python code into XMI?

2010-10-24 Thread Stefan Schwarzer
Hello,

I'm looking for a tool which can read Python files and write
a corresponding XMI file for import into UML tools.

Ideally, the conversion tool should:

- be open source or freeware

- be available for Linux

- be a command line tool

- allow to specify exactly the Python files that should to
  be taken into account

- include operations, together with their parameters

- include attributes (in the UML sense) if possible (I know
  this isn't reliable)

Here are some programs I found via PyPI or search engine and
tried so far. All of them miss something I'd really like to
have.

- pyreverse (now included with Pylint):
  http://www.logilab.org/857

  I think this has the most potential. You can specify the
  files to import on the command line. There are many output
  formats for the information from the Python code, but XMI
  doesn't seem to be included. There are a few formats that
  could be parsed and turned into XMI (plain, plain-ext).
  Unfortunately, pyreverse doesn't include the parameters of
  operations.

- Umbrello:
  http://uml.sourceforge.net/

  Umbrello is a graphical KDE tool. You can specify either
  single files or whole directories including
  subdirectories. In my case, one of the subdirectories is
  for unit tests, which clutters the UML model. Attributes
  (again, in the UML sense) are seemingly not considered but
  operations. The parameters and return values of the latter,
  however, are all output as of type "string".

- Gaphor:
  http://gaphor.sourceforge.net/

  Gaphor is a GUI tool allowing to specify the files one by
  one which might be ok for a few files but surely
  becomes tedious if there are some more. The generated
  diagram, which can be exported to XMI, lacks the
  parameters of the operations.

Do you have any other suggestions for programs I should have
a look at? Does someone even has written such a tool and has
not published it yet? :)

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


<<< What Online University Scholarships Are Available? >>>

2010-10-24 Thread neha shena
<<< What Online University Scholarships Are Available? >>>

Contrary to what you might believe, college scholarships and grants
are available to students other than those with a perfect SAT score,
or the star of the basketball team. They are available for every type
of student interested in a higher education; and that includes
students interested in online universities.

READ MORE ON

http://studyabrods.blogspot.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Exception Handling in Python 3

2010-10-24 Thread Peter Otten
Steve Holden wrote:

> On 10/24/2010 1:26 AM, Chris Rebert wrote:
>>> I was somewhat surprised to discover that Python 3 no longer allows an
>>> > exception to be raised in an except clause (or rather that it reports
>>> > it as a separate exception that occurred during the handling of the
>>> > first).
>> 
> [snip]
>>> > What
>>> > is the correct paradigm for this situation?
>> There doesn't seem to be one at the moment, although the issue isn't
>> very serious. Your Traceback is merely being made slightly longer/more
>> complicated than you'd prefer; however, conversely, what if a bug was
>> to be introduced into your exception handler? Then you'd likely very
>> much appreciate the "superfluous" Traceback info.
>> 
>> Your quandary is due to the unresolved status of the "Open Issue:
>> Suppressing Context" in PEP 3141
>> (http://www.python.org/dev/peps/pep-3134/ ). I guess you could start a
>> discussion about closing that issue somehow.
> 
> You're right about the issue not being serious, (and about the possible
> solution, though I don't relish a lengthy discussion on python-dev) but
> it does seem that there ought to be some way to suppress that
> __context__. From the user's point of view the fact that I am raising
> AttributeError because of some implementation detail of __getattr__() is
> exposing *way* too much information.
> 
> I even tried calling sys.exc_clear(), but alas that doesn't help :(

You can install a custom excepthook:

>>> import traceback, sys
>>> from functools import partial
>>> def f():
... try: 1/0
... except: raise AttributeError
...
>>> f()
Traceback (most recent call last):
  File "", line 2, in f
ZeroDivisionError: int division or modulo by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "", line 1, in 
  File "", line 3, in f
AttributeError
>>> sys.excepthook = partial(traceback.print_exception, chain=False)
>>> f()
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 3, in f
AttributeError

Peter

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


Re: Unix-head needs to Windows-ize his Python script (II)

2010-10-24 Thread Dave Angel

On 2:59 PM, Lawrence D'Oliveiro wrote:

In message
, Dave Angel wrote:


Presumably the original pythonw.exe was called that because it's marked
as a windows-app. In win-speak, that means it has a gui. Applications
that are not so-marked are console-apps, and get a console created if
they weren't already started from one. That's where stdin/stdout/stderr
get pointed.

Which is completely backwards, isn’t it?

No. GUI programs are marked as win-app, so w stands for "Windows". Non 
GUI programs run in the console. Non-gui programs came first, so that's 
the type that doesn't need any tags. No event loop, no window handlers. 
GUI programs are the exception.


DaveA


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


Interactive mode under DOS?

2010-10-24 Thread huisky
Hi,

I'm trying to use the interactive mode under DOS for Python 2.7. As a
newbie, I do NOT know what is the following problem:

>>>world_is_flat=1
>>>if world_is_flat:
. . . print "be carefule to be not fall out!"
File "", line 2
  print "be carefule to be not fall out!"
  ^
IndenatationError : expected an idented block
>>>

Enybody knows how to fix this simple issue?

regards

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


Re: Interactive mode under DOS?

2010-10-24 Thread Lie Ryan
On 10/24/10 21:37, huisky wrote:
> Hi,
> 
> I'm trying to use the interactive mode under DOS for Python 2.7. As a
> newbie, I do NOT know what is the following problem:
> 
 world_is_flat=1
 if world_is_flat:
> .. . . print "be carefule to be not fall out!"
> File "", line 2
>   print "be carefule to be not fall out!"
>   ^
> IndenatationError : expected an idented block

> 
> Enybody knows how to fix this simple issue?
> 

Python is indentation-sensitive, you need to add some whitespace to the
line after an if-statement:

>>> world_is_flat=1
>>> if world_is_flat:
... print "be carefule to be not fall out!"
... 
be carefule to be not fall out!
>>>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Interactive mode under DOS?

2010-10-24 Thread Peter Otten
huisky wrote:

> Hi,
> 
> I'm trying to use the interactive mode under DOS for Python 2.7. As a
> newbie, I do NOT know what is the following problem:
> 
world_is_flat=1
if world_is_flat:
> . . . print "be carefule to be not fall out!"
> File "", line 2
>   print "be carefule to be not fall out!"
>   ^
> IndenatationError : expected an idented block

> 
> Enybody knows how to fix this simple issue?
> 
> regards

The lines that shall be executed when world_is_flat is True must be indented 
more than the if. If you don't follow this rule Python complains and raises 
an IndentationError. 

>>> if world_is_flat:
... print "be careful"
  File "", line 2
print "be careful"
^
IndentationError: expected an indented block

The easiest way to indent in interactive mode is to hit TAB once:

>>> if world_is_flat:
... print "be careful"
...
be careful

By the way, you should not retype your error messages because that is error 
prone. Instead cut and paste. I think there is an entry in the system menu 
of the dos window to help you do that.

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


Re: Exception Handling in Python 3

2010-10-24 Thread Steve Holden
On 10/24/2010 4:48 AM, Martin v. Loewis wrote:
> Am 24.10.2010 07:01, schrieb Steve Holden:
>> I was somewhat surprised to discover that Python 3 no longer allows an
>> exception to be raised in an except clause (or rather that it reports it
>> as a separate exception that occurred during the handling of the first).
> 
> I think you are misinterpreting what you are seeing. The exception being
> raised actually *is* an attribute error, and it actually is the
> attribute error that gets reported. It's only that reporting an
> exception that has a __context__ first reports the context, then reports
> the actual exception.
> 
I don't believe I *am* misinterpreting it. The fact of the matter is
that the context is irrelevant to the user, and there should be some way
to suppress it to avoid over-complicating the traceback.

This behavior is quite reasonable during testing, but I would prefer to
exclude an explicit raise directly in the except handler since that is
hardly to be construed as accidental (whereas an exception in a function
called in the handler perhaps *should* be reported).

> You may now wonder whether it is possible to set __context__ to None
> somehow. See PEP 3134:
> 
> Open Issue: Suppressing Context
> 
> As written, this PEP makes it impossible to suppress '__context__',
> since setting exc.__context__ to None in an 'except' or 'finally'
> clause will only result in it being set again when exc is raised.
> 
I have already read that. Peter Otten has separately explained how to
suppress the behavior using sys.excepthook, which appears to be a
halfway satisfactory solution.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon 2011 Atlanta March 9-17   http://us.pycon.org/
See Python Video!   http://python.mirocommunity.org/
Holden Web LLC http://www.holdenweb.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie to python

2010-10-24 Thread Steve Holden
On 10/24/2010 3:29 AM, Hrishikesh wrote:
> I want to start python, I have just downloaded python compiler, Can
> somebody please tell me what python really is and explain its
> architecture, in what problems it is used and from where should I
> start?

If you don't know the answers to *any* of those questions, one wonders
why you "want to start Python". Perhaps you can explain, or is this
simply trolling?

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon 2011 Atlanta March 9-17   http://us.pycon.org/
See Python Video!   http://python.mirocommunity.org/
Holden Web LLC http://www.holdenweb.com/

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


Re: Exception Handling in Python 3

2010-10-24 Thread Steve Holden
On 10/24/2010 2:22 AM, Lawrence D'Oliveiro wrote:
> In message , Steve 
> Holden wrote:
> 
>> I was somewhat surprised to discover that Python 3 no longer allows an
>> exception to be raised in an except clause (or rather that it reports it
>> as a separate exception that occurred during the handling of the first).
> 
> So what exactly is the problem? Exceptions are so easy to get wrong, it’s 
> just trying to report more info in a complicated situation to help you track 
> down the problem. Why is that bad?
> 
>> In a class's __getattr__() method this means that instead of being able
>> to say
>>
>> try:
>> value = _attrs[name]
>> except KeyError:
>> raise AttributeError ...
>>
>> I am forced to write
>>
>> if name not in _attrs:
>> raise AttributeError ...
>> value = _attrs[name]
> 
> I don’t see why. Presumably if you caught the exception in an outer try-
> except clause, you would pick up your AttributeError, not the KeyError, 
> right? Which is what you want, right?

Yes, *if the exception is caught* then it doesn't make any difference.
If the exception creates a traceback, however, I maintain that the
additional information is confusing to the consumer (while helpful to
the debugger of the consumed code).

I don't want people to think this is a big deal, however. It was just an
"eh?" that I thought must mean I was missing some way of suppressing the
additional traceback. Peter Otten has already provided a solution using
sys.except_hook().

regards
 Steve

-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon 2011 Atlanta March 9-17   http://us.pycon.org/
See Python Video!   http://python.mirocommunity.org/
Holden Web LLC http://www.holdenweb.com/

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


Google AdWords $100 $75 $50 Promotional Vouchers Codes

2010-10-24 Thread tapaas
Need Google AdWords Coupons $100,$75,$50 ..

Contact me at turnier76 @ gmail.com

Only @ Cheap Rates.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A Unique XML Parsing Problem

2010-10-24 Thread Piet van Oostrum
Devon  writes:

> I must quickly and efficiently parse some data contained in multiple
> XML files in order to perform some learning algorithms on the data.
> Info:
>
> I have thousands of files, each file corresponds to a single song.
> Each XML file contains information extracted from the song (called
> features). Examples include tempo, time signature, pitch classes, etc.
> An example from the beginning of one of these files looks like:
>
> 
>  startOfFadeOut="29.12331" loudness="-12.097" tempo="71.031"
> tempoConfidence="0.386" timeSignature="4"
> timeSignatureConfidence="0.974" key="11" keyConfidence="1.000"
> mode="0" modeConfidence="1.000">
> 
> 
> 
> 
> 
> 
> 
> 
> -60.000
> -59.897
> 
> 
> 0.589
> 0.446
> 0.518
> 1.000
> 0.850
> 0.414
> 0.326
> 0.304
> 0.415
> 0.566
> 0.353
> 0.350
>

You could use XSLT to get the data. For example this xslt script extracts 
duration, tempo and time signature into a comma separated list. 

http://www.w3.org/2001/XMLSchema";  
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  
   
  


  


With xsltproc song.xsl song*.xml you would get your output.
No python necessary. Or if you would like to use it inside a Python program, 
use lxml to call the xslt processor, or just XPath to extract the values and 
format them with Python.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A good decorator library

2010-10-24 Thread Felipe Bastos Nunes
I thought about that. I won't use decorators for all the things. I
will use it only to free the simulator users to set which methods they
wanna log or save in a statistic, eq.: number of packets sent,
received, lost...

2010/10/24, Tim Roberts :
> Felipe Bastos Nunes  wrote:
>>
>>Hi! I was looking for a good decorator library to study and make my
>>own decorators. I've read the Bruce Eckel's blog at artima dot com.
>>But I need some more examples. I'm building a WSN simulator like SHOX
>>is in java, but programming it in python. I'd like to use decorators
>>to set methods that would be logged as statistics and some more
>>funcionalities like check a method to check the params' types.
>
> Be careful with this.  Decorators have their uses, but it is very easy to
> make a program completely unreadable through the unjudicious use of
> decorators.
>
> It is not good programming to use a language freature just because it is
> there.
> --
> Tim Roberts, t...@probo.com
> Providenza & Boekelheide, Inc.
> --
> http://mail.python.org/mailman/listinfo/python-list
>


-- 
Felipe Bastos Nunes
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python3: API Documentation generator

2010-10-24 Thread Tim Diels

On 27/09/2010 20:44, Ken Watford wrote:

On Mon, Sep 27, 2010 at 1:15 PM, Tim Diels  wrote:

On 27/09/2010 09:02, Chris Rebert wrote:

On Sun, Sep 26, 2010 at 11:56 PM, Tim Dielswrote:

  Hi all

I've just switched to python3 and it turns out my current API
documentation
generator (epydoc) no longer works. I am looking for a tool that reads
the
docstrings of all classes, ... in my project and turns it into HTML
documentation.

Sphinx (http://sphinx.pocoo.org/ ) is the new gold standard. You'll
want to enable the `autodoc` extension:
http://sphinx.pocoo.org/ext/autodoc.html

Cheers,
Chris
--
http://blog.rebertia.com

I tried, but it fails to run through python code that's not backwards
compatible with older python versions.

It fails with: ...autodoc can't import/find module 'pytilities', it reported
error: "invalid syntax (overload.py, line 55)"...

This is line 55 (runs in python3, not in python):
def process_args(self, *args, kwargs={})


As far as I'm aware, autodoc works by importing the modules and then
reading doctext straight from the __doc__ attributes.
So the module in question has to import correctly in whatever
interpreter Sphinx is using. You probably need to install Sphinx with
python3 to get it to use the right interpreter. If you're using
easy_install or pip, check if you have an easy_install-3.1 or pip-3.1
(or similar) script installed.


Sorry for the late reaction, but I still have trouble getting sphinx to 
work with python3.


I downloaded the latest version at PyPI:
http://pypi.python.org/pypi/Sphinx/1.0.4

Ran:
python3 setup.py

And it failed with a syntax error at line 50 which happens to be an 
old-styled print statement stating it requires at least py2.4.


I'd find it odd that the python3 documentation would refer to sphinx as 
a good tool if the tool wouldn't support it, so am I downloading the 
wrong package or ...?


Thanks in advance

Greetings

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


Re: Exception Handling in Python 3

2010-10-24 Thread Lie Ryan
On 10/24/10 16:01, Steve Holden wrote:
> I was somewhat surprised to discover that Python 3 no longer allows an
> exception to be raised in an except clause (or rather that it reports it
> as a separate exception that occurred during the handling of the first).

FYI, Java has a similar behavior. In Java, however, after a certain
length, some of the older exceptions will be suppressed and will only
print message informing that there are more exceptions above it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Interactive mode under DOS?

2010-10-24 Thread huisky
On Oct 24, 1:15 pm, Peter Otten <__pete...@web.de> wrote:
> huisky wrote:
> > Hi,
>
> > I'm trying to use the interactive mode under DOS for Python 2.7. As a
> > newbie, I do NOT know what is the following problem:
>
> world_is_flat=1
> if world_is_flat:
> > . . . print "be carefule to be not fall out!"
> >     File "", line 2
> >       print "be carefule to be not fall out!"
> >               ^
> > IndenatationError : expected an idented block
>
> > Enybody knows how to fix this simple issue?
>
> > regards
>
> The lines that shall be executed when world_is_flat is True must be indented
> more than the if. If you don't follow this rule Python complains and raises
> an IndentationError.
>
> >>> if world_is_flat:
>
> ... print "be careful"
>   File "", line 2
>     print "be careful"
>         ^
> IndentationError: expected an indented block
>
> The easiest way to indent in interactive mode is to hit TAB once:
>
> >>> if world_is_flat:
>
> ...     print "be careful"
> ...
> be careful
>
> By the way, you should not retype your error messages because that is error
> prone. Instead cut and paste. I think there is an entry in the system menu
> of the dos window to help you do that.
>
> Peter

Excellent reply! thanks very much Peter.
-- 
http://mail.python.org/mailman/listinfo/python-list


time difference interms of day

2010-10-24 Thread mukkera harsha
Hello
  I was wondering if there is an existing function that would let me
determine the difference in time. To explain:

Upon starting a program:

startup = time.time()

After some very long processing:
now = time.time()


On, doing now - startup I want the program to return in terms of days. How ?


Thanks,

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


Re: time difference interms of day

2010-10-24 Thread Steve Holden
On 10/24/2010 1:55 PM, mukkera harsha wrote:
> Hello 
>   I was wondering if there is an existing function that would let me 
> determine the difference in time. To explain: 
> 
> Upon starting a program: 
> 
> startup = time.time() 
> 
> After some very long processing: 
> now = time.time() 
> 
> 
> On, doing now - startup I want the program to return in terms of days. How ?
> 
> 
> Thanks,
> 
> Harsha.
> 
You'd probably be better off using the datetime module. That way you can
store datetime.datetime.now() at the start of your run and subtract
datetime.datetime.now() at the end, giving you a datetime.delta object
which contains days, seconds and microseconds:

>>> import datetime
>>> t1 = datetime.datetime.now()
[waited a while]
>>> t2 = datetime.datetime.now()
>>> t2-t1
datetime.timedelta(0, 16, 509000)
>>>

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon 2011 Atlanta March 9-17   http://us.pycon.org/
See Python Video!   http://python.mirocommunity.org/
Holden Web LLC http://www.holdenweb.com/

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


Re: time difference interms of day

2010-10-24 Thread Jonas H.

On 10/24/2010 07:55 PM, mukkera harsha wrote:

On, doing now - startup I want the program to return in terms of days. How ?


>>> import datetime
>>> now = datetime.datetime.now()
>>> after_few_seconds = datetime.datetime.now()
>>> after_few_seconds - now
datetime.timedelta(0, 14, 256614)
>>> (after_few_seconds - now).seconds
14

Hope this helps :-)

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


Re: yield all entries of an iterable

2010-10-24 Thread Stefan Schwarzer
Hi Sebastian,

On 2010-10-21 00:27, Sebastian wrote:
> Is there a simpler way to yield all elements of a sequence than this?
> for x in xs:
> yield x

Can you give an example where you would need this? Can't
you just iterate over the sequence? If you really need an
iterator, you can use `iter(sequence)`:

>>> my_list = [1, 2, 3]
>>> i = iter(my_list)
>>> i.next()
1
>>> i.next()
2
>>> i.next()
3
>>> i.next()
Traceback (most recent call last):
  File "", line 1, in 
StopIteration

This works at least with Python's built-in sequences (and
dictionaries and sets, but note that these don't have an
obvious order).

Stefan

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


linking on OSX Snow Leopard

2010-10-24 Thread john skaller
I'm not able to find the shared library version of Python3 on my Mac.
There are libpython.dylib things for Python2. There is a Python3
libpython.a static lib.


The docs on linking indicate a serious problem, there is mention
of applications containing symbols needed by dynamically loaded
extensions.

Unfortunately this idea is seriously broken (on all platforms) and should
not be used by quality products .. Python is quality product isn't it?

The correct way to do this is for the extension to be explicitly linked
against libpython so they find their symbols when loaded.

The problem with linking against an application is that it may not
be an application which is loading them .. it could be another shared
library, and in my case it is. 


in any case I find this:

~/felix>ls -lasp /Library/Frameworks/Python.framework/Versions/3.1/bin/python3.1
64 -rwxrwxr-x  1 root  admin  29560  2 Jun  2009 
/Library/Frameworks/Python.framework/Versions/3.1/bin/python3.1

and clearly a 29K byte executable must be loading a shared library somehow, but 
only 
one is listed (the dynamic loader itself).

Do I have to build Python myself to get around this deficiency?
Does the build script handle this correctly? (The standard Python
executable must be a thunk that doesn't nothing by run
a function inside a shared library). Or has something changed
in the build for Python 3, such as a collection of shared objects
instead of a monolithic libpython?


BTW: What I'm *actually* doing is: I have a compiler that generates shared 
libraries which
are run by an fixed executable driver. I want to be able to run Python from
inside my programs, but they're not "applications" they're shared libraries 
(.dylib on OSX).

In addition, my compiler can produce Python extension modules, which I want to
use from python scripts. Writing C extensions to Python is hard work for the
average user. It should be much easier to use my language to define functions
which can then be used in Python (but run millions of times faster).

The most interesting option (for me, anyhow), is to do both at the same
time: embed Python into my programs and use it to load the extension
modules.. this allows Python to handle some of the dynamics.

In particular my product's build script is entirely pure Python code
(so everything builds on all platforms including Windows).
We want to optionally replace some of the modules with
compiled extensions, which I can produce, but I also 
want to USE some of the components from the build
system inside my programs (which, recall, are shared
libraries).

--
john skaller
skal...@users.sourceforge.net




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


HTMLParser not parsing whole html file

2010-10-24 Thread josh logan
Hello,

I wanted to use python to scrub an html file for score data, but I'm
having trouble.
I'm using HTMLParser, and the parsing seems to fizzle out around line
192 or so. None of the event functions are being called anymore
(handle_starttag, handle_endtag, etc.) and I don't understand why,
because it is a html page over 1000 lines.

Could someone tell me if this is a bug or simply a misunderstanding on
how HTMLParser works? I'd really appreciate some help in
understanding.

I am using Python 3.1.2 on Windows 7 (hopefully shouldn't matter).

I put the HTML file on pastebin, because I couldn't think of anywhere
better to put it:
http://pastebin.com/wu6Pky2W

The source code has been pared down to the simplest form to exhibit
the problem. It is displayed below, and is also on pastebin for
download (http://pastebin.com/HxwRTqrr):


import sys
import re
import os.path
import itertools as it
import urllib.request
from html.parser import HTMLParser
import operator as op


base_url = 'http://www.dci.org'

class TestParser(HTMLParser):

def handle_starttag(self, tag, attrs):
print('position {}, staring tag {} with attrs
{}'.format(self.getpos(), tag, attrs))

def handle_endtag(self, tag):
print('ending tag {}'.format(tag))


def do_parsing_from_file_stream(fname):
parser = TestParser()

with open(fname) as f:
for num, line in enumerate(f, start=1):
# print('Sending line {} through parser'.format(num))
parser.feed(line)



if __name__ == '__main__':
do_parsing_from_file_stream(sys.argv[1])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: HTMLParser not parsing whole html file

2010-10-24 Thread josh logan
On Oct 24, 4:36 pm, josh logan  wrote:
> Hello,
>
> I wanted to use python to scrub an html file for score data, but I'm
> having trouble.
> I'm using HTMLParser, and the parsing seems to fizzle out around line
> 192 or so. None of the event functions are being called anymore
> (handle_starttag, handle_endtag, etc.) and I don't understand why,
> because it is a html page over 1000 lines.
>
> Could someone tell me if this is a bug or simply a misunderstanding on
> how HTMLParser works? I'd really appreciate some help in
> understanding.
>
> I am using Python 3.1.2 on Windows 7 (hopefully shouldn't matter).
>
> I put the HTML file on pastebin, because I couldn't think of anywhere
> better to put it:http://pastebin.com/wu6Pky2W
>
> The source code has been pared down to the simplest form to exhibit
> the problem. It is displayed below, and is also on pastebin for
> download (http://pastebin.com/HxwRTqrr):
>
> import sys
> import re
> import os.path
> import itertools as it
> import urllib.request
> from html.parser import HTMLParser
> import operator as op
>
> base_url = 'http://www.dci.org'
>
> class TestParser(HTMLParser):
>
>     def handle_starttag(self, tag, attrs):
>         print('position {}, staring tag {} with attrs
> {}'.format(self.getpos(), tag, attrs))
>
>     def handle_endtag(self, tag):
>         print('ending tag {}'.format(tag))
>
> def do_parsing_from_file_stream(fname):
>     parser = TestParser()
>
>     with open(fname) as f:
>         for num, line in enumerate(f, start=1):
>             # print('Sending line {} through parser'.format(num))
>             parser.feed(line)
>
> if __name__ == '__main__':
>     do_parsing_from_file_stream(sys.argv[1])

Sorry, the group doesn't like how i surrounded the Python code's
pastebin URL with parentheses:

http://pastebin.com/HxwRTqrr
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Exception Handling in Python 3

2010-10-24 Thread John Nagle

On 10/23/2010 10:42 PM, Steve Holden wrote:

On 10/24/2010 1:26 AM, Chris Rebert wrote:

I was somewhat surprised to discover that Python 3 no longer
allows an

exception to be raised in an except clause (or rather that it
reports it as a separate exception that occurred during the
handling of the first).



[snip]

What is the correct paradigm for this situation?

There doesn't seem to be one at the moment, although the issue
isn't very serious. Your Traceback is merely being made slightly
longer/more complicated than you'd prefer; however, conversely,
what if a bug was to be introduced into your exception handler?
Then you'd likely very much appreciate the "superfluous" Traceback
info.

Your quandary is due to the unresolved status of the "Open Issue:
Suppressing Context" in PEP 3141
(http://www.python.org/dev/peps/pep-3134/ ). I guess you could
start a discussion about closing that issue somehow.




   This is a traceback issue only, right?  The semantics of
the code below shouldn't change in Py3.x, I hope:

   try :
   ...
   try :
  x = 1/0 # in a real program, input data might cause a problem
   except ZeroDivisionError as msg:
  raise RuntimeError("Math error on problem: " + str(msg))
   except RuntimeError as msg :
   print("Trouble: " + str(msg))

I have code where I'm reading and parsing a web page,
a process which can produce a wide range of errors. A try-block
around the read and parse catches the various errors and creates a
single user-defined "bad web page" exception object, which is then
raised.   That gets caught further out, and is used to record the
troubled web page, schedule it for a retest, and such.  This is
normal program operation, indicative of external problems, not a
code error or cause for program termination with a traceback.

Are exception semantics changing in a way which would affect that?

John Nagle

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


Re: linking on OSX Snow Leopard

2010-10-24 Thread Benjamin Kaplan
On Sun, Oct 24, 2010 at 4:15 PM, john skaller
 wrote:
> I'm not able to find the shared library version of Python3 on my Mac.
> There are libpython.dylib things for Python2. There is a Python3
> libpython.a static lib.
>
> 
> The docs on linking indicate a serious problem, there is mention
> of applications containing symbols needed by dynamically loaded
> extensions.
>
> Unfortunately this idea is seriously broken (on all platforms) and should
> not be used by quality products .. Python is quality product isn't it?
>
> The correct way to do this is for the extension to be explicitly linked
> against libpython so they find their symbols when loaded.
>
> The problem with linking against an application is that it may not
> be an application which is loading them .. it could be another shared
> library, and in my case it is.
> 
>
> in any case I find this:
>
> ~/felix>ls -lasp 
> /Library/Frameworks/Python.framework/Versions/3.1/bin/python3.1
> 64 -rwxrwxr-x  1 root  admin  29560  2 Jun  2009 
> /Library/Frameworks/Python.framework/Versions/3.1/bin/python3.1
>
> and clearly a 29K byte executable must be loading a shared library somehow, 
> but only
> one is listed (the dynamic loader itself).
>
> Do I have to build Python myself to get around this deficiency?
> Does the build script handle this correctly? (The standard Python
> executable must be a thunk that doesn't nothing by run
> a function inside a shared library). Or has something changed
> in the build for Python 3, such as a collection of shared objects
> instead of a monolithic libpython?
>
>

You're using a Mac OS X Framework. The structure of the frameworks is
very specific. I believe that
/Library/Frameworks/Python.framework/Versions/3.1/Python will be your
shared library.

If you want your program to behave appropriately on OS X, you may want
to learn to use the Frameworks rather than just assuming you're on a
standard Unix system.
http://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPFrameworks/Frameworks.html

> BTW: What I'm *actually* doing is: I have a compiler that generates shared 
> libraries which
> are run by an fixed executable driver. I want to be able to run Python from
> inside my programs, but they're not "applications" they're shared libraries 
> (.dylib on OSX).
>
> In addition, my compiler can produce Python extension modules, which I want to
> use from python scripts. Writing C extensions to Python is hard work for the
> average user. It should be much easier to use my language to define functions
> which can then be used in Python (but run millions of times faster).
>
> The most interesting option (for me, anyhow), is to do both at the same
> time: embed Python into my programs and use it to load the extension
> modules.. this allows Python to handle some of the dynamics.
>
> In particular my product's build script is entirely pure Python code
> (so everything builds on all platforms including Windows).
> We want to optionally replace some of the modules with
> compiled extensions, which I can produce, but I also
> want to USE some of the components from the build
> system inside my programs (which, recall, are shared
> libraries).
>
> --
> john skaller
> skal...@users.sourceforge.net
>
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie to python

2010-10-24 Thread rantingrick
On Oct 24, 7:31 am, Steve Holden  wrote:
> On 10/24/2010 3:29 AM, Hrishikesh wrote:
>
> > I want to start python, I have just downloaded python compiler, Can
> > somebody please tell me what python really is and explain its
> > architecture, in what problems it is used and from where should I
> > start?
>
> If you don't know the answers to *any* of those questions, one wonders
> why you "want to start Python". Perhaps you can explain, or is this
> simply trolling?

Steve, I highly doubt this is an example of trolling we have witnessed
here. If it really *is* trolling then i would say the "alleged
troller" should visit xah's site for a crash course in professional
trolling ASAP because he's not very good at it -- i should know!

However, i have a good sense that our new friend is just very
inexperienced and looking for advice. He may have a very limited
knowledge --or even no knowledge of computer programming-- so that
would explain the "seemingly" nonsensicalness of his questions. But we
must always remember that every one here (*ahem* and i mean everyone S-
T-E-V-E-O!) had to start from the bottom and work his or her way up
the ladder of knowledge. So next time please try to be a little less
condescending.

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


Re: Exception Handling in Python 3

2010-10-24 Thread Steve Holden
On 10/24/2010 4:44 PM, John Nagle wrote:
> Are exception semantics changing in a way which would affect that?

No, I don't believe so. I simply felt that the traceback gives too much
information in the case where an exception is specifically being raised
to replace the one currently being handled.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon 2011 Atlanta March 9-17   http://us.pycon.org/
See Python Video!   http://python.mirocommunity.org/
Holden Web LLC http://www.holdenweb.com/

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


Re: newbie to python

2010-10-24 Thread Steve Holden
On 10/24/2010 5:19 PM, rantingrick wrote:
> On Oct 24, 7:31 am, Steve Holden  wrote:
>> On 10/24/2010 3:29 AM, Hrishikesh wrote:
>>
>>> I want to start python, I have just downloaded python compiler, Can
>>> somebody please tell me what python really is and explain its
>>> architecture, in what problems it is used and from where should I
>>> start?
>>
>> If you don't know the answers to *any* of those questions, one wonders
>> why you "want to start Python". Perhaps you can explain, or is this
>> simply trolling?
> 
> Steve, I highly doubt this is an example of trolling we have witnessed
> here. If it really *is* trolling then i would say the "alleged
> troller" should visit xah's site for a crash course in professional
> trolling ASAP because he's not very good at it -- i should know!
> 
> However, i have a good sense that our new friend is just very
> inexperienced and looking for advice. He may have a very limited
> knowledge --or even no knowledge of computer programming-- so that
> would explain the "seemingly" nonsensicalness of his questions. But we
> must always remember that every one here (*ahem* and i mean everyone S-
> T-E-V-E-O!) had to start from the bottom and work his or her way up
> the ladder of knowledge. So next time please try to be a little less
> condescending.
> 
Righty ho.

regards
 s-t-e-v-e-o
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon 2011 Atlanta March 9-17   http://us.pycon.org/
See Python Video!   http://python.mirocommunity.org/
Holden Web LLC http://www.holdenweb.com/

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


How do I chain methods?

2010-10-24 Thread chad
I tried the following...



#!/usr/bin/python

class foo:
def first(self):
print "Chad "

def last(self):
print "A "

x = foo()
y = x.first()
y.last()

But when I ran it, I got the following...

[cdal...@localhost oakland]$ ./chain.py
Chad
Traceback (most recent call last):
  File "./chain.py", line 12, in ?
y.last()
AttributeError: 'NoneType' object has no attribute 'last'
[cdal...@localhost oakland]$
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I chain methods?

2010-10-24 Thread Chris Rebert
On Sun, Oct 24, 2010 at 3:47 PM, chad  wrote:
> How do I chain methods?
> I tried the following...
>
> #!/usr/bin/python
>
> class foo:
>    def first(self):
>        print "Chad "
>
>    def last(self):
>        print "A "
>
> x = foo()
> y = x.first()
> y.last()
>
> But when I ran it, I got the following...
>
> [cdal...@localhost oakland]$ ./chain.py
> Chad
> Traceback (most recent call last):
>  File "./chain.py", line 12, in ?
>    y.last()
> AttributeError: 'NoneType' object has no attribute 'last'
> [cdal...@localhost oakland]$

Functions/methods without "return" statements, such as your last() and
first(), implicitly return None, Python's equivalent of null. Python
has no special support for method chaining; having your methods
`return self` achieves the same effect however.

Method chaining is usually* not idiomatic in Python. Most people would
instead just write:
x = foo()
x.first()
x.last()

If you insist on method chaining, adding the aforementioned `return`
statements would let you write:
x = foo()
x.first().last()

Cheers,
Chris
--
*Notwithstanding magic SQL query builders and the like
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: yield all entries of an iterable

2010-10-24 Thread Cameron Simpson
On 24Oct2010 20:58, Stefan Schwarzer  wrote:
| On 2010-10-21 00:27, Sebastian wrote:
| > Is there a simpler way to yield all elements of a sequence than this?
| > for x in xs:
| > yield x
| 
| Can you give an example where you would need this? Can't
| you just iterate over the sequence?

The usual example is when the sequence comes from inside.
Example, closely resembling some code from on of my projects:

  def leaves(N):
if N.isleaf:
  yield N
for subN in N.subnodes:
  for leaf in leaves(subN):
yield leaf

which walks a tree structure returning leaf nodes.

The point is that you're calling leaves() on the subnode and yiled them
directly to the outside. The caller may not even know there are "subnodes".

Cheers,
-- 
Cameron Simpson  DoD#743
http://www.cskk.ezoshosting.com/cs/

The Borg assimilated my race and all I got was this lousy tagline.
- Cath Lawrence 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I chain methods?

2010-10-24 Thread James Mills
On Mon, Oct 25, 2010 at 8:47 AM, chad  wrote:
> I tried the following...
>
>
>
> #!/usr/bin/python
>
> class foo:
>    def first(self):
>        print "Chad "
>
>    def last(self):
>        print "A "
>
> x = foo()
> y = x.first()
> y.last()
>
> But when I ran it, I got the following...
>
> [cdal...@localhost oakland]$ ./chain.py
> Chad
> Traceback (most recent call last):
>  File "./chain.py", line 12, in ?
>    y.last()
> AttributeError: 'NoneType' object has no attribute 'last'
> [cdal...@localhost oakland]$

What you want is this:

>>> class Foo(object):
... def first(self):
... print "Chad "
... return self
... def last(self):
... print "A "
... return self
...
>>> x = Foo()
>>> y = x.first()
Chad
>>> y.last()
A
<__main__.Foo object at 0x1011d7ad0>
>>>

NB: You must return "self" in this case so you can do chained calls.

cheers
James

-- 
-- James Mills
--
-- "Problems are solved by method"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I chain methods?

2010-10-24 Thread James Mills
On Mon, Oct 25, 2010 at 9:02 AM, Chris Rebert  wrote:
> Method chaining is usually* not idiomatic in Python.

I don't agree but anyway... I've just not seen it commonly used
amongst python programmers.

cheers
James

-- 
-- James Mills
--
-- "Problems are solved by method"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I chain methods?

2010-10-24 Thread chad
On Oct 24, 4:11 pm, James Mills  wrote:
> On Mon, Oct 25, 2010 at 9:02 AM, Chris Rebert  wrote:
> > Method chaining is usually* not idiomatic in Python.
>
> I don't agree but anyway... I've just not seen it commonly used
> amongst python programmers.
>
> cheers
> James
>

I just saw this technique used in python script that was/is used to
automatically log them in myspace.com. Hence the question.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: yield all entries of an iterable

2010-10-24 Thread Stefan Schwarzer
Hi Cameron,

On 2010-10-25 01:08, Cameron Simpson wrote:
> On 24Oct2010 20:58, Stefan Schwarzer  wrote:
> | On 2010-10-21 00:27, Sebastian wrote:
> | > Is there a simpler way to yield all elements of a sequence than this?
> | > for x in xs:
> | > yield x
> | 
> | Can you give an example where you would need this? Can't
> | you just iterate over the sequence?
> 
> The usual example is when the sequence comes from inside.
> Example, closely resembling some code from on of my projects:
> 
>   def leaves(N):
> if N.isleaf:
>   yield N
> for subN in N.subnodes:
>   for leaf in leaves(subN):
> yield leaf
> 
> which walks a tree structure returning leaf nodes.
> 
> The point is that you're calling leaves() on the subnode and yiled them
> directly to the outside. The caller may not even know there are "subnodes".

>From the question and the code snippet the OP gave I assumed
he meant that there already was a sequence (i. e. linear
structure) to begin with.

By the way, I think a well-known example of what you
describe is `os.walk`.

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


Re: How do I chain methods?

2010-10-24 Thread James Mills
On Mon, Oct 25, 2010 at 9:21 AM, chad  wrote:
> I just saw this technique used in python script that was/is used to
> automatically log them in myspace.com. Hence the question.

Function/Method Chaining is probably used a lot in Python itself:

>>> x = 4
>>> x.__add__(1).__sub__(3)
2

The implementation of many common operators return self (the object
you're working with).

cheers
James

-- 
-- James Mills
--
-- "Problems are solved by method"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Exception Handling in Python 3

2010-10-24 Thread Ben Finney
Steve Holden  writes:

> I simply felt that the traceback gives too much information in the
> case where an exception is specifically being raised to replace the
> one currently being handled.

Ideally, that description of the problem would suggest the obvious
solution: replace the class of the exception and allow the object to
continue up the exception handler stack.

But that doesn't work either::

>>> d = {}
>>> try:
... val = d['nosuch']
... except KeyError as exc:
... exc.__class__ = AttributeError
... raise exc
...
Traceback (most recent call last):
  File "", line 2, in 
KeyError: 'nosuch'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "", line 4, in 
TypeError: __class__ assignment: only for heap types

which means, AFAICT, that re-binding ‘__class__’ is only allowed for
objects of a type defined in the Python run-time heap, not those defined
in C code (like the built-in-exception types).

-- 
 \ “I wish there was a knob on the TV to turn up the intelligence. |
  `\  There's a knob called ‘brightness’ but it doesn't work.” |
_o__) —Eugene P. Gallagher |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I chain methods?

2010-10-24 Thread Steve Holden
On 10/24/2010 7:39 PM, James Mills wrote:
> On Mon, Oct 25, 2010 at 9:21 AM, chad  wrote:
>> I just saw this technique used in python script that was/is used to
>> automatically log them in myspace.com. Hence the question.
> 
> Function/Method Chaining is probably used a lot in Python itself:
> 
 x = 4
 x.__add__(1).__sub__(3)
> 2
> 
> The implementation of many common operators return self (the object
> you're working with).
> 
You surely aren't trying to suggest that (4).__add__(1) returns 4?

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon 2011 Atlanta March 9-17   http://us.pycon.org/
See Python Video!   http://python.mirocommunity.org/
Holden Web LLC http://www.holdenweb.com/

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


Re: pythagorean triples exercise

2010-10-24 Thread BartC


"Tim Roberts"  wrote in message
news:5na7c6dlv0qii3pta58as50lmjcrrtk...@4ax.com...

Baba  wrote:



a^a + b^b = c^c is the condition to satisfy


No, it's not.  It's a^2 + b^2 = c^2, where a, b, and c are integers.
Perhaps you meant a*a + b*b = c*c.


Or possibly a**2 + b**2 = c**2


and i need to use loops
and "n" will be an upper limit of one (or more?) of the loops but i am
a bit lost. Please help me get thinking about this right.


The simplest (but not most efficient) method is brute force, using three
loops, one each for a, b, and c.  You can compute the largest "c" you will
need by computing the square root of a*a+b*b.


If square roots have to be used, you might as well use the two-loop
algorithm, as you're nearly there.

A simpler estimate for the largest c is just a+b, although it might involve 
a few extra iterations.


--
Bartc 


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


Re: yield all entries of an iterable

2010-10-24 Thread Cameron Simpson
On 25Oct2010 01:37, Stefan Schwarzer  wrote:
| From the question and the code snippet the OP gave I assumed
| he meant that there already was a sequence (i. e. linear
| structure) to begin with.

I suspected that was your interpretation.

| By the way, I think a well-known example of what you
| describe is `os.walk`.

Indeed. I'm surprised it didn't occur to me.

Cheers,
-- 
Cameron Simpson  DoD#743
http://www.cskk.ezoshosting.com/cs/

The only thing necessary for the triumph of evil is for good men to do nothing.
- Edmund Burke (1729-1797)
-- 
http://mail.python.org/mailman/listinfo/python-list


How to optimize and monitor garbage collection?

2010-10-24 Thread kj



I'm designing a system that will be very memory hungry unless it
is "garbage-collected" very aggressively.

In the past I have had disappointing results with the gc module:
I noticed practically no difference in memory usage with and without
it.  It is possible, however, that I was not measuring memory
consumption adequately.

What's the most accurate way to monitor memory consumption in a
Python program, and thereby ensure that gc is working properly?

Also, are there programming techniques that will result in better 
garbage collection?  For example, would it help to explicitly call
del on objects that should be gc'd?

TIA!

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


Re: A Unique XML Parsing Problem

2010-10-24 Thread Lawrence D'Oliveiro
In message , Piet van Oostrum 
wrote:

> With xsltproc song.xsl song*.xml you would get your output.
> No python necessary.

Is that supposed to be some kind of advantage?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unix-head needs to Windows-ize his Python script (II)

2010-10-24 Thread Lawrence D'Oliveiro
In message , Dave Angel 
wrote:

> On 2:59 PM, Lawrence D'Oliveiro wrote:
>>
>> In message
>> , Dave Angel wrote:
>>
>>> Presumably the original pythonw.exe was called that because it's marked
>>> as a windows-app. In win-speak, that means it has a gui. Applications
>>> that are not so-marked are console-apps, and get a console created if
>>> they weren't already started from one. That's where stdin/stdout/stderr
>>> get pointed.
>>
>> Which is completely backwards, isn’t it?
>>
> No. GUI programs are marked as win-app, so w stands for "Windows". Non
> GUI programs run in the console.

You mean “GUI console”. So non-GUI apps get a GUI element whether they want 
it or not, while GUI ones don’t. That’s completely backwards.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Exception Handling in Python 3

2010-10-24 Thread Lawrence D'Oliveiro
In message , Steve 
Holden wrote:

> Yes, *if the exception is caught* then it doesn't make any difference.
> If the exception creates a traceback, however, I maintain that the
> additional information is confusing to the consumer (while helpful to
> the debugger of the consumed code).

Who needs the information more?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unix-head needs to Windows-ize his Python script (II)

2010-10-24 Thread MRAB

On 25/10/2010 02:19, Lawrence D'Oliveiro wrote:

In message, Dave Angel
wrote:


On 2:59 PM, Lawrence D'Oliveiro wrote:


In message
, Dave Angel wrote:


Presumably the original pythonw.exe was called that because it's marked
as a windows-app. In win-speak, that means it has a gui. Applications
that are not so-marked are console-apps, and get a console created if
they weren't already started from one. That's where stdin/stdout/stderr
get pointed.


Which is completely backwards, isn’t it?


No. GUI programs are marked as win-app, so w stands for "Windows". Non
GUI programs run in the console.


You mean “GUI console”. So non-GUI apps get a GUI element whether they want
it or not, while GUI ones don’t. That’s completely backwards.


No, it's not. The fact that the console is also a GUI window is an
implementation detail: it happens to be displayed within a GUI
environment.
--
http://mail.python.org/mailman/listinfo/python-list


Re: HTMLParser not parsing whole html file

2010-10-24 Thread josh logan
On Oct 24, 4:38 pm, josh logan  wrote:
> On Oct 24, 4:36 pm, josh logan  wrote:
>
>
>
>
>
> > Hello,
>
> > I wanted to use python to scrub an html file for score data, but I'm
> > having trouble.
> > I'm using HTMLParser, and the parsing seems to fizzle out around line
> > 192 or so. None of the event functions are being called anymore
> > (handle_starttag, handle_endtag, etc.) and I don't understand why,
> > because it is a html page over 1000 lines.
>
> > Could someone tell me if this is a bug or simply a misunderstanding on
> > how HTMLParser works? I'd really appreciate some help in
> > understanding.
>
> > I am using Python 3.1.2 on Windows 7 (hopefully shouldn't matter).
>
> > I put the HTML file on pastebin, because I couldn't think of anywhere
> > better to put it:http://pastebin.com/wu6Pky2W
>
> > The source code has been pared down to the simplest form to exhibit
> > the problem. It is displayed below, and is also on pastebin for
> > download (http://pastebin.com/HxwRTqrr):
>
> > import sys
> > import re
> > import os.path
> > import itertools as it
> > import urllib.request
> > from html.parser import HTMLParser
> > import operator as op
>
> > base_url = 'http://www.dci.org'
>
> > class TestParser(HTMLParser):
>
> >     def handle_starttag(self, tag, attrs):
> >         print('position {}, staring tag {} with attrs
> > {}'.format(self.getpos(), tag, attrs))
>
> >     def handle_endtag(self, tag):
> >         print('ending tag {}'.format(tag))
>
> > def do_parsing_from_file_stream(fname):
> >     parser = TestParser()
>
> >     with open(fname) as f:
> >         for num, line in enumerate(f, start=1):
> >             # print('Sending line {} through parser'.format(num))
> >             parser.feed(line)
>
> > if __name__ == '__main__':
> >     do_parsing_from_file_stream(sys.argv[1])
>
> Sorry, the group doesn't like how i surrounded the Python code's
> pastebin URL with parentheses:
>
> http://pastebin.com/HxwRTqrr

I found the error. The HTML file I'm parsing has invalid HTML at line
193.
It has something like:



Note there is no space between the closing quote for the "href" tag
and the class attribute. I guess I'll go through each file and correct
these issues as I parse them.

Thanks for reading, anyways.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I chain methods?

2010-10-24 Thread Steven D'Aprano
On Mon, 25 Oct 2010 09:39:47 +1000, James Mills wrote:

> On Mon, Oct 25, 2010 at 9:21 AM, chad  wrote:
>> I just saw this technique used in python script that was/is used to
>> automatically log them in myspace.com. Hence the question.
> 
> Function/Method Chaining is probably used a lot in Python itself:
> 
 x = 4
 x.__add__(1).__sub__(3)
> 2
> 
> The implementation of many common operators return self (the object
> you're working with).


I can't think of any operations on built-ins that return self, except in 
the special case of an identity operation. And even then, it's not common:

>>> x = 2.5
>>> y = x.__add__(1)
>>> y is x
False
>>> y = x.__add__(0)
>>> y is x
False


Ah wait, no, I thought of one: __iadd__:

>>> x = [2.5]
>>> y = x.__iadd__([None])
>>> y is x
True

But:

>>> x = 2.5
>>> y = x.__iadd__(1)
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'float' object has no attribute '__iadd__'



By the way, in case any newbies out there are reading... 

...if you're writing x.__add__(1).__sub__(3) instead of x + 1 - 3 then 
you're almost certainly doing it wrong.



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


Re: How do I chain methods?

2010-10-24 Thread James Mills
On Mon, Oct 25, 2010 at 12:49 PM, Steven D'Aprano
 wrote:
> ...if you're writing x.__add__(1).__sub__(3) instead of x + 1 - 3 then
> you're almost certainly doing it wrong.

It was just an example :) ... And this isn't python-tutor

--James

-- 
-- James Mills
--
-- "Problems are solved by method"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I chain methods?

2010-10-24 Thread James Mills
On Mon, Oct 25, 2010 at 9:39 AM, James Mills
 wrote:
> Function/Method Chaining is probably used a lot in Python itself:
>
 x = 4
 x.__add__(1).__sub__(3)
> 2
>
> The implementation of many common operators return self (the object
> you're working with).

My apologies, this was a terribly example! int is immutable and
__add__ nor __sub__ return self :)

cheers
James

-- 
-- James Mills
--
-- "Problems are solved by method"
-- 
http://mail.python.org/mailman/listinfo/python-list


[ANN]littletable 0.3 release

2010-10-24 Thread Paul McGuire
Announcing the 0.3 release of littletable (the module formerly known
as dulce).  The version includes (thanks to much help from Colin
McPhail, thanks Colin!):
- support for namedtuples as table objects
- Python 3 compatibility
- Table.pivot() to summarize record counts by 1 or 2 table attributes

littletable (formerly dulce) is a simple ORM-like wrapper for managing
collections of Python objects like relational tables.  No schema
definition is used; instead table columns are introspected from the
attributes of objects inserted into the table, and inferred from index
and query parameters.  Tables can be:
- indexed
- queried
- joined
- pivoted
- imported from/exported to .CSV files

Also, every query or join returns a new full-fledged littletable Table
- no distinction of Tables vs. DataSets vs. RecordSets vs. whatever.
So it is easy to build up a complex database analysis from a
succession of joins and queries.

littletable is a simple environment for experimenting with tables,
joins, and indexing, with a minimum of startup overhead.  You can
download littletable at http://sourceforge.net/projects/littletable/ -
htmldocs can be viewed at http://packages.python.org/littletable/.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I chain methods?

2010-10-24 Thread Chris Rebert
On Sun, Oct 24, 2010 at 4:11 PM, James Mills
 wrote:
> On Mon, Oct 25, 2010 at 9:02 AM, Chris Rebert  wrote:
>> Method chaining is usually* not idiomatic in Python.
>
> I don't agree but anyway... I've just not seen it commonly used
> amongst python programmers.

If Python wanted to encourage method-chaining-style, then list.sort(),
list.reverse(), and several other built-in type methods would (ala
Ruby) return self rather than None. Since they don't, and since
"uncommon idiom" is a near-oxymoron, I think we can safely conclude
that method chaining isn't idiomatic in Python. Not that it doesn't
have specialized uses though (See asterisk note).

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Exception Handling in Python 3

2010-10-24 Thread Steve Holden
On 10/24/2010 7:51 PM, Ben Finney wrote:
> which means, AFAICT, that re-binding ‘__class__’ is only allowed for
> objects of a type defined in the Python run-time heap, not those defined
> in C code (like the built-in-exception types).

Yeah, that's a given. Ruby would probably let you do that, but Python
insists that you don't dick around with the built-in types. And roghtly
so, IMHO.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon 2011 Atlanta March 9-17   http://us.pycon.org/
See Python Video!   http://python.mirocommunity.org/
Holden Web LLC http://www.holdenweb.com/

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


Re: How to optimize and monitor garbage collection?

2010-10-24 Thread Steve Holden
On 10/24/2010 8:39 PM, kj wrote:
> What's the most accurate way to monitor memory consumption in a
> Python program, and thereby ensure that gc is working properly?

Trust me, it is. But don't forget that CPython doesn't actually *use*
the garbage collector until you start to create cyclic data structures.
In their absence reference counting alone is sufficient to ensure that
unused object memory is reclaimed.

regards
 Steve

[aside: in general, if you think your program is not working because of
a bug in Python, look harder at your program].
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon 2011 Atlanta March 9-17   http://us.pycon.org/
See Python Video!   http://python.mirocommunity.org/
Holden Web LLC http://www.holdenweb.com/

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


Re: How do I chain methods?

2010-10-24 Thread Steve Holden
On 10/24/2010 11:42 PM, Chris Rebert wrote:
> On Sun, Oct 24, 2010 at 4:11 PM, James Mills
>  wrote:
>> On Mon, Oct 25, 2010 at 9:02 AM, Chris Rebert  wrote:
>>> Method chaining is usually* not idiomatic in Python.
>>
>> I don't agree but anyway... I've just not seen it commonly used
>> amongst python programmers.
> 
> If Python wanted to encourage method-chaining-style, then list.sort(),
> list.reverse(), and several other built-in type methods would (ala
> Ruby) return self rather than None. Since they don't, and since
> "uncommon idiom" is a near-oxymoron, I think we can safely conclude
> that method chaining isn't idiomatic in Python. Not that it doesn't
> have specialized uses though (See asterisk note).
> 
Yes, the Twisted guys use method chaining a lot - it's definitely
idiomatic in that framework.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon 2011 Atlanta March 9-17   http://us.pycon.org/
See Python Video!   http://python.mirocommunity.org/
Holden Web LLC http://www.holdenweb.com/

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


Re: Unix-head needs to Windows-ize his Python script (II)

2010-10-24 Thread Steve Holden
On 10/24/2010 9:40 PM, MRAB wrote:
> On 25/10/2010 02:19, Lawrence D'Oliveiro wrote:
>> In message, Dave
>> Angel
>> wrote:
>>
>>> On 2:59 PM, Lawrence D'Oliveiro wrote:

 In message
 , Dave Angel wrote:

> Presumably the original pythonw.exe was called that because it's
> marked
> as a windows-app. In win-speak, that means it has a gui. Applications
> that are not so-marked are console-apps, and get a console created if
> they weren't already started from one. That's where
> stdin/stdout/stderr
> get pointed.

 Which is completely backwards, isn’t it?

>>> No. GUI programs are marked as win-app, so w stands for "Windows". Non
>>> GUI programs run in the console.
>>
>> You mean “GUI console”. So non-GUI apps get a GUI element whether they
>> want
>> it or not, while GUI ones don’t. That’s completely backwards.
> 
> No, it's not. The fact that the console is also a GUI window is an
> implementation detail: it happens to be displayed within a GUI
> environment.

and, in fact, the console is only a GUI window in a windowed system. It
might be one of the console emulation windows that init starts under
linux, or even a terminal connected to a computer by a serila line, for
heavens sake.

Let's not go overboard looking for things to disagree about ;-)

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon 2011 Atlanta March 9-17   http://us.pycon.org/
See Python Video!   http://python.mirocommunity.org/
Holden Web LLC http://www.holdenweb.com/

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


Re: Unicode questions

2010-10-24 Thread Lawrence D'Oliveiro
In message , Petite 
Abeille wrote:

> Characters vs. Bytes

And why do certain people insist on referring to bytes as “octets”?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unicode questions

2010-10-24 Thread Lawrence D'Oliveiro
In message , Chris Rebert 
wrote:

> There is no such thing as "plain Unicode representation".

UCS-4 or UTF-16 probably come the closest.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unicode questions

2010-10-24 Thread Chris Rebert
On Sun, Oct 24, 2010 at 10:43 PM, Lawrence D'Oliveiro
 wrote:
> In message , Chris Rebert
> wrote:
>
>> There is no such thing as "plain Unicode representation".
>
> UCS-4 or UTF-16 probably come the closest.

How do you figure that?

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Want / Need Genuine Google Adsense Account for Rs.300/-

2010-10-24 Thread sathiya moorthi
Google Adsense Account For Cheap rate : p.sathi...@gmail.com

> On Wednesday, March 31, 2010 4:18 PM SWETHA wrote:

> Genuine Google Adsense Account for Rs.300/-
> 
> We will approve your google adsense approval.
> 
> We will help you till you get the first adsense check.
> 
> Please send your mobile number to pramesh0...@gmail.com , i will call
> you with in 15 minutes and give you the google adsense account.
> 
> High Price Reduction for multiple adsense accounts.


> Submitted via EggHeadCafe - Software Developer Portal of Choice 
> ASP.NET 4.0 browser capabilities
> http://www.eggheadcafe.com/tutorials/aspnet/668c15e2-9fe5-4a7a-94ac-001af0bf4d1b/aspnet-40-browser-capabilities.aspx
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unicode questions

2010-10-24 Thread Steve Holden
On 10/25/2010 1:42 AM, Lawrence D'Oliveiro wrote:
> In message , Petite 
> Abeille wrote:
> 
>> Characters vs. Bytes
> 
> And why do certain people insist on referring to bytes as “octets”?

Because back in the old days bytes were of varying sizes on different
architectures - indeed the DECSystem-10 and -20 had instructions that
could be parameterized as to byte size. So octet was an unambiguous term
for the (now standard) 8-bit byte.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon 2011 Atlanta March 9-17   http://us.pycon.org/
See Python Video!   http://python.mirocommunity.org/
Holden Web LLC http://www.holdenweb.com/

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


Re: HTMLParser not parsing whole html file

2010-10-24 Thread Stefan Behnel

josh logan, 25.10.2010 04:14:

I found the error. The HTML file I'm parsing has invalid HTML at line
193. It has something like:



Note there is no space between the closing quote for the "href" tag
and the class attribute. I guess I'll go through each file and correct
these issues as I parse them.


HTMLparser is not made to deal with non-HTML input. You can take a look at 
lxml.html or BeautifulSoup (up to 3.0), which handle these problems a lot 
better.


Stefan

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