Re: Passing arguments to & executing, a python script on a remote machine from a python script on local machine

2012-09-20 Thread ashish
On Thursday, September 20, 2012 10:39:28 AM UTC+5:30, Chris Angelico wrote:
> On Thu, Sep 20, 2012 at 2:27 PM, Steven D'Aprano
> 
>  wrote:
> 
> > On Wed, 19 Sep 2012 12:46:33 -0700, ashish wrote:
> 
> >
> 
> >> 2. I have a python script, local.py, running on local which needs to
> 
> >> pass arguments ( 3/4 string arguments, containing whitespaces like
> 
> >> spaces, etc ) to a python script, remote.py running on remote (the
> 
> >> remote machine).
> 
> >
> 
> > If 3/4 of the arguments are strings, what sort of objects are the other
> 
> > 1/4?

Sorry for the confusion Steven.
There are 3 or 4 arguments, all of which are strings.

> 
> 
> 
> I understand the OP as meaning "three or four string arguments".

Yup, that's what i meant.
> 
> 
> 
> ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to send email programmatically from a gmail email a/c when port 587(smtp) is blocked

2012-09-20 Thread Mark Lawrence

On 19/09/2012 20:12, ashish wrote:

Folks,

I asked the same query on the python tutor mailing list.
The responses i received are here :
http://thread.gmane.org/gmane.comp.python.tutor/77601


Mark,

There is nothing wrong in asking a query on multiple forums.

Poeple on the tutor list, may not be part of comp.lang.python & subscribers to 
comp.lang.python



And you've done exactly the same thing with another question.  So there 
are two sets of answers on two mls/ngs that are completely unconnected 
except that I amongst others know that they exist.  If you had to ask on 
both, why not post to both in one hit?


--
Cheers.

Mark Lawrence.

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


Re: Installing Pip onto a mac os x system

2012-09-20 Thread Hans Mulder
On 20/09/12 03:32:40, John Mordecai Dildy wrote:
> Does anyone know how to install Pip onto a mac os x ver 10.7.4?
> 
> Ive tried easy_instal pip but it brings up this message (but it doesn't help 
> with my problem):
> 
> error: can't create or remove files in install directory
> 
> The following error occurred while trying to add or remove files in the
> installation directory:
> 
> [Errno 13] Permission denied: 
> '/Library/Python/2.7/site-packages/test-easy-install-1820.write-test'
> 
> The installation directory you specified (via --install-dir, --prefix, or
> the distutils default setting) was:
> 
> /Library/Python/2.7/site-packages/
> 
> Perhaps your account does not have write access to this directory?  If the
> installation directory is a system-owned directory, you may need to sign in
> as the administrator or "root" account.  If you do not have administrative
> access to this machine, you may wish to choose a different installation
> directory, preferably one that is listed in your PYTHONPATH environment
> variable.
> 
> For information on other options, you may wish to consult the
> documentation at:
> 
>   http://peak.telecommunity.com/EasyInstall.html
> 
> Please make the appropriate changes for your system and try again.
> 
> Thing is I am the Administrator of the computer

In that case, you should be able to do

sudo easy_instal pip

This will ask your login password, and then run "easy_instal pip"
with so-called "root" privileges.

> and can use all of the folders on the mac computer.

You may be able to view them, but I doubt that you can add
new files to folders like, say, /Library/Python .


Hope this helps,

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


Re: How to get the list of all my open file(descriptor)s and locks?

2012-09-20 Thread Hans Mulder
On 20/09/12 05:11:11, Chris Angelico wrote:
> On Thu, Sep 20, 2012 at 7:09 AM, Ian Kelly  wrote:
>> You could do:
>>
>> os.listdir("/proc/%d/fd" % os.getpid())
>>
>> This should work on Linux, AIX, and Solaris, but obviously not on Windows.

On MacOS X, you can use

os.listdir("/dev/fd")

This might work on other OSes.

> I'm not sure how cross-platform it is, but at least on Linux, you can
> use /proc/self as an alias for "/proc/"+os.getpid() - worth a try,
> might make your code a bit easier to read.
> 
> It's documented as useful when you don't know the PID yet (eg telling
> a process to read the file /proc/self/fd/0 to force it to use stdin).

At least on Linux, Solaris and MacOS X, you shouldd use /dev/stdin,
which is a symlink to whatever the official name is.
"/dev/stdin" is more readable and more portable than the official name.

> So I'm confident enough to recommend testing it. :)

Testing such things is the best way to learn.


Hope this helps,

-- HansM

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


[Q] How to exec code object with local variables specified?

2012-09-20 Thread Makoto Kuwata
Hi,

Is it possible to run code object with local variables specified?
I'm trying the following code but not work:

def fn():
   x = 1
   y = 2
localvars = {'x': 0}
exec(fn.func_code, globals(), localvars)
print(localvars)
## what I expected is: {'x': 1, 'y': 2}
## but actual is:  {'x': 0}

Python: 2.7.3
OS: MacOS X

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


Re: Programming Issues

2012-09-20 Thread Dave Angel
On 09/19/2012 07:01 PM, Nathan Spicer wrote:
> Dave,
> 

You sent this response privately, which isn't the way the mailing list
works.  Private responses are good for thank-yous and for personal
remarks of no interest to others.  But you're short-circuiting the
helping process if you don't let everyone else see what you're saying
here.  I'll forward my response to the list, so others can jump in.

You also top-posted, rather than putting your remarks after what you're
quoting.

> I'm running Python 3.2. I think doing raw input is what the instructor is
> looking for.

What version of Python does the instructor think you're using?
raw_input is not in Python 3.2, being renamed to input().  The 2.x logic
of input() is fortunately gone, as it was quite unsafe.

> I know a need to do a loop, but the problem is i don't know ho
> to do this. 

An open-ended loop can be done with the while statement.  What I mean by
open-ended is that you have no way of predicting how many times it will
loop, because it depends on data you don't have ahead of time.  The
other type of loop is the for loop, where the loop will normally
progress till it processes all items in a sequence.

A while loop might look something like:

def ask_user():
amount = get_amount()
while amount != 0:
 do_some_stuff(amount)
 amount =  get_amount()
print "done with program"  #

And your top-level code might look something like:

if __name__ == "__main__":
ask_user()

If that makes sense to you, then write the functions that this one
calls.  If you don't understand this loop, then ask us to elaborate.
And if you don't understand what this while statement itself does, ask
your professor.


> There is no code, because i'm not sure how or what to write. So
> far we've done simple inputs. Are you aware of any examples similar to my
> problem, that i could look at and possibly base my program accordingly?
> 
> Thanks,
> Nathan
> 


-- 

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


Re: [Q] How to exec code object with local variables specified?

2012-09-20 Thread Peter Otten
Makoto Kuwata wrote:

> Is it possible to run code object with local variables specified?
> I'm trying the following code but not work:
> 
> def fn():
>x = 1
>y = 2
> localvars = {'x': 0}
> exec(fn.func_code, globals(), localvars)
> print(localvars)
> ## what I expected is: {'x': 1, 'y': 2}
> ## but actual is:  {'x': 0}
> 
> Python: 2.7.3

>>> loc = {}
>>> exec("x = 1; y = 2", globals(), loc)
>>> loc
{'y': 2, 'x': 1}

However, this won't work with the code object taken from a function which 
uses a different a bytecode (STORE_FAST instead of STORE_NAME):


>>> import dis
>>> def f(): x = 1
... 
>>> dis.dis(f)
  1   0 LOAD_CONST   1 (1)
  3 STORE_FAST   0 (x)
  6 LOAD_CONST   0 (None)
  9 RETURN_VALUE
>>> dis.dis(compile("x=1", "", "exec"))
  1   0 LOAD_CONST   0 (1)
  3 STORE_NAME   0 (x)
  6 LOAD_CONST   1 (None)
  9 RETURN_VALUE


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


Re: 'indent'ing Python in windows bat

2012-09-20 Thread Duncan Booth
Jason Friedman  wrote:

>> I'm converting windows bat files little by little to Python 3 as I
>> find time and learn Python.
>> The most efficient method for some lines is to call Python like:
>> python -c "import sys; sys.exit(3)"
>>
>> How do I "indent" if I have something like:
>> if (sR=='Cope'): sys.exit(1) elif (sR=='Perform') sys.exit(2) else
>> sys.exit(3)
> 
> Some months ago I posted what I think is a similar question in the
> Unix world:  I wanted to call a small portion of Python from within a
> Bash script.
> 
> Someone on this list answered (for Bash):
> 
> #!/bin/bash
> command1
> command2
> python -c "if True:
> import module
> if condition:
> do_this
> else:
> do_that
> "
> command4
> # end code
> 
> Perhaps something similar would work for a .bat file.
> 
Provided there's only one Python block in the .bat or .cmd file then 
there's no real problem; you just have to hide each language from the 
other:

goto :start
"""
:start
@echo off
echo This is a CMD script
python -x %~f0 "%1"
echo Back in the CMD script
goto :eof
"""
import sys
print("Welcome to Python")
print("Arguments were {}".format(sys.argv))
print("Bye!")


You can put the Python code either before or after the triple-quote 
string containing the CMD commands, just begin the file with a goto to 
skip into the batch commands and end them by jumping to eof.

-- 
Duncan Booth http://kupuguy.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Fool Python class with imaginary members (serious guru stuff inside)

2012-09-20 Thread Jure Erznožnik
I'm trying to create a class that would lie to the user that a member is in 
some cases a simple variable and in other cases a class. The nature of the 
member would depend on call syntax like so:
1. x = obj.member #x becomes the "simple" value contained in member
2. x = obj.member.another_member #x becomes the "simple" value contained in 
first member's another_member.

So the first method "detects" that we only need a simple value and returns 
that. The second method "sees" that we need "member" as a class and returns 
that. Note that "simple type" could mean anything, from int to bitmap image.

I have determined that this is possible if I sacrifice the final member 
reference to the __call__ override using function-call syntax: 1. x = 
obj.member(). The call syntax returns the simple value and the other returns 
the class. It is also possible if I override the __xxxitem__ methods to 
simulate a dictionary.

However, I would like to use the "true member" access syntax if possible.

So, is it possible?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Passing arguments to & executing, a python script on a remote machine from a python script on local machine (using ssh ?)

2012-09-20 Thread Emile van Sebille

On 9/19/2012 12:50 PM ashish said...

Hi c.l.p folks

Here is my situation

1. I have two machines. Lets call them 'local' & 'remote'.
Both run ubuntu & both have python installed

2. I have a python script, local.py, running on 'local' which needs to pass 
arguments ( 3/4 string arguments, containing whitespaces like spaces, etc ) to 
a python script, remote.py running on 'remote' (the remote machine).

I have the following questions:

1. What's the best way to accomplish my task ?


Check out http://rpyc.sourceforge.net/ -- It's reasonably lightweight 
and has been working well for our similar situation.


Emile


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


How to limit CPU usage in Python

2012-09-20 Thread Rolando Cañer Roblejo

Hi all,

Is it possible for me to put a limit in the amount of processor usage (% 
CPU) that my current python script is using? Is there any module useful 
for this task? I saw Resource module but I think it is not the module I 
am looking for. Some people recommend to use nice and cpulimit unix 
tools, but those are external to python and I prefer a python solution. 
I am working with Linux (Ubuntu 10.04).


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


Re: [Q] How to exec code object with local variables specified?

2012-09-20 Thread Makoto Kuwata
On Thu, Sep 20, 2012 at 10:15 PM, Peter Otten <__pete...@web.de> wrote:
>
 loc = {}
 exec("x = 1; y = 2", globals(), loc)
 loc
> {'y': 2, 'x': 1}
>
> However, this won't work with the code object taken from a function which
> uses a different a bytecode (STORE_FAST instead of STORE_NAME):
>

Is there any way to use STORE_FAST instead of STORE_NAME?

exec("string", ...) is not a solution for me.

# What is different between fn.func_code and compile("string")?

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


Re: [Q] How to exec code object with local variables specified?

2012-09-20 Thread Terry Reedy

On 9/20/2012 7:27 AM, Makoto Kuwata wrote:


Is it possible to run code object with local variables specified?


In the way you mean that, no.


I'm trying the following code but not work:

 def fn():
x = 1
y = 2
 localvars = {'x': 0}
 exec(fn.func_code, globals(), localvars)


The globals and locals you pass to exec are the globals and locals for 
the context in which the code object runs. They have nothing to do with 
the code objects local namespace.


Running exec with separate globals and locals is like running the code 
within a class definition context. If you ran


def fn(): x = 1
class dummy:
  fn()

dummy.x would not be defined and I presume you would not expect it to.

--
Terry Jan Reedy

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


Re: Passing arguments to & executing, a python script on a remote machine from a python script on local machine (using ssh ?)

2012-09-20 Thread Chris Rebert
On Wed, Sep 19, 2012 at 12:50 PM, ashish  wrote:

> 2. I have a python script, local.py, running on 'local' which needs to pass 
> arguments ( 3/4 string arguments, containing whitespaces like spaces, etc ) 
> to a python script, remote.py running on 'remote' (the remote machine).

> 3. Has anybody been able to do this using os.system ?
>
> I tried this
 import os
 os.system ("ssh remoteuser@remote python remote.py arg1 arg2 arg3")
>
> This worked, but if the arguments i tried to pass, had spaces, i was not able 
> to 'escape' the spaces.

Use the `subprocess` module instead (with shell=False). You then won't
need to worry about escaping.
http://docs.python.org/library/subprocess.html

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


Re: A little morning puzzle

2012-09-20 Thread Tobiah



Here is my solution:



** Incredibly convoluted and maximally less concise solution
than other offerings. **



Might be better ones though.


Unlikely.


Zing!



 


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


Re: How to limit CPU usage in Python

2012-09-20 Thread Terry Reedy

On 9/20/2012 11:12 AM, Rolando Cañer Roblejo wrote:

Hi all,

Is it possible for me to put a limit in the amount of processor usage (%
CPU) that my current python script is using? Is there any module useful
for this task? I saw Resource module but I think it is not the module I
am looking for. Some people recommend to use nice and cpulimit unix
tools, but those are external to python and I prefer a python solution.
I am working with Linux (Ubuntu 10.04).


Call the external tools with subprocess.open.

--
Terry Jan Reedy


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


Re: How to limit CPU usage in Python

2012-09-20 Thread Terry Reedy

On 9/20/2012 12:46 PM, Terry Reedy wrote:

On 9/20/2012 11:12 AM, Rolando Cañer Roblejo wrote:

Hi all,

Is it possible for me to put a limit in the amount of processor usage (%
CPU) that my current python script is using? Is there any module useful
for this task? I saw Resource module but I think it is not the module I
am looking for. Some people recommend to use nice and cpulimit unix
tools, but those are external to python and I prefer a python solution.
I am working with Linux (Ubuntu 10.04).


Call the external tools with subprocess.open.


I meant to end that with ? as I don't know how easy it is to get the 
external id of the calling process that is to be limited. I presume that 
can be done by first calling ps (with subprocess) and searching the 
piped-back output.



--
Terry Jan Reedy


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


Re: Fool Python class with imaginary members (serious guru stuff inside)

2012-09-20 Thread Terry Reedy

On 9/20/2012 9:52 AM, Jure Erznožnik wrote:

I'm trying to create a class that would lie to the user that a member is in 
some cases a simple variable and in other cases a class. The nature of the 
member would depend on call syntax like so:
1. x = obj.member #x becomes the "simple" value contained in member
2. x = obj.member.another_member #x becomes the "simple" value contained in 
first member's another_member.


x.y.z is parsed and executed as (x.y).z, so you are asking if the 
attribute-getter can know what will be done with the object it returns.
Assuming CPython, you would have to write something that searches the 
Python code before compilation, the ast during compilation, or the 
bytecode after compilation.


Much easier would be to define a union class that is a simple type with 
attributes and return that in the first lookup.


class AttrInt(int):
def __getattr__(self, name): return 'attribute'

y = AttrInt(3)
print(y, y.a)
###
3 attribute

If x.y returns an AttrInt, it will act like an int for most purposes, 
while x.y.z will return whatever AttrInt.__getattr__ does and the 
temporary AttrInt y disappears.


--
Terry Jan Reedy


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


Re: How to limit CPU usage in Python

2012-09-20 Thread Jerry Hill
On Thu, Sep 20, 2012 at 11:12 AM, Rolando Cañer Roblejo
 wrote:
> Hi all,
>
> Is it possible for me to put a limit in the amount of processor usage (%
> CPU) that my current python script is using? Is there any module useful for
> this task? I saw Resource module but I think it is not the module I am
> looking for. Some people recommend to use nice and cpulimit unix tools, but
> those are external to python and I prefer a python solution. I am working
> with Linux (Ubuntu 10.04).

Maximum percentage of CPU used isn't normally something you control.
The only way I know of to do it involves having another process
monitor the thing you want to control and sending signals to stop and
start it (e.g., http://cpulimit.sourceforge.net/).

Typically, you instead want to control the priority (so that higher
priority apps can easily take more CPU time).  That's what nice is for
(http://docs.python.org/library/os.html#os.nice).  If you want to
limit a process in the same way that ulimit does, then the resources
module is what you want
(http://docs.python.org/library/resource.html#resource.setrlimit).

Is there a particular reason that you'd rather have your CPU sitting
idle, rather than continuing with whatever code is waiting to be run?
I'm having a hard time understanding what problem you might be having
that some combination of setting the nice level and imposing resource
limits won't handle.

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


Re: Passing arguments to & executing, a python script on a remote machine from a python script on local machine (using ssh ?)

2012-09-20 Thread Piet van Oostrum
Ismael Farfán  writes:

> How about something like this:
> os.system ( 'ssh remoteuser@remote python remote.py "arg 1" "arg 2" "arg 3"' )

That won't work. You need an additional level of quoting because ssh is
also a shell so it adds another level of interpretation.

The following works:

os.system('''ssh remoteuser@remote "python remote.py 'arg 1' 'arg 2' 'arg 
3'"''')
-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Passing arguments to & executing, a python script on a remote machine from a python script on local machine (using ssh ?)

2012-09-20 Thread Piet van Oostrum
Chris Rebert  writes:

> Use the `subprocess` module instead (with shell=False). You then won't
> need to worry about escaping.
> http://docs.python.org/library/subprocess.html

You will still need to worry about escaping because on the remote end
you invoke ssh which is a shell. The obvious call:

subprocess.call(["ssh", "remoteuser@remote", "python", "remote.py", "arg
1", "arg 2", "arg 3"])

won't work because ssh will break up the "arg n" strings. You need to
use  "'arg n'" or put the whole python command in a string like:

"python TEST/testargs.py 'arg 1' 'arg 2' 'arg 3'"
-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


looping in array vs looping in a dic

2012-09-20 Thread giuseppe . amatulli
Hi,  
I have this script in python that i need to apply for very large arrays (arrays 
coming from satellite images). 
The script works grate but i would like to speed up the process. 
The larger computational time is in the for loop process.
Is there is a way to improve that part?
Should be better to use dic() instead of np.ndarray for saving the results?
and if yes how i can make the sum in dic()(like in the correspondent 
matrix[row_c,1] = matrix[row_c,1] + valuesRaster[row,col] )?
If the dic() is the solution way is faster?

Thanks
Giuseppe

import numpy  as  np
import sys
from time import clock, time

# create the arrays

start = time()
valuesRaster = np.random.random_integers(0, 100, 100).reshape(10, 10)
valuesCategory = np.random.random_integers(1, 10, 100).reshape(10, 10)

elapsed = (time() - start)
print(elapsed , "create the data")

start = time()

categories = np.unique(valuesCategory)
matrix = np.c_[ categories , np.zeros(len(categories))]

elapsed = (time() - start)
print(elapsed , "create the matrix and append a colum zero ")

rows = 10
cols = 10

start = time()

for col in range(0,cols):
for row in range(0,rows):
for row_c in range(0,len(matrix)) :
if valuesCategory[row,col] == matrix[row_c,0] :
matrix[row_c,1] = matrix[row_c,1] + valuesRaster[row,col]
break
elapsed = (time() - start)
print(elapsed , "loop in the  data ")

print (matrix)
-- 
http://mail.python.org/mailman/listinfo/python-list


"Development mode"

2012-09-20 Thread py_lrnr
I am new to python and I have come across the following command and its 
description: 

>Now to be able to run the project you will need to install it and its 
>>dependencies. 

>python setup.py develop

I looked up what the 'develop' argument does and found:

>Extra commands:
>  develop   install package in 'development mode'

I searched for a description of 'development mode' but could not find a good 
description.

Can anyone (very briefly) explain to me, in a sentence or two:

what 'development mode' is? 
how 'development mode' differs from other 'modes'?
why/when I would use 'development mode'? 
what 'development mode' does or does not allow me to do?

Many thanks in advance.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: looping in array vs looping in a dic

2012-09-20 Thread MRAB

On 2012-09-20 19:31, giuseppe.amatu...@gmail.com wrote:

Hi,
I have this script in python that i need to apply for very large arrays (arrays 
coming from satellite images).
The script works grate but i would like to speed up the process.
The larger computational time is in the for loop process.
Is there is a way to improve that part?
Should be better to use dic() instead of np.ndarray for saving the results?
and if yes how i can make the sum in dic()(like in the correspondent 
matrix[row_c,1] = matrix[row_c,1] + valuesRaster[row,col] )?
If the dic() is the solution way is faster?

Thanks
Giuseppe

import numpy  as  np
import sys
from time import clock, time

# create the arrays

start = time()
valuesRaster = np.random.random_integers(0, 100, 100).reshape(10, 10)
valuesCategory = np.random.random_integers(1, 10, 100).reshape(10, 10)

elapsed = (time() - start)
print(elapsed , "create the data")

start = time()

categories = np.unique(valuesCategory)
matrix = np.c_[ categories , np.zeros(len(categories))]

elapsed = (time() - start)
print(elapsed , "create the matrix and append a colum zero ")

rows = 10
cols = 10

start = time()

for col in range(0,cols):
 for row in range(0,rows):
 for row_c in range(0,len(matrix)) :
 if valuesCategory[row,col] == matrix[row_c,0] :
 matrix[row_c,1] = matrix[row_c,1] + valuesRaster[row,col]
 break
elapsed = (time() - start)
print(elapsed , "loop in the  data ")

print (matrix)


If I understand the code correctly, 'matrix' contains the categories in
column 0 and the totals in column 1.

What you're doing is performing a linear search through the categories
and then adding to the corresponding total.

Linear searches are slow because on average you have to search through
half of the list. Using a dict would be much faster (although you
should of course measure it!).

Try something like this:

import numpy as np
from time import time

# Create the arrays.

start = time()

valuesRaster = np.random.random_integers(0, 100, 100).reshape(10, 10)
valuesCategory = np.random.random_integers(1, 10, 100).reshape(10, 10)

elapsed = time() - start
print(elapsed, "Create the data.")

start = time()

categories = np.unique(valuesCategory)
totals = dict.fromkeys(categories, 0)

elapsed = time() - start
print(elapsed, "Create the totals dict.")

rows = 100
cols = 10

start = time()

for col in range(cols):
for row in range(rows):
cat = valuesCategory[row, col]
ras = valuesRaster[row, col]
totals[cat] += ras

elapsed = time() - start
print(elapsed, "Loop in the data.")

print(totals)

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


Re: Fool Python class with imaginary members (serious guru stuff inside)

2012-09-20 Thread Steven D'Aprano
On Thu, 20 Sep 2012 06:52:07 -0700, Jure Erznožnik wrote:

> I'm trying to create a class that would lie to the user that a member is
> in some cases a simple variable and in other cases a class. The nature
> of the member would depend on call syntax like so: 
> 1. x = obj.member #x becomes the "simple" value contained in member 
> 2. x = obj.member.another_member #x becomes the "simple" value
> contained in first member's another_member.

Why do you hate your users so much that you want to cause them enormous 
difficulty with perfectly reasonable code like this?

tmp = obj.member
x = tmp.another_member


> So the first method "detects" that we only need a simple value and
> returns that.

Fortunately that is impossible without nasty bytecode or AST hacks. Thank 
the stars that Python doesn't allow anything as badly designed as this!



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


Re: How to limit CPU usage in Python

2012-09-20 Thread Christian Heimes
Am 20.09.2012 17:12, schrieb Rolando Cañer Roblejo:
> Hi all,
> 
> Is it possible for me to put a limit in the amount of processor usage (%
> CPU) that my current python script is using? Is there any module useful
> for this task? I saw Resource module but I think it is not the module I
> am looking for. Some people recommend to use nice and cpulimit unix
> tools, but those are external to python and I prefer a python solution.
> I am working with Linux (Ubuntu 10.04).

Hello,

you have two options here. You can either limit the total amount of CPU
seconds with the resource module or reduce the priority and scheduling
priority of the process.

The resource module is a wrapper around the setrlimit and getrlimit
feature as described in http://linux.die.net/man/2/setrlimit .

The scheduling priority can be altered with nice, get/setpriority or io
priority. The psutil package http://code.google.com/p/psutil/ wraps all
functions in a nice Python API.

Regards
Christian



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


Re: looping in array vs looping in a dic

2012-09-20 Thread Ian Kelly
On Thu, Sep 20, 2012 at 1:09 PM, MRAB  wrote:
> for col in range(cols):
> for row in range(rows):
> cat = valuesCategory[row, col]
> ras = valuesRaster[row, col]
> totals[cat] += ras

Expanding on what MRAB wrote, since you probably have far fewer
categories than pixels, you may be able to take better advantage of
numpy's vectorized operations (which are pretty much the whole point
of using numpy in the first place) by looping over the categories
instead:

for cat in categories:
totals[cat] += np.sum(valuesCategory * (valuesRaster == cat))
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: looping in array vs looping in a dic

2012-09-20 Thread Ian Kelly
On Thu, Sep 20, 2012 at 1:28 PM, Ian Kelly  wrote:
> Expanding on what MRAB wrote, since you probably have far fewer
> categories than pixels, you may be able to take better advantage of
> numpy's vectorized operations (which are pretty much the whole point
> of using numpy in the first place) by looping over the categories
> instead:
>
> for cat in categories:
> totals[cat] += np.sum(valuesCategory * (valuesRaster == cat))

Of course, that should have read:

for cat in categories:
totals[cat] += np.sum(valuesRaster * (valuesCategory == cat))
-- 
http://mail.python.org/mailman/listinfo/python-list


When should I use "development mode"?

2012-09-20 Thread py_lrnr
I am new to python and I have come across the following command and its 
description:

>Now to be able to run the project you will need to install it and its 
>>dependencies.

>python setup.py develop

I looked up what the 'develop' argument does and found:

>Extra commands:
>  develop   install package in 'development mode'

I searched for a description of 'development mode' but could not find a good 
description.

Can anyone (very briefly) explain to me, in a sentence or two:

what 'development mode' is?
how 'development mode' differs from other 'modes'?
why/when I would use 'development mode'?
what 'development mode' does or does not allow me to do?

Many thanks in advance. 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: When should I use "development mode"?

2012-09-20 Thread Ian Kelly
On Thu, Sep 20, 2012 at 1:38 PM, py_lrnr  wrote:
> Can anyone (very briefly) explain to me, in a sentence or two:
>
> what 'development mode' is?
> how 'development mode' differs from other 'modes'?
> why/when I would use 'development mode'?
> what 'development mode' does or does not allow me to do?

Instead of installing the package into your site-packages, it installs
a link back to the source folder.  This lets you make changes to the
code in the source folder which are immediately visible to Python
without having to install the package again.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Installing Pip onto a mac os x system

2012-09-20 Thread John Mordecai Dildy
Thank you Hans M. for some input.

Now it shows the error of:

sudo: easy_instal: command not found
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Installing Pip onto a mac os x system

2012-09-20 Thread John Gordon
In  John Mordecai Dildy 
 writes:

> Now it shows the error of:

> sudo: easy_instal: command not found

Try 'easy_install' instead of 'easy_instal'.

-- 
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: Installing Pip onto a mac os x system

2012-09-20 Thread John Mordecai Dildy
On Thursday, September 20, 2012 4:37:36 PM UTC-4, John Gordon wrote:
> In  John Mordecai 
> Dildy  writes:
> 
> 
> 
> > Now it shows the error of:
> 
> 
> 
> > sudo: easy_instal: command not found
> 
> 
> 
> Try 'easy_install' instead of 'easy_instal'.
> 
> 
> 
> -- 
> 
> 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"

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


portable way of locating an executable (like which)

2012-09-20 Thread Gelonida N

I'd like to implement the equivalent functionality of the unix command
/usr/bin/which

The function should work under Linux and under windows.

Did anybody already implement such a function.
If not, is there a portable way of splitting the environment variable PATH?

Thanks for any sugestions


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


Re: portable way of locating an executable (like which)

2012-09-20 Thread Mark Lawrence

On 20/09/2012 22:06, Gelonida N wrote:

I'd like to implement the equivalent functionality of the unix command
/usr/bin/which

The function should work under Linux and under windows.

Did anybody already implement such a function.


Searching found nothing obvious to me :(


If not, is there a portable way of splitting the environment variable PATH?


With os.sep ?



Thanks for any sugestions




--
Cheers.

Mark Lawrence.

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


Re: portable way of locating an executable (like which)

2012-09-20 Thread Jason Swails
On Thu, Sep 20, 2012 at 5:06 PM, Gelonida N  wrote:

> I'd like to implement the equivalent functionality of the unix command
> /usr/bin/which
>
> The function should work under Linux and under windows.
>
> Did anybody already implement such a function.
> If not, is there a portable way of splitting the environment variable PATH?
>

I've used the following in programs I write:

def which(program):
   def is_exe(fpath):
  return os.path.exists(fpath) and os.access(fpath, os.X_OK)

   fpath, fname = os.path.split(program)
   if fpath:
  if is_exe(program):
 return program
   else:
  for path in os.getenv("PATH").split(os.pathsep):
 exe_file = os.path.join(path, program)
 if is_exe(exe_file):
return exe_file
   return None

IIRC, I adapted it from StackOverflow.  I know it works on Linux and Mac OS
X, but not sure about windows (since I don't know if PATH works the same
way there).

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


Re: portable way of locating an executable (like which)

2012-09-20 Thread Chris Angelico
On Fri, Sep 21, 2012 at 7:47 AM, Mark Lawrence  wrote:
> On 20/09/2012 22:06, Gelonida N wrote:
>>
>> I'd like to implement the equivalent functionality of the unix command
>> /usr/bin/which
>>
>> The function should work under Linux and under windows.
>>
>> Did anybody already implement such a function.
>
> Searching found nothing obvious to me :(
>
>> If not, is there a portable way of splitting the environment variable
>> PATH?
> With os.sep ?

os.sep is the directory separator, but os.pathsep may be what you
want. Between that and os.getenv('path') you can at least get the
directories. Then on Windows, you also need to check out
os.getenv('pathext') and split _that_ on the semicolon, and try each
of those as a file extension. I'm not sure whether or not Windows will
add extensions from pathext if one is given on the command line - for
instance, if typing "foo.exe" will search for "foo.exe.bat" - but the
basics are there.

Alternatively, there may be a Win32 API funct5ion that does this.
Would be worth a look.

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


Re: portable way of locating an executable (like which)

2012-09-20 Thread Ian Kelly
On Thu, Sep 20, 2012 at 4:21 PM, Chris Angelico  wrote:
> os.sep is the directory separator, but os.pathsep may be what you
> want. Between that and os.getenv('path') you can at least get the
> directories. Then on Windows, you also need to check out
> os.getenv('pathext') and split _that_ on the semicolon, and try each
> of those as a file extension. I'm not sure whether or not Windows will
> add extensions from pathext if one is given on the command line - for
> instance, if typing "foo.exe" will search for "foo.exe.bat" - but the
> basics are there.

Easy enough to test:

C:\>echo echo hello! > foo.exe.bat

C:\>foo.exe
hello!

Yup, it does.  It looks like it tries it without the extension first, though:

C:\>copy c:\windows\notepad.exe foo.exe
1 file(s) copied.

C:\>foo.exe
[starts notepad]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: portable way of locating an executable (like which)

2012-09-20 Thread Chris Angelico
On Fri, Sep 21, 2012 at 8:32 AM, Ian Kelly  wrote:
> On Thu, Sep 20, 2012 at 4:21 PM, Chris Angelico  wrote:
>> os.sep is the directory separator, but os.pathsep may be what you
>> want. Between that and os.getenv('path') you can at least get the
>> directories. Then on Windows, you also need to check out
>> os.getenv('pathext') and split _that_ on the semicolon, and try each
>> of those as a file extension. I'm not sure whether or not Windows will
>> add extensions from pathext if one is given on the command line - for
>> instance, if typing "foo.exe" will search for "foo.exe.bat" - but the
>> basics are there.
>
> Easy enough to test:
>
> C:\>echo echo hello! > foo.exe.bat
>
> C:\>foo.exe
> hello!
>
> Yup, it does.  It looks like it tries it without the extension first, though:
>
> C:\>copy c:\windows\notepad.exe foo.exe
> 1 file(s) copied.
>
> C:\>foo.exe
> [starts notepad]

Well, at least it's consistent. Makes your PATH extremely sensitive,
though, easy for anyone to inject executables into it. But then, you
can already do that by putting them in the current directory, so
that's not really any different.

Jason's solution looks fine apart from the PATHEXT requirement, so if
you know you have the full filename and you don't care if the actual
command interpreter will do exactly the same, that'll do you fine.

Is this something that might want to be a function in the os module?
Particularly so if, as I suspect there might be, there's a Win32 API
function that precisely replicates the behaviour of executable
invocation. A while since I've done much Windows programming but I
think there's a SearchPath function?

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


Re: portable way of locating an executable (like which)

2012-09-20 Thread Gelonida N

On 09/21/2012 12:21 AM, Chris Angelico wrote:

On Fri, Sep 21, 2012 at 7:47 AM, Mark Lawrence  wrote:

On 20/09/2012 22:06, Gelonida N wrote:


I'd like to implement the equivalent functionality of the unix command
/usr/bin/which

The function should work under Linux and under windows.

Did anybody already implement such a function.


Searching found nothing obvious to me :(


I was afraid so, but wanted to be sure



If not, is there a portable way of splitting the environment variable
PATH?

With os.sep ?


os.sep is the directory separator, but os.pathsep may be what you
want.


Thanks,
os.pathsep was the missing piece for portably splitting the searchpath


 Between that and os.getenv('path') you can at least get the
directories. Then on Windows, you also need to check out
os.getenv('pathext') and split _that_ on the semicolon, and try each
of those as a file extension. I'm not sure whether or not Windows will
add extensions from pathext if one is given on the command line - for
instance, if typing "foo.exe" will search for "foo.exe.bat" - but the
basics are there.

For what I am doing I can even skip trying the pathexts, the ext is 
already given, but good to know :-)




Alternatively, there may be a Win32 API funct5ion that does this.
Would be worth a look.


Yeah true, but ideally I'd like to avoid platform detection and
just have a generic function.




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


Re: portable way of locating an executable (like which)

2012-09-20 Thread Gelonida N

On 09/21/2012 12:04 AM, Jason Swails wrote:



Thanks a lot Jason,



I've used the following in programs I write:

def which(program):
def is_exe(fpath):
   return os.path.exists(fpath) and os.access(fpath, os.X_OK)

fpath, fname = os.path.split(program)
if fpath:
   if is_exe(program):
  return program
else:
   for path in os.getenv("PATH").split(os.pathsep):
  exe_file = os.path.join(path, program)
  if is_exe(exe_file):
 return exe_file
return None

IIRC, I adapted it from StackOverflow.  I know it works on Linux and Mac
OS X, but not sure about windows (since I don't know if PATH works the
same way there).


I'll try it, the script looks reasonably portable (using os.pathsep)
to really replicate which I had probably to add os.getenv('pathext')
as Chris mentioned.
However for my current use case this is not necessarily required.


HTH,
Jason






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


Re: looping in array vs looping in a dic

2012-09-20 Thread giuseppe . amatulli
Hi Ian and MRAB
thanks to you input i have improve the speed  of my code. Definitely reading in 
dic() is faster. I have one more question.
In the dic() I calculate the sum of the values, but i want count also the 
number of observation, in order to calculate the average in the end. 
Should i create a new dic() or is possible to do in the same dic().
Here in the final code. 
Thanks Giuseppe
  


rows = dsCategory.RasterYSize
cols = dsCategory.RasterXSize

print("Generating output file %s" %(dst_file))

start = time()

unique=dict()

for irows in xrange(rows):
valuesRaster=dsRaster.GetRasterBand(1).ReadAsArray(0,irows,cols,1)
valuesCategory=dsCategory.GetRasterBand(1).ReadAsArray(0,irows,cols,1)
for icols in xrange(cols):
if ( valuesRaster[0,icols] != no_data_Raster ) and ( 
valuesCategory[0,icols] != no_data_Category ) :
row = valuesCategory[0, icols],valuesRaster[0, icols]
if row[0] in unique :
unique[row[0]] += row[1]
else:
unique[row[0]] = 0+row[1] # this 0 was add if not the first 
observation was considered = 0

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


Re: portable way of locating an executable (like which)

2012-09-20 Thread Mark Lawrence

On 21/09/2012 00:15, Gelonida N wrote:

On 09/21/2012 12:04 AM, Jason Swails wrote:



Thanks a lot Jason,



I've used the following in programs I write:

def which(program):
def is_exe(fpath):
   return os.path.exists(fpath) and os.access(fpath, os.X_OK)

fpath, fname = os.path.split(program)
if fpath:
   if is_exe(program):
  return program
else:
   for path in os.getenv("PATH").split(os.pathsep):
  exe_file = os.path.join(path, program)
  if is_exe(exe_file):
 return exe_file
return None

IIRC, I adapted it from StackOverflow.  I know it works on Linux and Mac
OS X, but not sure about windows (since I don't know if PATH works the
same way there).


I'll try it, the script looks reasonably portable (using os.pathsep)
to really replicate which I had probably to add os.getenv('pathext')
as Chris mentioned.
However for my current use case this is not necessarily required.


HTH,
Jason








http://nedbatchelder.com/code/utilities/wh_py.html

--
Cheers.

Mark Lawrence.

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


Re: looping in array vs looping in a dic

2012-09-20 Thread MRAB

On 2012-09-21 00:35, giuseppe.amatu...@gmail.com wrote:

Hi Ian and MRAB
thanks to you input i have improve the speed  of my code. Definitely reading in 
dic() is faster. I have one more question.
In the dic() I calculate the sum of the values, but i want count also the 
number of observation, in order to calculate the average in the end.
Should i create a new dic() or is possible to do in the same dic().
Here in the final code.
Thanks Giuseppe


Keep it simple. Use 2 dicts.




rows = dsCategory.RasterYSize
cols = dsCategory.RasterXSize

print("Generating output file %s" %(dst_file))

start = time()

unique=dict()

for irows in xrange(rows):
 valuesRaster=dsRaster.GetRasterBand(1).ReadAsArray(0,irows,cols,1)
 valuesCategory=dsCategory.GetRasterBand(1).ReadAsArray(0,irows,cols,1)
 for icols in xrange(cols):
 if ( valuesRaster[0,icols] != no_data_Raster ) and ( 
valuesCategory[0,icols] != no_data_Category ) :
 row = valuesCategory[0, icols],valuesRaster[0, icols]
 if row[0] in unique :
 unique[row[0]] += row[1]
 else:
 unique[row[0]] = 0+row[1] # this 0 was add if not the first 
observation was considered = 0


You could use defaultdict instead:

from collections import defaultdict

unique = defaultdict(int)
...
 category, raster = valuesCategory[0, icols], 
valuesRaster[0, icols]

 unique[category] += raster

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


Re: portable way of locating an executable (like which)

2012-09-20 Thread Nobody
On Thu, 20 Sep 2012 23:06:46 +0200, Gelonida N wrote:

> I'd like to implement the equivalent functionality of the unix command
> /usr/bin/which
> 
> The function should work under Linux and under windows.

Note that "which" attempts to emulate the behaviour of execvp() etc. The
exec(3) manpage will explain the precise algorithm used (e.g. they skip
files for which the process lacks execute permission).

Also, note that the shell has built-in commands, functions, and aliases in
addition to programs. The "type" built-in command performs a similar
function to "which" but using the shell's semantics. On some systems,
the default configuration may alias "which" to "type".

On Windows, there's a host of different "execute program" interface, all
with subtly different semantics: which extensions they will run, which
extensions can be omitted, which paths are used (e.g. %PATH%, paths
from the registry, current directory).

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


Re: portable way of locating an executable (like which)

2012-09-20 Thread Dave Angel
On 09/20/2012 06:04 PM, Jason Swails wrote:
> On Thu, Sep 20, 2012 at 5:06 PM, Gelonida N  wrote:
>
>> I'd like to implement the equivalent functionality of the unix command
>> /usr/bin/which
>>
>> The function should work under Linux and under windows.
>>
>> Did anybody already implement such a function.
>> If not, is there a portable way of splitting the environment variable PATH?
>>
> I've used the following in programs I write:
>
> def which(program):
>def is_exe(fpath):
>   return os.path.exists(fpath) and os.access(fpath, os.X_OK)
>
>fpath, fname = os.path.split(program)
>if fpath:
>   if is_exe(program):
>  return program
>else:
>   for path in os.getenv("PATH").split(os.pathsep):
>  exe_file = os.path.join(path, program)
>  if is_exe(exe_file):
> return exe_file
>return None
>
> IIRC, I adapted it from StackOverflow.  I know it works on Linux and Mac OS
> X, but not sure about windows (since I don't know if PATH works the same
> way there).
>

I don't have a Windows machine set up right now, but I believe there are
two more directories to search, besides the ones described in the PATH
variable.

One is the current directory, and the other is the Windows directory
(maybe also the xxx/system32 or something).

They don't have analogues in Linux or Mac, as far as I know.

-- 

DaveA

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


Re: portable way of locating an executable (like which)

2012-09-20 Thread Andrew Berg
On 2012.09.20 21:31, Dave Angel wrote:
> I don't have a Windows machine set up right now, but I believe there are
> two more directories to search, besides the ones described in the PATH
> variable.
> 
> One is the current directory, and the other is the Windows directory
> (maybe also the xxx/system32 or something).
Those system directories are in the path by default.

-- 
CPython 3.3.0rc2 | Windows NT 6.1.7601.17835
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Obnoxious postings from Google Groups

2012-09-20 Thread Robert Miles

On 9/16/2012 8:18 AM, Ben Finney wrote:

Νικόλαος Κούρας  writes:


Iam sorry i didnt do that on purpose and i dont know how this is done.

Iam positng via google groups using chrome, thats all i know.


It is becoming quite clear that some change has happened recently to
Google Groups that makes posts coming from there rather more obnoxious
than before. And there doesn't seem to be much its users can do except
use something else.

Using Google Groups for posting to Usenet has been a bad idea for a long
time, but now it just seems to be a sure recipe for annoying the rest of
us. Again, not something you have much control over, except to stop
using Google Groups.


Could this mean that Google wants all the spam posted through Google
Groups to look obnoxious to the rest of Usenet that the spammers will go 
elsewhere?


Robert Miles

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


python idioms : some are confusing

2012-09-20 Thread Vineet
Amongst the python idioms, how the below-mentioned make sense?

## There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.

--- In programming, there can be a number of ways, equally efficient, to do 
certain  thing.

## Although never is often better than *right* now.

--- How come "never" is better that "right now" ?


Any thoughts?...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python idioms : some are confusing

2012-09-20 Thread alex23
On Sep 21, 3:34 pm, Vineet  wrote:
> Amongst the python idioms, how the below-mentioned make sense?
> ## There should be one-- and preferably only one --obvious way to do it.
> --- In programming, there can be a number of ways, equally efficient, to do 
> certain  thing.

This isn't talking about your Python code as much as about Python
itself. For example, in Python 2.x you can use either `open` or `file`
to open a file, with `file` being a factory function for creating file
objects, and `open` using it internally. In Python 3.x, `file` is no
longer a built-in, as it produced a point of confusion as to which was
the one obvious way to open a file.

> ## Although never is often better than *right* now.
> --- How come "never" is better that "right now" ?

It's better to not add a language feature than it is to add it poorly,
especially when you endeavour to provide backwards compatibility as
much as possible within major versions.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python idioms : some are confusing

2012-09-20 Thread Chris Rebert
On Thu, Sep 20, 2012 at 10:34 PM, Vineet  wrote:
> Amongst the python idioms, how the below-mentioned make sense?

These aren't idioms (that term has a specific technical meaning in
programming); they're *way* too abstract to be idioms. "Design
principles" or "design guidelines" would be a better description.

> ## There should be one-- and preferably only one --obvious way to do it.
> Although that way may not be obvious at first unless you're Dutch.
>
> --- In programming, there can be a number of ways, equally efficient, to do 
> certain  thing.

Yes, but that brings with it the cost of having to understand/learn
them all, because you'll encounter them when
reading/maintaining/modifying others' code. And you'll have to
evaluate them all to choose which one you should use (which might even
vary from moment to moment depending on the circumstances). And you'll
have to watch out for subtle variants that actually do something
significantly different. Better to keep things simple in the X% of
cases where the differences don't matter enough, and save those brain
cycles for other, more important things.

See also: the so-called "paradox of choice".
Further reading: the criticisms on
http://c2.com/cgi/wiki?ThereIsMoreThanOneWayToDoIt

> ## Although never is often better than *right* now.
>
> --- How come "never" is better that "right now" ?

Because "right now" is so quick that it was likely hastily hacked
together and thus of poor (or at least lesser) quality.

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


Re: python idioms : some are confusing

2012-09-20 Thread Chris Angelico
I'm responding to the OP here, not to Alex, but I'm quoting his text
to expand on it. :)

On Fri, Sep 21, 2012 at 3:52 PM, alex23  wrote:
> On Sep 21, 3:34 pm, Vineet  wrote:
>> Amongst the python idioms, how the below-mentioned make sense?
>> ## There should be one-- and preferably only one --obvious way to do it.
>> --- In programming, there can be a number of ways, equally efficient, to do 
>> certain  thing.
>
> This isn't talking about your Python code as much as about Python
> itself.

The "it" in the zen there refers to some programming task. For
instance, there's only one obvious way to increment an integer:

spam += 1

Python's philosophy is to have just that, and to not trouble itself
with "spam++" and "++spam" and the distinction between them. As a C
programmer, I'm quite accustomed to them, and know what they mean, but
not everyone does. And don't get me started on "&&" vs "and" in PHP...
Python is a simpler and cleaner language for not having superfluous
operators.

>> ## Although never is often better than *right* now.
>> --- How come "never" is better that "right now" ?
>
> It's better to not add a language feature than it is to add it poorly,
> especially when you endeavour to provide backwards compatibility as
> much as possible within major versions.

The compatibility issue is the thing here. It's better to get
something good now rather than dither for another fifty years, because
the longer you dally, the more code will be written using third party
libraries. But it's better to not put it into the standard library at
all than to put in a messy API that now can't be changed because
code's using it.

The Zen of Python is a whole lot of tradeoffs and ideas. Several of
them balance each other directly. Some, while not contradicted in the
Zen itself, are still violated at times in the language and/or stdlib.
They're principles, not laws, and need to be read with the
understanding that people who write code are intelligent, thinking
beings (though a quick look at TheDailyWTF.com proves that this is not
universal).

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


Re: python idioms : some are confusing

2012-09-20 Thread Vineet
Oh I see !
On these lines mentioned by you, I can now sense the sense.
Thanks.

On Friday, 21 September 2012 11:22:45 UTC+5:30, alex23  wrote:
> On Sep 21, 3:34 pm, Vineet  wrote:
> 
> > Amongst the python idioms, how the below-mentioned make sense?
> 
> > ## There should be one-- and preferably only one --obvious way to do it.
> 
> > --- In programming, there can be a number of ways, equally efficient, to do 
> > certain  thing.
> 
> 
> 
> This isn't talking about your Python code as much as about Python
> 
> itself. For example, in Python 2.x you can use either `open` or `file`
> 
> to open a file, with `file` being a factory function for creating file
> 
> objects, and `open` using it internally. In Python 3.x, `file` is no
> 
> longer a built-in, as it produced a point of confusion as to which was
> 
> the one obvious way to open a file.
> 
> 
> 
> > ## Although never is often better than *right* now.
> 
> > --- How come "never" is better that "right now" ?
> 
> 
> 
> It's better to not add a language feature than it is to add it poorly,
> 
> especially when you endeavour to provide backwards compatibility as
> 
> much as possible within major versions.

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


Is there a public API equvalent for urllib2.parse_http_list?

2012-09-20 Thread Cosmia Luna
I'm porting my code to python3, and found there is no parse_http_list in any 
module of urllib of python3.

So, is there a public API equvalent for urllib2.parse_http_list?

Thanks.


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


Re: python idioms : some are confusing

2012-09-20 Thread Steven D'Aprano
On Thu, 20 Sep 2012 22:34:48 -0700, Vineet wrote:

> Amongst the python idioms, how the below-mentioned make sense?

They're not Python idioms. Idioms are common pieces of code, like looping:

for item in sequence:
do_something

What you have quoted are parts of the Zen of Python, which is 
deliberately named. Zen koans are notorious for being contradictory and 
impossible to understand.

As Terry Pratchett wrote:

In the second scroll of Wen the Eternally Surprised a story
is written concerning one day when the apprentice Clodpool, 
in a rebellious mood, approached Wen and spake thusly: 
"Master, what is the difference between a humanistic, monastic
system of belief in which wisdom is sought by means of an 
apparently nonsensical system of questions and answers, and a 
lot of mystic gibberish made up on the spur of the moment?" 
Wen considered this for some time, and at last said: "A fish!" 
And Clodpool went away, satisfied.
-- (Terry Pratchett, Thief of Time)


So be careful about over-interpreting the Zen of Python. Half of it is 
meant to followed seriously, half is meant as a joke, and half is meant 
as a guideline only.


> ## There should be one-- and preferably only one --obvious way to do it.
> Although that way may not be obvious at first unless you're Dutch.

This tells us that for any task you might want to do in Python, there 
should be some way to do it which is obvious. It is not enough that there 
is some (hard to find, convoluted) way to do it, it should be obvious. 
And while it isn't forbidden to be two or more obvious ways, it is better 
if there is only one.

The joke is that even this single sentence goes against its own advice. 
There are at least three obvious ways to put a parenthetical aside in a 
sentence:

There should be one--and preferably only one--obvious way to do it.
There should be one -- and preferably only one -- obvious way to do it.
There should be one (and preferably only one) obvious way to do it.

The author of the Zen deliberately choose a fourth, non-obvious way.

Finally, the second line warns that although Python has many obvious ways 
to solve things, they may only be obvious to the creator of Python, Guido 
van Rossum, who is Dutch.


> --- In programming, there can be a number of ways, equally efficient, to
> do certain  thing.

The Zen refers to the philosophy that Python the language should provide 
an obvious way to solve a problem. The emphasis is on the *obvious* part, 
not the *one* part.


> ## Although never is often better than *right* now.
> 
> --- How come "never" is better that "right now" ?


Solving a problem in the language -- adding a new language feature such 
as a keyword, new syntax, a library, etc. -- should only be done when 
that new feature brings more benefit than problems. But sometimes a new 
feature might bring more problems than benefits. In this case, it is 
better to *never* solve that problem *in the language* than to add a 
feature that solves the problem badly and causes more problems than it 
solves. E.g. multi-line lambdas.

The problem is that once you add a feature to the language, it becomes 
almost impossible to remove it. You are stuck with it nearly forever, or 
at least for many years. So better to not add it than to be stuck with a 
bad feature.



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


Re: Storing of folder structure in SQL DB

2012-09-20 Thread santhosh . sweetmemory
folderid   name parentid

1  cricket 0
2   india  1
3   sachin 2
4  tennis  0
5  saniamirza  4

i need coding for this table..folder id 'll automatically populate..
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a public API equvalent for urllib2.parse_http_list?

2012-09-20 Thread Cosmia Luna
On Friday, September 21, 2012 2:22:08 PM UTC+8, Cosmia Luna wrote:
> I'm porting my code to python3, and found there is no parse_http_list in any 
> module of urllib of python3.
> 
> 
> 
> So, is there a public API equvalent for urllib2.parse_http_list?
> 
> 
> 
> Thanks.
> 
> 
> 
> 
> 
> Cosmia Luna

I'm sorry, but I found it at urllib.request.parse_http_list.

But I still want to know where is a DOCUMENTED equivalent of this function, or 
the python team forgot to document it?

Thanks.


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


Re: python idioms : some are confusing

2012-09-20 Thread Steven D'Aprano
On Thu, 20 Sep 2012 22:52:45 -0700, alex23 wrote:

> On Sep 21, 3:34 pm, Vineet  wrote:
>> Amongst the python idioms, how the below-mentioned make sense? ## There
>> should be one-- and preferably only one --obvious way to do it. --- In
>> programming, there can be a number of ways, equally efficient, to do
>> certain  thing.
> 
> This isn't talking about your Python code as much as about Python
> itself. For example, in Python 2.x you can use either `open` or `file`
> to open a file, with `file` being a factory function for creating file
> objects, and `open` using it internally. In Python 3.x, `file` is no
> longer a built-in, as it produced a point of confusion as to which was
> the one obvious way to open a file.

I don't think that's the reason. I think the reason is that moving the 
built-in file into the _io library gives the developers a lot more 
flexibility in how they handle text and binary files. E.g.:


py> open('junk', 'w')
<_io.TextIOWrapper name='junk' mode='w' encoding='UTF-8'>

py> open('junk', 'wb')
<_io.BufferedWriter name='junk'>

py> open('junk', 'wb', buffering=0)
<_io.FileIO name='junk' mode='wb'>


The open() function now can return three (or more?) types instead of 
having a single built-in type handle all cases.



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


Re: Storing of folder structure in SQL DB

2012-09-20 Thread santhosh . sweetmemory
On Friday, September 21, 2012 11:57:05 AM UTC+5:30, santhosh.s...@gmail.com 
wrote:
> folderid   name parentid
> 
> 
> 
> 1  cricket 0
> 
> 2   india  1
> 
> 3   sachin 2
> 
> 4  tennis  0
> 
> 5  saniamirza  4
> 
> 
> 
> i need coding for this table..folder id 'll automatically populate..

in asp.net or sql
-- 
http://mail.python.org/mailman/listinfo/python-list