Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

2010-02-17 Thread Lawrence D'Oliveiro
In message <60b1abce-4381-46ab-91ed-
f2ab2154c...@g19g2000yqe.googlegroups.com>, Andrej Mitrovic wrote:

> Also, lambda's are expressions, not statements ...

Is such a distinction Pythonic, or not? For example, does Python distinguish 
between functions and procedures?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

2010-02-17 Thread Lawrence D'Oliveiro
In message 
<8ca440b2-6094-4b35-80c5-81d000517...@v20g2000prb.googlegroups.com>, 
Jonathan Gardner wrote:

> I used to think anonymous functions (AKA blocks, etc...) would be a
> nice feature for Python.
> 
> Then I looked at a stack trace from a different programming language
> with lots of anonymous functions. (I believe it was perl.)

Didn’t it have source line numbers in it?

What more do you need?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: GIL state during import

2010-02-17 Thread Doron Tal
On Wed, Feb 17, 2010 at 1:01 AM, Terry Reedy  wrote:

> On 2/16/2010 4:37 PM, Doron Tal wrote:
>
>> Is the GIL released during import statement execution when accessing the
>> file?
>> If not, is it a bug?
>> If it is not a bug, or it is here to stay for any other reason, I think
>> it should be mentioned in the documentation.
>>
>
> The CPython GIL is a CPython implementation artifact and should not be
> mentioned in the language docs (and is not, as far as I know). Were you
> thinking of something else? And why this specific feature of its behavior,
> whatever it is.
>
> tjr
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

I'm sorry, I should have explained my case better.
Quoting the documentation (
http://docs.python.org/c-api/init.html#thread-state-and-the-global-interpreter-lock
):

 "The lock is also released and reacquired around potentially blocking I/O
operations like reading or writing a file, so that other threads can run
while the thread that requests the I/O is waiting for the I/O operation to
complete."


Since on some cases the IO may be _very_ slow, any IO command which does not
release the GIL can potentially stall the process. There are two problems
(or more...), the first, it may stall the process for unacceptable duration.
The second problem is that it is not an easy task to track down the root
cause of this stall.

In the specific case I'm dealing with, the problematic command is execfile
(at the time of my initial writing I did not know that).

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


Re: listing existing windows services with python

2010-02-17 Thread News123
Hi David,


Thanks a lot.

As I have pywin32 already installed this is probbaly the way to go.

Meanwhile I implemented already a small module, which is parsing
sc.exe's output, but probably 'll change it as my implementation is a
little clumsy.


bye


N

David Bolen wrote:
> alex23  writes:
> 
>> News123  wrote:
>>> What is the best way with python to get a list of all windows services.
>>>
>>> As a start I would be glad to receive only the service names.
>>>
>>> However it would be nicer if I could get all the properties of a service
>>> as well.
>> I highly recommend Tim Golden's fantastic WMI module[1].
> 
> Another alternative is the win32service module from the pywin32
> package (which IMO you'll almost certainly want available when doing
> any significant Windows-specific operations) which wraps the native
> win32 libraries for enumerating, querying and controlling services.
> 
> A simple loop could use EnumServicesStatus to iterate through the
> services, OpenService with the SERVICE_QUERY_CONFIG flag to get a
> handle to each service, and then QueryServiceConfig to retrieve
> configuration information.
> 
> Since pywin32 is a relatively thin wrapper over the win32 libraries,
> pure MSDN documentation can be used for help with the calls, augmented
> by any Python-related information contained in the pywin32
> documentation.
> 
> -- David
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: plugin / intra process communication system

2010-02-17 Thread Florian Ludwig
On Tue, 2010-02-16 at 19:20 +0100, Diez B. Roggisch wrote:
> Am 15.02.10 23:12, schrieb Florian Ludwig:
> > On Sun, 2010-02-14 at 18:47 +0100, Diez B. Roggisch wrote:
> >>> Here there problem with the trac (and other plugin systems I've
> >> seen)
> >>> approach:
> >>>
> >>> You need to define something like:
> >>> |
> >>> | class IAuthPlugin(Interface): [...]
> >>> 
> >>> in your blog software.
> >>
> >> Why? Any reason you can't define it in a separate package the
> >> blog-software depends on, as well as your wiki?
> >
> > That's actually my point - most plugin systems I've seen, like the one
> > trac uses, are not encouraging you to do so. Having a module that just
> > defines an Interface is kind of weird - and in the real world no one is
> > doing it.
> 
> Just because nobody doesn't do it doesn't mean it's not desirable.

Good point.

> IMHO the ones using interfaces usually immediatly implement them for some 
> cases - so you actually get the interface for you authentication 
> framework, and also implement an OpenID plugin for it. And this forms 
> one package. Then you can add new packages that implement other things.

So if I want to implement another authentication mechanism but doesn't
use any of the auth-frameworks code you mentioned you still would depend
on it.

> repoze.who and what are examples for such design.

repoze.who is new for me, it might not actually solve my problem here
but it looks interesting for other tasks :) Thanks.


> >> And then of course, this is not really needed. In Python, behavior
> >> counts, not type-information. So you can get away without any explicit
> >> declared interface. You might chose to not do that, for aestetic
> >> reasons, or better documentation. But you aren't forced.
> >
> > Actually some plugin-systems in python do force you and they check if
> > your "implementation" comply with the "interface".
> 
> I didn't say nobody does it, I just said it isn't needed to get a 
> working plugin system. If you don't want to have a separate package 
> *and* don't want to pull in any unused code, this is pretty much your 
> only option.

Do you know somebody/some project/system/... who does it?

Pulling unused code I definitely want to avoid so I probably will stay
with this way.


> > [...]
> >
> > What do you think? Any obvious pitfalls (besides reinventing something)?
> 
> I don't know what pbus is supposed to do. Nor how it's laid out on a 
> python package level.

Its "the plugin system", connecting "service providers" (plugins) and
"service users". In my example the "users" also are providers at the
same time, the service "auth", provides the service "wiki".

Hope that clarifies my intentions a bit.

Thanks,
Florian


PS I removed you (diez) from the TO list as that bounced. Probably 
   related to the (not existing?) domain nospam.web.de


-- 
Florian Ludwig 


signature.asc
Description: This is a digitally signed message part
-- 
http://mail.python.org/mailman/listinfo/python-list


Replacement for e.message() in python 2.6

2010-02-17 Thread Nandakumar Chandrasekhar

Dear Folks,

In previous versions of Python I used to use e.message() to print out 
the error message of an exception like so:


try:
result = x / y
except ZeroDivisionError, e:
print e.message()

Unfortunately in Python 2.6 the message method is deprecated.

Is there any replacement for the message method in Python 2.6 or is 
there any best practice that should be used in Python from now on?


Thank you.

Yours sincerely,
Nanda
--
http://mail.python.org/mailman/listinfo/python-list


Re: GIL state during import

2010-02-17 Thread Dave Angel

Terry Reedy wrote:
On 
2/16/2010 4:37 PM, Doron Tal wrote:

Is the GIL released during import statement execution when accessing the
file?
If not, is it a bug?
If it is not a bug, or it is here to stay for any other reason, I think
it should be mentioned in the documentation.


The CPython GIL is a CPython implementation artifact and should not be 
mentioned in the language docs (and is not, as far as I know). Were 
you thinking of something else? And why this specific feature of its 
behavior, whatever it is.


tjr

Threading vs. imports is mentioned at least once in the Python docs.  
See   http://docs.python.org/library/threading.html, section 17.2.9 (at 
least in version 2.6.4)


"""While the import machinery is thread safe, there are two key 
restrictions on threaded imports due to inherent limitations in the way 
that thread safety is provided:


   * Firstly, other than in the main module, an import should not have
 the side effect of spawning a new thread and then waiting for that
 thread in any way. Failing to abide by this restriction can lead
 to a deadlock if the spawned thread directly or indirectly
 attempts to import a module.
   * Secondly, all import attempts must be completed before the
 interpreter starts shutting itself down. This can be most easily
 achieved by only performing imports from non-daemon threads
 created through the threading module. Daemon threads and threads
 created directly with the thread module will require some other
 form of synchronization to ensure they do not attempt imports
 after system shutdown has commenced. Failure to abide by this
 restriction will lead to intermittent exceptions and crashes
 during interpreter shutdown (as the late imports attempt to access
 machinery which is no longer in a valid state).

"""

So it may or may not use the GIL, but there are thread restrictions 
during an import.  The rule I try to follow is not to do anything 
non-trivial during top-level code of a module, except inside the

 if __name__ == "__main__":

portion.  If we're inside that portion, we're not a module, we're a script.

Even better, import all your modules before starting any new threads.

DaveA


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


Re: Replacement for e.message() in python 2.6

2010-02-17 Thread Arnaud Delobelle
Nandakumar Chandrasekhar  writes:

> Dear Folks,
>
> In previous versions of Python I used to use e.message() to print out
> the error message of an exception like so:
>
> try:
>   result = x / y
> except ZeroDivisionError, e:
>   print e.message()
>
> Unfortunately in Python 2.6 the message method is deprecated.
>
> Is there any replacement for the message method in Python 2.6 or is
> there any best practice that should be used in Python from now on?

You can just use the __str__() method of the BaseException object for
this.  So instead of

print e.message

You can write

print str(e)

which in turn is equivalent to

print e

For more details see PEP 352 (http://www.python.org/dev/peps/pep-0352/)

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


Re: Hubris connects Ruby to Haskell, will there be such a connection between Python and Haskell?

2010-02-17 Thread jkn
On Feb 17, 2:04 am, Carl Banks  wrote:

> > Hubris connects Ruby to Haskell, will there be such a connection
> > between Python and Haskell?
>
> I would have expected Hubris to link Ada and Common Lisp together.
>
> Carl Banks

;-)

J^n
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

2010-02-17 Thread Bruno Desthuilliers

Aahz a écrit :

In article <8ca440b2-6094-4b35-80c5-81d000517...@v20g2000prb.googlegroups.com>,
Jonathan Gardner   wrote:

I used to think anonymous functions (AKA blocks, etc...) would be a
nice feature for Python.

Then I looked at a stack trace from a different programming language
with lots of anonymous functions. (I believe it was perl.)

I became enlightened.


+1 QOTW


++1 QOTW !-)

Had the same problem trying to debug some javascript...
--
http://mail.python.org/mailman/listinfo/python-list


Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

2010-02-17 Thread Bruno Desthuilliers

Lawrence D'Oliveiro a écrit :

In message <60b1abce-4381-46ab-91ed-
f2ab2154c...@g19g2000yqe.googlegroups.com>, Andrej Mitrovic wrote:


Also, lambda's are expressions, not statements ...


Is such a distinction Pythonic, or not? 


Python is (by design) a statement-based language, so yes, this 
distinction is pythonic !-)

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


Re: GIL state during import

2010-02-17 Thread Doron Tal
On Wed, Feb 17, 2010 at 11:59 AM, Dave Angel  wrote:

> Terry Reedy wrote:
>
>  On 2/16/2010
>> 4:37 PM, Doron Tal wrote:
>>
>>> Is the GIL released during import statement execution when accessing the
>>> file?
>>> If not, is it a bug?
>>> If it is not a bug, or it is here to stay for any other reason, I think
>>> it should be mentioned in the documentation.
>>>
>>
>> The CPython GIL is a CPython implementation artifact and should not be
>> mentioned in the language docs (and is not, as far as I know). Were you
>> thinking of something else? And why this specific feature of its behavior,
>> whatever it is.
>>
>> tjr
>>
>>  Threading vs. imports is mentioned at least once in the Python docs.  See
>   http://docs.python.org/library/threading.html, section 17.2.9 (at least
> in version 2.6.4)
>
> """While the import machinery is thread safe, there are two key
> restrictions on threaded imports due to inherent limitations in the way that
> thread safety is provided:
>
>   * Firstly, other than in the main module, an import should not have
> the side effect of spawning a new thread and then waiting for that
> thread in any way. Failing to abide by this restriction can lead
> to a deadlock if the spawned thread directly or indirectly
> attempts to import a module.
>   * Secondly, all import attempts must be completed before the
> interpreter starts shutting itself down. This can be most easily
> achieved by only performing imports from non-daemon threads
> created through the threading module. Daemon threads and threads
> created directly with the thread module will require some other
> form of synchronization to ensure they do not attempt imports
> after system shutdown has commenced. Failure to abide by this
> restriction will lead to intermittent exceptions and crashes
> during interpreter shutdown (as the late imports attempt to access
> machinery which is no longer in a valid state).
>
> """
>
> So it may or may not use the GIL, but there are thread restrictions during
> an import.  The rule I try to follow is not to do anything non-trivial
> during top-level code of a module, except inside the
>  if __name__ == "__main__":
>
> portion.  If we're inside that portion, we're not a module, we're a script.
>
> Even better, import all your modules before starting any new threads.
>
> DaveA
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

No argue here.
The specific problem, as I wrote in reply to Terry, is with the execfile
statement. It might be a part of some online plugin machinery. In such case
the nature of the cause does not allow to execute it upfront.
I think that the problem can be circumvented by first reading the file
followed by compile and eval, as being done in Python3 (no execfile there).

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


Re: Wrestling with the Py2exe Install, Win7, Py2.5

2010-02-17 Thread mk

W. eWatson wrote:
I've finally decided to see if I could make an executable out of a py 
file. Win7. Py2.5. I brought down the install file and proceeded with 
the install. I got two warning messages. Forgot the first. The second 
said,"Could not set the key value." I again used OK. I think that was 
the only choice. It then issued a message in a larger dialog. It was 
about setting a key, and pointed me to a log. It mentions a Removepy2exe -u


Although it finished, I have no idea where the program is. It does not 
show up on the Start menu All Programs List nore my desktop. What's up?


I've had these messages (key) occur on other Python installs as I 
transition to Win7. So far no problem.




You may want to consider dumping the thing and going for PyInstaller, 
which in my experience is better and has friendly developer community 
behind it.


Regards,
mk

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


Re: The future of "frozen" types as the number of CPU cores increases

2010-02-17 Thread Daniel Stutzbach
On Tue, Feb 16, 2010 at 3:15 PM, John Nagle  wrote:

> One possible implementation would be
> to have unfrozen objects managed by reference counting and locking as
> in CPython.  Frozen objects would live in a different memory space and be
> garbage collected by a concurrent garbage collector.
>

A concurrent garbage collector for frozen object would still need to walk
unfrozen objects, to find references to frozen objects.


--
Daniel Stutzbach, Ph.D.
President, Stutzbach Enterprises, LLC 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Wrestling with the Py2exe Install, Win7, Py2.5

2010-02-17 Thread mk

W. eWatson wrote:


P.S. I didn't really use PyInstaller on Windows, though -- just on 
Linux, where it works beautifully.


Regards,
mk

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


Re: Which mock library do you prefer?

2010-02-17 Thread Lacrima
On Feb 16, 7:38 pm, Phlip  wrote:
>
> This paper _almost_ gets the 
> idea:http://www.netobjectives.com/download/Code%20Qualities%20and%20Practi...
>
>
> Do you run your tests after the fewest possible edits? Such as 1-3
> lines of code?
>

Hi!
I run my tests all the time (they almost replaced debugger in my IDE).
But there are times, when I can't just run tests after 1-3 lines of
code.
For example, I am developing an application that talks to some web
service. One of methods of a class, which implements API for a given
web service, should parse xml response from web service. At first, I
hardcoded returned values, so that they looked like already parsed.
But further, additional tests forced me to actually operate with
sample xml data instead of hardcoded values. So I created sample xml
file that resembled response from server. And after that I can't just
write 1-3 lines between each test. Because I need to read() the file
and sort it out in a loop (at least 6-9 lines of code for small xml
file). And only after this procedure I run my tests with the hope that
they all pass.
Maybe it's not proper TDD, but I can't figure out how to reduce period
between running tests in a case above.
-- 
http://mail.python.org/mailman/listinfo/python-list


Over(joy)riding

2010-02-17 Thread mk

Found in Dive into Python:

"""Guido, the original author of Python, explains method overriding this 
way: "Derived classes may override methods of their base classes. 
Because methods have no special privileges when calling other methods of 
the same object, a method of a base class that calls another method 
defined in the same base class, may in fact end up calling a method of a 
derived class that overrides it. (For C++ programmers: all methods in 
Python are effectively virtual.)" """


So, I set out to create such case:

class A(object):
def __init__(self):
print "A"

def met(self):
print "I'm A's method"

def overriden(self):
print "I'm A's method to be overriden"

def calling_overriden(self):
self.overriden()

class B(object):
def __init__(self):
print "B"

def met(self):
print "I'm B's method"


class C(A):
def __init__(self, arg):
print "C","arg=",arg
A.__init__(self)

def met(self):
print "I'm C's method"


class D(B):
def __init__(self, arg):
print "D", "arg=",arg
B.__init__(self)

def met(self):
print "I'm D's method"


class E(C,D):
def __init__(self, arg):
print "E", "arg=",arg
C.__init__(self, arg)
D.__init__(self, arg)

def some(self):
self.met()

def overriden(self):
print "I'm really E's method"

e = E(10)
print 'MRO:', ' '.join([c.__name__ for c in E.__mro__])
e.some()
e.calling_overriden()


Result:
...
MRO: E C A D B object
I'm C's method
I'm really E's method


Is what I concocted in e.calling_overriden() == what Guido said on base 
class sometimes calling overriden method instead of its own original method?


Regards,
mk

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


Re: Shipping Executables

2010-02-17 Thread rodmc

> > Hi Rod,
> > The user's ability to hack into the code is usually considered one of
> > the strengths of Python & open source software in general. Since most
> > Python software that's distributed  is open source, you're doing
> > something different than most. It'd help if you explain how you want
> > your software to differ from a typical open source distribution. Do you
> > not want people to change the code? Are you worried about your code &
> > ideas being stolen?

Thanks to everyone for their replies.

Normally I have no problem with adopting an open source model, indeed
I usually encourage it. However the main problem was related to end-
user licencing e.g. via some form of registration key. The other
problem was related to end-user private data and sending this via a
secure Internet connection. While I am ok with secure Internet
connection side of it I was concerned that with the source code being
available to others the security may in some way be reduced- however I
note one reply which says this is not the case.


Kind regards,

rod

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


Re: listing existing windows services with python

2010-02-17 Thread Tim Golden

On 16/02/2010 13:51, Alf P. Steinbach wrote:

It doesn't seem to provide ordinary Windows "service"s, but it's a bit unclear
since e.g. the URL above says


[... snip ...]

Well the useful info in there appears to come from:

  http://msdn.microsoft.com/en-us/library/aa392783%28VS.85%29.aspx

Looking around the matter, I think I'm taking the view that this
is an implementation detail of WMI.

However you use it, WMI is a layer on a layer on layer
(especially if you're using it in Python with my module!)
and it's never going to be the fastest thing going. One
contributory factor will be this particular model of
process-within-process. But I don't feel that it would
help anybody if I were to mention it on my webpages as
there's nothing the casual user can do about it.

In general I tend to unrecommend WMI for something small
in an app which doesn't otherwise use it, unless the
convenience (and it *is* very convenient sometimes)
outweighs the slight performance issue.

Thanks for the heads-up anyway, Alf. I need to get up
to speed with the Vista & W7 issues referred to on
that page as well.

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


Re: Over(joy)riding

2010-02-17 Thread Arnaud Delobelle
mk  writes:

> Found in Dive into Python:
>
> """Guido, the original author of Python, explains method overriding
> this way: "Derived classes may override methods of their base
> classes. Because methods have no special privileges when calling other
> methods of the same object, a method of a base class that calls
> another method defined in the same base class, may in fact end up
> calling a method of a derived class that overrides it. (For C++
> programmers: all methods in Python are effectively virtual.)" """
>
> So, I set out to create such case:
>
> class A(object):
> def __init__(self):
> print "A"
>
> def met(self):
> print "I'm A's method"
>
> def overriden(self):
> print "I'm A's method to be overriden"
>
> def calling_overriden(self):
> self.overriden()
[...]
> class E(C,D):
> def __init__(self, arg):
> print "E", "arg=",arg
> C.__init__(self, arg)
> D.__init__(self, arg)
>
> def some(self):
> self.met()
>
> def overriden(self):
> print "I'm really E's method"
>
> e = E(10)
> print 'MRO:', ' '.join([c.__name__ for c in E.__mro__])
> e.some()
> e.calling_overriden()
>
>
> Result:
> ...
> MRO: E C A D B object
> I'm C's method
> I'm really E's method
>
>
> Is what I concocted in e.calling_overriden() == what Guido said on
> base class sometimes calling overriden method instead of its own
> original method?

Yes!

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


Re: Which mock library do you prefer?

2010-02-17 Thread Lacrima
On Feb 16, 10:30 pm, Ben Finney  wrote:
> Lacrima  writes:
> > And I have already refused to write totally isolated tests, because it
> > looks like a great waste of time.
>
> It only looks like that until you chase your tail in a long, fruitless
> debugging session because (you later realise) the behaviour of one test
> is being affected by another. Test isolation is essential to ensure that
> your tests are doing what you think they're doing.
>
> --
>  \       “A ‘No’ uttered from deepest conviction is better and greater |
>   `\       than a ‘Yes’ merely uttered to please, or what is worse, to |
> _o__)                              avoid trouble.” —Mohandas K. Gandhi |
> Ben Finney

Hi!

Right, isolation is essential. But I can't decide to which extent I
should propagate isolation.
For example, in "Python Testing: Beginner's Guide" by Daniel Arbuckle,
author suggests that if you do unittesting you should isolate the
smallest units of code from each other. For example, if you have a
class:
Class SomeClass(object):
def method1(self):
return 5
def method2(self):
return self.method1 + 10

According to the book, if you want to test method2, you should isolate
it from method1 and class instance('self').
Other books are not so strict...

And what should I follow as newbie?

Currently, I don't create mocks of units if they are within the same
class with the unit under test. If that is not right approach, please,
explain what are best practices... I am just learning TDD..

with regards,
Maxim
-- 
http://mail.python.org/mailman/listinfo/python-list


Is automatic reload of a module available in Python?

2010-02-17 Thread R (Chandra) Chandrasekhar

Dear Folks,

I am currently developing a python program, let us call it "generic.py", 
and I am testing out the functions therein by testing them out 
interactively in the python interpreter by invoking python and doing


import generic

Once I hit an error, I need to revise my file and reload the module using

reload(generic)

The difference in syntax between invoking import and reload is really 
costing me time and patience.


Therefore, I would like to ask:

1. Is there a method of auto-reloading a module while developing it and 
testing it with the interactive python interpreter?


2. Is there a better way of developing a program?

Thank you.

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


Re: Over(joy)riding

2010-02-17 Thread mk

Arnaud Delobelle wrote:

Is what I concocted in e.calling_overriden() == what Guido said on
base class sometimes calling overriden method instead of its own
original method?


Yes!


For a change I achieved resounding success with Python. :-)

P.S. Method resolution order in Python makes me want to kill small kittens.

Regards,
mk

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


Re: How to efficiently extract information from structured text file

2010-02-17 Thread Imaginationworks
On Feb 16, 7:14 pm, Gary Herron  wrote:
> Imaginationworks wrote:
> > Hi,
>
> > I am trying to read object information from a text file (approx.
> > 30,000 lines) with the following format, each line corresponds to a
> > line in the text file.  Currently, the whole file was read into a
> > string list using readlines(), then use for loop to search the "= {"
> > and "};" to determine the Object, SubObject,and SubSubObject. My
> > questions are
>
> > 1) Is there any efficient method that I can search the whole string
> > list to find the location of the tokens(such as '= {' or '};'
>
> Yes.   Read the *whole* file into a single string using file.read()
> method, and then search through the string using string methods (for
> simple things) or use re, the regular expression module, (for more
> complex searches).    
>
> Note:  There is a point where a file becomes large enough that reading
> the whole file into memory at once (either as a single string or as a
> list of strings) is foolish.    However, 30,000 lines doesn't push that
> boundary.
>
> > 2) Is there any efficient ways to extract the object information you
> > may suggest?
>
> Again, the re module has nice ways to find a pattern, and return parse
> out pieces of it.   Building a good regular expression takes time,
> experience, and a bit of black magic...    To do so for this case, we
> might need more knowledge of your format.  Also regular expressions have
> their limits.  For instance, if the sub objects can nest to any level,
> then in fact, regular expressions alone can't solve the whole problem,
> and you'll need a more robust parser.
>
> > Thanks,
>
> > - Jeremy
>
> > = Structured text file =
> > Object1 = {
>
> > ...
>
> > SubObject1 = {
> > 
>
> > SubSubObject1 = {
> > ...
> > };
> > };
>
> > SubObject2 = {
> > 
>
> > SubSubObject21 = {
> > ...
> > };
> > };
>
> > SubObjectN = {
> > 
>
> > SubSubObjectN = {
> > ...
> > };
> > };
> > };
>
>

Gary and Rhodri, Thank you for the suggestions.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Over(joy)riding

2010-02-17 Thread Bruno Desthuilliers

mk a écrit :

P.S. Method resolution order in Python makes me want to kill small kittens.


mro is only a "problem" when using MI.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Is automatic reload of a module available in Python?

2010-02-17 Thread Arnaud Delobelle
"R (Chandra) Chandrasekhar"  writes:

> Dear Folks,
>
> I am currently developing a python program, let us call it
> "generic.py", and I am testing out the functions therein by testing
> them out interactively in the python interpreter by invoking python
> and doing
>
> import generic
>
> Once I hit an error, I need to revise my file and reload the module using
>
> reload(generic)
>
> The difference in syntax between invoking import and reload is really
> costing me time and patience.
>
> Therefore, I would like to ask:
>
> 1. Is there a method of auto-reloading a module while developing it
> and testing it with the interactive python interpreter?
>
> 2. Is there a better way of developing a program?
>
> Thank you.
>
> Chandra

Here is a very simple way to improve what you do, which won't require
you to change the way you work or to learn a new paradigm:

Instead of testing your functions interactively, put your testing code
in a file, e.g. 'program_tests.py'.  Your can then type

python program_tests.py

at the shell interactive prompt.  To perform the tests again, just
re-execute that file.  If your tests are divided into
different units, you can put these in functions:

def test_frobz():
#testing code for frobzation of klops

def test_frizz():
#testing code for frizzment of frobzied klops

# etc..

So if you want to keep doing interactive tests, you can import
program_tests and call whichever testing functions you want.  You may
even have arguments to those functions to test them with different
parameters.

I know some people will point at more 'pro' ways of testing but this has
the merit of being very straightforward.  Then when you move on to more
sophisticated techniques, I think you will understand better the
motivations behind them.

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


How to build a list of all modules in standard library?

2010-02-17 Thread python
We're building a py2exe executable that may need to do some
dynamic module imports.

I'm looking for suggestions on how we can mechanically generate a
list of standard library modules/packages to make sure our build
has the full set of Python 2.6.4 libraries.

We're planning on creating a module called stdlib.py that
explictly imports all modules within an "if False:" block of code
per example below and then importing this script (stdlib.py) in
our main script.

# stdlib.py - insure that py2exe imports all modules in its build

if False:
 # list of all standard modules
 import 
 ...
 import 

 # list of 3rd party modules
 import win32api
 import wmi
 ...

Any suggestions or feedback appreciated.

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


Re: Over(joy)riding

2010-02-17 Thread mk

Bruno Desthuilliers wrote:

mk a écrit :
P.S. Method resolution order in Python makes me want to kill small 
kittens.


mro is only a "problem" when using MI.


Oh sure! And I have the impression that multiple inheritance is not used 
all that often. What (some) Python code I've read in open source 
projects typically uses single inheritance.


Nevertheless MI is there in Python, might be useful in some cases and 
Guido put it there for some reason, so Citizens Aspiring To Become 
Pythonistas like me work to learn it.


Regards,
mk

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


Recommendations for cloud based file storage service with Python API?

2010-02-17 Thread python
I'm looking for recommendations on a cloud based file storage
service with a Python API.

Would appreciate hearing feedback from anyone using Python to
interface to Amazon S3, RackSpace Cloud Files, Microsoft Azure,
Rsync.net, or other hosted file storage service. What services do
you recommend or suggest we avoid? Are there Python specific
issues (such as lagging library updates when an API gets updated)
I should be concerned about?

Our use case is mananging thousands of 100K-500K data files owned
by hundreds of users.

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


Re: Is automatic reload of a module available in Python?

2010-02-17 Thread Jean-Michel Pichavant

R (Chandra) Chandrasekhar wrote:

Dear Folks,

I am currently developing a python program, let us call it 
"generic.py", and I am testing out the functions therein by testing 
them out interactively in the python interpreter by invoking python 
and doing


import generic

Once I hit an error, I need to revise my file and reload the module using

reload(generic)

The difference in syntax between invoking import and reload is really 
costing me time and patience.


Therefore, I would like to ask:

1. Is there a method of auto-reloading a module while developing it 
and testing it with the interactive python interpreter?


2. Is there a better way of developing a program?

Thank you.

Chandra
You will always find people explaining to you how it can be possible if 
you really know precisely how python object model works. If you actually 
do, you can possibly read their technics.
But my short answer would be "No, there is no reliable way to reload a 
module".


Still there is 'reload' builting function you can call on any module, by 
you must understand that it won't affect any other object that the 
module itself. New objects created from that module will take effects, 
but all objects created before won't.


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


Re: Over(joy)riding

2010-02-17 Thread Kurt Smith
On Wed, Feb 17, 2010 at 9:08 AM, mk  wrote:
> Bruno Desthuilliers wrote:
>>
>> mk a écrit :
>>>
>>> P.S. Method resolution order in Python makes me want to kill small
>>> kittens.
>>
>> mro is only a "problem" when using MI.
>
> Oh sure! And I have the impression that multiple inheritance is not used all
> that often. What (some) Python code I've read in open source projects
> typically uses single inheritance.
>
> Nevertheless MI is there in Python, might be useful in some cases and Guido
> put it there for some reason, so Citizens Aspiring To Become Pythonistas
> like me work to learn it.

In case you're not familiar with it, MI allows you to have mixins &
traits.  They work very well if the mixin superclasses don't have any
clashes with the other superclasses, so each mixin adds its own unique
set of methods to the derived class.  Then you don't have to worry
about mro and all that (at least as much).

Here's an article on it, with examples:

http://www.linuxjournal.com/node/4540/print

Michele Simionato has some criticism of MI pitfalls and has come up
with the straits module to remedy it -- you might be interested.  He
goes into detail here:

http://www.artima.com/weblogs/viewpost.jsp?thread=246488

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


Re: Wrap and intercept function calls

2010-02-17 Thread Dan Yamins
Really, nobody has any idea about this?   (Sorry to repost.)

On Tue, Feb 16, 2010 at 7:29 PM, Dan Yamins  wrote:

> Hi:
>
> I'm wondering what the best way to wrap and modify function calls is.
> Essentially what I want to achieve is to have a function like this:
>
> def Wrap(frame,event,arg):
>  if event == 'call':
> result = PerformCheck(GetArgumentsFromFrame(frame))
> if Condition(result):
> return result
> else:
> return [normal function call]
>
> called whenever a "call" event is about to occur.
>
> When I say "return result" I mean:  return that data object instead of what
> the function would have returned, and prevent execution of the function.
>
>
> Is there any way to do this using sys.settrace?  Or perhaps something from
> the bdb or pbd module?
>
>
In other words, what I'm looking for is a way to intercept all function
calls with a "wrapper" --  like one can do using settrace and other
debugging methods -- but, unlike the debugging methods, have the "wrapping"
function actually be able to intervene in the stack and, for instance,
conditionally replace the function call's return with something determined
in the wrapping function and prevent the function call's execution.   I want
to be able to do this by setting a single system-wide (or at any rate,
thread-wide) value, like with settrace, and not have to modify individual
functions one by one.

Could I, for example, set a settrace function that somehow modifies the
stack?  Or is there some much better way to do this?  Or, if someone can
tell me that this can't be done without having to roll my own implementation
of the Python interpreter, that would be great to know too.

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


Re: Recommendations for cloud based file storage service with Python API?

2010-02-17 Thread Robert Kern

On 2010-02-17 09:10 AM, pyt...@bdurham.com wrote:

I'm looking for recommendations on a cloud based file storage service
with a Python API.
Would appreciate hearing feedback from anyone using Python to interface
to Amazon S3, RackSpace Cloud Files, Microsoft Azure, Rsync.net, or
other hosted file storage service. What services do you recommend or
suggest we avoid? Are there Python specific issues (such as lagging
library updates when an API gets updated) I should be concerned about?


I don't have any deep experience with using any of them, it is worth noting that 
the Python interface to the RackSpace Cloud Files API is implemented by 
RackSpace itself and is the premier interface to their API. It won't lag behind 
the API.


--
Robert Kern

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

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


Re: Which mock library do you prefer?

2010-02-17 Thread Phlip
Lacrima wrote:

> I run my tests all the time (they almost replaced debugger in my IDE).
> But there are times, when I can't just run tests after 1-3 lines of
> code.
...
> Maybe it's not proper TDD

You are still being too literal. The "1-3 lines of code" guideline is
a guideline, not a rule. It means 1 small edit is best, 2 edits are
mostly harmless, 3 is okay, 4 is acceptable, and so on. It's the peak
of the Zipf's Law curve:

http://www.rimmkaufman.com/content/mobydick.png

You "mocked the wire" with that hardcoded XML so that your subsequent
edits can be very short and reliable. Props!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The future of "frozen" types as the number of CPU cores increases

2010-02-17 Thread John Nagle

Daniel Stutzbach wrote:

On Tue, Feb 16, 2010 at 3:15 PM, John Nagle  wrote:


One possible implementation would be
to have unfrozen objects managed by reference counting and locking as
in CPython.  Frozen objects would live in a different memory space and be
garbage collected by a concurrent garbage collector.



A concurrent garbage collector for frozen object would still need to walk
unfrozen objects, to find references to frozen objects.


   Right.  But the reverse is not the case.  Reference count updating
wouldn't need to look at frozen object space.

   One way to implement locking is something like this:

   Mutable objects have a reference count field, a lock field,
and an "owner" field.  Initially, the owner of an object is its thread.
If an object's only reference is a field of a synchronized object, the
owner is the synchronized object.  Mutable objects with an owner can
be manipulated by that owner without locking.

   When an object reference is passed to another thread or
another synchronized object, the object becomes multi-owner and
the "owner" field is set to null.  Thereafter, the object must
be locked during updates.

   The headache with this is that when an object becomes multi-owner,
so does everything it points to.  So there's a transient as the
system runs down the references, locking objects momentarily and
clearing owner fields.

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


Re: Over(joy)riding

2010-02-17 Thread mk

Kurt Smith wrote:


In case you're not familiar with it, MI allows you to have mixins &
traits.  They work very well if the mixin superclasses don't have any
clashes with the other superclasses, so each mixin adds its own unique
set of methods to the derived class.  Then you don't have to worry
about mro and all that (at least as much).

Here's an article on it, with examples:

http://www.linuxjournal.com/node/4540/print

Michele Simionato has some criticism of MI pitfalls and has come up
with the straits module to remedy it -- you might be interested.  He
goes into detail here:

http://www.artima.com/weblogs/viewpost.jsp?thread=246488


Thanks Kurt, I will certainly look into that!

Regards,
mk

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


Referring to class methods in class attributes

2010-02-17 Thread mk

Hello everyone,

OK so I have this:

def print_internal_date(filename):
f = open(filename + 'c', "rb")
data = f.read(8)
mtime = struct.unpack("It works. But if I'd like to def print_internal_date in PYFileInfo body 
like so:


class PYFileInfo(FileInfo):
'python file properties'

def print_internal_date(self, filename):
f = open(filename + 'c', "rb")
data = f.read(8)
mtime = struct.unpack("
class PYFileInfo(FileInfo):
  File "C:/mp3i/finfo2.py", line 64, in PYFileInfo
'internal_date': PYFileInfo.print_internal_date
NameError: name 'PYFileInfo' is not defined


Hmm do I get that class data attributes are defined before class is 
defined?!


Why I would like to do it this way:

1. keeping all the stuff specific to PYFileInfo where it belongs, in a 
class: the print_internal_method does nothing but read timestamp from a 
.pyc file, so it's not a generic function.


2. I guess I could define tagdata in __init__ and be done with it: 
however, it's an instance attribute then and not class attribute: a few 
bytes are wasted. Seriously, however, suppose tagdata or smth like this 
is really large? It would make sense to make it class attribute and not 
instance attribute. So I'm trying to work out if there's a way to do it.



Regards,
mk

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


Re: Referring to class methods in class attributes

2010-02-17 Thread Stephen Hansen
On Wed, Feb 17, 2010 at 9:38 AM, mk  wrote:

> It works. But if I'd like to def print_internal_date in PYFileInfo body
> like so:
>
> class PYFileInfo(FileInfo):
>'python file properties'
>
>def print_internal_date(self, filename):
>f = open(filename + 'c', "rb")
>data = f.read(8)
>mtime = struct.unpack("return time.asctime(time.gmtime(mtime[0]))
>
>tagdata = {'compiled_fname': lambda x: x + 'c',
>'size': os.path.getsize,
>'internal_date': PYFileInfo.print_internal_date
>}
>

You don't have to (and can't) refer to the class within the body. Class
statements are sort of... odd. They are code which is directly executed, and
the results are then passed into a metaclass/type/whatever and a class
object is created. While within the class body, the class doesn't exist yet.

But you don't need it to.

Just do:

   'internal_date': print_internal_date

The 'def' is in the same local scope as 'tagdata' is.

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


mixins and new style classes

2010-02-17 Thread mk

>>> class Person(object):
... pass
...
>>> class Friendly(object):
... def hello(self):
... print 'hello'
...
>>>
>>> Person.__bases__ += (Friendly,)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: Cannot create a consistent method resolution
order (MRO) for bases object, Friendly




But it works for old-style classes:


>>> class Person:
... pass
...
>>> class Friendly:
... def hello(self):
... print 'hello'
...
>>>
>>> p = Person()
>>>
>>> Person.__bases__ += (Friendly,)
>>>
>>> p.hello()
hello


Python is 2.6.1.

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


Traversing through variable-sized lists

2010-02-17 Thread Andrej Mitrovic
Hi,

I couldn't figure out a better description for the Subject line, but
anyway, I have the following:

_num_frames = 32
_frames = range(0, _num_frames) # This is a list of actual objects,
I'm just pseudocoding here.
_values = [0, 1, 2, 3, 4]

I want to call a function of _frames for each frame with a _values
argument, but in a way to "spread out" the actual values.

I would want something similar to the following to be called:

_frames[0].func(_values[0])
_frames[1].func(_values[0])
_frames[2].func(_values[0])
_frames[3].func(_values[0])
_frames[4].func(_values[1])
_frames[5].func(_values[1])
_frames[6].func(_values[1])
_frames[7].func(_values[1])
_frames[8].func(_values[2])
...etc...

Both the _values list and _frames list can be of variable and uneven
size, which is what is giving me the problems. I'm using Python 2.6.

I've tried the following workaround, but it often gives me inaccurate
results (due to integer division), so I had to add a safety check:

num_frames = 32
values = [0, 1, 2, 3, 4]
offset_step = num_frames / len(values)
for index in xrange(0, num_frames):
offset = index / offset_step
if offset > offset_values[-1]:
offset = offset_values[-1]
frames[index].func(values[offset])

There has to be a better way to do this. I'd appreciate any help.
Cheers!
-- 
http://mail.python.org/mailman/listinfo/python-list


Timer

2010-02-17 Thread Victor Subervi
Hi;
I have the following css:


.splash { position:absolute; left:0px; top:0px; z-index:2 }
.page { position:absolute; left:0px; top:0px; z-index:1 }
.text {  font-family: Arial, Helvetica, sans-serif; font-size: 16px;
text-decoration: none; text-align: justify}


and the following python:

  if new is not None:
print ''
print 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0";
WIDTH="%s" HEIGHT="%s" id="myMovieName">' % (str(wn*1008), str(wn*200))
print '''


'''
print 'http://www.macromedia.com/go/getflashplayer";>' % (str(wn*1008),
str(wn*200))
print ''
print ''
Timer(5, removeCSS, ()).start()

Obviously, the removeCSS isn't going to work in that last line. What can I
put there to remove the splash page after 5 seconds?
TIA,
beno

-- 
The Logos has come to bear
http://logos.13gems.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Timer

2010-02-17 Thread Gabriel
On Wed, Feb 17, 2010 at 3:14 PM, Victor Subervi  wrote:
> Hi;
> I have the following css:
> 
> .splash { position:absolute; left:0px; top:0px; z-index:2 }
> .page { position:absolute; left:0px; top:0px; z-index:1 }
> .text {  font-family: Arial, Helvetica, sans-serif; font-size: 16px;
> text-decoration: none; text-align: justify}
> 
> and the following python:
>   if new is not None:
>     print ''
>     print ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0";
> WIDTH="%s" HEIGHT="%s" id="myMovieName">' % (str(wn*1008), str(wn*200))
>     print '''
> 
> 
> '''
>     print ' quality=high bgcolor=#FF WIDTH="%s" HEIGHT="%s" NAME="myMovieName"
> ALIGN="" TYPE="application/x-shockwave-flash"
> PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer";>' %
> (str(wn*1008), str(wn*200))
>     print ''
>     print ''
>     Timer(5, removeCSS, ()).start()
> Obviously, the removeCSS isn't going to work in that last line. What can I
> put there to remove the splash page after 5 seconds?
> TIA,
> beno


I don't think this has anything to do with python.

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


Re: Timer

2010-02-17 Thread Dotan Cohen
> What can I
> put there to remove the splash page after 5 seconds?
>

Javascript.

-- 
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il

Please CC me if you want to be sure that I read your message. I do not
read all list mail.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Referring to class methods in class attributes

2010-02-17 Thread mk

Stephen Hansen wrote:

You don't have to (and can't) refer to the class within the body. Class 
statements are sort of... odd. They are code which is directly executed, 
and the results are then passed into a metaclass/type/whatever and a 
class object is created. While within the class body, the class doesn't 
exist yet.


But you don't need it to.

Just do:

   'internal_date': print_internal_date

The 'def' is in the same local scope as 'tagdata' is.


Thanks, that worked. But in order to make it work I had to get rid of 
'self' in print_internal_date signature, bc all other functions in 
tagdata have only a single argument:


class PYFileInfo(FileInfo):
'python file properties'

def print_internal_date(filename):
...

tagdata = {'compiled_fname': lambda x: x + 'c',
'size': os.path.getsize,
'internal_date': print_internal_date
}

That looks weird: a method with no 'self'. Hmm that is probably 
seriously wrong.


This obviously means no other method can call it like 
self.print_internal_date(), because self would get passed as first 
argument, yes?


I checked that print_internal_date can be called on an instance, so the 
same method can be seen as:


- class method -- when used in local scope definition like tagdata, or
- instance method -- when called from an instance or from self?

It should be called __print_internal_date really. ;-)

I wonder if I'm not trying to make Python things it shouldn't be doing, 
but it's the problem at hand that is leading me into this conundrum: all 
other functions for tagdata use single arguments. I should probably code 
around that anyway..


Regards,
mk



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


Re: Timer

2010-02-17 Thread Stephen Hansen
On Wed, Feb 17, 2010 at 10:14 AM, Victor Subervi wrote:

> Obviously, the removeCSS isn't going to work in that last line. What can I
> put there to remove the splash page after 5 seconds?
>

Even though you're generating this with python, it doesn't have anything to
do with Python. You'll have to use javascript and DHTML and enable/disable
classes. How to do that is beyond the scope of this group, and might depend
on the browser. I'd go look into jQuery or some such which will encapsulate
such dynamic things better.

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


Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

2010-02-17 Thread John Bokma
Jonathan Gardner  writes:

> Then I looked at a stack trace from a different programming language
> with lots of anonymous functions. (I believe it was perl.)
>
> I became enlightened.

If it was Perl [1], I doubt it. Because line numbers are reported, and
if that doesn't help you, you can annotate anonymous functions with a
nick name using

local *__ANON__ = 'nice name';

Finding an issue, and not looking for a solution is not called becoming
enlightened ;-)


~$ perl -e '
use Carp;

my $anon = sub { local *__ANON__ = "hello, world"; croak "oops"; };
$anon->();
'

oops at -e line 4
main::hello, world() called at -e line 5

As you can see, and a line number is generated, and the nice name is
shown.

If you generate anonymouse functions on the fly based on parameters, you
can encode this into the nice name, of course.

Sadly, often bold statements about a language are made in ignorance.


[1] perl is the program that executes Perl programs ;-).

-- 
John Bokma   j3b

Hacking & Hiking in Mexico -  http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Referring to class methods in class attributes

2010-02-17 Thread Stephen Hansen
On Wed, Feb 17, 2010 at 10:38 AM, mk  wrote:

> Thanks, that worked. But in order to make it work I had to get rid of
> 'self' in print_internal_date signature, bc all other functions in tagdata
> have only a single argument:
>

Right, I should have caught that.

You can make print_internal_date a @staticmethod.

Or just leave it as a top level function where it was perfectly happy to
live :)


> That looks weird: a method with no 'self'. Hmm that is probably seriously
> wrong.
>

> This obviously means no other method can call it like
> self.print_internal_date(), because self would get passed as first argument,
> yes?
>

It doesn't have to be. It could be a class method-- @classmethod does that,
makes it receive 'cls' the actual class as the first argument. Or a
@staticmethod, in which case it has no first-argument at all.


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


Re: Referring to class methods in class attributes

2010-02-17 Thread Jean-Michel Pichavant

mk wrote:

Stephen Hansen wrote:

You don't have to (and can't) refer to the class within the body. 
Class statements are sort of... odd. They are code which is directly 
executed, and the results are then passed into a 
metaclass/type/whatever and a class object is created. While within 
the class body, the class doesn't exist yet.


But you don't need it to.

Just do:

   'internal_date': print_internal_date

The 'def' is in the same local scope as 'tagdata' is.


Thanks, that worked. But in order to make it work I had to get rid of 
'self' in print_internal_date signature, bc all other functions in 
tagdata have only a single argument:


class PYFileInfo(FileInfo):
'python file properties'

def print_internal_date(filename):
...

tagdata = {'compiled_fname': lambda x: x + 'c',
'size': os.path.getsize,
'internal_date': print_internal_date
}

That looks weird: a method with no 'self'. Hmm that is probably 
seriously wrong.

[snip]



Absolutely not. It's called a static method, you have to decorate it 
with @staticmethod though.


class A:
   @staticmethod
   def getInstance():
  return A()

a = A.getInstance()

you have also the class method version:

class B:
   @classmethod:
   def getInstance(cls): # class methods take a class as first parameter
  return cls()


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


Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

2010-02-17 Thread cjw

On 17-Feb-10 05:48 AM, Bruno Desthuilliers wrote:

Lawrence D'Oliveiro a écrit :

In message <60b1abce-4381-46ab-91ed-
f2ab2154c...@g19g2000yqe.googlegroups.com>, Andrej Mitrovic wrote:


Also, lambda's are expressions, not statements ...


Is such a distinction Pythonic, or not?


Python is (by design) a statement-based language, so yes, this
distinction is pythonic !-)

Aren't lambda forms better described as function?

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


Re: Traversing through variable-sized lists

2010-02-17 Thread Matt McCredie
> I've tried the following workaround, but it often gives me inaccurate
> results (due to integer division), so I had to add a safety check: 
>
> num_frames = 32
> values = [0, 1, 2, 3, 4]
> offset_step = num_frames / len(values)
> for index in xrange(0, num_frames):
> offset = index / offset_step
> if offset > offset_values[-1]:
> offset = offset_values[-1]
> frames[index].func(values[offset])
> 
> There has to be a better way to do this. I'd appreciate any help.
> Cheers!
> 

This is how I would do it, assuming you just want to call the remaining frames 
with the last value.

from itertools import izip

def stretch(seq, n):
for val in seq:
for i in xrange(n):
yield val
while True:
yield val

frames_per_value = num_frames // len(values)
for frame, value in izip(frames, stretch(values, frames_per_value)):
frame.func(value)


Matt



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


Re: Traversing through variable-sized lists

2010-02-17 Thread Peter Pearson
On Wed, 17 Feb 2010 10:10:37 -0800 (PST), Andrej Mitrovic wrote:
[snip]
> _num_frames = 32
> _frames = range(0, _num_frames) # This is a list of actual objects,
> I'm just pseudocoding here.
> _values = [0, 1, 2, 3, 4]
>
> I want to call a function of _frames for each frame with a _values
> argument, but in a way to "spread out" the actual values.
>
> I would want something similar to the following to be called:
>
> _frames[0].func(_values[0])
> _frames[1].func(_values[0])
> _frames[2].func(_values[0])
> _frames[3].func(_values[0])
> _frames[4].func(_values[1])
> _frames[5].func(_values[1])
> _frames[6].func(_values[1])
> _frames[7].func(_values[1])
> _frames[8].func(_values[2])
> ...etc...
>
> Both the _values list and _frames list can be of variable and uneven
> size, which is what is giving me the problems. I'm using Python 2.6.
>
> I've tried the following workaround, but it often gives me inaccurate
> results (due to integer division), so I had to add a safety check:
>
> num_frames = 32
> values = [0, 1, 2, 3, 4]
> offset_step = num_frames / len(values)
> for index in xrange(0, num_frames):
> offset = index / offset_step
> if offset > offset_values[-1]:
> offset = offset_values[-1]
> frames[index].func(values[offset])

Here's my suggestion:

Python 2.5.2 (r252:60911, Jul 22 2009, 15:35:03) 
>>> import math
>>> import itertools
>>> values = [ 1, 2, 3 ]
>>> f = list( lambda x=i : x for i in range( 10 ) )
>>> n = int( math.ceil( len(f)/float( len(values) ) ) )
>>> for ff, dd in zip( f, itertools.chain(*zip( *n*[values] )) ):
...   print "Function %d applied to data %d." % ( ff(dd), dd )
... 
Function 1 applied to data 1.
Function 1 applied to data 1.
Function 1 applied to data 1.
Function 1 applied to data 1.
Function 2 applied to data 2.
Function 2 applied to data 2.
Function 2 applied to data 2.
Function 2 applied to data 2.
Function 3 applied to data 3.
Function 3 applied to data 3.
>>> 

-- 
To email me, substitute nowhere->spamcop, invalid->net.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Traversing through variable-sized lists

2010-02-17 Thread Wolfram Hinderer
On 17 Feb., 19:10, Andrej Mitrovic  wrote:
> Hi,
>
> I couldn't figure out a better description for the Subject line, but
> anyway, I have the following:
>
> _num_frames = 32
> _frames = range(0, _num_frames) # This is a list of actual objects,
> I'm just pseudocoding here.
> _values = [0, 1, 2, 3, 4]
>
> I want to call a function of _frames for each frame with a _values
> argument, but in a way to "spread out" the actual values.
>
> I would want something similar to the following to be called:
>
> _frames[0].func(_values[0])
> _frames[1].func(_values[0])
> _frames[2].func(_values[0])
> _frames[3].func(_values[0])
> _frames[4].func(_values[1])
> _frames[5].func(_values[1])
> _frames[6].func(_values[1])
> _frames[7].func(_values[1])
> _frames[8].func(_values[2])
> ...etc...
>
> Both the _values list and _frames list can be of variable and uneven
> size, which is what is giving me the problems. I'm using Python 2.6.
>
> I've tried the following workaround, but it often gives me inaccurate
> results (due to integer division), so I had to add a safety check:
>
> num_frames = 32
> values = [0, 1, 2, 3, 4]
> offset_step = num_frames / len(values)
>     for index in xrange(0, num_frames):
>         offset = index / offset_step
>         if offset > offset_values[-1]:
>             offset = offset_values[-1]
>         frames[index].func(values[offset])
>
> There has to be a better way to do this. I'd appreciate any help.
> Cheers!

Python 3.1:
>>> def apply_spreaded(funcs, values):
... num_funcs = len(funcs)
... num_values = len(values)
... for i, func in enumerate(funcs):
... func(values[(i * num_values) // num_funcs])

>>> apply_spreaded([print] * 8, range(5))
0
0
1
1
2
3
3
4

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


Re: Traversing through variable-sized lists

2010-02-17 Thread John Posner

On 2/17/2010 1:10 PM, Andrej Mitrovic wrote:

Hi,

I couldn't figure out a better description for the Subject line, but
anyway, I have the following:

_num_frames = 32
_frames = range(0, _num_frames) # This is a list of actual objects,
I'm just pseudocoding here.
_values = [0, 1, 2, 3, 4]

I want to call a function of _frames for each frame with a _values
argument, but in a way to "spread out" the actual values.

I would want something similar to the following to be called:

_frames[0].func(_values[0])
_frames[1].func(_values[0])
_frames[2].func(_values[0])
_frames[3].func(_values[0])
_frames[4].func(_values[1])
_frames[5].func(_values[1])
_frames[6].func(_values[1])
_frames[7].func(_values[1])
_frames[8].func(_values[2])
...etc...


The lines above show that you are using two different series of index 
values. Each function call (more properly, "method call") has the form:


  frames[INDEX_FROM_FIRST_SERIES].func(INDEX_FROM_SECOND_SERIES)

(I've dropped the underscores in the names, for simplicity.) You're 
getting hung up trying to keep the two series of index values in sync. 
But you don't really need to. More below ...




Both the _values list and _frames list can be of variable and uneven
size, which is what is giving me the problems. I'm using Python 2.6.

I've tried the following workaround, but it often gives me inaccurate
results (due to integer division), so I had to add a safety check:

num_frames = 32
values = [0, 1, 2, 3, 4]
offset_step = num_frames / len(values)
 for index in xrange(0, num_frames):
 offset = index / offset_step
 if offset>  offset_values[-1]:
 offset = offset_values[-1]
 frames[index].func(values[offset])

There has to be a better way to do this. I'd appreciate any help.
Cheers!


As you've shown above, a "for" loop takes care of the first series of 
index values:


  for index in xrange(num_frames):   # "0" arg unnecessary
  frames[index].func(INDEX_FROM_SECOND_SERIES)

The second series of index values needs to look like this:

  0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3 ...

The trick is not to worry about matching the second series to the first 
series. Instead, create an "infinite" second series using a Python 
generator, and use as many of its values as you need. Don't worry about 
the unused values, because the series isn't *really* infinite. :-)


Here's an easy way to create the generator

  import itertools
  second_series_gen = (i/4 for i in itertools.count())

Now, every time you need another number from this series, use its next() 
method. So the above code becomes:


  for index in xrange(num_frames):
  frames[index].func(second_series_gen.next())

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


Re: Traversing through variable-sized lists

2010-02-17 Thread Peter Otten
Andrej Mitrovic wrote:

> Hi,
> 
> I couldn't figure out a better description for the Subject line, but
> anyway, I have the following:
> 
> _num_frames = 32
> _frames = range(0, _num_frames) # This is a list of actual objects,
> I'm just pseudocoding here.
> _values = [0, 1, 2, 3, 4]
> 
> I want to call a function of _frames for each frame with a _values
> argument, but in a way to "spread out" the actual values.
> 
> I would want something similar to the following to be called:
> 
> _frames[0].func(_values[0])
> _frames[1].func(_values[0])
> _frames[2].func(_values[0])
> _frames[3].func(_values[0])
> _frames[4].func(_values[1])
> _frames[5].func(_values[1])
> _frames[6].func(_values[1])
> _frames[7].func(_values[1])
> _frames[8].func(_values[2])
> ...etc...
> 
> Both the _values list and _frames list can be of variable and uneven
> size, which is what is giving me the problems. I'm using Python 2.6.
> 
> I've tried the following workaround, but it often gives me inaccurate
> results (due to integer division), so I had to add a safety check:
> 
> num_frames = 32
> values = [0, 1, 2, 3, 4]
> offset_step = num_frames / len(values)
> for index in xrange(0, num_frames):
> offset = index / offset_step
> if offset > offset_values[-1]:
> offset = offset_values[-1]
> frames[index].func(values[offset])
> 
> There has to be a better way to do this. I'd appreciate any help.
> Cheers!

I tried to apply

http://en.wikipedia.org/wiki/Bresenham's_line_algorithm

on the problem:

def bresenham(xitems, yitems):
x1 = len(xitems)
y1 = len(yitems)

assert y1 <= x1
assert x1 > 0

deltax = x1-1
deltay = y1-1
error = deltax // 2

yitems = iter(yitems)
y = next(yitems)

for x in xitems:
yield x, y
error -= deltay
if error < 0:
y = next(yitems)
error += deltax

if __name__ == "__main__":
def make_f(i):
def f(v):
return "%d --> %s" % (i, v)
return f
functions = [make_f(i) for i in range(11)]
values = ["b%s" % k for k in range(5)]
for f, v in bresenham(functions, values):
print f(v)

The implementation is derived from the code on the wikipedia page and 
untested.

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


Re: How to efficiently extract information from structured text file

2010-02-17 Thread Paul McGuire
On Feb 16, 5:48 pm, Imaginationworks  wrote:
> Hi,
>
> I am trying to read object information from a text file (approx.
> 30,000 lines) with the following format, each line corresponds to a
> line in the text file.  Currently, the whole file was read into a
> string list using readlines(), then use for loop to search the "= {"
> and "};" to determine the Object, SubObject,and SubSubObject.

If you open(filename).read() this file into a variable named data, the
following pyparsing parser will pick out your nested brace
expressions:

from pyparsing import *

EQ,LBRACE,RBRACE,SEMI = map(Suppress,"={};")
ident = Word(alphas, alphanums)
contents = Forward()
defn = Group(ident + EQ + Group(LBRACE + contents + RBRACE + SEMI))

contents << ZeroOrMore(defn | ~(LBRACE|RBRACE) + Word(printables))

results = defn.parseString(data)

print results

Prints:

[
 ['Object1',
   ['...',
['SubObject1',
  ['',
['SubSubObject1',
  ['...']
]
  ]
],
['SubObject2',
  ['',
   ['SubSubObject21',
 ['...']
   ]
  ]
],
['SubObjectN',
  ['',
   ['SubSubObjectN',
 ['...']
   ]
  ]
]
   ]
 ]
]

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


Re: plugin / intra process communication system

2010-02-17 Thread Diez B. Roggisch

IMHO the ones using interfaces usually immediatly implement them for some
cases - so you actually get the interface for you authentication
framework, and also implement an OpenID plugin for it. And this forms
one package. Then you can add new packages that implement other things.


So if I want to implement another authentication mechanism but doesn't
use any of the auth-frameworks code you mentioned you still would depend
on it.


This depends on your definition of depending... (pun intended).

Sure, the code floats around. But it isn't *used* by your application. 
Why do you bother so much?



I didn't say nobody does it, I just said it isn't needed to get a
working plugin system. If you don't want to have a separate package
*and* don't want to pull in any unused code, this is pretty much your
only option.


Do you know somebody/some project/system/... who does it?

Pulling unused code I definitely want to avoid so I probably will stay
with this way.


Well, it's simply duck-typing, so you find it all over the place. Out of 
my head I only know about e.g. TurboMail, that allows you to specify 
plugins via pkg_resources entry points & simply uses them.



Its "the plugin system", connecting "service providers" (plugins) and
"service users". In my example the "users" also are providers at the
same time, the service "auth", provides the service "wiki".


So it's hypothetical?

To summarize my point again: if you want explicit type-checking or 
zope-style interface implementation, but don't want any code that 
implements this (at least not imported) - you need an explicit package. 
If you don't want this, use duck-typing.




PS I removed you (diez) from the TO list as that bounced. Probably
related to the (not existing?) domain nospam.web.de


I post through NNTP, and the email is obfuscated for the obvious reasons.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

2010-02-17 Thread Terry Reedy

On 2/17/2010 1:51 PM, cjw wrote:

On 17-Feb-10 05:48 AM, Bruno Desthuilliers wrote:

Lawrence D'Oliveiro a écrit :

In message <60b1abce-4381-46ab-91ed-
f2ab2154c...@g19g2000yqe.googlegroups.com>, Andrej Mitrovic wrote:


Also, lambda's are expressions, not statements ...


Is such a distinction Pythonic, or not?


Python is (by design) a statement-based language, so yes, this
distinction is pythonic !-)

Aren't lambda forms better described as function?


They are expressions that evaluate to function objects nearly identical 
to that produced by the def statememt they abbreviate. The only 
difference is the .__name__ attribute.


Terry Jan Reedy




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


Re: Over(joy)riding

2010-02-17 Thread Terry Reedy

On 2/17/2010 8:53 AM, mk wrote:

Found in Dive into Python:

"""Guido, the original author of Python, explains method overriding this
way: "Derived classes may override methods of their base classes.
Because methods have no special privileges when calling other methods of
the same object, a method of a base class that calls another method
defined in the same base class, may in fact end up calling a method of a
derived class that overrides it. (For C++ programmers: all methods in
Python are effectively virtual.)" """

So, I set out to create such case:

class A(object):
def __init__(self):
print "A"

def met(self):
print "I'm A's method"

def overriden(self):
print "I'm A's method to be overriden"

def calling_overriden(self):
self.overriden()

class B(object):
def __init__(self):
print "B"

def met(self):
print "I'm B's method"


class C(A):
def __init__(self, arg):
print "C","arg=",arg
A.__init__(self)

def met(self):
print "I'm C's method"


class D(B):
def __init__(self, arg):
print "D", "arg=",arg
B.__init__(self)

def met(self):
print "I'm D's method"


class E(C,D):
def __init__(self, arg):
print "E", "arg=",arg
C.__init__(self, arg)
D.__init__(self, arg)

def some(self):
self.met()

def overriden(self):
print "I'm really E's method"

e = E(10)
print 'MRO:', ' '.join([c.__name__ for c in E.__mro__])
e.some()
e.calling_overriden()


Result:
...
MRO: E C A D B object
I'm C's method
I'm really E's method


Is what I concocted in e.calling_overriden() == what Guido said on base
class sometimes calling overriden method instead of its own original
method?


Much more complicated than needed for this point, but I believe yes.


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


Re: Python version of perl's "if (-T ..)" and "if (-B ...)"?

2010-02-17 Thread Hrvoje Niksic
Tim Chase  writes:

> if portion is None:
>   content = iter(f)

iter(f) will iterate over lines in the file, which doesn't fit with the
rest of the algorithm.  Creating an iterator that iterates over
fixed-size file chunks (in this case of length 1) is where the
two-argument form of iter comes in handy:

content = iter(lambda: f.read(1), '')
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is automatic reload of a module available in Python?

2010-02-17 Thread Terry Reedy

On 2/17/2010 9:27 AM, R (Chandra) Chandrasekhar wrote:

Dear Folks,

I am currently developing a python program, let us call it "generic.py",
and I am testing out the functions therein by testing them out
interactively in the python interpreter by invoking python and doing

import generic

Once I hit an error, I need to revise my file and reload the module using

reload(generic)


Reload is sufficiently flakey that it has been removed in 3.x. The 
problem is that it genearally only *partially* replaces the module, so 
that some code uses the old version and some the new. Guido tried to 
rewrite it but gave up and removed it. The most sensible way to 
completely remove a module is to shutdown and restart the interpreter.



The difference in syntax between invoking import and reload is really
costing me time and patience.

Therefore, I would like to ask:

1. Is there a method of auto-reloading a module while developing it and
testing it with the interactive python interpreter?

2. Is there a better way of developing a program?


This is what I now do.
Edit
# xxx/module.py



def _test():
  

if __name__ == '__main__': _test()

with IDLE and hit F5 to run and test. IDLE runs the file in a *fresh* 
subinterpreter as the main module and then, whether or not an exception 
is raised, switches to interactive mode, so one can interactively test 
any objects created (before any exception). The switch to interactive 
mode after running a file can also be done with a command line switch 
when using CPython directly.


Terry Jan Reedy


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


Re: How to build a list of all modules in standard library?

2010-02-17 Thread Terry Reedy

On 2/17/2010 9:57 AM, pyt...@bdurham.com wrote:

We're building a py2exe executable that may need to do some dynamic
module imports.
I'm looking for suggestions on how we can mechanically generate a list
of standard library modules/packages to make sure our build has the full
set of Python 2.6.4 libraries.


Find the source file for the global module index that comes with the doc 
set.


Terry Jan Reedy

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


Re: Referring to class methods in class attributes

2010-02-17 Thread Bruno Desthuilliers
mk a écrit :
> Stephen Hansen wrote:
> 
>> You don't have to (and can't) refer to the class within the body.
>> Class statements are sort of... odd. They are code which is directly
>> executed, and the results are then passed into a
>> metaclass/type/whatever and a class object is created. While within
>> the class body, the class doesn't exist yet.
>>
>> But you don't need it to.
>>
>> Just do:
>>
>>'internal_date': print_internal_date
>>
>> The 'def' is in the same local scope as 'tagdata' is.
> 
> Thanks, that worked. But in order to make it work I had to get rid of
> 'self' in print_internal_date signature

Indeed. Using it that way, the print_internal_date will not be wrapped
in a method object.

> bc all other functions in
> tagdata have only a single argument:
> 
> class PYFileInfo(FileInfo):
> 'python file properties'
> 
> def print_internal_date(filename):
> ...
> 
> tagdata = {'compiled_fname': lambda x: x + 'c',
> 'size': os.path.getsize,
> 'internal_date': print_internal_date
> }
> 
> That looks weird: a method with no 'self'.

It's not a method.

> Hmm that is probably
> seriously wrong.
> 
> This obviously means no other method can call it like
> self.print_internal_date(), because self would get passed as first
> argument, yes?

Unless you make it a staticmethod.

> I checked that print_internal_date can be called on an instance, so the
> same method

s/method/function/


> can be seen as:
> 
> - class method -- when used in local scope definition like tagdata, or
> - instance method -- when called from an instance or from self?

Mmmm... Let's try to explain the whole damn thing. It's really (and IMHO
beautifully) simple once you get it, but I agree it's a bit peculiar
when compared to most mainstream OO languages.

The first thing is that the def statement *always* yield a function
object. Always. If you don't believe it, try the following snippet:

class Foo(object):
def bar(self):
return "baaz"

print Foo.__dict__.keys()
print type(Foo.__dict__['bar'])


So, why is it that type(Foo.bar) != type(Foo.__dict__['bar']) ? The
answer is : attribute lookup rules and the descriptor protocol.

To make a long story short, the descriptor protocol specify that, when,
during an attribute lookup, a name resolves to a class attribute AND
this attribute has a __get__ method, then this __get__ method is called
 (with either the instance or None and the class itself as arguments)
and whatever it returns becomes the result of the attribute lookup. This
mechanism is what provides support for computed attributes.

Now the trick is that the function type do implement the descriptor
protocol. So when a function is an attribute of a class object and you
try to access it as an attribute of either the class itself or an
instance of the class, it's __get__ method is called with the instance
(or None) and the class. Having access to itself (of course), the
instance (if there's one) and the class, it's easy for it to wrap all
this into a method object. Which is itself a callable object, that when
called mostly inject the instance as first object in the argument's list
and returns the result of calling the wrapped function object.

A (naive) implementation of the whole thing might look like this:

class method(object):
def __init__(self, func, instance, cls):
self.im_func = func
self.im_self = instance
self.im_class = cls

def __call__(self, *args, **kw):
# XXX : all sanity checks removed for readability
if self.im_self:
args = (self.im_func,) + args
return self.im_func(*args, **kw)


class function(object):
   # ...
   def __get__(self, instance, cls):
   return method(self, instance, cls)


So, what makes a function a "method" is not being defined in a class
statement's body (well, not directly at least), it's that it is an
attribute of the class. FWIW, the following code is perfectly legal:

class Foo(object):
pass

def func(obj):
   print "obj is %s " % obj

Foo.method = func

f = Foo()
f.method()
Foo.method(f)
func(f)

> I wonder if I'm not trying to make Python things it shouldn't be doing,
> but it's the problem at hand that is leading me into this conundrum: all
> other functions for tagdata use single arguments. I should probably code
> around that anyway..

Well, the simple solution is to just leave print_internal_date as a
plain function instead of insisting on making it a method. Python is
100% OO in that everything is an object, but it's not a "pure" OO
language, ie it doesn't require everything to happen in a method. So if
all you need is a function, by all mean just use a function !-)

Now if you really need print_internal_date to be exposed as a method of
PYFileInfo - like, you need polymorphic dispatch - then make it a
staticmethod.

My 2 cents...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Traversing through variable-sized lists

2010-02-17 Thread Andrej Mitrovic
On Feb 17, 8:24 pm, John Posner  wrote:
> On 2/17/2010 1:10 PM, Andrej Mitrovic wrote:
>
>
>
> > Hi,
>
> > I couldn't figure out a better description for the Subject line, but
> > anyway, I have the following:
>
> > _num_frames = 32
> > _frames = range(0, _num_frames) # This is a list of actual objects,
> > I'm just pseudocoding here.
> > _values = [0, 1, 2, 3, 4]
>
> > I want to call a function of _frames for each frame with a _values
> > argument, but in a way to "spread out" the actual values.
>
> > I would want something similar to the following to be called:
>
> > _frames[0].func(_values[0])
> > _frames[1].func(_values[0])
> > _frames[2].func(_values[0])
> > _frames[3].func(_values[0])
> > _frames[4].func(_values[1])
> > _frames[5].func(_values[1])
> > _frames[6].func(_values[1])
> > _frames[7].func(_values[1])
> > _frames[8].func(_values[2])
> > ...etc...
>
> The lines above show that you are using two different series of index
> values. Each function call (more properly, "method call") has the form:
>
>    frames[INDEX_FROM_FIRST_SERIES].func(INDEX_FROM_SECOND_SERIES)
>
> (I've dropped the underscores in the names, for simplicity.) You're
> getting hung up trying to keep the two series of index values in sync.
> But you don't really need to. More below ...
>
>
>
>
>
> > Both the _values list and _frames list can be of variable and uneven
> > size, which is what is giving me the problems. I'm using Python 2.6.
>
> > I've tried the following workaround, but it often gives me inaccurate
> > results (due to integer division), so I had to add a safety check:
>
> > num_frames = 32
> > values = [0, 1, 2, 3, 4]
> > offset_step = num_frames / len(values)
> >      for index in xrange(0, num_frames):
> >          offset = index / offset_step
> >          if offset>  offset_values[-1]:
> >              offset = offset_values[-1]
> >          frames[index].func(values[offset])
>
> > There has to be a better way to do this. I'd appreciate any help.
> > Cheers!
>
> As you've shown above, a "for" loop takes care of the first series of
> index values:
>
>    for index in xrange(num_frames):       # "0" arg unnecessary
>        frames[index].func(INDEX_FROM_SECOND_SERIES)
>
> The second series of index values needs to look like this:
>
>    0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3 ...
>
> The trick is not to worry about matching the second series to the first
> series. Instead, create an "infinite" second series using a Python
> generator, and use as many of its values as you need. Don't worry about
> the unused values, because the series isn't *really* infinite. :-)
>
> Here's an easy way to create the generator
>
>    import itertools
>    second_series_gen = (i/4 for i in itertools.count())
>
> Now, every time you need another number from this series, use its next()
> method. So the above code becomes:
>
>    for index in xrange(num_frames):
>        frames[index].func(second_series_gen.next())
>
> -John

That's a cool trick, but maybe I wasn't specific enough. The values
series are the range of values that the frames.func() function should
use, it should not overflow, and I would want the function to use as
evenly distributed number of values as possible from the first series.
The "," was just an example. Let me see if I can be more
specific:

values = [2, 3, 4]
frames = [obj1, obj2, obj3, obj4, obj5, obj6]

And the calls:

frames[0].func(values[0])  # func.(values[2])
frames[1].func(values[0])  # func.(values[2])
frames[2].func(values[1])  # func.(values[3])
frames[3].func(values[1])  # func.(values[3])
frames[4].func(values[2])  # func.(values[4])
frames[5].func(values[2])  # func.(values[4])


However the values list might have an uneven number of items. I would
like to make it as evenly distributed as possible, e.g.:

values = [-2, -1, 0]
frames = [obj1, obj2, obj3, obj4, obj5, obj6, obj7, obj8]

frames[0].func(values[0])  # func.(values[-2])
frames[1].func(values[0])  # func.(values[-2])
frames[2].func(values[1])  # func.(values[-2])
frames[3].func(values[1])  # func.(values[-1])
frames[4].func(values[1])  # func.(values[-1])
frames[5].func(values[2])  # func.(values[-1])
frames[6].func(values[2])  # func.(values[0])
frames[7].func(values[2])  # func.(values[0])


I'll be even more specific. I have a Minimum and Maximum value that
the user enters. The frame.func() function is a "translate" function,
it basically moves a frame in the application in one direction or
another depending on the argument value. So frame[0].func(2) would
move the frame[0] 2 pixels to the right. So what I want is the
function to create a smooth transition of all the frames from the
Minimum to the Maximum value. If minimum was 0, and maximum was 10,
I'd want the first frame moved 0 pixels (it stays in place), the last
frame to move 10 pixels, and the frames between are gradually moved
from 1 pixels to 9 pixels relative from their positions.

Perhaps I'm just overcomplicating. I'll have a look at some drawing
apps and see how they

Re: Wrap and intercept function calls

2010-02-17 Thread Terry Reedy

On 2/17/2010 11:04 AM, Dan Yamins wrote:

Really, nobody has any idea about this?   (Sorry to repost.)



On Tue, Feb 16, 2010 at 7:29 PM, Dan Yamins mailto:dyam...@gmail.com>> wrote:

Hi:

I'm wondering what the best way to wrap and modify function calls
is.Essentially what I want to achieve is to have a function like
this:

def Wrap(frame,event,arg):
  if event == 'call':
 result = PerformCheck(GetArgumentsFromFrame(frame))
 if Condition(result):
 return result
 else:
 return [normal function call]

called whenever a "call" event is about to occur.


For yourself as interpreter, execute the above for every call.

For CPython, alternative 1 is to create a custom interpreter to change 
(wrap) the interpretation of the call-function bytecode in the ceval 
loop. That is its 'call event', and I believe this would catch every 
explicit f(args) call and only such calls.


Python has no general metasyntax for changing the semantics of its 
syntax. The __xx__ methods are special cases for the corresponding 
operators *and* the corresponding user-defined class. The '__call__' 
method of a class applies to instances of that class. Alternative 2:


class func_wrap:
  def __init__(self, func):
self._func = func
  def __call__(self, args, kwds):
result = PerformCheck(args, kwds)
if Condition(result):
  return result
else:
  return self._func(*args,**kwds)

Now wrap *every* function you are interested in. Builtin functions are 
no problem; methods of builtin classes cannont be wrapped without 
subclassing.


Terry Jan Reedy


When I say "return result" I mean:  return that data object instead
of what the function would have returned, and prevent execution of
the function.

Is there any way to do this using sys.settrace?  Or perhaps
something from the bdb or pbd module?


In other words, what I'm looking for is a way to intercept all function
calls with a "wrapper" --  like one can do using settrace and other
debugging methods -- but, unlike the debugging methods, have the
"wrapping" function actually be able to intervene in the stack and, for
instance, conditionally replace the function call's return with
something determined in the wrapping function and prevent the function
call's execution.   I want to be able to do this by setting a single
system-wide (or at any rate, thread-wide) value, like with settrace, and
not have to modify individual functions one by one.

Could I, for example, set a settrace function that somehow modifies the
stack?  Or is there some much better way to do this?  Or, if someone can
tell me that this can't be done without having to roll my own
implementation of the Python interpreter, that would be great to know too.

Thanks again,
Dan




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


Re: Referring to class methods in class attributes

2010-02-17 Thread John Posner

On 2/17/2010 2:44 PM, Bruno Desthuilliers wrote:


Mmmm... Let's try to explain the whole damn thing. It's really (and IMHO
beautifully) simple once you get it, but I agree it's a bit peculiar
when compared to most mainstream OO languages.


Very nice writeup, Bruno -- thanks!





class method(object):
 def __init__(self, func, instance, cls):
 self.im_func = func
 self.im_self = instance
 self.im_class = cls

 def __call__(self, *args, **kw):
 # XXX : all sanity checks removed for readability
 if self.im_self:
 args = (self.im_func,) + args


In the line above, I think you meant:

   args = (self.im_self,) + args

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


Re: Over(joy)riding

2010-02-17 Thread Bruno Desthuilliers
mk a écrit :
> Bruno Desthuilliers wrote:
>> mk a écrit :
>>> P.S. Method resolution order in Python makes me want to kill small
>>> kittens.
>>
>> mro is only a "problem" when using MI.
> 
> Oh sure! And I have the impression that multiple inheritance is not used
> all that often. What (some) Python code I've read in open source
> projects typically uses single inheritance.

Single inheritance does indeed cover most of the needs - and FWIW, even
single inheritance is not used as much in Python as in some other more
mainstream OOPLs. This comes partly from duck typing - inheritance is
only used for implementation inheritance, not for type-based polymorphic
dispatch -, and partly from Python's support for composition/delegation
(thru __getattr__). Also - as you noticed - single inheritance is easier
to grasp, and Python's philosophy really values simplicity, readability,
maintainability, and coder's mental health !-)

> Nevertheless MI is there in Python, might be useful in some cases and
> Guido put it there for some reason,

Indeed. As with most Python's "advanced" features, it's there so you can
use it when appropriate.

> so Citizens Aspiring To Become
> Pythonistas like me work to learn it.

FWIW, I very rarely had to use it myself in almost 10 years of Python
programming - main exceptions being Zope2 stuff (which is IMHO highly
unpythonic) and a couple mixin classes here and there.
-- 
http://mail.python.org/mailman/listinfo/python-list


MediaWiki to RTF/Word/PDF

2010-02-17 Thread Josh English
I have several pages exported from a private MediaWiki that I need to
convert to a PDF document, or an RTF document, or even a Word
document.

So far, the only Python module I have found that parses MediaWiki
files is mwlib, which only runs on Unix, as far as I can tell. I'm
working on Windows here.

Has anyone heard of a module that parses wiki markup and transforms
it? Or am I looking at XSLT?

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


Re: MediaWiki to RTF/Word/PDF

2010-02-17 Thread Shashwat Anand
why not try installing cygwin. I am just guessing though but I had heard it
emulates *nix decently on windows. Or a better idea is to shift to *nix ;)

On Thu, Feb 18, 2010 at 2:30 AM, Josh English wrote:

> I have several pages exported from a private MediaWiki that I need to
> convert to a PDF document, or an RTF document, or even a Word
> document.
>
> So far, the only Python module I have found that parses MediaWiki
> files is mwlib, which only runs on Unix, as far as I can tell. I'm
> working on Windows here.
>
> Has anyone heard of a module that parses wiki markup and transforms
> it? Or am I looking at XSLT?
>
> Josh
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Referring to class methods in class attributes

2010-02-17 Thread Arnaud Delobelle
Bruno Desthuilliers  writes:

[...]
> class Foo(object):
> def bar(self):
> return "baaz"
>
> print Foo.__dict__.keys()
> print type(Foo.__dict__['bar'])
>
>
> So, why is it that type(Foo.bar) != type(Foo.__dict__['bar']) ? The
> answer is : attribute lookup rules and the descriptor protocol.

It's true of Python 2.X.  In Python 3 there are no more unbound method:

Python 3.2a0 (py3k:75274, Oct  7 2009, 20:25:52) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
...def f(self): pass
... 
>>> A.f

>>> A.f is A.__dict__['f']
True

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


Re: mixins and new style classes

2010-02-17 Thread Bruno Desthuilliers
mk a écrit :
 class Person(object):
> ... pass
> ...
 class Friendly(object):
> ... def hello(self):
> ... print 'hello'
> ...

 Person.__bases__ += (Friendly,)
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: Cannot create a consistent method resolution
> order (MRO) for bases object, Friendly

Indeed. After the addition of Friendly to Person.__bases__, the mro for
Person would be (Person, object, Friendly). But the mro for Friendly is
(Friendly, object). This is not consistent. The right solution would be
to inject Friendly before object, ie:

Person.__bases__ = (Mixin, object)

Now the bad news is that this fails too:

TypeError: __bases__ assignment: 'Mixin' deallocator differs from 'object'

The above problem is not new, and had been discussed here:

http://bugs.python.org/issue672115

...and it's still unresolved AFAICT :-/

OTHO, the concrete use case for such a feature seem to be rather uncommon.

> 
> 
> But it works for old-style classes:

Old-style classes are a very different beast.

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


Re: Referring to class methods in class attributes

2010-02-17 Thread Bruno Desthuilliers
John Posner a écrit :
> On 2/17/2010 2:44 PM, Bruno Desthuilliers wrote:
>> 
> Very nice writeup, Bruno -- thanks!



> 
> 
>>
>>
>>  def __call__(self, *args, **kw):
>>  # XXX : all sanity checks removed for readability
>>  if self.im_self:
>>  args = (self.im_func,) + args
> 
> In the line above, I think you meant:
> 
>args = (self.im_self,) + args

oops :?

Yes, indeed. Thanks for the correction.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Referring to class methods in class attributes

2010-02-17 Thread Ben Finney
Bruno Desthuilliers  writes:

> Mmmm... Let's try to explain the whole damn thing. It's really (and
> IMHO beautifully) simple once you get it, but I agree it's a bit
> peculiar when compared to most mainstream OO languages.
[…]

Bruno, that's the first time I've understood the descriptor protocol, or
even understood *why* the descriptor protocol is important.

Could you please post (a version of) this as a separate article
somewhere? A weblog post, or a new thread in this forum, or in the
documentation? Somewhere that web searches will find it prominently when
searching for the Python descriptor protocol.

Thank you.

-- 
 \  “I don't want to live peacefully with difficult realities, and |
  `\ I see no virtue in savoring excuses for avoiding a search for |
_o__)real answers.” —Paul Z. Myers, 2009-09-12 |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: MediaWiki to RTF/Word/PDF

2010-02-17 Thread John Bokma
Josh English  writes:

> I have several pages exported from a private MediaWiki that I need to
> convert to a PDF document, or an RTF document, or even a Word
> document.
>
> So far, the only Python module I have found that parses MediaWiki
> files is mwlib, which only runs on Unix, as far as I can tell. I'm
> working on Windows here.
>
> Has anyone heard of a module that parses wiki markup and transforms
> it? Or am I looking at XSLT?

One option might be to install a printer driver that prints to PDF and
just print the web pages.

Using Saxon or AltovaXML and a suitable stylesheet might give you the
nicest looking result though (but quite some work).

-- 
John Bokma   j3b

Hacking & Hiking in Mexico -  http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Referring to class methods in class attributes

2010-02-17 Thread Mark Lawrence

Ben Finney wrote:

Bruno Desthuilliers  writes:


Mmmm... Let's try to explain the whole damn thing. It's really (and
IMHO beautifully) simple once you get it, but I agree it's a bit
peculiar when compared to most mainstream OO languages.

[…]

Bruno, that's the first time I've understood the descriptor protocol, or
even understood *why* the descriptor protocol is important.

Could you please post (a version of) this as a separate article
somewhere? A weblog post, or a new thread in this forum, or in the
documentation? Somewhere that web searches will find it prominently when
searching for the Python descriptor protocol.

Thank you.



I'll second, third and fourth this request.  More please Bruno, or from 
anybody similarly qualified.


Thanks very much.

Mark Lawrence.

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


Re: The future of "frozen" types as the number of CPU cores increases

2010-02-17 Thread sjdevn...@yahoo.com
On Feb 17, 2:35 am, Steven D'Aprano
 wrote:
> On Tue, 16 Feb 2010 21:09:27 -0800, John Nagle wrote:
> >     Yes, we're now at the point where all the built-in mutable types
> > have "frozen" versions.  But we don't have that for objects.  It's
> > generally considered a good thing in language design to offer, for user
> > defined types, most of the functionality of built-in ones.
>
> It's not hard to build immutable user-defined types. Whether they're
> immutable enough is another story :)
>
> >>> class FrozenMeta(type):
>
> ...     def __new__(meta, classname, bases, classDict):
> ...         def __setattr__(*args):
> ...             raise TypeError("can't change immutable class")
> ...         classDict['__setattr__'] = __setattr__
> ...         classDict['__delattr__'] = __setattr__
> ...         return type.__new__(meta, classname, bases, classDict)
> ...
>
> >>> class Thingy(object):
>
> ...     __metaclass__ = FrozenMeta
> ...     def __init__(self, x):
> ...         self.__dict__['x'] = x
> ...
>
> >>> t = Thingy(45)
> >>> t.x
> 45
> >>> t.x = 42
>
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "", line 4, in __setattr__
> TypeError: can't change immutable class
>
> It's a bit ad hoc, but it seems to work for me. Unfortunately there's no
> way to change __dict__ to a "write once, read many" dict.

Which makes it not really immutable, as does the relative ease of
using a normal setattr:

... t.__dict__['x'] = "foo"
... print t.x
foo
... object.__setattr__(t, "x", 42)
... print t.x
42
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

2010-02-17 Thread Lawrence D'Oliveiro
In message , cjw wrote:

> Aren't lambda forms better described as function?

Is this a function?

lambda : None

What about this?

lambda : sys.stdout.write("hi there!\n")

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


Re: Traversing through variable-sized lists

2010-02-17 Thread Dave Angel



Andrej Mitrovic wrote:

On Feb 17, 8:24 pm, John Posner  wrote:
  

On 2/17/2010 1:10 PM, Andrej Mitrovic wrote:




However the values list might have an uneven number of items. I would
like to make it as evenly distributed as possible, e.g.:

values =-2, -1, 0]
frames =obj1, obj2, obj3, obj4, obj5, obj6, obj7, obj8]

frames[0].func(values[0])  # func.(values[-2])
frames[1].func(values[0])  # func.(values[-2])
frames[2].func(values[1])  # func.(values[-2])
frames[3].func(values[1])  # func.(values[-1])
frames[4].func(values[1])  # func.(values[-1])
frames[5].func(values[2])  # func.(values[-1])
frames[6].func(values[2])  # func.(values[0])
frames[7].func(values[2])  # func.(values[0])


I'll be even more specific. I have a Minimum and Maximum value that
the user enters. The frame.func() function is a "translate" function,
it basically moves a frame in the application in one direction or
another depending on the argument value. So frame[0].func(2) would
move the frame[0] 2 pixels to the right. So what I want is the
function to create a smooth transition of all the frames from the
Minimum to the Maximum value. If minimum was 0, and maximum was 10,
I'd want the first frame moved 0 pixels (it stays in place), the last
frame to move 10 pixels, and the frames between are gradually moved
from 1 pixels to 9 pixels relative from their positions.

Perhaps I'm just overcomplicating. I'll have a look at some drawing
apps and see how they've implemented drawing straight lines under an
angle, I guess that could be called a gradual change of values.

Thanks for all the suggestions everyone, I'll have a look at the rest
shortly.

  
I think you're overcomplicating.  If you have 27 frames, and you want 
frame 0 to move 0 pixels, and frame 27 to move 10 pixels, then you want 
to move frame[i] by i*10/27.  And since you do the multiply first, the 
fact that Python 2.x division gives you integers isn't a problem.


There are fancier methods for drawing lines (bresenham for example), but 
the main purpose of them is to to avoid multiply and divide, as well as 
floats.  But in Python, an integer multiply is just as fast as an add or 
subtract, so there's no point.


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


Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

2010-02-17 Thread Terry Reedy

On 2/17/2010 5:46 PM, Lawrence D'Oliveiro wrote:

In message, cjw wrote:


Aren't lambda forms better described as function?


Is this a function?

 lambda : None

What about this?

 lambda : sys.stdout.write("hi there!\n")


To repeat: Python lambda expressions evaluate to function objects 
identical, except for .__name__ attribute, to the equivalent def statememnt.


>>> type(lambda:None)

>>> import sys
>>> type(lambda : sys.stdout.write("hi there!\n"))




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


Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

2010-02-17 Thread Ben Finney
Lawrence D'Oliveiro  writes:

> In message , cjw wrote:
>
> > Aren't lambda forms better described as function?
>
> Is this a function?
>
> lambda : None
>
> What about this?
>
> lambda : sys.stdout.write("hi there!\n")

They are both lambda forms in Python. As a Python expression, they
evaluate to (they “return”) a function object.

-- 
 \   “It is wrong to think that the task of physics is to find out |
  `\ how nature *is*. Physics concerns what we can *say* about |
_o__) nature…” —Niels Bohr |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to efficiently extract information from structured text file

2010-02-17 Thread Imaginationworks
On Feb 17, 1:40 pm, Paul McGuire  wrote:
> On Feb 16, 5:48 pm, Imaginationworks  wrote:
>
> > Hi,
>
> > I am trying to read object information from a text file (approx.
> > 30,000 lines) with the following format, each line corresponds to a
> > line in the text file.  Currently, the whole file was read into a
> > string list using readlines(), then use for loop to search the "= {"
> > and "};" to determine the Object, SubObject,and SubSubObject.
>
> If you open(filename).read() this file into a variable named data, the
> following pyparsing parser will pick out your nested brace
> expressions:
>
> from pyparsing import *
>
> EQ,LBRACE,RBRACE,SEMI = map(Suppress,"={};")
> ident = Word(alphas, alphanums)
> contents = Forward()
> defn = Group(ident + EQ + Group(LBRACE + contents + RBRACE + SEMI))
>
> contents << ZeroOrMore(defn | ~(LBRACE|RBRACE) + Word(printables))
>
> results = defn.parseString(data)
>
> print results
>
> Prints:
>
> [
>  ['Object1',
>    ['...',
>     ['SubObject1',
>       ['',
>         ['SubSubObject1',
>           ['...']
>         ]
>       ]
>     ],
>     ['SubObject2',
>       ['',
>        ['SubSubObject21',
>          ['...']
>        ]
>       ]
>     ],
>     ['SubObjectN',
>       ['',
>        ['SubSubObjectN',
>          ['...']
>        ]
>       ]
>     ]
>    ]
>  ]
> ]
>
> -- Paul

Wow, that is great! Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


string to list when the contents is a list

2010-02-17 Thread Wes James
I have been trying to create a list form a string.  The string will be
a list (this is the contents will look like a list).  i.e. "[]" or
"['a','b']"

The "[]" is simple since I can just check if value == "[]" then return []

But with "['a','b']" I have tried and get:

a="['a','b']"

b=a[1:-1].split(',')

returns

[ " 'a' "," 'b' " ]

when I want it to return ['a','b'].

How can I do this?

thx,

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


error trying to join #python on irc.freenode.net

2010-02-17 Thread Wes James
When I try to join #python on irc.freenode.net it keeps saying:

You need to identify with network services to join the room "#python"
on "irc.freenode.net".

Server Details:
Cannot join channel (+r) - you need to be identified with services

What does this mean?

thx,

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


Re: error trying to join #python on irc.freenode.net

2010-02-17 Thread Wes James
On Wed, Feb 17, 2010 at 4:53 PM, Wes James  wrote:
> When I try to join #python on irc.freenode.net it keeps saying:
>
> You need to identify with network services to join the room "#python"
> on "irc.freenode.net".
>
> Server Details:
> Cannot join channel (+r) - you need to be identified with services
>
> What does this mean?


Nevermind, I think it means I need to register with the service and
supply real username/password.

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


Re: string to list when the contents is a list

2010-02-17 Thread Vlastimil Brom
2010/2/18 Wes James :
> I have been trying to create a list form a string.  The string will be
> a list (this is the contents will look like a list).  i.e. "[]" or
> "['a','b']"
>
> The "[]" is simple since I can just check if value == "[]" then return []
>
> But with "['a','b']" I have tried and get:
>
> a="['a','b']"
>
> b=a[1:-1].split(',')
>
> returns
>
> [ " 'a' "," 'b' " ]
>
> when I want it to return ['a','b'].
>
> How can I do this?
>
> thx,
>
> -wes
> --
> http://mail.python.org/mailman/listinfo/python-list
>

The potentially problematic exec or eval options left aside,
if you really need to do this, you might consider pyparsing; check the example
http://pyparsing.wikispaces.com/file/view/parsePythonValue.py

If you know, the input string will always have this exact format
(single quoted comma separated one-character strings between square
brackets), you might use regular expressions to some extent, e.g.

print re.findall(r"(?<=')\w(?=')", "['a','b','c','b','A']")
['a', 'b', 'c', 'b', 'A']

hth,
  vbr
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Wrap and intercept function calls

2010-02-17 Thread Dan Yamins
> For CPython, alternative 1 is to create a custom interpreter to change
> (wrap) the interpretation of the call-function bytecode in the ceval loop.
> That is its 'call event', and I believe this would catch every explicit
> f(args) call and only such calls.
>
>


> Python has no general metasyntax for changing the semantics of its syntax.
> The __xx__ methods are special cases for the corresponding operators *and*
> the corresponding user-defined class. The '__call__' method of a class
> applies to instances of that class. Alternative 2:
>
>
Terry thanks for your response.

My hoped-for spec explicitly prohibits wrapping required each (user-defined)
function to be wrapped individual, since I want to be able to have this
wrapping behavior apply to many functions created by many users on the fly,
and such users can't be asked to remember to wrap each function.  (At most,
they can be asked to set a single evironment variable or the like at the
beginning of modules or interpreter sessions that should the "turn on
wrapping" for all future function calls.)

So I suppose the situation is as I feared? That the only way to achieve my
goal without having to wrap every (user-defined) function call individually
is to modify the interpreter.   Is there no way to edit frame objects from
inside a function?  if I could do that in a settrace function, would it even
help?

Also: am I right in thinking that the "modified interpreter" approach would
mean that my resulting software couldn't be used as a 3-rd party module,
that could be easily integrated into existing python installations?  Or is
there some standard way of integrating dynamically loadable modifications to
the interpreter in python?  (e.g. so that users of my code wouldn't have to
re-install of their existing modules in a separate new python installation?)

Thanks!
Dan




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


Re: string to list when the contents is a list

2010-02-17 Thread Rhodri James

On Wed, 17 Feb 2010 23:48:38 -, Wes James  wrote:


I have been trying to create a list form a string.  The string will be
a list (this is the contents will look like a list).  i.e. "[]" or
"['a','b']"


If your string is trusted (i.e. goes nowhere near a user), just eval() it.

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


Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

2010-02-17 Thread Steven D'Aprano
On Thu, 18 Feb 2010 11:46:52 +1300, Lawrence D'Oliveiro wrote:

> In message , cjw wrote:
> 
>> Aren't lambda forms better described as function?
> 
> Is this a function?
> 
> lambda : None
> 
> What about this?
> 
> lambda : sys.stdout.write("hi there!\n")

Of course they are; the first is a function that takes no arguments and 
returns None, and the second is a function that takes no arguments, 
returns None, and has a side-effect of writing "hi there\n" to stout. 

But I imagine you already know that, so I'm not really sure I understand 
the point of your (rhetorical?) question.


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


Re: Which mock library do you prefer?

2010-02-17 Thread Mark Lawrence

Phlip wrote:

On Feb 17, 6:26 am, Lacrima  wrote:


Right, isolation is essential.


Please read my reply: Ben is well intentioned but completely wrong
here.

Mock abuse will not cure the runtime isolation problem.



I believe that Ben is perfectly correct, and that you are talking at 
cross purposes because you've missed the significance of his


(you later realise)

within his post.
Runtime test isolation doesn't enter into from what I can see.
Can you please clarify the situation one way or the other.

TIA.

Mark Lawrence.

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


Re: string to list when the contents is a list

2010-02-17 Thread Steven D'Aprano
On Thu, 18 Feb 2010 00:13:05 +, Rhodri James wrote:

> On Wed, 17 Feb 2010 23:48:38 -, Wes James 
> wrote:
> 
>> I have been trying to create a list form a string.  The string will be
>> a list (this is the contents will look like a list).  i.e. "[]" or
>> "['a','b']"
> 
> If your string is trusted (i.e. goes nowhere near a user), just eval()
> it.

Or use something like YAML or JSON to parse it.

Fredrik Lundh has a simple_eval function which should be safe to use:

http://effbot.org/zone/simple-iterator-parser.htm


But it's fairly simple to parse a simple list like this. Here's a quick 
and dirty version:


def string_to_list(s):
s = s.strip()
if not s.startswith('[') and s.endswith(']'):
raise ValueError
s = s[1:-1].strip()
items = [item.strip() for item in s.split(',')]
for i, item in enumerate(items):
items[i] = dequote(item)
return items


def dequote(s):
for delimiter in ('"""', "'''", '"', "'"):
if s.startswith(delimiter) and s.endswith(delimiter):
n = len(delimiter)
return s[n:-n]
raise ValueError



>>> s = "['a','b']"
>>> print s
['a','b']
>>> string_to_list(s)
['a', 'b']
>>> x = string_to_list(s)
>>> type(x)

>>> x
['a', 'b']



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


Re: string to list when the contents is a list

2010-02-17 Thread Matt McCredie
Wes James  gmail.com> writes:

> 
> I have been trying to create a list form a string.  The string will be
> a list (this is the contents will look like a list).  i.e. "[]" or
> "['a','b']"
> 
> The "[]" is simple since I can just check if value == "[]" then return []
> 
> But with "['a','b']" I have tried and get:
> 
> a="['a','b']"
> 
> b=a[1:-1].split(',')
> 
> returns
> 
> [ " 'a' "," 'b' " ]
> 
> when I want it to return ['a','b'].
> 
> How can I do this?


eval will work, but has a safety issue. It also has the issue of evaluating any 
and everything that a user might pass in. 

If you are using python 2.6 check out ast.literal_eval. It uses python's built 
in ast parser to generate an AST and then traverses it to generate a python 
object. Unlike eval though, it will raise an exception if anything other than a 
literal is represented in the string. I have used the same function in python 
2.5 (copied from 2.6) and it works just fine.

Here is a version modified from the code in python 2.6 that should only parse 
lists of strings:

from _ast import List, Str, PyCF_ONLY_AST

def parse(expr, filename='', mode='exec'):
"""
Parse an expression into an AST node.
Equivalent to compile(expr, filename, mode, PyCF_ONLY_AST).
"""
return compile(expr, filename, mode, PyCF_ONLY_AST)


def list_eval(text):
"""
Safely evaluate an expression node or a string containing a Python
expression.  The string or node provided may only consist of the following
Python literal structures: strings, numbers, tuples, lists, dicts, booleans,
and None.
"""

node = parse(text, mode='eval').body
if not isinstance(node, List):
raise ValueError('malformed string')
def _convert(node):
if isinstance(node, Str):
return node.s
raise ValueError('malformed string')

return list(map(_convert, node.elts))




Matt McCredie

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


Re: string to list when the contents is a list

2010-02-17 Thread Ben Finney
Wes James  writes:

> I have been trying to create a list form a string.  The string will be
> a list (this is the contents will look like a list).  i.e. "[]" or
> "['a','b']"

Pulling back to ask about the larger problem: Are you trying to create
Python data structures from a serialised representation?

There are several well-implemented solutions, including the standard
library modules ‘pickle’ and ‘json’. Do you have control over the choice
of serialisation format?

-- 
 \“I went to court for a parking ticket; I pleaded insanity. I |
  `\   said ‘Your Honour, who in their right mind parks in the passing |
_o__)   lane?’” —Steven Wright |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

2010-02-17 Thread Jonathan Gardner
On Feb 17, 10:39 am, John Bokma  wrote:
> Jonathan Gardner  writes:
> > Then I looked at a stack trace from a different programming language
> > with lots of anonymous functions. (I believe it was perl.)
>
> > I became enlightened.
>
> If it was Perl [1], I doubt it. Because line numbers are reported, and
> if that doesn't help you, you can annotate anonymous functions with a
> nick name using
>
> local *__ANON__ = 'nice name';
>
> Finding an issue, and not looking for a solution is not called becoming
> enlightened ;-)
>
> ~$ perl -e '
> use Carp;
>
> my $anon = sub { local *__ANON__ = "hello, world"; croak "oops"; };
> $anon->();
> '
>
> oops at -e line 4
>         main::hello, world() called at -e line 5
>
> As you can see, and a line number is generated, and the nice name is
> shown.
>
> If you generate anonymouse functions on the fly based on parameters, you
> can encode this into the nice name, of course.
>
> Sadly, often bold statements about a language are made in ignorance.
>

$ perl -e '$a = sub () {die "it may have been javascript, but"}; $b =
sub () {die "I am pretty sure it was perl"}; $b->()'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

2010-02-17 Thread Jonathan Gardner
On Feb 17, 12:02 am, Lawrence D'Oliveiro  wrote:
> In message
> <8ca440b2-6094-4b35-80c5-81d000517...@v20g2000prb.googlegroups.com>,
>
> Jonathan Gardner wrote:
> > I used to think anonymous functions (AKA blocks, etc...) would be a
> > nice feature for Python.
>
> > Then I looked at a stack trace from a different programming language
> > with lots of anonymous functions. (I believe it was perl.)
>
> Didn’t it have source line numbers in it?
>
> What more do you need?

I don't know, but I tend to find the name of the function I called to
be useful. It's much more memorable than line numbers, particularly
when line numbers keep changing.

I doubt it's just me, though.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

2010-02-17 Thread Jonathan Gardner
On Feb 17, 12:02 am, Lawrence D'Oliveiro  wrote:
> In message <60b1abce-4381-46ab-91ed-
>
> f2ab2154c...@g19g2000yqe.googlegroups.com>, Andrej Mitrovic wrote:
> > Also, lambda's are expressions, not statements ...
>
> Is such a distinction Pythonic, or not? For example, does Python distinguish
> between functions and procedures?

Not to the programmer, no. Callables are callable, no matter what they
are, and they are all called the same way.

(What the heck is a procedure, anyway? Is this different from a
subroutine, a method, or a block?)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

2010-02-17 Thread Steven D'Aprano
On Wed, 17 Feb 2010 12:39:30 -0600, John Bokma wrote:

> Jonathan Gardner  writes:
> 
>> Then I looked at a stack trace from a different programming language
>> with lots of anonymous functions. (I believe it was perl.)
>>
>> I became enlightened.
> 
> If it was Perl [1], I doubt it. Because line numbers are reported, and
> if that doesn't help you, you can annotate anonymous functions with a
> nick name using
> 
> local *__ANON__ = 'nice name';
[...]
> As you can see, and a line number is generated, and the nice name is
> shown.

Given that it has a nice name, what makes it an anonymous function?

It seems to me that Perl effectively has three ways of creating 
functions, one anonymous and two named (even if one syntax for creating a 
named function is almost identical to the syntax for creating an 
anonymous function). Once you annotate a function with a nickname, it's 
no different from giving it a name.

If this is the case, then your answer to "anonymous functions are a PITA" 
is "don't use anonymous functions", which exactly the same answer we'd 
give here in Python land. The only difference is that Perl provides two 
ways of making a named function, and Python only one[1].






[1] Technically, you can make named functions with the new module and a 
bit of work, so Python has two ways too.


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


Re: How to efficiently extract information from structured text file

2010-02-17 Thread Jonathan Gardner
On Feb 16, 3:48 pm, Imaginationworks  wrote:
> Hi,
>
> I am trying to read object information from a text file (approx.
> 30,000 lines) with the following format, each line corresponds to a
> line in the text file.  Currently, the whole file was read into a
> string list using readlines(), then use for loop to search the "= {"
> and "};" to determine the Object, SubObject,and SubSubObject. My
> questions are
>
> 1) Is there any efficient method that I can search the whole string
> list to find the location of the tokens(such as '= {' or '};'
>
> 2) Is there any efficient ways to extract the object information you
> may suggest?

Parse it!

Go full-bore with a real parser. You may want to consider one of the
many fine Pythonic implementations of modern parsers, or break out
more traditional parsing tools.

This format is nested, meaning that you can't use regexes to parse
what you want out of it. You're going to need a real, full-bore, no-
holds-barred parser for this.

Don't worry, the road is not easy but the destination is absolutely
worth it.

Once you come to appreciate and understand parsing, you have earned
the right to call yourself a red-belt programmer. To get your black-
belt, you'll need to write your own compiler. Having mastered these
two tasks, there is no problem you cannot tackle.

And once you realize that every program is really a compiler, then you
have truly mastered the Zen of Programming in Any Programming Language
That Will Ever Exist.

With this understanding, you will judge programming language utility
based solely on how hard it is to write a compiler in it, and
complexity based on how hard it is to write a compiler for it. (Notice
there are not a few parsers written in Python, as well as Jython and
PyPy and others written for Python!)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

2010-02-17 Thread Rhodri James
On Thu, 18 Feb 2010 01:04:00 -, Jonathan Gardner  
 wrote:



On Feb 17, 12:02 am, Lawrence D'Oliveiro  wrote:

In message <60b1abce-4381-46ab-91ed-

f2ab2154c...@g19g2000yqe.googlegroups.com>, Andrej Mitrovic wrote:
> Also, lambda's are expressions, not statements ...

Is such a distinction Pythonic, or not? For example, does Python  
distinguish

between functions and procedures?


Not to the programmer, no. Callables are callable, no matter what they
are, and they are all called the same way.

(What the heck is a procedure, anyway? Is this different from a
subroutine, a method, or a block?)


In classic Pascal, a procedure was distinct from a function in that it had  
no return value.  The concept doesn't really apply in Python; there are no  
procedures in that sense, since if a function terminates without supplying  
an explicit return value it returns None.


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


Re: Which mock library do you prefer?

2010-02-17 Thread Ben Finney
Lacrima  writes:

> Right, isolation [of test cases] is essential. But I can't decide to
> which extent I should propagate isolation.

You used “propagate” in a sense I don't understand there.

> For example, in "Python Testing: Beginner's Guide" by Daniel Arbuckle,
> author suggests that if you do unittesting you should isolate the
> smallest units of code from each other.

I'm not sure what the author means, but I would say that as it stands
that advice is independent of what testing is being done. In all cases:

* Make your code units small, so each one is not doing much and is easy
  to understand.

* Make the interface of units at each level as narrow as feasible, so
  they're not brittle in the face of changes to the implementation.

> For example, if you have a
> class:
> Class SomeClass(object):
> def method1(self):
> return 5
> def method2(self):
> return self.method1 + 10
>
> According to the book, if you want to test method2, you should isolate
> it from method1 and class instance('self').

I don't really know what that means.

Remember that each test case should not be “test method1”. That is far
too broad, and in some cases too narrow. There is no one-to-one mapping
between methods and unit test cases.

Instead, each test case should test one true-or-false assertion about
the behaviour of the code. “When we start with this initial state (the
test fixture), and perform this operation, the resulting state is that”.

It makes a lot of sense to name the test case so the assertion being
made *is* its name: not ‘test frobnicate’ with dozens of assertions, but
one ‘test_frobnicate_with_valid_spangulator_returns_true’ which makes
that assertion, and extra ones for each distinct assertion.

The failure of a unit test case should indicate *exactly* what has gone
wrong. If you want to make multiple assertions about a code unit, write
multiple test cases for that unit and name the tests accordingly.

This incidentally requires that you test something small enough that
such a true-or-false assertion is meaningful, which leads to
well-designed code with small easily-tested code units. But that's an
emergent property, not a natural law.

> Currently, I don't create mocks of units if they are within the same
> class with the unit under test. If that is not right approach, please,
> explain what are best practices... I am just learning TDD..

In the fixture of the unit test case, create whatever test doubles are
necessary to put your code into the initial state you need for the test
case; then tear all those down whatever the result of the test case.

If you need to create great honking wads of fixtures for any test case,
that is a code smell: your code units are too tightly coupled to
persistent state, and need to be decoupled with narrow interfaces.

The Python ‘unittest’ module makes this easier by letting you define
fixtures common to many test cases (the ‘setUp’ and ‘tearDown’
interface). My rule of thumb is: if I need to make different fixtures
for some set of test cases, I write a new test case class for those
cases.

-- 
 \   “Following fashion and the status quo is easy. Thinking about |
  `\your users' lives and creating something practical is much |
_o__)harder.” —Ryan Singer, 2008-07-09 |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to efficiently extract information from structured text file

2010-02-17 Thread Steven D'Aprano
On Wed, 17 Feb 2010 17:13:23 -0800, Jonathan Gardner wrote:

> And once you realize that every program is really a compiler, then you
> have truly mastered the Zen of Programming in Any Programming Language
> That Will Ever Exist.

In the same way that every tool is really a screwdriver.


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


Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

2010-02-17 Thread Steven D'Aprano
On Wed, 17 Feb 2010 17:04:00 -0800, Jonathan Gardner wrote:

> (What the heck is a procedure, anyway? Is this different from a
> subroutine, a method, or a block?)

The name is used in Pascal, which probably means it originated from 
Fortran or Algol. 

A subroutine is a generic piece of code which can be re-used by some 
unspecified mechanism (GOSUB in Basic, by calling it in most other 
languages). A function is a subroutine that returns a result, and a 
procedure is a subroutine that doesn't return anything (not even None, or 
the equivalent thereof) and operates entirely by side-effect.



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


  1   2   >