Failing on string exceptions in 2.4

2007-06-14 Thread Stephen R Laniel
Reading the Python docs, it looks like string exceptions
will be a DeprecationWarning in Python 2.5. Is there any way
to make them so in 2.4? Now how about if I want to turn all
DeprecationWarnings into compile-time errors? Is there some
way to do this?

End goal being that string exceptions would cause
compilation to fail. A few times now, I've found myself
doing

class SomeClass:
"""docstring"""
pass

raise SomeClass, "Some description"

and I've gotten a weird compiler error about the constructor
for SomeClass. I'd prefer it just to fail there and not let
me raise an exception that isn't subclassed beneath
Exception.

-- 
Stephen R. Laniel
[EMAIL PROTECTED]
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Html parser

2007-06-15 Thread Stephen R Laniel
On Fri, Jun 15, 2007 at 07:11:56AM -0700, HMS Surprise wrote:
> Could you recommend  an html parser that works with python (jython
> 2.2)?

I'm new here, but I believe BeautifulSoup is the canonical
answer:
http://www.crummy.com/software/BeautifulSoup/

-- 
Stephen R. Laniel
[EMAIL PROTECTED]
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-- 
http://mail.python.org/mailman/listinfo/python-list


PEP 3107 and stronger typing (note: probably a newbie question)

2007-06-20 Thread Stephen R Laniel
Before I ask anything, let me note that this is surely an
old question that has inspired its share of flame wars; I'm
new to Python, but not new to how Internet discussions work.
So if there's a canonical thread or web page that documents
the whole battle, feel free to point me to it.

Reading [1], I wonder: why isn't the compiler making better
use of (purely optional) type labeling? Why not make a compiler
directive so that

a) it will check the types of all my arguments and return
   values, and maybe even
b) do some type inference up the call stack?

E.g.,

def( Class1 arg1, Class2 arg2, ..., ClassN argN ):
someStuff()

would check the types of the arguments, whereas

def( arg1, arg2, ..., argN):
someStuff()

would not? I.e., if I *want* strong static
type-checking, why shouldn't I be able to get it? Is it that
allowing this as a compile-time option would mess up too
many knobs to make it optional?

Again, probably an old debate. I'd like to know why Guido's
decided that not only is strong static typing
productivity-reducing [2], but that it should be *forbidden*.

[1] - http://www.python.org/dev/peps/pep-3107/
[2] - http://www.artima.com/intv/strongweakP.html

-- 
Stephen R. Laniel
[EMAIL PROTECTED]
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PEP 3107 and stronger typing (note: probably a newbie question)

2007-06-20 Thread Stephen R Laniel
On Wed, Jun 20, 2007 at 12:59:28PM -0700, [EMAIL PROTECTED] wrote:
> Then you should use another language.

This is what I meant about knowing how Internet discussions
go.

-- 
Stephen R. Laniel
[EMAIL PROTECTED]
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PEP 3107 and stronger typing (note: probably a newbie question)

2007-06-20 Thread Stephen R Laniel
On Wed, Jun 20, 2007 at 09:41:09PM +0100, Michael Hoffman wrote:
> If you asked Java programmers why you couldn't turn *off* Java's static 
> type checking if you wanted to, you'd probably get a similar response.

Perhaps it would help for me to explain what I'd like.

Under both Perl and Python, I've found myself
having/wanting to write things like so:

def my_func( int_arg, str_arg ):
try:
int_arg = int( int_arg )
str_arg = str( str_arg )
except ValueError:
sys.stderr.write( "Args are not of the right type\n" )
sys.exit(1)

Granted, in a dynamic language we won't always (maybe "won't
often") have a situation where the types are known this well
at compile time. But sometimes we will. And it would be nice
to catch these before the program even runs.

So my question is: would bolting on "static type checking
when we can, no type checking when we can't" be too much
to ask?

My reason for asking initially was that the PEP adds all
kinds of information to the function prototype -- e.g.
(quoting from there)

def foobar( a: Integer, b: Sequence ) -> String:

so why not, I reasoned, use this information at compile
time?

Of course I understand the virtue of writing code with good
doctests, etc. But my question is why we can't get some more
static typing as well. Given the tools that'll be in Python
3.0, that doesn't seem unreasonable to ask.

Apparently it is unreasonable. I'm sorry I asked.

-- 
Stephen R. Laniel
[EMAIL PROTECTED]
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: strip() 2.4.4

2007-06-21 Thread Stephen R Laniel
On Thu, Jun 21, 2007 at 06:23:01AM -0700, Nick wrote:
> Why is there a apostrophe still at the end?

Is it possible that you actually have whitespace at the end
of the line? So then strip() is looking for an apostrophe at
the end of the line, not finding it, and therefore not
stripping it?

-- 
Stephen R. Laniel
[EMAIL PROTECTED]
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: strip() 2.4.4

2007-06-21 Thread Stephen R Laniel
On Thu, Jun 21, 2007 at 01:42:03PM +, linuxprog wrote:
> that should work for you ?

I reproduced the original poster's problem by adding one
extra space after the final "'" on each line. I'd vote that
that's the problem.

-- 
Stephen R. Laniel
[EMAIL PROTECTED]
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PEP 3107 and stronger typing (note: probably a newbie question)

2007-06-21 Thread Stephen R Laniel
On Thu, Jun 21, 2007 at 03:55:48PM +0200, Bjoern Schliessmann wrote:
> I agree. I also notice that (rather newbie-) OPs with not-so-simple
> questions are easily offended by technical answers. I'd love to
> know why.

One doesn't like to get meta on such things, as so often
happens, so I'll be brief. The way I read my original post
was like so: "I know lots of people have probably had a
similar question, so please point me to a canonical answer
is there is one. So here's the question...". To me, an
appropriate answer there is, "That's a good question, and
many people have asked it. Here's why it's unlikely that
static type-checking makes sense in the context of Python,
and why it couldn't just be added in pieces." We got there
eventually, but only after what seemed to me to be a bout of
rudeness. Now, maybe my own followup clarified something
that I should have included in the original.

"Use another language" is not a technical answer. "Python
could not adopt static typing without substantially changing
the language and destroying what everyone loves about it,
and here are examples of where the problem shows up" is.

But I've just gone meta, so I'll stop.

-- 
Stephen R. Laniel
[EMAIL PROTECTED]
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PEP 3107 and stronger typing (note: probably a newbie question)

2007-06-21 Thread Stephen R Laniel
On Thu, Jun 21, 2007 at 10:11:57AM -0400, Stephen R Laniel wrote:
> "Use another language" is not a technical answer. "Python
> could not adopt static typing without substantially changing
> the language and destroying what everyone loves about it,
> and here are examples of where the problem shows up" is.

The best dynamic-versus-static-typing discussion I've ever
seen, by the way, was Mark-Jason Dominus's (he of
"Higher-Order Perl"):
http://perl.plover.com/yak/typing/notes.html

-- 
Stephen R. Laniel
[EMAIL PROTECTED]
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tailing a log file?

2007-06-22 Thread Stephen R Laniel
On Fri, Jun 22, 2007 at 11:46:56AM -0700, Evan Klitzke wrote:
> I checked the source code for tail and they actually poll the file by
> using fstat and sleep to check for changes in the file size. This
> didn't seem right so I thought about it more and realized I ought to
> be using inotify. So I guess I answered my own question.

I've not thought about it much, but how about using Twisted?
Something like the LineReceiver class seems appropriate
here.

http://twistedmatrix.com/documents/current/api/twisted.protocols.basic.LineReceiver.html

-- 
Stephen R. Laniel
[EMAIL PROTECTED]
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie tcp/ip question

2007-06-24 Thread Stephen R Laniel
On Sun, Jun 24, 2007 at 02:52:25PM -0500, Zachary Manning wrote:
> I want to send some hex characters as a string literal to port 7142 and 
> to a specific ip address. How do I do that in python?

Does this cover what you want to do?
http://docs.python.org/lib/socket-example.html

-- 
Stephen R. Laniel
[EMAIL PROTECTED]
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: server wide variables

2007-06-25 Thread Stephen R Laniel
On Mon, Jun 25, 2007 at 03:29:25PM -, Jay Sonmez wrote:
> I want to be able to save some server variables as long as Apache runs
> on the server (mod_python).
> 
> How is that possible in Python?

Would setting environment variables help you? You'd use
os.environ.
http://docs.python.org/lib/os-procinfo.html

-- 
Stephen R. Laniel
[EMAIL PROTECTED]
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: finding an element in a string

2007-06-25 Thread Stephen R Laniel
On Mon, Jun 25, 2007 at 04:09:50PM -0400, Miguel Oliveira wrote:
> But that, obviously, will only respond "good" when one writes "fine". I was
> looking for a way for the program to respond "good" to any sentence that would
> contain the word "fine" in it.

What you want is a regular-expression match. Are you
familiar with regexes? The relevant Python documentation is
here (for the module 're'):
http://docs.python.org/lib/module-re.html

though I find it's not particularly easy reading. The
Friedl book recommended in there is the canonical source
about regexes, and it is indeed a great book. You should
read some manpages first, though. Just google for 'regular
expressions' and you'll find all that you need.

In the meantime, what you want is something like

if re.search( r'fine', x ):
# things one does if the user said 'fine'

Alternately, you could just use rfind():
http://docs.python.org/lib/string-methods.html

but regexes are a good tool to get to know. They're overused
(particularly in Perl), but this is just the sort of task
they're good for.

-- 
Stephen R. Laniel
[EMAIL PROTECTED]
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help needed in Handling HTML file

2007-06-26 Thread Stephen R Laniel
On Tue, Jun 26, 2007 at 09:42:37AM -0500, senthil arasu wrote:
> Right now Iam handling HTML files using Python.
> Is there any modules to support HTML parsing and rendering ?
> or suggest me any other way to support HTML.

Parsing: BeautifulSoup
http://www.crummy.com/software/BeautifulSoup/

Rendering: not sure. If worse came to worst, you could
always call out to 'w3m -dump' using system(). How
sophisticated do you want your rendering to be? Other people
on this last may well know of ways to fully render a page,
including graphics and JavaScript and whatever else, using a
scriptable Python object; I do not.

-- 
Stephen R. Laniel
[EMAIL PROTECTED]
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Return name of caller function?

2007-06-26 Thread Stephen R Laniel
On Mon, Jun 25, 2007 at 06:27:29PM -0700, Matthew Peter wrote:
> For example, how do I get this to work?
> 
> def func():
> print "This is", __?__
> return __caller__
> 
> def echo():
> print "This is ", __?__
> return func()

inspect is your friend:
http://docs.python.org/lib/inspect-stack.html

-- 
Stephen R. Laniel
[EMAIL PROTECTED]
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PEP 3107 and stronger typing (note: probably a newbie question)

2007-06-27 Thread Stephen R Laniel
On Wed, Jun 27, 2007 at 01:44:17PM +0200, Bruno Desthuilliers wrote:
> Indeed - static typing is for compilers, not for programmers.

When done well, static typing helps the programmer -- just
like writing good unit tests. It's not a magic bullet, but
it can help.

I'd like to point again to Mark-Jason Dominus's
explanation of how strong static typing can be done well:
http://perl.plover.com/yak/typing/notes.html

The example toward the end of how ML actually spots an
infinite loop at compile time seems to me to be "for
programmers" rather than "for compilers."

-- 
Stephen R. Laniel
[EMAIL PROTECTED]
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PEP 3107 and stronger typing (note: probably a newbie question)

2007-06-27 Thread Stephen R Laniel
On Wed, Jun 27, 2007 at 04:03:39PM +0200, Bruno Desthuilliers wrote:
> Stephen, you may not know yet, but Python is *dynamic*. This defeats 
> almost all compile-time checking. Of what help would type inference be 
> when you can dynamically add/remove/replace attributes (including 
> methods and class) at runtime ?

Heh. [Insert grimacing smile here]

People on here have been ... ahhh ... *generous* about
explaining that Python is dynamic, and hence that static
typing doesn't make sense. I've got it. I merely passed
along that MJD reference because you were making a point
about what static typing is for in general (i.e., not
specific to Python). I thought I'd give examples of how
languages other than Python use it to good effect, and not
just for compiler optimization.

Cheers,
Steve

-- 
Stephen R. Laniel
[EMAIL PROTECTED]
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Return name of caller function?

2007-06-27 Thread Stephen R Laniel
On Wed, Jun 27, 2007 at 01:25:14PM -0700, Matthew Peter wrote:
> Parsing the stack's tuple to get those attributes didn't feel reliable or 
> pythonic.
> I am likely overlooking something. Is there a brief example you could show me 
> in the
> context of using inspect to accomplish the goal I outlined above? The goal is 
> using
> a function and not a class. Thanks!

The code below doesn't do the trick for you?

#!/usr/bin/python
import inspect

def master():
print "I am the master"
slave()

def slave():
stack = inspect.stack()
caller = stack[1][3]
print "I am the slave; my caller was %s" % caller

def main():
    master()

if __name__ == '__main__':
main()

-- 
Stephen R. Laniel
[EMAIL PROTECTED]
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PEP 3107 and stronger typing (note: probably a newbie question)

2007-06-27 Thread Stephen R Laniel
On Thu, Jun 28, 2007 at 09:08:16AM +0200, Bruno Desthuilliers wrote:
> You said ?

I could link again to Mark-Jason Dominus, who writes that
people often make the following inference:

1) C is strongly typed.
2) C's typing sucks.
3) Hence strong typing sucks.

But I won't.

It doesn't need to be a religious war. Why can't people just
say "When strong typing is done and used well, it's a
useful tool; when it's not, it's not"?

-- 
Stephen R. Laniel
[EMAIL PROTECTED]
Cell: +(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-- 
http://mail.python.org/mailman/listinfo/python-list