moving Connection/PipeConnection between processes

2009-06-13 Thread Randall Smith
I've got a situation in which I'd like to hand one end of a pipe to 
another process.  First, in case you ask why, a spawner process is 
created early before many modules are imported.  That spawner process is 
responsible for creating new processes and giving a proxy to the parent 
process.


   (h1-1   <--  Pipe()   -->h1-2)
  |> child1
  |(s1 < Pipe() > s2)
parent ->  spawner ->|--> child2
  |(h2-1)  <--  Pipe()   -->   (h2-2)
  |--|
  |> child3
   (h3-1   <--  Pipe()   -->h3-2)

When I try to pass Connection h1-1 to the parent using Connection s1, I 
get an error:


TypeError: Required argument 'handle' (pos 1) not found

This error is from unpickling the Connection h1-1.  You can duplicate 
the error like this:


pickled_connection = pickle.dumps(h1-1, 2)
pickle.loads(pickled_connection) # raises the same error

Looking at the pickle docs, I wonder if this could be resolved by adding 
a __getnewargs__ method to _multiprocessing.Connection.  But even if 
that would work I couldn't do it now since it's an extension module. 
I've thought about trying to recreate the Connection.  Looks like it 
should be possible with Connection.fileno().  The Unix portion looks 
easy, but the win32 portion does not.


So if it's possible, what's the best way to pass a Connection to another 
process?


Randall

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


Re: moving Connection/PipeConnection between processes

2009-06-13 Thread Mike Kazantsev
On Sat, 13 Jun 2009 02:23:37 -0500
Randall Smith  wrote:

> I've got a situation in which I'd like to hand one end of a pipe to 
> another process.  First, in case you ask why, a spawner process is 
> created early before many modules are imported.  That spawner process is 
> responsible for creating new processes and giving a proxy to the parent 
> process.
>
...
> 
> Looking at the pickle docs, I wonder if this could be resolved by adding 
> a __getnewargs__ method to _multiprocessing.Connection.  But even if 
> that would work I couldn't do it now since it's an extension module. 
> I've thought about trying to recreate the Connection.  Looks like it 
> should be possible with Connection.fileno().  The Unix portion looks 
> easy, but the win32 portion does not.
> 
> So if it's possible, what's the best way to pass a Connection to another 
> process?

Pickle has nothing to do with the problem since it lay much deeper: in
the OS.

From kernel point of view, every process has it's own "descriptor
table" and the integer id of the descriptor is all the process gets, so
when you say "os.pipe()" kernel actually gives you a number which is
completely meaningless for any other process - it either doesn't exists
in it's descriptor table or points to something else.

So, what you actually need is to tell the kernel to duplicate
underlying object in another process' table (with it's own numbering),
which is usually done via special flag for sendmsg(2) in C, so you
should probably look out for py implementation of this call, which I
haven't stumbled upon, but, admittely, never looked for.


-- 
Mike Kazantsev // fraggod.net


signature.asc
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: distutils extension configuration problem

2009-06-13 Thread jenifer adam
On May 27, 2:10 am, Ron Garret  wrote:
> I'm trying to build PyObjC on an Intel Mac running OS X 10.5.7.  The
> build is breaking because distutils seems to want to build extension
> modules as universal binaries, but some of the libraries it depends on
> are built for intel-only, i.e.:
>
> [...@mickey:~/Desktop/pyobjc-framework-ScreenSaver-2.2b2]$ python2.6
> setup.py build
> /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/distutils
> /dist.py:266: UserWarning: Unknown distribution option: 'options'
>   warnings.warn(msg)
> running build
> running build_py
> running build_ext
> building 'ScreenSaver._inlines' extension
> gcc -arch ppc -arch i386 -isysroot /Developer/SDKs/MacOSX10.4u.sdk-g
> -bundle -undefined dynamic_lookup
> build/temp.macosx-10.3-i386-2.6/Modules/_ScreenSaver_inlines.o -o
> build/lib.macosx-10.3-i386-2.6/ScreenSaver/_inlines.so -framework
> ScreenSaver
> ld: in /Developer/SDKs/MacOSX10.4u.sdk/usr/local/lib/libTIFF.dylib, file
> is not of required architecture for architecture ppc
> collect2: ld returned 1 exit status
> lipo: can't open input file:
> /var/folders/nT/nTiypn-v2RatkU+BYncrKU+++TI/-Tmp-//ccMFYRkt.out (No such
> file or directory)
> error: command 'gcc' failed with exit status 1
>
> [...@mickey:~/Desktop/pyobjc-framework-ScreenSaver-2.2b2]$ file
> build/temp.macosx-10.3-i386-2.6/Modules/_ScreenSaver_inlines.o
> build/temp.macosx-10.3-i386-2.6/Modules/_ScreenSaver_inlines.o: Mach-O
> universal binary with 2 architectures
> build/temp.macosx-10.3-i386-2.6/Modules/_ScreenSaver_inlines.o (for
> architecture ppc): Mach-O object ppc
> build/temp.macosx-10.3-i386-2.6/Modules/_ScreenSaver_inlines.o (for
> architecture i386):   Mach-O object i386
>
> [...@mickey:~/Desktop/pyobjc-framework-ScreenSaver-2.2b2]$ file
> /usr/local/lib/libtiff.dylib
> /usr/local/lib/libtiff.dylib: Mach-O dynamically linked shared library
> i386
>
> How do I get distutils to stop trying to build extensions as universal
> binaries?
>
> Thanks,
> rg

Check http://www.voipsipsdk.com its a good one.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can't run PyQt apps with MacPython

2009-06-13 Thread jenifer adam
On May 13, 8:53 pm, Morad  wrote:
> I recently got a new MacBook Pro with Leopard, and would like to
> develop using Python and PyQt. I installed the latest QtSDK, updated
> MacPython to V 2.5.4 and then proceeded to install SIP and PyQt as
> described in Mark Summerfield's book on PyQt Programming. Everything
> went fine and none of the scripts complained. However when I want to
> run a demo app, I get the error message "Fatal Python error:
> Interpreter not initialized (version mismatch?) / Abort Trap". Anybody
> can help?

Check http://www.voipsipsdk.com its a good one.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help AIX 5.3 build on Python-3.1a2

2009-06-13 Thread jenifer adam
On Apr 26, 2:14 pm, Jeroen Ruigrok van der Werven  wrote:
> -On [20090425 19:17], Aahz (a...@pythoncraft.com) wrote:
>
> >In article 
> >,
> >  wrote:
> >>"Include/token.h", line 42.9: 1506-213 (S) Macro name TILDE cannot be
> >>redefined.
> >>"Include/token.h", line 42.9: 1506-358 (I) "TILDE" is defined on line
> >>250 of /usr/include/sys/ioctl.h.
>
> >Can you try trimming down the compilation to a small reproducible case?
>
> I thought it was already clear from what was provided?
>
> Include/token.h has a #define for TILDE and apparently AIX has a #define for
> TILDE in /usr/include/sys/ioctl.h as well. So you can a redefinition.
>
> One way around it, depending how AIX protects headers might be to change
> Include/token.h to either:
>
> #if defined(_SYS_IOCTL_H_)
> #undef TILDE
> #define TILDE           32
> #endif
>
> or
>
> #if defined(aix)
> #undef TILDE
> #define TILDE           32
> #endif
>
> --
> Jeroen Ruigrok van der Werven  / asmodai
> イェルーン ラウフロック ヴァン デル ウェルヴェンhttp://www.in-nomine.org/|http://www.rangaku.org/| 
> GPG: 2EAC625B
> A rose is a rose is a rose is a rose...

Check http://www.voipsipsdk.com its a good one.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: moving Connection/PipeConnection between processes

2009-06-13 Thread jenifer adam
On Jun 13, 1:11 pm, Mike Kazantsev  wrote:
> On Sat, 13 Jun 2009 02:23:37 -0500
>
>
>
>
>
> Randall Smith  wrote:
> > I've got a situation in which I'd like to hand one end of a pipe to
> > another process.  First, in case you ask why, a spawner process is
> > created early before many modules are imported.  That spawner process is
> > responsible for creating new processes and giving a proxy to the parent
> > process.
>
> ...
>
> > Looking at the pickle docs, I wonder if this could be resolved by adding
> > a __getnewargs__ method to _multiprocessing.Connection.  But even if
> > that would work I couldn't do it now since it's an extension module.
> > I've thought about trying to recreate the Connection.  Looks like it
> > should be possible with Connection.fileno().  The Unix portion looks
> > easy, but the win32 portion does not.
>
> > So if it's possible, what's the best way to pass a Connection to another
> > process?
>
> Pickle has nothing to do with the problem since it lay much deeper: in
> the OS.
>
> From kernel point of view, every process has it's own "descriptor
> table" and the integer id of the descriptor is all the process gets, so
> when you say "os.pipe()" kernel actually gives you a number which is
> completely meaningless for any other process - it either doesn't exists
> in it's descriptor table or points to something else.
>
> So, what you actually need is to tell the kernel to duplicate
> underlying object in another process' table (with it's own numbering),
> which is usually done via special flag for sendmsg(2) in C, so you
> should probably look out for py implementation of this call, which I
> haven't stumbled upon, but, admittely, never looked for.
>
> --
> Mike Kazantsev // fraggod.net
>
>  signature.asc
> < 1KViewDownload

Check http://www.voipsipsdk.com its a good one.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Lexical scope: converting Perl to Python

2009-06-13 Thread jenifer adam
On Jun 13, 10:44 am, Mike Kazantsev  wrote:
> On Fri, 12 Jun 2009 22:02:53 -0700 (PDT)
>
>
>
>
>
> Andrew Savige  wrote:
> > I'd like to convert the following Perl code to Python:
>
> >  use strict;
> >  {
> >    my %private_hash = ( A=>42, B=>69 );
> >    sub public_fn {
> >  my $param = shift;
> >  return $private_hash{$param};
> >    }
> >  }
> >  print public_fn("A");    # good:  prints 42
> >  my $x = $private_hash{"A"};  # error: good, hash not in scope
>
> ...
>
> > What is the Pythonic equivalent of Perl's lexical scope, as
> > illustrated by the code snippet above?
>
> If you're using scope for garbage-collecting purposes, there's "with"
> statement and contextlib:
>
>   from contextlib import contextmanager
>
>   @contextmanager
>   def get_hash():
>     complex_hash = dict(A=42, B-69)
>     try: yield complex_hash
>     except Exception as ex:
>       del complex_hash # complex destructor ;)
>       raise ex
>
>   with get_hash() as hash:
>     # do stuff with hash
>
> Note that this only makes sense if you need to implement some complex
> operation on hash destruction, and do that whatever-happens-inside-with
> to close the object, obviously not the case with simple dict above.
>
> And if you want to obfuscate one part of your code from another, you'll
> probably have better luck with languages like java, since no one seem
> to care about such stuff with python, so it'd be a hack against the
> language, at best.
> Why would you want to hide the code from itself, anyway? It's not like
> you'd be able to accomplish it - code can easily grep it's process body
> in memory and harvest all the "private" values, so I'd suggest getting
> some fresh air when you start to feel like doing that.
>
> --
> Mike Kazantsev // fraggod.net
>
>  signature.asc
> < 1KViewDownload

Check http://www.voipsipsdk.com its a good one.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How should I compare two txt files separately coming from windows/dos and linux/unix

2009-06-13 Thread Piet van Oostrum
> Emile van Sebille  (EvS) wrote:

>EvS> On 6/11/2009 12:09 AM higer said...
>>> Tool can certainly be used to compare two files,but I just want to
>>> compare them using Python code.
>>> 

>EvS> difflib?

If I understand correctly the OP just wanted to know whether two files
were equal, not what the differences would be. In that case difflib is
overkill. 

On the other hand `equal' has many meanings. Ignoring line endings is
one option, ignoring trailing whitespace could be another one. Yet
another one could be normalizing the character encoding, etc.
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: uncompress base64-gzipped string

2009-06-13 Thread Niels Egberts
On Sat, Jun 13, 2009 at 4:38 AM, John Machin wrote:
> | >>> guff[:100]
> | '\x1b\x00\x00\x00\x1b\x00\x00\x00\x1b\x00\x00\x00 [snip]
>
> What a long journey: parse xml, base64 decode, gunzip,
> and you're still not home; next stop is struct.unpack ...
>
> HTH,
> John

Thanks for the help! I dont think I could have done that.

And thanks for the tip on struct.unpack I managed to get a list of
integers to work with.

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


Re: install Python-2.4.4 from source (parallel to existing Python-2.6)

2009-06-13 Thread Simon

Christian Heimes wrote:

Simon schrieb:

Christian Heimes wrote:

Simon wrote:

I installed Python-2.4.4.tar.bz2 from python.org, using gcc-4.3 (within
openSUSE 11.1 x86_64) via 'make altinstall'.

First, I tried to configure with the following flags:
--prefix=/opt/python-24 --enable-framework --with-pydebug
This provoked an error during compilation via make (sorry, the list was
so long, but I will post it, if it helps).

--enable-framework is for Mac OS X only.


Second, configured again without any flags. The installation by 'make
altinstall' to /usr/local was a success. Python2.6 seams unaffected,
too. So, I got my parallel installation.

You have chosen the correct and canonical way to install a parallel
installation of Python.


However, I cannot import modules like Tkinter or readline within
python2.4.

You must install the development library of tk, readline, zlib and
libbz2 prior to configure && make.

Try this on your box:

zypper install gcc make autoconf automake libtool zlib-devel
readline-devel tk-devel tcl-devel sqlite2-devel libbz2-devel
libopenssl-devel



Unfortunately, I got the following errors, while compiling via make.
Please, see attached text file for details about everything I did from
installing the missing packages via zypper until make.


zypper should have installed all necessary dependencies, including a
whole bunch of X11 headers. Something seems to be wrong on your system
or SuSE's package dependencies.
I know why I dislike SuSE. :) Although I started my Linux career 12
years ago with SuSE I prefer Debian based systems since Woody came out
in 2002. :)


X11 says:
Program 'X11' is present in package 'xorg-x11', which is installed on
your system.


zypper search X11 | grep devel
...
zypper install xorg-x11-devel

That should (hopefully) do the trick. On a Debian based systems it's
much easier to install all Python dependencies with "apt-get build-dep
python2.5"

To quote SuSE: "Have a lot of fun ..."

Und viel Glück!

Christian



Danke :)

On Monday, I will be able to tell if it worked out, or if I have to try 
a new strategy, e.g. to convince the admin about (K)ubuntu.

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


Re: CAD file format specifications?

2009-06-13 Thread Anthra Norell

Andres Acosta wrote:
HI there Anthara have you checked out www.Blender.org, It is open 
source and accepts a lot of your formats. for import and export.

Anrdres
Anthra Norell wrote:

Hi,
  Anyone working with CAD who knows about numeric data entry? I have 
3d coordinates of a construction site on a slope (borders, setbacks 
and isometric elevation lines). Someone made me aware of Google's 
Sketch Up. It looks very attractive for the purpose of architectural 
planning, especially suited to create visual impressions. Doodling 
seems easy. But I have to start with modeling the terrain and the 
envelope and the only way to do that seems to be in one of several 
CAD file formats (skp, dwg, dxf, 3ds, ddf and dem). So I need to cast 
my numbers into one of these formats. Any suggestions?


Thanks

Frederic





Andres,

 Thanks for the tip. I wasn't aware of Blender. The web page looks 
promising. I downloaded it and am going to have a look at it.


Frederic

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


Re: Multi-Threading and KeyboardInterrupt

2009-06-13 Thread koranthala
On Jun 13, 10:25 am, Mike Kazantsev  wrote:
> On Thu, 11 Jun 2009 22:35:15 -0700
> Dennis Lee Bieber  wrote:
>
>
>
> > On Thu, 11 Jun 2009 08:44:24 -0500, "Strax-Haber, Matthew (LARC-D320)"
> >  declaimed the following in
> > gmane.comp.python.general:
>
> > > I sent this to the Tutor mailing list and did not receive a response.
> > > Perhaps one of you might be able to offer some sagely wisdom or pointed
> > > remarks?
>
> > > Please reply off-list and thanks in advance. Code examples are below in
> > > plain text.
>
> >    Sorry -- you post to a public forum, expect to get the response on a
> > public forum...
>
> > > > My program runs interactively by allowing the user to directly
> > > > interact with the python prompt. This program has a runAll() method
> > > > that runs a series of subprocesses with a cap on how many instances
> > > > are running at a time. My intent is to allow the user to use Ctrl-C to
> > > > break these subprocesses. Note that while not reflected in the demo
>
> >    Are they subprocesses or threads? Your sample code seems to be using
> > threads.
>
> >    When using threads, there is no assurance that any thread other than
> > the main program will receive a keyboard interrupt.
>
> In fact, no thread other than the main will get interrupt.
>
>
>
> > > def runAll():
> > >     workers = [ Thread(target = runSingle, args = [i])
> > >             for i in xrange(MAX_SUBPROCS + 1) ]
> > >     try:
> > >         for w in workers:
> > >             w.start()
> > >     except KeyboardInterrupt:
> > >         ## I want this to be shown on a KeyboardInterrupt
> > >         print '* stopped midway '
>
> >    You are unlikely to see that... After you start the defined worker
> > /threads/ (which doesn't take very long -- all threads will be started,
> > but some may immediately block on the semaphore) this block will exit
> > and you will be at...
>
> > >     for w in workers:
> > >         w.join()
>
> >    ... a .join() call, which is the most likely position at which the
> > keyboard interrupt will be processed, killing the main program thread
> > and probably generating some errors as dangling active threads are
> > forceably killed.
>
> There was quite interesting explaination of what happens when you send
> ^C with threads, posted on concurrency-sig list recently:
>
>  http://blip.tv/file/2232410
>  http://www.dabeaz.com/python/GIL.pdf
>
> Can be quite shocking, but my experience w/ threads only confirms that.
>
> --
> Mike Kazantsev // fraggod.net
>
>  signature.asc
> < 1KViewDownload

Thank you very much for the link Mike.
Are there other videos/audio like this? I am learning more from these
videos than by experience alone.
I did find one - http://www.awaretek.com/python/ - are there other
links?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Lexical scope: converting Perl to Python

2009-06-13 Thread I V
On Fri, 12 Jun 2009 22:02:53 -0700, Andrew Savige wrote:
> Notice that this code uses Perl's lexical scope to hide the
> %private_hash variable, but not the public_fn() function.

You might try:

def  public_fn(param):
private_hash = publicfn.private_hash
return private_hash[param]

public_fn.private_hash = {'A':42, 'B':69}

(You don't have to assign public_fn.private_hash to a local variable, but 
you might want to, to avoid extra typing, if you refer to private_hash 
more than a couple of times in the function).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Making the case for repeat

2009-06-13 Thread Steven D'Aprano
Boris Borcic wrote:

> This reminds me of an early programming experience that left me with a
> fascination. At a time where code had to fit in a couple dozens kilobytes,
> I once had to make significant room in what was already very tight and
> terse code. Code factoring *did* provide the room in the end, but the
> fascinating part came before.
> 
> There was strictly no redundancy apparent at first, and finding a usable
> one involved contemplating code execution paths for hours until some
> degree of similarity was apparent between two code path families. And
> then, the fascinating part, was to progressively mutate both *away* from
> minimality of code, in order to enhance the similarity until it could be
> factored out.
> 
> I was impressed; in various ways. First; that the effort could be
> characterized quite mechanically and in a sense stupidly as finding a
> shortest equivalent program, while the subjective feeling was that the
> task exerted perceptive intelligence to the utmost. Second; by the notion
> that a global constraint of code minimization could map more locally to a
> constraint that drew code to expand. Third; that the process resulted in
> bottom-up construction of what's usually constructed top-down, mimicking
> the willful design of the latter case, eg. an API extension, as we might
> call it nowadays.

This is much the same that happens in maximisation problems: the value gets
trapped in a local maximum, and the only way to reach a greater global
maximum is to go downhill for a while.

I believe that hill-climbing algorithms allow some downhill movement for
just that reason. Genetic algorithms allow "mutations" -- and of course
real evolution of actual genes also have mutation.


-- 
Steven

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


Re: Lexical scope: converting Perl to Python

2009-06-13 Thread Nick Craig-Wood
Andrew Savige  wrote:
> 
>  I'd like to convert the following Perl code to Python:
> 
>   use strict;
>   {
>     my %private_hash = ( A=>42, B=>69 );
>     sub public_fn {
>   my $param = shift;
>   return $private_hash{$param};
>     }
>   }
>   print public_fn("A");    # good:  prints 42
>   my $x = $private_hash{"A"};  # error: good, hash not in scope
> 
>  The real code is more complex; the above is a simplified example.
> 
>  Notice that this code uses Perl's lexical scope to hide the
>  %private_hash variable, but not the public_fn() function.
> 
>  While I could convert this code to the following Python code:
> 
>   private_hash = dict( A=42, B=69 )
>   def public_fn(param):
>     return private_hash[param]
>   print public_fn("A") # good:  prints 42
>   x = private_hash["A"]    # works: oops, hash is in scope
> 
>  I'm not happy with that because I'd like to limit the scope of the
>  private_hash variable so that it is known only inside public_fn.
> 
>  Of course, I could hide the hash like so:
> 
>   def public_fn(param):
>     private_hash = dict( A=42, B=69 )
>     return private_hash[param]
> 
>  yet I'm not happy with that either because of the repeated
>  initialization the hash each time the function is called.
> 
>  What is the Pythonic equivalent of Perl's lexical scope, as
>  illustrated by the code snippet above?

Either

_private_hash = dict( A=42, B=69)

def public_fn(param):
return _private_hash[param]

Or

def public_fn(param, _private_hash = dict( A=42, B=69)):
return _private_hash[param]

Is probably the pythonic equivalents.  Note that private_hash starts
with an underscore which means it won't be exported from a module by
default and it is a convention that it is private and shouldn't be
fiddled with.  I'd probably go with the latter of the two examples.

-- 
Nick Craig-Wood  -- http://www.craig-wood.com/nick
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Multi-Threading and KeyboardInterrupt

2009-06-13 Thread Mike Kazantsev
On Sat, 13 Jun 2009 04:42:16 -0700 (PDT)
koranthala  wrote:

> Are there other videos/audio like this? I am learning more from these
> videos than by experience alone.

Indeed, it is a very interesting presentation, but I'm afraid I've
stumbled upon it just as you did, but on concurrency-sig mailing list.

It's a relatively new list (now hosted on mail.python.org), not
specifically dedicated to podcasts or, for that matter, any
implementation details. I haven't seen any other material like this
there.

> I did find one - http://www.awaretek.com/python/ - are there other
> links?

Thanks for sharing this link, although I prefer such information in
written form - it's easier/faster to work with and much more accessible.

-- 
Mike Kazantsev // fraggod.net


signature.asc
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Exceptions and Object Destruction (was: Problem with apsw and garbage collection)

2009-06-13 Thread Piet van Oostrum
> Nikolaus Rath  (NR) wrote:

>NR> Is there a way to have the obj variable (that is created in dostuff())
>NR> destroyed earlier than at the end of the program? As you can see, I
>NR> already tried to explicitly call the garbage collector, but this does
>NR> not help.

The exact time of the destruction of objects is an implementation detail
and should not be relied upon.
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Making the case for repeat

2009-06-13 Thread K4NTICO
On 13 juin, 14:15, Steven D'Aprano
 wrote:
> Boris Borcic wrote:
> > This reminds me of an early programming experience that left me with a
> > fascination. At a time where code had to fit in a couple dozens kilobytes,
> > I once had to make significant room in what was already very tight and
> > terse code. Code factoring *did* provide the room in the end, but the
> > fascinating part came before.
>
> > There was strictly no redundancy apparent at first, and finding a usable
> > one involved contemplating code execution paths for hours until some
> > degree of similarity was apparent between two code path families. And
> > then, the fascinating part, was to progressively mutate both *away* from
> > minimality of code, in order to enhance the similarity until it could be
> > factored out.
>
> > I was impressed; in various ways. First; that the effort could be
> > characterized quite mechanically and in a sense stupidly as finding a
> > shortest equivalent program, while the subjective feeling was that the
> > task exerted perceptive intelligence to the utmost. Second; by the notion
> > that a global constraint of code minimization could map more locally to a
> > constraint that drew code to expand. Third; that the process resulted in
> > bottom-up construction of what's usually constructed top-down, mimicking
> > the willful design of the latter case, eg. an API extension, as we might
> > call it nowadays.
>
> This is much the same that happens in maximisation problems: the value gets
> trapped in a local maximum, and the only way to reach a greater global
> maximum is to go downhill for a while.
>
> I believe that hill-climbing algorithms allow some downhill movement for
> just that reason. Genetic algorithms allow "mutations" -- and of course
> real evolution of actual genes also have mutation.
>
> --
> Steven

Indeed exactly. But it wasn't quite the same to think through it first
hand, as I said that the subjective feeling was "perceptive
intelligence got exercized to the utmost".

To illustrate, it is very much the memory of that experience - the
perceptive training - that made me notice, for another high point,
what I think to be a common factor worthy of capture between the
sociology of Archimedes' Eureka and that of Einstein's E=mc^2, let's
just describe Archimedes' case in the right manner: There is a trading
port city, and thus citizens who are experts in both floating ships on
the seas and of weighting goods on the scale in the markets. And then
comes Archimedes who says : "hey, experts, I show you that these two
aeras of your expertise that of course you think have nothing to do
with each other except for what you know so well and clear - they are
in fact two faces of a single coin that you ignore."

And thus a codeful question : "What does F(Syracuse) hear if F(Eureka)
is the = in E=mc^2 ?"

And a more serious one : what happens to the potential for similar
discoveries, when society specializes expertise to the point that
there isn't any more any community of "simultaneous experts" ?

Cheers, BB
--
"Hope achieves the square root of the impossible"


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


[RELEASED] Python 3.1 Release Candidate 2

2009-06-13 Thread Benjamin Peterson
On behalf of the Python development team, I'm happy to announce the second
release candidate of Python 3.1.

Python 3.1 focuses on the stabilization and optimization of the features and
changes that Python 3.0 introduced.  For example, the new I/O system has been
rewritten in C for speed.  File system APIs that use unicode strings now handle
paths with undecodable bytes in them. Other features include an ordered
dictionary implementation, a condensed syntax for nested with statements, and
support for ttk Tile in Tkinter.  For a more extensive list of changes in 3.1,
see http://doc.python.org/dev/py3k/whatsnew/3.1.html or Misc/NEWS in the Python
distribution.

This is a release candidate, and as such, we do not recommend use in production
environments.  However, please take this opportunity to test the release with
your libraries or applications.  This will hopefully discover bugs before the
final release and allow you to determine how changes in 3.1 might impact you.
If you find things broken or incorrect, please submit a bug report at

 http://bugs.python.org

For more information and downloadable distributions, see the Python 3.1 website:

 http://www.python.org/download/releases/3.1/

See PEP 375 for release schedule details:

 http://www.python.org/dev/peps/pep-0375/



Enjoy,
-- Benjamin

Benjamin Peterson
benjamin at python.org
Release Manager
(on behalf of the entire python-dev team and 3.1's contributors)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: preferring [] or () in list of error codes?

2009-06-13 Thread Mel
Gunter Henriksen wrote:
[ ... ]
> I guess to me, fundamentally, the interpretation of
> tuple as a sequence whose elements have semantic meaning
> implicitly defined by position is a relatively abstract
> intrepretation whose value is dubious relative to the
> value of immutability, since it seems like a shortcut
> which sacrifices explicitness for the sake of brevity.

The immutability makes it easier to talk about the semantic meanings.  After 
you do
> event_timestamp = (2009, 06, 04, 05, 02, 03)
there's nothing that can happen to the tuple to invalidate
> (year, month, day, hour, minute, second) = event_timestamp
even though, as you say, there's nothing in the tuple to inform anybody 
about the year, month, day, ... interpretation.

And of course there's nothing in a C struct object that isn't in the 
equivalent Python tuple.  The difference is that the C compiler has arranged 
all the outside code that uses the struct object to use it in the correct 
way.  The only object I've found in Python that truly replaces a struct 
object in C is a dict with string keys -- or an object that uses such a dict 
as its __dict__.


Mel.

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


Re: Python 3.1 Release Candidate 2

2009-06-13 Thread Khalid
On Jun 13, 5:46 pm, Benjamin Peterson  wrote:
> On behalf of the Python development team, I'm happy to announce the second
> release candidate of Python 3.1.
>
> Python 3.1 focuses on the stabilization and optimization of the features and
> changes that Python 3.0 introduced.  For example, the new I/O system has been
> rewritten in C for speed.  File system APIs that use unicode strings now 
> handle
> paths with undecodable bytes in them. Other features include an ordered
> dictionary implementation, a condensed syntax for nested with statements, and
> support for ttk Tile in Tkinter.  For a more extensive list of changes in 3.1,
> seehttp://doc.python.org/dev/py3k/whatsnew/3.1.htmlor Misc/NEWS in the Python
> distribution.
>
> This is a release candidate, and as such, we do not recommend use in 
> production
> environments.  However, please take this opportunity to test the release with
> your libraries or applications.  This will hopefully discover bugs before the
> final release and allow you to determine how changes in 3.1 might impact you.
> If you find things broken or incorrect, please submit a bug report at
>
>      http://bugs.python.org
>
> For more information and downloadable distributions, see the Python 3.1 
> website:
>
>      http://www.python.org/download/releases/3.1/
>
> See PEP 375 for release schedule details:
>
>      http://www.python.org/dev/peps/pep-0375/
>
> Enjoy,
> -- Benjamin
>
> Benjamin Peterson
> benjamin at python.org
> Release Manager
> (on behalf of the entire python-dev team and 3.1's contributors)

GOOD NEWS ... THANKS
-- 
http://mail.python.org/mailman/listinfo/python-list


Good books in computer science?

2009-06-13 Thread koranthala
Hi all,
I do understand that this is not a python question and I apologize
for that straight up.
But I am a full time follower of this group and I have seen very
very brilliant programmers and solutions.
I also want to be a good programmer - so this question.

Which are the classic books in computer science which one should
peruse?
I have  (a) Code Complete (b) GOF (c) Art of programming.

Art of programming was too tough for me - and I couldnt understand
much. The other two were good books - I understood and implemented
quite a bit from both.
What are the other books which I should peruse?

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


Re: uncompress base64-gzipped string

2009-06-13 Thread Scott David Daniels

Niels Egberts wrote:

On Sat, Jun 13, 2009 at 4:38 AM, John Machin wrote:
...
And thanks for the tip on struct.unpack I managed to get a list of
integers to work with.


You might make do with array.fromstring (perhaps using .byteswap
afterwards) to get your numbers if you are talking more than a handful.

--Scott David Daniels
scott.dani...@acm.org

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


Re: Good books in computer science?

2009-06-13 Thread python
Timeless classics - highly recommended:

Software Tools by Plaugher
Mythical Man Month by Brooks

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


Re: Question about None

2009-06-13 Thread Terry Reedy

Steven D'Aprano wrote:
[ snip excellent discussion of proper use of assert]


The third bug (the nitpick) is a bug in the API: you name the argument "nxt"
with no e, but in the error message, you call it "next" with an e. The fact
that any intelligent person should be able to guess what parameter the
error is referring to doesn't make it less wrong.

So, fixing the three issues:

def setNext(next):
if not (next is None or isinstance(next, Node)):
raise ValueError("next must be a Node")
self.next = next


[counternit] Of course, next() is a builtin function and some consider 
reusing builtin names as bare names (versus attribute names) a bad 
habit.  I think the OP had a good idea in using a variation, but should 
have been consistent.


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


Re: Good books in computer science?

2009-06-13 Thread Paul Rubin
koranthala  writes:
> Which are the classic books in computer science which one should
> peruse?
> I have  (a) Code Complete (b) GOF (c) Art of programming.
> 
> Art of programming was too tough for me - and I couldnt understand
> much. The other two were good books - I understood and implemented
> quite a bit from both.
> What are the other books which I should peruse?

Code Complete and GOF are software engineering books but not really
CS books.  TAOCP is a CS book but a bit old fashioned.  Other classics:

Introduction to Algorithms by Thomas H. Cormen, Charles E. Leiserson,
Ronald L. Rivest, and Clifford Stein.

Structure and Interpretation of Computer Programs by Harold Abelson
and Gerald Jay Sussman (online at mitpress.mit.edu/sicp)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Good books in computer science?

2009-06-13 Thread koranthala

> Code Complete and GOF are software engineering books but not really
> CS books.

I understand and concur. Since I am a software engineer - coming in to
software from a different background - what I am looking for is self-
improvement books for a software engineer. This can include both CS
and Software books - even though I found that CS books are much less
understandable to me :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about None

2009-06-13 Thread John Yeung
On Jun 13, 2:29 am, Steven D'Aprano
 wrote:
> Paul LaFollette wrote:
> > 3) (this is purely philosophical but I am curious)
> > Would it not be more intuitive if
> > isinstance(None, ) returned true?
>
> Good grief no!!!
>
> None is an object. It has a type, NoneType. It's *not* a
> string, or a float, or an int, or a list, so why would
> you want isinstance() to *lie* and say that it is?

Because you might want None to behave as though it were nothing at
all.

Paul LaFollette is probably thinking along the lines of formal logic
or set theory.  It's a little bit confused because programming isn't
quite the same as math, and so it's a common question when designing
and implementing programming languages how far to take certain
abstractions.  In some languages, nil, null, or none will try to
behave as mathematically close to "nothing" (complete absence of
anything) as possible, even though in reality they have to have some
concrete implementation, such as perhaps being a singleton object.
But mathematically speaking, it's intuitive that "nothing" would match
any type.

I find that it's somewhat like the confusion that often occurs
regarding the all() function.  Some people are surprised that all([])
returns True, but it's the same logic behind the truth of the
statement "every element of the empty set is an integer".  It's also
true that every element of the empty set is a float.  Or an elephant.

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


Re: Question about None

2009-06-13 Thread Paul Rubin
John Yeung  writes:
> Because you might want None to behave as though it were nothing at all.

Sure, you might also want strings to behave as if they were ints, but
wishing doesn't make it so.

> But mathematically speaking, it's intuitive that "nothing" would match
> any type.

Completely wrong.  The concept you're thinking of in denotational
semantics is called "bottom", but bottom is not a value that functions
can compute and return.  It is really the absence of a value.  We
would say that a function semantically returns bottom, if calling does
something like loop forever (never return), or it makes the program
crash, or something like that.  None is a value which in Haskell has a
particular type.  In some other languages, there are None-like values
in more than one type, like in Haskell, for any type there is a
Nothing for the Maybe of that type, but they all are of differing
types, just like Python has an integer 0 and a floating point 0 that
are of differing types.

> I find that it's somewhat like the confusion that often occurs
> regarding the all() function. 

It's the other way though, all([])=True makes sense,
isinstance(None,int) does not.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about None

2009-06-13 Thread Paul Rubin
Paul Rubin  writes:
> crash, or something like that.  None is a value which in Haskell has a

I got ahead of myself, of course I meant Python (the *next* sentence
was going to mention Haskell).  The corresponding concept in Haskell
is called Nothing, which lives in a type functor called Maybe.  I
don't have time to go into it right now but there is a notion among
language weenies these days that the presence of something like None
(in Python) or Null (in Java, SQL, etc) is a wart in a language and
that it's preferable to have something like Maybe.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: moving Connection/PipeConnection between processes

2009-06-13 Thread Randall Smith

Mike Kazantsev wrote:

On Sat, 13 Jun 2009 02:23:37 -0500
Randall Smith  wrote:

I've got a situation in which I'd like to hand one end of a pipe to 
another process.  First, in case you ask why, a spawner process is 
created early before many modules are imported.  That spawner process is 
responsible for creating new processes and giving a proxy to the parent 
process.



...
Looking at the pickle docs, I wonder if this could be resolved by adding 
a __getnewargs__ method to _multiprocessing.Connection.  But even if 
that would work I couldn't do it now since it's an extension module. 
I've thought about trying to recreate the Connection.  Looks like it 
should be possible with Connection.fileno().  The Unix portion looks 
easy, but the win32 portion does not.


So if it's possible, what's the best way to pass a Connection to another 
process?


Pickle has nothing to do with the problem since it lay much deeper: in
the OS.

From kernel point of view, every process has it's own "descriptor
table" and the integer id of the descriptor is all the process gets, so
when you say "os.pipe()" kernel actually gives you a number which is
completely meaningless for any other process - it either doesn't exists
in it's descriptor table or points to something else.

So, what you actually need is to tell the kernel to duplicate
underlying object in another process' table (with it's own numbering),
which is usually done via special flag for sendmsg(2) in C, so you
should probably look out for py implementation of this call, which I
haven't stumbled upon, but, admittely, never looked for.



As I was referring to earlier, Unix is easy.

fd = mycon.fileno()
new_con = _multiprocessing.Connection(os.dup(fd))

But Windows?

The implementation of the pipe creation is on line 167 of connection.py. 
  I don't know where to start.


Randall

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


Perl's @foo[3,7,1,-1] ?

2009-06-13 Thread kj


Switching from Perl here, and having a hard time letting go...

Suppose I have an "array" foo, and that I'm interested in the 4th, 8th,
second, and last element in that array.  In Perl I could write:

  my @wanted = @foo[3, 7, 1, -1];

I was a bit surprised when I got this in Python:

>>> wanted = foo[3, 7, 1, -1]
Traceback (most recent call last):
  File "", line 1, in 
TypeError: list indices must be integers

Granted, Perl's syntax is often obscure and hard-to-read, but in
this particular case I find it quite transparent and unproblematic,
and the fictional "pythonized" form above even more so.

The best I've been able to come up with in Python are the somewhat
Perl-like-in-its-obscurity:

>>> wanted = map(foo.__getitem__, (3, 7, 1, -1))

or the clearer but unaccountably sesquipedalian

>>> wanted = [foo[i] for i in 3, 7, 1, -1]
>>> wanted = [foo[3], foo[7], foo[7], foo[-1]]

Are these the most idiomatically pythonic forms?  Or am I missing
something better?

TIA!

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


Re: Good books in computer science?

2009-06-13 Thread Nathan Stoddard
On Sat, 13 Jun 2009 08:49:52 -0700, koranthala wrote:

> Hi all,
> I do understand that this is not a python question and I apologize
> for that straight up.
> But I am a full time follower of this group and I have seen very
> very brilliant programmers and solutions.
> I also want to be a good programmer

The best way to become a good programmer is to program. Write a lot of
code; work on some large projects. This will improve your skill more than
anything else. It's also important to learn new languages regularly. I
recommend to learn C, Python, and Lisp first.

> Which are the classic books in computer science which one should
> peruse?

A list of some good books is at steve.yegge.googlepages.com/ten-great-
books. Also read programming blogs.





-- 
Nathan Stoddard, http://nathanstoddard.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Perl's @foo[3,7,1,-1] ?

2009-06-13 Thread Jack Diederich
On Sat, Jun 13, 2009 at 2:11 PM, kj wrote:
>
>
> Switching from Perl here, and having a hard time letting go...
>
> Suppose I have an "array" foo, and that I'm interested in the 4th, 8th,
> second, and last element in that array.  In Perl I could write:
>
>  my @wanted = @foo[3, 7, 1, -1];
>
> I was a bit surprised when I got this in Python:
>
 wanted = foo[3, 7, 1, -1]
> Traceback (most recent call last):
>  File "", line 1, in 
> TypeError: list indices must be integers
>
> Granted, Perl's syntax is often obscure and hard-to-read, but in
> this particular case I find it quite transparent and unproblematic,
> and the fictional "pythonized" form above even more so.
>
> The best I've been able to come up with in Python are the somewhat
> Perl-like-in-its-obscurity:
>
 wanted = map(foo.__getitem__, (3, 7, 1, -1))
>
> or the clearer but unaccountably sesquipedalian
>
 wanted = [foo[i] for i in 3, 7, 1, -1]
 wanted = [foo[3], foo[7], foo[7], foo[-1]]
>
> Are these the most idiomatically pythonic forms?  Or am I missing
> something better?
>

There is only so much room in the syntax for common cases before you
end up with ... perl (no offense intended, I'm a perl monk[1]).   The
Python grammar isn't as context sensitive or irregular as the perl
grammar so mylist[1,2,3]  so the "1,2,3" tuple is always interpreted
as a tuple and the square brackets always expect an int or a slice.
Not including special cases everywhere means there isn't a short way
to handle special cases but it also means human readers have to
remember fewer special cases.   Perl and Python make different
tradeoffs in that respect.


-Jack

[1] http://www.perlmonks.org/?node_id=111952
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Perl's @foo[3,7,1,-1] ?

2009-06-13 Thread Nick Craig-Wood
kj  wrote:
> 
> 
>  Switching from Perl here, and having a hard time letting go...
> 
>  Suppose I have an "array" foo, and that I'm interested in the 4th, 8th,
>  second, and last element in that array.  In Perl I could write:
> 
>my @wanted = @foo[3, 7, 1, -1];
> 
>  I was a bit surprised when I got this in Python:
> 
> >>> wanted = foo[3, 7, 1, -1]
>  Traceback (most recent call last):
>File "", line 1, in 
>  TypeError: list indices must be integers

You've just tried to index a list with a tuple...

If foo was a dictionary then this might make sense.

>  Granted, Perl's syntax is often obscure and hard-to-read, but in
>  this particular case I find it quite transparent and unproblematic,
>  and the fictional "pythonized" form above even more so.
> 
>  The best I've been able to come up with in Python are the somewhat
>  Perl-like-in-its-obscurity:
> 
> >>> wanted = map(foo.__getitem__, (3, 7, 1, -1))
> 
>  or the clearer but unaccountably sesquipedalian
> 
> >>> wanted = [foo[i] for i in 3, 7, 1, -1]
> >>> wanted = [foo[3], foo[7], foo[7], foo[-1]]
> 
>  Are these the most idiomatically pythonic forms?  Or am I missing
>  something better?

Firstly run "import this" at the python interactive interpreter to
remind youself of the philosophical differences between perl and
python.  I think the philosophy of python is the major reason why it
is such a good language.

As I transitioned from perl to python it took me a while to let go of
perlisms, and get used to writing a little bit more code (usually of
the order of a few characters only) but which was much much clearer.

Perl is full of cleverness which give you great pleasure to write as a
programmer.  However when you or someone else looks at that code later
they don't get that same pleasure - horror is more likely the
reaction!  Python just isn't like that.

I'd probably write

  wanted = foo[3], foo[7], foo[1], foo[-1]

(assuming you didn't mind having a tuple rather than a list)

or maybe this

  wanted = [ foo[i] for i in 3, 7, 1, -1 ]

However I can't think of the last time I wanted to do this - array
elements having individual purposes are usually a sign that you should
be using a different data structure.

-- 
Nick Craig-Wood  -- http://www.craig-wood.com/nick
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Perl's @foo[3,7,1,-1] ?

2009-06-13 Thread Brian Quinlan

kj wrote:


Switching from Perl here, and having a hard time letting go...

Suppose I have an "array" foo, and that I'm interested in the 4th, 8th,
second, and last element in that array.  In Perl I could write:

  my @wanted = @foo[3, 7, 1, -1];


Could you explain your use case? It could be that a list isn't the 
appropriate data structure.


Cheers,
Brian

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


Re: Perl's @foo[3,7,1,-1] ?

2009-06-13 Thread Arnaud Delobelle
kj  writes:

> Switching from Perl here, and having a hard time letting go...
>
> Suppose I have an "array" foo, and that I'm interested in the 4th, 8th,
> second, and last element in that array.  In Perl I could write:
>
>   my @wanted = @foo[3, 7, 1, -1];
>
> I was a bit surprised when I got this in Python:
>
 wanted = foo[3, 7, 1, -1]
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: list indices must be integers
>
> Granted, Perl's syntax is often obscure and hard-to-read, but in
> this particular case I find it quite transparent and unproblematic,
> and the fictional "pythonized" form above even more so.
>
> The best I've been able to come up with in Python are the somewhat
> Perl-like-in-its-obscurity:
>
 wanted = map(foo.__getitem__, (3, 7, 1, -1))
>
> or the clearer but unaccountably sesquipedalian
>
 wanted = [foo[i] for i in 3, 7, 1, -1]
 wanted = [foo[3], foo[7], foo[7], foo[-1]]
>
> Are these the most idiomatically pythonic forms?  Or am I missing
> something better?

You're missing operator.itemgetter:

>>> from operator import itemgetter
>>> foo = "spam & eggs"
>>> itemgetter(3, 7, 1, -1)(foo)
('m', 'e', 'p', 's')
>>> 

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


https post request

2009-06-13 Thread Travis Altman
i'm trying to make a https post request, i'm following the example linked
below.

http://www.java2s.com/Tutorial/Python/0420__Network/SendinganHTTPPOSTRequestfromaPythonScript.htm

i'm trying to make the https post request to netflix.com, below is my code

  1 import httplib
  2
  3 # def statement used to parse response from web server
  4 def printText(txt):
  5 lines = txt.split('\n')
  6 for lines in lines:
  7 print line.strip()
  8
  9 # connect to web server
 10 httpServ = httplib.HTTPSConnection("https://www.netflix.com";, 443)
 11 print httpServ
 12 httpServ.connect()
 13
 14 # specifying the exact login URL
 15
 16 request = "test"
 17
 18 httpServ.request('POST', \
 19 '/Login', \
 20 'nextpage=http%3A%2F%2Fwww.netflix.com
%2F&SubmitButton=Click+Here+to+Continue&movieid=&trkid=&email=email%
40gmail.com&password1=supersecret % request')
 21
 22 # response
 23
 24 response = httpServ.getresponse()
 25
 26 print response
 27 if response.status == httplib.OK:
 28 printText (response.read())
 29
 30 httpServ.close()

getting the following errors, any suggestions?

Traceback (most recent call last):
  File "printerPythonScript.py", line 12, in 
httpServ.connect()
  File "/usr/lib/python2.5/httplib.py", line 1130, in connect
sock.connect((self.host, self.port))
  File "", line 1, in connect
socket.gaierror: (-5, 'No address associated with hostname')
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Lexical scope: converting Perl to Python

2009-06-13 Thread John S
On Jun 13, 8:29 am, Nick Craig-Wood  wrote:
> Andrew Savige  wrote:
>
> >  I'd like to convert the following Perl code to Python:
>
> >   use strict;
> >   {
> >     my %private_hash = ( A=>42, B=>69 );
> >     sub public_fn {
> >   my $param = shift;
> >   return $private_hash{$param};
> >     }
> >   }
> >   print public_fn("A");    # good:  prints 42
> >   my $x = $private_hash{"A"};  # error: good, hash not in scope
>
> >  The real code is more complex; the above is a simplified example.
>
> >  Notice that this code uses Perl's lexical scope to hide the
> >  %private_hash variable, but not the public_fn() function.
>
> >  While I could convert this code to the following Python code:
>
> >   private_hash = dict( A=42, B=69 )
> >   def public_fn(param):
> >     return private_hash[param]
> >   print public_fn("A") # good:  prints 42
> >   x = private_hash["A"]    # works: oops, hash is in scope
>
> >  I'm not happy with that because I'd like to limit the scope of the
> >  private_hash variable so that it is known only inside public_fn.
>
> >  Of course, I could hide the hash like so:
>
> >   def public_fn(param):
> >     private_hash = dict( A=42, B=69 )
> >     return private_hash[param]
>
> >  yet I'm not happy with that either because of the repeated
> >  initialization the hash each time the function is called.
>
> >  What is the Pythonic equivalent of Perl's lexical scope, as
> >  illustrated by the code snippet above?
>
> Either
>
> _private_hash = dict( A=42, B=69)
>
> def public_fn(param):
>     return _private_hash[param]
>
> Or
>
> def public_fn(param, _private_hash = dict( A=42, B=69)):
>     return _private_hash[param]
>
> Is probably the pythonic equivalents.  Note that private_hash starts
> with an underscore which means it won't be exported from a module by
> default and it is a convention that it is private and shouldn't be
> fiddled with.  I'd probably go with the latter of the two examples.
>
> --
> Nick Craig-Wood  --http://www.craig-wood.com/nick

Another approach is to just use a class to hold the data, and a class
method (AKA classmethod) to access the data:

# class definition
class my_util(object):
_private_hash = { 'A':42,'B':69 }

@classmethod
def public_fn(cls,index):
return cls._private_hash[index]

# usage
print my_util.public_fn('A')
print my_util.public_fn('B')

This keeps pretty close to the OP's intention. It does require you to
use the class name, but that's.OK, and better than an anonymous
block IMHO.

Note: I'm a recovering Perl hacker.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about None

2009-06-13 Thread John Yeung
On Jun 13, 1:49 pm, Paul Rubin  wrote:
> John Yeung  writes:
> > Because you might want None to behave as though it were
> > nothing at all.
>
> Sure, you might also want strings to behave as if they
> were ints, but wishing doesn't make it so.

I'm not saying that the OP, as a programmer using Python, would wish
that Python's None behaves like nothing.  I'm saying that as a
philosophical question, which is how he phrased it, one might want
one's language (perhaps one is designing this language) to have
something that behaves like "nothing", and then call this "None".  I
don't think he's saying that the wrong choice was made for Python; I'm
certainly not saying that; it was just a musing.

> > But mathematically speaking, it's intuitive that
> > "nothing" would match any type.
>
> Completely wrong.  The concept you're thinking of in
> denotational semantics is called "bottom", but bottom
> is not a value that functions can compute and return.
> It is really the absence of a value.

I've never heard a mathematician use the term "bottom".  It certainly
could be that I just haven't talked to the right types of
mathematicians.  I'm not sure it's even relevant.  "Denotational
semantics" is specific to computer science.  My point was simply that
even an elementary understanding of mathematics (and I'll grant a
naive understanding of computer science as well) might lead someone to
think that it *might* make sense for None to be the name for nothing.

> > I find that it's somewhat like the confusion that often
> > occurs regarding the all() function.
>
> It's the other way though, all([])=True makes sense,
> isinstance(None,int) does not.

Look, even a lot of the people on this very list, who answer questions
as if they are experts, have been tripped up by all().  I am not here
to say they are stupid.  I also do not mean to present myself as an
expert, whether in math, computer science, or Python in particular.
I'm just trying to present the reasoning someone (however naive) might
have to even bring up the possibility that it might make sense for
isinstance(None, foo) to be True for any foo.

I probably would not have been prompted to respond in the first place
if Steven D'Aprano hadn't deemed the notion to require three
exclamation points to shoot down.  I think it is enough to say that
None is an object and it has a specific type, thus it shouldn't match
any other type; which is what he then said, albeit excitedly. ;)

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


Re: Good books in computer science?

2009-06-13 Thread Rhodri James
On Sat, 13 Jun 2009 18:37:13 +0100, koranthala   
wrote:





Code Complete and GOF are software engineering books but not really
CS books.


I understand and concur. Since I am a software engineer - coming in to
software from a different background - what I am looking for is self-
improvement books for a software engineer.


In that case The Mythical Man-Month (Brooks) is a must.

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


Re: Lexical scope: converting Perl to Python

2009-06-13 Thread Rhodri James
On Sat, 13 Jun 2009 06:02:53 +0100, Andrew Savige   
wrote:



What is the Pythonic equivalent of Perl's lexical scope, as
illustrated by the code snippet above?


The useful (to you) pythonic answer depends rather a lot on
why you want to do something like that.  While you've been
given several possibilities, the "correct" answer might well
be "don't do that at all" :-)

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


Re: Perl's @foo[3,7,1,-1] ?

2009-06-13 Thread kj
In  Jack Diederich 
 writes:

>There is only so much room in the syntax for common cases before you
>end up with ... perl (no offense intended, I'm a perl monk[1]).   The
>Python grammar isn't as context sensitive or irregular as the perl
>grammar so mylist[1,2,3]  so the "1,2,3" tuple is always interpreted
>as a tuple and the square brackets always expect an int or a slice.
>Not including special cases everywhere means there isn't a short way
>to handle special cases but it also means human readers have to
>remember fewer special cases.   Perl and Python make different
>tradeoffs in that respect.

OK, I see: if Python allowed foo[3,7,1,-1], then foo[3] would be
ambiguous: does it mean the fourth element of foo, or the tuple
consisting of this element alone?  I suppose that's good enough
reason to veto this idea...

Thanks for all the responses.

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


Re: Perl's @foo[3,7,1,-1] ?

2009-06-13 Thread kj
In  Nick Craig-Wood 
 writes:

>However I can't think of the last time I wanted to do this - array
>elements having individual purposes are usually a sign that you should
>be using a different data structure.

In the case I was working with, was a stand-in for the value returned
by some_match.groups().  The match comes from a standard regexp
defined elsewhere and that captures more groups than I need.  (This
regexp is applied to every line of a log file.)

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


Re: How to insert string in each match using RegEx iterator

2009-06-13 Thread John S
On Jun 10, 12:13 am, "504cr...@gmail.com" <504cr...@gmail.com> wrote:
> By what method would a string be inserted at each instance of a RegEx
> match?
>
> For example:
>
> string = '123 abc 456 def 789 ghi'
> newstring = ' INSERT 123 abc INSERT 456 def INSERT 789 ghi'
>
> Here's the code I started with:
>
> >>> rePatt = re.compile('\d+\s')
> >>> iterator = rePatt.finditer(string)
> >>> count = 0
> >>> for match in iterator:
>
>         if count < 1:
>                 print string[0:match.start()] + ' INSERT ' + 
> string[match.start
> ():match.end()]
>         elif count >= 1:
>                 print ' INSERT ' + string[match.start():match.end()]
>         count = count + 1
>
> My code returns an empty string.
>
> I'm new to Python, but I'm finding it really enjoyable (with the
> exception of this challenging puzzle).
>
> Thanks in advance.

I like using a *callback* function instead of *plain text* with the
re.sub() method. To do this, call the sub() function in the normal
way, but instead of specifying a string as the replacement, specify a
function. This function expects the same match object returned by
re.search() or re.match(). The text matched by your RE is replaced by
the return value of the function. This gives you a lot of flexibility;
you can use the matched text to look up values in files or databases,
or online, for instance, and you can do any sort of text manipulation
desired.

8<---
import re

# original string
oldstring = '123 abc 456 def 789 ghi'

# RE to match a sequence of 1 or more digits
rx_digits = re.compile(r"\d+")

# callback function -- expects a Match object, returns the replacement
string
def repl_func(m):
return 'INSERT ' + m.group(0)

# do the substitution
newstring =  rx_digits.sub(repl_func,oldstring)

print "OLD:",oldstring
print "NEW:",newstring
-
Output:
OLD: 123 abc 456 def 789 ghi
NEW: INSERT 123 abc INSERT 456 def INSERT 789 ghi


You could also do it with a lambda function if you didn't want to
write a separate function:
newstring =  rx_digits.sub(lambda m: 'INSERT ' + m.group(0),oldstring)

I understand that for this simple case, '
'INSERT ' + \1
is sufficient, and a callback is overkill; I wanted to show the OP a
more generic approach to complex substitutions.

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


Re: Question about None

2009-06-13 Thread Rhodri James
On Sat, 13 Jun 2009 21:25:28 +0100, John Yeung  
 wrote:



> But mathematically speaking, it's intuitive that
> "nothing" would match any type.

Completely wrong.  The concept you're thinking of in
denotational semantics is called "bottom", but bottom
is not a value that functions can compute and return.
 It is really the absence of a value.


I've never heard a mathematician use the term "bottom".  It certainly
could be that I just haven't talked to the right types of
mathematicians.  I'm not sure it's even relevant.  "Denotational
semantics" is specific to computer science.  My point was simply that
even an elementary understanding of mathematics (and I'll grant a
naive understanding of computer science as well) might lead someone to
think that it *might* make sense for None to be the name for nothing.


Such an understanding would be clearly wrong in the context in which we
were talking (and denotational semantics is a branch of category theory,
which is not specific to computer science if you don't mind).  If None
is nothing, then it can't be a string, int, float or anything else,
because they're all something.

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


Generating a unique filename in the face of unicode filename

2009-06-13 Thread skip
In my lockfile module I generate a unique name like so:

self.path = path
self.lock_file = os.path.abspath(path) + ".lock"
self.hostname = socket.gethostname()
self.pid = os.getpid()
if threaded:
name = threading.current_thread().get_name()
tname = "%s-" % quote(name, safe="")
else:
tname = ""
dirname = os.path.dirname(self.lock_file)
self.unique_name = os.path.join(dirname,
"%s.%s%s" % (self.hostname,
 tname,
 self.pid))

where path is the file which is to be locked.  Frank Niessink uses lockfile
in his Task Coach application and reported a problem to me one of his
Windows users encountered:

...
File "taskcoachlib\persistence\taskfile.pyo", line 266, in acquire_lock
File "taskcoachlib\thirdparty\lockfile\lockfile.pyo", line 537, in
FileLock
File "taskcoachlib\thirdparty\lockfile\lockfile.pyo", line 296, in
__init__
File "taskcoachlib\thirdparty\lockfile\lockfile.pyo", line 175, in
__init__
File "ntpath.pyo", line 102, in join
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 3: 
ordinal not in range(128)

where line 175 is the assignment to self.unique_name.  After a little
back-and-forth with his user it turns out that her computer's hostname
contains non-ASCII data, so presumably self.hostname is a unicode object.

I tried to replicate this on my Mac but can't:

>>> dirname = "/tmp"
>>> h = u"\xef"
>>> tname = threading.currentThread().getName()
>>> os.path.join(dirname, "%s.%s.%s" % (h, tname, os.getpid()))
u'/tmp/\xef.MainThread.11004'

It works for Frank on his Windows box as well.  Any ideas how to properly
Unicode-proof this code?

Thanks,

-- 
Skip Montanaro - s...@pobox.com - http://www.smontanaro.net/
when i wake up with a heart rate below 40, i head right for the espresso
machine. -- chaos @ forums.usms.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Perl's @foo[3,7,1,-1] ?

2009-06-13 Thread John Yeung
On Jun 13, 3:59 pm, Arnaud Delobelle  wrote:
> kj  writes:
> > Are these the most idiomatically pythonic forms?  Or am
> > I missing something better?
>
> You're missing operator.itemgetter:
>
> >>> from operator import itemgetter
> >>> foo = "spam & eggs"
> >>> itemgetter(3, 7, 1, -1)(foo)
>
> ('m', 'e', 'p', 's')

That looks to me like the best solution to the OP's specific
question.  It's amazing how many cool things are tucked into the
standard library.  While it's easy to miss these things, I appreciate
the effort to keep Python's core relatively small.  (Not knowing about
itemgetter, I would have gone with the list comprehension.)

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


frustrating failure of 'break' statement ( novice )

2009-06-13 Thread pdlemper
In my programs the 'break' fails to work.  I've studied the docs for
3.0 and Programming in Python, neither of which are illuminating.
Simplest example :

while True :
num = int(input())
print(num)
if num == 0 :
break

print('done')


SyntaxError : invalid syntax( pointing to end of break )
This happens whether the 'break' is put on same line as the
conditional, flush with while, flush with if, or appropriately
indented 4 spaces.  
 " I saw the number 4 in gold " -Guido
  with apologies to Wm Carlos Williams
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about None

2009-06-13 Thread John Yeung
On Jun 13, 5:22 pm, "Rhodri James" 
wrote:
> Such an understanding would be clearly wrong in the context
> in which we were talking (and denotational semantics is a
> branch of category theory, which is not specific to computer
> science if you don't mind).  If None is nothing, then it can't
> be a string, int, float or anything else, because they're all
> something.

I appreciate your explanation, and your politeness.

And I accept your answer, as well as Steven's and Paul's for that
matter.  I still think it is understandable (and people may choose to
understand in a condescending way, if they wish) that someone might
not get the difference between what you are saying and the statement
that all elements of the empty set are floats.  I mean, what's in the
empty set?  Nothing.  But you've said that floats are something.  How
is it that nothing is something?

Please, I do not wish to extend this thread.  My last question was
rhetorical.  It's not a challenge.  I accept your answer, as well as
the others.  To be clear, I find it very intuitive that None does not
match the other types.  But just as the devil seems to have plenty of
advocates, I guess I am a naive person's advocate, of sorts.  (I must
stress I'm not the OP's advocate, nor do I consider him naive.)

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


Re: Perl's @foo[3,7,1,-1] ?

2009-06-13 Thread Brian Quinlan

kj wrote:

In  Nick Craig-Wood 
 writes:


However I can't think of the last time I wanted to do this - array
elements having individual purposes are usually a sign that you should
be using a different data structure.


In the case I was working with, was a stand-in for the value returned
by some_match.groups().  The match comes from a standard regexp
defined elsewhere and that captures more groups than I need.  (This
regexp is applied to every line of a log file.)

kj


The common idiom for this sort of thing is:

_, _, _, val1, _, _, _, val2, ..., val3 = some_match.groups()

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


Re: frustrating failure of 'break' statement ( novice )

2009-06-13 Thread Mark Dickinson
On Jun 13, 10:48 pm, pdlem...@earthlink.net wrote:
> In my programs the 'break' fails to work.  I've studied the docs for
> 3.0 and Programming in Python, neither of which are illuminating.
> Simplest example :
>
> while True :
>     num = int(input())
>     print(num)
>     if num == 0 :
>         break
>
> print('done')
>
> SyntaxError : invalid syntax    ( pointing to end of break )
> [...]

Are you mixing tabs and spaces in your code at all?  Check that the
'break' line is indented with 8 spaces rather than a tab character.

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


Re: Perl's @foo[3,7,1,-1] ?

2009-06-13 Thread MRAB

Brian Quinlan wrote:

kj wrote:
In  Nick Craig-Wood 
 writes:



However I can't think of the last time I wanted to do this - array
elements having individual purposes are usually a sign that you should
be using a different data structure.


In the case I was working with, was a stand-in for the value returned
by some_match.groups().  The match comes from a standard regexp
defined elsewhere and that captures more groups than I need.  (This
regexp is applied to every line of a log file.)

kj


The common idiom for this sort of thing is:

_, _, _, val1, _, _, _, val2, ..., val3 = some_match.groups()


Alternatively:

val1, val2, val3 = some_match.group(4, 8, something)
--
http://mail.python.org/mailman/listinfo/python-list


Re: frustrating failure of 'break' statement ( novice )

2009-06-13 Thread Rhodri James

On Sat, 13 Jun 2009 22:48:43 +0100,  wrote:


In my programs the 'break' fails to work.  I've studied the docs for
3.0 and Programming in Python, neither of which are illuminating.
Simplest example :

while True :
num = int(input())
print(num)
if num == 0 :
break

print('done')


SyntaxError : invalid syntax( pointing to end of break )
This happens whether the 'break' is put on same line as the
conditional, flush with while, flush with if, or appropriately
indented 4 spaces.


It works for me, running it from file.  It fails with "invalid
syntax" (pointing to the end of *print*) if I cut and paste
the program exactly into a Python shell, presumably because the
shell gets unhappy about having more command input when it
wants to be executing the while loop.  Does this match what
you see?

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


Re: Perl's @foo[3,7,1,-1] ?

2009-06-13 Thread Brian Quinlan

MRAB wrote:

Brian Quinlan wrote:

kj wrote:
In  Nick Craig-Wood 
 writes:



However I can't think of the last time I wanted to do this - array
elements having individual purposes are usually a sign that you should
be using a different data structure.


In the case I was working with, was a stand-in for the value returned
by some_match.groups().  The match comes from a standard regexp
defined elsewhere and that captures more groups than I need.  (This
regexp is applied to every line of a log file.)

kj


The common idiom for this sort of thing is:

_, _, _, val1, _, _, _, val2, ..., val3 = some_match.groups()


Alternatively:

val1, val2, val3 = some_match.group(4, 8, something)


Actually, now that I think about it, naming the groups seems like it 
would make this code a lot less brittle.


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


Re: Question about None

2009-06-13 Thread Piet van Oostrum
> Paul Rubin  (PR) wrote:

>PR> Paul Rubin  writes:
>>> crash, or something like that.  None is a value which in Haskell has a

>PR> I got ahead of myself, of course I meant Python (the *next* sentence
>PR> was going to mention Haskell).  The corresponding concept in Haskell
>PR> is called Nothing, which lives in a type functor called Maybe.  I
>PR> don't have time to go into it right now but there is a notion among
>PR> language weenies these days that the presence of something like None
>PR> (in Python) or Null (in Java, SQL, etc) is a wart in a language and
>PR> that it's preferable to have something like Maybe.

The reason is that in Haskell the type system doesn't have union types.
Instead it has the disjoint sum, and Maybe is just one of them.
Otherwise you could have used the union of Nothing and any other type.
In Python there is no reason to use a disjoint sum as Python doesn't use
static typing. You can use unions of types at your hearts desire. So I
don't think there is not much difference between None and Nothing,
except for the type system.

Java, on the other hand does have static typing but does not have
disjoint sums. So they have not much choice but to put null in every
class-based type.
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Perl's @foo[3,7,1,-1] ?

2009-06-13 Thread kj
In  kj  writes:

>OK, I see: if Python allowed foo[3,7,1,-1], then foo[3] would be
>ambiguous: does it mean the fourth element of foo, or the tuple
>consisting of this element alone?  I suppose that's good enough
>reason to veto this idea...


Hmmm, come to think of it, this argument is weaker than I thought
at first.  Python already has cases where it has to deal with this
sort of ambiguity, and does so with a trailing comma.  So the two
cases could be disambiguated: foo[3] for the single element, and
foo[3,] for the one-element tuple.

Also, the association of this idiom with Perl is a bit unfair:
tuple-index is very common in other languages, and in pure math as
well.

As I said in my original post, Perl code often has very obscure
expressions, but I would never describe tuple indexing as one of
them.

By the same token, the desing of Python does not entirely disregard
considerations of ease of writing.  Who could argue that

  foo[:]

is intrinsically clearer, or easier to read than

  foo[0:len(foo)]

?

Please don't misunderstand me here.  I don't want to critize, let
alone change, Python.  I'm sure there is a good reason for why
Python doesn't support foo[3,7,1,-1], but I have not figured it
out yet.  I still find it unconvincing that it would be for the
sake of keeping the code easy to read, because I don't see how
foo[3,7,1,-1] is any more confusing than foo[:].

kj

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


Re: Perl's @foo[3,7,1,-1] ?

2009-06-13 Thread Piet van Oostrum
> kj  (k) wrote:

>k> Switching from Perl here, and having a hard time letting go...

>k> Suppose I have an "array" foo, and that I'm interested in the 4th, 8th,
>k> second, and last element in that array.  In Perl I could write:

>k>   my @wanted = @foo[3, 7, 1, -1];

>k> I was a bit surprised when I got this in Python:

> wanted = foo[3, 7, 1, -1]
>k> Traceback (most recent call last):
>k>   File "", line 1, in 
>k> TypeError: list indices must be integers

>k> Granted, Perl's syntax is often obscure and hard-to-read, but in
>k> this particular case I find it quite transparent and unproblematic,
>k> and the fictional "pythonized" form above even more so.

>k> The best I've been able to come up with in Python are the somewhat
>k> Perl-like-in-its-obscurity:

> wanted = map(foo.__getitem__, (3, 7, 1, -1))

>k> or the clearer but unaccountably sesquipedalian

> wanted = [foo[i] for i in 3, 7, 1, -1]
> wanted = [foo[3], foo[7], foo[7], foo[-1]]

>k> Are these the most idiomatically pythonic forms?  Or am I missing
>k> something better?

Do it yourself:

class MyList(list):
def __getitem__(self, indx):
if isinstance (indx, tuple):
return [self[i] for i in indx]
else:
return list.__getitem__(self, indx)

l = MyList((range(10)))

print l[3, 7, 1, -1]
print l[3]
print l[3:7]

# and now for something completely different

print l[3, (7, 1), -1]

duck :=)
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Distributed computing & sending the interpreter

2009-06-13 Thread Jeremy Cowles
Hi all,
I'm working on a distributed computing project (PyMW and BOINC) where we are
sending Python scripts to client machines. Currently, we make two very
unlikely assumptions: that Python 2.5 is installed and that the interpreter
is available on the PATH.

We are looking at our options to remove this assumption and the two most
likely are redistributing the entire Python installation (for each supported
platform) and embedding the interpreter in a custom executable and sending
select parts of the standard library with it (to reduce size).

It seems like we would have to pre-compile for each different platform,
which is a pain and sending the entire installation could be tricky.

I am looking for alternatives, comments and suggestions. Any help would be
greatly appreciated.

Also, please let me know if there is a better list to post this question to;
would the python-dev list be appropriate?

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


Re: Exceptions and Object Destruction (was: Problem with apsw and garbage collection)

2009-06-13 Thread Aahz
In article <873aa5m6ae@vostro.rath.org>,
Nikolaus Rath   wrote:
>
>I think I managed to narrow down the problem a bit. It seems that when
>a function returns normally, its local variables are immediately
>destroyed. However, if the function is left due to an exception, the
>local variables remain alive:

Correct.  You need to get rid of the stack trace somehow; the simplest
way is to wrap things in layers of functions (i.e. return from the
function with try/except and *don't* save the traceback).  Note that if
your goal is to ensure finalization rather than recovering memory, you
need to do that explicitly rather than relying on garbage collection.
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"Many customs in this life persist because they ease friction and promote
productivity as a result of universal agreement, and whether they are
precisely the optimal choices is much less important." --Henry Spencer
-- 
http://mail.python.org/mailman/listinfo/python-list


Persistent failure of 'break' statement ( novice )

2009-06-13 Thread pdlemper
Thanks Mark & Rhodri.  Neither works.

I'm using Notepad2 as editor.  Don't use the tab. Each space
counted with spacebar & recounted.  Rewrote the block from
scratch as new script and eliminated the print line in the loop.
Now no error message, but it will go on forever despite repeatedly
entering 0.  Have to get out with ctrl-c . 

while True :
num = int(input())
if num == 0 :
break

print('done')

   Frusting: I'm missing something fundamental. Dave WB3DWE
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: preferring [] or () in list of error codes?

2009-06-13 Thread Ben Finney
Mel  writes:

> The immutability makes it easier to talk about the semantic meanings.
> After you do
> > event_timestamp = (2009, 06, 04, 05, 02, 03)
> there's nothing that can happen to the tuple to invalidate
> > (year, month, day, hour, minute, second) = event_timestamp
> even though, as you say, there's nothing in the tuple to inform anybody 
> about the year, month, day, ... interpretation.

Also note that the stdlib ‘collections.namedtuple’ implementation
http://docs.python.org/library/collections.html#collections.namedtuple>
essentially acknowledges this: the names are assigned in advance to
index positions, tying a specific semantic meaning to each position.

-- 
 \   “Prediction is very difficult, especially of the future.” |
  `\   —Niels Bohr |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Persistent failure of 'break' statement ( novice )

2009-06-13 Thread Xavier Ho
I don't see any error in that code except the fact HTML ate one space from
the code you pasted. It worked fine and perfectly as expected, even with
print(num) present.

I think you have either a run-time environment problem, or an editor
encoding problem. Try a different editor, and/or different ways of launching
the program. And share with us what you did to run this to begin with, that
might help as well.

Best regards,

Ching-Yun "Xavier" Ho, Technical Artist

Contact Information
Mobile: (+61) 04 3335 4748
Skype ID: SpaXe85
Email: cont...@xavierho.com
Website: http://xavierho.com/


On Sun, Jun 14, 2009 at 10:20 AM,  wrote:

> Thanks Mark & Rhodri.  Neither works.
>
> I'm using Notepad2 as editor.  Don't use the tab. Each space
> counted with spacebar & recounted.  Rewrote the block from
> scratch as new script and eliminated the print line in the loop.
> Now no error message, but it will go on forever despite repeatedly
> entering 0.  Have to get out with ctrl-c .
>
> while True :
>num = int(input())
>if num == 0 :
>break
>
> print('done')
>
>   Frusting: I'm missing something fundamental. Dave WB3DWE
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Persistent failure of 'break' statement ( novice )

2009-06-13 Thread John Machin
On Jun 14, 10:20 am, pdlem...@earthlink.net wrote:

> Now no error message, but it will go on forever despite repeatedly
> entering 0.  Have to get out with ctrl-c .    

>            Frusting: I'm missing something fundamental. Dave WB3DWE

After you hit the zero key, do you hit the Enter key?

If the answer is yes, then our crystal balls have proved useless.
You'll have to supply some meaningful detail. If you are pasting your
script into a Python interactive prompt, stop doing that, save your
script as a file, and run it from the command line.

Here's a script for you to use. It has some diagnostic print() calls
in it so that we can see what is going wrong.

8<- cut here
import sys
print(sys.version)
assert sys.version.startswith('3.')
while True :
text = input('Enter a number -> ')
print('You entered', ascii(text))
if text != "0":
print('That is NOT the digit zero!')
num = int(text)
print('Value:', num)
if num == 0:
break
print('done')
8<- cut here

And when you run it, copy the outout and paste it into your return
message/posting, like I've done below:

[I called my copy break_fail.py]

C:\junk>\python30\python break_fail.py
3.0.1 (r301:69561, Feb 13 2009, 20:04:18) [MSC v.1500 32 bit (Intel)]
Enter a number -> 9
You entered '9'
That is NOT the digit zero!
Value: 9
Enter a number -> 0
You entered '0'
Value: 0
done

C:\junk>

Hoping this helps,
John
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Persistent failure of 'break' statement ( novice )

2009-06-13 Thread pdlemper
On Sat, 13 Jun 2009 19:03:38 -0700 (PDT), John Machin
 wrote:

>On Jun 14, 10:20 am, pdlem...@earthlink.net wrote:
>
>> Now no error message, but it will go on forever despite repeatedly
>> entering 0.  Have to get out with ctrl-c .    
>
>>            Frusting: I'm missing something fundamental. Dave WB3DWE
>
>After you hit the zero key, do you hit the Enter key?
>
>If the answer is yes, then our crystal balls have proved useless.
>You'll have to supply some meaningful detail. If you are pasting your
>script into a Python interactive prompt, stop doing that, save your
>script as a file, and run it from the command line.
>
>Here's a script for you to use. It has some diagnostic print() calls
>in it so that we can see what is going wrong.
>
>8<- cut here
>import sys
>print(sys.version)
>assert sys.version.startswith('3.')
>while True :
>text = input('Enter a number -> ')
>print('You entered', ascii(text))
>if text != "0":
>print('That is NOT the digit zero!')
>num = int(text)
>print('Value:', num)
>if num == 0:
>break
>print('done')
>8<- cut here
>
>And when you run it, copy the outout and paste it into your return
>message/posting, like I've done below:
>
>[I called my copy break_fail.py]
>
>C:\junk>\python30\python break_fail.py
>3.0.1 (r301:69561, Feb 13 2009, 20:04:18) [MSC v.1500 32 bit (Intel)]
>Enter a number -> 9
>You entered '9'
>That is NOT the digit zero!
>Value: 9
>Enter a number -> 0
>You entered '0'
>Value: 0
>done
>
>C:\junk>
>
>Hoping this helps,
>John


John : Hitting Enter solved it.  Thanks so much.  I was treating
input() as getch() or getche() .   Dave WB3DWE
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about None

2009-06-13 Thread John O'Hagan
On Sat, 13 Jun 2009, John Yeung wrote:
> On Jun 13, 5:22 pm, "Rhodri James" 
>
> wrote:
> > Such an understanding would be clearly wrong in the context
> > in which we were talking (and denotational semantics is a
> > branch of category theory, which is not specific to computer
> > science if you don't mind).  If None is nothing, then it can't
> > be a string, int, float or anything else, because they're all
> > something.
>
> I appreciate your explanation, and your politeness.
>
> And I accept your answer, as well as Steven's and Paul's for that
> matter.  I still think it is understandable (and people may choose to
> understand in a condescending way, if they wish) that someone might
> not get the difference between what you are saying and the statement
> that all elements of the empty set are floats.  I mean, what's in the
> empty set?  Nothing.  But you've said that floats are something.  How
> is it that nothing is something?
[...]

Also accepting that Python's implementation of None and all() are well-defined 
and practical, I would add that philosophically these matters of emptiness and 
nothingness are far from concluded. Bertrand Russell, for one, would have 
disputed the behaviour of all([]), although he may have appreciated its 
usefulness.

Regards,

John


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


Off-topic: Usenet archiving history (was Re: FW: [Tutor] Multi-Threading and KeyboardInterrupt)

2009-06-13 Thread Aahz
In article ,
Dennis Lee Bieber   wrote:
>
>   As for the use of X-Noarchive headers... Back before Google, most
>NNTP servers had automatic expiration of messages (on my ISP at the
>time, the binary groups expired after 24 hours!, most messages expired
>after one or two weeks). I never used X-Noarchive -- in truth, I didn't
>even know about it.
>
>   THEN... Google started doing Google Groups with no automatic
>expiration. That is when the subject of X-Noarchive headers came up in
>some of the groups I'd been following. I and a fair number of others
>immediately took advantage of it.

Actually, it's not really fair to blame Google for this, they were only
following the design of DejaNews, which they purchased in 2001.  Google
did create some additional value by finding old Usenet archives that
predated DejaNews.  (Google Groups, which I normally refer to as Gooja in
deference to the history, debuted the archive on Dec 10, 2001.)
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"Many customs in this life persist because they ease friction and promote
productivity as a result of universal agreement, and whether they are
precisely the optimal choices is much less important." --Henry Spencer
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Good books in computer science?

2009-06-13 Thread koranthala
On Jun 14, 1:52 am, "Rhodri James" 
wrote:
> On Sat, 13 Jun 2009 18:37:13 +0100, koranthala   
> wrote:
>
>
>
> >> Code Complete and GOF are software engineering books but not really
> >> CS books.
>
> > I understand and concur. Since I am a software engineer - coming in to
> > software from a different background - what I am looking for is self-
> > improvement books for a software engineer.
>
> In that case The Mythical Man-Month (Brooks) is a must.
>
> --
> Rhodri James *-* Wildebeest Herder to the Masses

Thank you Rhodri.
I do have Mythical Man-Month - a great book indeed.
I was looking for more technical books -
I have now got a good set - Putting it across so that others can also
use maybe -

Code Complete,
GOF,
Mythical Man-Month,
SICP - Thank you Paul - I have downloaded it from Web Site,
Introduction to algorithm - I have placed an order for the same,
The Pragmatic Programmer - Planning to buy,
Refactoring: Improving the Design of Existing Code - again planning to
buy,
The C Programming Language - I had this, lost it, now I will buy
again,
The Little Schemer - I am not sure about buying this - I dont know
scheme
Software Tools - Seems to be a classic - not sure whether I will buy.

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


Re: Exceptions and Object Destruction (was: Problem with apsw and garbage collection)

2009-06-13 Thread Mike Kazantsev
On Fri, 12 Jun 2009 18:33:13 -0400
Nikolaus Rath  wrote:

> Nikolaus Rath  writes:
> > Hi,
> >
> > Please consider this example:
> []
> 
> I think I managed to narrow down the problem a bit. It seems that when
> a function returns normally, its local variables are immediately
> destroyed. However, if the function is left due to an exception, the
> local variables remain alive:
>
...
> 
> Is there a way to have the obj variable (that is created in dostuff())
> destroyed earlier than at the end of the program? As you can see, I
> already tried to explicitly call the garbage collector, but this does
> not help.

Strange thing is that no one suggested contextlib, which made _exactly_
for this purpose:


  #!/usr/bin/env python
  import gc

  class testclass(object):
def __init__(self):
  self.alive = True # just for example
  print "Initializing"

def __del__(self):
  if self.alive:
# try..except wrapper would suffice here,
# so destruction won't raise ex, if already done
print "Destructing"
self.alive = False

def __enter__(self): pass
def __exit__(self, ex_type, ex_val, ex_trace):
  self.__del__()
  if not ex_type is None:
raise RuntimeError(ex_val)

  
  def dostuff(fail):
with testclass() as obj:
  # some stuff
  if fail:
raise TypeError
  # some more stuff
print "success"

  
  print "Calling dostuff"
  dostuff(fail=False)
  print "dostuff returned"

  try:
print "Calling dostuff"
dostuff(fail=True)
  except TypeError:
pass

  gc.collect()
  print "dostuff returned" 


And it doesn't matter where you use "with", it creates a volatile
context, which destructs before anything else happens on higher level.

Another simplified case, similar to yours is file objects:


  with open(tmp_path, 'w') as file:
# write_ops
  os.rename(tmp_path, path)

So whatever happens inside "with", file should end up closed, else
os.rename might replace valid path with zero-length file.

It should be easy to use cursor with contextlib, consider using
contextmanager decorator:


  from contextlib import contextmanager

  @contextmanager
  def get_cursor():
try:
  cursor = conn.cursor()
  yield cursor
except Exception as ex: raise ex
finally: cursor.close()

  with get_cursor() as cursor:
# whatever ;)



-- 
Mike Kazantsev // fraggod.net


signature.asc
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Good books in computer science?

2009-06-13 Thread rustom
On Jun 14, 10:38 am, koranthala  wrote:
> Software Tools - Seems to be a classic - not sure whether I will buy.
In that vein but more modern -- Art of Unix Programming by Eric
Raymond (available online)
Some of my old favorites:
Intro to functional programming by Bird and Wadler
TAOCP slightly more modernized more heady and less programmer oriented
-- Concrete Mathematics by Knuth and...
Science of Programming by David Gries; again more modernized to
Logical Approach to Discrete Mathematics
Bentley's Wriiting Efficient Programs and Programming pearls

Dijkstra's writings -- 
http://www.cs.utexas.edu/~EWD/transcriptions/EWD10xx/EWD1036.html
--
are not kind to software engineers [His defn of SE -- How to program
if you cannot].

Seemingly irrelevant -- Good programmers are very good with their
editors -- someone mentioned yegge.  Read him for inspiration on
emacs.  Of course you can use something else but its important to get
good at it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Persistent failure of 'break' statement ( novice )

2009-06-13 Thread John Machin
On Jun 14, 12:27 pm, pdlem...@earthlink.net wrote:
> On Sat, 13 Jun 2009 19:03:38 -0700 (PDT), John Machin
>
>
>
>  wrote:
> >On Jun 14, 10:20 am, pdlem...@earthlink.net wrote:
>
> >> Now no error message, but it will go on forever despite repeatedly
> >> entering 0.  Have to get out with ctrl-c .    
>
> >>            Frusting: I'm missing something fundamental. Dave WB3DWE
>
> >After you hit the zero key, do you hit the Enter key?
>
[snip]
>
> John : Hitting Enter solved it.  Thanks so much.  I was treating
> input() as getch() or getche() .               Dave WB3DWE


Here's a tip: when all else fails, read the manual. In this case:

http://docs.python.org/3.0/library/functions.html#input

"""The function then reads a line from input, converts it to a string
(stripping a trailing newline)"""

If you are desperate for getch() and getche(), see here:

http://docs.python.org/3.0/library/msvcrt.html?highlight=msvcrt#console-i-o

HTH,
John



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


Re: Perl's @foo[3,7,1,-1] ?

2009-06-13 Thread Vito De Tullio
Jack Diederich wrote:

> the square brackets always expect an int or a slice.

true only for lists :)

In [1]: mydict = {}

In [2]: mydict[1,2,3] = 'hi'

In [3]: print mydict[1,2,3]
--> print(mydict[1,2,3])
hi


-- 
By ZeD

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