Re: Article on the future of Python

2012-09-28 Thread Bob Martin
in 681910 20120927 131113 Devin Jeanpierre  wrote:
>On Thu, Sep 27, 2012 at 2:13 AM, Steven D'Aprano
> wrote:
>> On Tue, 25 Sep 2012 09:15:00 +0100, Mark Lawrence wrote:
>> And a response:
>>
>> http://data.geek.nz/python-is-doing-just-fine
>
>Summary of that article:
>
>"Sure, you have all these legitimate concerns, but look, cake!"

Quote : "This piece argues that Python is an easy-to-learn 
language that where you can be almost immediately productive in."
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Article on the future of Python

2012-09-28 Thread Dwight Hutto
>>Summary of that article:
>>
>>"Sure, you have all these legitimate concerns, but look, cake!"
>
> Quote : "This piece argues that Python is an easy-to-learn
> language that where you can be almost immediately productive in."

It is, but so is every other language. "hello world" is the
standard... follow the syntax, import/include the appropriate library
functions, and create your own to use them.




-- 
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: REST code-golf: How concisely can you expose and consume services?

2012-09-28 Thread Chris Angelico
On Fri, Sep 28, 2012 at 3:41 PM, Alec Taylor  wrote:
> web2py (7 lines): https://gist.github.com/3798093

I love the idea, even though I shan't be entering. Code golf is awesome fun!

My latest golf game involved importing code comments and text-file
annotations into autodoc markup... with two one-liners. And it only
needed two because I did it in two parts (wasn't even aware of the
text file until I'd done the code comments).

Of course, a real RESTful API is likely to have rather more guts in
it, but it's cool how little you need of structure!

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


how to run shell command like "<

2012-09-28 Thread 叶佑群

Hi, all,

I have the shell command like this:

   sfdisk -uM /dev/sdb << EOT
   ,1000,83
   ,,83
   EOT


I have tried subprocess.Popen, pexpect.spawn and os.popen, but none 
of these works, but when I type this shell command in shell, it is works 
fine. I wonder how to emulate this type of behavior in python , and if 
someone can figure out the reason why?


The sample code of subprocess.Popen is:

command = ["sfdisk", "-uM",  target, "<-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reducing cache/buffer for faster display

2012-09-28 Thread Chris Angelico
On Fri, Sep 28, 2012 at 10:05 AM, Rikishi42  wrote:
> The scripts in question only increase numbers. But should that not be the
> case, solutions are simple enough. The numbers can be formatted to have a
> fixed size. In the case of random line contents (a list of filesnames, say)
> it's enough to create an output function that is aware of the length of the
> previously printed line, and add enough spaces to the current one to wipe
> exess content.

Yep, that's a pretty effective way to do it. One simple method to it
is to format the whole string as a single whole, then left justify it
in a field of (say) 79 characters, and output that:

msg = "Progress: %d%% (%d/%d)... %s" % (done*100/total, done, total,
current_file)
print msg.ljust(79)+"\r",
sys.stdout.flush()

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


Re: how to run shell command like "<

2012-09-28 Thread Kushal Kumaran
On Fri, Sep 28, 2012 at 1:15 PM, 叶佑群  wrote:
> Hi, all,
>
> I have the shell command like this:
>
> sfdisk -uM /dev/sdb << EOT
> ,1000,83
> ,,83
> EOT
>
>
> I have tried subprocess.Popen, pexpect.spawn and os.popen, but none of
> these works, but when I type this shell command in shell, it is works fine.
> I wonder how to emulate this type of behavior in python , and if someone can
> figure out the reason why?
>
> The sample code of subprocess.Popen is:
>
> command = ["sfdisk", "-uM",  target, "< ",", 1000, ",", "83", "\r\n",
> ",", ",", "83", "\r\n", "EOT", "\r\n"]
>
> pobj = subprocess.Popen (command, bufsize=1, \
> stderr=subprocess.PIPE, stdout=subprocess.PIPE)
>
> res = pobj.stderr.readline ()
> if res is not None and pobj.returncode != 0:
> observer.ShowProgress (u"对设备 %s 分区失败!" % target)
> return False
>

The "< and pexpect code is:
>
> child = pexpect.spawn ("sfdisk -uM /dev/sdb < child.sendline ()
> child.sendline ()
> child.sendline ()
>
> and os.popen like this:
>
> os.popen ("sfdisk -uM /dev/sdb <
> I tried "\r\n", and it doesn't work either.
>

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


Re: Article on the future of Python

2012-09-28 Thread Mark Lawrence

On 27/09/2012 20:08, Terry Reedy wrote:

On 9/27/2012 5:33 AM, Steven D'Aprano wrote:


Nevertheless, I think there is something here. The consequences are
nowhere
near as dramatic as jmf claims, but it does seem that replace() has
taken a
serious performance hit. Perhaps it is unavoidable, but perhaps not.

If anyone else can confirm similar results,


I already did, about a month ago, for windows. I think the actual
problem is with find, not replace (which does a find before the
replace). When I asked on pydev, Victor Stinner had no explanation, but
said he might look into it eventually. Others thought it not terribly
important since 7 times blazingly fast is still fast (in your example,
29 versus 3 microseconds per operation.

jmf wrapping a possible real issue with anti-3.3 crud did not help in
getting attention to the regression.

I also reported results of running stringbench.py on both 3.2 and 3.3 on
windows. Overall, Unicode is nearly as fast as bytes and 3.3 as fast as
3.2. Find/replace is the notable exception in stringbench, so it is an
anomaly. Other things are faster in 3.3.


I think this should be raised as a performance regression.


I agree, and Mark did it.



http://bugs.python.org/issue16061 and you should read it.  I've tried to 
really muddy the waters by opening this issue, and the python devs are 
giving out facts, how dare they!!!  It's just not my day is it? :(


--
Cheers.

Mark Lawrence.

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


"#!/usr/bin/env python" vs. "#!/usr/bin/python"?

2012-09-28 Thread Gilles
Hello

I've seen both shebang lines to run a Python script on a *nix host:

#!/usr/bin/env python
#!/usr/bin/python

What's the difference?

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


Re: "#!/usr/bin/env python" vs. "#!/usr/bin/python"?

2012-09-28 Thread Jussi Piitulainen
Gilles writes:

> #!/usr/bin/env python
> #!/usr/bin/python
> 
> What's the difference?

Not much if your python is /usr/bin/python: env looks for python and
finds the same executable.

When python is not /usr/bin/python but something else that is still
found by your system, /usr/bin/env still finds it.

For example, in a server where I work, python3 is installed as
something like /opt/python/3.2.2-gcc/bin/python3. There is no
/usr/bin/python3 at all, but "#! /usr/bin/env python3" works.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "#!/usr/bin/env python" vs. "#!/usr/bin/python"?

2012-09-28 Thread Roy Smith
In article <34va6856ocuas7jpueujscf3kdt7k44...@4ax.com>,
 Gilles  wrote:

> Hello
> 
> I've seen both shebang lines to run a Python script on a *nix host:
> 
> #!/usr/bin/env python
> #!/usr/bin/python
> 
> What's the difference?

The first one looks through your PATH to find the right python 
interpreter to run.  The second one is hard-wired to run /usr/bin/python.

If you only have a single copy of python installed, it doesn't really 
matter which you use.  But, you might as well get into the habit of 
using the /usr/bin/env flavor because it's more flexible.

I'm working on a number of different python projects.  For each one, I 
set up a new virtual environment using virtualenv.  This lets me run 
different versions of python in different projects, with different 
collections of installed packages (and possibly different versions).  I 
can only do this because I use the /usr/bin/env line in all my scripts.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "#!/usr/bin/env python" vs. "#!/usr/bin/python"?

2012-09-28 Thread Gilles
On Fri, 28 Sep 2012 06:57:28 -0400, Roy Smith  wrote:
>The first one looks through your PATH to find the right python 
>interpreter to run.  The second one is hard-wired to run /usr/bin/python.
>
>If you only have a single copy of python installed, it doesn't really 
>matter which you use.  But, you might as well get into the habit of 
>using the /usr/bin/env flavor because it's more flexible.

Thanks guys. I suspected that's what the difference was.
-- 
http://mail.python.org/mailman/listinfo/python-list


How to investigate web script not running?

2012-09-28 Thread Gilles
Hello

I'm trying to run my very first FastCGI script on an Apache shared
host that relies on mod_fcgid:
==
#!/usr/bin/python
from fcgi import WSGIServer
import cgitb

# enable debugging
cgitb.enable()

def myapp(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')])
return ['Hello World!\n']

WSGIServer(myapp).run()
==

After following a tutorial, Apache complains with the following when I
call my script:
==
Internal Server Error

The server encountered an internal error or misconfiguration and was
unable to complete your request.
==

Generally speaking, what tools are available to investigate issues
when running a Python web app?

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


Re: Article on the future of Python

2012-09-28 Thread rusi
On Sep 27, 5:11 pm, Devin Jeanpierre  wrote:
> On Thu, Sep 27, 2012 at 2:13 AM, Steven D'Aprano
>
>  wrote:
> > On Tue, 25 Sep 2012 09:15:00 +0100, Mark Lawrence wrote:
> > And a response:
>
> >http://data.geek.nz/python-is-doing-just-fine
>
> Summary of that article:
>
> "Sure, you have all these legitimate concerns, but look, cake!"
>
> -- Devin

My summary of the first (worried about python) article:
Python is about to miss the Bell's law bus:

http://en.wikipedia.org/wiki/Bell%27s_Law_of_Computer_Classes
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python source code easy to hack?

2012-09-28 Thread Mark Lawrence

On 28/09/2012 12:57, Jayden wrote:

Dear All,

I have a concern in developing commercial code with Python. Someone told me 
that its program can be easily hacked to get its source code. Is it really the 
case? Any way to protect your source code?

Thanks a lot!

Jayden



This question has been asked on numerous occasions so if you search the 
archives you're sure to get loads of answers.


--
Cheers.

Mark Lawrence.

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


Re: How to investigate web script not running?

2012-09-28 Thread Gilles
On Fri, 28 Sep 2012 13:37:36 +0200, Gilles  wrote:

>==
>Internal Server Error
>
>The server encountered an internal error or misconfiguration and was
>unable to complete your request.
>==

Looks like fcgi.py doesn't support WSGI:

Traceback (most recent call last):
File "hello.fcgi", line 2, in ?
from fcgi import WSGIServer
ImportError: cannot import name WSGIServer
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python source code easy to hack?

2012-09-28 Thread zig-zag

On 09/28/2012 02:17 PM, Mark Lawrence wrote:

On 28/09/2012 12:57, Jayden wrote:

Dear All,

I have a concern in developing commercial code with Python. Someone
told me that its program can be easily hacked to get its source code.
Is it really the case? Any way to protect your source code?

Thanks a lot!

Jayden



This question has been asked on numerous occasions so if you search the
archives you're sure to get loads of answers.



http://stackoverflow.com/questions/261638/how-do-i-protect-python-code
http://stackoverflow.com/questions/164137/how-do-i-deploy-a-python-desktop-application
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to investigate web script not running?

2012-09-28 Thread Michael Ross

On Fri, 28 Sep 2012 13:37:36 +0200, Gilles  wrote:


Hello

I'm trying to run my very first FastCGI script on an Apache shared
host that relies on mod_fcgid:
==
#!/usr/bin/python
from fcgi import WSGIServer
import cgitb

# enable debugging
cgitb.enable()

def myapp(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')])
return ['Hello World!\n']

WSGIServer(myapp).run()
==

After following a tutorial, Apache complains with the following when I
call my script:
==
Internal Server Error

The server encountered an internal error or misconfiguration and was
unable to complete your request.
==



Do it the other way around:

# cgitb before anything else
import cgitb
cgitb.enable()

# so this error will be caught
from fcgi import WSGIServer



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


Re: py2exe deal with python command line inside a program

2012-09-28 Thread Miki Tebeka
> sys.executable was printed out as ''C:\\Python25\\python.exe'', how
> can I make this work in executable package through py2exe?
Does http://www.py2exe.org/index.cgi/WhereAmI help?

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


Re: Article on the future of Python

2012-09-28 Thread Steven D'Aprano
On Fri, 28 Sep 2012 05:08:24 -0700, rusi wrote:

> On Sep 27, 5:11 pm, Devin Jeanpierre  wrote:
>> On Thu, Sep 27, 2012 at 2:13 AM, Steven D'Aprano
>>
>>  wrote:
>> > On Tue, 25 Sep 2012 09:15:00 +0100, Mark Lawrence wrote: And a
>> > response:
>>
>> >http://data.geek.nz/python-is-doing-just-fine
>>
>> Summary of that article:
>>
>> "Sure, you have all these legitimate concerns, but look, cake!"
>>
>> -- Devin
> 
> My summary of the first (worried about python) article: Python is about
> to miss the Bell's law bus:
> 
> http://en.wikipedia.org/wiki/Bell%27s_Law_of_Computer_Classes


Except that very concept is stupid. Mainframes have not be replaced. 
There are more mainframes around today than fifty years ago. 
Minicomputers too, only we don't call them minicomputers, we call them 
"servers".

In ten years time, there will be more desktop PCs around than now. Most 
of them will be in the 90% of the world that isn't America. And most of 
them will be laptops. But they'll be used as desktops too. Not everybody 
wants to read email on a device smaller than your hand, clumsily poking 
at a tiny virtual keyboard.

And anybody who thinks that Python can't run on tablets or smartphones 
hasn't been paying attention.


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


what is the difference between st_ctime and st_mtime one is the time of last change and the other is the time of last modification, but i can not understand what is the difference between 'change' and

2012-09-28 Thread 陈伟

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


Re: Article on the future of Python

2012-09-28 Thread rusi
On Sep 28, 5:54 pm, Steven D'Aprano  wrote:
> On Fri, 28 Sep 2012 05:08:24 -0700, rusi wrote:
> > On Sep 27, 5:11 pm, Devin Jeanpierre  wrote:
> >> On Thu, Sep 27, 2012 at 2:13 AM, Steven D'Aprano
>
> >>  wrote:
> >> > On Tue, 25 Sep 2012 09:15:00 +0100, Mark Lawrence wrote: And a
> >> > response:
>
> >> >http://data.geek.nz/python-is-doing-just-fine
>
> >> Summary of that article:
>
> >> "Sure, you have all these legitimate concerns, but look, cake!"
>
> >> -- Devin
>
> > My summary of the first (worried about python) article: Python is about
> > to miss the Bell's law bus:
>
> >http://en.wikipedia.org/wiki/Bell%27s_Law_of_Computer_Classes
>
> Except that very concept is stupid. Mainframes have not be replaced.
> There are more mainframes around today than fifty years ago.
> Minicomputers too, only we don't call them minicomputers, we call them
> "servers".
>
> In ten years time, there will be more desktop PCs around than now. Most
> of them will be in the 90% of the world that isn't America. And most of
> them will be laptops. But they'll be used as desktops too. Not everybody
> wants to read email on a device smaller than your hand, clumsily poking
> at a tiny virtual keyboard.
>
> And anybody who thinks that Python can't run on tablets or smartphones
> hasn't been paying attention.
>
> --
> Steven

It would be good to pay attention before calling others to pay
attention.

http://litmus.com/blog/email-client-market-share-stats-infographic-june-2012/email-client-market-share-june-2012
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "#!/usr/bin/env python" vs. "#!/usr/bin/python"?

2012-09-28 Thread D'Arcy Cain
On Fri, 28 Sep 2012 06:57:28 -0400
Roy Smith  wrote:
> > I've seen both shebang lines to run a Python script on a *nix host:
> > 
> > #!/usr/bin/env python
> > #!/usr/bin/python
> > 
> > What's the difference?
> 
> The first one looks through your PATH to find the right python 
> interpreter to run.  The second one is hard-wired to run /usr/bin/python.
> 
> If you only have a single copy of python installed, it doesn't really 
> matter which you use.  But, you might as well get into the habit of 
> using the /usr/bin/env flavor because it's more flexible.

Not just flexible but portable.  On various systems I have Python
in /usr/bin, /usr/local/bin and /usr/pkg/bin.  "#!/usr/bin/env python"
finds it in each case so I only need one version of the script.

-- 
D'Arcy J.M. Cain  |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
IM: da...@vex.net
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to investigate web script not running?

2012-09-28 Thread Gilles
On Fri, 28 Sep 2012 14:16:22 +0200, "Michael Ross" 
wrote:
>Do it the other way around:
>
># cgitb before anything else
>import cgitb
>cgitb.enable()
>
># so this error will be caught
> from fcgi import WSGIServer

Thanks much for the tip. The error isn't displayed when calling the
script from a web browser but it is when running the script on a shell
account.

It looks like that newer version of fcgi.py doesn't include support
for WSGI, and I need some extra (Flup?) software to sit between
mod_fcgid and a WSGI Python application.

Definitely not plug 'n play :-/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "#!/usr/bin/env python" vs. "#!/usr/bin/python"?

2012-09-28 Thread Gilles
On Fri, 28 Sep 2012 09:19:54 -0400, D'Arcy Cain 
wrote:
>Not just flexible but portable.  On various systems I have Python
>in /usr/bin, /usr/local/bin and /usr/pkg/bin.  "#!/usr/bin/env python"
>finds it in each case so I only need one version of the script.

Good to know.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "#!/usr/bin/env python" vs. "#!/usr/bin/python"?

2012-09-28 Thread Demian Brecht

On 12-09-28 06:19 AM, D'Arcy Cain wrote:

Not just flexible but portable.  On various systems I have Python
in /usr/bin, /usr/local/bin and /usr/pkg/bin.  "#!/usr/bin/env python"
finds it in each case so I only need one version of the script.



+1. This also resolves correctly on Cygwin, even if Python is installed 
via Windows installers (as long as it's on system PATH). Tremendously 
useful if you're bouncing between *nix and Windows regularly.


--
Demian Brecht
@demianbrecht
http://demianbrecht.github.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: REST code-golf: How concisely can you expose and consume services?

2012-09-28 Thread Demian Brecht

(A little OT so my apologies up front)

On 12-09-28 12:39 AM, Chris Angelico wrote:

I love the idea, even though I shan't be entering. Code golf is awesome fun!


Code golf is indeed awesome fun and I usually enjoy taking part as well. 
However, I'm not a fan of code golf such as this, that uses a framework 
and then defines a rule that you *can't* hack on the same framework.


In the framework/external module spirit, I *could* write a module that 
does *all* of this for me (in Pyramid, Django, web2py, etc) and then 
enter with a simple method call in another module.


Done. One liner. I win ;)

I much prefer code golf that tests algorithmic/core language feature 
knowledge. Of course, that's entirely only my opinion.


--
Demian Brecht
@demianbrecht
http://demianbrecht.github.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python source code easy to hack?

2012-09-28 Thread sturla
kl. 13:57:14 UTC+2 fredag 28. september 2012 skrev Jayden følgende:
> Dear All, I have a concern in developing commercial code with Python. Someone 
> told me that its program can be easily hacked to get its source code. Is it 
> really the case? Any way to protect your source code? Thanks a lot! Jayden


Python bytecode is not easier to hack than Java or .NET bytecodes. You don't 
have to distribute your source code. Dropbox and BitTorrent are written in 
Python. I don't think "hacking the source" is a major problem. You also have 
the option of compiling parts of the source code to native C DLLs using Cython. 
If you are very paranoid about protecting your sources, perhaps you shouldn't 
distribute it at all, but provide a web application?

Sturla




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


Re: Python source code easy to hack?

2012-09-28 Thread Jerry Hill
On Fri, Sep 28, 2012 at 10:18 AM,   wrote:
> Python bytecode is not easier to hack than Java or .NET bytecodes.

This is true, but both java and .net are also relatively easy to decompile.

In general though, why does it matter?  What are you trying to protect
yourself against?  If you're including secrets in your code like
encryption keys or bank account numbers, there's no way to keep them
out of the hands of a determined attacker that has access to your
file, no matter what language it may be written in.

If you must keep anyone from ever seeing how your code works, the only
way to do that is to keep all the sensitive bits running on a machine
that you control.  Typically, you would do that by distributing a
client portion of your application, and also running a web service.
Then you can have your client connect to the web service, request that
the sensitive calculations, or money transfer, or whatever, be done on
the server, and just pass back the results.

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


howto handle nested for

2012-09-28 Thread Neal Becker
I know this should be a fairly basic question, but I'm drawing a blank.

I have code that looks like:
 
  for s0 in xrange (n_syms):
for s1 in xrange (n_syms):
for s2 in xrange (n_syms):
for s3 in xrange (n_syms):
for s4 in range (n_syms):
for s5 in range (n_syms):

Now I need the level of nesting to vary dynamically.  (e.g., maybe I need to 
add 
for  s6 in range (n_syms))

Smells like a candidate for recursion.  Also sounds like a use for yield.  Any 
suggestions? 

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


Re: howto handle nested for

2012-09-28 Thread Alister
On Fri, 28 Sep 2012 10:39:32 -0400, Neal Becker wrote:

> I know this should be a fairly basic question, but I'm drawing a blank.
> 
> I have code that looks like:
>  
>   for s0 in xrange (n_syms):
> for s1 in xrange (n_syms):
> for s2 in xrange (n_syms):
> for s3 in xrange (n_syms):
> for s4 in range (n_syms):
> for s5 in range (n_syms):
> 
> Now I need the level of nesting to vary dynamically.  (e.g., maybe I
> need to add for  s6 in range (n_syms))
> 
> Smells like a candidate for recursion.  Also sounds like a use for
> yield.  Any suggestions?

It definitely looks like for is the wrong way to go for this
without more information on the reason why it is difficult to say what 
the correct approach would be



-- 
Calm down, it's *only* ones and zeroes.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: howto handle nested for

2012-09-28 Thread Wojtek

W dniu 2012-09-28 16:42, Alister pisze:

On Fri, 28 Sep 2012 10:39:32 -0400, Neal Becker wrote:


I know this should be a fairly basic question, but I'm drawing a blank.

I have code that looks like:

   for s0 in xrange (n_syms):
 for s1 in xrange (n_syms):
 for s2 in xrange (n_syms):
 for s3 in xrange (n_syms):
 for s4 in range (n_syms):
 for s5 in range (n_syms):

Now I need the level of nesting to vary dynamically.  (e.g., maybe I
need to add for  s6 in range (n_syms))

Smells like a candidate for recursion.  Also sounds like a use for
yield.  Any suggestions?


It definitely looks like for is the wrong way to go for this
without more information on the reason why it is difficult to say what
the correct approach would be





it's well described in head first: python book ;)
check this sources from this book 
http://www.headfirstlabs.com/books/hfpython/code/chapter1.zip


hope it helps,

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


Re: Article on the future of Python

2012-09-28 Thread Chris Angelico
On Sat, Sep 29, 2012 at 12:31 AM, Dennis Lee Bieber
 wrote:
> On Fri, 28 Sep 2012 14:37:21 +1000, Chris Angelico 
> declaimed the following in gmane.comp.python.general:
>
>
>> For further details, poke around on the web; I'm sure you'll find
>> plenty of good blog posts etc. But as for me and my house, we will
>> have Postgres serve us.
>>
>
> Please, at least use the proper name... "Postgres" is a non-SQL
> database inspired by Ingres. "PostgreSQL" is Postgres with an SQL query
> engine.

http://www.postgresql.org/docs/9.1/static/history.html
"Many people continue to refer to PostgreSQL as "Postgres" (now rarely
in all capital letters) because of tradition or because it is easier
to pronounce. This usage is widely accepted as a nickname or alias."

There's lots of internal documentation that references "Postgres". I
don't see it as that big a deal.

> On my side... I have MySQL running on my desktop. When I started,
> MySQL had a native build that would run on Win9X; PostgreSQL at the time
> required installing a Cygwin environment.
>
> MySQL v5 has improved a lot from those days (v3)... It now supports
> stored procedures, triggers, a form of views, and prepared statements
> (though MySQLdb is still pre v5 and sends completely formatted string
> queries). They've even added GIS capabilities. (And then there is the
> "drop-in" replacement for MySQL -- MariaDB:
> http://kb.askmonty.org/en/mariadb-vs-mysql-compatibility/ )

Yes, MySQL has definitely improved. There was a time when its
unreliability applied to all your data too, but now you can just click
in InnoDB and have mostly-real transaction support etc. But there's
still a lot of work that by requirement happens outside of
transactions - MySQL doesn't let you roll back DDL, for instance.

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


Re: howto handle nested for

2012-09-28 Thread Laszlo Nagy

On 2012-09-28 16:39, Neal Becker wrote:

I know this should be a fairly basic question, but I'm drawing a blank.

I have code that looks like:
  
   for s0 in xrange (n_syms):

 for s1 in xrange (n_syms):
 for s2 in xrange (n_syms):
 for s3 in xrange (n_syms):
 for s4 in range (n_syms):
 for s5 in range (n_syms):

Now I need the level of nesting to vary dynamically.  (e.g., maybe I need to add
for  s6 in range (n_syms))

Smells like a candidate for recursion.  Also sounds like a use for yield.  Any
suggestions?

In your example, it seem that the iterable of the for loop is always the 
same: range(n_sysms). It seems to be a number. Is that true? If that is 
so, then here is something useful:


import copy

class MultiLevelIterator(object):
def __init__(self,levels,n):
assert(levels>0)
assert(n>0)
self.levels = levels
self.values = [0]*levels
self.n = n

def __iter__(self):
return self

def next(self):
res = copy.copy(self.values)
idx = self.levels-1
while idx>=0:
self.values[idx]+=1
if self.values[idx]>=self.n:
self.values[idx] = 0
idx-=1
else:
return res
raise StopIteration

i = MultiLevelIterator(2,3)
for values in i:
print values

This will print:

[0, 0]
[0, 1]
[0, 2]
[1, 0]
[1, 1]
[1, 2]
[2, 0]
[2, 1]


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


Re: what is the difference between st_ctime and st_mtime one is the time of last change and the other is the time of last modification, but i can not understand what is the difference between 'change'

2012-09-28 Thread Chris Angelico
On Fri, Sep 28, 2012 at 11:12 PM, 陈伟  wrote:
>
> --
> http://mail.python.org/mailman/listinfo/python-list

In future, can you put the body of your message into the body please? :)

ctime is creation time, not change time. mtime is modification time,
as you have. But I can understand where the confusion comes from;
Google tells me there've been documentation bugs involving this very
thing (and Google, being extremely Lawful Neutral, would have happily
told you the same thing if you'd asked).

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


Re: Article on the future of Python

2012-09-28 Thread Ian Kelly
On Fri, Sep 28, 2012 at 8:58 AM, Chris Angelico  wrote:
> Yes, MySQL has definitely improved. There was a time when its
> unreliability applied to all your data too, but now you can just click
> in InnoDB and have mostly-real transaction support etc. But there's
> still a lot of work that by requirement happens outside of
> transactions - MySQL doesn't let you roll back DDL, for instance.

Neither does Oracle, for that matter.  I don't really see any reason
why DDL *should* be transactional in nature.  If your web app is
issuing DDL statements, then you're probably doing something wrong.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what is the difference between st_ctime and st_mtime one is the time of last change and the other is the time of last modification, but i can not understand what is the difference between 'change'

2012-09-28 Thread Christian Heimes
Am 28.09.2012 17:07, schrieb Chris Angelico:
> On Fri, Sep 28, 2012 at 11:12 PM, 陈伟  wrote:
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
> 
> In future, can you put the body of your message into the body please? :)
> 
> ctime is creation time, not change time. mtime is modification time,
> as you have. But I can understand where the confusion comes from;
> Google tells me there've been documentation bugs involving this very
> thing (and Google, being extremely Lawful Neutral, would have happily
> told you the same thing if you'd asked).

In the future please read the manual before replying! ;) You are wrong,
ctime is *not* the creation time. It's the change time of the inode.
It's updated whenever the inode is modified, e.g. metadata modifications
like permission changes, link/unlink of hard links etc.

Christian






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


Re: Python source code easy to hack?

2012-09-28 Thread sturla
kl. 16:38:10 UTC+2 fredag 28. september 2012 skrev Jerry Hill følgende:

> This is true, but both java and .net are also relatively easy to decompile. 

Neither of them are very "obfuscated". 


> In general though, why does it matter? 

Paranoia among managers?


> What are you trying to protect yourself against? 

Embarassment? 

Patent trolls? 

Unauthorized access to priviledged features?

Industrial espionage?


> If you must keep anyone from ever seeing how your code works, the only way to 
> do that is to keep all the sensitive bits running on a machine that you 
> control. 

Indeed :) 



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


Re: Article on the future of Python

2012-09-28 Thread Chris Angelico
On Sat, Sep 29, 2012 at 1:14 AM, Ian Kelly  wrote:
> On Fri, Sep 28, 2012 at 8:58 AM, Chris Angelico  wrote:
>> Yes, MySQL has definitely improved. There was a time when its
>> unreliability applied to all your data too, but now you can just click
>> in InnoDB and have mostly-real transaction support etc. But there's
>> still a lot of work that by requirement happens outside of
>> transactions - MySQL doesn't let you roll back DDL, for instance.
>
> Neither does Oracle, for that matter.  I don't really see any reason
> why DDL *should* be transactional in nature.  If your web app is
> issuing DDL statements, then you're probably doing something wrong.

I have an auto-update script that ensures that our database is at the
correct patchlevel. It's fairly straight-forward: switch on
patchlevel, execute the statements required to get up to the next one,
at the bottom record the patchlevel in the database. (This relieves us
of issues of schema changes done in development that didn't get pushed
to production, for instance; our source code repository has
_everything_ needed.) If anything goes wrong, Postgres will roll the
transaction back. It doesn't matter if the first statement added a
column to a table and the second does an INSERT... SELECT; they both
get rolled back (as would any change to the patchlevel field, though
that happens at the very end so it's not significant here). I can
guarantee that the patch has either been completely applied or
completely rolled back - exactly what transactions are for.

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


Re: what is the difference between st_ctime and st_mtime one is the time of last change and the other is the time of last modification, but i can not understand what is the difference between 'change'

2012-09-28 Thread Chris Angelico
On Sat, Sep 29, 2012 at 1:18 AM, Christian Heimes  wrote:
> Am 28.09.2012 17:07, schrieb Chris Angelico:
> In the future please read the manual before replying! ;) You are wrong,
> ctime is *not* the creation time. It's the change time of the inode.
> It's updated whenever the inode is modified, e.g. metadata modifications
> like permission changes, link/unlink of hard links etc.

Whoops, my bad! Sorry. I was remembering some other APIs with similar
terminology.

Lesson: Check the docs, they're more reliable.

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


Re: howto handle nested for

2012-09-28 Thread Tim Chase
On 09/28/12 09:39, Neal Becker wrote:
> I know this should be a fairly basic question, but I'm drawing a blank.
> 
> I have code that looks like:
>  
>   for s0 in xrange (n_syms):
> for s1 in xrange (n_syms):
> for s2 in xrange (n_syms):
> for s3 in xrange (n_syms):
> for s4 in range (n_syms):
> for s5 in range (n_syms):
> 
> Now I need the level of nesting to vary dynamically.  (e.g., maybe I need to 
> add 
> for  s6 in range (n_syms))
> 
> Smells like a candidate for recursion.  Also sounds like a use for yield.  
> Any 
> suggestions? 

There was a good discussion on this back in 2008 that might be worth
reading over.  For some reason the mail.python.org archives[1] seem
to have broken threading on this topic (Andrew Reedick's reply using
exec() is waaay down in the archive, disassociated from the thread),
so here it is archived somewhere else where the 2 pages of threading
seems more manageable/accurate:

http://www.velocityreviews.com/forums/t585147-creating-unique-combinations-from-lists.html

-tkc

[1]
http://mail.python.org/pipermail/python-list/2008-January/487851.html





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


Re: howto handle nested for

2012-09-28 Thread Ian Kelly
On Fri, Sep 28, 2012 at 8:39 AM, Neal Becker  wrote:
> I know this should be a fairly basic question, but I'm drawing a blank.
>
> I have code that looks like:
>
>   for s0 in xrange (n_syms):
> for s1 in xrange (n_syms):
> for s2 in xrange (n_syms):
> for s3 in xrange (n_syms):
> for s4 in range (n_syms):
> for s5 in range (n_syms):
>
> Now I need the level of nesting to vary dynamically.  (e.g., maybe I need to 
> add
> for  s6 in range (n_syms))
>
> Smells like a candidate for recursion.  Also sounds like a use for yield.  Any
> suggestions?

levels = 6
for combination in itertools.product(xrange(n_syms), levels):
# do stuff

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


Re: howto handle nested for

2012-09-28 Thread Neal Becker
Neal Becker wrote:

> I know this should be a fairly basic question, but I'm drawing a blank.
> 
> I have code that looks like:
>  
>   for s0 in xrange (n_syms):
> for s1 in xrange (n_syms):
> for s2 in xrange (n_syms):
> for s3 in xrange (n_syms):
> for s4 in range (n_syms):
> for s5 in range (n_syms):
> 
> Now I need the level of nesting to vary dynamically.  (e.g., maybe I need to
> add
> for  s6 in range (n_syms))
> 
> Smells like a candidate for recursion.  Also sounds like a use for yield.  Any
> suggestions?

Thanks for the suggestions: I found itertools.product is just great for this.

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


Re: Article on the future of Python

2012-09-28 Thread rurpy
On 09/27/2012 10:37 PM, Chris Angelico wrote:>[...]
> * MySQL is designed for dynamic web sites, with lots of reading and
> not too much writing. Its row and table locking system is pretty
> rudimentary, and it's quite easy for performance to suffer really
> badly if you don't think about it. But if your needs are simple, MySQL
> is probably enough. PostgreSQL uses MVCC to avoid locks in many cases.
> You can happily read from a row while it's being updated; you'll be
> unaware of the update until it's committed.

MVCC comes with a cost though, as anyone who has ever needed
to do a SELECT COUNT(*) on a large Postgresql table knows.

>[...]
> * Both engines have good support in popular languages, including
> (dragging this back on topic, kicking and screaming) Python.

Maybe things are different now but a few years ago I was trying 
to choose between Postgresql and Mysql about the time Python
2.4 (I think) was released.  After waiting for over a year for
the Python mysql dbi module to be released for the then current
version of Python (I needed a binary for Windows) I finally 
gave up and decided to go with Postgresql (the psycopg2 module
was available a very short time after the new Python was.)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: howto handle nested for

2012-09-28 Thread Peter Otten
Neal Becker wrote:

> I know this should be a fairly basic question, but I'm drawing a blank.
> 
> I have code that looks like:
>  
>   for s0 in xrange (n_syms):
> for s1 in xrange (n_syms):
> for s2 in xrange (n_syms):
> for s3 in xrange (n_syms):
> for s4 in range (n_syms):
> for s5 in range (n_syms):
> 
> Now I need the level of nesting to vary dynamically.  (e.g., maybe I need
> to add
> for  s6 in range (n_syms))
> 
> Smells like a candidate for recursion.  Also sounds like a use for yield. 
> Any suggestions?

for s in itertools.product(range(n_syms), repeat=6):
print s

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


Re: Python source code easy to hack?

2012-09-28 Thread Littlefield, Tyler

On 9/28/2012 9:19 AM, stu...@molden.no wrote:

kl. 16:38:10 UTC+2 fredag 28. september 2012 skrev Jerry Hill følgende:


This is true, but both java and .net are also relatively easy to decompile.

Neither of them are very "obfuscated".



In general though, why does it matter?

Paranoia among managers?



What are you trying to protect yourself against?


Embarassment?

Patent trolls?

Unauthorized access to priviledged features?

Industrial espionage?

Sounds like a web solution is the best way. Use a thin client and run your 
NSA-level code on a server. It's worth pointing out though that even c/c++ 
isn't free. If someone wants to decompile or disassemble your code bad enough, 
it's going to happen.
 


If you must keep anyone from ever seeing how your code works, the only way to 
do that is to keep all the sensitive bits running on a machine that you control.

Indeed :)



Sturla



--
Take care,
Ty
http://tds-solutions.net
The aspen project: a barebones light-weight mud engine:
http://code.google.com/p/aspenmud
He that will not reason is a bigot; he that cannot reason is a fool; he that 
dares not reason is a slave.

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


Re: howto handle nested for

2012-09-28 Thread Ian Kelly
On Sep 28, 2012 9:49 AM, "Ian Kelly"  wrote:
> levels = 6
> for combination in itertools.product(xrange(n_syms), levels):
> # do stuff

Sorry, that should have read "product(xrange(n_syms), repeat=levels)". The
repeat argument is keyword-only.
-- 
http://mail.python.org/mailman/listinfo/python-list


How to get progress in python script.

2012-09-28 Thread Rolando Cañer Roblejo

Hi all,

Please, I need you suggest me a way to get statistics about a progress 
of my python script. My python script could take a lot of time 
processing a file, so I need a way that an external program check the 
progress of the script. My first idea was that the python script write a 
temp file showing the progress and the external program can check that 
file, but I think might happen file read/write locking issues.


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


Re: REST code-golf: How concisely can you expose and consume services?

2012-09-28 Thread Alec Taylor
On Sat, Sep 29, 2012 at 12:17 AM, Demian Brecht  wrote:
> (A little OT so my apologies up front)
>
>
> On 12-09-28 12:39 AM, Chris Angelico wrote:
>>
>> I love the idea, even though I shan't be entering. Code golf is awesome
>> fun!
>
>
> Code golf is indeed awesome fun and I usually enjoy taking part as well.
> However, I'm not a fan of code golf such as this, that uses a framework and
> then defines a rule that you *can't* hack on the same framework.
>
> In the framework/external module spirit, I *could* write a module that does
> *all* of this for me (in Pyramid, Django, web2py, etc) and then enter with a
> simple method call in another module.
>
> Done. One liner. I win ;)
>
> I much prefer code golf that tests algorithmic/core language feature
> knowledge. Of course, that's entirely only my opinion.
>
> --
> Demian Brecht
> @demianbrecht
> http://demianbrecht.github.com
> --
> http://mail.python.org/mailman/listinfo/python-list

Well I suppose I could lax that same requirement. I.e.: if you can get
these decorators (also working with RBAC) in, for example: Django;
then it still satisfied the rules :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get progress in python script.

2012-09-28 Thread John Gordon
In  
=?ISO-8859-1?Q?Rolando_Ca=F1er_Roblejo?=  writes:

> Hi all,

> Please, I need you suggest me a way to get statistics about a progress 
> of my python script. My python script could take a lot of time 
> processing a file, so I need a way that an external program check the 
> progress of the script. My first idea was that the python script write a 
> temp file showing the progress and the external program can check that 
> file, but I think might happen file read/write locking issues.

The external program should open the progress file for read only, so I
wouldn't think file locking would be an issue.

-- 
John Gordon   A is for Amy, who fell down the stairs
gor...@panix.com  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

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


Re: How to get progress in python script.

2012-09-28 Thread Jean-Michel Pichavant
- Original Message -
> Hi all,
> 
> Please, I need you suggest me a way to get statistics about a
> progress
> of my python script. My python script could take a lot of time
> processing a file, so I need a way that an external program check the
> progress of the script. My first idea was that the python script
> write a
> temp file showing the progress and the external program can check
> that
> file, but I think might happen file read/write locking issues.
> 
> Thanks.
> --
> http://mail.python.org/mailman/listinfo/python-list
> 

Hello,

One easy way would be to use a RPC system. Pyro implements one of them.
See http://packages.python.org/Pyro4/tutorials.html

This could be achieved in very few lines.

Your script create a thread in which it serves the remote requests.

xmlrpclib is another popular RPC package.

Another solution is to use a web server (sounds overkill but it isn't).

A lot of python web frameworks allow you to create such server in very few 
lines. look at http://www.cherrypy.org/ for instance, use their 'hello word' 
example and replace the 'hello word' by the current script progress and you're 
done.

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


Re: what is the difference between st_ctime and st_mtime one is the time of last change and the other is the time of last modification, but i can not understand what is the difference between 'change'

2012-09-28 Thread Nobody
On Fri, 28 Sep 2012 06:12:35 -0700, 陈伟 wrote:

> what is the difference between st_ctime and st_mtime one is the time of
> last change and the other is the time of last modification, but i can
> not understand what is the difference between 'change' and 'modification'. 

st_mtime is updated when the file's contents change. st_ctime is updated
when the file's metadata (owner, group, permissions, link count, etc)
changes.

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


Re: Python source code easy to hack?

2012-09-28 Thread 88888 Dihedral
Jayden於 2012年9月28日星期五UTC+8下午7時57分14秒寫道:
> Dear All,
> 
> 
> 
> I have a concern in developing commercial code with Python. Someone told me 
> that its program can be easily hacked to get its source code. Is it really 
> the case? Any way to protect your source code?
> 
> 
> 
> Thanks a lot!
> 
> 
> 
> Jayden

Nowadays high priced commercial IDE software products shipped  with a 
built in interpreter with some GUI to let users customize their own
needs in designs. This also means examples in source codes to 
be provided, too.

Anyway even compiled instructions can be iced and reverse engineered
for all the flows of the software.



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


Re: what is the difference between st_ctime and st_mtime one is the time of last change and the other is the time of last modification, but i can not understand what is the difference between 'change'

2012-09-28 Thread Kristen J. Webb

The Windows stat() call treats things differently,

FROM: http://msdn.microsoft.com/en-us/library/14h5k7ff%28v=vs.80%29.aspx

 st_ctime

Time of creation of file. Valid on NTFS but not on FAT formatted disk 
drives.

I don't think that Windows has a concept of a "change time" for meta data
(though that would be nice).  How's that for compatibility ;)

NOTE: I am a C programmer and new to python, so can anyone comment
on what the st_ctime value is when os.stat() is called on Windows?

Kris
On 9/28/12 9:25 AM, Chris Angelico wrote:

On Sat, Sep 29, 2012 at 1:18 AM, Christian Heimes  wrote:

Am 28.09.2012 17:07, schrieb Chris Angelico:
In the future please read the manual before replying! ;) You are wrong,
ctime is *not* the creation time. It's the change time of the inode.
It's updated whenever the inode is modified, e.g. metadata modifications
like permission changes, link/unlink of hard links etc.


Whoops, my bad! Sorry. I was remembering some other APIs with similar
terminology.

Lesson: Check the docs, they're more reliable.

ChrisA



--
This message is NOT encrypted

Mr. Kristen J. Webb
Chief Technology Officer
Teradactyl LLC.
2301 Yale Blvd. SE.
Suite C7
Albuquerque, NM 87106
Phone: 1-505-338-6000
Email: kw...@teradactyl.com
Web: http://www.teradactyl.com

Home of the

 True incremental Backup System

NOTICE TO RECIPIENTS: Any information contained in or attached to this message 
is intended solely for the use of the intended recipient(s). If you are not the 
intended recipient of this transmittal, you are hereby notified that you 
received this transmittal in error, and we request that you please delete and 
destroy all copies and attachments in your possession, notify the sender that 
you have received this communication in error, and note that any review or 
dissemination of, or the taking of any action in reliance on, this communication 
is expressly prohibited.



Regular internet e-mail transmission cannot be guaranteed to be secure or 
error-free. Therefore, we do not represent that this information is complete or 
accurate, and it should not be relied upon as such. If you prefer to communicate 
with Teradactyl LLC. using secure (i.e., encrypted and/or digitally signed) 
e-mail transmission, please notify the sender. Otherwise, you will be deemed to 
have consented to communicate with Teradactyl via regular internet e-mail 
transmission. Please note that Teradactyl reserves the right to intercept, 
monitor, and retain all e-mail messages (including secure e-mail messages) sent 
to or from its systems as permitted by applicable law.




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


RE: data attributes override method attributes?

2012-09-28 Thread Prasad, Ramit
Terry Reedy wrote:
> On 9/25/2012 4:07 PM, Ian Kelly wrote:
> > On Tue, Sep 25, 2012 at 1:58 PM, Terry Reedy  wrote:
> >> On 9/25/2012 11:03 AM, Chris Angelico wrote:
> >>> Instance attributes override (shadow) class attributes.
> >>
> >>
> >> except for (some? all?) special methods
> >
> > Those names are shadowed too.  If you call foo.__len__() and the name
> > is bound on the instance, it will call that function preferentially.
> > It's just that when the special Python machinery calls the method, it
> > skips the instance and goes straight to the class.
> 
> I added "Ian Kelly reminds me that instance.__xxx__ is only skipped by
> the internal machinery and not by direct accesses in user code. In the
> other hand, docs, official or otherwise, are filled with things like
> 'len(a) calls a.__len__', so I think something should be said that
> giving instances special method attributes does not have the effect one
> might expect."
> 
> to the issue.
> 


Just to make sure I am following, if you call
foo.__len__() it goes to the instance code while
if you do len(foo) it will go to class.__len__()?

If so, why? 

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: regular expression : the dollar sign ($) work with re.match() or re.search() ?

2012-09-28 Thread Prasad, Ramit
iMath wrote:
> Sent: Wednesday, September 26, 2012 2:39 AM
> To: python-list@python.org
> Subject: regular expression : the dollar sign ($) work with re.match() or 
> re.search() ?
> 
> I only know  the dollar sign ($) will match a pattern from the
> end of a string,but which method does it work with ,re.match() or re.search() 
>  ?


You can try this on the interactive interpreter.
>>> re.match('hi$', 'xihi')
>>> re.search('hi$', 'xihi')
<_sre.SRE_Match object at 0x13FF7100>

Although, I think match does not work since match
only starts searching at the start of the string
while search looks for the pattern anywhere in the string.

>>> re.match('x.hi$', 'xihi')
<_sre.SRE_Match object at 0x15693BF0>

I guess you can consider re.match's pattern to be 
prefixed with '^'.

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: data attributes override method attributes?

2012-09-28 Thread Ian Kelly
On Fri, Sep 28, 2012 at 12:02 PM, Prasad, Ramit
 wrote:
> Just to make sure I am following, if you call
> foo.__len__() it goes to the instance code while
> if you do len(foo) it will go to class.__len__()?

Yes:

>>> class Foo(object):
... def __len__(self):
... return 42
...
>>> foo = Foo()
>>> foo.__len__ = lambda: 43
>>> foo.__len__()
43
>>> len(foo)
42

> If so, why?

In the first case, "foo.__len__" just does the normal attribute lookup
for the class.  Instance attributes shadow class attributes, so the
instance attribute is returned and then called.

In the second case, "len(foo)" is implemented by a method in a
prescribed location:  foo.__class__.__len__.  It only looks in the
class for efficiency and because that is what the class object is for:
to define how its instances behave.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: data attributes override method attributes?

2012-09-28 Thread Terry Reedy

On 9/28/2012 2:02 PM, Prasad, Ramit wrote:


Just to make sure I am following, if you call
foo.__len__() it goes to the instance code while
if you do len(foo) it will go to class.__len__()?


len(foo) calls someclass.__len__(foo) where someclass is foo.__class__or 
some superclass.



If so, why?


Efficiency and perhaps simpler implementation, especially for binary ops.

--
Terry Jan Reedy

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


Re: regular expression : the dollar sign ($) work with re.match() or re.search() ?

2012-09-28 Thread Ian Kelly
On Fri, Sep 28, 2012 at 12:07 PM, Prasad, Ramit
 wrote:
> I guess you can consider re.match's pattern to be
> prefixed with '^'.

You can in this case, but they're not equivalent in multi-line mode:

>>> re.match('^two', 'one\ntwo', re.M)
>>> re.search('^two', 'one\ntwo', re.M)
<_sre.SRE_Match object at 0x0475BFA8>
-- 
http://mail.python.org/mailman/listinfo/python-list


print or write on a text file ?

2012-09-28 Thread Franck Ditter
Hi !
Here is Python 3.3
Is it better in any way to use print(x,x,x,file='out')
or out.write(x) ? Any reason to prefer any of them ?
There should be a printlines, like readlines ?
Thanks,

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


Re: Article on the future of Python

2012-09-28 Thread Devin Jeanpierre
On Thu, Sep 27, 2012 at 8:59 PM, alex23  wrote:
> On Sep 28, 2:17 am, Devin Jeanpierre  wrote:
>> Uncharitably, it's just a way of hiding one's head in the sand,
>> ignoring any problems Python has by focusing on what problems it
>> doesn't have.
>
> But isn't that what counterpoint is all about?

Actually I think it's about the charitable interpretation. Nobody
writes an article and says, "I'm going to stick my head in the sand".
I do believe the article is trying to provide a counterweight to the
gloomy mood set by the first.

> Calvin's article
> highlighted what he felt were areas that Python isn't successful,
> while Tim McNamara's pointed out areas it was. Just because Python
> isn't used much in commercial video games doesn't undermine the point
> that it's heavily used in education and research.

Absolutely. But also, vice versa -- just because Python is a wonderful
language for education and research does not mean that its problems in
commercial video games are not worth looking at.

> Does Python _have_ to be a go-to tool in gaming for it to not be on
> its death march?

I don't think anyone is arguing it's on a death march, just that there
are issues that we downplay but should address to keep a vibrant and
diverse community. Or something.

I'm pretty sure nobody thinks Python is on a death march.

> Or more to the point: rather than just complain about it... It's not
> like there aren't active communities that _are_ working in this area:
> PyGame, pyglet, Kivy are all projects that can be contributed to.

Haha, I wouldn't phrase it as "complaining".

Of these, I have mixed feelings. For example, Calvin has concerns that
Python isn't so good on mobile because battery usage (or some such
thing). I have no experience on mobile development, so no comment
there. I intend to use Kivy this weekend actually, so... Maybe this
one is very promising!

You didn't mention it, but for the browser issue there is PyJS... but
we've all heard the issues that project has. Not sure if there are
sane alternatives. Perhaps Silverlight? In all cases you end up with
output that's rather large. I'm not sure how practical it is, in the
end, to use Python. It may just be that the difference in productivity
for common web tasks, is tiny enough that the difficulty of setting up
an efficient python->JS toolchain is overwhelming.

As for pygame and pyglet, and game development, well, those are
things. That's a sort of frustrating response for a number of reasons,
but I'll keep it to just one:

Those have been around for a very long time, and are very stable (to
the point where the infrequency of updates sometimes leads to
questions as to whether they're even maintained (I think they are)).
The situation for using Python for game development is virtually
unchanged in the past several years, and it's been failing for the
past several years, so these two projects can't fix it. If these are
the best we have, barring additional argument we are going nowhere on
this front.

For what it's worth, I think there are much larger problems in the
game development world than how well Python is faring. Open source
projects for game development are few and of not-so-amazing quality.

> I love using Python and will endeavour to do so wherever I can, but
> it's always going to be the case that a language has strengths &
> weaknesses that other languages lack.

Absolutely! Python will never be perfect. There will always be paths
we can take to improve it. And we should always seek to improve it, as
long as we stand behind it as a good and useful language compared to
the alternatives.

On the other hand, I will not use Python "wherever I can". I will use
it wherever it makes the most sense to use it. For example, I would
write a first person shooter game in C# and Unity 3D, and I would
write my AJAX website in Javascript. It's just easier for me that way.
Why use Python if it makes my job harder?

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


RE: [python-list] python application file format

2012-09-28 Thread Prasad, Ramit
Benjamin Jessup wrote:
> Hello all,
> 
> What do people recommend for a file format for a python desktop
> application? Data is complex with 100s/1000s of class instances, which
> reference each other.
> 
> Write the file with struct module? (Rebuild object pointers, safe,
> compact, portable, not expandable without reserved space)
> 
> Use cPickle with a module/class whitelist? (Can't easily port, not
> entirely safe, compact enough, expandable)
> 
> Use JSON or similar? (Rebuild object pointers, portable, expandable, size?)
> 
> Any advice is greatly appreciated!

I would think your options are pickle, json or database (either sqlite or 
something like Postgres). I am unfamiliar with the struct module so I 
cannot comment on its applicability.

I would guess that your data would be best saved by using a sqlite 
database. Your biggest problem might be how the different classes are 
referencing each other. If you are using identifiers then any of these 
options will probably work. If you are using aggregation then I know
that pickle will work (at least for built-in types). JSON will
keep the structure but duplicate elements.


>>> a = [ 1,2,3 ]
>>> b = [ 'a', 'b', 'c' ]
>>> a.append( b )
>>> e = [ a,b ]
>>> s = json.dumps( e ) 
>>> eret = json.loads( s )
>>> id(eret[0][3]), id(eret[1]) # Same result for json and simplejson
(329443808, 327677272) 
>>> eret[0][3].append( 'o')
>>> eret[0][3], eret[1]
([u'a', u'b', u'c', 'o'], [u'a', u'b', u'c'])

So pickle will be your easiest option, but I am not sure how well it
will scale with a large number items. Using sqlite/db should scale well
but it will take you longer/more effort to create a system for converting 
your objects to and from the DB.



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: howto handle nested for

2012-09-28 Thread Neil Cerutti
On 2012-09-28, Laszlo Nagy  wrote:
> In your example, it seem that the iterable of the for loop is
> always the same: range(n_sysms). It seems to be a number. Is
> that true? If that is so, then here is something useful:
>
> import copy
>
> class MultiLevelIterator(object):
>  def __init__(self,levels,n):
>  assert(levels>0)
>  assert(n>0)
>  self.levels = levels
>  self.values = [0]*levels
>  self.n = n
>
>  def __iter__(self):
>  return self
>
>  def next(self):
>  res = copy.copy(self.values)
>  idx = self.levels-1
>  while idx>=0:
>  self.values[idx]+=1
>  if self.values[idx]>=self.n:
>  self.values[idx] = 0
>  idx-=1
>  else:
>  return res
>  raise StopIteration
>
> i = MultiLevelIterator(2,3)
> for values in i:
>  print values
>
> This will print:
>
> [0, 0]
> [0, 1]
> [0, 2]
> [1, 0]
> [1, 1]
> [1, 2]
> [2, 0]
> [2, 1]

It looks like you might have missed the last one. Also, be sure
to check itertools for occasionally for cool stuff like this.

>>> for values in itertools.product(range(3), repeat=2):
...   print(values)
...
(0, 0)
(0, 1)
(0, 2)
(1, 0)
(1, 1)
(1, 2)
(2, 0)
(2, 1)
(2, 2)

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


Re: print or write on a text file ?

2012-09-28 Thread Wayne Werner

On Fri, 28 Sep 2012, Franck Ditter wrote:


Hi !
Here is Python 3.3
Is it better in any way to use print(x,x,x,file='out')
or out.write(x) ? Any reason to prefer any of them ?
There should be a printlines, like readlines ?
Thanks,


The print function automatically appends newlines to the end of what it 
prints.


So if you had

text = 'Hello!'

and you did:

print(text, file=outfile)

then outfile would contain 'Hello!\n'

In contrast, outfile.write(text) would only write 'Hello!'. No newline.

There are lots of other handy things you can do with the print function:

values = [1,2,3,4]
print(*values, sep='\n', file=outfile)

I'll leave it to you to experiment.
HTH,
Wayne
--
http://mail.python.org/mailman/listinfo/python-list


How to pass FILE *

2012-09-28 Thread xDog Walker

The function I am trying to call wants a FILE *:

dlg_progressbox(const char *title,
const char *cprompt,
int height,
int width,
int pauseopt,
FILE *fp)

I can open the file to be referenced:

fp = os.fdopen(self.pipefds[0], 'r')

Every thing I've tried ends with ctypes raising a TypeError.

--
Yonder nor sorghum stenches shut ladle gulls stopper torque wet
strainers.

---

-- 
Yonder nor sorghum stenches shut ladle gulls stopper torque wet 
strainers.

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


[fcgi.py] Force cache upgrade?

2012-09-28 Thread Gilles
Hello

Does someone know if something must be done after editing a FastCGI +
WSGI script so that the changes will show in the browser immediately
instead of having to wait X minutes?

===
#!/usr/bin/env python2.6

def myapp(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')])
return ['I CHANGED THIS\n']

if __name__ == '__main__':
from flup.server.fcgi import WSGIServer
WSGIServer(myapp).run()
===

I guess the FastCGI server (Flup) only updates its cache every so
often. Do I need to type a command to force Flup to recompile the
Python script?

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


creating an artificial "last element" in sort list

2012-09-28 Thread dave
a = ['a', 'b', x]

b = sorted(a)

What does x need to be to always be last on an ascending sort no matter what 
'a' and 'b' are within reason... I am expecting 'a' and 'b' will be not 
longer than 10 char's long I tried making x = '' and 
believe it or not, this appears FIRST on the sort!!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [fcgi.py] Force cache upgrade?

2012-09-28 Thread Demian Brecht
If you don't have access to restart Apache (or `x` server), then touch
fcgi.py *should* work.

On Fri, Sep 28, 2012 at 2:57 PM, Gilles  wrote:

> Hello
>
> Does someone know if something must be done after editing a FastCGI +
> WSGI script so that the changes will show in the browser immediately
> instead of having to wait X minutes?
>
> ===
> #!/usr/bin/env python2.6
>
> def myapp(environ, start_response):
> start_response('200 OK', [('Content-Type', 'text/plain')])
> return ['I CHANGED THIS\n']
>
> if __name__ == '__main__':
> from flup.server.fcgi import WSGIServer
> WSGIServer(myapp).run()
> ===
>
> I guess the FastCGI server (Flup) only updates its cache every so
> often. Do I need to type a command to force Flup to recompile the
> Python script?
>
> Thank you.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: creating an artificial "last element" in sort list

2012-09-28 Thread Ian Kelly
On Fri, Sep 28, 2012 at 5:39 PM, dave  wrote:
> a = ['a', 'b', x]
>
> b = sorted(a)
>
> What does x need to be to always be last on an ascending sort no matter what 
> 'a' and 'b' are within reason... I am expecting 'a' and 'b' will be not 
> longer than 10 char's long I tried making x = '' and 
> believe it or not, this appears FIRST on the sort!!!

It appears last when I run the code.

To answer your question, though, if you want to force x to be last,
then I suggest removing it from the list and then appending it to the
end.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: creating an artificial "last element" in sort list

2012-09-28 Thread dave
more clearer, this is a more realistic use case:

['awefawef', 'awefawfsf', 'awefsdf', 'zz', 'zz', 
'zz']

and the quantity of ''zz'' would be dynamic.

On Friday, September 28, 2012 4:46:15 PM UTC-7, Ian wrote:

> 
> > a = ['a', 'b', x]
> 
> >
> 
> > b = sorted(a)
> 
> >
> 
> > What does x need to be to always be last on an ascending sort no matter 
> > what 'a' and 'b' are within reason... I am expecting 'a' and 'b' will 
> > be not longer than 10 char's long I tried making x = '' 
> > and believe it or not, this appears FIRST on the sort!!!
> 
> 
> 
> It appears last when I run the code.
> 
> 
> 
> To answer your question, though, if you want to force x to be last,
> 
> then I suggest removing it from the list and then appending it to the
> 
> end.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: creating an artificial "last element" in sort list

2012-09-28 Thread Demian Brecht
Maybe

l = filter(a, lambda v: v == a[-1])
sorted(a[:-len(l)]) + l

?

On Fri, Sep 28, 2012 at 4:51 PM, dave  wrote:

> more clearer, this is a more realistic use case:
>
> ['awefawef', 'awefawfsf', 'awefsdf', 'zz', 'zz',
> 'zz']
>
> and the quantity of ''zz'' would be dynamic.
>
> On Friday, September 28, 2012 4:46:15 PM UTC-7, Ian wrote:
>
> >
> > > a = ['a', 'b', x]
> >
> > >
> >
> > > b = sorted(a)
> >
> > >
> >
> > > What does x need to be to always be last on an ascending sort no
> matter what 'a' and 'b' are within reason... I am expecting 'a' and 'b'
> will be not longer than 10 char's long I tried making x =
> '' and believe it or not, this appears FIRST on the sort!!!
> >
> >
> >
> > It appears last when I run the code.
> >
> >
> >
> > To answer your question, though, if you want to force x to be last,
> >
> > then I suggest removing it from the list and then appending it to the
> >
> > end.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [fcgi.py] Force cache upgrade?

2012-09-28 Thread Gilles
On Fri, 28 Sep 2012 23:57:14 +0200, Gilles  wrote:
>I guess the FastCGI server (Flup) only updates its cache every so
>often. Do I need to type a command to force Flup to recompile the
>Python script?

Turns out that, yes, mod_fcgid is configured to reload a script only
after some time or some number of hits, and yes, mod_fcgid settings
are off-limit on a shared host.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: creating an artificial "last element" in sort list

2012-09-28 Thread 88888 Dihedral
dave於 2012年9月29日星期六UTC+8上午7時51分10秒寫道:
> more clearer, this is a more realistic use case:
> 
> 
> 
> ['awefawef', 'awefawfsf', 'awefsdf', 'zz', 'zz', 
> 'zz']
> 
> 
> 
> and the quantity of ''zz'' would be dynamic.
> 
> 
> 
> On Friday, September 28, 2012 4:46:15 PM UTC-7, Ian wrote:
> 
> 
> 
> > 
> 
> > > a = ['a', 'b', x]
> 
> > 
> 
> > >
> 
> > 
> 
> > > b = sorted(a)
> 
> > 
> 
> > >
> 
> > 
> 
> > > What does x need to be to always be last on an ascending sort no matter 
> > > what 'a' and 'b' are within reason... I am expecting 'a' and 'b' will 
> > > be not longer than 10 char's long I tried making x = 
> > > '' and believe it or not, this appears FIRST on the 
> > > sort!!!
> 
> > 
> 
> > 
> 
> > 
> 
> > It appears last when I run the code.
> 
> > 
> 
> > 
> 
> > 
> 
> > To answer your question, though, if you want to force x to be last,
> 
> > 
> 
> > then I suggest removing it from the list and then appending it to the
> 
> > 
> 
> > end.

I am thinking if it is helpful to preprocess an arbitrary
list first into some set of unique  ordered elements before a sort. 

Anyway lists are passed by references  to functions in python. 

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


write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.

2012-09-28 Thread iMath
write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: creating an artificial "last element" in sort list

2012-09-28 Thread Demian Brecht

Apparently gmail hates me and my last response didn't get through:

a = ['awefawef', 'awefawfsf', 'awefsdf', 'zz', 
'zz', 'zz']

f = filter(lambda s: s == a[-1], a)
l = sorted(lst[:-len(f)]) + f

Now, not 100% sure about efficiency over large sizes of a, but that's a 
naive stab at it anyway.


--
Demian Brecht
@demianbrecht
http://demianbrecht.github.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: print or write on a text file ?

2012-09-28 Thread Terry Reedy

On 9/28/2012 2:42 PM, Franck Ditter wrote:

Hi !
Here is Python 3.3
Is it better in any way to use print(x,x,x,file='out')
or out.write(x) ? Any reason to prefer any of them ?


print converts objects to strings and adds separators and terminators. 
If you have a string s and want to output it as is, out.write(s) is 
perhaps faster. It is 6 chars shorted than print(s, file=out).



There should be a printlines, like readlines ?


No, now that files are iterators, I believe readlines is somewhat obsolete.

file.readlines() == list(file)

The only reason not to deprecate it is for the hint parameter to limit 
the bytes read. That is  little harder to do with the iterator.



If you have any iterator of lines,
for line in lines: line.print()
is quite sufficient. There is little or no need for output limitation.

--
Terry Jan Reedy

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


Re: how to run shell command like "<

2012-09-28 Thread 叶佑群

于 2012-9-28 16:16, Kushal Kumaran 写道:

On Fri, Sep 28, 2012 at 1:15 PM, 叶佑群  wrote:

Hi, all,

 I have the shell command like this:

sfdisk -uM /dev/sdb<<  EOT
,1000,83
,,83
EOT


 I have tried subprocess.Popen, pexpect.spawn and os.popen, but none of
these works, but when I type this shell command in shell, it is works fine.
I wonder how to emulate this type of behavior in python , and if someone can
figure out the reason why?

 The sample code of subprocess.Popen is:

 command = ["sfdisk", "-uM",  target, "<
The "<
I tried this, but it is still not work.



 and pexpect code is:

 child = pexpect.spawn ("sfdisk -uM /dev/sdb<


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


Re: how to run shell command like "<

2012-09-28 Thread 叶佑群

于 2012-9-28 16:16, Kushal Kumaran 写道:

On Fri, Sep 28, 2012 at 1:15 PM, 叶佑群  wrote:

Hi, all,

 I have the shell command like this:

sfdisk -uM /dev/sdb<<  EOT
,1000,83
,,83
EOT


 I have tried subprocess.Popen, pexpect.spawn and os.popen, but none of
these works, but when I type this shell command in shell, it is works fine.
I wonder how to emulate this type of behavior in python , and if someone can
figure out the reason why?

 The sample code of subprocess.Popen is:

 command = ["sfdisk", "-uM",  target, "<
The "

Re: creating an artificial "last element" in sort list

2012-09-28 Thread Demian Brecht
> f = filter(lambda s: s == a[-1], a)

That line's assuming that the last element may also be found in arbitrary 
locations in the list. If it's guaranteed that they're all contiguous at the 
upper bounds, I'd just walk the list backwards until I found one that wasn't 
matching rather than filtering.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reducing cache/buffer for faster display

2012-09-28 Thread Rikishi42
On 2012-09-28, Dennis Lee Bieber  wrote:
> On Thu, 27 Sep 2012 22:25:39 + (UTC), John Gordon 
> declaimed the following in gmane.comp.python.general:
>
>> 
>> Isn't terminal output line-buffered?  I don't understand why there would
>> be an output delay.  (Unless the "\r" is messing things up...)
>
>   It's the trailing , The \r is being used to reset to the
> beginning of the console line, but the comma "says" more output for
> /this/ line will be coming... So no output until explicitly flushed, or
> a new-line is issued.

Well, the \r seems to be the problem, allright.
But output was not completely blocked, just delayed a very long time.  

So perhaps flushing and a sending a newline aren't the only triggers for
output.  Perhaps there's a maximum delay or a maximum cumulated size, and
the output is flushed when such a limit is reached.

Anyway, that's mainly academic. I doubt there will be a correction to
that behaviour. 

-- 
When in doubt, use brute force.
-- Ken Thompson
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reducing cache/buffer for faster display

2012-09-28 Thread Rikishi42
On 2012-09-28, Chris Angelico  wrote:
> On Fri, Sep 28, 2012 at 10:05 AM, Rikishi42  wrote:
>> The scripts in question only increase numbers. But should that not be the
>> case, solutions are simple enough. The numbers can be formatted to have a
>> fixed size. In the case of random line contents (a list of filesnames, say)
>> it's enough to create an output function that is aware of the length of the
>> previously printed line, and add enough spaces to the current one to wipe
>> exess content.
>
> Yep, that's a pretty effective way to do it. One simple method to it
> is to format the whole string as a single whole, then left justify it
> in a field of (say) 79 characters, and output that:
>
> msg = "Progress: %d%% (%d/%d)... %s" % (done*100/total, done, total,
> current_file)
> print msg.ljust(79)+"\r",
> sys.stdout.flush()

Mmm, I allmost went for that. It's elegant, simple and clear. But there's
one drawback: I usually reduce the terminal's window to take up less desktop
surface during those long runs.  
So fixing it to 79 chars won't do.  And I'm not even tempted to go for a
detection of the width of the terminal from within the script.  The idea is
after all to keep the scripts simple (syntax) and light (execution).

Well, good night everyone.

-- 
When in doubt, use brute force.
-- Ken Thompson
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: howto handle nested for

2012-09-28 Thread Peter Pearson
On Fri, 28 Sep 2012 09:49:36 -0600, Ian Kelly  wrote:
>
> levels = 6
> for combination in itertools.product(xrange(n_syms), levels):
> # do stuff

>>> n_syms = 3
>>> levels = 6
>>> for combination in itertools.product(xrange(n_syms), levels):
...   print combination
... 
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'int' object is not iterable


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


Re: creating an artificial "last element" in sort list

2012-09-28 Thread duncan smith

On 29/09/12 00:51, dave wrote:

more clearer, this is a more realistic use case:

['awefawef', 'awefawfsf', 'awefsdf', 'zz', 'zz', 
'zz']

and the quantity of ''zz'' would be dynamic.



Maybe,

class Greatest:

def __lt__(self, other):
return False

def __eq__(self, other):
return type(other) == type(self)

etc.

Duncan

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


Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.

2012-09-28 Thread Tim Chase
On 09/28/12 19:31, iMath wrote:
> write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.

Okay, that was pretty easy.  Thanks for the challenge :-)

-tkc



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


Re: creating an artificial "last element" in sort list

2012-09-28 Thread Paul Rubin
dave  writes:
> What does x need to be to always be last on an ascending sort no
> matter what 'a' and 'b' are within reason... 

Why are you trying to do that?  It sounds ugly.  Just sort the list with
the a's and b's.  If you absolutely have to, you could make a class with
comparison methods that put all the x's at the bottom, but look for
something cleaner.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.

2012-09-28 Thread Paul Rubin
iMath  writes:
> write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.

And then you have two problems.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.

2012-09-28 Thread Mark Lawrence

On 29/09/2012 02:35, Tim Chase wrote:

On 09/28/12 19:31, iMath wrote:

write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.


Okay, that was pretty easy.  Thanks for the challenge :-)

-tkc



What's the run time speed like?  How much memory does it use?  Shouldn't 
you be using the regex module from pypi instead of the standard library 
re?  Guess who's borrowed the time machine?


--
Cheers.

Mark Lawrence.

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


Re: QThread.terminate in Python 3

2012-09-28 Thread Lee Harr

>> I understand that use of QThread.terminate is discouraged,
>> but it has worked well previously and I would like to continue
>> this use if possible.
>>
>       And now you've encountered the reason it is discouraged.

Ok. Point taken.

What I hear you saying is that once I use .terminate anything
following that is indeterminate. It might work on my system
and nowhere else. Even though it was working for me before,
it was likely causing trouble elsewhere.


So, I need another approach.

The problem I am facing is that I want to run arbitrary
(user-supplied) code in the other thread and need to be
able to stop it at any time.

This is for a python turtle-graphics application:
http://pynguin.googlecode.com/


I found another possible approach here:
http://tomerfiliba.com/recipes/Thread2/

It uses ctypes to raise an exception in the other thread.
For the simple test case, at least, it works (for me).

Is this any safer or more reliable than .terminate ?

Is it portable? ie, if I build this in to my actual application
and it works for me, is it likely to work for everyone?


Thanks for any insight.

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


Re: creating an artificial "last element" in sort list

2012-09-28 Thread Steven D'Aprano
On Fri, 28 Sep 2012 16:39:33 -0700, dave wrote:

> a = ['a', 'b', x]
> b = sorted(a)
>
> What does x need to be to always be last on an ascending sort no matter
> what 'a' and 'b' are within reason... 

How about this?

a = ['a', 'b']
b = sorted(a) + ['whatever you want']

You could also do this:

x = max(a)
a.append(x)
b = sorted(a)


> I am expecting 'a' and 'b'
> will be not longer than 10 char's long I tried making x =
> '' and believe it or not, this appears FIRST on the
> sort!!!

I think you are mistaken.

py> sorted(['a', 'b', ''])
['a', 'b', '']



But really, I don't understand what problem you are trying to solve. 
Perhaps if you explain the purpose of this, we can suggest a solution.


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


Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.

2012-09-28 Thread Tim Chase
On 09/28/12 20:58, Mark Lawrence wrote:
> On 29/09/2012 02:35, Tim Chase wrote:
>> On 09/28/12 19:31, iMath wrote:
>>> write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.
>>
>> Okay, that was pretty easy.  Thanks for the challenge :-)
> 
> What's the run time speed like?

O(1)

r = re.compile(
"800-555-1212|"
"555-1212|"
   r"\(800\) 555-1212"
)

(okay, so I also have one that solves the OP's underqualified
problem, but without the OP at least *trying* to code up an answer
and asking for help with it, I've give the snarky solution :-)

> How much memory does it use? 

Insignificant.

> Shouldn't you be using the regex module from pypi instead of the
> standard library re? 

Only if the OP requested it ;-)

> Guess who's borrowed the time machine?

Neutrino!

-tkc


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


Missing library path (WIndows)

2012-09-28 Thread FPEFPE
Hello -- I am running python from an application, starting it with a call to 
the python31.dll

I think I am missing something in my path -- any help would be appreciated -- 
thanks

Here is the script and the output ---

# this is a test

import sys

print('hello from python')

print('Number of arguments:', len(sys.argv), 'arguments.')

print('Argument List:', str(sys.argv))

#--#
#  o u t p u t #
#--#

Argument List: hello from python

Number of arguments: 5 arguments.

Traceback (most recent call last):

File "", line 1, in

File "C:/DOCUME~1/Frank/LOCALS~1/Temp/foo.py", line 10, in

print('Argument List:', str(sys.argv))

File "C:\Python32\Lib\encodings\cp437.py", line 19, in encode

return codecs.charmap_encode(input,self.errors,encoding_map)[0]

UnicodeEncodeError: 'charmap' codec can't encode characters in position 2-18: 
character maps to
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Article on the future of Python

2012-09-28 Thread Steven D'Aprano
On Fri, 28 Sep 2012 14:50:14 -0400, Devin Jeanpierre wrote:

> I'm pretty sure nobody thinks Python is on a death march.

Don't be so sure. There's always *someone* complaining about something, 
and they're usually convinced that (Language X) is on it's last legs 
because (feature Y) is missing or (event Z) happened.

Seriously. If you believe the haters and the complainers, Python will 
never be taken seriously as a language because:

- it has significant whitespace.
- it doesn't have braces.
- it doesn't have static typing.
- Python is too slow.
- it has lost momentum to Ruby on RAILS.
- it has lost momentum to Javascript.
- it doesn't have a real garbage collector that can collect cycles.
- oh, Python has had one of those for a decade? I meant a garbage
  collector that can collect cycles involving objects with __del__
  methods.
- threads aren't exactly like threads in some other language.
- Python only uses a single core of the CPU.
- I mean CPython. IronPython and Jython don't count.
- I mean ordinary Python code, using multiprocessing doesn't count.
- Neither do C extensions or numpy.
- Python changes too fast. People can't keep up. Python should be an ISO
  standard managed by a committee, like C, with a guarantee that 30 year
  old code will run in the latest version.
- Python changes too slow. People can't use all these great new features.
  It has gotten too big and the developers care too much about backward
  compatibility and aren't willing to delete cruft from the language.
- you can't compile to native machine code. No language can possibly be
  successful with byte-code running in a virtual machine.
- it isn't a pure object-oriented language exactly like Java.
- you can't hide your source code from the end user. People will
  STEEEAL MY INTELLECTUUUALL PROPERTY!!!
- oh, you can? Yeah, but it's too hard, and besides they might decompile
  the .pyc files.
- Python 3 is a failure and has split the community.


I think I've got all the most common reasons for dismissing Python. 
"Python has lost ground to Flash" is a new one for me, as is "Python ate 
my mobile phone's batteries".


In a way, it's quite unfortunate that you can't write a blog post 
discussing weaknesses of a language (as opposed to strengths) without 
turning it into fuel for the haters:

http://news.ycombinator.com/item?id=4567023

But when you give a blog post an inflammatory title like "I am worried 
about the future of Python", what do you expect?



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


Re: data attributes override method attributes?

2012-09-28 Thread Steven D'Aprano
On Fri, 28 Sep 2012 18:02:04 +, Prasad, Ramit wrote:

> Just to make sure I am following, if you call foo.__len__() it goes to
> the instance code while if you do len(foo) it will go to
> class.__len__()?

If you call foo.__len__, the attribute lookup of __len__ will use the 
exact same search path as foo.spam would use:

1) does __getattribute__ exist and intercept the call?
2) if not, does a instance attribute exist?
3) if not, does a class attribute exist?
4) if not, does a superclass attribute exist?
5) if not, does __getattr__ exist and intercept the call?

Using len(foo) bypasses steps 1) and 2) as a speed optimization. For the 
common case where an instance's class directly defines a __len__ method, 
that saves about 10-15% of the overhead of calling a special method, 
compared to old-style classes.



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


Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.

2012-09-28 Thread Ian Kelly
On Fri, Sep 28, 2012 at 8:17 PM, Tim Chase
 wrote:
> On 09/28/12 20:58, Mark Lawrence wrote:
>> On 29/09/2012 02:35, Tim Chase wrote:
>>> On 09/28/12 19:31, iMath wrote:
 write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.
>>>
>>> Okay, that was pretty easy.  Thanks for the challenge :-)
>>
>> What's the run time speed like?
>
> O(1)
>
> r = re.compile(
> "800-555-1212|"
> "555-1212|"
>r"\(800\) 555-1212"
> )

Mine is simpler and faster.

r = re.compile("")
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: creating an artificial "last element" in sort list

2012-09-28 Thread Ian Kelly
On Fri, Sep 28, 2012 at 6:59 PM, Demian Brecht  wrote:
>> f = filter(lambda s: s == a[-1], a)
>
> That line's assuming that the last element may also be found in arbitrary 
> locations in the list. If it's guaranteed that they're all contiguous at the 
> upper bounds, I'd just walk the list backwards until I found one that wasn't 
> matching rather than filtering.

The slicing operation in the second line assumes that they're all
collected at the end of the list anyway.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.

2012-09-28 Thread Steven D'Aprano
On Fri, 28 Sep 2012 21:25:35 -0600, Ian Kelly wrote:

> Mine is simpler and faster.
> 
> r = re.compile("")

The OP doesn't say that you have to compile it, so just:

''

wins.



-- 
Steven



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


Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.

2012-09-28 Thread Tim Chase
On 09/28/12 22:25, Ian Kelly wrote:
> On Fri, Sep 28, 2012 at 8:17 PM, Tim Chase
 On 09/28/12 19:31, iMath wrote:
> write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.
>>
>> r = re.compile(
>> "800-555-1212|"
>> "555-1212|"
>>r"\(800\) 555-1212"
>> )
> 
> Mine is simpler and faster.
> 
> r = re.compile("")

doh!  «smacks forehead»  Yours is FAR more efficient, and much more
readable than mine.  iMath's teacher will be pleased :-)

-tkc


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


Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.

2012-09-28 Thread Tim Chase
On 09/28/12 22:30, Steven D'Aprano wrote:
> On Fri, 28 Sep 2012 21:25:35 -0600, Ian Kelly wrote:
> 
>> Mine is simpler and faster.
>>
>> r = re.compile("")
> 
> The OP doesn't say that you have to compile it, so just:
> 
> ''
> 
> wins.

OP doesn't say it even has to be a string, so I guess



wins. :-P

It's-too-late-on-a-Friday-night'ly yers,

-tkc


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


Re: creating an artificial "last element" in sort list

2012-09-28 Thread Demian Brecht
On Fri, Sep 28, 2012 at 8:29 PM, Ian Kelly  wrote:

> The slicing operation in the second line assumes that they're all
> collected at the end of the list anyway.
>


True enough. Hadn't considered otherwise when I first whipped that off with
the first example (thinking/trying it out *before* posting would likely be
a good idea ;)).

>>> a = ['z'*5, 'b', 'a', 'c', 'z'*5]
>>> b = filter(lambda n: n == a[-1], a)
>>> c = filter(lambda n: n != a[-1], a)
>>> d = sorted(c) + b
['a', 'b', 'c', 'z', 'z']

It does the trick for what he was asking in the original question, although
there are likely more optimal solutions for a sufficiently large a.

As Steven noted, it definitely would help to get a little more information
about the specific problem.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: write a regex matches 800-555-1212, 555-1212, and also (800) 555-1212.

2012-09-28 Thread Devin Jeanpierre
On Fri, Sep 28, 2012 at 9:58 PM, Mark Lawrence  wrote:
> What's the run time speed like?  How much memory does it use?  Shouldn't you
> be using the regex module from pypi instead of the standard library re?
> Guess who's borrowed the time machine?

O(n), O(1), and I used RE2.

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


Re: How to pass FILE *

2012-09-28 Thread Chris Rebert
On Fri, Sep 28, 2012 at 2:55 PM, xDog Walker  wrote:
>
> The function I am trying to call wants a FILE *:
>
> dlg_progressbox(const char *title,
> const char *cprompt,
> int height,
> int width,
> int pauseopt,
> FILE *fp)
>
> I can open the file to be referenced:
>
> fp = os.fdopen(self.pipefds[0], 'r')
>
> Every thing I've tried ends with ctypes raising a TypeError.

What specifically did you try?

A tiny bit of googling suggests the following approach:
http://stackoverflow.com/questions/3794309/python-ctypes-python-file-object-c-file/3794401#3794401
Related POSIX docs:
http://pubs.opengroup.org/onlinepubs/009695399/functions/fdopen.html

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


  1   2   >