Re: Is there some Python function that searches "sys.path" for a module?

2008-01-14 Thread John Nagle
Shane Geiger wrote:
> If I understand you correctly, you want this:
> 
> module.__file__

No, this is for a module that isn't loaded, and
I don't want to load it. What I needed was "imp.find_module()".
I don't want to load the module in the current Python instance.
I need its path so I can start it in a subprocess.

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


Re: Is there some Python function that searches "sys.path" for a module?

2008-01-14 Thread John Nagle
Miki wrote:

> http://docs.python.org/lib/module-imp.html

   Ah. "imp.find_module".  I was looking in "sys" and
path-related places.  Thanks.

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


Re: Python too slow?

2008-01-14 Thread Jaimy Azle
<[EMAIL PROTECTED]> wrote:

>> > fact 1: CPython compiles source code to byte-code.
>> > fact 2: CPython executes this byte-code.
>> > fact 3: Sun's JDK compiles source code to byte-code.
>> > fact 4: Sun's JDK executes this byte-code.
>>
>> Fact 4 is misleading because it is only one option available to Sun's
>> JDK.  Sun's JDK is also capable of transforming the byte-code to
>> native code and letting the processor execute that instead of the
>> original byte code, and that is where the most significant speed
>> increase comes from.  Most importantly, it does so automatically, by
>> default, with no programmer intervention or configuration, and with
>> 100% compatibility, so it doesn't compare well to Python accelerators
>> like psyco.
>
> Then fact 1 is misleading too since Python handles the compilation
> automatically without programmer's intervention while Java requires
> someone to explicitely invoke the byte-code compiler.
>

Sadly it is true also, I read somewhere this silly point was used also to 
make distinction between java and python, then claiming python is just like 
another interpreter, while java has it's own (what they call as) 'compiler'. 
:)

perhaps in the future another sillly point could be added also, Java has 
Jython, while Python doesn't have some thing like PyJava or... perhaps Py-va 
(Python based Java Language).

Salam,

-Jaimy.


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


Re: Is there some Python function that searches "sys.path" for a module?

2008-01-14 Thread Shane Geiger
If I understand you correctly, you want this:

module.__file__




John Nagle wrote:
>Python's own loader searches "sys.path" for module names, but is there
> some function that makes that search functionality accessible to
> Python programs?  I need the absolute pathname of a module, with the
> search being done exactly the same way "import" does it.  The loader for
> "egg" files has this functionality, but I'd like to find out if there's
> a standard way to do this before looking into that source code.
>
>Also, it seems that the environment variable "PYTHONPATH" applies to
> "import", but not to the starting module named on the Python command
> line.  Is that correct?  Thanks.
>
>   John Nagle
>   


-- 
Shane Geiger
IT Director
National Council on Economic Education
[EMAIL PROTECTED]  |  402-438-8958  |  http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy

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


Re: Is there some Python function that searches "sys.path" for a module?

2008-01-14 Thread Miki
Hello John,

>    Python's own loader searches "sys.path" for module names, but is there
> some function that makes that search functionality accessible to
> Python programs?  I need the absolute pathname of a module, with the
> search being done exactly the same way "import" does it.  The loader for
> "egg" files has this functionality, but I'd like to find out if there's
> a standard way to do this before looking into that source code.
>
>    Also, it seems that the environment variable "PYTHONPATH" applies to
> "import", but not to the starting module named on the Python command
> line.  Is that correct?  Thanks.
http://docs.python.org/lib/module-imp.html

HTH,
--
Miki Tebeka <[EMAIL PROTECTED]>
http://pythonwise.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Is there some Python function that searches "sys.path" for a module?

2008-01-14 Thread John Nagle
   Python's own loader searches "sys.path" for module names, but is there
some function that makes that search functionality accessible to
Python programs?  I need the absolute pathname of a module, with the
search being done exactly the same way "import" does it.  The loader for
"egg" files has this functionality, but I'd like to find out if there's
a standard way to do this before looking into that source code.

   Also, it seems that the environment variable "PYTHONPATH" applies to
"import", but not to the starting module named on the Python command
line.  Is that correct?  Thanks.

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


Re: "env" parameter to "popen" won't accept Unicode on Windows - minor Unicode bug

2008-01-14 Thread John Nagle
Benjamin wrote:
> On Jan 14, 6:26 pm, Bjoern Schliessmann  [EMAIL PROTECTED]> wrote:
>> John Nagle wrote:
>>> It turns out that the strings in the "env" parameter have to be
>>> ASCII, not Unicode, even though Windows fully supports Unicode in
>>> CreateProcess.
>> Are you sure it supports Unicode, not UTF8 or UTF16? Probably using
>> something like u"thestring".encode("utf16") will help.
> Otherwise: bugs.python.org

 Whatever translation is necessary should be done in "popen", which
has cases for Windows and POSIX.  "popen" is supposed to be cross-platform
to the extent possible.  I think it's just something that didn't get fixed
when Unicode support went in.

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


Re: A question about event handlers with wxPython

2008-01-14 Thread Miki
Hello Eric,

> I'd appreciate any pointer on a simple way to tell within an event handler
> where the event came from.
def HandleSomething(self, event):
generating_control = event.GetEventObject()
print generating_control

HTH,
--
Miki Tebeka <[EMAIL PROTECTED]>
http://pythonwise.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: paging in python shell

2008-01-14 Thread Alex K
Thanks it's good to know. iPyton looks really neat.

On 15/01/2008, Ben Finney <[EMAIL PROTECTED]> wrote:
> John Machin <[EMAIL PROTECTED]> writes:
>
> > C:\junk>python demomore.py | more
>
> Your example uses the OS shell to invoke a pager on the output of the
> Python process. The OP was asking about paging *within* the Python
> shell.
>
> To my knowledge there's nothing in the default Python shell that
> enables what the OP is asking for. There are other Python shells, e.g.
> Idle, ipython, or a Python window inside Emacs, that may be better
> suited.
>
> --
>  \  "Compulsory unification of opinion achieves only the unanimity |
>   `\ of the graveyard."  -- Justice Roberts in 319 U.S. 624 (1943) |
> _o__)  |
> Ben Finney
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: LANG, locale, unicode, setup.py and Debian packaging

2008-01-14 Thread Martin v. Löwis
>Has it been decided how Python 3.0 will implement os.listdir on Unix?
> Will there be only a single attempt to encode using the current locale
> or will there be a backup technique?

That's what it currently does.

> I'd probably define an optional
> encoding parameter so you can ask for os.listdir(encoding="iso-8859-1")
> although that then propagates into open, ...

I had the same idea, and I think that parameter should be added.

For open(), I think we should continue to accept byte strings as file
names.

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


Re: super, decorators and gettattribute

2008-01-14 Thread Michele Simionato
On Jan 14, 11:47 pm, Richard Szopa <[EMAIL PROTECTED]> wrote:
> Could you tell me what are the pros and cons of the two approaches
> (i.e. writing a decorator function and a decorator descriptor class)?

I prefer to use a class for introspection sake, since
there is no way to get information about an inner function
in a closure, whereas your users can introspect classes
pretty well.

> super_object = super(self.__class__, self)

Notice that using super(self.__class__, self) is a common
mistake: the pitfalls with inheritance were discussed
very recently in this same newsgroup, you should be
able to find the post. What you need is

super(class_where_the_method_is_defined, self)

which is in general different from

super(self.__class__, self)

There is no clean way to determine the current class
in Python < 3.0 (Python 3.0 does it automatically);
if you want to see a hackish way involving
bytecode tricks see
http://groups.google.com/group/comp.lang.python/browse_frm/thread/a6010c7494871bb1/62a2da68961caeb6?hl=en&lnk=gst&q=currentClass#62a2da68961caeb6

(and notice that this is really not recommended).

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


A question about event handlers with wxPython

2008-01-14 Thread Erik Lind
I'd appreciate any pointer on a simple way to tell within an event handler 
where the event came from.
I want to have "while" condition in a handler to stop or change processing 
if an event occurs from some other button click.

Trying to bind more than one event to the same handler still doesn't tell me 
where the event came from in a basic bind. Is there an argument I can put in 
the bind so as to identify the source of the event in the event argument? .





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


Re: __init__ explanation please

2008-01-14 Thread Ben Finney
Hrvoje Niksic <[EMAIL PROTECTED]> writes:

> Ben Finney <[EMAIL PROTECTED]> writes:
> 
> > Hrvoje Niksic <[EMAIL PROTECTED]> writes:
> >> __init__ *is* the closest equivalent to what other languages
> >> would call a constructor.
> >
> > No. That would be '__new__', which actually constructs the
> > instance,
> 
> That's not what other OO languages (C++, Java) actually call a
> constructor

More fool them, then. It seems that the best referent for the term
"constructor" is the thing that does the constructing and returns the
result.

-- 
 \"Imagine a world without hypothetical situations."  -- Anonymous |
  `\   |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: NotImplimentedError

2008-01-14 Thread Ben Finney
George Sakkis <[EMAIL PROTECTED]> writes:

> On Jan 14, 5:39 pm, Ben Finney <[EMAIL PROTECTED]>
> wrote:
> 
> > I think of NotImplemented as equivalent to None; it's useful as a
> > sentinel value to set an attribute to in (e.g.) an abstract class.
> 
> My guess would be that it is more of an implementation performance
> decision than semantic. Checking 'if result is NotImplemented' is much
> faster than guarding the call with a try/except NotImplementedError
> block

Even better is not to check at all, and just try to use the return
value as normal. If it's the NotImplemented object, exceptions will
soon be raised that explain the problem. EAFP, dotcha know.

> Semantically though, 'return NotImplemented' looks odd compared to
> 'raise NotImplementedError'.

I use 'NotImplemented' as the default value for something that
*shouldn't* be used as-is, because the *programmer* needs to do more
work on the implementation. This either means an abstract base class,
that must be inherited from to override the attribute; or a section of
code that needs more work directly to complete it.

I have never written 'return NotImplemented' except for the
implementation of __cmp__ and friends. I can't think when that would
be a good idea as compared to 'raise NotImplementedError("explanatory
text")' (note: create an instance, don't just raise the class object).

-- 
 \ "To succeed in the world it is not enough to be stupid, you |
  `\ must also be well-mannered."  -- Voltaire |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: module naming conventions

2008-01-14 Thread Ben Finney
grackle <[EMAIL PROTECTED]> writes:

> On Jan 14, 6:28 pm, Ben Finney <[EMAIL PROTECTED]>
> wrote:
> > Release your package as free software on the Cheeseshop
> > http://cheeseshop.python.org/>. If the name you want is
> > already taken, pick one that will help users distinguish yours
> > from the existing one.
> 
> Unfortunately, my company thinks it's their software and not mine :-)

That's fine. Companies can release (and sell!) freely-licensed
software too, and many of them do so. You, as someone who works there
and is presumably responsible for this package, have leverage more
than anyone else in this discussion to make it happen.

-- 
 \"I spent a lot of money on wine and women, and like a fool I |
  `\  squandered the rest."  -- Benny Hill |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


reliable whois in python

2008-01-14 Thread eliss
Hi everyone,

I'm trying to write a python script to whois a few domains twice a day
so I get notified when they become available. I did it 2 ways, but
neither way is very reliable, so I'm asking here.

1) I tried just calling "whois %s" using popen, but I found that once
every few weeks, I get errors like:
connect: No route to host OR
fgets: Connection reset by peer
I don't think it's a connectivity issue because looking up the other
domains that day works, and just one domain will give an error. So
it's probably more like a networking issue? I don't know...

2) I tried using sockets to connect to the whois port of
whois.internic.net and then read the info, which works a little better
than 1). However, sometimes this is not reliable too and I get errors.

Does anyone have any ideas how I can do this better?

Thanks,

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


Re: __init__ explanation please

2008-01-14 Thread Steven D'Aprano
On Tue, 15 Jan 2008 00:00:45 +0100, Hrvoje Niksic wrote:

> Ben Finney <[EMAIL PROTECTED]> writes:
> 
>> Hrvoje Niksic <[EMAIL PROTECTED]> writes:
>>
>>> Wildemar Wildenburger <[EMAIL PROTECTED]> writes:
>>> > __init__() /initializes/ an instance (automatically after creation).
>>> > It is called, /after/ the instance has been constructed
>>> 
>>> I don't understand the purpose of this "correction".  After all,
>>> __init__ *is* the closest equivalent to what other languages would
>>> call a constructor.
>>
>> No. That would be '__new__', which actually constructs the instance,
> 
> That's not what other OO languages (C++, Java) actually call a
> constructor, so your correction is misplaced.  My other posts in this
> thread have expanded on this.


How fortunate that Python isn't one of those other OO languages, 
otherwise it might cause a bit of confusion.



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


Re: "env" parameter to "popen" won't accept Unicode on Windows - minor Unicode bug

2008-01-14 Thread Benjamin
On Jan 14, 6:26 pm, John Nagle <[EMAIL PROTECTED]> wrote:
> I passed a dict for the "env" variable to Popen with Unicode strings
> for the dictionary values.
>
> Got:
>
>File "D:\Python24\lib\subprocess.py", line 706, in _execute_child
> TypeError: environment can only contain strings
>
> It turns out that the strings in the "env" parameter have to be ASCII,
> not Unicode, even though Windows fully supports Unicode in CreateProcess.

>
> John Nagle

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


Re: "env" parameter to "popen" won't accept Unicode on Windows - minor Unicode bug

2008-01-14 Thread Benjamin
On Jan 14, 6:26 pm, Bjoern Schliessmann  wrote:
> John Nagle wrote:
> > It turns out that the strings in the "env" parameter have to be
> > ASCII, not Unicode, even though Windows fully supports Unicode in
> > CreateProcess.
>
> Are you sure it supports Unicode, not UTF8 or UTF16? Probably using
> something like u"thestring".encode("utf16") will help.
Otherwise: bugs.python.org
>
> Regards,
>
> Björn
>
> --
> BOFH excuse #31:
>
> cellular telephone interference

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


SimCity GPLed

2008-01-14 Thread Terry Reedy
http://weblogs.asp.net/bsimser/archive/2008/01/10/simcity-source-code-released-to-the-wild-let-the-ports-begin.aspx

The release appears to include both a C/Tcl/TK version and a C++/Python 
version of at least part.

"The code hopefully serves as a good example of how to use SWIG to 
integrate C++ classes into
 Python and Cairo, in a portable cross platform way that works on Linux and 
Windows."

I have not gotten the code yet. 



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


Re: short path evaluation, why is f() called here: dict(a=1).get('a', f())

2008-01-14 Thread Steven D'Aprano
On Mon, 14 Jan 2008 15:15:28 -0800, Paul Rubin wrote:

> Steven D'Aprano <[EMAIL PROTECTED]> writes:
>> map = {'a': Aclass, 'b': Bclass, 'c': Cclass} class_ = map.get(astring,
>> default=Zclass)
>> 
>> The result I want is the class, not the result of calling the class
>> (which would be an instance). If I wanted the other semantics, I'd be
>> using defaultdict instead.
> 
> I used default as a keyward arg name indicating the presence of a
> callable.  I probably should have called it defaultfunc or something.
> 
> x = d.get('a', f)  # --> default value is f x = d.get('a',
> defaultfunc=f)  # --> default value is result of f() .


So you're talking about proposed *added* behaviour, rather than 
*replacing* the current behaviour?

Sorry if I misunderstood you in the first place.




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


Restart crashing modules in windows

2008-01-14 Thread Astan Chee
Hi,
I have a python module that keeps on crashing with various windows 
errors (not BSOD but the less lethal windows XP popup ones). Now these 
are intentional and rather sporadic so I cant really solve it by 
attempting to fix the crash; rather what Im trying to do is make another 
module outside it that restarts this module every time it crashes. Is 
this possible? How do I do this? Or does one windows crash in one python 
module crash python entirely and I have to resort in an external program 
to restart python everytime it crashes?
Thanks again for all the help.
Astan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: jpype with JFreeChart, anyone interested to help?

2008-01-14 Thread oyster
Thanx
However I knew Chaco and matplotlib, and I use matplotlib during my
school days. And as I have pointed out, they are for "plot", but not
"chart". If you don't know the difference between plot and chart, you
can have a look at at http://www.jfree.org/jfreechart,
http://www.rmchart.com
Yes, it is true we can use plot lib to draw chart, but that is tedious.

2008/1/15, [EMAIL PROTECTED] <[EMAIL PROTECTED]>:
> From: Peter Wang <[EMAIL PROTECTED]>
> To: python-list@python.org
> Date: Mon, 14 Jan 2008 07:58:02 -0800 (PST)
> Subject: Re: jpype with JFreeChart, anyone interested to help?
> On Jan 14, 6:51 am, oyster <[EMAIL PROTECTED]> wrote:
> > As you may know, there is no beautiful and free chart(notplot, you
> > can find the examples 
> > athttp://www.jfree.org/jfreechart,http://www.rmchart.com) module for python 
> > than runs on
> > windows/linux/mac osx.
>
> Actually, may I humbly suggest two:
>
> Chaco: http://code.enthought.com/chaco/gallery/index.shtml
>
> matplotlib: http://matplotlib.sourceforge.net/
>
>
> -Peter
>
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: super, decorators and gettattribute

2008-01-14 Thread Basilisk96
On Jan 14, 7:53 am, Michele Simionato <[EMAIL PROTECTED]>
wrote:
> I really need to publish this one day or another, since these
> questions
> about super keeps coming out:
>
> http://www.phyast.pitt.edu/~micheles/python/super.html

Please do. It is a very enlightening discussion, and I'm sure a bunch
of folks will benefit from it. And please update it (if necessary) to
the current Python version. At the time of that writing, 2.3 must have
been King, but oh my, how time flies :-)

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


Re: NotImplimentedError

2008-01-14 Thread George Sakkis
On Jan 14, 5:39 pm, Ben Finney <[EMAIL PROTECTED]>
wrote:

> I think of NotImplemented as equivalent to None; it's useful as a
> sentinel value to set an attribute to in (e.g.) an abstract class.

My guess would be that it is more of an implementation performance
decision than semantic. Checking 'if result is NotImplemented' is much
faster than guarding the call with a try/except NotImplementedError
block, and since the motivation for NotImplemented was AFAIK rich
comparisons, the potential overhead could be substantial (e.g. a tight
'while ihttp://mail.python.org/mailman/listinfo/python-list


Re: Dynamical scoping

2008-01-14 Thread Paul Rubin
George Sakkis <[EMAIL PROTECTED]> writes:
> What's the best way to simulate dynamically scoped variables ala Lisp ? 

Ugh check the docs for the python 2.5 "with" statement, which
gives you sort of a programmable unwind-protect (more powerful than
try/except).  You'd have an environment dictionary and use the "with"
statement to maintain a stack of shallow-binding cells like in an
old-time lisp system, automatically unwinding when the "with" suite
finishes.  The whole concept sounds hopelessly crufty--I think nobody
even does it that way in Lisp any more, you're better off passing an
environment around explicitly.  If there were a lot of variables, this
could be a good application for functional maps, which I've been wanting
to implemetn for python.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: module naming conventions

2008-01-14 Thread grackle
On Jan 14, 6:28 pm, Ben Finney <[EMAIL PROTECTED]>
wrote:
> grackle <[EMAIL PROTECTED]> writes:
> What do you mean by "top-level module", and "the same top-level name"?
> Do you mean "the same fully-qualified name"? If two modules are in
> separate places in the hierarchy, they will have different
> fully-qualified names.

I mean that if I worked at Google (which I don't) and developed
google.search and google.officeapps, they would share the same top-
level name "google".  Because of this, if I wanted to use the two in
the same program, they would have to be deployed at the same point in
the source path:  if I deployed one set of source at src1/google/
search/etc. and the other at src2/google/officeapps/etc. and added
src1/ and src2/ to my application's Python source path, one of them
would be found and the other not.  (Essentially I'd be trying to
create two different modules, both named "google", which is
nonsense.)  I would have to put them in the same source directory and
merge the two google/__init__.py files together.

I might as well admit that I already made the same mistake in reverse,
using a single top-level module for my application, which I now want
to split into a group of core application modules and one or more
optional, separately-deployed packages.  Since I screwed up the first
time, I figured I'd get advice before attempting to rectify the
situation.  My next (slightly more considered, possibly just as naive)
impulse is to turn google.search and google.officeapps into
google_search and google_officeapps, thereby avoiding name clashes
with internal or external projects.  (It feels dangerous, not to
mention presumptuous, to put generic names like "search" and
"officeapps" in the global namespace.)

> Release your package as free software on the Cheeseshop
> http://cheeseshop.python.org/>. If the name you want is already
> taken, pick one that will help users distinguish yours from the
> existing one.

Unfortunately, my company thinks it's their software and not mine :-)

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


Data mapper - need to map an dictionary of values to a model

2008-01-14 Thread Luke
I am writing an order management console. I need to create an import
system that is easy to extend. For now, I want to accept an dictionary
of values and map them to my data model. The thing is, I need to do
things to certain columns:

- I need to filter some of the values (data comes in as -MM-
DDTHH:MM:SS-(TIMEZONE-OFFSET) and it needs to map to Order.date as a
-MM-DD field)
- I need to map parts of an input column to more than one model param
(for instance if I get a full name for input--like "John Smith"--I
need a function to break it apart and map it to
Order.shipping_first_name and Order.shipping_last_name)
- Sometimes I need to do it the other way too... I need to map
multiple input columns to one model param (If I get a shipping fee, a
shipping tax, and a shipping discount, I need them added together and
mapped to Order.shipping_fee)

I have begun this process, but I'm finding it difficult to come up
with a good system that is extensible and easy to understand. I won't
always be the one writing the importers, so I'd like it to be pretty
straight-forward. Any ideas?

Oh, I should also mention that many times the data will map to several
different models. For instance, the importer I'm writing first would
map to 3 different models (Order, OrderItem, and OrderCharge)

I am not looking for anybody to write any code for me. I'm simply
asking for inspiration. What design patterns would you use here? Why?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: module naming conventions

2008-01-14 Thread Carl Banks
On Jan 14, 11:44 am, grackle <[EMAIL PROTECTED]> wrote:
> Obviously Java-style naming is a mistake in Python, since top-level
> names have to be unique.  Is there a standard naming convention to
> facilitate mixing code from different sources, such as
> mygroupname_modulename?  Is there a best practices guide for module
> naming?


Not really.  Try to pick a unique name for your project.  <:\


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


Re: module naming conventions

2008-01-14 Thread Ben Finney
grackle <[EMAIL PROTECTED]> writes:

> I do use packages. I mentioned the Java naming conventions because
> they were my first thought for solving the problem of name clashes,
> and they work well in some non-Java languages. They don't apply well
> to Python, since every top-level module has a unique identity that
> can only be specified on one source path. If two libraries use the
> same top-level module name, they can't be used together in the same
> program without modifying their source code.

What do you mean by "top-level module", and "the same top-level name"?
Do you mean "the same fully-qualified name"? If two modules are in
separate places in the hierarchy, they will have different
fully-qualified names.

=
$ find foo/ -name '*.py'
foo/__init__.py
foo/spam.py
foo/bar/__init__.py
foo/bar/spam.py
foo/baz/__init__.py
foo/baz/spam.py
=

= eggs.py =
import foo.spam
import foo.bar.spam
import foo.baz.spam

# ...
=

> mycompany_mymodulename was just the first solution I thought of that
> seemed practical.  The mycompany_ prefix protects me from name clashes
> with useful modules I might acquire from elsewhere.

Ah, I see; you're referring to a worldwide package namespace, enforced
by the language. AFAIK there's no such thing in Python.

> Of course, the best convention is probably the predominant one, and
> that's my question: What is the standard way of naming Python
> packages?

Release your package as free software on the Cheeseshop
http://cheeseshop.python.org/>. If the name you want is already
taken, pick one that will help users distinguish yours from the
existing one.

-- 
 \   "If you do not trust the source do not use this program." |
  `\—Microsoft Vista security dialogue |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: "env" parameter to "popen" won't accept Unicode on Windows - minor Unicode bug

2008-01-14 Thread Bjoern Schliessmann
John Nagle wrote:

> It turns out that the strings in the "env" parameter have to be
> ASCII, not Unicode, even though Windows fully supports Unicode in
> CreateProcess.

Are you sure it supports Unicode, not UTF8 or UTF16? Probably using
something like u"thestring".encode("utf16") will help.

Regards,


Björn

-- 
BOFH excuse #31:

cellular telephone interference

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


"env" parameter to "popen" won't accept Unicode on Windows - minor Unicode bug

2008-01-14 Thread John Nagle
I passed a dict for the "env" variable to Popen with Unicode strings
for the dictionary values.

Got:

   File "D:\Python24\lib\subprocess.py", line 706, in _execute_child
TypeError: environment can only contain strings

It turns out that the strings in the "env" parameter have to be ASCII,
not Unicode, even though Windows fully supports Unicode in CreateProcess.

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


Re: LANG, locale, unicode, setup.py and Debian packaging

2008-01-14 Thread Neil Hodgson
Martin v. Löwis:

> That's not true. Try open("\xff","w"), then try interpreting the file
> name as UTF-8. Some byte strings are not meaningful UTF-8, hence that
> approach cannot work.

Has it been decided how Python 3.0 will implement os.listdir on 
Unix? Will there be only a single attempt to encode using the current 
locale or will there be a backup technique? I'd probably define an 
optional encoding parameter so you can ask for 
os.listdir(encoding="iso-8859-1") although that then propagates into 
open, ...

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


Re: module naming conventions

2008-01-14 Thread grackle
On Jan 14, 4:47 pm, Ben Finney <[EMAIL PROTECTED]>
wrote:
> I'm not sure, but it sounds as though you have yet to discover Python
> module packages http://www.python.org/doc/essays/packages.html>.
> They allow a hierarchy of modules in directories.

I do use packages.  I mentioned the Java naming conventions because
they were my first thought for solving the problem of name clashes,
and they work well in some non-Java languages.  They don't apply well
to Python, since every top-level module has a unique identity that can
only be specified on one source path.  If two libraries use the same
top-level module name, they can't be used together in the same program
without modifying their source code.

mycompany_mymodulename was just the first solution I thought of that
seemed practical.  The mycompany_ prefix protects me from name clashes
with useful modules I might acquire from elsewhere.  There's still a
chance of accidental name clashes with other developers in my company,
but those can be dealt with.  I'm not forced to share a top-level
module with every other Python project in the company, so I can
develop different projects independently and then import one into the
other, without modifying or merging their source.

Of course, the best convention is probably the predominant one, and
that's my question:  What is the standard way of naming Python
packages?

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


Re: super, decorators and gettattribute

2008-01-14 Thread Richard Szopa
On Jan 14, 11:05 pm, thebjorn <[EMAIL PROTECTED]>
wrote:

> I don't remember if CLOS was changed to use C3 Linearization also, but
> the concept came from Dylan (http://www.webcom.com/haahr/dylan/
> linearization-oopsla96.html) and that's what is implemented in Python.

The Common Lisp ANSI standard is from 1994, and the article you cite
is from 1996, which strongly suggests C3 linearization wasn't included
in CLOS. The most important difference between CLOS and C3
linearization AFAIK is that the latter enforces monotonicity, while
the former doesn't.

Of course, it shouldn't be very difficult to implement the C3 behavior
in Common Lisp using the de facto standard MetaObject Protocol.

(Nb. Dylan was such a nice language... It's a pity it is practically
dead right now.)

> but if you're a language geek like me, you might be excited that in
> some cases it is
>
>   type(x).__dict__['y'].__get__(x, type(x))
>
> which says you get the value of x.y by calling y and passing x as an
> argument -- if you know CLOS you'll recognize that it's a primitive
> multi-method call. (there are some other special cases too, although
> not as exciting ;-)

Yeah, I also feel the excitement, so probably I am a language geek
too ;-). However, this is still quite far away from full fledged
multimethods. (OTOH trying to get something more from these primitive
multimethods by abusing __get__ looks kind of tempting ;-))

Regards,

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


Re: SyntaxError: 'import *' not allowed with 'from .'

2008-01-14 Thread Carl Banks
On Jan 14, 2:37 pm, George Sakkis <[EMAIL PROTECTED]> wrote:
> Unless I missed it, PEP 328 doesn't mention anything about this.
> What's the reason for not allowing "from .relative.module import *' ?

I'm just guessing: it could accidentally create infinite recursion.
Or, perhaps something more subtle than infinite recursion, such as
hard-to-comprehend rules about what modules and subpackages would be
imported.  So they just decided to disallow it.  Just a guess.


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


Re: NotImplimentedError

2008-01-14 Thread Ben Finney
Ben Finney <[EMAIL PROTECTED]> writes:

> I think of NotImplemented as equivalent to None

Not "equivalent"; rather "comparable in usage".

-- 
 \ "[W]e are still the first generation of users, and for all that |
  `\ we may have invented the net, we still don't really get it."  |
_o__) -- Douglas Adams |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: import from question

2008-01-14 Thread George Sakkis
On Jan 14, 4:22 pm, iu2 <[EMAIL PROTECTED]> wrote:
> Hi all
>
> I've got three files:
>
> file a1.py:
> 
> the_number = None
>
> file a2.py:
> 
> import a1
>
> def init():
> a1.the_number = 100
>
> file a3.py:
> 
> from a1 import the_number
> import a2
>
> a2.init()
> print the_number, type(the_number)
>
> Runninr a3.py I get:
> None 
>
> Changing a3.py to:
> import a1
> import a2
>
> a2.init()
> print a1.the_number, type(a1.the_number)
>
> gives:
> 100 
>
> Why doesn't it work in the first version of a3.py?
>
> Thanks,
> iu2

Try to guess what the following snippet prints, run it, and see if you
guessed correctly:

s = {'a':None}
x = s['a']
s['a'] = 1
print x

The same mechanism applies to what "from ... import" does.

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


Re: LANG, locale, unicode, setup.py and Debian packaging

2008-01-14 Thread Martin v. Löwis
> Ah. Can one call it after the full call has been done:
> locale.setlocale(locale.LC_ALL,'')
> locale.setlocale(locale.LC_ALL)
> Without any issues?

If you pass LC_ALL, then some systems will give you funny results
(semicolon-separated enumerations of all the categoryies). Instead,
pick a specific category, e.g. LC_CTYPE.

>>> I need that two-letter code that's hidden in a
>>> typical locale like en_ZA.utf8 -- I want that 'en' part.
> Okay, I need it because I have a tree of dirs: en, it, fr and so on for the 
> help files -- it's to help build a path to the right html file for the 
> language being supported.

Ok - taking the first two letters should then be fine, assuming all your
directories have two-letter codes.

>> Not sure why you want that. Notice that the locale name is fairly system
>> specific, in particular on non-POSIX systems. It may be
>> "English_SouthAfrica" on some systems.
> Wow, another thing I had no idea about. So far all I've seen are the 
> xx_yy.utf8 shaped ones.
> 
> I will have some trouble then, with the help system.

If you have "unknown" systems, you can try to use locale.normalize.
This has a hard-coded database which tries to deal with some different
spellings. For "English", it will give you en_EN.ISO8859-1.

OTOH, if your software only works on POSIX systems, anyway, I think
it is a fair assumption that they use two-letter codes for the
languages (the full language name is only used on Windows, AFAIK).

Notice that xx_yy.utf8 definitely is *not* the only syntactical form.
utf8 is spelled in various ways (lower and upper case, with and without
dash), and there may be other encodings (see the en_EN example above),
or no encoding at all in the locale name, and their may be "modifiers":

[EMAIL PROTECTED] (saaho dialect in Eritrea)
[EMAIL PROTECTED] (as opposed to the Cyrillic be_BY locale)
likewise for sr_RS
[EMAIL PROTECTED] (as opposed to the D-Mark locale); likewise for other
   members of the Euro zone
[EMAIL PROTECTED] (Valencian - Southern Catalan)
   (no real difference to [EMAIL PROTECTED], but differences in
message translations)
[EMAIL PROTECTED]  (Ge'ez language in Eritrea with Abegede collation)
[EMAIL PROTECTED]   (Tatar language written in IQTElif alphabet)
[EMAIL PROTECTED]  (as opposed to latin uz_UZ)

There used to be a @bokmal modifier for Norwegian (as opposed to
the Nynorsk grammar), but they have separate language codes now
(nb vs. nn).

Regards,
Martin



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


Re: short path evaluation, why is f() called here: dict(a=1).get('a', f())

2008-01-14 Thread Paul Rubin
Steven D'Aprano <[EMAIL PROTECTED]> writes:
> map = {'a': Aclass, 'b': Bclass, 'c': Cclass}
> class_ = map.get(astring, default=Zclass)
> 
> The result I want is the class, not the result of calling the class 
> (which would be an instance). If I wanted the other semantics, I'd be 
> using defaultdict instead.

I used default as a keyward arg name indicating the presence of
a callable.  I probably should have called it defaultfunc or something.

x = d.get('a', f)  # --> default value is f
x = d.get('a', defaultfunc=f)  # --> default value is result of f() .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: __init__ explanation please

2008-01-14 Thread Hrvoje Niksic
Ben Finney <[EMAIL PROTECTED]> writes:

> Hrvoje Niksic <[EMAIL PROTECTED]> writes:
>
>> Wildemar Wildenburger <[EMAIL PROTECTED]> writes:
>> > __init__() /initializes/ an instance (automatically after
>> > creation). It is called, /after/ the instance has been constructed
>> 
>> I don't understand the purpose of this "correction".  After all,
>> __init__ *is* the closest equivalent to what other languages would
>> call a constructor.
>
> No. That would be '__new__', which actually constructs the instance,

That's not what other OO languages (C++, Java) actually call a
constructor, so your correction is misplaced.  My other posts in this
thread have expanded on this.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SyntaxError: 'import *' not allowed with 'from .'

2008-01-14 Thread George Sakkis
On Jan 14, 6:01 pm, Ben Finney <[EMAIL PROTECTED]>
wrote:

> George Sakkis <[EMAIL PROTECTED]> writes:
> > Unless I missed it, PEP 328 doesn't mention anything about this.
> > What's the reason for not allowing "from .relative.module import *'
> > ?
>
> It makes the code much harder to follow visually and inspect with
> static analysis tools, since there's no way to see where names come
> from in the code. It defeats the purpose of separate namespaces,
> confusing the imported module's names with the current module's names
> in a way that makes the indistinguishable.
>
> If you want to use all or most of the names in a module, keep them in
> their own namespace:
>
> import spam
> import eggs
>
> spam.do_stuff()
> eggs.do_stuff()
>
> If you don't like the name of the module, then use whatever one suits
> you:
>
> import your_mother_was_a_hamster as spam
> import your_father_smelled_of_elderberries as eggs
>
> spam.do_stuff()
> eggs.do_stuff()
>
> Both of these are superior to 'from spam import *' because it's clear
> (to the reader and to static analysis tools) where every name comes
> from: unqualified names must be defined in the current module, any
> ones from the imported module are qualified with the module name.
>
> You also, in cases like the above example, avoid unknowingly
> clobbering existing names by importing from another module into the
> current namespace.

All the above are well-known and apply to both absolute and relative
imports. I was asking why it's a syntax error specifically for
relative imports.

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


Re: paging in python shell

2008-01-14 Thread Ben Finney
John Machin <[EMAIL PROTECTED]> writes:

> C:\junk>python demomore.py | more

Your example uses the OS shell to invoke a pager on the output of the
Python process. The OP was asking about paging *within* the Python
shell.

To my knowledge there's nothing in the default Python shell that
enables what the OP is asking for. There are other Python shells, e.g.
Idle, ipython, or a Python window inside Emacs, that may be better
suited.

-- 
 \  "Compulsory unification of opinion achieves only the unanimity |
  `\ of the graveyard."  -- Justice Roberts in 319 U.S. 624 (1943) |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Append zip files together, just get the binary data (in memory)

2008-01-14 Thread BerlinBrown
On Jan 14, 5:58 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> BerlinBrown schrieb:
>
> > Is it possible to just build the binary content of a zip file.  I want
> > to create the content in memory (e.g. return binary data) and then get
> > those byte strings representing the zip file?  Is that possible?
>
> > Or could I possibly override functions in the zip class.
>
> > 1. Create a zip file object (e.g. dont actually create the file).
> > 2. Append stuff to the zip file (e.g. a file)
> > 3. Zip that content into memory (but still not touching the
> > filesystem)
> > 4. Extract those byte strings for (an array?) later use.
>
> > My goal is to concatenate multiple zip files into another binary file.
>
> Module StringIO is your friend.
>
> Diez


Clearly, someone doesn't know python as well as he should.  That is
good idea, thanks.  So basically treat StringIO as the in memory
container to write the zip file data to.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SyntaxError: 'import *' not allowed with 'from .'

2008-01-14 Thread Ben Finney
George Sakkis <[EMAIL PROTECTED]> writes:

> Unless I missed it, PEP 328 doesn't mention anything about this.
> What's the reason for not allowing "from .relative.module import *'
> ?

It makes the code much harder to follow visually and inspect with
static analysis tools, since there's no way to see where names come
from in the code. It defeats the purpose of separate namespaces,
confusing the imported module's names with the current module's names
in a way that makes the indistinguishable.

If you want to use all or most of the names in a module, keep them in
their own namespace:

import spam
import eggs

spam.do_stuff()
eggs.do_stuff()

If you don't like the name of the module, then use whatever one suits
you:

import your_mother_was_a_hamster as spam
import your_father_smelled_of_elderberries as eggs

spam.do_stuff()
eggs.do_stuff()

Both of these are superior to 'from spam import *' because it's clear
(to the reader and to static analysis tools) where every name comes
from: unqualified names must be defined in the current module, any
ones from the imported module are qualified with the module name.

You also, in cases like the above example, avoid unknowingly
clobbering existing names by importing from another module into the
current namespace.

-- 
 \   "Never use a long word when there's a commensurate diminutive |
  `\ available."  -- Stan Kelly-Bootle |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Perl Template Toolkit: Now in spicy new Python flavor

2008-01-14 Thread [EMAIL PROTECTED]
I'd like to inform the Python community that the powerful and popular
Template Toolkit system, previously available only in its original
Perl implementation, is now also available in a beta Python
implementation:

http://tt2.org/python/index.html

I created this port both as a fun programming project, and for use in
environments where  Perl is not available, for reasons technical,
cultural, or otherwise.  The extensive Perl test suites have also been
ported, and most templates require no or very little modification.

Discussion of the Python implementation should be conducted on the
main Template Toolkit developer mailing list; see the site above for
details.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Append zip files together, just get the binary data (in memory)

2008-01-14 Thread Diez B. Roggisch
BerlinBrown schrieb:
> Is it possible to just build the binary content of a zip file.  I want
> to create the content in memory (e.g. return binary data) and then get
> those byte strings representing the zip file?  Is that possible?
> 
> Or could I possibly override functions in the zip class.
> 
> 1. Create a zip file object (e.g. dont actually create the file).
> 2. Append stuff to the zip file (e.g. a file)
> 3. Zip that content into memory (but still not touching the
> filesystem)
> 4. Extract those byte strings for (an array?) later use.
> 
> My goal is to concatenate multiple zip files into another binary file.


Module StringIO is your friend.

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


Re: super, decorators and gettattribute

2008-01-14 Thread Richard Szopa
On Jan 14, 1:53 pm, Michele Simionato <[EMAIL PROTECTED]>
wrote:

> I really need to publish this one day or another, since these
> questions
> about super keeps coming out:
>
> http://www.phyast.pitt.edu/~micheles/python/super.html

Thanks, Michele! Your essay was enlightening [2]. Specially if you
take in account super's documentation is slightly outdated :).

I also read Raymond Hettinger's article about descriptors (which you
mention, but don't link to!) and decided to use them to reimplement
methods that always call their superclass [1] method of the same name.
Could you tell me what are the pros and cons of the two approaches
(i.e. writing a decorator function and a decorator descriptor class)?
The former gives slightly shorter code, while the second gives access
to the original function and I somehow feel it is more... classy :)
[code follows]

Cheers,

-- Richard

[1] By which I mean the first class in their MRO.
[2] I also found some other papers of yours about the "not for the
faint of heart" corners of Python: MRO, metaclasses, class
memoization, etc. and they were most enjoyable and interesting.

def callingprevious1(fun):
"""Decorator to call a superclass' fun first. Decorator function
approach.

>>> class parent(object):
... def foo(self):
... print "I am foo of parent"
...
>>> class child(parent):
... @callingprevious1
... def foo(self):
... print "I am foo of child"
...
>>> x = child()
>>> x.foo()
I am foo of parent
I am foo of child
>>> child.foo(x)
I am foo of parent
I am foo of child
"""

name = fun.__name__
def decorated(self, *args, **kwargs):
try:
super_object = super(self.__class__, self)
getattr(super_object, name)(*args, **kwargs)
except AttributeError:
pass # if parent doesn't implement fun, we don't care
 # about it
return fun(self, *args, **kwargs) # hopefully None

decorated.__name__ = name
return decorated

class callingprevious(object):
"""
Decorator making the defined method call the method of the first
superclass in mro at the beginning. Descriptor approach.

>>> class parent(object):
... def foo(self):
... print "I am foo of parent"
...
>>> class child(parent):
... @callingprevious
... def foo(self):
... print "I am foo of child"
...
>>> x = child()
>>> x.foo()
I am foo of parent
I am foo of child
>>> child.foo(x)
I am foo of parent
I am foo of child
"""

def __init__(self, initval):
self.name = initval.__name__
self.__combine_methods(initval)

def __combine_methods(self, val):
self.val = val
def with_parent(obj, *args, **kwargs):
try:
parent_method = getattr(super(type(obj), obj),
self.val.__name__)
except AttributeError:
pass
else:
parent_method(*args, **kwargs)
return self.val(obj, *args, **kwargs)
with_parent.__name__ = self.val.__name__
self.to_return = with_parent

def __get__(self, obj, objtype):
from types import MethodType

# btw, is it anyhow better than just returning the function?
return MethodType(self.to_return, obj, objtype)

def __set__(self, obj, val):
self.__combine_methods(val)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: module naming conventions

2008-01-14 Thread Ben Finney
grackle <[EMAIL PROTECTED]> writes:

> Obviously Java-style naming is a mistake in Python, since top-level
> names have to be unique.  Is there a standard naming convention to
> facilitate mixing code from different sources, such as
> mygroupname_modulename?  Is there a best practices guide for module
> naming?

I'm not sure, but it sounds as though you have yet to discover Python
module packages http://www.python.org/doc/essays/packages.html>.
They allow a hierarchy of modules in directories.

The Python tutorial http://www.python.org/doc/tut/> will make
this and many more fundamental concepts of Python clearer by working
through it. Read each section, run every example, and experiment with
it until you understand it. Then move on to the next section. Repeat
until done.

-- 
 \"A hundred times every day I remind myself that [...] I must |
  `\   exert myself in order to give in the same measure as I have |
_o__)received and am still receiving" —Albert Einstein |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: LANG, locale, unicode, setup.py and Debian packaging

2008-01-14 Thread Donn
> You get the full locale name with locale.setlocale(category) (i.e.
> without the second argument)
Ah. Can one call it after the full call has been done:
locale.setlocale(locale.LC_ALL,'')
locale.setlocale(locale.LC_ALL)
Without any issues?

> > I need that two-letter code that's hidden in a
> > typical locale like en_ZA.utf8 -- I want that 'en' part.
Okay, I need it because I have a tree of dirs: en, it, fr and so on for the 
help files -- it's to help build a path to the right html file for the 
language being supported.

> Not sure why you want that. Notice that the locale name is fairly system
> specific, in particular on non-POSIX systems. It may be
> "English_SouthAfrica" on some systems.
Wow, another thing I had no idea about. So far all I've seen are the 
xx_yy.utf8 shaped ones.

I will have some trouble then, with the help system.

Thanks,
\d


-- 
"There may be fairies at the bottom of the garden. There is no evidence for 
it, but you can't prove that there aren't any, so shouldn't we be agnostic 
with respect to fairies?"
-- Richard Dawkins

Fonty Python and other dev news at:
http://otherwiseingle.blogspot.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: NotImplimentedError

2008-01-14 Thread Ben Finney
George Sakkis <[EMAIL PROTECTED]> writes:

> By the way, why do we need both NotImplementedError and the
> NotImplemented singleton object ? Couldn't we have just one of them ?

One of them is an exception class, the other is not. They have rather
different semantics.

I think of NotImplemented as equivalent to None; it's useful as a
sentinel value to set an attribute to in (e.g.) an abstract class.

For a function that is (or has a flow branch which is) not (yet?)
implemented, I don't want to return at all. Instead, I want to raise
an instance of NotImplementedError.

I think it's desirable to keep them both distinct as is.

-- 
 \  "If you get invited to your first orgy, don't just show up |
  `\ nude. That's a common mistake. You have to let nudity |
_o__)   'happen.'"  -- Jack Handey |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [ANN] pylint 0.14 / logilab-astng 0.17.2

2008-01-14 Thread Ben Finney
Sylvain Thénault <[EMAIL PROTECTED]> writes:

> I'm pleased to announce a new release of pylint [1] and logilab-astng
> [2]. I haven't personally found a lot of time to work on those projects
> since the latest releases but others contributors have and so I decided
> to publish releases including various contributions and other minor bug
> or crash fixes (some of which were pending for a while now). You're
> greatly encouraged to upgrade, see projects'changelog for more
> information about what changed.

Thanks very much for this.

Can you please give an overview of the major user-visible changes?
That will help in convincing people to upgrade.

-- 
 \"Most people don't realize that large pieces of coral, which |
  `\   have been painted brown and attached to the skull by common |
_o__) wood screws, can make a child look like a deer."  -- Jack Handey |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: super, decorators and gettattribute

2008-01-14 Thread Ben Finney
Michele Simionato <[EMAIL PROTECTED]> writes:

> I really need to publish this one day or another, since these
> questions about super keeps coming out:
> 
> http://www.phyast.pitt.edu/~micheles/python/super.html

Yes, please. Your article has improved my understanding just from
skimming the main headings :-)

-- 
 \ "Buy not what you want, but what you need; what you do not need |
  `\   is expensive at a penny."  -- Cato, 234-149 BC, Relique |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: encrypting python modules

2008-01-14 Thread Ben Finney
Robert Latest <[EMAIL PROTECTED]> writes:

> And, contrary to the advice I gave elsethread, unfortunately it's
> impossible to just drop uncooperative customers when you develop GPL
> software ;-)

On the contrary. The GPL includes a big fat "NO WARRANTY" clause. If
you're not selling warranties to people, don't provide any more
support to them than you want to.

The main benefit of software licensed under the GPL is that your
customer has the freedom to take the software and get someone else to
improve it instead of you. That's good for them *and* good for you,
since you *can* drop them if it's to your advantage to do so.

-- 
 \  "Everything is futile."  -- Marvin of Borg |
  `\   |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: __init__ explanation please

2008-01-14 Thread Ben Finney
Hrvoje Niksic <[EMAIL PROTECTED]> writes:

> Wildemar Wildenburger <[EMAIL PROTECTED]> writes:
> > __init__() /initializes/ an instance (automatically after
> > creation). It is called, /after/ the instance has been constructed
> 
> I don't understand the purpose of this "correction".  After all,
> __init__ *is* the closest equivalent to what other languages would
> call a constructor.

No. That would be '__new__', which actually constructs the instance,
and actually returns it to the caller. '__init__' does neither of
those.

It so happens that, in Python, one usually overrrides the initialiser
and not the constructor. Thus, the confusion is understandable, but
still regrettable and avoidable.

-- 
 \ "My, your, his, hers, ours, theirs, its. I'm, you're, he's, |
  `\ she's, we're, they're, it's." —anonymous, |
_o__)alt.sysadmin.recovery |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list

Append zip files together, just get the binary data (in memory)

2008-01-14 Thread BerlinBrown
Is it possible to just build the binary content of a zip file.  I want
to create the content in memory (e.g. return binary data) and then get
those byte strings representing the zip file?  Is that possible?

Or could I possibly override functions in the zip class.

1. Create a zip file object (e.g. dont actually create the file).
2. Append stuff to the zip file (e.g. a file)
3. Zip that content into memory (but still not touching the
filesystem)
4. Extract those byte strings for (an array?) later use.

My goal is to concatenate multiple zip files into another binary file.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: super, decorators and gettattribute

2008-01-14 Thread thebjorn
On Jan 14, 1:41 pm, Richard Szopa <[EMAIL PROTECTED]> wrote:
> On Jan 13, 3:31 pm, thebjorn <[EMAIL PROTECTED]>
> wrote:
>
> > They do, except for when it comes to what super(..) returns. It isn't
> > really an object in the sense that they're presented in the tutorial,
> > but rather a sort of proxy to the methods in the ancestor classes of
> > the concrete object (self), relative to the current method's class. I
> > can't imagine that sentence would ease any confusion however, suffice
> > it to say that you have to call getattr(super(..), 'name') instead of
> > super(..).__getattr__('name') and you have to call super(..).__len__()
> > instead of len(super(..)) -- I can't imagine that lessens any
> > confusion either :-/
>
> Surprisingly, I think your first sentence *does* make something more
> clear. Let me check if I understand it right: when we call a method on
> super(Foo, self) it is as if we were calling call-next-method in
> Common Lisp or Dylan

I don't remember if CLOS was changed to use C3 Linearization also, but
the concept came from Dylan (http://www.webcom.com/haahr/dylan/
linearization-oopsla96.html) and that's what is implemented in Python.

[...]
> However, there's one piece that doesn't completely fit to the puzzle:
> why does getattr work? The help says:
>
> getattr(...)
> getattr(object, name[, default]) -> value
>
> Get a named attribute from an object; getattr(x, 'y') is
> equivalent to x.y. When a default argument is given, it
> is returned when the attribute doesn't exist; without it,
> an exception is raised in that case.
>
> Does it work on the basis that "getattr(x, 'y') is equivalent to x.y"?
> What is then a "named attribute for an object" in Python? It seems not
> to be equivalent to the value of the item whose name is 'y' in the
> object's class __dict__...

Conceptually, x.y is always "get the y attribute of x" and the same as
getattr(x, 'y'). Depending on the type of x and y, and your
familiarity with Python internals, what actually happens during a
lookup might be surprising. In the vast majority of cases however, x.y
is equivalent to one of

  x.__dict__['y']
  type(x).__dict__['y']

but if you're a language geek like me, you might be excited that in
some cases it is

  type(x).__dict__['y'].__get__(x, type(x))

which says you get the value of x.y by calling y and passing x as an
argument -- if you know CLOS you'll recognize that it's a primitive
multi-method call. (there are some other special cases too, although
not as exciting ;-)

Much more detail can be found in Raymond's paper on descriptors
(http://users.rcn.com/python/download/Descriptor.htm) and Michele's
paper on super (http://www.phyast.pitt.edu/~micheles/python/
super.html).

-- bjorn


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


Re: short path evaluation, why is f() called here: dict(a=1).get('a', f())

2008-01-14 Thread Tim Chase
> But how can Python determine when you want the result to be *the 
> callable* and when you want it to be *the result of calling the 
> callable*? 
> 
> Functions and other callables are first-class objects, and it is quite 
> reasonable to have something like this:
> 
> map = {'a': Aclass, 'b': Bclass, 'c': Cclass}
> class_ = map.get(astring, default=Zclass)
> 
> The result I want is the class, not the result of calling the class 
> (which would be an instance). If I wanted the other semantics, I'd be 
> using defaultdict instead.

For an example of the defaultdict usage without calling it the 
first time:

   from collections import defaultdict
   def f():
 print "Doing some expensive calculation"
 return 42

   d = defaultdict(f)
   d['hello'] = 3.14159
   print 'Hello:', d['hello']
   print 'World:', d['world']
   print 'World (again):', d['world']

This results in the expensive calculation only being executed 
once and having the result stored in the defaultdict.  This is a 
good thing.

If you're doing as Steven suggests, you can pass and store 
function objects or class objects with the same ease:

> map = {'a': Aclass, 'b': Bclass, 'c': Cclass}
> class_ = map.get(astring, default=Zclass)

Other than tromping on the "map" built-in, one can then 
instantiate the given class with

   my_instance = map.get(astring, default=Zclass)(params)

Perfect for the factory pattern if you groove on that sort of thing.

-tkc



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


Re: split parameter line with quotes

2008-01-14 Thread Ricardo Aráoz
teddyber wrote:
> here's the solution i have for the moment :
> 
>   t = shlex.shlex(data)
>   t.wordchars = t.wordchars + "/+.-"
>   r=''
>   while 1:
>   token = t.get_token()
>   if not token:
>   break
>   if not token==',': r = r+token
>   else: r = r + ' '
>   self.DEBUG(r,'ok')
> for pair in r.split(' '):
> key,value=pair.split('=', 1)
> print(key+':'+value)
> 
> i know this is not perfect still but i'm coming a long way from very
> bad php habits! :o)
> and thanks for your help!
> 
> On 11 jan, 23:30, teddyber <[EMAIL PROTECTED]> wrote:
>> wow! that's perfect this shlex module! thanks for pointing this!
>>
>> On 11 jan, 20:36, Joshua Kugler <[EMAIL PROTECTED]> wrote:
>>
>>> teddyber wrote:
 first i'm a newbie to python (but i searched the Internet i swear).
 i'm looking for some way to split up a string into a list of pairs
 'key=value'. This code should be able to handle this particular
 example string :
 qop="auth,auth-int,auth-conf",cipher="rc4-40,rc4-56,rc4,des,
 3des",maxbuf=1024,charset=utf-8,algorithm=md5-sess
 i know i can do that with some regexp (i'm currently trying to learn
 that) but if there's some other way...
>>> Take a look at the shlex module.  You might be able to fiddle with the shlex
>>> object and convince it to split on the commas.  But, to be honest, that
>>> above would be a lot easier to parse if the dividing commas were spaces
>>> instead.
>>> j
> 

Maybe you like :

>>> x = 'qop = "auth,auth-int,auth-conf",cipher="rc4-40,rc4-56,rc4,des,
3des",maxbuf=1024,charset=utf-8,algorithm=md5-sess'

>>> dict(zip([k[-1].strip() for k in (j.split(',') for j in ''.join(i
for i in x if i != '"').split('='))][:-1], [k[:-1] or k for k in
(j.split(',') for j in ''.join(i for i in x if i != '"').split('='))][1:]))

{'maxbuf': ['1024'], 'cipher': ['rc4-40', 'rc4-56', 'rc4', 'des', '
3des'], 'charset': ['utf-8'], 'algorithm': ['md5-sess'], 'qop': ['
auth', 'auth-int', 'auth-conf']}

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


Re: hide object property from dir() function?

2008-01-14 Thread Matimus
On Jan 14, 1:20 pm, jerryji <[EMAIL PROTECTED]> wrote:
> Hi,
>
> Sorry for this newbie question, I was puzzled why the existing
> property of an object is not shown in the dir() function output.
>
> "v" is an lxml Element object variable --
>
> In [44]: v
> Out[44]: 
>
> In [45]: dir(v)
> Out[45]:
> ['__copy__',
>  '__deepcopy__',
>  '__reduce__',
>  'append',
>  'clear',
>  'find',
>  'findall',
>  'findtext',
>  'get',
>  'getchildren',
>  'getiterator',
>  'insert',
>  'items',
>  'keys',
>  'makeelement',
>  'remove',
>  'set']
>
> dir() output doesn't contain the ".tag", which does exist --
>
> In [46]: v.tag
> Out[46]: 'documentProperties'
>
> what is the rule governing the display of a property from dir()?


You can also make properties by modifying the __getattr__ and
__setattr__ methods of a class. When done that way it won't show up
when dir is called. So it isn't necessarily a rule, just a different
way of implementing a property.

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


Re: LANG, locale, unicode, setup.py and Debian packaging

2008-01-14 Thread Martin v. Löwis
> Given that getlocale() is not to be used, what's the best way to get the 
> locale later in the app?

You get the full locale name with locale.setlocale(category) (i.e.
without the second argument)

> I need that two-letter code that's hidden in a 
> typical locale like en_ZA.utf8 -- I want that 'en' part.

Not sure why you want that. Notice that the locale name is fairly system
specific, in particular on non-POSIX systems. It may be
"English_SouthAfrica" on some systems.

If you are certain that *your* locale names will only ever be of the
form [_][.<[EMAIL PROTECTED] (or whatever
the syntax is), take anything before the underscore as the language code.

However, you should reevaluate why you need that.

> BTW - things are hanging-together much better now, thanks to your info. I 
> have 
> it running in locale 'C' as well as my other test locales. What a relief!

Great!

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


Re: X/Linux mouse_event (like in win32api)

2008-01-14 Thread Atila Olah
Thank you Jorgen. Your answer helped me a lot.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: short path evaluation, why is f() called here: dict(a=1).get('a', f())

2008-01-14 Thread Steven D'Aprano
On Mon, 14 Jan 2008 12:08:52 -0800, Paul Rubin wrote:

> aspineux <[EMAIL PROTECTED]> writes:
>> Yes, I missed 'get' and 'setdefault' are functions :-) Then why not
>> some new semantic
>> 
>> d.get('a', f()) --> d['a', f()]
>> d.setdefault('a', f()) --> d['a'=f()]
>> 
>> Is is a good idea enough to change the python semantic ? Or simply is
>> it a good idea ?
> 
> Changing python semantics for something like this is nuts.  Allowing
> passing a callable (sort of like re.sub allows) makes a certain amount
> of sense:
> 
>d.get('a', default=f)


But how can Python determine when you want the result to be *the 
callable* and when you want it to be *the result of calling the 
callable*? 

Functions and other callables are first-class objects, and it is quite 
reasonable to have something like this:

map = {'a': Aclass, 'b': Bclass, 'c': Cclass}
class_ = map.get(astring, default=Zclass)

The result I want is the class, not the result of calling the class 
(which would be an instance). If I wanted the other semantics, I'd be 
using defaultdict instead.


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


Re: problem with logging exceptions with non-ASCII __str__ result

2008-01-14 Thread Vinay Sajip
On Jan 14, 5:46 pm, Karsten Hilbert <[EMAIL PROTECTED]> wrote:
> Dear all,
>
> I have a problem withloggingan exception.
>
> environment:
>
> Python 2.4, Debian testing
>
> ${LANGUAGE} not set
> ${LC_ALL} not set
> ${LC_CTYPE} not set
> ${LANG}=de_DE.UTF-8
>
> activating user-default locale with  '')> returns: [de_DE.UTF-8]
>
> locale.getdefaultlocale() - default (user) locale: ('de_DE', 'utf-8')
> encoding sanity check (also check "locale.nl_langinfo(CODESET)" 
> below):
> sys.getdefaultencoding(): [ascii]
> locale.getpreferredencoding(): [UTF-8]
> locale.getlocale()[1]: [utf-8]
> sys.getfilesystemencoding(): [UTF-8]
>
> _logfile = codecs.open(filename = _logfile_name, mode = 'wb', 
> encoding = 'utf8', errors = 'replace')
>
>logging.basicConfig (
> format = fmt,
> datefmt = '%Y-%m-%d %H:%M:%S',
> level =logging.DEBUG,
> stream = _logfile
> )
>
> I am using psycopg2 which in turn uses libpq. When trying to
> connect to the database and providing faulty authentication
> information:
>
> try:
> ... try to connect ...
> except StandardError, e:
> _log.error(u"login attempt %s/%s failed:", attempt+1, 
> max_attempts)
>
> print "exception type  :", type(e)
> print "exception dir   :", dir(e)
> print "exception args  :", e.args
> msg = e.args[0]
> print "msg type:", type(msg)
> print "msg.decode(utf8):", msg.decode('utf8')
> t,v,tb = sys.exc_info()
> print "sys.exc_info()  :", t, v
> _log.exception(u'exception detected')
>
> the following output is generated:
>
> exception type  : 
> exception dir   : ['__doc__', '__getitem__', '__init__', 
> '__module__', '__str__', 'args']
> exception args  : ('FATAL:  Passwort-Authentifizierung f\xc3\xbcr 
> Benutzer \xc2\xbbany-doc\xc2\xab fehlgeschlagen\n',)
> msg type: 
> msg.decode(utf8): FATAL:  Passwort-Authentifizierung für Benutzer 
> »any-doc« fehlgeschlagen
>
> sys.exc_info()  : psycopg2.OperationalError FATAL:  
> Passwort-Authentifizierung für Benutzer »any-doc« fehlgeschlagen
>
> Traceback (most recent call last):
>   File "/usr/lib/python2.4/logging/__init__.py", line 739, in emit
> self.stream.write(fs % msg.encode("UTF-8"))
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 
> 191: ordinal not in range(128)
>
> Now, the string "FATAL: Passwort-Auth..." comes from libpq
> via psycopg2. It is translated to German via gettext within
> libpq (at the C level). As we can see it is of type string.
> I know from the environment that it is likely encoded in
> utf8 manually applying which (see the decode call) succeeds.
>
> On _log.exception() theloggingmodule wants to output the
> message as encoded as utf8 (that's what the log file is set
> up as). So it'll look at the string, decide it is of type
> "str" and decode with the *Python default encoding* to get
> to type "unicode". Following which it'll re-encode with utf8
> to get back to type "str" ready for outputting to the log
> file.
>
> However, since the Python default encoding is "ascii" that
> conversion fails.
>
> Changing the Python default encoding isn't really an option
> as it is advocated against and would have to be made to work
> reliably on other users machines.
>
> One could, of course, write code to specifically check for
> this condition and manually pre-convert the message string
> to unicode but that seems not as things should be.
>
> How can I cleanly handle this situation ?
>
> Should theloggingmodule internally use an encoding gotten
> from the locale module rather than the default string encoding ?
>
> Karsten
> --
> GPG key ID E4071346 @ wwwkeys.pgp.net
> E167 67FD A291 2BEA 73BD  4537 78B9 A9F9 E407 1346

Please reduce to a minimal program which demonstrates the issue and
log an issue on bugs.python.org.

Best regards,

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


Re: encrypting python modules

2008-01-14 Thread Steven D'Aprano
On Mon, 14 Jan 2008 12:46:48 +, Robert Latest wrote:

> And, contrary to the advice I gave elsethread, unfortunately it's
> impossible to just drop uncooperative customers when you develop GPL
> software ;-)

Just because you are writing GPLed code doesn't mean you are permanently 
linked to anyone you have supplied code to. You can send their emails 
straight to /dev/null. You don't have to give them any support, only the 
source code. And if you do give them support, the GPL doesn't limit what 
your hourly rates are: you are free to charge them one million dollars 
per hour, payable in advance in blocks of fifteen hours.

If they don't like it, they can always fork the code, or find another 
person to support it, or simply stop being dicks.



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


import from question

2008-01-14 Thread iu2
Hi all

I've got three files:

file a1.py:

the_number = None

file a2.py:

import a1

def init():
a1.the_number = 100

file a3.py:

from a1 import the_number
import a2

a2.init()
print the_number, type(the_number)

Runninr a3.py I get:
None 

Changing a3.py to:
import a1
import a2

a2.init()
print a1.the_number, type(a1.the_number)

gives:
100 

Why doesn't it work in the first version of a3.py?

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


Re: __init__ explanation please

2008-01-14 Thread Steven D'Aprano
On Mon, 14 Jan 2008 22:18:44 +1100, Ben Finney wrote:

> What one is "in reality" calling is the '__new__' method of the Person
> class. That function, in turn, is creating a new Person instance, and
> calling the '__init__' method of the newly-created instance. Finally,
> the '__new__' method returns that instance back to the caller.


But not if Person is an old-style class.


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


hide object property from dir() function?

2008-01-14 Thread jerryji
Hi,

Sorry for this newbie question, I was puzzled why the existing
property of an object is not shown in the dir() function output.

"v" is an lxml Element object variable --

In [44]: v
Out[44]: 

In [45]: dir(v)
Out[45]:
['__copy__',
 '__deepcopy__',
 '__reduce__',
 'append',
 'clear',
 'find',
 'findall',
 'findtext',
 'get',
 'getchildren',
 'getiterator',
 'insert',
 'items',
 'keys',
 'makeelement',
 'remove',
 'set']

dir() output doesn't contain the ".tag", which does exist --

In [46]: v.tag
Out[46]: 'documentProperties'

what is the rule governing the display of a property from dir()?

Many thanks in advance!

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


Re: Simple List division problem

2008-01-14 Thread Pierre Quentel
On 12 jan, 19:37, marcstuart <[EMAIL PROTECTED]> wrote:
> How do I divide a list into a set group of sublist's- if the list is
> not evenly dividable ?
> consider this example:
>
> x = [1,2,3,4,5,6,7,8,9,10]
> y = 3  # number of lists I want to break x into
> z = y/x
>
> what I would like to get is 3 sublists
>
> print z[0] = [1,2,3]
> print z[2] = [4,5,6]
> print z[3] = [7,8,9,10]
>
> obviously not even, one list will have 4 elements, the other 2 will
> have 3.,
> the overriding logic, is that I will get 3 lists and find a way for
> python to try to break it evenly, if not one list can have a greater
> amount of elements
>
> Would I use itertools ? How would I do this ?
>
> Thanks

Hi,

If you want to split the list in 4, do you want
[1,2],[3,4],[5,6],[7,8,9,10] : all extra items in the last sublist
or
[1,2],[3,4],[5,6,7],[8,9,10] : one extra item in each of the last
sublists ?

Assuming you want the second version :

===
def split_list(lst,nb):
# ln = length of smaller sublists
# extra = number of longer sublists (they have ln+1 items)
ln,extra = divmod(len(lst),nb)
pos = ln*(nb-extra) # position where larger sublists begin
return [ lst[i*ln:(i+1)*ln] for i in xrange(nb-extra) ] \
+ [lst[pos+i*(ln+1):pos+(i+1)*(ln+1)] for i in xrange(extra)]

==

x = range(1,11)

print split_list(x,1)
>>>[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
print split_list(x,2)
>>>[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
print split_list(x,3)
>>>[[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
print split_list(x,4)
>>>[[1, 2], [3, 4], [5, 6, 7], [8, 9, 10]]
print split_list(x,5)
>>>[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
print split_list(x,10)
>>>[[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: paging in python shell

2008-01-14 Thread Alex K
Thanks John, but would it be possible to remain in the python interpreter?

On 14/01/2008, John Machin <[EMAIL PROTECTED]> wrote:
> On Jan 15, 7:35 am, "Alex K" <[EMAIL PROTECTED]> wrote:
> > Hi Tim,
> >
> > Yes I mean piping the output into "more" for example.
> >
>
> Why don't you "suck it and see"???
>
> E.g.
>
> C:\junk>copy con demomore.py
> for i in range(100):
> print 'line', i
> ^Z
> 1 file(s) copied.
>
> C:\junk>python demomore.py | more
> line 0
> line 1
> line 2
> line 3
> line 4
> [snip]
> line 50
> line 51
> line 52
> line 53
> line 54
> line 55
> line 56
> -- More  --
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: paging in python shell

2008-01-14 Thread John Machin
On Jan 15, 7:35 am, "Alex K" <[EMAIL PROTECTED]> wrote:
> Hi Tim,
>
> Yes I mean piping the output into "more" for example.
>

Why don't you "suck it and see"???

E.g.

C:\junk>copy con demomore.py
for i in range(100):
print 'line', i
^Z
1 file(s) copied.

C:\junk>python demomore.py | more
line 0
line 1
line 2
line 3
line 4
[snip]
line 50
line 51
line 52
line 53
line 54
line 55
line 56
-- More  --


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


Re: time.time or time.clock

2008-01-14 Thread John Machin
On Jan 15, 4:50 am, [EMAIL PROTECTED] wrote:
> """
> 
> time.clock() isn't high enough resolution for Ubuntu, and time.time()
> isn't
> high enough resolution on windows.
>
> Take a look at datetime.  It is good to the micro-second on Linux and
> milli-second on Windows.
> """

On Windows, time.clock has MICROsecond resolution, but your method
appears to have exactly the same (MILLIsecond) resolution as
time.time, but with greater overhead, especially when the result is
required in seconds-and-a-fraction as a float:

>>> def datetimer(start=datetime.datetime(1970,1,1,0,0,0), 
>>> nowfunc=datetime.datetime.now):
... delta = nowfunc() - start
... return delta.days * 86400 + delta.seconds +
delta.microseconds / 100.0
...
>>> tt = time.time(); td = datetimer(); diff = td - tt; print map(repr, (tt, td,
 diff))
['1200341583.484', '1200381183.484', '39600.0']
>>> tt = time.time(); td = datetimer(); diff = td - tt; print map(repr, (tt, td,
 diff))
['1200341596.484', '1200381196.484', '39600.0']
>>> tt = time.time(); td = datetimer(); diff = td - tt; print map(repr, (tt, td,
 diff))
['1200341609.4530001', '1200381209.4530001', '39600.0']
>>> tt = time.time(); td = datetimer(); diff = td - tt; print map(repr, (tt, td,
 diff))
['1200341622.562', '1200381222.562', '39600.0']
>>>

The difference of 39600 seconds (11 hours) would be removed by using
datetime.datetime.utcnow.

>
> import datetime
> begin_time=datetime.datetime.now()
> for j in range(10):
>x = j+1 # wait a small amount of time
> print "Elapsed time =", datetime.datetime.now()-begin_time
>
> ## You can also access the individual time values
> print begin_time.second
> print begin_time.microsecond ## etc.

Running that on my Windows system (XP Pro, Python 2.5.1, AMD Turion 64
Mobile cpu rated at 2.0 GHz), I get
Elapsed time = 0:00:00.031000
or
Elapsed time = 0:00:00.047000
Using 5 iterations, I get it down to 15 or 16 milliseconds. 15 ms
is the lowest non-zero interval that can be procured.

This is consistent with results obtained by using time.time.

Approach: get first result from timer function; call timer in a tight
loop until returned value changes; ignore the first difference so
found and save the next n differences.

Windows time.time appears to tick at 15 or 16 ms intervals, averaging
about 15.6 ms. For comparison, Windows time.clock appears to tick at
about 2.3 MICROsecond intervals.

Finally, some comments from the Python 2.5.1 datetimemodule.c:

/* No flavor of gettimeofday exists on this platform.  Python's
 * time.time() does a lot of other platform tricks to get the
 * best time it can on the platform, and we're not going to do
 * better than that (if we could, the better code would belong
 * in time.time()!)  We're limited by the precision of a double,
 * though.
 */

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


Re: paging in python shell

2008-01-14 Thread Alex K
Hi Tim,

Yes I mean piping the output into "more" for example.

Alex

On 14/01/2008, Tim Roberts <[EMAIL PROTECTED]> wrote:
> "Alex K" <[EMAIL PROTECTED]> wrote:
> >
> >Does anyone know if the python shell supports paging or if I should
> >look into iPython? Thank you so much.
>
> "Paging" is an overloaded term.  What do you mean, exactly?  Do you mean
> something like piping the output into "more"?  The Python shell does that
> for the "help" command, but maybe you could post a more precise example of
> what you want.
> --
> Tim Roberts, [EMAIL PROTECTED]
> Providenza & Boekelheide, Inc.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Dynamical scoping

2008-01-14 Thread George Sakkis
What's the best way to simulate dynamically scoped variables ala
Lisp ? The use case is an open-ended set of objects that need to
access the same piece of information (e.g. a  dict, a ConfigParser
object, a logger etc.). I know that the "proper" OO and functional way
is to pass the information explicitly but that's less maintenable in
the long run. Also this is in a web environment so the information
can't be really global (though within-thread global should be fine).
Is there some standard pattern for this scenario ?

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


Re: How to get user home directory on Windows

2008-01-14 Thread Giampaolo Rodola'
Thanks to Tim Golden suggestions I solved my problem.
...In case it would help someone:



import _winreg
import win32security

username = 'Administrator'
sid = win32security.ConvertSidToStringSid(
   win32security.LookupAccountName(None, username)[0]
   )
key = _winreg.OpenKey(
   _winreg.HKEY_LOCAL_MACHINE,
   r"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" + "\
\" + sid
   )
value, type = _winreg.QueryValueEx(key, "ProfileImagePath")
print value

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


Re: short path evaluation, why is f() called here: dict(a=1).get('a', f())

2008-01-14 Thread Paul Rubin
aspineux <[EMAIL PROTECTED]> writes:
> Yes, I missed 'get' and 'setdefault' are functions :-)
> Then why not some new semantic
> 
> d.get('a', f()) --> d['a', f()]
> d.setdefault('a', f()) --> d['a'=f()]
> 
> Is is a good idea enough to change the python semantic ?
> Or simply is it a good idea ?

Changing python semantics for something like this is nuts.  Allowing
passing a callable (sort of like re.sub allows) makes a certain
amount of sense:

   d.get('a', default=f)

You can also write (python 2.5, untested):

   d['a'] if 'a' in d else f()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Kamaelia] TCPClient: How to sense connection failure?

2008-01-14 Thread Bjoern Schliessmann
Michael Sparks wrote:
> It is sufficient, and running with Kamaelia from /trunk, your
> listener does indeed shutdown correctly

Great, thanks for validating. :)
 
> My suggestion for the moment would be to use the code on /trunk
> since this is stable at present (development happens on branches
> rather than /trunk) 

I will.

Regards,


Björn

-- 
BOFH excuse #243:

The computer fleetly, mouse and all.

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


Re: bags? 2.5.x?

2008-01-14 Thread Wildemar Wildenburger
Dan Stromberg wrote:
> Is there a particular reason why bags didn't go into 2.5.x or 3000?
> 
> I keep wanting something like them - especially bags with something akin 
> to set union, intersection and difference.
> 
How about this recepie 
http://www.ubookcase.com/book/Oreilly/Python.Cookbook.2nd.edition/0596007973/pythoncook2-chp-18-sect-8.html>?

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


SyntaxError: 'import *' not allowed with 'from .'

2008-01-14 Thread George Sakkis
Unless I missed it, PEP 328 doesn't mention anything about this.
What's the reason for not allowing "from .relative.module import *' ?

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


Re: help with slicing/replacing matrix sections.

2008-01-14 Thread Robert Kern
Erik Lind wrote:
> I see a more complicated thread on a similar sounding question, but my 
> question is simpler, I hope.

numpy questions are usually answered better on the numpy mailing list.

   http://www.scipy.org/Mailing_Lists

> I have a large numpy matrix, initially created as:
> 
> Mat = zeros((a,b), int)
> 
> and a smaller array with other data
> 
> Sub = [1,2,3,4,5],[6,7,8,9,0]
> 
> I want to replace a section of Mat matrix with Sub matrix without having to 
> loop through each cell in each matrix individually.
> 
> I thought index/slice assignments, should be able to do that, but I can only 
> get it to work (as per book examples) with simple arrays. Every syntax 
> combination I have tried gives one error or another, typically "can't 
> broadcast an object", but intuitively it seems there should be a simple 
> solution.
> 
> In short, how do I insert the data in the two (or any number of) rows in 
> Sub[0:2] into any part of Mat starting at Mat[x,y] without using "for" loops 
> ? 

In [11]: Sub = array([[1,2,3,4,5],[6,7,8,9,0]])

In [12]: Mat = zeros((10, 10), int)

In [13]: Mat[5:5+2,4:4+5] = Sub

In [14]: Mat
Out[14]:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 2, 3, 4, 5, 0],
[0, 0, 0, 0, 6, 7, 8, 9, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

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


Re: time.time or time.clock

2008-01-14 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote:
> """
> 
> time.clock() isn't high enough resolution for Ubuntu,   and time.time()
> isn't > high enough resolution on windows.
> 
> Take a look at datetime.  It is good to the micro-second on Linux and
> milli-second on Windows.

datetime.datetime.now() does the same thing as time.time(); it uses the 
gettimeofday() API for platforms that have it (and so does time.time()), 
and calls the fallback implementation in time.time() if gettimeofdat() 
isn't supported.  from the datetime sources:

#ifdef HAVE_GETTIMEOFDAY
struct timeval t;
#ifdef GETTIMEOFDAY_NO_TZ
gettimeofday(&t);
#else
gettimeofday(&t, (struct timezone *)NULL);
#endif
 ...
#else   /* ! HAVE_GETTIMEOFDAY */

/* No flavor of gettimeofday exists on this platform.  Python's
 * time.time() does a lot of other platform tricks to get the
 * best time it can on the platform, and we're not going to do
 * better than that (if we could, the better code would belong
 * in time.time()!)  We're limited by the precision of a double,
 * though.
 */

(note the "if we could" part).



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


help with slicing/replacing matrix sections.

2008-01-14 Thread Erik Lind
I see a more complicated thread on a similar sounding question, but my 
question is simpler, I hope.

I have a large numpy matrix, initially created as:

Mat = zeros((a,b), int)

and a smaller array with other data

Sub = [1,2,3,4,5],[6,7,8,9,0]

I want to replace a section of Mat matrix with Sub matrix without having to 
loop through each cell in each matrix individually.

I thought index/slice assignments, should be able to do that, but I can only 
get it to work (as per book examples) with simple arrays. Every syntax 
combination I have tried gives one error or another, typically "can't 
broadcast an object", but intuitively it seems there should be a simple 
solution.

In short, how do I insert the data in the two (or any number of) rows in 
Sub[0:2] into any part of Mat starting at Mat[x,y] without using "for" loops 
? 


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


Re: short path evaluation, why is f() called here: dict(a=1).get('a', f())

2008-01-14 Thread aspineux
On Jan 14, 7:49 pm, "Chris Mellon" <[EMAIL PROTECTED]> wrote:
> On Jan 14, 2008 12:39 PM, aspineux <[EMAIL PROTECTED]> wrote:
>
>
>
> > This append in both case
>
> > dict(a=1).get('a', f())
> > dict(a=1).setdefault('a', f())
>
> > This should be nice if f() was called only if required.
>
> Think about the change to Python semantics that would be required for
> this to be true, and then use collections.defaultdict instead.

Yes, I missed 'get' and 'setdefault' are functions :-)
Then why not some new semantic

d.get('a', f()) --> d['a', f()]
d.setdefault('a', f()) --> d['a'=f()]

Is is a good idea enough to change the python semantic ?
Or simply is it a good idea ?


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


Re: How to get user home directory on Windows

2008-01-14 Thread Lie
On Jan 14, 8:21 am, "Martin P. Hellwig" <[EMAIL PROTECTED]> wrote:
> Giampaolo Rodola' wrote:
> > Hi all,
> > I'm trying to use the pywin32 extension to find out the user's home
> > directory but currently I didn't find a solution yet.
> > What I'd need to do is not getting the home directory of the currently
> > logged in user but something like:
>
>  get_homedir("frank")
> > "C:\home\users\frank"
>  get_homedir("josh")
> > "C:\home\users\josh"
>
> > Is there a way to do that?
> > I tried to search through the Pywin32 documentation with no luck.
> > In addition I'm not practiced with the Windows API at all.
>
> Well, windows, to my knowledge, uses the same base path for all profiles
> (this is not true for the My Documents folder which can differ). So what
> you could do is get the location from the ALLUSERPROFILE environment
> variable, go one folder higher and iterate through that.
> Ignoring the folders for the 'pseudo' users: 'All Users', 'Default
> User', 'LocalService' and 'NetworkService'.
>
> hth
> --
> mph

There is one problem with that, sometimes the folders for the users
are not the same with the user name, usually because of deleting user
name without deleting the profile, then recreate a user with similar
name. It doesn't happens everytime, but it could.

Possibly it is possible to get the SID of the user name (using the way
described in Tim Golden's Microsoft Link' post), then find the user
directory from the registry: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft
\Windows NT\CurrentVersion\ProfileList\[PUT SID HERE]\Profile Image
Path
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: __init__ explanation please

2008-01-14 Thread Hrvoje Niksic
"Reedick, Andrew" <[EMAIL PROTECTED]> writes:

>> Only if by "construct" you mean "allocate".  __init__ starts out
>> with an empty object and brings it to a valid state, therefore
>> "constructing" the object you end up with.  That operation is
>> exactly what other languages call a constructor.
>
> Nah.  Most other languages combine the constructor and an init
> function.

Maybe so, but the standard term for what Python calls __init__ is
still "constructor".

> Also, how can a constructor require 'self' as an argument...?
> __init__(self, ...)

Think of it as the equivalent of Java's and C++'s "this", except it's
explicit in the argument list.  "Explicit is better than implicit" and
all that.  :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: short path evaluation, why is f() called here: dict(a=1).get('a', f())

2008-01-14 Thread Neil Cerutti
On Jan 14, 2008 1:39 PM, aspineux <[EMAIL PROTECTED]> wrote:
>
> This append in both case
>
> dict(a=1).get('a', f())
> dict(a=1).setdefault('a', f())
>
> This should be nice if f() was called only if required.

Shortcomings of those methods is probably why collections.defaultdict
is so popular.

>>> def f():
...return 7
...
>>> d = defaultdict(f, a=1)
>>> d['a']
1
>>> d['b']
7

get and setdefault aren't needed when using a default dict, and the
default factory is called only when needed.

-- 
Neil Cerutti <[EMAIL PROTECTED]>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Magic function

2008-01-14 Thread dg . google . groups
Hi Rüdiger,

Thanks for your message. I liked your approach and I've been trying
something along exactly these sorts of lines, but I have a few
problems and queries.

The first problem is that the id of the frame object can be re-used,
so for example this code (where I haven't defined InstanceTracker and
getInstances, but they are very closely based on the ideas in your
message):

class A(InstanceTracker):
gval = 0
def __init__(self):
self.value = A.gval # each time you make a new object, give
A.gval += 1 # it a value one larger
def __repr__(self):
return str(self.value)

def f2():
a = A() # objects 0 and 2
return getInstances(A)

def f3():
a = A() # object 1
return f2()

inst2 = f2()
inst3 = f3()
print inst2
print inst3

The output is:

[0]
[0, 2]

The A-variable with value 0 is not being garbage collected because
it's saved in the variable inst2, but it's also being returned by the
second call to getInstances because the frame of f2 is the same each
time (which makes sense, but may be implementation specific?). The
same problem doesn't exist when you use the stack searching method
because from f2's point of view, the only bound instance of A is the
one in that particular call of f2. If you had at the end instead of
the inst2, inst3 stuff:

print f2()
print f3()

The output is:

[0]
[2]

Again, I guess this because A with value 0 is being garbage collected
between print f2() and print f3(), but again I think this is
implementation specific? You don't have a guarantee that this object
will be garbage collected straight away do you?

So my concern here is that this approach is actually less safe than
the stack based approach because it depends on implementation specific
details in a non-straightforward way. That said, I very much like the
fact that this approach works if I write:

a = [A()]
a = [[A()]]
etc.

To achieve the same thing with the stack based approach you have to
search through all containers to (perhaps arbitrary) depth.

I also have another problem which is that I have a function decorator
which returns a callable object (a class instance not a function).
Unfortunately, the frame in which the callable object is created is
the frame of the decorator, not the place where the definition is.
I've written something to get round this, but it seems like a bit of a
hack.

Can anyone suggest an approach that combines the best of both worlds,
the instance tracking approach and the stack searching approach? Or do
I need to just make a tradeoff here?

Thanks again for all your help everyone,
Dan Goodman
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Exceptions - How do you make it work like built-in exceptions?

2008-01-14 Thread Lie
On Jan 14, 1:51 am, "Mark Tolonen" <[EMAIL PROTECTED]>
wrote:
> "Lie" <[EMAIL PROTECTED]> wrote in message
>
> news:[EMAIL PROTECTED]
>
>
>
> >A built-in exceptions, when raised, would print traceback that points
> > out the offending code, like this:
>
> > Traceback (most recent call last):
> >  File "F:\dir\code.py", line 43, in 
> >    a = 1/0 <<<---
> > ZeroDivisionError: integer division or modulo by zero
>
> > a user-made exception, when raised, would print traceback that points
> > out the code that raises the exception
>
> > Traceback (most recent call last):
> >  File "F:\dir\code.py", line 48, in 
> >    raise SomeException('Some Exception Message') <<<---
> > SomeException: Some Exception Message
>
> > which is generally of little use (yeah, it's possible to trace the
> > code from the line number, but sometimes it might not be that easy,
> > cause the line number is (again) the line number for the raising code
> > instead of the offending code)
>
> > The sample exception was generated from this code:
> > 
> > class SomeException(Exception):
> >    pass
>
> > try:
> >    a = 1/0
> > except:
> >    raise SomeException('Some Exception Message')
> > 
>
> > Is it possible to make the user-made exception points out the
> > offending code?
>
> The raise statement *was* the offending (unhandled exception) code.  The
> ZeroDivisionError was handled by your except clause.
>

Well, what you meant by offending code and what I meant by offending
code is different, what I meant by offending code as the code that
makes the exception _need_ to be called (i.e. the a=1/0) and in my
view (in this case), anything inside the except clause is not a real
code, as it doesn't do anything "useful" for the program.

> You can override the traceback your exception will use with the
> three-expression form of the raise statement (See Section 6.9 "The raise
> statement" in the Python Reference Manual) by passing the traceback of the
> original exception:
>
> ## CODE #
>
> import sys
>
> class SomeException(Exception):
>     pass
>
> try:
>     a=1/0
> except:
>     org_type,org_value,org_traceback = sys.exc_info()
>     raise SomeException,'had some problems with this code',org_traceback
>
> ## OUTPUT ##
>
> Traceback (most recent call last):
>   File "exc.py", line 7, in 
>     a=1/0
> SomeException: had some problems with this code
>
> --Mark

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


Re: short path evaluation, why is f() called here: dict(a=1).get('a', f())

2008-01-14 Thread Chris Mellon
On Jan 14, 2008 12:39 PM, aspineux <[EMAIL PROTECTED]> wrote:
>
> This append in both case
>
> dict(a=1).get('a', f())
> dict(a=1).setdefault('a', f())
>
> This should be nice if f() was called only if required.
>

Think about the change to Python semantics that would be required for
this to be true, and then use collections.defaultdict instead.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: __init__ explanation please

2008-01-14 Thread Neil Cerutti
On Jan 14, 2008 12:08 PM, Reedick, Andrew <[EMAIL PROTECTED]> wrote:
> > -Original Message-
> > From: [EMAIL PROTECTED] [mailto:python-
> > [EMAIL PROTECTED] On Behalf Of Hrvoje Niksic
> > Sent: Monday, January 14, 2008 11:29 AM
> > Only if by "construct" you mean "allocate".  __init__ starts out with
> > an empty object and brings it to a valid state, therefore
> > "constructing" the object you end up with.  That operation is exactly
> > what other languages call a constructor.
>
> Nah.  Most other languages combine the constructor and an init function.
> Normally with c++ I'll have the constructor call an Init() function so I
> can re-initialize the object as needed.  Python has explicitly split the
> two.

> Besides, the Python docs say that __new__ is the constructor and
> __init__ may or may not be called after the instance is created:

The documentation of __new__ carefully refrains from calling __new__ a
constructor. Both __new__ and __init__ mention the object constructor
expression, e.g., "list()".

> __init__( self[, ...])
> Called when the instance is created.
> ...
> As a special constraint on constructors, no value may be
> returned;

Once again, the documentation is referring to constructor expressions.
As you noted, __init__ may return a value when not called by a
constructor expression.

C++'s constructor initialization lists are the closest thing to
Python's __new__. They can perform tasks for which Python might need
__new__. For example, a class member that's a reference must be
initialized in the initialization list, because it cannot be set once
the body of the constructor begins.

-- 
Neil Cerutti <[EMAIL PROTECTED]>
-- 
http://mail.python.org/mailman/listinfo/python-list


short path evaluation, why is f() called here: dict(a=1).get('a', f())

2008-01-14 Thread aspineux

This append in both case

dict(a=1).get('a', f())
dict(a=1).setdefault('a', f())

This should be nice if f() was called only if required.

Regards.

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


Re: wxpython-wx.CheckListBox: changing item bacgkground color

2008-01-14 Thread Mike
On Jan 13, 9:22 am, Massi <[EMAIL PROTECTED]> wrote:
> Hi everyone! In my application (under windows) I'm using a
> wx.checklistbox. I would like the background color of an item to
> become red whenever an EVT_LISTBOX_DCLICK occurs. Is there any simple
> way to achieve it?
> Thanks in advance.

Did you try chkbox.SetBackgroundColor('SomeColor') ?

That might work, but I think that will just set the entire control's
background to the color passed in. You're welcome to give it a try,
though.

Also, I would recommend posting this query to the wxpython's user
group, which you can find at the wxPython website: www.wxpython.org

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


Re: Basic inheritance question

2008-01-14 Thread Lie
On Jan 7, 2:46 am, Bruno Desthuilliers
<[EMAIL PROTECTED]> wrote:
> Lie a écrit :
>
> > On Jan 5, 5:40 pm, [EMAIL PROTECTED] wrote:
>
> >>Jeroen Ruigrok van der Werven wrote:
>
> >>>Shouldn't this be:
>
> >>>self.startLoc = start
> >>>self.stopLoc = stop
>
> >>Thanks! Of course it should. Old Java habits die slowly.
>
> > No, seriously it isn't Java habits only, most other languages wouldn't
> > need explicit calling of class name.
>
> Where is the "explicit calling of class name" exactly ?

Perhaps I was a bit tired when writing that (I wouldn't understand
what I wrote if I were you)... what I meant is most other languages
doesn't usually enforce us to explicitly state the containing class
name, which in python is generally called "self". Most other languages
1) automatically assign the containing class' object in a keyword
(Java: this, VB: Me) behind the screen, and 2) automatically searches
variable name in both the local variable table and the containing
class variable table (so to refer to a class variable named var from a
method inside the class, we only need to write var, not self.var as in
python). In VB, Me is extremely rarely used, in Python, self is all
over the place. Well, there is positive and negative to both sides,
convenience in VB, and flexibility in Python.

Compare the following codes:
VB.NET:
Public Class A
Dim var
Public Function aFunction()
return var

Python:
class A:
def aFunction(self):
return self.var
-- 
http://mail.python.org/mailman/listinfo/python-list


Parsing links within a html file.

2008-01-14 Thread Shriphani
Hello,
I have a html file over here by the name guide_ind.html and it
contains links to other html files like guides.html#outline . How do I
point BeautifulSoup (I want to use this module) to
guides.html#outline ?
Thanks
Shriphani P.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: time.time or time.clock

2008-01-14 Thread dwblas
"""

time.clock() isn't high enough resolution for Ubuntu, and time.time()
isn't
high enough resolution on windows.

Take a look at datetime.  It is good to the micro-second on Linux and
milli-second on Windows.
"""

import datetime
begin_time=datetime.datetime.now()
for j in range(10):
   x = j+1 # wait a small amount of time
print "Elapsed time =", datetime.datetime.now()-begin_time

## You can also access the individual time values
print begin_time.second
print begin_time.microsecond ## etc.
-- 
http://mail.python.org/mailman/listinfo/python-list


problem with logging exceptions with non-ASCII __str__ result

2008-01-14 Thread Karsten Hilbert
Dear all,

I have a problem with logging an exception.

environment:

Python 2.4, Debian testing

${LANGUAGE} not set
${LC_ALL} not set
${LC_CTYPE} not set
${LANG}=de_DE.UTF-8

activating user-default locale with  returns: [de_DE.UTF-8]

locale.getdefaultlocale() - default (user) locale: ('de_DE', 'utf-8')
encoding sanity check (also check "locale.nl_langinfo(CODESET)" below):
sys.getdefaultencoding(): [ascii]
locale.getpreferredencoding(): [UTF-8]
locale.getlocale()[1]: [utf-8]
sys.getfilesystemencoding(): [UTF-8]

_logfile = codecs.open(filename = _logfile_name, mode = 'wb', encoding 
= 'utf8', errors = 'replace')

logging.basicConfig (
format = fmt,
datefmt = '%Y-%m-%d %H:%M:%S',
level = logging.DEBUG,
stream = _logfile
)

I am using psycopg2 which in turn uses libpq. When trying to
connect to the database and providing faulty authentication
information:

try:
... try to connect ...
except StandardError, e:
_log.error(u"login attempt %s/%s failed:", attempt+1, 
max_attempts)

print "exception type  :", type(e)
print "exception dir   :", dir(e)
print "exception args  :", e.args
msg = e.args[0]
print "msg type:", type(msg)
print "msg.decode(utf8):", msg.decode('utf8')
t,v,tb = sys.exc_info()
print "sys.exc_info()  :", t, v
_log.exception(u'exception detected')

the following output is generated:

exception type  : 
exception dir   : ['__doc__', '__getitem__', '__init__', '__module__', 
'__str__', 'args']
exception args  : ('FATAL:  Passwort-Authentifizierung f\xc3\xbcr 
Benutzer \xc2\xbbany-doc\xc2\xab fehlgeschlagen\n',)
msg type: 
msg.decode(utf8): FATAL:  Passwort-Authentifizierung für Benutzer 
»any-doc« fehlgeschlagen

sys.exc_info()  : psycopg2.OperationalError FATAL:  
Passwort-Authentifizierung für Benutzer »any-doc« fehlgeschlagen

Traceback (most recent call last):
  File "/usr/lib/python2.4/logging/__init__.py", line 739, in emit
self.stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 
191: ordinal not in range(128)

Now, the string "FATAL: Passwort-Auth..." comes from libpq
via psycopg2. It is translated to German via gettext within
libpq (at the C level). As we can see it is of type string.
I know from the environment that it is likely encoded in
utf8 manually applying which (see the decode call) succeeds.

On _log.exception() the logging module wants to output the
message as encoded as utf8 (that's what the log file is set
up as). So it'll look at the string, decide it is of type
"str" and decode with the *Python default encoding* to get
to type "unicode". Following which it'll re-encode with utf8
to get back to type "str" ready for outputting to the log
file.

However, since the Python default encoding is "ascii" that
conversion fails.

Changing the Python default encoding isn't really an option
as it is advocated against and would have to be made to work
reliably on other users machines.

One could, of course, write code to specifically check for
this condition and manually pre-convert the message string
to unicode but that seems not as things should be.

How can I cleanly handle this situation ?

Should the logging module internally use an encoding gotten
from the locale module rather than the default string encoding ?

Karsten
-- 
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD  4537 78B9 A9F9 E407 1346
-- 
http://mail.python.org/mailman/listinfo/python-list


bags? 2.5.x?

2008-01-14 Thread Dan Stromberg

Is there a particular reason why bags didn't go into 2.5.x or 3000?

I keep wanting something like them - especially bags with something akin 
to set union, intersection and difference.

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


Re: super, decorators and gettattribute

2008-01-14 Thread George Sakkis
On Jan 12, 6:56 pm, Steven D'Aprano <[EMAIL PROTECTED]
cybersource.com.au> wrote:

> On Sat, 12 Jan 2008 15:47:05 -0500, Mike Meyer wrote:
> > There's an apparently common bug here: you don't want to pass super
> > self.__class__, but the class that the method is bound to.
>
> Given an instance method, is it possible to easily determine what class
> it is defined in?
>
> I thought the im_class attribute might do it, but it apparently just
> points to self.__class__.
>
> >>> class Foo(object):
>
> ... def foo(self):
> ... pass
> ...>>> class Bar(Foo):
>
> ... def bar(self):
> ... pass
> ...>>> Bar().bar.im_class  # expecting Bar
>
> >>> Bar().foo.im_class  # hoping for Foo
>
> 

Something like that seems to work for most cases:

from inspect import getmro

def getdef(obj,attr):
try: objattrs = obj.__dict__
except AttributeError:
objattrs = obj.__slots__
if attr in objattrs:
return obj
for cls in getmro(obj.__class__):
if attr in cls.__dict__:
return cls

>>> getdef(Bar(), 'bar')


>>> getdef(Bar(), 'foo')



It probably misses some edge cases but I can't think of any off the
top of my head.

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


Re: IDLE won't start in Python 2.5 for Windows

2008-01-14 Thread mikez302
I was able to start IDLE from the command line, and reset my keyboard
shortcuts.  It works fine now.  In case this happens again, where
would I find the config file?
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: __init__ explanation please

2008-01-14 Thread Reedick, Andrew
> -Original Message-
> From: [EMAIL PROTECTED] [mailto:python-
> [EMAIL PROTECTED] On Behalf Of Hrvoje Niksic
> Sent: Monday, January 14, 2008 11:29 AM
> To: python-list@python.org
> Subject: Re: __init__ explanation please
> 
> Mel <[EMAIL PROTECTED]> writes:
> 
> >> I don't understand the purpose of this "correction".  After all,
> >> __init__ *is* the closest equivalent to what other languages would
> >> call a constructor.
> >
> > Nevertheless, __init__ doesn't construct anything.
> 
> Only if by "construct" you mean "allocate".  __init__ starts out with
> an empty object and brings it to a valid state, therefore
> "constructing" the object you end up with.  That operation is exactly
> what other languages call a constructor.


Nah.  Most other languages combine the constructor and an init function.
Normally with c++ I'll have the constructor call an Init() function so I
can re-initialize the object as needed.  Python has explicitly split the
two.


Besides, the Python docs say that __new__ is the constructor and
__init__ may or may not be called after the instance is created:

__new__( cls[, ...]) 

Called to create a new instance of class cls. __new__() is a
static method (special-cased so you need not declare it as such) that
takes the class of which an instance was requested as its first
argument. The remaining arguments are those passed to the object
constructor expression (the call to the class). The return value of
__new__() should be the new object instance (usually an instance of
cls).

...
If __new__() returns an instance of cls, then the new instance's
__init__() method will be invoked
...
If __new__() does not return an instance of cls, then the new
instance's __init__() method will not be invoked. 


__init__( self[, ...]) 

Called when the instance is created.
...
As a special constraint on constructors, no value may be
returned;



Also, how can a constructor require 'self' as an argument...?
__init__(self, ...)


If the __init__ function is called by the constructor it cannot return a
value.  However if called as a normal function, it can return a value.
__init__ is just a function that gets called by the constructor, which
is __new__.


count = 0
class AClass (object):
def __init__ (self):
self.a = 4

global count
if count > 0:
return 'hello world'

count += 1


a = AClass()

print a.a
print a.__init__()


c:\foo\a.py>
4
hello world



*

The information transmitted is intended only for the person or entity to which 
it is addressed and may contain confidential, proprietary, and/or privileged 
material. Any review, retransmission, dissemination or other use of, or taking 
of any action in reliance upon this information by persons or entities other 
than the intended recipient is prohibited. If you received this in error, 
please contact the sender and delete the material from all computers. GA622


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


  1   2   >