Wednesday, February 17, 2010

comp.lang.python - 25 new messages in 12 topics - digest

comp.lang.python
http://groups.google.com/group/comp.lang.python?hl=en

comp.lang.python@googlegroups.com

Today's topics:

* Interesting talk on Python vs. Ruby and how he would like Python to have
just a bit more syntactic flexibility. - 3 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/9a88c79d4043ba30?hl=en
* listing existing windows services with python - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/a8c444a258aabc30?hl=en
* plugin / intra process communication system - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/0a210267753919b0?hl=en
* Replacement for e.message() in python 2.6 - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/8d4026967f66345f?hl=en
* GIL state during import - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/b9ba52627893a79d?hl=en
* Hubris connects Ruby to Haskell, will there be such a connection between
Python and Haskell? - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/5b5a0c768c788353?hl=en
* Wrestling with the Py2exe Install, Win7, Py2.5 - 2 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/11d75d0eb3dbf903?hl=en
* Which mock library do you prefer? - 2 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/a12daa9de8354ff2?hl=en
* Over(joy)riding - 6 messages, 4 authors
http://groups.google.com/group/comp.lang.python/t/d71fbc10c9d1f900?hl=en
* Shipping Executables - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/297a2754b1ad0538?hl=en
* Is automatic reload of a module available in Python? - 3 messages, 3 authors
http://groups.google.com/group/comp.lang.python/t/71d2e8e4a38c3b1d?hl=en
* How to efficiently extract information from structured text file - 1
messages, 1 author
http://groups.google.com/group/comp.lang.python/t/bc8840f8a254056b?hl=en

==============================================================================
TOPIC: Interesting talk on Python vs. Ruby and how he would like Python to
have just a bit more syntactic flexibility.
http://groups.google.com/group/comp.lang.python/t/9a88c79d4043ba30?hl=en
==============================================================================

== 1 of 3 ==
Date: Wed, Feb 17 2010 12:02 am
From: Lawrence D'Oliveiro


In message
<8ca440b2-6094-4b35-80c5-81d000517ce0@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?


== 2 of 3 ==
Date: Wed, Feb 17 2010 2:46 am
From: Bruno Desthuilliers


Aahz a écrit :
> In article <8ca440b2-6094-4b35-80c5-81d000517ce0@v20g2000prb.googlegroups.com>,
> Jonathan Gardner <jgardner@jonathangardner.net> 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...


== 3 of 3 ==
Date: Wed, Feb 17 2010 2:48 am
From: Bruno Desthuilliers


Lawrence D'Oliveiro a écrit :
> In message <60b1abce-4381-46ab-91ed-
> f2ab2154c0a1@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 !-)

==============================================================================
TOPIC: listing existing windows services with python
http://groups.google.com/group/comp.lang.python/t/a8c444a258aabc30?hl=en
==============================================================================

== 1 of 2 ==
Date: Wed, Feb 17 2010 1:21 am
From: 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 <wuwei23@gmail.com> writes:
>
>> News123 <news...@free.fr> 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


== 2 of 2 ==
Date: Wed, Feb 17 2010 6:29 am
From: 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

==============================================================================
TOPIC: plugin / intra process communication system
http://groups.google.com/group/comp.lang.python/t/0a210267753919b0?hl=en
==============================================================================

== 1 of 1 ==
Date: Wed, Feb 17 2010 1:29 am
From: 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 <dino@phidev.org>

==============================================================================
TOPIC: Replacement for e.message() in python 2.6
http://groups.google.com/group/comp.lang.python/t/8d4026967f66345f?hl=en
==============================================================================

== 1 of 2 ==
Date: Wed, Feb 17 2010 1:58 am
From: 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


== 2 of 2 ==
Date: Wed, Feb 17 2010 2:18 am
From: Arnaud Delobelle


Nandakumar Chandrasekhar <navanitachora@gmail.com> 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

==============================================================================
TOPIC: GIL state during import
http://groups.google.com/group/comp.lang.python/t/b9ba52627893a79d?hl=en
==============================================================================

== 1 of 1 ==
Date: Wed, Feb 17 2010 1:59 am
From: Dave Angel


Terry Reedy wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">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

==============================================================================
TOPIC: Hubris connects Ruby to Haskell, will there be such a connection
between Python and Haskell?
http://groups.google.com/group/comp.lang.python/t/5b5a0c768c788353?hl=en
==============================================================================

== 1 of 1 ==
Date: Wed, Feb 17 2010 2:31 am
From: jkn


On Feb 17, 2:04 am, Carl Banks <pavlovevide...@gmail.com> 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

==============================================================================
TOPIC: Wrestling with the Py2exe Install, Win7, Py2.5
http://groups.google.com/group/comp.lang.python/t/11d75d0eb3dbf903?hl=en
==============================================================================

== 1 of 2 ==
Date: Wed, Feb 17 2010 3:43 am
From: 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

== 2 of 2 ==
Date: Wed, Feb 17 2010 3:44 am
From: mk


W. eWatson wrote:


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

Regards,
mk


==============================================================================
TOPIC: Which mock library do you prefer?
http://groups.google.com/group/comp.lang.python/t/a12daa9de8354ff2?hl=en
==============================================================================

== 1 of 2 ==
Date: Wed, Feb 17 2010 5:25 am
From: Lacrima


On Feb 16, 7:38 pm, Phlip <phlip2...@gmail.com> 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.


== 2 of 2 ==
Date: Wed, Feb 17 2010 6:26 am
From: Lacrima


On Feb 16, 10:30 pm, Ben Finney <ben+pyt...@benfinney.id.au> wrote:
> Lacrima <lacrima.ma...@gmail.com> 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

==============================================================================
TOPIC: Over(joy)riding
http://groups.google.com/group/comp.lang.python/t/d71fbc10c9d1f900?hl=en
==============================================================================

== 1 of 6 ==
Date: Wed, Feb 17 2010 5:53 am
From: 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

== 2 of 6 ==
Date: Wed, Feb 17 2010 6:25 am
From: Arnaud Delobelle


mk <mrkafk@gmail.com> 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


== 3 of 6 ==
Date: Wed, Feb 17 2010 6:40 am
From: 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

== 4 of 6 ==
Date: Wed, Feb 17 2010 6:52 am
From: 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.


== 5 of 6 ==
Date: Wed, Feb 17 2010 7:08 am
From: 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

== 6 of 6 ==
Date: Wed, Feb 17 2010 8:00 am
From: Kurt Smith


On Wed, Feb 17, 2010 at 9:08 AM, mk <mrkafk@gmail.com> 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

==============================================================================
TOPIC: Shipping Executables
http://groups.google.com/group/comp.lang.python/t/297a2754b1ad0538?hl=en
==============================================================================

== 1 of 1 ==
Date: Wed, Feb 17 2010 5:53 am
From: 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


==============================================================================
TOPIC: Is automatic reload of a module available in Python?
http://groups.google.com/group/comp.lang.python/t/71d2e8e4a38c3b1d?hl=en
==============================================================================

== 1 of 3 ==
Date: Wed, Feb 17 2010 6:27 am
From: "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


== 2 of 3 ==
Date: Wed, Feb 17 2010 6:52 am
From: Arnaud Delobelle


"R (Chandra) Chandrasekhar" <chyavana@gmail.com> 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


== 3 of 3 ==
Date: Wed, Feb 17 2010 7:14 am
From: 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

==============================================================================
TOPIC: How to efficiently extract information from structured text file
http://groups.google.com/group/comp.lang.python/t/bc8840f8a254056b?hl=en
==============================================================================

== 1 of 1 ==
Date: Wed, Feb 17 2010 6:35 am
From: Imaginationworks


On Feb 16, 7:14 pm, Gary Herron <gher...@islandtraining.com> 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.


==============================================================================

You received this message because you are subscribed to the Google Groups "comp.lang.python"
group.

To post to this group, visit http://groups.google.com/group/comp.lang.python?hl=en

To unsubscribe from this group, send email to comp.lang.python+unsubscribe@googlegroups.com

To change the way you get mail from this group, visit:
http://groups.google.com/group/comp.lang.python/subscribe?hl=en

To report abuse, send email explaining the problem to abuse@googlegroups.com

==============================================================================
Google Groups: http://groups.google.com/?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate