Re: Problem using pbzip2 with bz2.BZ2File().read()

2010-06-21 Thread Chris Rebert
On Mon, Jun 21, 2010 at 5:32 PM, Ameet Nanda  wrote:
> When I compress a file with bzip2 from command line and read it with
> uncomp_data = bz2.BZ2File("fname").read() , it reads the whole file into
> uncomp_data.
>
> However when I compress the file with pbzip2 from command line and read it
> in a similar way it just reads the block size of data used for compression.

Read this recent nearly identical thread:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/be7d22a97b219a05

The `bz2` std lib module doesn't support multiple streams and thus
doesn't support files produced by pbzip2.
It's a known bug: http://bugs.python.org/issue1625

Cheers,
Chris
--
What's with everyone using pbzip2 all of a sudden??
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is the difference between 'type' and 'class'?

2010-06-21 Thread Steven D'Aprano
On Mon, 21 Jun 2010 15:43:01 -0700, Stephen Hansen wrote:

> many types are fundamentally immutable(i.e., ints, strings), and its
> awful hard to make an immutable class.

It's really simple if you can inherit from an existing immutable class.

class K(tuple):
pass


Of course, that lets you add attributes to K instances, which might not 
be what you want. So, in full knowledge that many Python programmers will 
laugh and point at you, you can (ab)use __slots__ to remove that ability:


>>> class K(tuple):
... __slots__ = []
...
>>> t = K((1,2,3))
>>> t[0] = 0
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'K' object does not support item assignment
>>> t.x = 1
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'K' object has no attribute 'x'



The tricky part is making an immutable class from scratch, i.e. 
inheriting from object. For a fairly large, complex example, you can see 
how the Decimal class does it -- basically uses read-only properties for 
public attributes, and hope that nobody modifies the private attributes. 
(If they do, they deserve whatever pain they get.)

Or you can do something like this:

http://northernplanets.blogspot.com/2007/01/immutable-instances-in-python.html


There's probably no way to make pure Python classes completely immutable 
without inheriting from an already immutable class. Pity.


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


Re: Is this make sence? Dynamic assembler for python

2010-06-21 Thread alex23
DivX  wrote:
> Another thing is that when you have assembler now you can write some
> small C compiler so that you don’t have to write assembly language.

That has to be the most paradoxical argument I've ever heard: "when
you use assembler you have the ability to not use assembler" :)

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


Re: What is the difference between 'type' and 'class'?

2010-06-21 Thread Terry Reedy

On 6/21/2010 6:11 PM, Peng Yu wrote:

pydoc xrange says:

Help on class xrange in module __builtin__:

class xrange(object)

python_2.6.5_library.pdf says:

Objects of type xrange are similar to buffers

Are type and class synonyms? It seems that they are at least according
to some webpages that I read. But I'm not completely sure. Could you
let me know in case my impress is wrong?


Additional answer:
'type' is the name of the built-in (meta)class that reveals the class of 
existing objects (one argument) and creates new classes (three arguments 
of the proper class/type ;-).



--
Terry Jan Reedy

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


[ANN] AutoRecalcDict 0.1.1 released

2010-06-21 Thread Steven W. Orr
AutoRecalcDict is a subclass of dict that allows programmers to create user
defined dependencies and functions on target keys.

You can find it at

http://pypi.python.org/pypi/AutoRecalcDict/0.1.1

I recently was designing tests for radio frequency analysis (about which, I know
nothing). All of the tests had a dict of parameterizations that was peculiar to
that test, and they all had a key called 'freqs' whose value was a simple list
of just a few frequencies. But then I ran into a test that had a couple hundred
frequencies. What I really needed was to have four keys called 'freqs', 'start',
'stop', and 'step', so that freqs would be recalculated to be the return value
of the range function, if ever start, stop or step ever changed.

This is my first time contribution to the python community, so I suspect that I
might have made a few misteaks in packaging. If you see things that should be
altered, please let me know. And since I know what it does, suggestions for
better docs that speak to you (instead of me) are very welcome.

What I got out of this personally was a good learning experience in a number of
aspects that I had not previously nailed down.

Thanks for checking it out.

-- 
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: setup server from scratch (with or without apache?)

2010-06-21 Thread News123
Hi Kruptein,

Kruptein wrote:
> I think that apache and mod_python are good enough, but I'm not an
> expert.
> 
> but I think that the security aspect for a large part depends on how
> secure your code is.
> 
> You can have a very secure server setting, but somewhere a bug in your
> code that makes it insecure.

Agreed. There's a lot of potential to make stupid things in one's own code.

In my case however I'd like to reject access to anybody not having a
client certificate.

Though users should be identified by their certificcates it would (in my
current case) not be a major disaster if one user would gain control
over another user's data.

The group of users is limited and all users are trusted during the live
time of their certificate.

Most important for me is, that my python script is only called when the
certificate is valid.







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


Re: float from numbers in text file

2010-06-21 Thread davidgp
On Jun 21, 5:13 pm, Stephen Hansen  wrote:
> On 6/21/10 4:26 PM, davidgp wrote:
>
>
>
>
>
> > ah, i see :P
> > float("45.34") or whatever does work fine, but the problem is that i'm
> > reading it from a text file. so somehow it is not a real string or
> > whatever..
> > here's a part of the code:
> > f = open ('/home/david/out.txt', 'r')
>
> > for line in f:
> > if tel ==6:
> >                    buf = line.replace('\n', '')
> >                    lat = float(buf)
> >            if tel ==7:
> >                    buf = line.replace('\n', '')
> >                    lng = float(buf)
>
> > basically it goes wrong in the last part where i try to convert the
> > line to a float..
> > i'm 100% sure that it's getting a number, not a text string
>
> First: you don't have to strip \n's. Try float("45.43\n")
>
> Second: this code, as is, can't run to show us the problem because
> there's this "tel" business and I don't know what it is. But I'll tweak
> it a bit and:
>
>   f = open ('/Users/ixokai/out.txt', 'r')
>
>   tel = 6
>   for line in f:
>      if tel == 6:
>         buf = line.replace('\n', '')
>         lat = float(buf)
>         print lat
>      if tel == 7:
>         buf = line.replace('\n', '')
>         lng = float(buf)
>
> And it works fine with a dummy data file I have. So... the problem is
> somewhere else. Its either in your data file, or some other logic going
> on (maybe something related to this 'tel' thing, I don't know).
>
> Are you sure there's no lines in out.txt which have, say, some text (on
> the left end)? Or maybe some blank lines?
>
> Maybe you should send the whole file and the data.txt if you don't see
> where the problem is.
>
> --
>
>    Stephen Hansen
>    ... Also: Ixokai
>    ... Mail: me+list/python (AT) ixokai (DOT) io
>    ... Blog:http://meh.ixokai.io/
>
>  signature.asc
> < 1KViewDownload

i found the error! apparently something was going wrong in the loop..
thanks for the help!
-- 
http://mail.python.org/mailman/listinfo/python-list


Problem using pbzip2 with bz2.BZ2File().read()

2010-06-21 Thread Ameet Nanda
When I compress a file with bzip2 from command line and read it with
*uncomp_data = bz2.BZ2File("fname").read()* , it reads the whole file into
uncomp_data.

However when I compress the file with pbzip2 from command line and read it
in a similar way* it just reads the block size of data* used for
compression.

So if I compress with:

*pbzip2 temp.temp -b3

*the read returns 30 bytes of data*.

*I tried with various block sizes to confirm. Has it got something to do
with the block separator fields ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: setup server from scratch (with or without apache?)

2010-06-21 Thread News123
News123 wrote:
> Hi,
>
>
> So far I never really had to ask this question and this is also, why I
> am stil a little shaky on this topic:
>
> So far the typical LAMP server existed already and contained already a
> lot of existing PHP web applications, which I couldn't remove.
> Therefore I just used mod_python to implement some minor functionality
> next to it.
>
> Now I have the opportunity to setup a server from scratch.
> 90% of the content will be non visual content over https with client AND
> server certificates.
> Access privileges will depend on the client certificate.

Thanks a lot for all your answers.


So it seems it's rather smart to not stick with mod_python,
though it might be, that it's just restin' .


As a first test I tried to use
apache with mod_wsgi and a hello world python wsgi script.

This works fine.


Almost everything of my mod_python code should translate rather easily.

The one problem, that I have is following.

my mod_python code uses:
 req.add_common_vars()
 ssh_dn_o = req.ssl_var_lookup("SSL_CLIENT_S_DN_O")


How could I obtain apache's SSL variable 'SSL_CLIENT_S_DN_O'

I'd like, that my script reacts differently depending on the client's
SSL certificate.


If that problem is solved then I could probably get rid of mod_python.











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


Re: float from numbers in text file

2010-06-21 Thread Stephen Hansen
On 6/21/10 4:26 PM, davidgp wrote:
> ah, i see :P
> float("45.34") or whatever does work fine, but the problem is that i'm
> reading it from a text file. so somehow it is not a real string or
> whatever..
> here's a part of the code:
> f = open ('/home/david/out.txt', 'r')
> 
> for line in f:
> if tel ==6:
>   buf = line.replace('\n', '')
>   lat = float(buf)
>   if tel ==7:
>   buf = line.replace('\n', '')
>   lng = float(buf)
> 
> basically it goes wrong in the last part where i try to convert the
> line to a float..
> i'm 100% sure that it's getting a number, not a text string
>

First: you don't have to strip \n's. Try float("45.43\n")

Second: this code, as is, can't run to show us the problem because
there's this "tel" business and I don't know what it is. But I'll tweak
it a bit and:

  f = open ('/Users/ixokai/out.txt', 'r')

  tel = 6
  for line in f:
 if tel == 6:
buf = line.replace('\n', '')
lat = float(buf)
print lat
 if tel == 7:
buf = line.replace('\n', '')
lng = float(buf)

And it works fine with a dummy data file I have. So... the problem is
somewhere else. Its either in your data file, or some other logic going
on (maybe something related to this 'tel' thing, I don't know).

Are you sure there's no lines in out.txt which have, say, some text (on
the left end)? Or maybe some blank lines?

Maybe you should send the whole file and the data.txt if you don't see
where the problem is.

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/



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


Re: float from numbers in text file

2010-06-21 Thread GMail Felipe


On 21/06/2010, at 20:26, davidgp  wrote:


On Jun 21, 4:18 pm, Stephen Hansen  wrote:

On 6/21/10 4:03 PM, davidgp wrote:




sorry :)


Okay, I should be more specific: include full tracebacks and some  
real

copied and pasted code :) Don't throw away nice debugging information
Python gave you, feed it to us.


invalid literal for long() with base 10: '51.9449702'
this is the error i'm getting when i use long(line)


Yes, "51.9449702" is an invalid literal for long. Long produces
integers: no decimal points.

However:


and this is the error for float(line)
invalid literal for float(): not found


Its a perfectly valid literal for float:>>> float('51.9449702')

51.9449702

So if you're getting that error, you're doing something else that  
you're

not telling us.

I suspect, somehow (I'd have to see your code to be sure), that your
"line" in the second case doesn't have that number. Try it in the
interactive interpreter. float('51.9449702') works fine. I suspect  
your

"line", for whatever reason, contains the string "not found", as in:


float('not found')


Traceback (most recent call last):
  File "", line 1, in 
ValueError: invalid literal for float(): not found

--

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog:http://meh.ixokai.io/

 signature.asc
< 1KViewDownload


ah, i see :P
float("45.34") or whatever does work fine, but the problem is that i'm
reading it from a text file. so somehow it is not a real string or
whatever..
here's a part of the code:
f = open ('/home/david/out.txt', 'r')

for line in f:
if tel ==6:
   buf = line.replace('\n', '')
   lat = float(buf)
   if tel ==7:
   buf = line.replace('\n', '')
   lng = float(buf)

basically it goes wrong in the last part where i try to convert the
line to a float..
i'm 100% sure that it's getting a number, not a text string

cheers!

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


Ok! Just to be sure, execute the following in your  file:

egrep -in 'not found' 

If it finds something, it will return the line number and what was  
found!


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


Re: tkInter Listbox question

2010-06-21 Thread Anthony Papillion
Thank you, RantingRick and EB303. Much appreciated and it looks like
it works fine now. Still learning but I am amazed every single day how
simple Python is!

Thanks Again,
Anthony Papillion
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: float from numbers in text file

2010-06-21 Thread davidgp
On Jun 21, 4:18 pm, Stephen Hansen  wrote:
> On 6/21/10 4:03 PM, davidgp wrote:
>
>
>
> > sorry :)
>
> Okay, I should be more specific: include full tracebacks and some real
> copied and pasted code :) Don't throw away nice debugging information
> Python gave you, feed it to us.
>
> > invalid literal for long() with base 10: '51.9449702'
> > this is the error i'm getting when i use long(line)
>
> Yes, "51.9449702" is an invalid literal for long. Long produces
> integers: no decimal points.
>
> However:
>
> > and this is the error for float(line)
> > invalid literal for float(): not found
>
> Its a perfectly valid literal for float:>>> float('51.9449702')
>
> 51.9449702
>
> So if you're getting that error, you're doing something else that you're
> not telling us.
>
> I suspect, somehow (I'd have to see your code to be sure), that your
> "line" in the second case doesn't have that number. Try it in the
> interactive interpreter. float('51.9449702') works fine. I suspect your
> "line", for whatever reason, contains the string "not found", as in:
>
> >>> float('not found')
>
> Traceback (most recent call last):
>   File "", line 1, in 
> ValueError: invalid literal for float(): not found
>
> --
>
>    Stephen Hansen
>    ... Also: Ixokai
>    ... Mail: me+list/python (AT) ixokai (DOT) io
>    ... Blog:http://meh.ixokai.io/
>
>  signature.asc
> < 1KViewDownload

ah, i see :P
float("45.34") or whatever does work fine, but the problem is that i'm
reading it from a text file. so somehow it is not a real string or
whatever..
here's a part of the code:
f = open ('/home/david/out.txt', 'r')

for line in f:
if tel ==6:
buf = line.replace('\n', '')
lat = float(buf)
if tel ==7:
buf = line.replace('\n', '')
lng = float(buf)

basically it goes wrong in the last part where i try to convert the
line to a float..
i'm 100% sure that it's getting a number, not a text string

cheers!

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


Re: float from numbers in text file

2010-06-21 Thread Stephen Hansen
On 6/21/10 4:03 PM, davidgp wrote:
> 
> sorry :)

Okay, I should be more specific: include full tracebacks and some real
copied and pasted code :) Don't throw away nice debugging information
Python gave you, feed it to us.

> invalid literal for long() with base 10: '51.9449702'
> this is the error i'm getting when i use long(line)

Yes, "51.9449702" is an invalid literal for long. Long produces
integers: no decimal points.

However:
> and this is the error for float(line)
> invalid literal for float(): not found

Its a perfectly valid literal for float:
>>> float('51.9449702')
51.9449702

So if you're getting that error, you're doing something else that you're
not telling us.

I suspect, somehow (I'd have to see your code to be sure), that your
"line" in the second case doesn't have that number. Try it in the
interactive interpreter. float('51.9449702') works fine. I suspect your
"line", for whatever reason, contains the string "not found", as in:

>>> float('not found')
Traceback (most recent call last):
  File "", line 1, in 
ValueError: invalid literal for float(): not found

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/



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


Re: float from numbers in text file

2010-06-21 Thread davidgp
On Jun 21, 4:00 pm, Stephen Hansen  wrote:
> On 6/21/10 3:54 PM, davidgp wrote:
>
> > i basically tried this:
> > lat =0.0
> > for line in f:
> >   lat = float(line)
>
> > but this gives an error.. does anyone know what i should to do?
> > thanks,
>
> "An error"?
>
> Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
> [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.>>> f = 
> open("test.txt", "r")
> >>> for line in f:
>
> ...     print float(line)
> ...
> 52.2375412
> 5.1802704
>
> Always include what the actual error is that you're running into.
>
> --
>
>    Stephen Hansen
>    ... Also: Ixokai
>    ... Mail: me+list/python (AT) ixokai (DOT) io
>    ... Blog:http://meh.ixokai.io/
>
>  signature.asc
> < 1KViewDownload

sorry :)

invalid literal for long() with base 10: '51.9449702'
this is the error i'm getting when i use long(line)

and this is the error for float(line)
invalid literal for float(): not found

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


Re: float from numbers in text file

2010-06-21 Thread Stephen Hansen
On 6/21/10 3:54 PM, davidgp wrote:
> i basically tried this:
> lat =0.0
> for line in f:
>   lat = float(line)
> 
> but this gives an error.. does anyone know what i should to do?
> thanks,

"An error"?

Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open("test.txt", "r")
>>> for line in f:
... print float(line)
...
52.2375412
5.1802704

Always include what the actual error is that you're running into.

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/



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


float from numbers in text file

2010-06-21 Thread davidgp
hello,
i have a text file that contains gps coordinates that i want to load
into my mysql database
the file is basically in this format:
52.2375412
5.1802704

i basically tried this:
lat =0.0
for line in f:
  lat = float(line)

but this gives an error.. does anyone know what i should to do?
thanks,
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is the difference between 'type' and 'class'?

2010-06-21 Thread Stephen Hansen
On 6/21/10 3:11 PM, Peng Yu wrote:
> Are type and class synonyms? It seems that they are at least according
> to some webpages that I read. But I'm not completely sure. Could you
> let me know in case my impress is wrong?

Once upon a time, a type was something that was only built-in, provided
by Python, whereas a class was a user-created kind of object that other
objects could inherit from. You couldn't inherit from a type -- only a
class. That's why there were UserDict implementations, and such.

But that's not the case anymore. IIUC, a new-style class is for all
intents and purposes a user-defined type, and the two serve the same
function and are essentially the same.

An old-style class (a class which does not ultimately inherit from
'object') is a bit of a different beast with some different semantics
and exists just for backwards compatibility, I think. The unification of
classes and types occurred in Python 2.2 with PEP252 and PEP253.

There may be some very narrow little cracks where something is slightly
different between types and new-style classes, but I've never run into
it -- except that many types are fundamentally immutable(i.e., ints,
strings), and its awful hard to make an immutable class.

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/



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


Re: What is the difference between 'type' and 'class'?

2010-06-21 Thread Thomas Jollans
On 06/22/2010 12:11 AM, Peng Yu wrote:
> pydoc xrange says:
> 
> Help on class xrange in module __builtin__:
> 
> class xrange(object)
> 
> python_2.6.5_library.pdf says:
> 
> Objects of type xrange are similar to buffers
> 
> Are type and class synonyms? It seems that they are at least according
> to some webpages that I read. But I'm not completely sure. Could you
> let me know in case my impress is wrong?
> 

They're the same. *

-- Thomas

* In versions prior to 3.0, this is not 100% true.  In versions prior to
2.0, this is not true. **

** Python 2.x distinguishes between "old-style" and "new-style" classes.
New-style classes (those which subclass object) are types. The others
are a bit different, and exist only for backwards compatibility. Don't
use them.
-- 
http://mail.python.org/mailman/listinfo/python-list


[ANN] python-signalfd 0.1 released

2010-06-21 Thread exarkun

Hello all,

I'm happy to announce the initial release of python-signalfd.  This 
simple package wraps the sigprocmask(2) and signalfd(2) calls, useful 
for interacting with POSIX signals in slightly more advanced ways than 
can be done with the built-in signal module.


You can find the package on PyPI or Launchpad:

   http://pypi.python.org/pypi/python-signalfd/0.1
   https://launchpad.net/python-signalfd

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


What is the difference between 'type' and 'class'?

2010-06-21 Thread Peng Yu
pydoc xrange says:

Help on class xrange in module __builtin__:

class xrange(object)

python_2.6.5_library.pdf says:

Objects of type xrange are similar to buffers

Are type and class synonyms? It seems that they are at least according
to some webpages that I read. But I'm not completely sure. Could you
let me know in case my impress is wrong?

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


Re: What module to parse/generate ical files?

2010-06-21 Thread Michael Crute
On Mon, Jun 21, 2010 at 1:50 PM, Grant Edwards  wrote:
> What module is recommended for parsing/generating ical files?
>
> However, I'm not sure it's being maintained.  Despite the claim on the
> above page that the current version is 2.1, The latest version I can
> find is v1.2 from 2006.

I've had pretty good luck with iCalendar, I'm currently using it to
bridge Outlook Web Access to iCal for old versions of Exchange and it
works flawlessly.

-- 
Michael E. Crute
http://mike.crute.org

It is a mistake to think you can solve any major problem just with
potatoes. --Douglas Adams
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is this make sence? Dynamic assembler for python

2010-06-21 Thread Rhodri James
On Mon, 21 Jun 2010 04:34:40 +0100, Steven D'Aprano  
 wrote:



On Sun, 20 Jun 2010 22:45:14 +0100, Rhodri James wrote:


No.  Modern C compilers often produce very good machine code, but the
best hand-written assembly code will be better.  I can usually write
*very* marginally better code than GCC achieves at work, though 99% of
the time I don't because it would be a maintenance nightmare.


Not that I don't believe you, but that is an extraordinary claim that
would require more evidence than just "Hey, some guy on the Internet
reckons his assembly code can regularly out-perform optimizing C
compilers" before I will change my opinion *wink*


Fairy 'nuff.  In this case it's a matter of having a simple processor and  
knowing a trick or two that our in-house compiler apparently doesn't.  Or  
didn't, since it has improved recently.  Oh, and having done this for a  
decade or two :-)


But like we've been agreeing, most of the time knocking an instruction or  
two out of a function isn't worth the effort.


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


Re: process cannot access the file because it is being used by other process

2010-06-21 Thread Thomas Jollans
On 06/21/2010 12:18 PM, shanti bhushan wrote:
> [snip]
> 
> i used below code
> 
> import subprocess
> import time
> def invoke_server1():
> proc = subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /C "D:
> \372\pythonweb\mongoose-2.8.exe -root D:\New1\"')
> 
> 
> invoke_server1()
> 
> 
> time.sleep(10)
> proc.kill()

This cannot work, since proc is not a global variable, it's local to
invoke_server1. You could do something like this instead:

def invoke_server1()
return subprocess.Popen([r'D:\372\pythonweb\mongoose-2.8.exe',
  '-root', r'D:\New1\"])

server1_proc = invoke_server1()
time.sleep(10)
server1_proc.kill()


Are you running the code from a command prompt ? It should have printed
a nice and helpful NameError. Had you read that error message, it should
have been easy to figure out that there's something wrong with your
variable scopes.

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


Re: process cannot access the file because it is being used by other process

2010-06-21 Thread Chris Rebert
On Mon, Jun 21, 2010 at 6:44 AM, Felipe Vinturini
 wrote:

> Your problem seems to be with stdout redirect to the same file:
> ">YourOutput1.txt". Windows is not like Unix like systems!
> You can try, instead of redirecting to the same file, redirect each to a
> separate file and use the following form:
> 
>        proc = subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /C
> "D:\372\pythonweb\mongoose-2.8.exe -root D:\New1\ 1>YourOutput.txt 2>&1"')
> 

I believe the cmd.exe ugliness can also be avoided altogether:
proc = subprocess.Popen([r'D:\372\pythonweb\mongoose-2.8.exe',
'-root', 'D:\New1\'], stderr=subprocess.STDOUT,
stdout=file("YourOutput.txt", 'w'))

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help with suds: HTTP Error 401

2010-06-21 Thread plainsane
On Jun 19, 12:16 pm, Sean DiZazzo  wrote:
> On Jun 11, 5:27 am, Eric von Horst  wrote:
>
> > I have small program that tries to open a wsdl. When I execute the
> > program I am getting 'suds.transport.TransportError: HTTP Error 401:
> > Unauthorized'
>
> Hey Eric,
>
> Im a suds noob as well.  I found some code that led me to the below
> example.  It worked for me when connecting to one particular site.  I
> couldnt tell you why though...  I guess its worth a shot.
>
> from suds.transport.https import HttpAuthenticated
> import urllib2
>
> t = HttpAuthenticated(username='me', password='password')
> t.handler = urllib2.HTTPBasicAuthHandler(t.pm)
> t.urlopener =  urllib2.build_opener(t.handler)
> c = client.Client(url='http://xxx.xxx.xxx.xxx/path/to?
> WSDL',transport=t)
>
> ~Sean

thank you.  that helped a lot.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: __slot__: what is it good for?

2010-06-21 Thread Stephen Hansen
On 6/21/10 11:06 AM, Terry Reedy wrote:
> On 6/21/2010 11:24 AM, Stephen Hansen wrote:
>> On 6/21/10 8:08 AM, Stephen Hansen wrote:
>>> If you don't want a class to have attributes added at runtime, the
> 
>> The Pythonic way to achieve that is to... simply NOT add attributes at
>> runtime.
>>
>> I.e., choose to follow the rule you've decided on.
> 
> Or add a __setattr__ method, but the above is easier.

I'd sort of argue that adding a __setattr__ for that purpose is
un-Pythonic (as opposed to using __setattr__ for some functional purpose
and that restriction being a side-effect). Its possible to do it, sure;
but its also possible to use __slots__ to do so as well. But its bad
form in both cases.

But, that argument is largely Nit meeting Pick and having a fight about
which kettle is blacker. :)

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/




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


Re: Decode II (more complex)

2010-06-21 Thread John Bokma
Thomas Lehmann  writes:

>> Your email(s) get send as 7 bit (ASCII). Email them as utf-8 and I guess
>> your problem is solved.
>>
>> How do you email the notifications?
>>
>
>  I was copying partly the logic from 
> http://code.activestate.com/recipes/473810
>  Changing to buffer.decode("utf-8", 'replace') where I'm reading the
> file and
>  changing the html template at top to utf-8 leads to following output:

Most likely you have to do something like this

msgText = MIMEText('your HTML utf-8 message')
msg.set_charset('utf-8')
msgAlternative.attach(msgText)

Or:

msgText = MIMEText('your HTML utf-8 message', 'utf-8')   [1]
msgAlternative.attach(msgText)


[1] with a quick Google I found one message stating that this didn't
work for utf-8, in Python 3.1.1, see: 
http://mail.python.org/pipermail/python-list/2010-March/1238611.html

-- 
John Bokma   j3b

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


Re: Generator (re-)definition within a loop

2010-06-21 Thread Terry Reedy

On 6/21/2010 3:29 AM, Pierre Reinbold wrote:


On 06/18/2010 11:48 PM, Terry Reedy wrote:



Let's apply Reedy's Rule: when you have trouble understanding a function
expression, replace it with the (near) equivalent def statement. (Among
other advantages, one can insert print calls!)

Genexps, like lambdas, are specialized function expressions.

def augment(s_of_s, s):
  for x in s_of_s:
   for y in s:
yield x+[y]

def gen_product(*args):
 pools = map(tuple, args)
 result = [[]]
 for pool in pools:
 result = augment(result,pool)
 for prod in result:
 yield tuple(prod)

print(list(gen_product("ABC", "xy")))


#3.1

[('A', 'x'), ('A', 'y'), ('B', 'x'), ('B', 'y'), ('C', 'x'), ('C', 'y')]


Very instructive post ! Thank you !



Just trying to understand, I have apply the Reedy's Rule in an attempt
to reproduce the same behaviour as the generator expression.


I intentionally said '(near) equivalent' because, as you seem to have 
found, exactly reproducing a bug in a function expression with a def 
statement may be difficult to impossible. The form I applied above was 
'equivalent to what one *thought* one was writing'. If one just wants to 
fix the bug without completely understanding it, that should be enough.



The idea, I
guess, is to (re-)define the generator function inside the loop.


To produce a similar bug, in this case, yes.


My first try gives this:

def badgen_product1(*args, **kwds):
 pools = map(tuple, args)
 result = [[]]
 for pool in pools:
 def result():
 for x in result():
 for y in pool:
 yield x+[y]
 for prod in result():
 yield tuple(prod)

But this does not reproduce the generator expression, it leads naturally
to an infinite recursion (which is what I expected first for the
generator expression btw)

 for x in result():
RuntimeError: maximum recursion depth exceeded

Another try to avoid infinite recursion:

def badgen_product2(*args, **kwds):
 pools = map(tuple, args)
 result = [[]]
 for pool in pools:
 def augments():
 for x in result:
 for y in pool:
 yield x+[y]
 result = augments()
 for prod in result:
 yield tuple(prod)

And this one gives the infamous:

 for x in result:
ValueError: generator already executing

Which seems to indicate that the lazy evaluation leads to eventually
bind everything to the same generator.

So, neither of my attempts reproduce the behaviour of the generator
expression. What do I miss here ?


See above. Exact reproduction is not always possible.


Thank for your help,


You are welcome. I hope you learned something. Wrestling with code is a 
good way to do that. I love Python because it makes it so easy to do that.


--
Terry Jan Reedy

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


Re: __slot__: what is it good for?

2010-06-21 Thread Terry Reedy

On 6/21/2010 11:24 AM, Stephen Hansen wrote:

On 6/21/10 8:08 AM, Stephen Hansen wrote:

If you don't want a class to have attributes added at runtime, the



The Pythonic way to achieve that is to... simply NOT add attributes at
runtime.

I.e., choose to follow the rule you've decided on.


Or add a __setattr__ method, but the above is easier.

--
Terry Jan Reedy

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


Re: deduping

2010-06-21 Thread Paul Rubin
dirknbr  writes:
> done_={}
> for line in done:
> done_[line.strip()]=0
> ...

Maybe you mean:

done_ = set(line.strip() for line in done)
outf_ = set(line.split(',')[1] for line in outf)
universe = done_ & outf   # this finds the set intersection
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Generator (re-)definition within a loop

2010-06-21 Thread Thomas Jollans
On 06/21/2010 09:29 AM, Pierre Reinbold wrote:
> [snip]
> 
> Another try to avoid infinite recursion:
> 
> def badgen_product2(*args, **kwds):
> pools = map(tuple, args)
> result = [[]]
> for pool in pools:
> def augments():
> for x in result:
This line is executed when you start iterating over the generator,
(because generators are lazy...). To get "result", which is not defined
in the inner function, Python looks in the namespace of the outer
function, where it's already been re-bound. So the generator basically
tries to iterate over itself (I think)

> for y in pool:
> yield x+[y]
> result = augments()
> for prod in result:
> yield tuple(prod)
> 
> And this one gives the infamous:
> 
> for x in result:
> ValueError: generator already executing
> 
> Which seems to indicate that the lazy evaluation leads to eventually
> bind everything to the same generator.
> 
> So, neither of my attempts reproduce the behaviour of the generator
> expression. What do I miss here ?

Bind "result" within the inner function. Either of these concepts should
work (not tested):

...
for pool in pools:
def augments(result):
for x in result:
...
result = augments(result)


#or
...
result = lambda: [[]]
for pool in pools:
def result(oldresult=result):
for x in oldresult():
...

With the second option, Python takes "result" and saves it in the
definition of the new "result".


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


Re: Where is the help function defined?

2010-06-21 Thread Terry Reedy

On 6/21/2010 1:17 PM, Peng Yu wrote:

help(help) gives me the following explanation.

##
Help on _Helper in module site object:

class _Helper(__builtin__.object)
  |  Define the built-in 'help'.


See 'built-in'?



  |  This is a wrapper around pydoc.help (with a twist).
  |
  |  Methods defined here:
  |
  |  __call__(self, *args, **kwds)
  |
  |  __repr__(self)
  |
  |
--
  |  Data descriptors defined here:
  |
  |  __dict__
  |  dictionary for instance variables (if defined)
  |
  |  __weakref__
  |  list of weak references to the object (if defined)
###

I then looked at pydoc site.


Which is?

Lib Ref 2. Built-in Functions has entry

help([object])
Invoke the built-in help system. (This function is intended for 
interactive use.) If no argument is given, the interactive help system 
starts on the interpreter console. If the argument is a string, then the 
string is looked up as the name of a module, function, class, method, 
keyword, or documentation topic, and a help page is printed on the 
console. If the argument is any other kind of object, a help page on the 
object is generated.


This function is added to the built-in namespace by the site module.

Peruse the first 6 chapers of the Library Reference so you are familiar 
with what is there. I use them pretty regularly.


Terry Jan Reedy


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


Re: Where is the help function defined?

2010-06-21 Thread Emile van Sebille

On 6/21/2010 10:17 AM Peng Yu said...

help(help) gives me the following explanation.





I then looked at pydoc site. But I don't see an entry on help(). How
to figure out where help() (or a function in general) is defined?


ActivePython 2.4.1 Build 247 (ActiveState Corp.) based on
Python 2.4.1 (#65, Jun 20 2005, 17:01:55) [MSC v.1310 32 bit (Intel)] on 
win32

Type "help", "copyright", "credits" or "license" for more information.
>>> dir(help)
['__call__', '__class__', '__delattr__', '__dict__', '__doc__', 
'__getattribute__', '__hash__', '__init__', '__module__', '__new__',
 '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 
'__weakref__']

>>> help.__module__
'site'
>>> import site
>>> dir (site)
['_Helper', '_Printer', '__builtin__', '__builtins__', '__doc__', 
'__file__', '__name__', '_init_pathinfo', '_test', 'abs__file__',
'addbuilddir', 'addpackage', 'addsitedir', 'addsitepackages', 
'aliasmbcs', 'execsitecustomize', 'main', 'makepath', 'os', 'removedup
paths', 'setBEGINLIBPATH', 'setcopyright', 'setencoding', 'sethelper', 
'setquit', 'sys']

>>> site.__file__
'C:\\Python24\\lib\\site.pyc'
>>>


HTH,

Emile



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


What module to parse/generate ical files?

2010-06-21 Thread Grant Edwards
What module is recommended for parsing/generating ical files?

Specifically, I'd like to parse invitations generated by MS Outlook
and generate accept/decline responses.  I've tinkered with iCalendar
1.2, and have had some success after manualling munging/filtering the
data for some fields:

  http://pypi.python.org/pypi/icalendar/1.2

However, I'm not sure it's being maintained.  Despite the claim on the
above page that the current version is 2.1, The latest version I can
find is v1.2 from 2006.

I've also run across vobject:

  http://vobject.skyhouseconsulting.com/

That appears to be a little more active, but I haven't tried it yet.

Is there a canonical "ical" parser/generator module for Python?

-- 
Grant Edwards   grant.b.edwardsYow! I feel better about
  at   world problems now!
  gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


ANNOUNCING Tahoe, the Least-Authority File System, v1.7.0

2010-06-21 Thread Zooko O'Whielacronx
Dear people of python-list:

We just released Tahoe-LAFS v1.7, the secure distributed filesystem
written entirely [*] in Python.

The major new feature is an SFTP server. This means that (with enough
installing software and tinkering with your operating system
configuration) you can have a normal-looking mount point backed by a
Tahoe-LAFS grid.

Google is sponsoring us through Google Summer of Code. The next
release after this one will hopefully include the resulting
improvements.

Regards,

Zooko

[*] That's a lie. The parts that require maximum CPU efficiency—secure
hash functions, ciphers, erasure coding—are written in C or C++. But
we wrote as much of it as we could in Python.


ANNOUNCING Tahoe, the Least-Authority File System, v1.7.0

The Tahoe-LAFS team is pleased to announce the immediate
availability of version 1.7.0 of Tahoe-LAFS, an extremely
reliable distributed storage system.

Tahoe-LAFS is the first distributed storage system which offers
"provider-independent security"—meaning that not even the
operator of your storage server can read or alter your data
without your consent. Here is the one-page explanation of its
unique security and fault-tolerance properties:

http://tahoe-lafs.org/source/tahoe/trunk/docs/about.html

Tahoe-LAFS v1.7.0 is the successor to v1.6.1, which was released
February 27, 2010 [1].

v1.7.0 is a major new release with new features and bugfixes. It
adds a fully functional SFTP interface, support for non-ASCII character
encodings, and a new upload algorithm which guarantees that each file
is spread over multiple servers for fault-tolerance. See the NEWS file
[2] for details.


WHAT IS IT GOOD FOR?

With Tahoe-LAFS, you distribute your filesystem across multiple
servers, and even if some of the servers are compromised by
by an attacker, the entire filesystem continues to work
correctly, and continues to preserve your privacy and
security. You can easily share specific files and directories
with other people.

In addition to the core storage system itself, volunteers have
built other projects on top of Tahoe-LAFS and have integrated
Tahoe-LAFS with existing systems.

These include frontends for Windows, Macintosh, JavaScript,
iPhone, and Android, and plugins for Hadoop, bzr, mercurial,
duplicity, TiddlyWiki, and more. See the Related Projects page
on the wiki [3].

We believe that strong cryptography, Free and Open Source
Software, erasure coding, and principled engineering practices
make Tahoe-LAFS safer than RAID, removable drive, tape,
on-line backup or "cloud storage" systems.

This software is developed under test-driven development, and
there are no known bugs or security flaws which would
compromise confidentiality or data integrity under recommended
use. (For all currently known issues please see the
known_issues.txt file [4].)


COMPATIBILITY

This release is fully compatible with the version 1 series of
Tahoe-LAFS. Clients from this release can write files and
directories in the format used by clients of all versions back
to v1.0 (which was released March 25, 2008). Clients from this
release can read files and directories produced by clients of
all versions since v1.0. Servers from this release can serve
clients of all versions back to v1.0 and clients from this
release can use servers of all versions back to v1.0.

This is the ninth release in the version 1 series. This series
of Tahoe-LAFS will be actively supported and maintained for
the forseeable future, and future versions of Tahoe-LAFS will
retain the ability to read and write files compatible with
Tahoe-LAFS v1.


LICENCE

You may use this package under the GNU General Public License,
version 2 or, at your option, any later version. See the file
"COPYING.GPL" [5] for the terms of the GNU General Public
License, version 2.

You may use this package under the Transitive Grace Period
Public Licence, version 1 or, at your option, any later
version. (The Transitive Grace Period Public Licence has
requirements similar to the GPL except that it allows you to
wait for up to twelve months after you redistribute a derived
work before releasing the source code of your derived work.)
See the file "COPYING.TGPPL.html" [6] for the terms of the
Transitive Grace Period Public Licence, version 1.

(You may choose to use this package under the terms of either
licence, at your option.)


INSTALLATION

Tahoe-LAFS works on Linux, Mac OS X, Windows, Cygwin, Solaris,
*BSD, and probably most other systems. Start with
"docs/quickstart.html" [7].


HACKING AND COMMUNITY

Please join us on the mailing list [8]. Patches are gratefully
accepted -- the RoadMap page [9] shows the next improvements
that we plan to make and CREDITS [10] lists the names of people
who've contributed to the project. The Dev page [11] contains
resources for hackers.


SPONSORSHIP

Tahoe-LAFS was originally developed by Allmydata, Inc., a
provider of commercial backup services. After discontinuing
funding of Tahoe-LAFS R&D in early 2009, they have continued

Re: Where is the help function defined?

2010-06-21 Thread Thomas Jollans
On 06/21/2010 07:17 PM, Peng Yu wrote:
> help(help) gives me the following explanation.
> 
> [snip]
> 
> I then looked at pydoc site. But I don't see an entry on help(). How
> to figure out where help() (or a function in general) is defined?

>>> type(dir)

>>> type(help)

>>>


help is not a function, it's an instance of site._Helper. It just
"feels" like a function by being callable.
You could create your own "help" callable simply by using the _Helper
contructor:

>>> import site
>>> help2 = site._Helper()
>>>


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


Re: Where is the help function defined?

2010-06-21 Thread Stephen Hansen
On 6/21/10 10:17 AM, Peng Yu wrote:
> Help on _Helper in module site object:

It says so right here.

> I then looked at pydoc site. But I don't see an entry on help(). How
> to figure out where help() (or a function in general) is defined?

Generally:

Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> help.__class__





-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/



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


Re: Is this make sence? Dynamic assembler for python

2010-06-21 Thread Stephen Hansen
On 6/21/10 10:12 AM, MRAB wrote:
> A human can write better assembly code than a compiler, but would take a
> much longer, and usually for not much gain, so it's usually a waste of
> time (premature optimisation, and all that).

When you get to the point where you're considering writing something in
assembly, its no longer "premature" -- you are clearly in a domain which
needs to process a lot of data and crunch it very efficiently.

Once you're at that point, there can be quite significant gain.

Fortunately, the vast majority of people, and situations, and programs,
don't ever get there.

And even more fortunately: very, very often when those people who *do*
get to that place, they actually can use a library which has already
done it and get those major performance gains without writing the
assembly themselves.

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/



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


Where is the help function defined?

2010-06-21 Thread Peng Yu
help(help) gives me the following explanation.

##
Help on _Helper in module site object:

class _Helper(__builtin__.object)
 |  Define the built-in 'help'.
 |  This is a wrapper around pydoc.help (with a twist).
 |
 |  Methods defined here:
 |
 |  __call__(self, *args, **kwds)
 |
 |  __repr__(self)
 |
 |
--
 |  Data descriptors defined here:
 |
 |  __dict__
 |  dictionary for instance variables (if defined)
 |
 |  __weakref__
 |  list of weak references to the object (if defined)
###

I then looked at pydoc site. But I don't see an entry on help(). How
to figure out where help() (or a function in general) is defined?
-- 
http://mail.python.org/mailman/listinfo/python-list


[RELEASED] Python 2.7 release candidate 2

2010-06-21 Thread Benjamin Peterson
On behalf of the Python development team, I'm tickled pink to announce the
second release candidate of Python 2.7.

Python 2.7 is scheduled (by Guido and Python-dev) to be the last major version
in the 2.x series. However, 2.7 will have an extended period of bugfix
maintenance.

2.7 includes many features that were first released in Python 3.1. The faster io
module, the new nested with statement syntax, improved float repr, set literals,
dictionary views, and the memoryview object have been backported from 3.1. Other
features include an ordered dictionary implementation, unittests improvements, a
new sysconfig module, auto-numbering of fields in the str/unicode format method,
and support for ttk Tile in Tkinter.  For a more extensive list of changes in
2.7, see http://doc.python.org/dev/whatsnew/2.7.html or Misc/NEWS in the Python
distribution.

To download Python 2.7 visit:

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

While this is a preview release and is thus not suitable for production use, we
strongly encourage Python application and library developers to test the release
with their code and report any bugs they encounter to:

 http://bugs.python.org/

This helps ensure that those upgrading to Python 2.7 will encounter as few bumps
as possible.

2.7 documentation can be found at:

 http://docs.python.org/2.7/


Enjoy!

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


Re: Is this make sence? Dynamic assembler for python

2010-06-21 Thread MRAB

David Cournapeau wrote:

On Mon, Jun 21, 2010 at 12:34 PM, Steven D'Aprano
 wrote:

On Sun, 20 Jun 2010 22:45:14 +0100, Rhodri James wrote:


Mixing Python and assembler is a bizarre thing to want to do in general,
but...

On Sun, 20 Jun 2010 01:52:15 +0100, Steven D'Aprano
 wrote:


(3) Modern C compilers can produce better (faster, more efficient)
machine code than the best assembly code written by hand.

No.  Modern C compilers often produce very good machine code, but the
best hand-written assembly code will be better.  I can usually write
*very* marginally better code than GCC achieves at work, though 99% of
the time I don't because it would be a maintenance nightmare.


Not that I don't believe you, but that is an extraordinary claim that
would require more evidence than just "Hey, some guy on the Internet
reckons his assembly code can regularly out-perform optimizing C
compilers" before I will change my opinion *wink*


No, it is not extraordinary claim, I would actually think it is common
knowledge. For performance sensitive, extremely well constraints
algorithm, ASM easily beats general C compilers. Most optimized
numerical libraries rely quite heavily on ASM to get significant speed
up (Atlas, FFTW, MKL). C has actually a few majors aspects which makes
some optimizations very hard to do - things like pointer aliasing, for
example, where the compiler has to be quite pessimistic in general.

Also, the fact that current compilers can generate code which is
significantly faster than they used to a few years ago *on the exact
same C code* show that it is not that hard to beat C compilers. They
manage to do it by themselves :)


A human can write better assembly code than a compiler, but would take a
much longer, and usually for not much gain, so it's usually a waste of
time (premature optimisation, and all that).
--
http://mail.python.org/mailman/listinfo/python-list


Re: Is this make sence? Dynamic assembler for python

2010-06-21 Thread David Cournapeau
On Mon, Jun 21, 2010 at 12:34 PM, Steven D'Aprano
 wrote:
> On Sun, 20 Jun 2010 22:45:14 +0100, Rhodri James wrote:
>
>> Mixing Python and assembler is a bizarre thing to want to do in general,
>> but...
>>
>> On Sun, 20 Jun 2010 01:52:15 +0100, Steven D'Aprano
>>  wrote:
>>
>>> (3) Modern C compilers can produce better (faster, more efficient)
>>> machine code than the best assembly code written by hand.
>>
>> No.  Modern C compilers often produce very good machine code, but the
>> best hand-written assembly code will be better.  I can usually write
>> *very* marginally better code than GCC achieves at work, though 99% of
>> the time I don't because it would be a maintenance nightmare.
>
>
> Not that I don't believe you, but that is an extraordinary claim that
> would require more evidence than just "Hey, some guy on the Internet
> reckons his assembly code can regularly out-perform optimizing C
> compilers" before I will change my opinion *wink*

No, it is not extraordinary claim, I would actually think it is common
knowledge. For performance sensitive, extremely well constraints
algorithm, ASM easily beats general C compilers. Most optimized
numerical libraries rely quite heavily on ASM to get significant speed
up (Atlas, FFTW, MKL). C has actually a few majors aspects which makes
some optimizations very hard to do - things like pointer aliasing, for
example, where the compiler has to be quite pessimistic in general.

Also, the fact that current compilers can generate code which is
significantly faster than they used to a few years ago *on the exact
same C code* show that it is not that hard to beat C compilers. They
manage to do it by themselves :)

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


Re: tkInter Listbox question

2010-06-21 Thread rantingrick
On Jun 21, 12:36 am, Anthony Papillion  wrote:
> So I'm trying to add a Listbox to my window. I want it to be the width
> of my window and the height of my window.  I'm using the following
> code ('root' is my toplevel window):
>
> gsItems = Listbox(root, width=root.winfo_width(),
> height=root.winfo_height())
> gsItems.pack()
>
> While you would think this code would set the height and width of
> gsItems to the height and width of root, it doesn't. Instead it puts a
> little tiny listbox in the middle of the form.


eb303 answered your question perfectly however he failed to point you
toward the proper documentation. The Python docs you need are here...

http://effbot.org/tkinterbook/

 Remember Guys, this is a Python mailing list. Always give
reference FIRST to docs that are "Python" specific. *Then* if you want
to show off your multi lingual-ness by all means post extra links. 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Decode II (more complex)

2010-06-21 Thread Thomas Lehmann
> Your email(s) get send as 7 bit (ASCII). Email them as utf-8 and I guess
> your problem is solved.
>
> How do you email the notifications?
>

 I was copying partly the logic from http://code.activestate.com/recipes/473810
 Changing to buffer.decode("utf-8", 'replace') where I'm reading the
file and
 changing the html template at top to utf-8 leads to following output:

 File "/srv/buildManager/BuildManagerMail.py", line 27, in sendMail
msgText = MIMEText(htmlMessage, 'html')
  File "/usr/lib64/python2.6/email/mime/text.py", line 30, in __init__
self.set_payload(_text, _charset)
  File "/usr/lib64/python2.6/email/message.py", line 224, in
set_payload
self.set_charset(charset)
  File "/usr/lib64/python2.6/email/message.py", line 264, in
set_charset
cte(self)
  File "/usr/lib64/python2.6/email/encoders.py", line 73, in
encode_7or8bit
orig.encode('ascii')
AttributeError: 'DynamicallyCompiledCheetahTemplate' object has no
attribute 'encode'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: __slot__: what is it good for?

2010-06-21 Thread Stephen Hansen
On 6/21/10 8:08 AM, Stephen Hansen wrote:
> If you don't want a class to have attributes added at runtime, the
> Pythonic way to achieve that is to... simply add attributes at runtime.

Errr.

The Pythonic way to achieve that is to... simply NOT add attributes at
runtime.

I.e., choose to follow the rule you've decided on.

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/



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


Re: __slot__: what is it good for?

2010-06-21 Thread Peter Otten
Alexander Eisenhuth wrote:

> - what is the reason, that __slots__ are introduced in python?

When you have "many" instances of a class with a fixed set of attributes 
__slots__ can save you some memory because it avoids the overhead for the 
instance __dict__. Note that "many" means millions rather than thousands. 
The best approach is to measure the memory footprint of your app with and 
without __slots__ in the class you see as a good candidate and only use it 
when you find a significant difference.

> - I want to use slots to define a class where no attributes are added at
> runtime. Is that a good idea to use slots for that?

No.

Peter

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


Re: __slot__: what is it good for?

2010-06-21 Thread Stephen Hansen
On 6/21/10 7:27 AM, Alexander Eisenhuth wrote:
> Hello out there,
> 
> - what is the reason, that __slots__ are introduced in python?
> 
> - I want to use slots to define a class where no attributes are added at
> runtime. Is that a good idea to use slots for that?

In short, its best to use __slots__ in situations where you are creating
a large number of objects with a set few number of attributes. It lets
you pre-define those variables and the descriptors to access them,
without having to "waste" a dictionary. Unless you're making many
objects, the "waste" of the instance dictionary is largely irrelevant.

Its an optimization: its not an access-control mechanism, or a mechanism
designed to "declare" the attributes of a class before-hand or prevent
attribute creation at runtime.

You could use it that way: but its bad form :)

If you don't want a class to have attributes added at runtime, the
Pythonic way to achieve that is to... simply add attributes at runtime.

Python doesn't really believe in mandates. It believes in good behavior
of adults, awhile acknowledging sometimes an adult might have a
perfectly good reason to do something sneaky (such as add an attribute
to an instance at runtime: its actually *extremely* rare for someone to
do that, so why try to force it away?) down the road that you might not
think of at the time.

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/



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


Re: If Not CGI...

2010-06-21 Thread Victor Subervi
On Mon, Jun 21, 2010 at 9:03 AM, Tim Chase wrote:

> On 06/21/2010 07:40 AM, Victor Subervi wrote:
>
>> I would like to explore rewriting the shopping cart in Django.
>> The reality of the matter may make it difficult. Working
>> literally from the time I awake to when I go to sleep and not
>> having enough hours to complete everything I set for myself
>> makes it difficult,
>>
>
> The reason for using a framework is because web development is hard work --
> the creators of these frameworks have gone before you doing much of the hard
> work so you can concentrate on your domain-specific tasks instead of the
> infrastructure.
>
> When you want to drive across the country, do you begin by procuring all
> the land in between and then paving your roads? No...you leave that to
> people who do infrastructure full-time (and with lots of experience in this
> area).  You just get in your car and drive to your destination (your
> domain-specific task) and let the professionals deal with the
> infrastructure.  That common infrastructure investment is then shared
> between many users with widely different destinations.
>
> So you may find that, by switching to a web framework, you actually end up
> *saving* time because you don't need to recreate all the work of building a
> framework.  Given that tens of thousands of development hours (by some very
> smart & very experienced people) have gone into some of the big-name
> frameworks such as Django, those are tens-of-thousands of hours that *you*
> don't have to spend.
>

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


Re: Decode II (more complex)

2010-06-21 Thread John Bokma
Thomas Lehmann  writes:

> Hi all,
>
> I have written a small python xmlrpc server which checks logfiles of a
> build
> sending notifications to the responsible teams. On a machine I'm
> forced to
> a problem with one logfile with special characters inside generated by
> a
> gnu compiler.
>
> Using cheetah for generating the HTML mail I get a:
> ('ascii' codec can't decode byte 0xe2 in position 25868: ordinal not
> in range(128))

Your email(s) get send as 7 bit (ASCII). Email them as utf-8 and I guess
your problem is solved.

How do you email the notifications?

-- 
John Bokma   j3b

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


Re: __slot__: what is it good for?

2010-06-21 Thread James Mills
On Tue, Jun 22, 2010 at 12:27 AM, Alexander Eisenhuth
 wrote:
> Hello out there,
>
> - what is the reason, that __slots__ are introduced in python?
>
> - I want to use slots to define a class where no attributes are added at
> runtime. Is that a good idea to use slots for that?

Here is the relevant documentation:'

http://docs.python.org/reference/datamodel.html#slots

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


__slot__: what is it good for?

2010-06-21 Thread Alexander Eisenhuth

Hello out there,

- what is the reason, that __slots__ are introduced in python?

- I want to use slots to define a class where no attributes are added at 
runtime. Is that a good idea to use slots for that?



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


Re: OT! Python vs... Objective-C!

2010-06-21 Thread Stephen Hansen
On 6/21/10 6:47 AM, Grant Edwards wrote:
> On 2010-06-21, Stephen Hansen  wrote:
> 
> [...]
> 
>> I'm just learning Objective-C on my spare time, and am having these
>> entirely disturbing feelings of familiarity, where strange swirling
>> thoughts enter my head that sound oddly like, "This feels sorta
>> Pythony, how oddly pleasant." With, of course, a bad smell from the
>> syntax at the same time.
> 
> Both drew a great deal of inspiration from Smalltalk.  It's much more
> obvious in O-C's syntax than in Python's.

Oh, that somehow skipped my mind. I knew the Objective part came from
Smalltalk, but knowing that in the back of my head didn't get over to my
emotional side of my brain which was finding it all comfortable after I
learned it.

Yeah, I think you've pegged it.

>> And yet, after a day or two in it, it doesn’t really bother me so
>> much anymore.
> 
> Sort of like significant whitespace?  At first I thought it a horrible
> idea.

Oh, we all did. Its more the symbols, I usually get annoyed by symbols
in every language and don't quite get past it. Objective-C has a lot of
them; from - instance methods to + class methods to @everything to the
^{ blocks } and such.

But its less offensive to my eye and brain then say, Perl or even Ruby.
They never quite stopped jarring when I learned those languages.

Anyhoo. Appreciate the insight. :)

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/



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


Re: OT! Python vs... Objective-C!

2010-06-21 Thread Grant Edwards
On 2010-06-21, Stephen Hansen  wrote:

[...]

> I'm just learning Objective-C on my spare time, and am having these
> entirely disturbing feelings of familiarity, where strange swirling
> thoughts enter my head that sound oddly like, "This feels sorta
> Pythony, how oddly pleasant." With, of course, a bad smell from the
> syntax at the same time.

Both drew a great deal of inspiration from Smalltalk.  It's much more
obvious in O-C's syntax than in Python's.

>[...]

> And so, I’m learning. Its interesting. The most startling thing
> about it is how *Pythonic* it feels. I can’t quite put my finger
> on exactly why: its not the syntax, surely. Somehow the language has
> taken something perfectly repulsive on its own (Hi, C, I’m looking
> at you!) and turned it into a shrine to perversity.

It's just Smalltalk.

> And yet, after a day or two in it, it doesn’t really bother me so
> much anymore.

Sort of like significant whitespace?  At first I thought it a horrible
idea.

-- 
Grant Edwards   grant.b.edwardsYow! Where's SANDY DUNCAN?
  at   
  gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: process cannot access the file because it is being used by other process

2010-06-21 Thread Felipe Vinturini
On Mon, Jun 21, 2010 at 7:18 AM, shanti bhushan
wrote:

> On Jun 21, 2:15 pm, Tim Golden  wrote:
> > On 21/06/2010 09:23, shanti bhushan wrote:
> >
> > > i am using below code ,it works fine on ordinary python 26 ,but when i
> > > use this script in my python testing tool it gives me message "process
> > > cannot access the file because it is being used by other process" for
> > > the second time invoking of mongoose server.
> > > Please help me in handling this exception.
> >
> > Before I make any suggestions on the code, I might suggest that you
> > learn to wait a little. You sent three pretty much identical messages
> > within the space of three hours. Believe me: if people aren't helping,
> > it's not because they haven't seen your first message. Or the follow-up.
> > Or the one after that. It's because they don't know the answer, or
> > haven't the time to answer. Or aren't in the same timezone as you and
> > so haven't woken up yet!
> >
> > > def invoke_server2():
> > >  file = open("mongoose-2.8.exe", "r")
> > >  try:
> > >  proc = subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /C "D:
> > > \372\pythonweb\mongoose-2.8.exe -root D:\New1\>YourOutput.txt"')
> > >  except OSError:
> > >  print "os error"
> > >  file.close()
> > >  sys.exc_clear()
> > >  os.remove("mongoose-2.8.exe")
> >
> > OK. I'm not sure what you're achieving with the open ("mongoose...") line
> > and its corresponding close. In fact, I can't work out what the whole
> > exception block is achieving. I actually had to go and look up what
> > sys.exc_clear is doing -- and I don't think it's doing what you think
> > it's doing. You appear to be trapping an OS error, such as file-not-found
> > or access-denied, by trying to ignore the error and then deleting the
> > server
> > itself!
> >
> > Let's straighten some stuff out. First your Popen line could almost
> > certainly
> > be simplified to this:
> >
> > 
> > import subprocess
> >
> > with open ("YourOutput.txt", "w") as outf:
> >proc = subprocess.Popen (
> >  [r"D:\372\pythonweb\mongoose-2.8.exe", "-root", r"D:\New1"],
> >  stdout=outf
> >)
> >
> > 
> >
> > and to kill the proc, you can just call proc.kill ()
> >
> > Does that take you forward? Are you still seeing the "Cannot access
> file..."
> > errors?
> >
> > TJG
>
> i used below code
>
> import subprocess
> import time
> def invoke_server1():
> proc = subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /C "D:
> \372\pythonweb\mongoose-2.8.exe -root D:\New1\"')
>
>
> invoke_server1()
>
>
> time.sleep(10)
> proc.kill()
>
> this code only invokes the server but is not killing after 10 seconds.
>
> my purpose is invoke server many times with different argument and
> kill it.
> but when ever  i use subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /
> c
> "taskkill /F /IM mongoose-2.8.exe >YourOutput1.txt"'
> this gives me error "process cannot access the file because it is
> being used by other process"
>

Your problem seems to be with *stdout* redirect to the same file:
">YourOutput1.txt". Windows is not like Unix like systems!

You can try, instead of redirecting to the same file, redirect each to a
separate file and use the following form:

   proc = subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /C
"D:\372\pythonweb\mongoose-2.8.exe -root D:\New1\ 1>YourOutput.txt 2>&1"')


I hope it helps!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: If Not CGI...

2010-06-21 Thread Tim Chase

On 06/21/2010 07:40 AM, Victor Subervi wrote:

I would like to explore rewriting the shopping cart in Django.
The reality of the matter may make it difficult. Working
literally from the time I awake to when I go to sleep and not
having enough hours to complete everything I set for myself
makes it difficult,


The reason for using a framework is because web development is 
hard work -- the creators of these frameworks have gone before 
you doing much of the hard work so you can concentrate on your 
domain-specific tasks instead of the infrastructure.


When you want to drive across the country, do you begin by 
procuring all the land in between and then paving your roads? 
No...you leave that to people who do infrastructure full-time 
(and with lots of experience in this area).  You just get in your 
car and drive to your destination (your domain-specific task) and 
let the professionals deal with the infrastructure.  That common 
infrastructure investment is then shared between many users with 
widely different destinations.


So you may find that, by switching to a web framework, you 
actually end up *saving* time because you don't need to recreate 
all the work of building a framework.  Given that tens of 
thousands of development hours (by some very smart & very 
experienced people) have gone into some of the big-name 
frameworks such as Django, those are tens-of-thousands of hours 
that *you* don't have to spend.


-tkc


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


Re: deduping

2010-06-21 Thread Peter Otten
dirknbr wrote:

> Hi
> 
> I have 2 files (done and outf), and I want to chose unique elements
> from the 2nd column in outf which are not in done. This code works but
> is not efficient, can you think of a quicker way? The a=1 is just a
> redundant task obviously, I put it this way around because I think
> 'in' is quicker than 'not in' - is that true?
> 
> done_={}
> for line in done:
> done_[line.strip()]=0
> 
> print len(done_)
> 
> universe={}
> for line in outf:
> if line.split(',')[1].strip() in universe.keys():
> a=1
> else:
> if line.split(',')[1].strip() in done_.keys():
> a=1
> else:
> universe[line.split(',')[1].strip()]=0

Instead of

if key in some_dict.keys():
#...

which converts the keys in the dictionary to a list and then performs an 
O(N) lookup on that list you should use

if key in some_dict:
#...

which doesn't build a list and looks up the key in constant time.

Peter

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


Re: deduping

2010-06-21 Thread Dave Angel

dirknbr wrote:

Hi

I have 2 files (done and outf), and I want to chose unique elements
from the 2nd column in outf which are not in done. This code works but
is not efficient, can you think of a quicker way? The a=1 is just a
redundant task obviously, I put it this way around because I think
'in' is quicker than 'not in' - is that true?

done_={}
for line in done:
done_[line.strip()]=0

print len(done_)

universe={}
for line in outf:
if line.split(',')[1].strip() in universe.keys():
a=1
else:
if line.split(',')[1].strip() in done_.keys():
a=1
else:
universe[line.split(',')[1].strip()]=0

Dirk

  
Where you have a=1, one would normally use the "pass" statement.  But 
you're wrong that 'not in' is less efficient than 'in'.  If there's a 
difference, it's probably negligible, and almost certainly less than the 
extra else clause you're forcing here.


When doing an 'in', do *not* use the keys() method, as you're replacing 
a fast lookup with a slow one, not to mention the time it takes to build 
the keys() list each time.


In both these cases, you can use a set, rather than a dict.  And there's 
no need to test whether the item is already in the set, just put it in 
again.


Changing all that, you'll wind up with something like (untested)

done_set = set()
universe = set()
for line in done:
   done_set.add(line.strip())
for line in outf:
   item = line.split(',')[1].strip()
   if item not in done_set
   universe.add(item)


DaveA

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


Re: deduping

2010-06-21 Thread python
Use a set instead of a dictionary for done keys?

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


Re: Is this make sence? Dynamic assembler for python

2010-06-21 Thread Dave Angel

Steven D'Aprano wrote:

On Mon, 21 Jun 2010 03:00:12 -0400, Dave Angel wrote:


  

But the OP said of his friend:

"He dynamically generates mashine code and call that from python."

I took that to mean he dynamically generated machine code, not that he
hired some human to do it.




Well, I suppose if his friend is a robot or alien intelligence, you could 
very well be right. *wink* 

Otherwise, whether his friends writes the assembly, or he hires someone 
to do it, what's the difference?


(If you follow the OP's link to the code, it seems fairly clear to me 
that it uses hand-written assembly code.)



  
My fault.  I didn't follow the link.  I assumed the words in all those 
messages were clear enough.  So his acquaintance is writing an 
assembler, not a code generator.


DaveA

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


Re: deduping

2010-06-21 Thread Thomas Lehmann
> universe={}
> for line in outf:
>     if line.split(',')[1].strip() in universe.keys():
>         a=1
>     else:
>         if line.split(',')[1].strip() in done_.keys():
>             a=1
>         else:
>             universe[line.split(',')[1].strip()]=0
>

I can not say too much because I don't see what is processed
but what I can say is: "line.split(',')[1].strip()" might be
called three times so I would do it once only. And I would write
it like this:

for line in outf:
key = line.split(',')[1].strip()
if not (key in universe.keys()):
if not (key in done_.keys()):
universe[key] = 0

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


Decode II (more complex)

2010-06-21 Thread Thomas Lehmann
Hi all,

I have written a small python xmlrpc server which checks logfiles of a
build
sending notifications to the responsible teams. On a machine I'm
forced to
a problem with one logfile with special characters inside generated by
a
gnu compiler.

Using cheetah for generating the HTML mail I get a:
('ascii' codec can't decode byte 0xe2 in position 25868: ordinal not
in range(128))

I have found characters like this:
 error: expected type-specifier before â<80><98>Assocâ<80><99>
 (copy and paste of a linux console)

I have tried different mechanism but very probably I do not understand
how the decode/encode functionality is working. Can somebody help me
to get out of that problem?

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


Re: If Not CGI...

2010-06-21 Thread Victor Subervi
I really can't begin to thank you guys enough. Great information, goes
without saying. A lot to consider. I would like to explore rewriting the
shopping cart in Django. The reality of the matter may make it difficult.
Working literally from the time I awake to when I go to sleep and not having
enough hours to complete everything I set for myself makes it difficult, to
say the least, although I love what I do. (My main work is in the arts, not
programming. That just pays the bills.) Again, thank you all so much, and I
will explore and consider my options.
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


deduping

2010-06-21 Thread dirknbr
Hi

I have 2 files (done and outf), and I want to chose unique elements
from the 2nd column in outf which are not in done. This code works but
is not efficient, can you think of a quicker way? The a=1 is just a
redundant task obviously, I put it this way around because I think
'in' is quicker than 'not in' - is that true?

done_={}
for line in done:
done_[line.strip()]=0

print len(done_)

universe={}
for line in outf:
if line.split(',')[1].strip() in universe.keys():
a=1
else:
if line.split(',')[1].strip() in done_.keys():
a=1
else:
universe[line.split(',')[1].strip()]=0

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


Re: start and kill process by command promt

2010-06-21 Thread Giampaolo Rodolà
There's no need to use taskill.exe;
keep a reference of the subprocess.Popen() object around and use its
kill() method instead.


--- Giampaolo
http://code.google.com/p/pyftpdlib
http://code.google.com/p/psutil


2010/6/21 shanti bhushan :
> On Jun 21, 10:41 am, shanti bhushan  wrote:
>> Hi,
>> I want to invoke local wen server named mogoose, and kill it after
>> some time by python script.
>> Then i want to change the argument and invoke it again.
>>
>> I am able to do this with below code.
>>
>> import subprocess
>> import time
>>
>> def invoke_server1():
>>     s1ret=subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /C "D:
>> \372\pythonweb\mongoose-2.8.exe >YourOutput.txt"')
>>     print s1ret
>>
>> def invoke_server2():
>>     s2ret=subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /C "D:
>> \372\pythonweb\mongoose-2.8.exe -root D:\New1\ >YourOutput.txt"')
>>     print s2ret
>> def invoke_server3():
>>     s3ret=subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /C "D:
>> \372\pythonweb\mongoose-2.8.exe -root D:\New2\ >YourOutput.txt"')
>>     print s3ret
>>
>> def kill_server():
>>     killret=subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /c
>> "taskkill /F /IM mongoose-2.8.exe >YourOutput1.txt"')
>>     print killret
>>
>> kill_server()
>> time.sleep(5)
>> invoke_server1()
>> time.sleep(5)
>> kill_server()
>> time.sleep(5)
>> invoke_server2()
>> time.sleep(5)
>> kill_server()
>> time.sleep(10)
>> invoke_server3()
>> time.sleep(5)
>> kill_server()
>>
>> This works fine in python26 ,but when i use this code in one testing
>> tool, server is invoked 1st time properly.but for second time it says
>> process already occupied cannot access the process. I am not able to
>> understand why subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /c
>> "taskkill /F /IM mongoose-2.8.exe  is not working second time .Please
>> guide me in this respect.
>>
>> Or please tell me is there any other way,to do same thing with python
>> script that will start and kill windows process for my requirement.
>>
>> Regards
>> Bhushan
>
> i am getting following error message
> "process cannot acces the file because it is being used by other
> process"
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is this make sence? Dynamic assembler for python

2010-06-21 Thread DivX
On 20 lip, 12:57, DivX  wrote:
> On 20 lip, 12:46, Steven D'Aprano 
>
>
>
>
> cybersource.com.au> wrote:
> > On Sun, 20 Jun 2010 03:19:48 -0700, DivX wrote:
> > > On 20 lip, 02:52, Steven D'Aprano  > > cybersource.com.au> wrote:
> > [...]
> > >> I think that mixing assembly and python is a gimmick of very little
> > >> practical significance. If you really need the extra performance, check
> > >> out PyPy, Cython, Pyrex and Psyco.
>
> > >> --
> > >> Steven
>
> > > I can agree with you about most of the arguments, but why he continues
> > > to developing it. What he sees and we do not see?
>
> > Why ask us? You should ask him.
>
> > --
> > Steven
>
> Be sure I will ask him, but before, I wanted to know your opinions
> about it. Hear arguments on both sides...

Where I wrote that he was my friend? But that is not the point, I send
him a mail and here is answer:

"I know that writing assembly code is hard but when you want some
simple algorithm like algorithms in image processing or in digital
signal processing you must write functions in C and then write wrapper
to call that from python because it’s slow to do all work from python.
And then if you want to support windows and Linux you will spend more
time about compiling and see if everything works correctly than on
algorithms.

Another thing is that when you have assembler now you can write some
small C compiler so that you don’t have to write assembly language.
It’s doesn’t matter if gcc will produce better code it’s enough to
achieve speed that you are satisfied  and you don’t have to worry
about how to write extensions for Python.

Also many people use intrinsic functions to achieve more speed when
they need. I think programming using intrinsic functions is like using
sse instructions directly.

Best regards,

Tahir"

So, thanks people for your opinions and arguments...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: constructing an object from another instance of the same class

2010-06-21 Thread Carl Banks
On Jun 18, 3:55 pm, Christoph Groth  wrote:
> Bruno Desthuilliers  writes:
> > Anyway: the simplest solution here is to replace the call to your Base
> > class with a call to a factory function. I'd probably go for something
> > like (Q&D untested code and other usual warnings) :
>
> > (...)
>
> Yeah, that will do what I want.
>
> My confusion arose from the expectation that there had to be some
> mechanism to do the conversion automatically.  But actually a simple
>
> def bar(arg):
>     if not isinstance(arg, Base):
>         arg = Base(arg)
>         # Do something with arg.
>
> is a simple and explicit solution of the problem.

What if someone wants to call bar with an argument that mimics a Base
but isn't a subclass?  Your function would try to convert it to an
actual Base.

Something to think about before you make a habit of this.


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


Re: setup server from scratch (with or without apache?)

2010-06-21 Thread Christian Heimes
> I will only have one IP address and only port 443.
> 
> 1.) What alternatives would exist compared to apache / mod_python

You can use a combination of mod_proxy and mod_rewrite to set up a
forwarding proxy in your Apache server. Let Apache deal with SSL,
virtual hosting etc. Then bind your application to a local port (e.g.
localhost:8080) and configure the Apache proxy to rewrite your requests.
Pound offers a similar setup.

Christian

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


Re: process cannot access the file because it is being used by other process

2010-06-21 Thread shanti bhushan
On Jun 21, 2:15 pm, Tim Golden  wrote:
> On 21/06/2010 09:23, shanti bhushan wrote:
>
> > i am using below code ,it works fine on ordinary python 26 ,but when i
> > use this script in my python testing tool it gives me message "process
> > cannot access the file because it is being used by other process" for
> > the second time invoking of mongoose server.
> > Please help me in handling this exception.
>
> Before I make any suggestions on the code, I might suggest that you
> learn to wait a little. You sent three pretty much identical messages
> within the space of three hours. Believe me: if people aren't helping,
> it's not because they haven't seen your first message. Or the follow-up.
> Or the one after that. It's because they don't know the answer, or
> haven't the time to answer. Or aren't in the same timezone as you and
> so haven't woken up yet!
>
> > def invoke_server2():
> >      file = open("mongoose-2.8.exe", "r")
> >      try:
> >          proc = subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /C "D:
> > \372\pythonweb\mongoose-2.8.exe -root D:\New1\>YourOutput.txt"')
> >      except OSError:
> >          print "os error"
> >          file.close()
> >          sys.exc_clear()
> >          os.remove("mongoose-2.8.exe")
>
> OK. I'm not sure what you're achieving with the open ("mongoose...") line
> and its corresponding close. In fact, I can't work out what the whole
> exception block is achieving. I actually had to go and look up what
> sys.exc_clear is doing -- and I don't think it's doing what you think
> it's doing. You appear to be trapping an OS error, such as file-not-found
> or access-denied, by trying to ignore the error and then deleting the
> server
> itself!
>
> Let's straighten some stuff out. First your Popen line could almost
> certainly
> be simplified to this:
>
> 
> import subprocess
>
> with open ("YourOutput.txt", "w") as outf:
>    proc = subprocess.Popen (
>      [r"D:\372\pythonweb\mongoose-2.8.exe", "-root", r"D:\New1"],
>      stdout=outf
>    )
>
> 
>
> and to kill the proc, you can just call proc.kill ()
>
> Does that take you forward? Are you still seeing the "Cannot access file..."
> errors?
>
> TJG

i used below code

import subprocess
import time
def invoke_server1():
proc = subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /C "D:
\372\pythonweb\mongoose-2.8.exe -root D:\New1\"')


invoke_server1()


time.sleep(10)
proc.kill()

this code only invokes the server but is not killing after 10 seconds.

my purpose is invoke server many times with different argument and
kill it.
but when ever  i use subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /
c
"taskkill /F /IM mongoose-2.8.exe >YourOutput1.txt"'
this gives me error "process cannot access the file because it is
being used by other process"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is this make sence? Dynamic assembler for python

2010-06-21 Thread Steven D'Aprano
On Mon, 21 Jun 2010 03:00:12 -0400, Dave Angel wrote:


> But the OP said of his friend:
> 
> "He dynamically generates mashine code and call that from python."
> 
> I took that to mean he dynamically generated machine code, not that he
> hired some human to do it.


Well, I suppose if his friend is a robot or alien intelligence, you could 
very well be right. *wink* 

Otherwise, whether his friends writes the assembly, or he hires someone 
to do it, what's the difference?

(If you follow the OP's link to the code, it seems fairly clear to me 
that it uses hand-written assembly code.)



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


Re: Pick items from list with probability based upon property of list member ?

2010-06-21 Thread southof40
On Jun 20, 10:58 pm, Cameron Simpson  wrote:
> On 20Jun2010 12:44, Stefan Behnel  wrote:
> | southof40, 20.06.2010 12:19:
> | >I have list of of N Vehicle objects - the only possible vehicles are
> | >cars, bikes, trucks.
> | >
> | >I want to select an object from the list with a probability of : cars
> | >0.7, bikes 0.3, trucks 0.1.
> | >
> | >I've currently implemented this by creating another list in which each
> | >car object from the original list appears 7 times, each bike 3 times
> | >and each truck once. I then pick at random from that list.
> | >
> | >This works but seems very clunky to me.
> |
> | Why? It's a very simple, generic, easy to understand and fast
> | solution to the problem.
>
> Only 3 out of 4, if you want to be precise in your selections.
> Supposing he wants probabilities 0.7432, 0.3765, 0.1087654 ?
> The required list needs to be Very Long to achieve an accurate
> representation, and thus Very Slow to construct/populate.
>

Yes you're spot on here. Although I have used simple probabilities it
occurred to me that the list for my current method would get pretty
big if I changed the probs to be a little more refined.

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


Re: Pick items from list with probability based upon property of list member ?

2010-06-21 Thread southof40
On Jun 20, 11:27 pm, Mel  wrote:
> southof40 wrote:
> > I have list of of N Vehicle objects - the only possible vehicles are
> > cars, bikes, trucks.
>
> > I want to select an object from the list with a probability of : cars
> > 0.7, bikes 0.3, trucks 0.1.
>
> > I've currently implemented this by creating another list in which each
> > car object from the original list appears 7 times, each bike 3 times
> > and each truck once. I then pick at random from that list.
>
> This seems about right.  It's like a lottery where the more likely
> winners have more tickets, but all tickets are the same.  Pick one to
> pick the winner.
>
> There's a very expensive, but flexible technique that effectively gives
> some tickets a better chance than others.  You have to examine each
> ticket individually, so this algorithm burns random numbers like
> kindling:
>
> import random
>
> #===
> def weighted_choice (candidates, weight):
>     chosen = None
>     total = 0
>     for c in candidates:
>         w = weight (c)
>         total += w
>         if random.randint (0, total-1) < w:
>             chosen = c
>     return chosen
> #===
>
> def test_weight (c):
>     return {'c':7, 'b':3, 't':1}[c]
>
> def item_count (s, target):
>     return sum (1 for x in s if x==target)
>
> test_candidates = 'c'*100 + 'b'*100 + 't'*100
>
> for i in xrange (10):
>     test = [weighted_choice (test_candidates, test_weight) for k in xrange 
> (100)]
>     for x in 'cbt':
>         print x, item_count (test, x), '\t',
>     print
>
>         Mel.

I like this - makes altering the probabilities straightforward and
this is something I may want to do.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pick items from list with probability based upon property of list member ?

2010-06-21 Thread southof40
On Jun 20, 10:55 pm, Rob Williscroft  wrote:
> southof40 wrote in news:da3cc892-b6dd-4b37-a6e6-
> b606ef967...@t26g2000prt.googlegroups.com in gmane.comp.python.general:
>
> > I have list of of N Vehicle objects - the only possible vehicles are
> > cars, bikes, trucks.
>
> > I want to select an object from the list with a probability of : cars
> > 0.7, bikes 0.3, trucks 0.1.
>
> Aside, all your probabilities add up to 1.1, they should add up to 1.
>
> > I've currently implemented this by creating another list in which each
> > car object from the original list appears 7 times, each bike 3 times
> > and each truck once. I then pick at random from that list.
>
> Aside, so 7 / 11 bikes, 3 / 11 cars and 1 / 11 trucks, are your
> actual probabilities.
>
> But to answer your question, you could create 3 list, and then
> pick the list you draw from based on a random number then pick
> the item from the list based on another random number:
>
> r = ( random() * 11 )
>
> if r  < 1:
>   picklist = truck_list
> elif r  < 4:
>   picklist = bike_list
> else:
>   picklist = car_list
>
> # now pick the final item from pick list.
>
> Rob.

thanks also - nice clean solution
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: setup server from scratch (with or without apache?)

2010-06-21 Thread Bruno Desthuilliers

News123 a écrit :

Hi,


So far I never really had to ask this question and this is also, why I
am stil a little shaky on this topic:

So far the typical LAMP server existed already and contained already a
lot of existing PHP web applications, which I couldn't remove.
Therefore I just used mod_python to implement some minor functionality
next to it.

Now I have the opportunity to setup a server from scratch.
90% of the content will be non visual content over https with client AND
server certificates.
Access privileges will depend on the client certificate.

I will only have one IP address and only port 443.

1.) What alternatives would exist compared to apache / mod_python


wsgi + any wsgi-compatible web server.


2.) What disadvantage would I have using apache and mod_python compared
to other solutions


Err... I guess you already got the most important answer on this !-)


3.) What's the stability / security aspect of other solutions,
especially concerning client / server certificates


Can't tell, sorry.


4.) How could I prepare for the case, that customer might lateron
require PHP? (not very probably, but who knows.


Just make sure you can run PHP with the web server you choose.

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


Re: Pick items from list with probability based upon property of list member ?

2010-06-21 Thread southof40
On Jun 20, 10:53 pm, Steven D'Aprano  wrote:
> On Sun, 20 Jun 2010 03:19:55 -0700, southof40 wrote:
> > I have list of of N Vehicle objects - the only possible vehicles are
> > cars, bikes, trucks.
>
> > I want to select an object from the list with a probability of : cars
> > 0.7, bikes 0.3, trucks 0.1.
>
> That adds to a probability of 1.1, which is impossible.
>
> I'm going to assume that bikes is a typo and should be 0.2.
>
> cars = [car1, car2]
> bikes = [bike1, bike2, bike3, bike4, bike5, bike6]
> trucks = [truck1, truck2, truck3, truck4]
>
> x = random.random()
> if x < 0.7:
>     return random.choice(cars)
> elif x < 0.9:
>     return random.choice(bikes)
> else:
>     return random.choice(trucks)
>
> But surely this is not physically realistic? The probability of selecting
> a car would normally depend on the number of cars, and not be set before
> hand.
>
> --
> Steven

Nice solution thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pick items from list with probability based upon property of list member ?

2010-06-21 Thread southof40
Oh yes as several have pointed out there was a typo in my original
question ... I can only blame 'toolongatscreenitis' !
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pick items from list with probability based upon property of list member ?

2010-06-21 Thread southof40
Thanks to everybody ... as usual on c.l.p I'm blown away by the
knowledge and skills ! I've added some replies/clarifications to other
posts but thanks again to you all.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is this make sence? Dynamic assembler for python

2010-06-21 Thread Carl Banks
On Jun 21, 12:00 am, Dave Angel  wrote:
> Steven D'Aprano wrote:
> > On Sun, 20 Jun 2010 18:21:43 -0400, Dave Angel wrote:
>
> >> Something's intrinsically wrong with the argument made in this thread
> >> against generating assembly code.  That's exactly what happens every
> >> time you write code in C.
>
> > I don't know whether C compilers generate assembly mnemonics or direct
> > machine code, but the distinction for this argument is irrelevant.
>
> > The argument in this thread is that it's not worth the *human coder*
> > writing assembly, not that no assembly code is involved in the process
> > anywhere.
>
> But the OP said of his friend:
>
> "He dynamically generates mashine code and call that from python."
>
> I took that to mean he dynamically generated machine code, not that he hired 
> some human to do it.

Well we know what you meant, but he did post a snippet of the code
showing handwritten assembly, from which the machine code is
dynamically generated.

Inline assembly not too useful for general purpose Python programming,
but I'm sure there's a time and place for it.

I wonder how easy it'd be to bundle a small C compiler.


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


Re: constructing an object from another instance of the same class

2010-06-21 Thread Jean-Michel Pichavant

Christoph Groth wrote:

Dear all,

sometimes it is handy to have a function which can take as argument
anything which can be converted into something, e.g.

def foo(arg):
arg = float(arg)
# ...

I would like to mimic this behavior of float for a user-defined type,
e.g. 


def bar(arg):
arg = My_type(arg)
# ...

Now I wonder what is the most pythonic way to write the __init__ method
of My_type?  The following comes to my mind:

class My_type:
def __init__(self, other):
if isinstance(other, type(self)):
self.a = other.a
self.b = other.b
return
# initialize self in some other way

It seems to me that in this way I might get problems when I pass an
instance of Derived_from_my_type to bar, as it will become an instance
of My_type.

What is a good way to express this?  In C++ (which I know better than
python) I would make bar accept a const reference to My_type.  Then I
could use it directly with instances of My_type, Derived_from_my_type
and other types which can be converted into My_type.

thanks
Christoph

  
There is no need to do such thing in python most of the time. Python is 
strongly typed and won't change the type of an object for you (unlike 
perl for instance). That means no matter where you are in your code, you 
should know the exact type of your objects.


If you don't, that means you are assigning to the *same name*, object of 
different types. You don't want to that.


One possible solution is to make your class also a factory class:
python 2.5

class My_type(object):
   @classmethod
   def fromInt(cls, anInteger):
  """Return a My_type instance given an integer"""
  pass

   @classmethod
   def fromString(cls, aString):
  """Return a My_type instance given an integer"""
  pass


Try to resist the temptation of making one constructor that would handle 
any type of parameter, it may look stylish,  but from the "I don't care 
about the parameter type" you will soon experience the "I don't know 
about the parameter type" which is problematic when debugging / 
maintaining the code.


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


Re: process cannot access the file because it is being used by other process

2010-06-21 Thread Tim Golden

On 21/06/2010 09:23, shanti bhushan wrote:

i am using below code ,it works fine on ordinary python 26 ,but when i
use this script in my python testing tool it gives me message "process
cannot access the file because it is being used by other process" for
the second time invoking of mongoose server.
Please help me in handling this exception.


Before I make any suggestions on the code, I might suggest that you
learn to wait a little. You sent three pretty much identical messages
within the space of three hours. Believe me: if people aren't helping,
it's not because they haven't seen your first message. Or the follow-up.
Or the one after that. It's because they don't know the answer, or
haven't the time to answer. Or aren't in the same timezone as you and
so haven't woken up yet!


def invoke_server2():
 file = open("mongoose-2.8.exe", "r")
 try:
 proc = subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /C "D:
\372\pythonweb\mongoose-2.8.exe -root D:\New1\>YourOutput.txt"')
 except OSError:
 print "os error"
 file.close()
 sys.exc_clear()
 os.remove("mongoose-2.8.exe")


OK. I'm not sure what you're achieving with the open ("mongoose...") line
and its corresponding close. In fact, I can't work out what the whole
exception block is achieving. I actually had to go and look up what
sys.exc_clear is doing -- and I don't think it's doing what you think
it's doing. You appear to be trapping an OS error, such as file-not-found
or access-denied, by trying to ignore the error and then deleting the 
server

itself!

Let's straighten some stuff out. First your Popen line could almost 
certainly

be simplified to this:


import subprocess

with open ("YourOutput.txt", "w") as outf:
  proc = subprocess.Popen (
[r"D:\372\pythonweb\mongoose-2.8.exe", "-root", r"D:\New1"],
stdout=outf
  )



and to kill the proc, you can just call proc.kill ()


Does that take you forward? Are you still seeing the "Cannot access file..."
errors?

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


process cannot access the file because it is being used by other process

2010-06-21 Thread shanti bhushan
Hi,

i am using below code ,it works fine on ordinary python 26 ,but when i
use this script in my python testing tool it gives me message "process
cannot access the file because it is being used by other process" for
the second time invoking of mongoose server.
Please help me in handling this exception.

def invoke_server2():
file = open("mongoose-2.8.exe", "r")
try:
proc = subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /C "D:
\372\pythonweb\mongoose-2.8.exe -root D:\New1\ >YourOutput.txt"')
except OSError:
print "os error"
file.close()
sys.exc_clear()
os.remove("mongoose-2.8.exe")

def invoke_server3():
file = open("mongoose-2.8.exe", "r")
try:
proc = subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /C "D:
\372\pythonweb\mongoose-2.8.exe -root D:\New2\ >YourOutput.txt"')
except OSError:
print "os error"
file.close()
sys.exc_clear()
os.remove("mongoose-2.8.exe")


def kill_server():
killret=subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /c
"taskkill /F /IM mongoose-2.8.exe >YourOutput1.txt"')
print killret


invoke_server1()
time.sleep(5)
kill_server()
time.sleep(5)
invoke_server2()
time.sleep(5)
kill_server()
time.sleep(5)
invoke_server3()
time.sleep(5)
kill_server()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tkInter Listbox question

2010-06-21 Thread eb303
On Jun 21, 7:36 am, Anthony Papillion  wrote:
> So I'm trying to add a Listbox to my window. I want it to be the width
> of my window and the height of my window.  I'm using the following
> code ('root' is my toplevel window):
>
> gsItems = Listbox(root, width=root.winfo_width(),
> height=root.winfo_height())
> gsItems.pack()
>
> While you would think this code would set the height and width of
> gsItems to the height and width of root, it doesn't. Instead it puts a
> little tiny listbox in the middle of the form.
>
> I've been Googling for almost an hour. Can anyone help me figure this
> out? Point me in the right direction?
>
> Thanks!
> Anthony

The problem you have in your script is probably simple: at the time
when you put the listbox in it, the windows still contains nothing, or
very little. And what you're doing is to take its _current_ size and
assign it to the listbox. So the listbox will be very small, since at
the time you ask for its dimensions, the window itself is very small.

Basically, you're taking the wrong approach there: you should put the
listbox in the window telling it to adapt its size to its parent
container, and then resize the window if you want to.

The first step is done via the geometry manager, which can be pack or
grid. Please refer to your documentation to know how to use them to
make a widget adapt its size to its container. The documentation I use
myself is the plain tcl/tk documentation, that you can find at
http://www.tcl.tk/man/tcl8.5/contents.htm. The documentation for pack
is at http://www.tcl.tk/man/tcl8.5/TkCmd/pack.htm and the one for grid
at http://www.tcl.tk/man/tcl8.5/TkCmd/grid.htm. Please note this
documentation might not be suitable for you, as it is not a Python/
Tkinter documentation.

The second step can be done with the method called geometry on your
window. The size you want should then be specified as a string
'x'.

Here is an example, using the pack geometry manager:

from Tkinter import *
root = Tk()
gsItems = Listbox(root)
gsItems.pack(fill=BOTH, expand=TRUE)
root.geometry('400x300')
root.mainloop()

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


Re: plac 0.5 is out!

2010-06-21 Thread Andre Alexander Bell
On 06/21/2010 05:38 AM, Michele Simionato wrote:
>>> A few weeks ago I presented on this list my most recent effort, plac.
>>>  http://micheles.googlecode.com/hg/plac/doc/plac_ext.html
>>
>> But this one is broken. :(
> 
> Aagh! The good one is 
> http://micheles.googlecode.com/hg/plac/doc/plac_adv.html.
> Luckily the one I posted on PyPI is correct.

Oh. Sorry, I was too lazy to search on PyPI. I just recognized the link
was broken and the link to the advanced document within the basic
document was broken too. So I did not search around.

> Indeed this is already possible and covered in the advanced document.
> See the section containers of commands:
> 
> http://micheles.googlecode.com/hg/plac/doc/plac_adv.html#containers-of-commands

Great. :) I'll read it...


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


Re: Generator (re-)definition within a loop

2010-06-21 Thread Pierre Reinbold
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On 06/18/2010 11:48 PM, Terry Reedy wrote:
> On 6/18/2010 3:57 PM, Pierre Reinbold wrote:
>
>> def genexp_product(*args):
>>  pools = map(tuple, args)
>>  result = [[]]
>>  for pool in pools:
>>  result = (x+[y] for x in result for y in pool)
> 
> The name binding in the first for-clause in a genexp is handled slightly
> differently from that in subsequent for-clauses. I suspect that this is
> relevant here. This code rebinds 'result' to a new generator *without*
> running running the previously bound 'result' generator.
> 
>>  for prod in result:
>>  yield tuple(prod)
> 
> This runs N nested generators, but which?
> 
>> but this do not work as expected:
>>
> print list(product("ABC", "xy"))
>> [('A', 'x'), ('A', 'y'), ('B', 'x'), ('B', 'y'), ('C', 'x'), ('C', 'y')]
> print list(genexp_product("ABC", "xy"))
>> [('x', 'x'), ('x', 'y'), ('y', 'x'), ('y', 'y')]
>>
>> My question is why ? What is the final generator "result" at the end of
>> the main loop in genexp_product ? How is it build exactly ? Is this an
>> effet of some sort of "lazy evaluation" ?
> 
> That and/or a namespace issue.
> 
> Let's apply Reedy's Rule: when you have trouble understanding a function
> expression, replace it with the (near) equivalent def statement. (Among
> other advantages, one can insert print calls!)
> 
> Genexps, like lambdas, are specialized function expressions.
> 
> def augment(s_of_s, s):
>  for x in s_of_s:
>   for y in s:
>yield x+[y]
> 
> def gen_product(*args):
> pools = map(tuple, args)
> result = [[]]
> for pool in pools:
> result = augment(result,pool)
> for prod in result:
> yield tuple(prod)
> 
> print(list(gen_product("ABC", "xy")))
> 
 #3.1
> [('A', 'x'), ('A', 'y'), ('B', 'x'), ('B', 'y'), ('C', 'x'), ('C', 'y')]

Very instructive post ! Thank you !

Just trying to understand, I have apply the Reedy's Rule in an attempt
to reproduce the same behaviour as the generator expression. The idea, I
guess, is to (re-)define the generator function inside the loop.

My first try gives this:

def badgen_product1(*args, **kwds):
pools = map(tuple, args)
result = [[]]
for pool in pools:
def result():
for x in result():
for y in pool:
yield x+[y]
for prod in result():
yield tuple(prod)

But this does not reproduce the generator expression, it leads naturally
to an infinite recursion (which is what I expected first for the
generator expression btw)

for x in result():
RuntimeError: maximum recursion depth exceeded

Another try to avoid infinite recursion:

def badgen_product2(*args, **kwds):
pools = map(tuple, args)
result = [[]]
for pool in pools:
def augments():
for x in result:
for y in pool:
yield x+[y]
result = augments()
for prod in result:
yield tuple(prod)

And this one gives the infamous:

for x in result:
ValueError: generator already executing

Which seems to indicate that the lazy evaluation leads to eventually
bind everything to the same generator.

So, neither of my attempts reproduce the behaviour of the generator
expression. What do I miss here ?

Thank for your help,


3.14r
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkwfFNIACgkQ9D25xYOIvisX3ACgu3BjwvepFDcbuUbtvaUc10eD
pJkAnA3cqPEM60kYfrcNLI6qmFzHvtRe
=LxCp
-END PGP SIGNATURE-
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is this make sence? Dynamic assembler for python

2010-06-21 Thread Dave Angel

Steven D'Aprano wrote:

On Sun, 20 Jun 2010 18:21:43 -0400, Dave Angel wrote:

  

Something's intrinsically wrong with the argument made in this thread
against generating assembly code.  That's exactly what happens every
time you write code in C.



I don't know whether C compilers generate assembly mnemonics or direct 
machine code, but the distinction for this argument is irrelevant.


The argument in this thread is that it's not worth the *human coder* 
writing assembly, not that no assembly code is involved in the process 
anywhere.



  

But the OP said of his friend:

"He dynamically generates mashine code and call that from python."

I took that to mean he dynamically generated machine code, not that he hired 
some human to do it.

DaveA


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