Re: 2.6, 3.0, and truly independent intepreters

2008-10-29 Thread Patrick Stinson
Close, I work currently for EastWest :)

Well, I actually like almost everything else about CPython,
considering my audio work the only major problem I've had is with the
GIL. I like the purist community, and I like the code, since
integrating it on both platforms has been relatively clean, and
required *zero* support. Frankly, with the exception of some windows
deployment issues relating to static linking of libpython and some
extensions, it's been a dream lib to use.

Further, I really appreciate the discussions that happen in these
lists, and I think that this particular problem is a wonderful example
of a situation that requires tons of miscellaneous opinions and input
from all angles - especially at this stage. I think that this problem
has lots of standing discussion and lots of potential solutions and/or
workarounds, and it would be cool for someone to aggregate and
paraphrase that stuff into a page to assist those thinking about doing
some patching. That's probably something that the coder would do
themselves though.

On Fri, Oct 24, 2008 at 10:25 AM, Andy O'Meara <[EMAIL PROTECTED]> wrote:
>>
>> So we are sitting this music platform with unimaginable possibilities
>> in the music world (of which python does not play a role), but those
>> little CPU spikes caused by the GIL at low latencies won't let us have
>> it. AFAIK, there is no music scripting language out there that would
>> come close, and yet we are so close! This is a big deal.
>
>
> Perfectly said, Patrick.  It pains me to know how widespread python
> *could* be in commercial software!
>
> Also, good points about people being longwinded and that "code talks".
>
> Sadly, the time alone I've spend in the last couple days on this
> thread is scary, but I'm committed now, I guess.  :^(   I look at the
> length of the posts of some of these guys and I have to wonder what
> the heck they do for a living!
>
> As I mentioned, however, I'm close to just blowing the whistle on this
> crap and start making CPythonES (as I call it, in the spirit of the
> "ES" in "OpenGLES").  Like you, we just want the core features of
> python in a clean, tidy, *reliable* fashion--something that we can
> ship and not lose sleep (or support hours) over.  Basically, I imagine
> developing an interpreter designed for dev houses like yours and mine
> (you're Ableton or Propellerhead, right?)--a python version of lua, if
> you will.  The nice thing about it is that is could start fresh and
> small, but I have a feeling it would really catch on because every
> commercial dev house would choose it over CPython any day of the week
> and it would be completely disjoint form CPython.
>
> Andy
>
--
http://mail.python.org/mailman/listinfo/python-list


separate shared libraries or different Linux/Unix

2008-10-29 Thread stuntgoat
Hi,

I want to start using Python 2.6 and 3000. I have several questions.

What, in your experiences, is a functionally elegant solution to
installing 2.6 and 3 from source without breaking package dependencies
on your favorite Linux/Unix flavor? Is compiling Python 2.6 and 3.0 on
a *nix development machine and having it work seamlessly as simple as
choosing a particular flavor or are there always going to be package
juggling/mangling/pinning/managing issues affecting the other programs
on the operating system? Is it as simple as choosing a flavor that is
likely to have a Python 3 package available?

Or:

Is there a way to make a copy of shared libraries ( under perhaps /usr/
local/py2.6lib/ and /usr/local/py3lib/ ) so that I can use 2.6 and 3
without causing package problems with other programs within my
operating system? If this seems like a good solution, where can I find
more information about how to implement separate libraries inside the
same OS as appropriate for Python? This might be the better solution
than simply choosing a development flavor of *nix because I am going
to want to install other Python libraries like numpy and matplotlib
from source that might depend on other potentially incompatible shared
libraries than either versions of Python or my Linux/Unix distro ( I
have a feeling I am going to learn how to use ldconfig ).

I have a bit of experience with Debian Etch but I recently garbled my
package management database while compiling the latest version of zlib
for Python 2.6. ( why was I compiling and installing zlib from source?
I was learning another lesson in patience and planning )
--
http://mail.python.org/mailman/listinfo/python-list


Re: 2.6, 3.0, and truly independent intepreters

2008-10-29 Thread Patrick Stinson
Wow, man. Excellent post. You want a job?

The gui could use PyA threads for sure, and the audio thread could use
PyC threads. It would not be a problem to limit the audio thread to
only reentrant libraries.

This kind of thought is what I had in mind about finding a compromise,
especially in the way that PyD would not break old code assuming that
it could eventually be ported.

On Fri, Oct 24, 2008 at 11:02 AM, Glenn Linderman <[EMAIL PROTECTED]> wrote:
> On approximately 10/24/2008 8:42 AM, came the following characters from the
> keyboard of Andy O'Meara:
>>
>> Glenn, great post and points!
>>
>
> Thanks. I need to admit here that while I've got a fair bit of professional
> programming experience, I'm quite new to Python -- I've not learned its
> internals, nor even the full extent of its rich library. So I have some
> questions that are partly about the goals of the applications being
> discussed, partly about how Python is constructed, and partly about how the
> library is constructed. I'm hoping to get a better understanding of all of
> these; perhaps once a better understanding is achieved, limitations will be
> understood, and maybe solutions be achievable.
>
> Let me define some speculative Python interpreters; I think the first is
> today's Python:
>
> PyA: Has a GIL. PyA threads can run within a process; but are effectively
> serialized to the places where the GIL is obtained/released. Needs the GIL
> because that solves lots of problems with non-reentrant code (an example of
> non-reentrant code, is code that uses global (C global, or C static)
> variables – note that I'm not talking about Python vars declared global...
> they are only module global). In this model, non-reentrant code could
> include pieces of the interpreter, and/or extension modules.
>
> PyB: No GIL. PyB threads acquire/release a lock around each reference to a
> global variable (like "with" feature). Requires massive recoding of all code
> that contains global variables. Reduces performance significantly by the
> increased cost of obtaining and releasing locks.
>
> PyC: No locks. Instead, recoding is done to eliminate global variables
> (interpreter requires a state structure to be passed in). Extension modules
> that use globals are prohibited... this eliminates large portions of the
> library, or requires massive recoding. PyC threads do not share data between
> threads except by explicit interfaces.
>
> PyD: (A hybrid of PyA & PyC). The interpreter is recoded to eliminate global
> variables, and each interpreter instance is provided a state structure.
> There is still a GIL, however, because globals are potentially still used by
> some modules. Code is added to detect use of global variables by a module,
> or some contract is written whereby a module can be declared to be reentrant
> and global-free. PyA threads will obtain the GIL as they would today. PyC
> threads would be available to be created. PyC instances refuse to call
> non-reentrant modules, but also need not obtain the GIL... PyC threads would
> have limited module support initially, but over time, most modules can be
> migrated to be reentrant and global-free, so they can be used by PyC
> instances. Most 3rd-party libraries today are starting to care about
> reentrancy anyway, because of the popularity of threads.
>
> The assumptions here are that:
>
> Data-1) A Python interpreter doesn't provide any mechanism to share normal
> data among threads, they are independent... but message passing works.
> Data-2) A Python interpreter could be extended to provide mechanisms to
> share special data, and the data would come with an implicit lock.
> Data-3) A Python interpreter could be extended to provide unlocked access to
> special data, requiring the application to handle the synchronization
> between threads. Data of type 2 could be used to control access to data of
> type 3. This type of data could be large, or frequently referenced data, but
> only by a single thread at a time, with major handoffs to a different thread
> synchronized by the application in whatever way it chooses.
>
> Context-1) A Python interpreter would know about threads it spawns, and
> could pass in a block of context (in addition to the state structure) as a
> parameter to a new thread. That block of context would belong to the thread
> as long as it exists, and return to the spawner when the thread completes.
> An embedded interpreter would also be given a block of context (in addition
> to the state structure). This would allow application context to be created
> and passed around. Pointers to shared memory structures, might be typical
> context in the embedded case.
>
> Context-2) Embedded Python interpreters could be spawned either as PyA
> threads or PyC threads. PyC threads would be limited to modules that are
> reentrant.
>
>
> I think that PyB and PyC are the visions that people see, which argue
> against implementing independent interpreters. PyB isn't truly independent,
> because 

Re: 2.6, 3.0, and truly independent intepreters

2008-10-29 Thread Patrick Stinson
On Fri, Oct 24, 2008 at 12:51 PM, Andy O'Meara <[EMAIL PROTECTED]> wrote:
>
> Another great post, Glenn!!  Very well laid-out and posed!! Thanks for
> taking the time to lay all that out.
>
>>
>> Questions for Andy: is the type of work you want to do in independent
>> threads mostly pure Python? Or with libraries that you can control to
>> some extent? Are those libraries reentrant? Could they be made
>> reentrant? How much of the Python standard library would need to be
>> available in reentrant mode to provide useful functionality for those
>> threads? I think you want PyC
>>
>
> I think you've defined everything perfectly, and you're you're of
> course correct about my love for for the PyC model.  :^)
>
> Like any software that's meant to be used without restrictions, our
> code and frameworks always use a context object pattern so that
> there's never and non-const global/shared data).  I would go as far to
> say that this is the case with more performance-oriented software than
> you may think since it's usually a given for us to have to be parallel
> friendly in as many ways as possible.  Perhaps Patrick can back me up
> there.

And I will.

>
> As to what modules are "essential"...  As you point out, once
> reentrant module implementations caught on in PyC or hybrid world, I
> think we'd start to see real effort to whip them into compliance--
> there's just so much to be gained imho.  But to answer the question,
> there's the obvious ones (operator, math, etc), string/buffer
> processing (string, re), C bridge stuff (struct, array), and OS basics
> (time, file system, etc).  Nice-to-haves would be buffer and image
> decompression (zlib, libpng, etc), crypto modules, and xml. As far as
> I can imagine, I have to believe all of these modules already contain
> little, if any, global data, so I have to believe they'd be super easy
> to make "PyC happy".  Patrick, what would you see you guys using?
>

We don't need anything :) Since our goal is just to use python as a
scripting language/engine to our MIDI application, all we really need
is to make calls to the api that we expose using __builtins__.

You know, the standard python library is pretty siick, but the
syntax, object model, and import mechanics of python itself is an
**equally exportable function** of the code. Funny that I'm lucky
enough to say:

"Screw the extension modules - I just want the LANGUAGE". But, I can't have it.

>
>> > That's the rub...  In our case, we're doing image and video
>> > manipulation--stuff not good to be messaging from address space to
>> > address space.  The same argument holds for numerical processing with
>> > large data sets.  The workers handing back huge data sets via
>> > messaging isn't very attractive.
>>
>> In the module multiprocessing environment could you not use shared
>> memory, then, for the large shared data items?
>>
>
> As I understand things, the multiprocessing puts stuff in a child
> process (i.e. a separate address space), so the only to get stuff to/
> from it is via IPC, which can include a shared/mapped memory region.
> Unfortunately, a shared address region doesn't work when you have
> large and opaque objects (e.g. a rendered CoreVideo movie in the
> QuickTime API or 300 megs of audio data that just went through a
> DSP).  Then you've got the hit of serialization if you're got
> intricate data structures (that would normally would need to be
> serialized, such as a hashtable or something).  Also, if I may speak
> for commercial developers out there who are just looking to get the
> job done without new code, it's usually always preferable to just a
> single high level sync object (for when the job is complete) than to
> start a child processes and use IPC.  The former is just WAY less
> code, plain and simple.
>
>
> Andy
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: Specifying an IP address for outbound connections?

2008-10-29 Thread Martin v. Löwis
erikcw wrote:
> Python seems to default to the main system IP for outbound connections
> (such as urllib), but I want to bind to one of my other IPs for
> outbound connections.
> 
> Any ideas?

Just use the .bind method of a socket to bind it to a specific address.
If you then want to continue to use urllib, you'll have to override a
lot of classes, or monkey-patch, or use, e.g., PyContext to selectively
replace httplib.HTTPConnection.connect.

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


Re: separate shared libraries or different Linux/Unix

2008-10-29 Thread Martin v. Löwis
> What, in your experiences, is a functionally elegant solution to
> installing 2.6 and 3 from source without breaking package dependencies
> on your favorite Linux/Unix flavor? Is compiling Python 2.6 and 3.0 on
> a *nix development machine and having it work seamlessly as simple as
> choosing a particular flavor or are there always going to be package
> juggling/mangling/pinning/managing issues affecting the other programs
> on the operating system? Is it as simple as choosing a flavor that is
> likely to have a Python 3 package available?

If your distribution provides neither Python 2.6 nor 3.0, then there
shouldn't be any package juggling/mangling/pinning/managing issues
when you build it yourself. Just install it in /usr/local, and be done.

If your distribution does provide those versions, don't build them
yourself at all - just use the distribution ones, and be done.

> Is there a way to make a copy of shared libraries ( under perhaps /usr/
> local/py2.6lib/ and /usr/local/py3lib/ ) so that I can use 2.6 and 3
> without causing package problems with other programs within my
> operating system?

You don't need to make copies of shared libraries. Just install
with a prefix of /usr/local, and there won't be any shared library
conflicts. If something invokes the "python" binary without explicit
path, and /usr/local/bin precedes /usr/bin in the path, then it will
pick up your Python interpreter. If you don't want it to, "make
altinstall" instead of "make install" for Python.

> If this seems like a good solution, where can I find
> more information about how to implement separate libraries inside the
> same OS as appropriate for Python?

There is not much information that you need. It Just Works.

> This might be the better solution
> than simply choosing a development flavor of *nix because I am going
> to want to install other Python libraries like numpy and matplotlib
> from source that might depend on other potentially incompatible shared
> libraries than either versions of Python or my Linux/Unix distro ( I
> have a feeling I am going to learn how to use ldconfig ).

Just don't even think of passing --enable-shared to Python's configure,
and it will all work fine, and you won't need to use ldconfig.

> I have a bit of experience with Debian Etch but I recently garbled my
> package management database while compiling the latest version of zlib
> for Python 2.6. ( why was I compiling and installing zlib from source?
> I was learning another lesson in patience and planning )

Installing *other* stuff (but Python) from source is something that you
should completely avoid. Instead of installing zlib, you should have
just installed Debian's zlib1g-dev package. Likewise for any other
header files that you will need. The libraries provided by Debian are
sufficient for building Python 2.6 with all extension modules (that
can possibly work on Linux).

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


Re: How to get high precision timer in python?

2008-10-29 Thread James Mills
On Wed, Oct 29, 2008 at 4:22 PM, Tim Roberts <[EMAIL PROTECTED]> wrote:
> I'm not sure you understood what he was saying.  time.time() and
> time.clock() can both be used for elapsed timing, but because of a fluke of
> implementation, time.time() is more precise on Linux, and time.clock() is
> more precise on Windows.

That's exactly what I was getting at, only I couldn't
have been bothered explaining why! :)

--JamesMills


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


Re: @property decorator doesn't raise exceptions

2008-10-29 Thread Rafe
OT... Sorry about he spam.
Thanks for taking the time to post this Duncan.

I had the same thought. I have posted to this list before but never
experienced anything like this wait. I figured it was possible that I
hit "Reply to Author" the first time so I sent it again. I waited
about 8 hours before sending the third time (and I posted to
GoogleGroups support as well). Even then I didn't see my original
post. I'm surprised it took so long to update. Next time I'll just
make sure the post was made successfully and wait...as long as it
takes.


Cheers,

- Rafe


On Oct 26, 4:23 pm, Duncan Booth <[EMAIL PROTECTED]> wrote:
> Rafe<[EMAIL PROTECTED]> wrote:
> > Peter Oten pointed me in the right direction. I tried to reply to his
> > post 2 times and in spite of GoogleGroups reporting the post was
> > successful, it never showed up.
>
> This is the third variant on your message that has shown up in the
> newsgroup.
>
> Please be aware that messages take time to propogate through usenet: don't
> repost just because Google groups hasn't yet got around to displaying your
> message. If it says the post was successful then the post was successful.
> Just be patient a bit longer for it to become visible to you.

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


Re: @property decorator doesn't raise exceptions

2008-10-29 Thread Rafe
On Oct 27, 2:47 pm, Peter Otten <[EMAIL PROTECTED]> wrote:
> Rafewrote:
> > Can anyone explain why this is happening?
>
> When an attribute error is raised that is an indication that the requested
> attribute doesn't exist, and __getattr__() must be called as a fallback.
>
> > I can hack a work-around,
> > but even then I could use some tips on how to raise the 'real'
> > exception so debugging isn't guesswork.
>
> Look at the problem again, maybe you can find a solution without
> __getattr__() and use only properties.
>
> Otherwise you have to wrap your getter with something like
>
> try:
>     ...
> except AttributeError:
>     raise BuggyProperty, None, original_traceback
>
> If you put that functionality into a decorator you get:
>
> import sys
>
> class BuggyProperty(Exception):
>     pass
>
> def safe_getter(get):
>     def safe_get(self):
>         try:
>             return get(self)
>         except AttributeError:
>             t, e, tb = sys.exc_info()
>             raise BuggyProperty("AttributeError in getter %s(); "
>                             "giving original traceback"
>                             % get.__name__), None, tb
>     return property(safe_get)
>
> class A(object):
>     @safe_getter
>     def attr(self):
>         return self.m(3)
>
>     def m(self, n):
>         if n > 0:
>             return self.m(n-1)
>         raise AttributeError("it's a bug")
>
>     def __getattr__(self, name):
>         return "<%s>" % name
>
> A().attr
>
> Peter


Thanks for the idea Peter. What confuses me is why this only happens
to @Property (and I assume other decorator related bindings?). Does it
have something to do with the way the class gets built? 'Normal'
attributes will raise AttributeErrors as expected, without triggering
__getattr__(). Considering this is a built-in decorator, it would be
nice if this behavior was fixed if possible.

Unfortunately, I need __getattr__() because my class is a wrapper (it
is delegating calls to another object when attributes aren't found in
the class). As a hack, I am testing the passed attr name against the
instance, class and super-class attributes. If there is a match, I
assume it is an error and raise an exception which points the
developer in the right direction. It isn't ideal, but it helps.

You're code is better, as it displays the 'real' traceback, but I need
to know more about the effects of using an exception which is not an
AttrbiuteError. Which brings me to my next question...

In case it isn't obvious, I'm fairly new to Python (and this level of
programming in general). I've been wondering about the optimal use of
custom exceptions. Up until now, I've been sticking to the built-in
exceptions, which seem to work in 90% of situations. Are you aware of
any resources which talk about this aspect of programming (more about
theory than code)?


Thanks again,

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


how to use logging module to log an object like print()

2008-10-29 Thread davy zhang
mport logging
import pickle


# create logger
logger = logging.getLogger("simple_example")
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s
- %(message)s ")
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)

d = {'key':'msg','key2':'msg2'}

# "application" code
logger.debug("debug message",d)#can not do this
logger.info("info message")
logger.warn("warn message")
logger.error("error message")
logger.critical("critical message")
--
http://mail.python.org/mailman/listinfo/python-list


Re: Deviation from object-relational mapping (pySQLFace)

2008-10-29 Thread Bruno Desthuilliers

[EMAIL PROTECTED] a écrit :

On okt. 22, 06:27, huy <[EMAIL PROTECTED]> wrote:

(snip)

Best use of XML for SQL generation/use I have seen is Ibatis SQLMAPS.

This focuses on the right things i.e queries and mapping values to/
from objects.

It would be great if python had such a tool.


I have looked into Ibatis SQLMAPS. It claims that its biggest
advantage is simplicity over other frameworks and ORMs.


Well... "Simplicity" is here to be taken relatively to the Java world 
standards, I guess !-)

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


Car Loans banned in 7 nations

2008-10-29 Thread blixs9

GMAC Financial Services said Tuesday that it no longer will make auto
loans in seven European countries, citing troubles in the lending
industry that have forced it to adjust its operations.

The financing arm of Detroit-based General Motors Corp. said as of
Nov. 1, it no longer will originate retail loans in the Czech
Republic, Finland, Greece, Norway, Portugal, Slovakia and Spain.

GMAC said it will assess the implications of the tough credit markets
in those countries and in its markets in Hungary and Denmark.

Gina Proia, a spokeswoman for GMAC, said the company's moves in Europe
are a result of a shortage of capital

http://quickloans.aokhost.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: how to use logging module to log an object like print()

2008-10-29 Thread davy zhang
thanks so much , I ganna check the formatter str for more info:)

On Wed, Oct 29, 2008 at 5:10 PM, Diez B. Roggisch <[EMAIL PROTECTED]> wrote:
> davy zhang schrieb:
>>
>> mport logging
>> import pickle
>>
>>
>> # create logger
>> logger = logging.getLogger("simple_example")
>> logger.setLevel(logging.DEBUG)
>> # create console handler and set level to debug
>> ch = logging.StreamHandler()
>> ch.setLevel(logging.DEBUG)
>> # create formatter
>> formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s
>> - %(message)s ")
>> # add formatter to ch
>> ch.setFormatter(formatter)
>> # add ch to logger
>> logger.addHandler(ch)
>>
>> d = {'key':'msg','key2':'msg2'}
>>
>> # "application" code
>> logger.debug("debug message",d)#can not do this
>
> logger.debug("yes you can: %r", d)
>
>
> Diez
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


[2.5.1] "UnicodeDecodeError: 'ascii' codec can't decode byte"?

2008-10-29 Thread Gilles Ganault
Hello

I'm getting this error while downloading and parsing web pages:

=
title = m.group(1)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position
48: ordinal  not in range(128)
=

>From what I understand, it's because some strings are Unicode, and
hence contain characters that are illegal in ASCII.

Does someone know how to solve this error?

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


Re: how to use logging module to log an object like print()

2008-10-29 Thread Diez B. Roggisch

davy zhang schrieb:

mport logging
import pickle


# create logger
logger = logging.getLogger("simple_example")
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s
- %(message)s ")
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)

d = {'key':'msg','key2':'msg2'}

# "application" code
logger.debug("debug message",d)#can not do this


logger.debug("yes you can: %r", d)


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


Re: @property decorator doesn't raise exceptions

2008-10-29 Thread Peter Otten
Rafe wrote:

> Thanks for the idea Peter. What confuses me is why this only happens
> to @Property (and I assume other decorator related bindings?). Does it
> have something to do with the way the class gets built? 'Normal'
> attributes will raise AttributeErrors as expected, without triggering
> __getattr__(). Considering this is a built-in decorator, it would be
> nice if this behavior was fixed if possible.

Normal attributes either exist, and then they don't raise an AttributeError,
or they don't exist, and then __getattr__() *is* triggered. The problem has
nothing to do with decorators. It is just that properties invoke custom
code, and Python currently has no way of finding out whether an
AttributeError was raised accidentally by a buggy getter or whether it is
meant to signal that the attribute wasn't found in the class  hierarchy and
now should be calculated by __getattr__().

I guess (without looking into the C source) that it could be changed to meet
your intuition but that it would complicate the implementation.

Peter

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


free IDE with removing import and refactoring

2008-10-29 Thread pihentagy
Hi!

I am looking for a python IDE which can remove my unused imports, and
can do basic refactoring under windows.

Can somebody advice me such an IDE?

I have played with eclipse and netbeans, but I cannot find such a
functionality (but maybe I looked it over).

Besides, an installation howto would be useful for me.

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


Re: [2.5.1] "UnicodeDecodeError: 'ascii' codec can't decode byte"?

2008-10-29 Thread Ulrich Eckhardt
Gilles Ganault wrote:
> I'm getting this error while downloading and parsing web pages:
> 
> =
> title = m.group(1)
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position
> 48: ordinal  not in range(128)
> =
> 
> From what I understand, it's because some strings are Unicode, and
> hence contain characters that are illegal in ASCII.

You just need to use a codec according to the encoding of the webpage. Take
a look at 
  http://wiki.python.org/moin/Python3UnicodeDecodeError
It is about Python 3, but the principles apply nonetheless. In any case,
throwing the error at a websearch will turn up lots of solutions.

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

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


Export email contact

2008-10-29 Thread xspiritualoraclex
Hi everyone :)
I'd like export email contact from gmail, hotmail and yahoo account.
How can I do that?

thanks and excuse me for my english :P
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to get high precision timer in python?

2008-10-29 Thread Steve Holden
甜瓜 wrote:
> ^_^ Oh!  I did not read the document for time.clock before,
> and supposed it was same with time.time().
> 
> Thank you very much.
> 
If you are using wxPython then you should probably see whether a wxTimer
would meet your needs.

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Finding the instance reference of an object

2008-10-29 Thread Antoon Pardon
On 2008-10-17, Joe Strout <[EMAIL PROTECTED]> wrote:

>> Python's assignment semantics (as opposed to its "object handling, a
>> term for which I have no referent) are not the same as those of, say  
>> C.
>
> They are, though.  The only difference you've pointed out is that  
> *numbers* are different in Python vs. C, and that's an internal  
> implementation detail I was blissfully unaware of until this  
> discussion.  (I'm grateful to know it, but it really doesn't matter in  
> day-to-day coding.)

No they are not. An assignment in Python is like making an (new) 
alias/reference,
while an asignment in C is copying the content of one variable into another.

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


Re: [Novice]Installing eric4 with python 2.6

2008-10-29 Thread Steve Holden
Saurabh Agrawal wrote:
> Hi,
> 
> I am fairly new to python. Coming from a world of IDEs, I wanted a
> fancier one than IDLE. Hence, I downloaded eric4. When I tried to
> compile it, it asked for PyQt4, which I again downloaded (exe for
> Windows XP) and installed it. It seems to be properly installed as I can
> invoke "import PyQt4" from IDLE without any complaints.
> 
> However, when I try to install eric4, I am getting the following error:
> 
> D:\Python26\eric4-4.2.2a>python install.py
> Sorry, please install PyQt4.
> Error: Module use of python25.dll conflicts with this version of Python.
> 
> So, won't eric4 work with python 2.6? I have searched on the net for
> this, but am unable to find any solution.
> 
> I am new to python, so please excuse if this has an obvious solution.
> Thanks.
> 
> OS: Windows XP SP2.
> 
Unfortunately when a new release comes out package authors don't always
rush to meet the demand for their software by providing a release for
the new version.

If the download site doesn't specifically offer a 2.6-compatible version
that's likely the problem here, sorry :(

In the meantime take a look at WingIDE - there's a free or low-cost
version that might meet your needs, and I'd be very surprised if they
weren't 2.6-compatible on the day of 2.6's release.

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Is ctypes appropriate in my case?

2008-10-29 Thread dudeja . rajat
Hi,

I've a dll and its header file that controls an hardware. I want to write a
wrapper for this dll in Python.
What is the best way that I can write a wrapper?


I know ctypes modules and have used it before. As far as I know ctypes is
only used to call the dll functions in a python module. This is not
appropriate for writing a wrapper.
Please suggest some way to write a wrapper.


Thanks and regards,
Rajat
--
http://mail.python.org/mailman/listinfo/python-list


Re: parsing MS word docs -- tutorial request

2008-10-29 Thread Okko Willeboordsed

Get a copy of;  Python Programming on Win32, ISBN 1-56592-621-8
Use Google and VBA for help

[EMAIL PROTECTED] wrote:

All,

I am trying to write a script that will parse and extract data from a
MS Word document.  Can / would anyone refer me to a tutorial on how to
do that?  (perhaps from tables).  I am aware of, and have downloaded
the pywin32 extensions, but am unsure of how to proceed -- I'm not
familiar with the COM API for word, so help for that would also be
welcome.

Any help would be appreciated.  Thanks for your attention and
patience.

::bp::

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


Re: Python memory usage

2008-10-29 Thread [EMAIL PROTECTED]
On Oct 21, 5:19 pm, Rolf Wester <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I have the problem that with long running Python scripts (many loops)
> memory consumption increases until the script crashes. I used the
> following small script to understand what might happen:
>


AFAIK, python uses malloc behind the scenes to allocate memory. From
the malloc man page...

"The  malloc() and free() functions provide a simple, general-purpose
memory allocation package. The malloc() function returns a pointer to
a block of at least size bytes suitably aligned for any use. If the
space assigned by malloc() is overrun, the results are undefined.

The argument to free() is a pointer to a block previously allocated by
malloc(), calloc(), or realloc(). After free() is executed, this space
is made available for further  allocation by the application, though
not returned to the system. Memory is returned to the system only
upon  termination of  the  application.  If ptr is a null pointer, no
action occurs. If a random number is passed to free(), the results are
undefined."

HTH,

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


Amharic Question Answering

2008-10-29 Thread seid muhie


Dear All
I am new to Python. Am new to NLP(NAtural LAnguage Processing) too.
But take the initiation to develop Autamatic Amharic Question Answering as part 
of my MSc. degree partial fuflfilment thesis work.
Now I need,
1. If python could help me in doing the QA system
2. if any QA system been developed in Python.
am going through the NLTK book with its nice examples. That is why I seemed to 
prefer python as a development tool for my QA.
I appriciate any help
thanks all
Seid M
Addis Ababab University
Ethiopia




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


Re: [Tkinter-discuss] WxPython -> Tkinter

2008-10-29 Thread Guilherme Polo
On 10/29/08, Olrik Lenstra <[EMAIL PROTECTED]> wrote:
> Hello everyone,
>
> A while ago I joined the Tutor mailing list, and they helped me out with a
> question regarding wxPython.
> Now however, I have tried a program in Tkinter and I would like to see if
> there is a similar command to "wx.SafeYield(self, True)".

It will be a combination of commands, not a single one. Initially I
considered this as "probably without solution", since tcl acquired a
yield command just in the 8.6a3 release, but then I looked at
wx.SafeYield code and apparently it is possible to replicate it.

Here is an initial cut, it is very possible to contain something not
equivalent to wx.SafeYield (besides it could be improved):


import ttk

inside_tkyield = False
disabled_wins = {}

def safe_yield(window, only_if_needed=False):
window_disabler(window)

try:
return tk_yield(window, only_if_needed)
finally:
for widget, flags in disabled_wins.iteritems():
ttk.Widget.state(widget, flags)
disabled_wins.clear()


def window_disabler(window):
widgets = window.children.values()
widgets.append(window)

for widget in widgets:
if widget.instate(['!disabled']):
prev_flags = widget.state(['disabled'])
disabled_wins[widget] = prev_flags


def tk_yield(window, only_if_needed=False):
# wx implements this differently based on the backend it is using
global inside_tkyield
if inside_tkyield:
if not only_if_needed:
raise RuntimeError("safe_yield called recursively")

return False

inside_tkyield = True;

window.update()
window.update_idletasks()

inside_tkyield = False;

return True


Note that this depends on ttk widgets
(http://pypi.python.org/pypi/pyttk) since it uses widget.state to
disable and reenable the widgets. On windows the "wm" command supports
disabling the entire window, so it is easier if you can use it.

>  Below is a copy of the message to the tutor list.
>
> > Dear Mailing list,
> >
> > a while ago a few of you helped me solve an issue I had with a GUI / scan
> > program that I made.
> > The problem was that when I tried to move the frame it would hang until
> the
> > scan was finished.
> > To solve this I had to add "wx.SafeYield(self, True)" to the scan and the
> > GUI wouldn't hang any more.
> > Now I have redone the program and have written it with Tkinter instead of
> > WxPython.
> >
> > So is there a similar command for Tkinter as there is for WxPython?
> >
> > Thanks in advance.
> > Regards,
> > Olrik
> >
>
> ___
>  Tkinter-discuss mailing list
>  [EMAIL PROTECTED]
> http://mail.python.org/mailman/listinfo/tkinter-discuss
>
>


-- 
-- Guilherme H. Polo Goncalves
--
http://mail.python.org/mailman/listinfo/python-list


Re: [Tkinter-discuss] WxPython -> Tkinter

2008-10-29 Thread Guilherme Polo
On 10/29/08, Guilherme Polo <[EMAIL PROTECTED]> wrote:
> On 10/29/08, Olrik Lenstra <[EMAIL PROTECTED]> wrote:
>  > Hello everyone,
>  >
>  > A while ago I joined the Tutor mailing list, and they helped me out with a
>  > question regarding wxPython.
>  > Now however, I have tried a program in Tkinter and I would like to see if
> > ...

oops, wrong list.


-- 
-- Guilherme H. Polo Goncalves
--
http://mail.python.org/mailman/listinfo/python-list


Re: [Novice]Installing eric4 with python 2.6

2008-10-29 Thread Phil Thompson
On Wed, 29 Oct 2008 06:51:10 -0400, Steve Holden <[EMAIL PROTECTED]>
wrote:
> Saurabh Agrawal wrote:
>> Hi,
>> 
>> I am fairly new to python. Coming from a world of IDEs, I wanted a
>> fancier one than IDLE. Hence, I downloaded eric4. When I tried to
>> compile it, it asked for PyQt4, which I again downloaded (exe for
>> Windows XP) and installed it. It seems to be properly installed as I can
>> invoke "import PyQt4" from IDLE without any complaints.
>> 
>> However, when I try to install eric4, I am getting the following error:
>> 
>> D:\Python26\eric4-4.2.2a>python install.py
>> Sorry, please install PyQt4.
>> Error: Module use of python25.dll conflicts with this version of Python.
>> 
>> So, won't eric4 work with python 2.6? I have searched on the net for
>> this, but am unable to find any solution.
>> 
>> I am new to python, so please excuse if this has an obvious solution.
>> Thanks.
>> 
>> OS: Windows XP SP2.
>> 
> Unfortunately when a new release comes out package authors don't always
> rush to meet the demand for their software by providing a release for
> the new version.
> 
> If the download site doesn't specifically offer a 2.6-compatible version
> that's likely the problem here, sorry :(

PyQt supported Python 2.6 on the day it was released.

A snapshot of the PyQt Windows installer for Python 2.6 can be downloaded
from the same page as you downloaded the installer for Python 2.5.

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


is there a way to access postgresql in python3.0rc1

2008-10-29 Thread davy zhang
I'm currently on a  project, it could last for at least 1 or 2 years.
so I choose python3 as server side programing language.
All I found on are python2.x ready libraries, Is there any library is
python3.0 ready? or just under alpha ,beta or something, I don't much
features, just basic functions are OK

Thanks for any hint~
--
http://mail.python.org/mailman/listinfo/python-list


Re: Improving interpreter startup speed

2008-10-29 Thread Lie
On Oct 27, 2:36 pm, Paul Rubin  wrote:
> "James Mills" <[EMAIL PROTECTED]> writes:
> > Heaven knows! I hardly think invoking hundreds
> > and possibly thousands of short-lived python
> > interpreters to be an optimal solution that may
> > have spawned this particular thread.
>
> It's not optimal but it is very common (CGI for example).

CGI? When you're talking about CGI, network traffic is simply the
biggest bottleneck, not something like python interpreter startup
time. Also, welcome to the 21st century, where CGI is considered as an
outdated protocol.
--
http://mail.python.org/mailman/listinfo/python-list


Re: free IDE with removing import and refactoring

2008-10-29 Thread [EMAIL PROTECTED]
HI!
Did you tried the ERIC python IDE?

Rew

pihentagy írta:
> Hi!
>
> I am looking for a python IDE which can remove my unused imports, and
> can do basic refactoring under windows.
>
> Can somebody advice me such an IDE?
>
> I have played with eclipse and netbeans, but I cannot find such a
> functionality (but maybe I looked it over).
>
> Besides, an installation howto would be useful for me.
>
> Thanks
> Gergo
--
http://mail.python.org/mailman/listinfo/python-list


Re: how to use logging module to log an object like print()

2008-10-29 Thread Steve Holden
Diez B. Roggisch wrote:
> davy zhang schrieb:
>> mport logging
>> import pickle
>>
>>
>> # create logger
>> logger = logging.getLogger("simple_example")
>> logger.setLevel(logging.DEBUG)
>> # create console handler and set level to debug
>> ch = logging.StreamHandler()
>> ch.setLevel(logging.DEBUG)
>> # create formatter
>> formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s
>> - %(message)s ")
>> # add formatter to ch
>> ch.setFormatter(formatter)
>> # add ch to logger
>> logger.addHandler(ch)
>>
>> d = {'key':'msg','key2':'msg2'}
>>
>> # "application" code
>> logger.debug("debug message",d)#can not do this
> 
> logger.debug("yes you can: %r", d)
> 
One deficiency of this approach, however, is that the string formatting
is performed even when no logging is required, thereby wasting a certain
amount of effort on unnecessary formatting.

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: [2.5.1] "UnicodeDecodeError: 'ascii' codec can't decode byte"?

2008-10-29 Thread Steve Holden
Ulrich Eckhardt wrote:
> Gilles Ganault wrote:
>> I'm getting this error while downloading and parsing web pages:
>>
>> =
>> title = m.group(1)
>> UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position
>> 48: ordinal  not in range(128)
>> =
>>
>> From what I understand, it's because some strings are Unicode, and
>> hence contain characters that are illegal in ASCII.
> 
> You just need to use a codec according to the encoding of the webpage. Take
> a look at 
>   http://wiki.python.org/moin/Python3UnicodeDecodeError
> It is about Python 3, but the principles apply nonetheless. In any case,
> throwing the error at a websearch will turn up lots of solutions.
> 
I won't believe that statement is producing the error until I see a
traceback. As far as I'm aware the re module can handle Unicode. Getting
a UnicodeDecodeError in an assignment would be unusual to say the least.
Though it's not, I suppose, impossible that calling the .group() method
of a match object might, it seems unlikely.

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Fastest way to convert sql result into a dict or list ?

2008-10-29 Thread [EMAIL PROTECTED]
Hello,

I'm trying to find the fastest way to convert an sql result into a
dict or list.
What i mean, for example:
my sql result:
contact_id, field_id, field_name, value
sql_result=[[1, 1, 'address', 'something street'],
 [1, 2, 'telnumber', '11'],
 [1, 3, 'email', '[EMAIL PROTECTED]'],
 [2, 1, 'address','something stree'],
 [2, 3, 'email','[EMAIL PROTECTED]']]
the dict can be:
dict={1:['something street', '11' ,
'[EMAIL PROTECTED]'],
2:['something street', '', '[EMAIL PROTECTED]' ]}
or a list can be:
list=[[1,'something street', '11' ,
'[EMAIL PROTECTED]'],
   [2,'something street', '', '[EMAIL PROTECTED]' ]]

I tried to make a dict, but i think it is slower then make a list, and
i tried the "one lined for" to make a list, it's look like little bit
faster than make a dict.

def empty_list_make(sql_result):
return [ [line[0],"", "", ""]   for line in sql_result]

than fill in the list with another for loop.
I hope there is an easyest way to do something like this ??
any idea ?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Improving interpreter startup speed

2008-10-29 Thread James Mills
On Wed, Oct 29, 2008 at 9:43 PM, Lie <[EMAIL PROTECTED]> wrote:
> On Oct 27, 2:36 pm, Paul Rubin  wrote:
>> It's not optimal but it is very common (CGI for example).
>
> CGI? When you're talking about CGI, network traffic is simply the
> biggest bottleneck, not something like python interpreter startup
> time. Also, welcome to the 21st century, where CGI is considered as an
> outdated protocol.

That's right. That's why we have WSGI. That's
why we built mod_wsgi for Apache. Hell taht's
why we actually have really nice Web Frameworks
such as: CherryPy, Pylons, Paste, etc. They
perform pretty damn well!

--JamesMills

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


Re: question about the textwrap module

2008-10-29 Thread Sion Arrowsmith
Gabriel Genellina <[EMAIL PROTECTED]> wrote:
>py> text = 'This  is some \t   text with  multiple\n\n  spaces.'
>py> import re
>py> re.sub(r'\s+', ' ', text)
>'This is some text with multiple spaces.'

py> ' '.join(text.split())
'This is some text with multiple spaces.'

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
   "Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
--
http://mail.python.org/mailman/listinfo/python-list


Re: Improving interpreter startup speed

2008-10-29 Thread J. Clifford Dyer
Maybe Ruby is the right language for your need.

Just sayin'.



On Sun, 2008-10-26 at 13:19 +, Pedro Borges wrote:
> The scripts i need to run but be executed with no apparent delay  
> specially when the text transforms are simple.
> 
> 
> On Oct 26, 2008, at 11:13 AM, James Mills wrote:
> 
> > On Sun, Oct 26, 2008 at 11:23 AM, BJörn Lindqvist  
> > <[EMAIL PROTECTED]> wrote:
> >> How are you getting those numbers? 330 μs is still pretty fast,  
> >> isn't
> >> it? :) Most disks have a seek time of 10-20 ms so it seem implausible
> >> to me that Ruby would be able to cold start in 47 ms.
> >
> > $ time python -c "pass"
> >
> > real0m0.051s
> > user0m0.036s
> > sys 0m0.008s
> >
> > $ time python3.0 -c "pass"
> >
> > real0m0.063s
> > user0m0.048s
> > sys 0m0.004s
> >
> > And yes I agree. the CPython interpreter startup times is
> > a stupid thing to be worrying about, especially since that
> > is never the bottleneck.
> >
> > Python loads plenty fast enough!
> >
> > --JamesMills
> >
> > -- 
> > --
> > -- "Problems are solved by method"
> 
> --
> http://mail.python.org/mailman/listinfo/python-list

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


PyCon 2009 - Call for tutorials nearing the end

2008-10-29 Thread Greg Lindstrom
The period for submitting tutorial proposals for Pycon 2009 (US) is open and
will continue through Friday, October 31th. This year features two
"pre-conference" days devoted to tutorials on Wednesday March 25 & Thursday
March 26 in Chicago. This allows for more classes than ever.

Tutorials are 3-hours long on a specific topic of your choice. Last year we
featured classes on Learning Python, Web Development, Scientific Computing,
and many more (there was particular interest in "Intermediate" level
classes).  Class size varied from 10 to over 60 students. The extended time
spent in class allows teachers to cover a lot of material while
allowing forinteraction with students.

The full Call for Tutorial Proposals, including submission details, an
example proposal as well as a template, is available at <
http://us.pycon.org/2009/tutorials/proposals/>.

Tutorial selections will be announced in early December to give you time to
prepare your class and PyCon will compensate instructors US$1,500 per
tutorial.

If you have any questions, please contact [EMAIL PROTECTED]

Greg Lindstrom
Tutorial Coordinator, PyCon 2009 (US)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Improving interpreter startup speed

2008-10-29 Thread bearophileHUGS
Terry Reedy:
> The current developers, most of whom use Python daily, [...]

Thank you for bringing some light in this thread so filled with worse
than useless comments.

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


Re: how to use logging module to log an object like print()

2008-10-29 Thread Vinay Sajip
On Oct 29, 11:24 am, Steve Holden <[EMAIL PROTECTED]> wrote:

>
> One deficiency of this approach, however, is that the string formatting
> is performed even when nologgingis required, thereby wasting a certain
> amount of effort on unnecessary formatting.
>

Though you can mitigate this using the pattern:

if logger.isEnabledFor(logging.DEBUG):
logger.debug("Message with variable data which may be expensive:
%s", expensive_call())

Which will only make the expensive_call() and formatting when the
logging call will actually do something.

Regards,

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


Re: 2.6, 3.0, and truly independent intepreters

2008-10-29 Thread Paul Boddie
On 28 Okt, 21:03, Rhamphoryncus <[EMAIL PROTECTED]> wrote:
> * get a short-term bodge that works, like hacking the 3rd party
> library to use your shared-memory allocator.  Should be far less work
> than hacking all of CPython.

Did anyone come up with a reason why shared memory couldn't be used
for the purpose described by the inquirer? With the disadvantages of
serialisation circumvented, that would leave issues of contention, and
on such matters I have to say that I'm skeptical about solutions which
try and make concurrent access to CPython objects totally transparent,
mostly because it appears to be quite a lot of work to get right (as
POSH illustrates, and as your own safethread work shows), and also
because systems where contention is spread over a large "surface" (any
object can potentially be accessed by any process at any time) are
likely to incur a lot of trouble for the dubious benefit of being
vague about which objects are actually being shared.

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


Re: Tkinter: How to get Label wraplength functionality in Text Box

2008-10-29 Thread Mudcat
Sounds like that would work really well. Problem is I can't get it to
work.

...
AttributeError: Text instance has no attribute 'count'
...

I think my usage is correct. I don't have any params at the moment,
but I was just checking the functionality.

numlines = widget.count()

According to the Tk 8.5 documentation it's used just like a normal
command.
WIDGET COMMAND
pathName count ?options? index1 index2
-chars
-displaychars
-displayindices
-displaylines
-indices
-lines
-xpixels
-ypixels


As for the environment, I thought I had everything set up correctly.
I've got the latest stable version of Python 2.6 (r26:66721, Oct  2
2008, 11:35:03). I'm implementing the TTK wrappers to access Tk 8.5.
Although when I check the wrapper I don't see any mods to the Text
Box. I also don't see this option in the Tkinter.py file.

Is there something else I need to add to access this new feature?


On Oct 28, 6:51 pm, "Guilherme Polo" <[EMAIL PROTECTED]> wrote:

>
> Are you looking for something like the new "count" command for the
> text widget in tk 8.5 ? "count" can count the number of logical lines
> (irrespective of wrapping), display lines (counts one for each time a
> line wraps) and some other things.
>
> >  Thanks
>
> > --
> >  http://mail.python.org/mailman/listinfo/python-list
>
> --
> -- Guilherme H. Polo Goncalves

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


Re: Finding the instance reference of an object

2008-10-29 Thread Grant Edwards
On 2008-10-29, Antoon Pardon <[EMAIL PROTECTED]> wrote:
> On 2008-10-17, Joe Strout <[EMAIL PROTECTED]> wrote:
>
>>> Python's assignment semantics (as opposed to its "object
>>> handling, a term for which I have no referent) are not the
>>> same as those of, say C.
>>
>> They are, though.  The only difference you've pointed out is
>> that *numbers* are different in Python vs. C, and that's an
>> internal implementation detail I was blissfully unaware of
>> until this discussion.  (I'm grateful to know it, but it
>> really doesn't matter in day-to-day coding.)
>
> No they are not. An assignment in Python is like making an
> (new) alias/reference, while an asignment in C is copying the
> content of one variable into another.

That's been pointed out over and over and over, but some people
seem unable or unwilling to grasp the difference.

-- 
Grant Edwards   grante Yow! Someone in DAYTON,
  at   Ohio is selling USED
   visi.comCARPETS to a SERBO-CROATIAN
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to get an object's name as a string?

2008-10-29 Thread ShanMayne

> The simplest and best option here is to store the objects in a
> dictionary with their keys being the generated names.

Thanks. Indeed Alex, that may well be the simplest way, to have an
overarching Dictionary of references/names and objects.

However this does not help me to use the reference/name of an object I
imported instead of created.

#-#

There has been much debate over terminology but I was under the
impression that there was a simple input-output defined task in
question.

Inherently a the variable/name/reference maps to the object it was
assigned to. The question is can one map it the other way? If there
are multiple assignments to the same object can they be listed? This
applies to the "names" object atributes as well.

Joe has mentioned that I shouldn't need to do this and should perhaps
rethink my problem formulation. Nonetheless, I would still like to
know how it might be done.

Thanks
Shannon

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


Re: Improving interpreter startup speed

2008-10-29 Thread BJörn Lindqvist
2008/10/27 James Mills <[EMAIL PROTECTED]>:
> On Mon, Oct 27, 2008 at 5:40 PM, David Cournapeau <[EMAIL PROTECTED]> wrote:
>> Depends on the tool: build tool and source control tools are example
>> it matters (specially when you start interfaciing them with IDE or
>> editors). Having fast command line tools is an important feature of
>> UNIX, and if you want to insert a python-based tool in a given
>> pipeline, it can hurt it the pipeline is regularly updated.
>
> Fair enough. But still:
> 0.5s old startup is fast enough
> 0.08s warm startup is fast enough.
>
> Often "fast enough" is "fast enough"

Nope, when it comes to start up speed the only thing that is fast
enough is "instantly." :) For example, if I write a GUI text editor in
Python, the total cold start up time might be 1500 ms on a cold
system. 750 ms for the interpreter and 750 ms for the app itself.
However, if I also have other processes competing for IO, torrent
downloads or compilations for example, the start up time grows
proportional to the disk load. For example, if there is 50% constant
disk load, my app will start in 1.5 / (1 - 0.5) = 3 seconds (in the
best case, assuming IO access is allocated as efficiently as possible
when the number of processes grows, which it isn't). If the load is
75%, the start time becomes 1.5 / (1 - 0.75) = 6 seconds.

Now if the Python interpreters start up time was 200 ms, by apps start
up time with 75% disk load becomes (0.2 + 0.75) / (1 - 0.75) = 3.8
seconds which is significantly better.


-- 
mvh Björn
--
http://mail.python.org/mailman/listinfo/python-list


Re: Improving interpreter startup speed

2008-10-29 Thread Paul Boddie
On 29 Okt, 13:55, [EMAIL PROTECTED] wrote:
> Terry Reedy:
>
> > The current developers, most of whom use Python daily, [...]
>
> Thank you for bringing some light in this thread so filled with worse
> than useless comments.

Indeed. Observing that CGI is old-fashioned, aside from not really
helping people who choose or are obliged to use that technology,
doesn't detract from arguments noting various other areas where start-
up performance is worth improving (in build environments, for example,
as someone suggested), nor does it address the more basic issue of why
Python issues as many system calls as it does when starting up,
something which has actually been looked at by the core developers
(indicating that it isn't a waste of time as one individual claimed)
as well as by developers in other projects where application start-up
time has been criticised.

Although people are using multi-GHz CPUs on the desktop, there are
environments where it is undesirable for Python to go sniffing around
the filesystem just for the fun of it. In fact, various embedded
projects have employed some kind of daemon for Python in order to get
Python-based programs started quickly enough - something that I'm sure
plenty of people would ridicule if, say, Java were involved. The
better solution would just involve improving the obvious: the start-up
performance.

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


Re: How to get an object's name as a string?

2008-10-29 Thread alex23
On Oct 29, 11:31 pm, ShanMayne <[EMAIL PROTECTED]> wrote:
> However this does not help me to use the reference/name of an object I
> imported instead of created.

I've never really understood these requests (and they come up a lot).
Unless you're doing a '*' import, you'll already know the bound names
of the objects you're importing. If you -are- doing a '*' import and
you -don't- know what objects are being imported, how will you refer
to the object to find out its name?

However, maybe one of the following examples will be of use.

Assume a module 'items' that contains:

a = 1
b = 'string'

The best way would be to use the module as it is intended, as a
namespace:

>>> import items
>>> names = [x for x in dir(items) if not '__' in x] # ignore
special objects
>>> names
['a', 'b']
>>> one = getattr(items, names[0])
>>> two = getattr(items, names[1])
>>> one
1
>>> two
'string'

Another way is to look through the variables in the current scope:

>>> before = set(locals())
>>> before.add('before') # you want to ignore this object too
>>> from items import *
>>> names = list(set(locals()).difference(before))
>>> names
['a', 'b']
>>> one = locals()[names[0]]
>>> two = locals()[names[1]]
>>> one
1
>>> two
'string'

Do either of these help with your problem?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Tkinter: How to get Label wraplength functionality in Text Box

2008-10-29 Thread Guilherme Polo
On 10/29/08, Mudcat <[EMAIL PROTECTED]> wrote:
> Sounds like that would work really well. Problem is I can't get it to
>  work.
>
>  ...
>  AttributeError: Text instance has no attribute 'count'
>  ...
>

Yep, it is not there yet.

>  I think my usage is correct. I don't have any params at the moment,
>  but I was just checking the functionality.
>
>  numlines = widget.count()
>
>  According to the Tk 8.5 documentation it's used just like a normal
>  command.
>  WIDGET COMMAND
> pathName count ?options? index1 index2
> -chars
> -displaychars
> -displayindices
> -displaylines
> -indices
> -lines
> -xpixels
> -ypixels
>
>
>  As for the environment, I thought I had everything set up correctly.
>  I've got the latest stable version of Python 2.6 (r26:66721, Oct  2
>  2008, 11:35:03). I'm implementing the TTK wrappers to access Tk 8.5.

Implementing a ttk wrapper won't give you this "count" command, since
this is a command for an already existing widget (the text widget), it
is not part of the ttk widgets. Also, there is already a ttk wrapper
at http://pypi.python.org/pypi/pyttk which is being proposed to be
included in python's stdlib.

>  Although when I check the wrapper I don't see any mods to the Text
>  Box. I also don't see this option in the Tkinter.py file.
>
>  Is there something else I need to add to access this new feature?
>

You would need to wrap it and add it as a method to the Text class in
Tkinter. Fortunately it is easily done:

import Tkinter

def text_count(self, index1, index2, *options):
args = ["-%s" % opt for opt in options]
args.extend([index1, index2])
return self.tk.call(self._w, "count", *args)

Tkinter.Text.count = text_count


Then to try it:


root = Tkinter.Tk()
text = Tkinter.Text()
text.pack()

text.insert("1.0", "a\nb\c\nd")
print text.count("1.0", "end", "displaylines", "lines")

root.mainloop()


Note that I inverted the order of the arguments here, indices and then
the options or no options. If it doesn't work saying "count" is not an
acceptable command then your tkinter is not compiled against tcl/tk
8.5 or later.

>
>
>  On Oct 28, 6:51 pm, "Guilherme Polo" <[EMAIL PROTECTED]> wrote:
>
>  >
>  > Are you looking for something like the new "count" command for the
>  > text widget in tk 8.5 ? "count" can count the number of logical lines
>  > (irrespective of wrapping), display lines (counts one for each time a
>  > line wraps) and some other things.
>  >
>
> > >  Thanks
>  >
>  > > --
>  > >  http://mail.python.org/mailman/listinfo/python-list
>  >
>
> > --
>  > -- Guilherme H. Polo Goncalves
>
>
>  --
>  http://mail.python.org/mailman/listinfo/python-list
>


-- 
-- Guilherme H. Polo Goncalves
--
http://mail.python.org/mailman/listinfo/python-list


Re: parsing MS word docs -- tutorial request

2008-10-29 Thread Mike Driscoll
On Oct 29, 4:32 am, Okko Willeboordsed <[EMAIL PROTECTED]>
wrote:
> Get a copy of;  Python Programming on Win32, ISBN 1-56592-621-8
> Use Google and VBA for help
>
> [EMAIL PROTECTED] wrote:
> > All,
>
> > I am trying to write a script that will parse and extract data from a
> > MS Word document.  Can / would anyone refer me to a tutorial on how to
> > do that?  (perhaps from tables).  I am aware of, and have downloaded
> > the pywin32 extensions, but am unsure of how to proceed -- I'm not
> > familiar with the COM API for word, so help for that would also be
> > welcome.
>
> > Any help would be appreciated.  Thanks for your attention and
> > patience.
>
> > ::bp::

Also check out MSDN as the win32 module is a thin wrapper so most of
the syntax on MSDN or in VB examples can be directly translated to
Python. There's also a PyWin32 mailing list which is quite helpful:

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

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


Re: How to get an object's name as a string?

2008-10-29 Thread ShanMayne
Indeed they do. My delighted thanks. You have most precisely addressed
the problem I intended to convey.

I should have given the case of module attributes a moments further
thought, an obvious answer. The locals() was unknown to me (rookie
gaps).

Thank you for the elaborated illustration.

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


Re: Fastest way to convert sql result into a dict or list ?

2008-10-29 Thread Peter Otten
[EMAIL PROTECTED] wrote:

> Hello,
> 
> I'm trying to find the fastest way to convert an sql result into a
> dict or list.
> What i mean, for example:
> my sql result:
> contact_id, field_id, field_name, value
> sql_result=[[1, 1, 'address', 'something street'],
>  [1, 2, 'telnumber', '11'],
>  [1, 3, 'email', '[EMAIL PROTECTED]'],
>  [2, 1, 'address','something stree'],
>  [2, 3, 'email','[EMAIL PROTECTED]']]
> the dict can be:
> dict={1:['something street', '11' ,
> '[EMAIL PROTECTED]'],
> 2:['something street', '', '[EMAIL PROTECTED]' ]}
> or a list can be:
> list=[[1,'something street', '11' ,
> '[EMAIL PROTECTED]'],
>[2,'something street', '', '[EMAIL PROTECTED]' ]]
> 
> I tried to make a dict, but i think it is slower then make a list, and
> i tried the "one lined for" to make a list, it's look like little bit
> faster than make a dict.
> 
> def empty_list_make(sql_result):
> return [ [line[0],"", "", ""]   for line in sql_result]
> 
> than fill in the list with another for loop.
> I hope there is an easyest way to do something like this ??
> any idea ?

I think it won't get much easier than this:

dod = {}
to_index = [None] + range(3)
for contact_id, field_id, field_name, value in data:
if contact_id not in dod:
dod[contact_id] = [""]*len(to_index)
dod[contact_id][to_index[field_id]] = value

A database expert might do it in SQL, but my try got a bit messy:

import sqlite3 as sqlite

conn = sqlite.connect(":memory:")
cs = conn.cursor()
cs.execute("create table tmp (contact_id, field_id, field_name, value);")

data = [[1, 1, 'address', 'one-address'],
 [1, 2, 'telnumber', 'one-telephone'],
 [1, 3, 'email', '[EMAIL PROTECTED]'],
 [2, 1, 'address','two-address'],
 [2, 3, 'email','[EMAIL PROTECTED]']]

cs.executemany("insert into tmp values (?, ?, ?, ?)", data)

def make_query(field_defs, table="tmp"):
field_defs = [("alias%s" % index, id, name)
  for index, (id, name) in enumerate(field_defs)]
fields = ", ".join("%s.value as %s" % (alias, name)
   for alias, id, name in field_defs)

format = ("left outer join %(table)s as %(alias)s "
  "on main.contact_id = %(alias)s.contact_id "
  "and %(alias)s.field_id=%(field_id)s ")
joins = "\n".join(format
  % dict(table=table, alias=alias, field_id=id)
  for alias, id, name in field_defs)

return ("select distinct main.contact_id, %(fields)s "
"from %(table)s as main\n %(joins)s" % dict(
table=table, fields=fields, joins=joins))

field_defs = list(
cs.execute("select distinct field_id, field_name from tmp"))

# XXX sanitize field ids and names

sql = make_query(field_defs)
for row in cs.execute(sql):
print row

Note that you get None for empty fields, not "".

Peter

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


Re: Python memory usage

2008-10-29 Thread David Cournapeau
On Wed, Oct 29, 2008 at 6:56 PM, [EMAIL PROTECTED]
<[EMAIL PROTECTED]> wrote:
> On Oct 21, 5:19 pm, Rolf Wester <[EMAIL PROTECTED]> wrote:
>> Hi,
>>
>> I have the problem that with long running Python scripts (many loops)
>> memory consumption increases until the script crashes. I used the
>> following small script to understand what might happen:
>>
> 
>
> AFAIK, python uses malloc behind the scenes to allocate memory. From
> the malloc man page...
>
> "The  malloc() and free() functions provide a simple, general-purpose
> memory allocation package. The malloc() function returns a pointer to
> a block of at least size bytes suitably aligned for any use. If the
> space assigned by malloc() is overrun, the results are undefined.
>
> The argument to free() is a pointer to a block previously allocated by
> malloc(), calloc(), or realloc(). After free() is executed, this space
> is made available for further  allocation by the application, though
> not returned to the system. Memory is returned to the system only
> upon  termination of  the  application.  If ptr is a null pointer, no
> action occurs. If a random number is passed to free(), the results are
> undefined."

Depending on your malloc implementation, that may not be true. IN
particular, with glibc, bit allocations are done with mmap, and those
areas are unmaped when free is called; any such area is immediatly
returned to the system

http://www.gnu.org/software/libtool/manual/libc/Malloc-Tunable-Parameters.html#Malloc-Tunable-Parameters

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


Re: Fastest way to convert sql result into a dict or list ?

2008-10-29 Thread alex23
On Oct 29, 9:35 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> I'm trying to find the fastest way to convert an sql result into a
> dict or list.

>>> from collections import defaultdict
>>> results = defaultdict(defaultdict)
>>> for contact_id, field_id, field_name, value in sql_result:
... results[contact_id][field_id] = value
... results[contact_id][field_name] = value
...

This lets you reference things in a straightforward way:

>>> results[1]['email']
'[EMAIL PROTECTED]'

If you'd prefer to use only the ids for reference:

>>> results = defaultdict(defaultdict)
>>> for contact_id, field_id, field_name, value in sql_result:
... results[contact_id][field_id] = (field_name, value)
...
>>> results[1][1]
('address', 'something street')

Hope this helps.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python memory usage

2008-10-29 Thread bieffe62
On 21 Ott, 17:19, Rolf Wester <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I have the problem that with long running Python scripts (many loops)
> memory consumption increases until the script crashes. I used the
> following small script to understand what might happen:
>
> import gc
>
> print len(gc.get_objects())
>
> a = []
> for i in range( 400 ):
>     a.append( None )
> for i in range( 400 ):
>     a[i] = {}
>
> print len(gc.get_objects())
>
> ret = raw_input("Return:")
>
> del a
> gc.collect()
>
> print len(gc.get_objects())
>
> ret = raw_input("Return:")
>
> The output is:
> 4002706
> Return:
> 2705
> Return:
>
> When I do ps aux | grep python before the first "Return" I get:
> wester    5255 51.2 16.3 1306696 1286828 pts/4 S+   17:59   0:30 python
> memory_prob2.py
>
> and before the second one:
> wester    5255 34.6 15.9 1271784 1255580 pts/4 S+   17:59   0:31 python
> memory_prob2.py
>
> This indicates that although the garbage collector freed 401 objects
> memory consumption does not change accordingly.
>
> I tried the C++ code:
>
> #include 
> using namespace std;
>
> int main()
> {
>         int i;
>         cout << ":";
> //ps 1
>         cin >> i;
>
>         double * v = new double[4000];
>         cout << ":";
> //ps 2
>         cin >> i;
>
>         for(int i=0; i < 4000; i++)
>                 v[i] = i;
>
>         cout << v[4000-1] << ":";
> //ps 3
>         cin >> i;
>
>         delete [] v;
>
>         cout << ":";
> //ps 4
>         cin >> i;
>
> }
>
> and got from ps:
>
> ps 1: 11184
> ps 1: 323688
> ps 1: 323688
> ps 1: 11184
>
> which means that the memery which is deallocated is no longer used by
> the C++ program.
>
> Do I miss something or is this a problem with Python? Is there any means
> to force Python to release the memory that is not used any more?
>
> I would be very appreciative for any help.
>
> With kind regards
>
> Rolf



To be sure that the deallocated memory is not cached at some level to
be reused, you could try
someting like this:

while 1:
l = [dict() for i in range(400)]
l = None # no need of gc and del

For what is worth, on my PC ( Windows XP and Python 2.5.2) the memory
usage of the process
monitored with the Task manager grows up to 600 MB before the memory
is actually released.

Note that in your example, as in mine, you do not need to call
gc.collect(), because
the huge list object is already deleted when you do "del a" ( or in my
case when I reassign "l" and the
huge list drops to 0 reference counts ). The basic memory garbage
collector in CPython
is based on reference counts; gc is only used to find and break
circular reference chains,
which your example do not create. As a proof of that, if ypu print the
retuirn value of
gc.collect (whic is the number of collected objects) you should get 0.

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


Re: Finding the instance reference of an object

2008-10-29 Thread Dale Roberts
On Oct 28, 11:59 am, Joe Strout <[EMAIL PROTECTED]> wrote:
> ...
>
> There are only the two cases, which Greg quite succinctly and  
> accurately described above.  One is by value, the other is by  
> reference.  Python quite clearly uses by value.  Parameters are  
> expressions that are evaluated, and the resulting value copied into  
> the formal parameter, pure and simple.  The continued attempts to  
> obfuscate this is pointless and wrong.
>
> Best,
> - Joe

Joe, you are being too generous and expansive here.

[Play along with me for a minute here...]

Don't you know? There is really only *ONE* case, and, you are right,
it is Pass By Value. There is no such thing as Pass By Reference at
the physical CPU level at all, right? If there is, show it to me. Pass
By Reference is just a silly idiom developed by high-minded CS
academics to confuse the rest of us. It has no practical use and
should not be given its own name, when we already have a good an
proper name for it.

Let me demonstrate with 3 examples of a function definition, and the
appropriate calling syntax for that function in C++, all sharing the
common "int i" global variable:

int i = 5;

myfunc(int &val){}   /*CALL:*/ myfunc(i);// "ByRef" (ya, right!)
myfunc(int val){}/*CALL:*/ myfunc(i);// ByVal
myfunc(int *val){}   /*CALL:*/ myfunc(&i);   // Joe's ByVal

The first is what all the fakers call "Pass By Reference" - sheesh,
how naive. We all know that what *really* happens internally is that
the *address* of val (A VALUE itself, or course) is copied and passed
on the stack, right? There couldn't be a more straightforward example
of Pass By Value (unless it's an inline function, or optimized away,
or possibly when implemented in a VM, or...). It passes the *address*
of i by value, then we can access the *value* of i too via
indirection. Hmm, did we need to have two definitions of VALUE there?
Well, never mind, no one will notice...

The next is obviously pass by value. It's right out there. The value
of i (which is what we are talking about, right?) is copied out, and
passed right on the stack in plain daylight where we can all see it.

How about the third? Pass By Value, obviously, of course. This is the
version you are defending, right? The parameter's value, &i, is
evaluated and copied right onto the stack, just like in the first
example. In fact, if you compare the assembler output of the first and
third examples, you may not even see a difference. Never mind the
actual contents of that pesky "i" variable that most people are
referring to when they use the term "value". We don't need to dress up
example 3 and call it an "idiom" where we are really passing a so-
called "reference" of the variable "i". Indeed! Don't insult our
intelligence. We can all see that it's an address passed by value,
plain and simple.


Pass By Reference? So "postmodern". Who needs it. Show me a so-called
"reference". I've looked at the assembler output and have never seen
one. There is no such thing.

"The continued attempts to obfuscate this is pointless and wrong."


---
I hate to have to add this, but for those not paying close attention:

  ;-)

dale

(tongue back out of cheek now)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Improving interpreter startup speed

2008-10-29 Thread Michael
Tried using the precache daemon to see if it gives any boost?
--
http://mail.python.org/mailman/listinfo/python-list


Re: how to use logging module to log an object like print()

2008-10-29 Thread bieffe62
On 29 Ott, 12:24, Steve Holden <[EMAIL PROTECTED]> wrote:
> Diez B. Roggisch wrote:
> > davy zhang schrieb:
> >> mport logging
> >> import pickle
>
> >> # create logger
> >> logger = logging.getLogger("simple_example")
> >> logger.setLevel(logging.DEBUG)
> >> # create console handler and set level to debug
> >> ch = logging.StreamHandler()
> >> ch.setLevel(logging.DEBUG)
> >> # create formatter
> >> formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s
> >> - %(message)s ")
> >> # add formatter to ch
> >> ch.setFormatter(formatter)
> >> # add ch to logger
> >> logger.addHandler(ch)
>
> >> d = {'key':'msg','key2':'msg2'}
>
> >> # "application" code
> >> logger.debug("debug message",d)#can not do this
>
> > logger.debug("yes you can: %r", d)
>
> One deficiency of this approach, however, is that the string formatting
> is performed even when no logging is required, thereby wasting a certain
> amount of effort on unnecessary formatting.
>
> regards
>  Steve
> --
> Steve Holden        +1 571 484 6266   +1 800 494 3119
> Holden Web LLC              http://www.holdenweb.com/- Nascondi testo citato
>

Sure about that?

This is the implementation of Logger.debug in
the file : ..Python25\lib\logging\__init__.py

   def debug(self, msg, *args, **kwargs):
"""
Log 'msg % args' with severity 'DEBUG'.

To pass exception information, use the keyword argument
exc_info with
a true value, e.g.

logger.debug("Houston, we have a %s", "thorny problem",
exc_info=1)
"""
if self.manager.disable >= DEBUG:
return
if DEBUG >= self.getEffectiveLevel():
apply(self._log, (DEBUG, msg, args), kwargs)


The other methods (info, warning, ...) are similar. It looks like
the formatting is only done if actually used.

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


Default Argument Question

2008-10-29 Thread Paulo J. Matos
Hi all,

Going through the tutorial brought up a question. Consider the functions:

def f(a, L=[]):
L.append(a)
return L

print f(3)
print f(9)
print f(7)

def f1(i = 0):
i = i + 1
print i

f1()
f1()
f1()
f1()

Since the f accumulates the values in L, I was expecting to see i
printing 1,2,3,4 but this doesn't happen.
Can someone please explain why and what is going on beneath the veil?

Cheers,

-- 
Paulo Jorge Matos - pocmatos @ gmail.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Default Argument Question

2008-10-29 Thread Chris Rebert
On Wed, Oct 29, 2008 at 9:14 AM, Paulo J. Matos <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> Going through the tutorial brought up a question. Consider the functions:
>
> def f(a, L=[]):
>L.append(a)
>return L
>
> print f(3)
> print f(9)
> print f(7)
>
> def f1(i = 0):
>i = i + 1
>print i
>
> f1()
> f1()
> f1()
> f1()
>
> Since the f accumulates the values in L, I was expecting to see i
> printing 1,2,3,4 but this doesn't happen.
> Can someone please explain why and what is going on beneath the veil?

http://effbot.org/zone/default-values.htm explains this FAQ quite
well. Note that the "accumulation" behavior of lists is considered an
aberration and is usually not desired.

Cheers,
Chris
-- 
Follow the path of the Iguana...
http://rebertia.com

>
> Cheers,
>
> --
> Paulo Jorge Matos - pocmatos @ gmail.com
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: Default Argument Question

2008-10-29 Thread Bruno Desthuilliers

Paulo J. Matos a écrit :

Hi all,

Going through the tutorial brought up a question. Consider the functions:

def f(a, L=[]):
L.append(a)
return L

print f(3)
print f(9)
print f(7)

def f1(i = 0):
i = i + 1
print i

f1()
f1()
f1()
f1()

Since the f accumulates the values in L, I was expecting to see i
printing 1,2,3,4 but this doesn't happen.
Can someone please explain why and what is going on beneath the veil?


In the first case, you are mutating L. In the second, you are rebinding 
the local name i. IOW, you are comparing oranges and apples. The 
corresponding code using a list would be:


>>> def f2(arg, alist=[]):
... alist = alist + [arg]
... print alist
...

And it behaves just like your f1 function:

>>> f2(1)
[1]
>>> f2(1)
[1]
>>> f2(1)
[1]
>>> f2(1)
[1]
>>>


But anyway: you just couldn't mutate i in f1, since integers are 
immutables...


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


Re: Default Argument Question

2008-10-29 Thread Bruno Desthuilliers

Chris Rebert a écrit :
(snip)

Note that the "accumulation" behavior of lists is considered an
aberration 


By who ?

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


beutifulsoup

2008-10-29 Thread luca72
Hello
I try to use beautifulsoup
i have this:
sito = urllib.urlopen('http://www.prova.com/')
esamino = BeautifulSoup(sito)
luca = esamino.findAll('tr', align='center')

print luca[0]

>>>href="#">#144.4MB>align="left"> Pc-prova.rar 

I need to get the following information:
1)Only|G|BoT|05
2)#1
3)44.4MB
4)Pc-prova.rar
with: print luca[0].a.stringi get #1
with print luca[0].td.stringi get 44.4MB
can you explain me how to get the others two value
Thanks
Luca
--
http://mail.python.org/mailman/listinfo/python-list


RE: parsing MS word docs -- tutorial request

2008-10-29 Thread Reedick, Andrew
> -Original Message-
> From: [EMAIL PROTECTED] [mailto:python-
> [EMAIL PROTECTED] On Behalf Of
> [EMAIL PROTECTED]
> Sent: Tuesday, October 28, 2008 10:26 AM
> To: python-list@python.org
> Subject: parsing MS word docs -- tutorial request
> 
> All,
> 
> I am trying to write a script that will parse and extract data from a
> MS Word document.  Can / would anyone refer me to a tutorial on how to
> do that?  (perhaps from tables).  I am aware of, and have downloaded
> the pywin32 extensions, but am unsure of how to proceed -- I'm not
> familiar with the COM API for word, so help for that would also be
> welcome.
> 
> Any help would be appreciated.  Thanks for your attention and
> patience.
> 
> ::bp::
> --
> http://mail.python.org/mailman/listinfo/python-list


Word Object Model:
http://msdn.microsoft.com/en-us/library/bb244515.aspx

Google for sample code to get you started.


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


How to Implement an XMLRPC Server in Python?

2008-10-29 Thread Zix
Hello,
I am a newbie to python and trying to get a hang of some of its
advanced features through an application I am building. Basically, I'd
like to build a weather forecasting web service. The clients should be
able to query the service with a location and date and get back the
weather forecast.

After reading a lot on the topic I've settled on the following:
* not using any python frameworks (Django, pylons etc)
* exposing the service through an XMLRPC API using Python 2.4.3
(that's the python version on CentOS 5.2)
* using mod_wsgi ( http://code.google.com/p/modwsgi/ ) instead of
mod_python

So far, I've just created the database design and been focusing on
that. But now I realize I've been procrastinating because I am not
sure how to really go about implementing this (the docs on xml rpc
python module are especially sparse). Please advice.


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


Re: [Novice]Installing eric4 with python 2.6

2008-10-29 Thread Saurabh Agrawal
>
>
> PyQt supported Python 2.6 on the day it was released.
>
> A snapshot of the PyQt Windows installer for Python 2.6 can be downloaded
> from the same page as you downloaded the installer for Python 2.5.
>
> Phil
> --
>  
>


Thanks Phil, it worked. My bad, sorry.
--
http://mail.python.org/mailman/listinfo/python-list


Re: error estimation in a non-linear least squares fitting

2008-10-29 Thread Evelien
Ok, then I have to look into scipy.odr to see how it can help me move
forward. Thanks Robert!
Evelien
--
http://mail.python.org/mailman/listinfo/python-list


Re: parsing MS word docs -- tutorial request

2008-10-29 Thread Kay Schluehr
On 28 Okt., 15:25, [EMAIL PROTECTED] wrote:
> All,
>
> I am trying to write a script that will parse and extract data from a
> MS Word document.  Can / would anyone refer me to a tutorial on how to
> do that?  (perhaps from tables).  I am aware of, and have downloaded
> the pywin32 extensions, but am unsure of how to proceed -- I'm not
> familiar with the COM API for word, so help for that would also be
> welcome.
>
> Any help would be appreciated.  Thanks for your attention and
> patience.
>
> ::bp::

One can convert MS-Word documents into some class of XML documents
called MHTML. If I remember correctly those documents had an .mht
extension. The result is a huge amount of ( nevertheless structured )
markup gibberish together with text. If one spends time and attention
one can find pattern in the markup ( we have XML and it's human
readable ).

A few years ago I used this conversion to implement roughly following
thing algorithm:

1. I manually highlighted one or more sections in a Word doc using a
background colour marker.
2. I searched for the colour marked section and determined the
structure. The structure information was fed into a state machine.
3. With this state machine I searched for all sections that were
equally structured.
4. I applied a href link to the text that was surrounded by the
structure and removed the colour marker.
5. In another document I searched for the same text and set an anchor.

This way I could link two documents ( those were public specifications
being originally disconnected ).

Kay

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


Python/Numeric users be aware!

2008-10-29 Thread Benyang
Maybe it has been reported somewhere, but it is a big surprise to me.

# Try the following:
import Numeric
a = Numeric.ones(10)
a[5:] = -1
print a

It works correctly on 32-bit linux machines and on 32-bit Windows XP:
[ 1  1  1  1  1 -1 -1 -1 -1 -1]

It is totally screwed up on 64-bit linux machines:
[1 1 1 1 1 1 1 1 1 1]

# The following works correctly on both 32-bit and 64-bit machines
(notice the comma):
a[5:,] *= -1

The Python version is 2.5.1, and Numeric is the latest version 24.2.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Amharic Question Answering

2008-10-29 Thread Terry Reedy

seid muhie wrote:



Dear All
I am new to Python. Am new to NLP(NAtural LAnguage Processing) too.
But take the initiation to develop Autamatic Amharic Question Answering 
as part of my MSc. degree partial fuflfilment thesis work.


Please excuse my ignorance, but is an Amharic QA system different from 
an English QA system, other that the characters and language?



Now I need,
1. If python could help me in doing the QA system
2. if any QA system been developed in Python.
am going through the NLTK book with its nice examples. That is why I 
seemed to prefer python as a development tool for my QA.


The only part I can answer is that Python2 supports Unicode, which I 
believe contains Amharic characters, which the upcoming Python3 uses 
Unicode as the text class.


tjr

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


Re: Python/Numeric users be aware!

2008-10-29 Thread Carl

Confirmed

Benyang wrote:

Maybe it has been reported somewhere, but it is a big surprise to me.

# Try the following:
import Numeric
a = Numeric.ones(10)
a[5:] = -1
print a

It works correctly on 32-bit linux machines and on 32-bit Windows XP:
[ 1  1  1  1  1 -1 -1 -1 -1 -1]

It is totally screwed up on 64-bit linux machines:
[1 1 1 1 1 1 1 1 1 1]

# The following works correctly on both 32-bit and 64-bit machines
(notice the comma):
a[5:,] *= -1

The Python version is 2.5.1, and Numeric is the latest version 24.2.

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


Re: Python/Numeric users be aware!

2008-10-29 Thread Jerry Hill
On Wed, Oct 29, 2008 at 1:53 PM, Benyang <[EMAIL PROTECTED]> wrote:
> The Python version is 2.5.1, and Numeric is the latest version 24.2.

While 24.2 is the latest version of Numeric, it's also three years old
and no longer supported.  From http://numpy.scipy.org/ - "Numeric was
the first arrayobject built for Python.  It has been quite successful
and is used in a wide variety of settings and applications.
Maintenance has ceased for Numeric, and users should transisition to
NumPy as quickly as possible."

So, you are unlikely to find anyone to do bug fixes on Numeric.

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


Re: How to Implement an XMLRPC Server in Python?

2008-10-29 Thread Daniel Fetchinson
On 10/29/08, Zix <[EMAIL PROTECTED]> wrote:
> Hello,
> I am a newbie to python and trying to get a hang of some of its
> advanced features through an application I am building. Basically, I'd
> like to build a weather forecasting web service. The clients should be
> able to query the service with a location and date and get back the
> weather forecast.
>
> After reading a lot on the topic I've settled on the following:
> * not using any python frameworks (Django, pylons etc)
> * exposing the service through an XMLRPC API using Python 2.4.3
> (that's the python version on CentOS 5.2)
> * using mod_wsgi ( http://code.google.com/p/modwsgi/ ) instead of
> mod_python
>
> So far, I've just created the database design and been focusing on
> that. But now I realize I've been procrastinating because I am not
> sure how to really go about implementing this (the docs on xml rpc
> python module are especially sparse). Please advice.
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

These pages might be useful, they include example code:

http://docs.python.org/library/simplexmlrpcserver.html
http://code.activestate.com/recipes/81549/
http://code.activestate.com/recipes/496786/
http://www.google.com/search?q=python+xmlrpc+server+example

Cheers,
Daniel

-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
--
http://mail.python.org/mailman/listinfo/python-list


Python Regex Question

2008-10-29 Thread MalteseUnderdog

Hi there I just started python (but this question isn't that trivial
since I couldn't find it in google :) )

I have the following text file entries (simplified)

start  #frag 1 start
x=Dog # frag 1 end
stop
start# frag 2 start
x=Cat # frag 2 end
stop
start #frag 3 start
x=Dog #frag 3 end
stop


I need a regex expression which returns the start to the x=ANIMAL for
only the x=Dog fragments so all my entries should be start ...
(something here) ... x=Dog .  So I am really interested in fragments 1
and 3 only.

My idea (primitive) ^start.*?x=Dog doesn't work because clearly it
would return results

start
x=Dog  # (good)

and

start
x=Cat
stop
start
x=Dog # bad since I only want start ... x=Dog portion

Can you help me ?

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


Re: free IDE with removing import and refactoring

2008-10-29 Thread Stef Mientki

pihentagy wrote:

Hi!

I am looking for a python IDE which can remove my unused imports,

is that possible in a language like Python ?
I can imagine the opposite, importing those things that are necessary.

 and
can do basic refactoring under windows.
  

What kind of refactoring do you think of ?

cheers,
Stef

Can somebody advice me such an IDE?

I have played with eclipse and netbeans, but I cannot find such a
functionality (but maybe I looked it over).

Besides, an installation howto would be useful for me.

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


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


Re: How to Implement an XMLRPC Server in Python?

2008-10-29 Thread Guilherme Polo
On 10/29/08, Zix <[EMAIL PROTECTED]> wrote:
> Hello,
>  I am a newbie to python and trying to get a hang of some of its
>  advanced features through an application I am building. Basically, I'd
>  like to build a weather forecasting web service. The clients should be
>  able to query the service with a location and date and get back the
>  weather forecast.
>
>  After reading a lot on the topic I've settled on the following:
>  * not using any python frameworks (Django, pylons etc)
>  * exposing the service through an XMLRPC API using Python 2.4.3
>  (that's the python version on CentOS 5.2)

Why did you decide to "expose" a web service through xmlrpc instead of
actually exposing it by using a restful web service ?

The links pointed by the previous email should help you, but well, I'm
still surprised on this decision specially because you said you read a
lot about this topic.

>  * using mod_wsgi ( http://code.google.com/p/modwsgi/ ) instead of
>  mod_python
>
>  So far, I've just created the database design and been focusing on
>  that. But now I realize I've been procrastinating because I am not
>  sure how to really go about implementing this (the docs on xml rpc
>  python module are especially sparse). Please advice.
>
>


-- 
-- Guilherme H. Polo Goncalves
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python/Numeric users be aware!

2008-10-29 Thread Kirk Strauser
At 2008-10-29T17:53:43Z, Benyang <[EMAIL PROTECTED]> writes:

> It is totally screwed up on 64-bit linux machines:
> [1 1 1 1 1 1 1 1 1 1]

And on 64-bit FreeBSD machines.
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to get an object's name as a string?

2008-10-29 Thread Duncan Booth
Steve Holden <[EMAIL PROTECTED]> wrote:

>> That explanation makes no sense. Given the assignment:
>> 
>> x = 57
>> 
>> if the name of x isn't 'x', then what on earth can it possibly mean to 
>> ask for the name of a variable?
>> 
> He didn't ask for the name of a variable, he asked for the name of an
> object. You may choose to equate them, but they aren't the same thing.

When I do that assignment there seem to be 5 references to that object, two 
of them are dictionary keys (what's the name of a dictionary key?), and two
are dictionary values. the last one is of course x. Any of these seem a 
reasonable answer to the question, but how the code is supposed to tell
which name is the one the user wanted is another matter.

>>> import varname
>>> x = 57
>>> for s in varname.object_info(x):
... print s
...
opcode.opmap['INPLACE_MULTIPLY']
encodings.cp850.encoding_map[57]
encodings.cp850.decoding_map[57]
dis.opmap['INPLACE_MULTIPLY']
__main__.x
>>>

When I repeat the experiment in Idle the cp850 encoding entries disappear so 
I only get a choice of 3 'names'.

> l = []
> l.append(l)
> del l
> 
> What's the name of the list formerly known as "l"?

My code thinks its name is <...>, but then that's just my code:

>>> l = []
>>> l.append(l)
>>> for s in varname.object_info(l):
print s


__main__.l
<...>
>>> 

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


Re: Is ctypes appropriate in my case?

2008-10-29 Thread Terry Reedy

[EMAIL PROTECTED] wrote:

Hi,

I've a dll and its header file that controls an hardware. I want to 
write a wrapper for this dll in Python.

What is the best way that I can write a wrapper?


What do you want to do with the wrapper?

I know ctypes modules and have used it before. As far as I know ctypes 
is only used to call the dll functions in a python module.


I am not sure what you mean here.  Python code can directly import and 
call functions in dlls that represent Python extension modules and that 
are placed in the Pythonxx/dlls directory in your Windows Python 
installation.  Ctypes is mostly used to call functions in a dll that is 
*not* a python extension module, that was not written for Python.


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


Re: how to get the thighest bit position in big integers?

2008-10-29 Thread Mensanator
On Oct 29, 1:26 am, Nick Mellor <[EMAIL PROTECTED]>
wrote:
> On Oct 6, 3:40 am, Gary Herron <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > [EMAIL PROTECTED] wrote:
> > > Hi,
>
> > > I'm using python to develop some proof-of-concept code for a
> > > cryptographic application. My code makes extended use of python's
> > > native bignum capabilities.
>
> > > In many cryptographic applications there is the need for a function
> > > 'get_highest_bit_num' that returns the position number of the highest
> > > set bit of a given integer. For example:
>
> > >    get_highest_bit_num( (1 << 159))     == 159
> > >    get_highest_bit_num( (1 << 160) - 1) == 159
> > >    get_highest_bit_num( (1 << 160))     == 160
>
> > How about a binary search?
>
> > >>> from bisect import bisect
> > >>> BITS = [1< > >>> bisect(BITS, 1<<159)
> > 159
> > >>> bisect(BITS, 1<<160-1)
> > 159
> > >>> bisect(BITS, 1<<160)
> > 160
>
> > I have no clue if this is faster or not.  The comparison function used
> > in the search is (potentially) slow, but the search is O(log n) on the
> > number of bits rather than O(n), so its worth a test.
>
> > If you run timing test, let us know the results.
>
> > Gary Herron
>
> > > I implemented this the following way:
>
> > > def get_highest_bit_num(r):
> > >     i = -1
> > >     while r > 0:
> > >         r >>= 1
> > >         i = i + 1
> > >     return i
>
> > > This works, but it is a very unsatisfying solution, because it is so
> > > slow.
>
> > > My second try was using the math.log function:
>
> > > import math
> > > r = (1 << 160) - 1
> > > print highest_bit_num(r)              # prints out 159
> > > print math.floor(math.log(r, 2))      # prints out 160.0
>
> > > We see that math.log can only serve as a heuristic for the highest bit
> > > position. For small r, for example r = (1 << 16) - 1, the result from
> > > math.log(, 2) is correct, for big r it isn't any more.
>
> > > My question to the group: Does anyone know of a non-hackish way to
> > > determine the required bit position in python? I know that my two
> > > ideas
> > > can be combined to get something working. But is there a *better* way,
> > > that isn't that hackish?
>
> > > cheers,
> > > mmg
> > > --
> > >http://mail.python.org/mailman/listinfo/python-list
>
> The following might be worth a try. It's faster the fewer set bits
> there are in the original number, and lightning fast if there are only
> one or two:
>
> def get_highest_bit_num(i):
>     while i>0: highestbit, i = i, i & (i-1)
>     return highestbit
>
> >>> highestbit(1<<31)
> 2147483648L
> >>> highestbit(1<<4)
> 16
> >>> highestbit(3<<4)
>
> 32
>
> Note that it returns the value of the highest bit, not its position.
>
> All it's doing is successively ANDing i with (i-1) until i is zero,
> then returning the previous value of i.
>
> It works because i & (i-1) has a useful property: it returns i less
> its least significant set bit:
>
> i=6 (binary 110) => i & (i-1)==4 (binary 100)
> i=3328 => (binary 1101__) then i & (i-1)==3072 (binary
> 1100__)
>
> (underscores for readability)
>
> As far as I know there isn't another piece of logic that helps you
> extract the most significant bit as simply :-)

import gmpy
print 'highest bit position: %d' % (len(gmpy.digits(3328,2))-1)

highest bit position: 11


or in 2.6

print 'highest bit position: %d' % (len(bin(3328)[2:])-1)

highest bit position: 11


>
> Best wishes,
>
> Nick
--
http://mail.python.org/mailman/listinfo/python-list


Re: is there a way to access postgresql in python3.0rc1

2008-10-29 Thread Terry Reedy

davy zhang wrote:

I'm currently on a  project, it could last for at least 1 or 2 years.
so I choose python3 as server side programing language.
All I found on are python2.x ready libraries, Is there any library is
python3.0 ready? or just under alpha ,beta or something, I don't much
features, just basic functions are OK


Python3.0 final should be released in about a month.  Extension 
libraries will start appearing after that.  You will have to ask the 
maintainers of a particular library what their plans are for Python 3. 
Some will port very easily and could be available soon.  Others will 
take more work and may not appear so soon.


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


Re: Python Regex Question

2008-10-29 Thread Tim Chase

I need a regex expression which returns the start to the x=ANIMAL for
only the x=Dog fragments so all my entries should be start ...
(something here) ... x=Dog .  So I am really interested in fragments 1
and 3 only.

My idea (primitive) ^start.*?x=Dog doesn't work because clearly it
would return results

start
x=Dog  # (good)

and

start
x=Cat
stop
start
x=Dog # bad since I only want start ... x=Dog portion


Looks like the following does the trick:

>>> s = """start  #frag 1 start
... x=Dog # frag 1 end
... stop
... start# frag 2 start
... x=Cat # frag 2 end
... stop
... start #frag 3 start
... x=Dog #frag 3 end
... stop"""
>>> import re
>>> r = re.compile(r'^start.*\nx=Dog.*\nstop.*', re.MULTILINE)
>>> for i, result in enumerate(r.findall(s)):
... print i, repr(result)
...
0 'start  #frag 1 start\nx=Dog # frag 1 end\nstop'
1 'start #frag 3 start\nx=Dog #frag 3 end\nstop'

-tkc







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


math equation, svg and matplotlib

2008-10-29 Thread André
Would anyone have a "quick and dirty" code sample to create an svg
output of a sample math equation using matplotlib?

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


Re: parsing MS word docs -- tutorial request

2008-10-29 Thread Terry Reedy

Kay Schluehr wrote:

On 28 Okt., 15:25, [EMAIL PROTECTED] wrote:

All,

I am trying to write a script that will parse and extract data from a
MS Word document.  Can / would anyone refer me to a tutorial on how to
do that?  (perhaps from tables).  I am aware of, and have downloaded
the pywin32 extensions, but am unsure of how to proceed -- I'm not
familiar with the COM API for word, so help for that would also be
welcome.

Any help would be appreciated.  Thanks for your attention and
patience.

::bp::


One can convert MS-Word documents into some class of XML documents
called MHTML. If I remember correctly those documents had an .mht
extension. The result is a huge amount of ( nevertheless structured )
markup gibberish together with text. If one spends time and attention
one can find pattern in the markup ( we have XML and it's human
readable ).


A related solution is to use OpenOffice to convert to 
OpenDocumentFormat, a zipped multiple XML format, and then use ODFPY to 
parse the XML and access the contents as linked objects.

http://opendocumentfellowship.com/development/projects/odfpy

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


Re: Default Argument Question

2008-10-29 Thread Benjamin Kaplan
On Wed, Oct 29, 2008 at 12:44 PM, Bruno Desthuilliers
<[EMAIL PROTECTED]> wrote:

> Chris Rebert a écrit :
> (snip)
>
>> Note that the "accumulation" behavior of lists is considered an
>> aberration
>>
>
> By who ?
>

All the python newbies who don't read the tutorial and get tripped up by
this.


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


Re: parsing MS word docs -- tutorial request

2008-10-29 Thread bp . tralfamadore

Thanks everyone -- very helpful!
I really appreciate your help -- that is what makes the world a
wonderful place.

peace.

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


Re: how to use logging module to log an object like print()

2008-10-29 Thread Diez B. Roggisch

Steve Holden schrieb:

Diez B. Roggisch wrote:

davy zhang schrieb:

mport logging
import pickle


# create logger
logger = logging.getLogger("simple_example")
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s
- %(message)s ")
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)

d = {'key':'msg','key2':'msg2'}

# "application" code
logger.debug("debug message",d)#can not do this

logger.debug("yes you can: %r", d)


One deficiency of this approach, however, is that the string formatting
is performed even when no logging is required, thereby wasting a certain
amount of effort on unnecessary formatting.


Nope, it's not. It only happens when the message is really logged.

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


Why gives "k = 09" a syntax error ?

2008-10-29 Thread Stef Mientki

hello,

Why gives "k = 09"  a syntax error ?

thanks,
Stef Mientki
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why gives "k = 09" a syntax error ?

2008-10-29 Thread Guilherme Polo
On 10/29/08, Stef Mientki <[EMAIL PROTECTED]> wrote:
> hello,
>
>  Why gives "k = 09"  a syntax error ?
>

09 is not a valid octal number. Instead use 011.

Ok, I guess you were not aware that prefixing a number with a '0'
would cause python to parse it as an octal and now you know.

>  thanks,
>  Stef Mientki
>  --
>  http://mail.python.org/mailman/listinfo/python-list
>


-- 
-- Guilherme H. Polo Goncalves
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python Regex Question

2008-10-29 Thread Arnaud Delobelle
On Oct 29, 7:01 pm, Tim Chase <[EMAIL PROTECTED]> wrote:
> > I need a regex expression which returns the start to the x=ANIMAL for
> > only the x=Dog fragments so all my entries should be start ...
> > (something here) ... x=Dog .  So I am really interested in fragments 1
> > and 3 only.
>
> > My idea (primitive) ^start.*?x=Dog doesn't work because clearly it
> > would return results
>
> > start
> > x=Dog  # (good)
>
> > and
>
> > start
> > x=Cat
> > stop
> > start
> > x=Dog # bad since I only want start ... x=Dog portion
>
> Looks like the following does the trick:
>
>  >>> s = """start      #frag 1 start
> ... x=Dog # frag 1 end
> ... stop
> ... start    # frag 2 start
> ... x=Cat # frag 2 end
> ... stop
> ... start     #frag 3 start
> ... x=Dog #frag 3 end
> ... stop"""
>  >>> import re
>  >>> r = re.compile(r'^start.*\nx=Dog.*\nstop.*', re.MULTILINE)
>  >>> for i, result in enumerate(r.findall(s)):
> ...     print i, repr(result)
> ...
> 0 'start      #frag 1 start\nx=Dog # frag 1 end\nstop'
> 1 'start     #frag 3 start\nx=Dog #frag 3 end\nstop'
>
> -tkc

This will only work if 'x=Dog' directly follows 'start' (which happens
in the given example).  If that's not necessarily the case, I would do
it in two steps (in fact I wouldn't use regexps probably but...):

>>> for chunk in re.split(r'\nstop', data):
... m = re.search('^start.*^x=Dog', chunk, re.DOTALL |
re.MULTILINE)
... if m: print repr(m.group())
...
'start  #frag 1 start \nx=Dog'
'start #frag 3 start \nx=Dog'

--
Arnaud

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


Re: Why gives "k = 09" a syntax error ?

2008-10-29 Thread Stef Mientki

Guilherme Polo wrote:

On 10/29/08, Stef Mientki <[EMAIL PROTECTED]> wrote:
  

hello,

 Why gives "k = 09"  a syntax error ?




09 is not a valid octal number. Instead use 011.

Ok, I guess you were not aware that prefixing a number with a '0'
would cause python to parse it as an octal and now you know.
  

thanks guys,
I didn't realize there were still people using octal notation ;-)

cheers,
Stef
--
http://mail.python.org/mailman/listinfo/python-list


Filter function and lists

2008-10-29 Thread John Townsend
I'm trying to figure out how to use filter to walk through a list.

If I try a simple scripts like this:

def greaterthanten (number):
#pdb.set_trace()
if (number > 10):
ret_val = 1

else:
ret_val = 0

return ret_val

old_list = [1,2,20,30,5]

new_list = filter(greaterthanten, old_list)

#new_list becomes [20, 30]

The script works as I would expect. However, what if I need to pass more than 
one argument to the function? Can I do that with filter? Or does filter work 
only with function that take only a single arg?

Thanks


John Townsend (5-7204),
AGM-FL and PSL QE Lead



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


Re: Python/Numeric users be aware!

2008-10-29 Thread Benyang Tang
I also found that the a[5:] problem is Python version dependent.

On a 64-bit linux, of the following combinations I have tried, only
the first one has the problem. The other two are ok.
* Python 2.5.1 and Numeric 24.2
* Python 2.4.5 and Numeric 24.2
* Python 2.3.7 and Numeric 24.2

On Oct 29, 10:53 am, Benyang <[EMAIL PROTECTED]> wrote:
> Maybe it has been reported somewhere, but it is a big surprise to me.
>
> # Try the following:
> import Numeric
> a = Numeric.ones(10)
> a[5:] = -1
> print a
>
> It works correctly on 32-bit linux machines and on 32-bit Windows XP:
> [ 1  1  1  1  1 -1 -1 -1 -1 -1]
>
> It is totally screwed up on 64-bit linux machines:
> [1 1 1 1 1 1 1 1 1 1]
>
> # The following works correctly on both 32-bit and 64-bit machines
> (notice the comma):
> a[5:,] *= -1
>
> The Python version is 2.5.1, and Numeric is the latest version 24.2.

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


Re: Why gives "k = 09" a syntax error ?

2008-10-29 Thread Mensanator
On Oct 29, 2:44 pm, Stef Mientki <[EMAIL PROTECTED]> wrote:
> Guilherme Polo wrote:
> > On 10/29/08, Stef Mientki <[EMAIL PROTECTED]> wrote:
>
> >> hello,
>
> >>  Why gives "k = 09"  a syntax error ?
>
> > 09 is not a valid octal number. Instead use 011.
>
> > Ok, I guess you were not aware that prefixing a number with a '0'
> > would cause python to parse it as an octal and now you know.
>
> thanks guys,
> I didn't realize there were still people using octal notation ;-)

Windows users don't have much need for it, but it's still
popular with the 'Nix crowd.

>
> cheers,
> Stef

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


Dot operator magic has me stymied...

2008-10-29 Thread Casey Rodarmor
Hi All,

I'm trying to use a class as a decorator for another class method, but
it's giving me a lot of grief. Basically, my problem is with the
example below:

>>> class decorator:
... def __init__(self, function):
... self.function = function
...
... def __call__(self, *args, **kwargs):
... self.function(*args, **kwargs)
...
>>> class Foo:
... def __init__(self):
... self.msg = "Hello,"
...
... @decorator
... def greet(self, name):
... print self.msg, name
...
>>> foo = Foo()
>>> foo.greet("Bob")
Traceback (most recent call last):
  File "", line 1, in 
  File "decorate.py", line 6, in __call__
self.function(*args, **kwargs)
TypeError: greet() takes exactly 2 arguments (1 given)


I'm guessing that using a decorator that returns a class instance
instead of a function instance has messed up the magic of the dot
operator, causing it not to bind the foo instance to the self
argument.

Can anybody shed some light on what's happening here?

Also, I really do like using classes as decorators. Are there any
workarounds to get it to work with methods?

Thanks a bunch!

Best regards,
Casey Rodarmor
--
http://mail.python.org/mailman/listinfo/python-list


Re: 2.6, 3.0, and truly independent intepreters

2008-10-29 Thread Rhamphoryncus
On Oct 29, 7:20 am, Paul Boddie <[EMAIL PROTECTED]> wrote:
> On 28 Okt, 21:03, Rhamphoryncus <[EMAIL PROTECTED]> wrote:
>
> > * get a short-term bodge that works, like hacking the 3rd party
> > library to use your shared-memory allocator.  Should be far less work
> > than hacking all of CPython.
>
> Did anyone come up with a reason why shared memory couldn't be used
> for the purpose described by the inquirer? With the disadvantages of
> serialisation circumvented, that would leave issues of contention, and
> on such matters I have to say that I'm skeptical about solutions which
> try and make concurrent access to CPython objects totally transparent,
> mostly because it appears to be quite a lot of work to get right (as
> POSH illustrates, and as your own safethread work shows), and also
> because systems where contention is spread over a large "surface" (any
> object can potentially be accessed by any process at any time) are
> likely to incur a lot of trouble for the dubious benefit of being
> vague about which objects are actually being shared.

I believe large existing libraries were the reason.  Thus my
suggestion of the evil fork+mmap abuse.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why gives "k = 09" a syntax error ?

2008-10-29 Thread Diez B. Roggisch

Stef Mientki schrieb:

hello,

Why gives "k = 09"  a syntax error ?


because numbers starting with 0 are an octal value, allowing only the 
literals 0-7.


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


Re: How to Implement an XMLRPC Server in Python?

2008-10-29 Thread Diez B. Roggisch

Guilherme Polo schrieb:

On 10/29/08, Zix <[EMAIL PROTECTED]> wrote:

Hello,
 I am a newbie to python and trying to get a hang of some of its
 advanced features through an application I am building. Basically, I'd
 like to build a weather forecasting web service. The clients should be
 able to query the service with a location and date and get back the
 weather forecast.

 After reading a lot on the topic I've settled on the following:
 * not using any python frameworks (Django, pylons etc)
 * exposing the service through an XMLRPC API using Python 2.4.3
 (that's the python version on CentOS 5.2)


Why did you decide to "expose" a web service through xmlrpc instead of
actually exposing it by using a restful web service ?

The links pointed by the previous email should help you, but well, I'm
still surprised on this decision specially because you said you read a
lot about this topic.


What is wrong with XMLRPC? I've implemented both kind of systems, and 
while REST has it's strengths in a HTML-centric environment, a real RCP 
mechanism with built-in type marshalling known to both sides of the 
communication is for pure interoperability better.


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


Re: Dot operator magic has me stymied...

2008-10-29 Thread Arnaud Delobelle
On Oct 29, 7:46 pm, "Casey Rodarmor" <[EMAIL PROTECTED]> wrote:
> Hi All,
>
> I'm trying to use a class as a decorator for another class method, but
> it's giving me a lot of grief. Basically, my problem is with the
> example below:
>
> >>> class decorator:
>
> ...     def __init__(self, function):
> ...         self.function = function
> ...
> ...     def __call__(self, *args, **kwargs):
> ...         self.function(*args, **kwargs)
> ...>>> class Foo:
>
> ...     def __init__(self):
> ...         self.msg = "Hello,"
> ...
> ...     @decorator
> ...     def greet(self, name):
> ...         print self.msg, name
> ...>>> foo = Foo()
> >>> foo.greet("Bob")
>
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "decorate.py", line 6, in __call__
>     self.function(*args, **kwargs)
> TypeError: greet() takes exactly 2 arguments (1 given)
>
> I'm guessing that using a decorator that returns a class instance
> instead of a function instance has messed up the magic of the dot
> operator, causing it not to bind the foo instance to the self
> argument.
>
> Can anybody shed some light on what's happening here?

When

@decorator
def greet(self, name):
print self.msg, name

is executed, a new decorator object is created whose function
attribute is the function 'greet' above (with two arguments) and the
result is given the name 'greet'.

Later, when

>>> foo = Foo()
>>> foo.greet("Bob")

is executed,

foo.greet is a decorator object whose attribute 'function' is the
plain function 'greet'. So it expects two arguments and you only
provide one.

To fix this you could use the Descriptor protocol.  If you don't know
what it is, I suggest you read this:

http://users.rcn.com/python/download/Descriptor.htm

It should give you what you need to get it right.

HTH

--
Arnaud


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


Re: Why gives "k = 09" a syntax error ?

2008-10-29 Thread Andrii V. Mishkovskyi
2008/10/29 Stef Mientki <[EMAIL PROTECTED]>:
> hello,
>
> Why gives "k = 09"  a syntax error ?

Because leading zero means that the number is octal, and there is no 9
among octal digits. :)

>
> thanks,
> Stef Mientki
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Wbr, Andrii Mishkovskyi.

He's got a heart of a little child, and he keeps it in a jar on his desk.
--
http://mail.python.org/mailman/listinfo/python-list


  1   2   >