Re: pythonOCC examples doesn't work?

2012-09-12 Thread Dwight Hutto
So used to google, forgot to check the python docs:

http://docs.python.org/faq/windows.html

and this should be useful as well, which is from:

http://stackoverflow.com/questions/5030362/how-to-use-opencv-in-python

  "I suspect you have the same problem I've run into. If you have a 64-bit
version of Python, it cannot load 32-bit DLLs. OpenCV currently only ships
32-bit binaries. If you want 64-bit .pyd and .dll files, you have to
compile them yourself. There are some
instructionson the
OpenCV Wiki, but it's not for the faint of heart. Expect to have a
substantial time investment.

The easiest solution is to:

   1. Uninstall 64-bit Python
   2. Install a 32-bit distribution.

The PythonXY distribution includes pyopencv -- a good set of OpenCV hooks.
The only limitation is that it's 32-bit, so don't make plans to process
gigapixel astronomy data with it! ;)

If you *must* have the 64-bit version, follow these
instructionsto
get it OpenCV to compile with Visual Studio 2010. There's a discussion
on stackoverflow that describes building 64-bit apps with VC Express."


And again, check on the pywin mailing list



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


Re: pythonOCC examples doesn't work?

2012-09-12 Thread Mark Lawrence

On 12/09/2012 08:19, Dwight Hutto wrote:

So used to google, forgot to check the python docs:

http://docs.python.org/faq/windows.html

and this should be useful as well, which is from:

http://stackoverflow.com/questions/5030362/how-to-use-opencv-in-python

   "I suspect you have the same problem I've run into. If you have a 64-bit
version of Python, it cannot load 32-bit DLLs. OpenCV currently only ships
32-bit binaries. If you want 64-bit .pyd and .dll files, you have to
compile them yourself. There are some
instructionson the
OpenCV Wiki, but it's not for the faint of heart. Expect to have a
substantial time investment.

The easiest solution is to:

1. Uninstall 64-bit Python
2. Install a 32-bit distribution.

The PythonXY distribution includes pyopencv -- a good set of OpenCV hooks.
The only limitation is that it's 32-bit, so don't make plans to process
gigapixel astronomy data with it! ;)

If you *must* have the 64-bit version, follow these
instructionsto
get it OpenCV to compile with Visual Studio 2010. There's a discussion
on stackoverflow that describes building 64-bit apps with VC Express."


And again, check on the pywin mailing list




All very impressive, but as Dennis Lee Bieber has pointed out and which 
I suspected there is a binary incompatibility between the OP's Python 
version and pythonOCC version.  Others would be able to see this for 
themselves but you insist on sending email without context.  Please 
don't do this.



--
Cheers.

Mark Lawrence.

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


Re: pythonOCC examples doesn't work?

2012-09-12 Thread Dwight Hutto
On Wed, Sep 12, 2012 at 3:37 AM, Mark Lawrence  wrote:
> On 12/09/2012 08:19, Dwight Hutto wrote:
>>
>> So used to google, forgot to check the python docs:
>>
>> http://docs.python.org/faq/windows.html
>>
>> and this should be useful as well, which is from:
>>
>> http://stackoverflow.com/questions/5030362/how-to-use-opencv-in-python
>>
>>"I suspect you have the same problem I've run into. If you have a
>> 64-bit
>> version of Python, it cannot load 32-bit DLLs. OpenCV currently only ships
>> 32-bit binaries. If you want 64-bit .pyd and .dll files, you have to
>> compile them yourself. There are some
>> instructionson the
>>
>> OpenCV Wiki, but it's not for the faint of heart. Expect to have a
>> substantial time investment.
>>
>> The easiest solution is to:
>>
>> 1. Uninstall 64-bit Python
>> 2. Install a 32-bit distribution.
>>
>>
>> The PythonXY distribution includes pyopencv -- a good set of OpenCV hooks.
>> The only limitation is that it's 32-bit, so don't make plans to process
>> gigapixel astronomy data with it! ;)
>>
>> If you *must* have the 64-bit version, follow these
>>
>> instructionsto
>>
>> get it OpenCV to compile with Visual Studio 2010. There's a discussion
>> on stackoverflow that describes building 64-bit apps with VC Express."
>>
>>
>> And again, check on the pywin mailing list
>>
>>
>
> All very impressive, but as Dennis Lee Bieber has pointed out and which I
> suspected there is a binary incompatibility between the OP's Python version
> and pythonOCC version.

DIdn't see this, I was too busy helping the OP search for an answer,
and educate us both at the same time.

 Others would be able to see this for themselves but
> you insist on sending email without context.  Please don't do this.


How are my emails without context? I'm referring the OP to the docs,
as well as posts related to their question. It goes to use google, and
RTFM, and putting it politely to them.

I could summarize, but they have to do the real reading. I'm not
researching this, and if I was, I'd charge for the time. This is to
show that things can get complex if you don't use google, or read the
docs.

Why does the OP keep asking here, when there are answers out there.
especially on the pywin list, which Windows users are usually referred
to.

Please point out what's out of context. The links and references place
it into context if the OP finds them useful, and I believe I searched
well for them.

Would the OP like to tell me I wasn't helpful? Because now they're
probably on a search to figure out how to make these compatible, which
means more questions, and more reading.

Plus the OP said he's on 64. Read the quote from stackoverflow:


   "I suspect you have the same problem I've run into. If you have a 64-bit
version of Python, it cannot load 32-bit DLLs. OpenCV currently only ships
32-bit binaries. If you want 64-bit .pyd and .dll files, you have to
compile them yourself. "

A different program, but roughly the same in analogy, I do believe.

And where in here was the OP referenced to the python docs on windows:

>> http://docs.python.org/faq/windows.html

Was that out of context as well?

Let's not argue about this, I was pointing them to what I saw as the
best possible resources to overcome his current problem, and it was
all in context of the conversation as far as I'm concerned.

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


Re: Which Version of Python?

2012-09-12 Thread alex23
On 12 Sep, 16:31, Mark Lawrence  wrote:
> Perhaps this will sway youhttp://docs.python.org/dev/whatsnew/3.3.html
> There is no longer an equivalent document for the Python 1.x or 2.x
> series of releases.

Perhaps not for 1.x but the 2.x series is still covered:
http://docs.python.org/dev/whatsnew/index.html

Actually, 1.6 is included here:
http://www.python.org/download/releases/1.6.1/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Which Version of Python?

2012-09-12 Thread Ramchandra Apte
On Wednesday, 12 September 2012 14:04:56 UTC+5:30, alex23  wrote:
> On 12 Sep, 16:31, Mark Lawrence  wrote:
> 
> > Perhaps this will sway youhttp://docs.python.org/dev/whatsnew/3.3.html
> 
> > There is no longer an equivalent document for the Python 1.x or 2.x
> 
> > series of releases.
> 
> 
> 
> Perhaps not for 1.x but the 2.x series is still covered:
> 
> http://docs.python.org/dev/whatsnew/index.html
> 
> 
> 
> Actually, 1.6 is included here:
> 
> http://www.python.org/download/releases/1.6.1/

I think he meant the length of the document.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Which Version of Python?

2012-09-12 Thread Ramchandra Apte
On Wednesday, 12 September 2012 14:11:56 UTC+5:30, Ramchandra Apte  wrote:
> On Wednesday, 12 September 2012 14:04:56 UTC+5:30, alex23  wrote:
> 
> > On 12 Sep, 16:31, Mark Lawrence  wrote:
> 
> > 
> 
> > > Perhaps this will sway youhttp://docs.python.org/dev/whatsnew/3.3.html
> 
> > 
> 
> > > There is no longer an equivalent document for the Python 1.x or 2.x
> 
> > 
> 
> > > series of releases.
> 
> > 
> 
> > 
> 
> > 
> 
> > Perhaps not for 1.x but the 2.x series is still covered:
> 
> > 
> 
> > http://docs.python.org/dev/whatsnew/index.html
> 
> > 
> 
> > 
> 
> > 
> 
> > Actually, 1.6 is included here:
> 
> > 
> 
> > http://www.python.org/download/releases/1.6.1/
> 
> 
> 
> I think he meant the length of the document.

Sorry, Mark must have meant theres no "What's New" document of the same length 
(its very long).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Double sided double underscored variable names

2012-09-12 Thread Joshua Landau
On 12/09/2012, Chris Angelico  wrote:
> On Wed, Sep 12, 2012 at 11:38 AM, Joshua Landau
>  wrote:
>> On 12 September 2012 02:14, Steven D'Aprano
>>  wrote:
>>>
>>> On Wed, 12 Sep 2012 08:52:10 +1000, Chris Angelico wrote:
>>>
>>> > Inline functions? I like this idea. I tend to want them in pretty much
>>> > any language I write in.
>>>
>>> What do you mean by in-line functions? If you mean what you literally
>>> say, I would answer that Python has that with lambda.
>>>
>>> But I guess you probably mean something more like macros.
>>
>> No, just multi-line lambda. Macros, if my knowledge of lower-level
>> languages
>> is valid, would be sorta' silly in Python.
>
> Ah, okay. I was thinking more along the lines of what you call macros,
> but in the C++ sense of inline functions. In C, macros are handled at
> precompilation stage, and are dangerous. Classic example:
>
> #define squared(x) x*x
>
> x_squared = squared(6+7)
>
> So your macros end up littered with parentheses, and it still doesn't
> solve anything, as the argument still gets evaluated twice. (A problem
> if it has side effects - eg if it's a function call.)
>
> What I'm thinking of, though, is like C++ functions. You can put the
> 'inline' keyword onto any function, and the compiler will do its best
> to inline it (in fact, a good optimizing compiler will inline things
> regardless, but that's a separate point). I can write:
>
> inline int squared(int x) {return x*x;}
>
> and C++ will add no function overhead, but will still do all the
> proper evaluation order etc.
>
> Of course, C++ doesn't allow monkeypatching, so you'll never have
> semantic differences from inlining. It's just a performance question.
> But I use inline functions like constants - for instance, I could
> create a function that converts a database ID into an internal
> reference number, and I can change the definition of that function in
> one place and have it apply everywhere, just like if I wanted to
> change the definition of math.PI to 3.142857 for fun one day. Of
> course I can use a normal (out-of-line) function for this, but that
> has overhead in most languages. Hence, wanting inline functions.

Interesting. I'd overestimated macros and underestimated inline functions.

I am not sure how to make a version of that with scope-compatibility. Inlining
inline_def f(y): x = y +1
would hopefully not change the outside scope*, but I'm not sure how to
make that.

I could make it work by banning "=", but then it's almost a macro but with
internal_a = input_a
internal_b = input_b
...
at the start...

* If I understand rightly
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: generators as decorators simple issue

2012-09-12 Thread pyjoshsys
The output is still not what I want. Now runtime error free, however the output 
is not what I desire.



def setname(cls):
'''this is the proposed generator to call SetName on the object'''

try:
cls.SetName(cls.__name__)
except Exception as e:
print e
finally:
return cls

class Trial(object):
'''class to demonstrate with'''
def __init__(self):
object.__init__(self)
self.name = None

@classmethod
def SetName(cls, name):
cls.name = name

@setname
class Test(Trial):
'''i want SetName to be called by using setname as a decorator'''
def __init__(self):
Trial.__init__(self)



if __name__ == '__main__':
test = Test()
print 'instance'
print '', test.name #should be Test
print 'class'
print '', Test.name


The output is: python decors2.py 
instance
 None
class
 Test

I want: 
instance
 Test
class
 Test

Is this possible in this manner?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: generators as decorators simple issue

2012-09-12 Thread Oscar Benjamin
On Wed, 12 Sep 2012 03:22:31 -0700 (PDT), pyjoshsys 
 wrote:
The output is still not what I want. Now runtime error free, 

however the output is not what I desire.


def setname(cls):
'''this is the proposed generator to call SetName on the 

object'''


try:
cls.SetName(cls.__name__)
except Exception as e:
print e
finally:
return cls


I would write the function above in one line:

cls.name = name



class Trial(object):
'''class to demonstrate with'''
def __init__(self):
object.__init__(self)
self.name = None


Remove the line above. The instance attribute self.name is hiding the 
class attribute cls.name.


Oscar

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


Re: Single leading dash in member variable names?

2012-09-12 Thread Tim Chase
On 09/12/12 00:10, Dwight Hutto wrote:
> Not to jump in with another question(this seems somewhat relevant
> to the conversation, maybe not), but is this similar to a
> private,public, or protected class similar to the C type langs?

Close, but C-like languages tend to strictly enforce it, while in
Python it's more of a gentleman's agreement.  Python doesn't *stop*
you from mucking with them, but you've been advised that, if it
breaks, you get to keep both parts.

-tkc


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


Re: SAP MM Cupertino, CA

2012-09-12 Thread Diabolic Preacher
I really hoped for at least one mention of Python in there. Like
'knows how to use it to calculate numbers'. anything.

Does GMail learn from Google Groups' spam reporting?

Thanks

> 

>> Please explain what does this have to do with Python.

-- 
Diabolic Preacher
As Is
Blog: http://abusiveviews.wordpress.com/
Bookmarks: http://diigo.com/user/dpreacher
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: generators as decorators simple issue

2012-09-12 Thread pyjoshsys
so decorators only pass the object and not any instance of the object as the 
implied argument? Is this right?

The idea was to use @setname instead of instance.SetName(instance.__name__).

I thought decorators would do this, but it seems not.







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


Re: SAP MM Cupertino, CA

2012-09-12 Thread Dwight Hutto
Looks to me like someone recruiting experienced programmers, and there
is a demographic here who would probably like a job like that.

Definitely not considered SPAM by me. They're not selling, they're
trying to gainfully employ a good programmer from a list meant to
interact and learn.

If I had enough business, this is one of the places I'd look as well.
-- 
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SAP MM Cupertino, CA

2012-09-12 Thread Dwight Hutto
On Wed, Sep 12, 2012 at 6:58 AM, Diabolic Preacher  wrote:
> I really hoped for at least one mention of Python in there.

It does seem like a blanketed letter to several groups, but maybe
they're looking for any good programmer.

Once you learn a language, and in my opinion learn to algorithm, then
functionality/widget set to implement, you can do any of them.

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


Re: forked processes and testing

2012-09-12 Thread Kushal Kumaran
On Wed, Sep 12, 2012 at 3:50 PM, andrea crotti
 wrote:
> I wrote a decorator that takes a function, run it in a forked process
> and return the PID:
>
> def on_forked_process(func):
> from os import fork
> """Decorator that forks the process, runs the function and gives
> back control to the main process
> """
> def _on_forked_process(*args, **kwargs):
> pid = fork()
> if pid == 0:
> func(*args, **kwargs)
> sys.exit(0)
> else:
> return pid
>
> return _on_forked_process
>
> It seems in general to work but I'm not able to test it, for example this 
> fails:
>
> def test_on_forked_process(self):
> @utils.on_forked_process
> def _dummy_func():
> pass
>
> with self.assertRaises(SystemExit):
> retpid = _dummy_func()
> # pid of the son process should be always > 0
> self.assertTrue(retpid > 0)
>
>
> and I'm not sure why, nose doesn't like the Exit apparently even if
> it's happening in an unrelated proces..
>
> Any idea of how to make it testable or improve it?
>

Use os._exit instead of sys.exit in your child process.  Remember that
fork creates a new process that is running the same code as the
original process.  In your case, the child process also has your test
harness as the caller.  sys.exit raises an exception that reaches the
test harness in the child process, which interferes with your test
run.

> In theory probably I will not use it for production because I should
> use something smarter to control the various processes I need to run,
> but for my integration tests it's quite useful, because then I can
> kill the processes like
>
> except KeyboardInterrupt:
> from os import kill
> from signal import SIGTERM
> print("Killing sink and worker")
> kill(sink_pid, SIGTERM)
> kill(worker_pid, SIGTERM)
> --
> http://mail.python.org/mailman/listinfo/python-list

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


Re: Which Version of Python?

2012-09-12 Thread Jamie Paul Griffin
[ Ramchandra Apte wrote on Tue 11.Sep'12 at 19:58:29 -0700 ]

> On Tuesday, 11 September 2012 22:19:08 UTC+5:30, Charles Hottel  wrote:
> > I have a lot of programming experience in many different languages and now 
> > 
> > I want to learn Python.  Which version do you suggest I download, Python 
> > 2.x 
> > 
> > or Python 3.x ?  Also why should I prefer one over the other?
> > 
> > 
> > 
> > Right now I am thinkng Python 3.x as it has been out since 2008, but I have 
> > 
> > some concerns about backward compatibility with older packages that I might 
> > 
> > want to use.
> > 
> > 
> > 
> > Thanks for your ideas and help.
> 
> Use Python 3 because most packages support Python 3. Python 2.7 has many 
> features that Python 3 has so it will require minimal effort to write Python 
> 2.x code.

When I first started to learn Python - about a year ago - I got myself a book 
which focuses on version 3 but is does also include Version 2. The author wrote 
that because a number of supporting libraries have not been updated to work in 
Python 3, and where "it is felt that the theory still needs to be expounded 
upon" Python 2.x will be used in lieu of Python 3. I personally, as a beginner, 
have found this approach helpful. Although, when this book was published Python 
3.1 was the latest release, so of course since then said libraries most 
probably have been updated. So, as a fellow beginner i'd go for version 3.x.

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


Re: Parsing ISO date/time strings - where did the parser go?

2012-09-12 Thread Pete Forman
John Nagle  writes:

>I want to parse standard ISO date/time strings such as
>
>   2012-09-09T18:00:00-07:00
>
> into Python "datetime" objects.

Consider whether RFC 3339 might be a more suitable format.

It is a subset of ISO 8601 extended format.  Some of the restrictions are

  Year must be 4 digits
  Fraction separator is period, not comma
  All components including time-offset are mandatory, except for time-secfrac
  time-minute in time-offset is not optional, must use ±hh:mm or Z

Some latitude is allowed

  T may be replaced by e.g. space

Extra feature

  time-offset of -00:00 means UTC but local time is unknown

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


Boolean function on variable-length lists

2012-09-12 Thread Libra
Hello,

I need to implement a function that returns 1 only if all the values in a list 
satisfy given constraints (at least one constraint for each element in the 
list), and zero otherwise.

For example, I may have a list L = [1, 2, 3, 4] and the following constraints:
L[0] >= 1
L[1] <= 3
L[2] == 2
L[3] >= 3

In this case, the function returns 0 because the third constraint is not 
satisfied. 

With fixed-length lists, I can sometimes use a very naive approach and 
hard-code the constraints combined with AND. Nonetheless, the problems are:
1) even with fixed-length lists, the hard-code approach requires a lot of 
effort (especially with long lists) and is prone to error;
2) the constraints may change, so with a hard-code approach the effort grows 
exponentially;
3) I need to work on variable-length lists (generally, lists of numbers).

I can't figure out anything useful.
Could you please suggest me a suitable ways?
 
Thanks
Libra  
-- 
http://mail.python.org/mailman/listinfo/python-list


avoid the redefinition of a function

2012-09-12 Thread Jabba Laci
Hi,

I have an installer script that contains lots of little functions. It
has an interactive menu and the corresponding function is called. Over
time it grew long and when I want to add a new function, I should give
a unique name to that function. However, Python allows the
redefinition of functions:

#!/usr/bin/env python

def step_1():
print 1

def step_1():
print 2

step_1()

This will call the 2nd function. Now my functions are called step_ID
(like step_27(), step_28(), etc.). How to avoid the danger of
redefinition? Now, when I write a new function, I search for its name
to see if it's unique but there must be a better way.

Thanks,

Laszlo
P.S.: the script is here ( https://github.com/jabbalaci/jabbatron ) if
you are interested. It's made for Ubuntu.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Boolean function on variable-length lists

2012-09-12 Thread Jussi Piitulainen
Libra writes:

> Hello,
> 
> I need to implement a function that returns 1 only if all the values
> in a list satisfy given constraints (at least one constraint for
> each element in the list), and zero otherwise.
> 
> For example, I may have a list L = [1, 2, 3, 4] and the following
> constraints:
> L[0] >= 1
> L[1] <= 3
> L[2] == 2
> L[3] >= 3
> 
> In this case, the function returns 0 because the third constraint is
> not satisfied.

So you would associate each constraint with an index. You could
maintain a list of constraints and apply it to the values as follows:

>>> cs = [ lambda x : x >= 1, lambda x : x <= 3, lambda x : x == 2,
...lambda x : x >= 3 ]
>>> { f(x) for f, x in zip(cs, [1,2,3,4]) }
{False, True}
>>> { f(x) for f, x in zip(cs, [1,2,2,4]) }
{True}
>>> 

Then map the return value to 0 if it contains a False, else to 1.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Boolean function on variable-length lists

2012-09-12 Thread Steven D'Aprano
On Wed, 12 Sep 2012 05:48:09 -0700, Libra wrote:

> Hello,
> 
> I need to implement a function that returns 1 only if all the values in
> a list satisfy given constraints (at least one constraint for each
> element in the list), and zero otherwise.

What are the restrictions on the constraints themselves?

Could they be arbitrarily complicated?

"Item 2 must be an even number divisible by 17 and 39 with at least eight 
digits but no greater than four million, unless today is Tuesday, in 
which case it must be equal to six exactly."

I'm going to assume you have a list of functions which take a single 
argument and then return True if it meets the constraint and False if it 
doesn't. Using your examples later:

> For example, I may have a list L = [1, 2, 3, 4] and the following
> constraints: 
> L[0] >= 1
> L[1] <= 3
> L[2] == 2
> L[3] >= 3

constraints = [ 
lambda x: x >= 1,
lambda x: x <= 3,
lambda x: x == 2,
lambda x: x >= 3,
]

ought to handle any reasonable constraint.

Then use the zip() function to match up constraints with values, and 
all() to make sure they are all met.

Since this sounds like homework, I won't give you the exact solution. If 
you still can't solve it, come back with your attempt and we'll give you 
a bit more help.


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


Re: avoid the redefinition of a function

2012-09-12 Thread Ramchandra Apte
On Wednesday, 12 September 2012 18:26:36 UTC+5:30, Jabba Laci  wrote:
> Hi,
> 
> 
> 
> I have an installer script that contains lots of little functions. It
> 
> has an interactive menu and the corresponding function is called. Over
> 
> time it grew long and when I want to add a new function, I should give
> 
> a unique name to that function. However, Python allows the
> 
> redefinition of functions:
> 
> 
> 
> #!/usr/bin/env python
> 
> 
> 
> def step_1():
> 
> print 1
> 
> 
> 
> def step_1():
> 
> print 2
> 
> 
> 
> step_1()
> 
> 
> 
> This will call the 2nd function. Now my functions are called step_ID
> 
> (like step_27(), step_28(), etc.). How to avoid the danger of
> 
> redefinition? Now, when I write a new function, I search for its name
> 
> to see if it's unique but there must be a better way.
> 
> 
> 
> Thanks,
> 
> 
> 
> Laszlo
> 
> P.S.: the script is here ( https://github.com/jabbalaci/jabbatron ) if
> 
> you are interested. It's made for Ubuntu.

Use a code checker such as PyLint (http://www.logilab.org/857 or pylint 
package).
Better idea:
I *strongly* recommend to never use names such as step_12. Use descriptive 
names and the problem will not occur.
Your project looks interesting. I can contribute. :-)
---
Bragging rights:SO account suspended
py2c, a Python to *pure* C/C++ translator, is my project (I am the author)
http://code.google.com/p/py2c/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Boolean function on variable-length lists

2012-09-12 Thread Tim Chase
On 09/12/12 08:02, Jussi Piitulainen wrote:
> Libra writes:
>> For example, I may have a list L = [1, 2, 3, 4] and the following
>> constraints:
>> L[0] >= 1
>> L[1] <= 3
>> L[2] == 2
>> L[3] >= 3
> 
> So you would associate each constraint with an index. You could
> maintain a list of constraints and apply it to the values as follows:
> 
 cs = [ lambda x : x >= 1, lambda x : x <= 3, lambda x : x == 2,
> ...lambda x : x >= 3 ]

This can even be decoupled a bit more for dynamic creation:

>>> lst = [1,2,3,4]
>>> import operator as o
>>> conditions = [
... (o.ge, 1),
... (o.le, 3),
... (o.eq, 2),
... (o.ge, 3),
... ]
>>> [op(v, constraint) for ((op, constraint), v) in zip(conditions,
lst)]
[True, True, False, True]
>>> all(compare(value, constraint) for ((compare, constraint),
value) in zip(conditions, lst))
False

Note that you'd also want to check len(conditions)==len(lst) for
obvious reasons. :-)

-tkc




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


Re: Boolean function on variable-length lists

2012-09-12 Thread Libra
On Wednesday, September 12, 2012 3:02:44 PM UTC+2, Jussi Piitulainen wrote:
 
> So you would associate each constraint with an index. You could
> maintain a list of constraints and apply it to the values as follows:

Yes, even though there could be more constraints for each value in the list (at 
least 1 constraint for each value)
> 
> >>> cs = [ lambda x : x >= 1, lambda x : x <= 3, lambda x : x == 2,
> 
> ...lambda x : x >= 3 ]
> 
> >>> { f(x) for f, x in zip(cs, [1,2,3,4]) }

Just to understand, with f(x) you are defining a function f with argument x, 
right? I didn't know it was possible to define functions in this way. Is this a 
case of anonymous function?
 
> {False, True}

Actually, I don't understand the output. Why it is both False and True?

> >>> { f(x) for f, x in zip(cs, [1,2,2,4]) }
> {True}

Ok.

Thank you very much


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


Re: Boolean function on variable-length lists

2012-09-12 Thread Libra
On Wednesday, September 12, 2012 3:11:42 PM UTC+2, Steven D'Aprano wrote:
> On Wed, 12 Sep 2012 05:48:09 -0700, Libra wrote:

> > I need to implement a function that returns 1 only if all the values in
> > a list satisfy given constraints (at least one constraint for each
> > element in the list), and zero otherwise.
> 
> What are the restrictions on the constraints themselves?
> Could they be arbitrarily complicated?
> "Item 2 must be an even number divisible by 17 and 39 with at least eight 
> digits but no greater than four million, unless today is Tuesday, in 
> which case it must be equal to six exactly."

Generally the constraints are quite simple, like the one in my example. But I 
can also have 2 or more constraints for each value:
L[0] >= 1
L[0] <= 5

To complicate a little, what about constraints like:
L[0] + L[2] >= 3

Thanks

> -- 
> 
> Steven

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


Re: avoid the redefinition of a function

2012-09-12 Thread Ken Seehart
Use lambda expressions to define some constraints:


gt = lambda x: lambda y: x>y
eq = lambda x: lambda y: x==y

constraints = [gt(2), eq(1)]
data = [3,1]

for i,c in enumerate(constraints):
print c(data[i])
   
   

On 9/12/2012 5:56 AM, Jabba Laci wrote:
> Hi,
>
> I have an installer script that contains lots of little functions. It
> has an interactive menu and the corresponding function is called. Over
> time it grew long and when I want to add a new function, I should give
> a unique name to that function. However, Python allows the
> redefinition of functions:
>
> #!/usr/bin/env python
>
> def step_1():
> print 1
>
> def step_1():
> print 2
>
> step_1()
>
> This will call the 2nd function. Now my functions are called step_ID
> (like step_27(), step_28(), etc.). How to avoid the danger of
> redefinition? Now, when I write a new function, I search for its name
> to see if it's unique but there must be a better way.
>
> Thanks,
>
> Laszlo
> P.S.: the script is here ( https://github.com/jabbalaci/jabbatron ) if
> you are interested. It's made for Ubuntu.




smime.p7s
Description: S/MIME Cryptographic Signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Boolean function on variable-length lists

2012-09-12 Thread Libra
On Wednesday, September 12, 2012 3:19:28 PM UTC+2, Libra wrote:

> > {False, True}
> Actually, I don't understand the output. Why it is both False and True?

Ok, I have understood now, I didn't noticed it was a set and not a list.
Regards


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


Re: Boolean function on variable-length lists

2012-09-12 Thread Jussi Piitulainen
Libra writes:
> On Wednesday, September 12, 2012 3:02:44 PM UTC+2, Jussi Piitulainen wrote:
>  
> > So you would associate each constraint with an index. You could
> > maintain a list of constraints and apply it to the values as
> > follows:
> 
> Yes, even though there could be more constraints for each value in
> the list (at least 1 constraint for each value)

Either you write more complex constraint functions, or you use more
complex data structures to hold them.

> > >>> cs = [ lambda x : x >= 1, lambda x : x <= 3, lambda x : x == 2,
> > 
> > ...lambda x : x >= 3 ]
> > 
> > >>> { f(x) for f, x in zip(cs, [1,2,3,4]) }
> 
> Just to understand, with f(x) you are defining a function f with
> argument x, right? I didn't know it was possible to define functions
> in this way. Is this a case of anonymous function?

The value of each lambda expression is a function. f(x) is a function
call, evaluated for each pair (f, x) from the list of pairs that the
zip returns.

{ ... for ... in ... } creates a set of the values, no duplicates.
[ ... for ... in ... ] creates a list of the values.

> > {False, True}
> 
> Actually, I don't understand the output. Why it is both False and
> True?

It's a set containing False and True. The False comes from the f(x)
where f = lambda x : x == 2, and x is 3. There is only one True
because I requested a set of the values.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Boolean function on variable-length lists

2012-09-12 Thread Oscar Benjamin
On 12 September 2012 14:25, Libra  wrote:

> On Wednesday, September 12, 2012 3:11:42 PM UTC+2, Steven D'Aprano wrote:
> > On Wed, 12 Sep 2012 05:48:09 -0700, Libra wrote:
>
> > > I need to implement a function that returns 1 only if all the values in
> > > a list satisfy given constraints (at least one constraint for each
> > > element in the list), and zero otherwise.
> >
> > What are the restrictions on the constraints themselves?
> > Could they be arbitrarily complicated?
> > "Item 2 must be an even number divisible by 17 and 39 with at least eight
> > digits but no greater than four million, unless today is Tuesday, in
> > which case it must be equal to six exactly."
>
> Generally the constraints are quite simple, like the one in my example.
> But I can also have 2 or more constraints for each value:
> L[0] >= 1
> L[0] <= 5
>

You can use:
   lambda x: 1 <= x and x <= 5
or
  lambda x: 1 <= x <= 5


> To complicate a little, what about constraints like:
> L[0] + L[2] >= 3


You could rewrite all your constraints as functions on the sequence of
values:
  lambda y: 1 <= y[0] <= 5
  lambda y: y[0] + y[2] >= 3

If all of your constraints are linear (like all of the ones you have shown)
then you can represent each one as a set of coefficients for a linear
projection of the list combined with a threshold value (if this last point
doesn't make sense then just ignore it).

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


Re: Boolean function on variable-length lists

2012-09-12 Thread Ken Seehart
Putting a few of peoples ideas together...


gt = lambda x: lambda y: x>y
eq = lambda x: lambda y: x==y

def constrain(c,d):
return all({f(x) for f, x in zip(c, d)})

constraints = [gt(2), eq(1)]
data0 = [1,1]
data1 = [3,1]
   
print constrain(constraints, data0)
print constrain(constraints, data1)




On 9/12/2012 6:37 AM, Jussi Piitulainen wrote:
> Libra writes:
>> On Wednesday, September 12, 2012 3:02:44 PM UTC+2, Jussi Piitulainen wrote:
>>  
>>> So you would associate each constraint with an index. You could
>>> maintain a list of constraints and apply it to the values as
>>> follows:
>> Yes, even though there could be more constraints for each value in
>> the list (at least 1 constraint for each value)
> Either you write more complex constraint functions, or you use more
> complex data structures to hold them.
>
>> cs = [ lambda x : x >= 1, lambda x : x <= 3, lambda x : x == 2,
>>> ...lambda x : x >= 3 ]
>>>
>> { f(x) for f, x in zip(cs, [1,2,3,4]) }
>> Just to understand, with f(x) you are defining a function f with
>> argument x, right? I didn't know it was possible to define functions
>> in this way. Is this a case of anonymous function?
> The value of each lambda expression is a function. f(x) is a function
> call, evaluated for each pair (f, x) from the list of pairs that the
> zip returns.
>
> { ... for ... in ... } creates a set of the values, no duplicates.
> [ ... for ... in ... ] creates a list of the values.
>
>>> {False, True}
>> Actually, I don't understand the output. Why it is both False and
>> True?
> It's a set containing False and True. The False comes from the f(x)
> where f = lambda x : x == 2, and x is 3. There is only one True
> because I requested a set of the values.




smime.p7s
Description: S/MIME Cryptographic Signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: avoid the redefinition of a function

2012-09-12 Thread D'Arcy Cain
On Wed, 12 Sep 2012 14:56:12 +0200
Jabba Laci  wrote:
> This will call the 2nd function. Now my functions are called step_ID
> (like step_27(), step_28(), etc.). How to avoid the danger of
> redefinition? Now, when I write a new function, I search for its name
> to see if it's unique but there must be a better way.

I think your real problem is how you name your functions.  Give them
names that describe the operation that they do such as
step_get_finagle, step_update_konkle, etc.  It's harder to duplicate
those unless you actually duplicate a function and it will make your
code much easier to read.

-- 
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: [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: avoid the redefinition of a function

2012-09-12 Thread Alister
On Wed, 12 Sep 2012 06:15:21 -0700, Ramchandra Apte wrote:

> On Wednesday, 12 September 2012 18:26:36 UTC+5:30, Jabba Laci  wrote:
>> Hi,
>> 
>> 
>> 
>> I have an installer script that contains lots of little functions. It
>> 
>> has an interactive menu and the corresponding function is called. Over
>> 
>> time it grew long and when I want to add a new function, I should give
>> 
>> a unique name to that function. However, Python allows the
>> 
>> redefinition of functions:
>> 
>> 
>> 
>> #!/usr/bin/env python
>> 
>> 
>> 
>> def step_1():
>> 
>> print 1
>> 
>> 
>> 
>> def step_1():
>> 
>> print 2
>> 
>> 
>> 
>> step_1()
>> 
>> 
>> 
>> This will call the 2nd function. Now my functions are called step_ID
>> 
>> (like step_27(), step_28(), etc.). How to avoid the danger of
>> 
>> redefinition? Now, when I write a new function, I search for its name
>> 
>> to see if it's unique but there must be a better way.
>> 
>> 
>> 
>> Thanks,
>> 
>> 
>> 
>> Laszlo
>> 
>> P.S.: the script is here ( https://github.com/jabbalaci/jabbatron ) if
>> 
>> you are interested. It's made for Ubuntu.
> 
> Use a code checker such as PyLint (http://www.logilab.org/857 or pylint
> package).
> Better idea:
> I *strongly* recommend to never use names such as step_12. Use
> descriptive names and the problem will not occur.
> Your project looks interesting. I can contribute. :-)
> ---
> Bragging rights:SO account suspended py2c, a Python to *pure* C/C++
> translator, is my project (I am the author)
> http://code.google.com/p/py2c/

+1 Regards the Naming of your functions it makes it harder for new users 
to read & understand the code (and yourself in 6 months!)



-- 
Overload -- core meltdown sequence initiated.
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: odt2sphinx 0.2.3 released

2012-09-12 Thread bamba777

ߒߤߒߡߜߦߡ ߠߧ


-Original Message-
From: "Christophe de Vienne"
Sent: Thu, 06 Sep 2012 15:15:23 +0200
To: [email protected]
Subject: odt2sphinx 0.2.3 released

Hello,

odt2sphinx 0.2.3 is now available on pypi :

http://pypi.python.org/pypi/odt2sphinx/.

Odt2sphinx convert OpenDocument Text file(s) to one or several .rst
files suitable for Sphinx.

Changes
---

* Fix filename generation by replacing any non-alphanumeric
character (issue #3).

* Fix handling of non-styled lists.


Regards,

Christophe de Vienne
--
http://mail.python.org/mailman/listinfo/python-announce-list

Support the Python Software Foundation:
http://www.python.org/psf/donations/



Sent using Eatoni's Triage
 app for
iPhone and iPad.-- 
http://mail.python.org/mailman/listinfo/python-list


Re: avoid the redefinition of a function

2012-09-12 Thread Michael Torrie
On 09/12/2012 06:56 AM, Jabba Laci wrote:
> I have an installer script that contains lots of little functions. It
> has an interactive menu and the corresponding function is called. Over
> time it grew long and when I want to add a new function, I should give
> a unique name to that function. However, Python allows the
> redefinition of functions:
> 
> #!/usr/bin/env python
> 
> def step_1():
> print 1
> 
> def step_1():
> print 2
> 
> step_1()
> 
> This will call the 2nd function. Now my functions are called step_ID
> (like step_27(), step_28(), etc.). How to avoid the danger of
> redefinition? Now, when I write a new function, I search for its name
> to see if it's unique but there must be a better way.

I don't understand the other poster's suggestion to your problem.  I
have looked at your script and think I understand how you are using
them.  Keep in mind that functions in python are just objects.  So
rather than define them as step_##() and then search the globals list
for them, why not just define them as descriptive functions (for
example, "install_java") and then put them into a list.

For example:

def install_java():
   pass

def install_tomcat():
   pass


steps = [install_java, install_tomcat, etc]
# or steps.append(install_java) if steps already has stuff in it, etc

# then:
for step in steps:
   step()


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


using text file to get ip address from hostname

2012-09-12 Thread dkatorza
hello ,

i'm new to Python and i searched the web and could not find an answer for my 
issue.

i need to get an ip address from list of hostnames which are in a textfile.

this is what i have so far 
--
#!/usr/bin/env python
#Get the IP Address

import socket
hostname = 'need it to read from a text file'
addr = socket.gethostbyname(hostname)
print 'The address of ', hostname, 'is', addr 

---

any idea ? 
sorry for my english

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


Re: Boolean function on variable-length lists

2012-09-12 Thread Mark Lawrence

On 12/09/2012 14:51, Ken Seehart wrote:

[snip]

Could you please not top post on this list, thanks.

--
Cheers.

Mark Lawrence.

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


Re: using text file to get ip address from hostname

2012-09-12 Thread Chris Angelico
On Thu, Sep 13, 2012 at 12:24 AM,   wrote:
> i'm new to Python and i searched the web and could not find an answer for my 
> issue.
>
> i need to get an ip address from list of hostnames which are in a textfile.

This is sounding like homework, so I'll just give you a basic pointer.

You have there something that successfully resolves one hostname to an
IP address. Now you want to expand that to reading an entire file of
them and resolving them all. Presumably you need to produce a list of
IP addresses; check the question as to whether you need to create a
file, or output to the screen, or something else.

What you want, here, is to open a file and iterate over it. The most
convenient way would be to have one hostname per line and iterate over
the lines of the file. Check out these pages in the Python docs
(you're using Python 2 so I'm going with Python 2.7.3 documentation):

Opening a file:
http://docs.python.org/library/functions.html#open
Ensuring that it'll be closed when you're done:
http://docs.python.org/reference/compound_stmts.html#the-with-statement
Looping over an iterable:
http://docs.python.org/tutorial/controlflow.html#for-statements

See where that takes you; in fact, all of
http://docs.python.org/tutorial/ is worth reading, if you haven't
already.

Have fun, enjoy Python!

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


Re: using text file to get ip address from hostname

2012-09-12 Thread dkatorza
בתאריך יום רביעי, 12 בספטמבר 2012 17:24:50 UTC+3, מאת [email protected]:
> hello ,
> 
> 
> 
> i'm new to Python and i searched the web and could not find an answer for my 
> issue.
> 
> 
> 
> i need to get an ip address from list of hostnames which are in a textfile.
> 
> 
> 
> this is what i have so far 
> 
> --
> 
> #!/usr/bin/env python
> 
> #Get the IP Address
> 
> 
> 
> import socket
> 
> hostname = 'need it to read from a text file'
> 
> addr = socket.gethostbyname(hostname)
> 
> print 'The address of ', hostname, 'is', addr 
> 
> 
> 
> ---
> 
> 
> 
> any idea ? 
> 
> sorry for my english
> 
> 
> 
> thanks.

thank you ChrisA
it's not really homework, i found a lab exercise on the web and i;m trying to 
study with it. maybe not the most efficient way.

i have a file with hostnames ordered line by line.

i will check the sources and will get back with outcome.

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


Re: generators as decorators simple issue

2012-09-12 Thread Ian Kelly
On Wed, Sep 12, 2012 at 4:22 AM, pyjoshsys  wrote:
> The output is still not what I want. Now runtime error free, however the 
> output is not what I desire.

[SNIP]

> class Trial(object):
> '''class to demonstrate with'''
> def __init__(self):
> object.__init__(self)
> self.name = None
>
> @classmethod
> def SetName(cls, name):
> cls.name = name

[SNIP]

> if __name__ == '__main__':
> test = Test()
> print 'instance'
> print '', test.name #should be Test
> print 'class'
> print '', Test.name
>
>
> The output is: python decors2.py
> instance
>  None
> class
>  Test
>
> I want:
> instance
>  Test
> class
>  Test
>
> Is this possible in this manner?


The SetName class method sets the name on the *class* dictionary.  The
class's __init__ method also sets a name (None) on the *instance*
dictionary.  From an instance's perspective, the instance dictionary
will shadow the class dictionary.  If you remove the attribute from
the instance dictionary entirely (delete the "self.name = None" line),
and leave the class dictionary as is, then you will get the output you
want (although from your later post I am not certain that this is the
behaviour you want).


On Wed, Sep 12, 2012 at 5:15 AM, pyjoshsys  wrote:
> so decorators only pass the object and not any instance of the object as the 
> implied argument? Is this right?

Right.

> The idea was to use @setname instead of instance.SetName(instance.__name__).

The appropriate place to do this so that it applies to all instances
of the class rather than to the class would be inside the __init__
method.

Also, instances don't have a __name__ attribute, so it's still unclear
to me what you're looking for.  Did you mean the effect to be that of
"instance.SetName(cls.__name__)"?  If so, then the decorator approach
(with the line "self.name = None" removed) should be fine for your
purposes -- you'll just have the name stored in the class dict instead
of in each instance dict, but it will still be visible as long as you
haven't shadowed it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: odt2sphinx 0.2.3 released

2012-09-12 Thread Dustin J. Mitchell
On Wed, Sep 12, 2012 at 10:06 AM,   wrote:
> ߒߤߒߡߜߦߡ ß ß§

And that's why you shouldn't let your kids play with your iPad :)

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


Re: pythonOCC examples doesn't work?

2012-09-12 Thread jelle
You installed python 2.7 and expect a 2.6 module to work with it.
That won't work. Finally, you need to install the OpenCasCade libs to be able 
to run PythonOCC.

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


Re: Single leading dash in member variable names?

2012-09-12 Thread Ian Kelly
On Tue, Sep 11, 2012 at 11:10 PM, Dwight Hutto  wrote:
> Not to jump in with another question(this seems somewhat relevant to the
> conversation, maybe not), but is this similar to a private,public, or
> protected class similar to the C type langs?

More like "this is an implementation detail and in the future it could
be changed or removed entirely without warning".  I consider them
private unless documented otherwise.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Single leading dash in member variable names?

2012-09-12 Thread e . doxtator
On Tuesday, September 11, 2012 5:02:31 PM UTC-5, Erik Max Francis wrote:
> On 09/11/2012 01:53 PM, me wrote:
> 
> > On Tuesday, September 11, 2012 2:06:45 PM UTC-5, Ian wrote:
> 
> >> On Tue, Sep 11, 2012 at 12:45 PM,  I wrote:
> 
> >>> What is the significance of the leading underscore in "self._bongo"?  
> >>> I've seen this a few times and, after looking through PEP 8, I didn't see 
> >>> anything relevant, but I could have missed it.
> 
> >>
> 
> >> Single leading underscore is a convention indicating that the name
> 
> >> should be considered private and not used externally.  It's a softer
> 
> >> version of the double leading underscore that means basically the same
> 
> >> thing but has syntactic significance.
> 
> >
> 
> > Thank you!
> 
> >
> 
> > PEP 8 says this is bad form.  What do you think?
> 
> 
> 
> Where does it say that?

Apologies.  It's in David Goodger's "Code Like A Pythonista" in the "Naming" 
section. 
(http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#naming)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using subprocess.Popen does not suppress terminal window on Windows

2012-09-12 Thread janis . judvaitis
Hi, and I'm sorry for using this old thread, but I'm experiencing the same 
problem, except, that I wan't to execute any shell script or exe without blank 
terminal window. 

Is there any way in python to supress blank console screen while script is 
executing? Multiplatform solution would be nice.

With best regards, Jānis.

On Friday, June 18, 2010 7:23:19 PM UTC+3, Steven wrote:
> I am calling a ruby program from a python gui and using
> subprocess.Popen in Windows XP using python 2.6.  Unfortunately,
> whenever the ruby program is called a blank command window appears on
> screen, annoying my users.  Is there a way to suppress this behaviour?
> 
> Below is a minimal program that demonstrates the problem.  The problem
> does not manifest if the python program is launched via the command
> line.  To duplicate launch from Windows Explorer by double-clicking on
> the python file.
> 
> --- call_double.pyw ---
> from subprocess import *
> import time
> 
> time.sleep(3) # to show that command window is result of call to Popen
> p = Popen(['ruby.exe', 'double.rb'], stdin=PIPE, stdout=PIPE,
> stderr=PIPE)
> results = open('results.txt', 'w')
> for n in range(10):
> p.stdin.write("%d\n" % n)
> result = p.stdout.readline().strip()
> results.write('double(%s) => %2s\n' % (n, result))
> results.close()
> 
> --- end of call_double.pyw ---
> 
> --- double.rb ---
> while true
> puts $stdin.gets().strip!.to_i * 2
> STDOUT.flush
> end
> --- end of double.rb ---
> 
> thanks for any help,
> Steven Rumbalski

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


Add a "key" parameter to bisect* functions?

2012-09-12 Thread Giacomo Alzetta
I've just noticed that the bisect module lacks of the key parameter.

The documentation points to a recipe that could be used to handle a sorted 
collection, but I think it's an overkill if I want to bisect my sequence only 
once or twice with a key. Having something like `bisect(sequence, key=my_key)` 
would be much easier and would conform to the other operations such as 
max/min/sorted.

Is there some reason behind this lack?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Boolean function on variable-length lists

2012-09-12 Thread MRAB

On 12/09/2012 14:51, Ken Seehart wrote:

Putting a few of peoples ideas together...


gt = lambda x: lambda y: x>y
eq = lambda x: lambda y: x==y

def constrain(c,d):
 return all({f(x) for f, x in zip(c, d)})


If you're going to use 'all', why use a set?

return all(f(x) for f, x in zip(c, d))

This will stop as soon as a constraint fails.


constraints = [gt(2), eq(1)]
data0 = [1,1]
data1 = [3,1]

print constrain(constraints, data0)
print constrain(constraints, data1)



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


Creating socket.connect while in epoll loop

2012-09-12 Thread Miki Tebeka
Greetings,

I'm trying to write a reverse TCP proxy using epoll (just for learning).

I've started with the code at http://scotdoyle.com/python-epoll-howto.html and 
got to https://gist.github.com/3707665.

However, I can't create a new connection. When reaching line 34 
(backend.connect), I get the following error (on line 34):
socket.error: [Errno 115] Operation now in progress

I tried moving the socket creation to a different thread, but same problem.
Any ideas how to so fix this? (keep in mind I'm a total newbie in this area).

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


Re: Add a "key" parameter to bisect* functions?

2012-09-12 Thread Miki Tebeka
> I've just noticed that the bisect module lacks of the key parameter.
> ...
> Is there some reason behind this lack?
See full discussion at http://bugs.python.org/issue4356. Guido said it's going 
in, however there's no time frame for it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Add a "key" parameter to bisect* functions?

2012-09-12 Thread Giacomo Alzetta
Il giorno mercoledì 12 settembre 2012 17:54:31 UTC+2, Giacomo Alzetta ha 
scritto:
> I've just noticed that the bisect module lacks of the key parameter.
> 
> 
> 
> The documentation points to a recipe that could be used to handle a sorted 
> collection, but I think it's an overkill if I want to bisect my sequence only 
> once or twice with a key. Having something like `bisect(sequence, 
> key=my_key)` would be much easier and would conform to the other operations 
> such as max/min/sorted.
> 
> 
> 
> Is there some reason behind this lack?

Uhm, I've found this piece of documentation: 
http://docs.python.org/library/bisect.html#other-examples

Now, what it's stated there does not make sense for me:

1) Adding a key/reversed parameter wont decrease the speed of calls to bisect 
that does not use those parameters. At least a good implementation should 
guarantee that

2) Yes, providing a key means that either bisect should do some caching in 
order to speed multiple look-ups, or the keys have to be recomputed every time.
But, I think that using this as a reason to not provide this parameter is wrong.
It's up to the user to decide what has to be done to make code fast.
If he has to do multiple lookups then he should precompute them(but this is 
true also for sort etc.), but if he just has to use this feature once than 
computing "on-the-fly" is simply perfect.

3) Also, the fact that you can bisect only in asceding order sounds strange to 
me. It's not hard to bisect in both directions...

Probably it would be correct to document possible pitfalls of using the 
eventually added "key" and "reversed" parameters(such as multiple key 
evaluation etc.), but I can't see other cons to this.

Also, this change would be 100% backward compatible.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python-list Digest, Vol 108, Issue 98

2012-09-12 Thread David Shi
Hi, 

Where do I find ArcPy.py?

It seems that the link disappeared.

Regards.

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


Re: Add a "key" parameter to bisect* functions?

2012-09-12 Thread Giacomo Alzetta
Il giorno mercoledì 12 settembre 2012 18:05:10 UTC+2, Miki Tebeka ha scritto:
> > I've just noticed that the bisect module lacks of the key parameter.
> 
> > ...
> 
> > Is there some reason behind this lack?
> 
> See full discussion at http://bugs.python.org/issue4356. Guido said it's 
> going in, however there's no time frame for it.

Oh, nice. So eventually it'll be added.

Correct me if I'm wrong, but the reason for this absence is that people could 
think that doing repeated bisects using keys would be fast, even though keys 
has to be recomputed?

I think that could simply be fixed with a "Note:\Warning:" on the documentation.
So that you can create keys with caching or use precomputed keys when you have 
to do a lot of bisects.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Single leading dash in member variable names?

2012-09-12 Thread Ian Kelly
On Wed, Sep 12, 2012 at 9:23 AM,  wrote:
>
> On Tuesday, September 11, 2012 5:02:31 PM UTC-5, Erik Max Francis wrote:
> > On 09/11/2012 01:53 PM, me wrote:
> > > PEP 8 says this is bad form.  What do you think?
> >
> >
> >
> > Where does it say that?
>
> Apologies.  It's in David Goodger's "Code Like A Pythonista" in the "Naming" 
> section. 
> (http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#naming)

That's arguing against double leading underscore, not single leading
underscore.  I pretty much agree with it; I rarely use the
name-mangling syntax myself.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Single leading dash in member variable names?

2012-09-12 Thread mdengler
On Wednesday, September 12, 2012 4:23:49 PM UTC+1, (unknown) wrote:
> [...] David Goodger's "Code Like A Pythonista" in the "Naming" section [says 
> single leading underscore is bad form]. 
> (http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#naming)

Looks like it says the opposite:

"[rather than trying to hide attributes with double-leading-underscores, i]t's 
better to use the single-leading-underscore convention, _internal".
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: avoid the redefinition of a function

2012-09-12 Thread Jabba Laci
> For example:
>
> def install_java():
>pass
>
> def install_tomcat():
>pass

Thanks for the answers. I decided to use numbers in the name of the
functions to facilitate function calls. Now if you have this menu
option for instance:

(5) install mc

You can type just "5" as user input and step_5() is called
automatically. If I use descriptive names like install_java() then
selecting a menu point would be more difficult. And I don't want users
to type "java", I want to stick to simple numbers.

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


Re: pythonOCC examples doesn't work?

2012-09-12 Thread Jayden
On Wednesday, September 12, 2012 11:12:55 AM UTC-4, jelle wrote:
> You installed python 2.7 and expect a 2.6 module to work with it.
> 
> That won't work. Finally, you need to install the OpenCasCade libs to be able 
> to run PythonOCC.
> 
> 
> 
> -jelle

I think that is  the reason. Thanks a lot!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Add a "key" parameter to bisect* functions?

2012-09-12 Thread Miki Tebeka
> Correct me if I'm wrong, but the reason for this absence is that people could 
> think that doing repeated bisects using keys would be fast, even though keys 
> has to be recomputed?
I think the main point Raymond had was:
If we added key= to bisect, it would encourage bad design and steer
people after from better solutions.

But you need to ask him :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: avoid the redefinition of a function

2012-09-12 Thread Tim Chase
On 09/12/12 11:56, Jabba Laci wrote:
>> For example:
>>
>> def install_java():
>>pass
>>
>> def install_tomcat():
>>pass
> 
> Thanks for the answers. I decided to use numbers in the name of the
> functions to facilitate function calls. Now if you have this menu
> option for instance:
> 
> (5) install mc
> 
> You can type just "5" as user input and step_5() is called
> automatically. If I use descriptive names like install_java() then
> selecting a menu point would be more difficult. And I don't want users
> to type "java", I want to stick to simple numbers.

You can do something like the below that I tossed together in a
couple minutes.  It sniffs for globals callables (usually functions,
though could be objects with a __call__ method) that are named
"install_*" and then maps the user's numeric answer to the
corresponding function and calls it.

-tkc

import sys
PREFIX = "install_"
def install_java(*args, **kwargs):
print "Java!"
def install_tomcat(*args, **kwargs):
print "Tomcat!"
def install_mc(*args, **kwargs):
print "mc!"
def install_exit(*args, **kwargs):
sys.exit(0)

functions = [
(name, value)
for name, value
in sorted(globals().items())
if name.startswith(PREFIX)
and callable(value)
]
dispatch = dict(
(i+1, value)
for i, (name, value)
in enumerate(functions)
)

for i, (name, value) in enumerate(functions):
print("%i) %s" % (i+1, name[len(PREFIX):]))

while True:
choice = raw_input("Choose to install/quit: ")
try:
i = int(choice)
except:
continue
if i in dispatch:
dispatch[i]("some args")
else:
print("Please choose a valid option")


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


Re: using text file to get ip address from hostname

2012-09-12 Thread Alister
On Wed, 12 Sep 2012 07:41:10 -0700, dkatorza wrote:

> בתאריך יום רביעי, 12 בספטמבר 2012 17:24:50 UTC+3, מאת [email protected]:
>> hello ,
>> 
>> 
>> 
>> i'm new to Python and i searched the web and could not find an answer
>> for my issue.
>> 
>> 
>> 
>> i need to get an ip address from list of hostnames which are in a
>> textfile.
>> 
>> 
>> 
>> this is what i have so far
>> 
>> 
--
>> 
>> #!/usr/bin/env python
>> 
>> #Get the IP Address
>> 
>> 
>> 
>> import socket
>> 
>> hostname = 'need it to read from a text file'
>> 
>> addr = socket.gethostbyname(hostname)
>> 
>> print 'The address of ', hostname, 'is', addr
>> 
>> 
>> 
>> 
---
>> 
>> 
>> 
>> any idea ?
>> 
>> sorry for my english
>> 
>> 
>> 
>> thanks.
> 
> thank you ChrisA it's not really homework, i found a lab exercise on the
> web and i;m trying to study with it. maybe not the most efficient way.
> 
> i have a file with hostnames ordered line by line.
> 
> i will check the sources and will get back with outcome.
> 
> once again , Thanks,

so self inflicted homework :-), a good way to learn so ChrisA's pointers 
of where to look are probably the best thing for you.

if you get stuck post back sample code & i am sure someone will be happy 
to hint a little deeper

Good luck



-- 
QOTD:
"Wouldn't it be wonderful if real life supported control-Z?"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: avoid the redefinition of a function

2012-09-12 Thread Alister
On Wed, 12 Sep 2012 18:56:46 +0200, Jabba Laci wrote:

>> For example:
>>
>> def install_java():
>>pass
>>
>> def install_tomcat():
>>pass
> 
> Thanks for the answers. I decided to use numbers in the name of the
> functions to facilitate function calls. Now if you have this menu option
> for instance:
> 
> (5) install mc
> 
> You can type just "5" as user input and step_5() is called
> automatically. If I use descriptive names like install_java() then
> selecting a menu point would be more difficult. And I don't want users
> to type "java", I want to stick to simple numbers.
> 
> Laszlo

No No NO!
you cant just pass user input to system calls without validating it first
(google sql injection for examples of the damage unsanitised input can 
cause, it is not just as SQL problem)

it is just as easy so select a reasonably named function as a bad one

option=raw_input('select your option :')

if option =="1": install_java()
if option =="2": install_other()

alternatively you cold add your functions into a dictionary an call them 
from that

opts={'1':install java,'2':install_other}

option=raw_input('select your option :')
opts[option]

Poorly named functions are a major example of poor programming style.

one of the fundamental pillars for python is readability!



-- 
 He's a about half the size of the others.
 But he's got a chainsaw.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: avoid the redefinition of a function

2012-09-12 Thread MRAB

On 12/09/2012 19:04, Alister wrote:

On Wed, 12 Sep 2012 18:56:46 +0200, Jabba Laci wrote:


For example:

def install_java():
   pass

def install_tomcat():
   pass


Thanks for the answers. I decided to use numbers in the name of the
functions to facilitate function calls. Now if you have this menu option
for instance:

(5) install mc

You can type just "5" as user input and step_5() is called
automatically. If I use descriptive names like install_java() then
selecting a menu point would be more difficult. And I don't want users
to type "java", I want to stick to simple numbers.

Laszlo


No No NO!
you cant just pass user input to system calls without validating it first
(google sql injection for examples of the damage unsanitised input can
cause, it is not just as SQL problem)

it is just as easy so select a reasonably named function as a bad one

option=raw_input('select your option :')

if option =="1": install_java()
if option =="2": install_other()

alternatively you cold add your functions into a dictionary an call them
from that

opts={'1':install java,'2':install_other}

option=raw_input('select your option :')
opts[option]

Poorly named functions are a major example of poor programming style.

one of the fundamental pillars for python is readability!


Or you could do this:


def install_java():
"Install Java"
print "Installing Java"

def install_tomcat():
"Install Tomcat"
print "Installing Tomcat"

menu = [install_java, install_tomcat]

for index, func in enumerate(menu, start=1):
print "{0}) {1}".format(index, func.__doc__)

option = raw_input("Select your option : ")

try:
opt = int(option)
except ValueError:
print "Not a valid option"
else:
if 1 <= opt < len(menu):
menu[opt - 1]()
else:
print "Not a valid option"

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


Java Python Developer Required at NYC NY

2012-09-12 Thread Sanith Nair
Hi,

I am looking for a Java Python developer at NYC NY for one of our direct 
client. It is a 6 Months contract position. We need a person with experience in 
developing trading applications and very good with Python Development. If 
interested, please send me your resume to my email address, ie 
[email protected]

Thanks

Sanith Nair
Lead Recruiter
Artech Information Systems LLC 
240 Cedar Knolls Road, Suite 100 | Cedar Knolls, NJ 07927   
Office: 973.967.3522 | Fax: 973.998.2599
Email: [email protected] | Website: www.artechinfo.com 

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


Re: Creating socket.connect while in epoll loop

2012-09-12 Thread calderone . jeanpaul
On Wednesday, September 12, 2012 12:01:58 PM UTC-4, Miki Tebeka wrote:
> Greetings,
> 
> I'm trying to write a reverse TCP proxy using epoll (just for learning).
> 
> I've started with the code at http://scotdoyle.com/python-epoll-howto.html 
> and got to https://gist.github.com/3707665.
>
> 
> However, I can't create a new connection. When reaching line 34 
> (backend.connect), I get the following error (on line 34):
> 
> socket.error: [Errno 115] Operation now in progress
> 

You cannot synchronously set up a new TCP connection using a non-blocking 
socket.  Instead, you begin the connection attempt and it fails with 
EINPROGRESS and then you use epoll to find out when the attempt completes.

I suggest reading the implementation of a Twisted reactor to see an example of 
how this is done.

Here are a couple links to get you started:

http://twistedmatrix.com/trac/browser/trunk/twisted/internet/tcp.py
http://twistedmatrix.com/trac/browser/trunk/twisted/internet/epollreactor.py

Jean-Paul

> 
> 
> I tried moving the socket creation to a different thread, but same problem.
> 
> Any ideas how to so fix this? (keep in mind I'm a total newbie in this area).
> 
> 
> 
> Thanks,
> 
> --
> 
> Miki
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: avoid the redefinition of a function

2012-09-12 Thread Terry Reedy

On 9/12/2012 8:56 AM, Jabba Laci wrote:


This will call the 2nd function. Now my functions are called step_ID
(like step_27(), step_28(), etc.). How to avoid the danger of
redefinition? Now, when I write a new function, I search for its name
to see if it's unique but there must be a better way.


Before starting to write the new function, write a test and see if it 
fails with a "NameError new_name not found". The test failure also 
assures one that the test is being run. (I don't always test first, but 
I once discovered a test not being run when I modified it in a way that 
should have made it fail, but it didn't.)



--
Terry Jan Reedy

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


Re: forked processes and testing

2012-09-12 Thread Terry Reedy

On 9/12/2012 6:20 AM, andrea crotti wrote:

I wrote a decorator that takes a function, run it in a forked process
and return the PID:


This strikes me as an abuse of the decorator syntax.

@wrap
def f(): pass

is just syntactic sugar for

def f(): pass
f = wrap(f)

but that is not what you are doing.

def run_in_fork(func)

def f(): pass

run_in_fork(f)

would let you test run_in_fork with simple test oriented functions and 
separately test your real functions without the complication of the fork 
process.


My 2 cents anyway.

--
Terry Jan Reedy

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


Re: Creating socket.connect while in epoll loop

2012-09-12 Thread Miki Tebeka
> You cannot synchronously set up a new TCP connection using a non-blocking > 
> socket.  Instead, you begin the connection attempt and it fails with 
> EINPROGRESS and then you use epoll to find out when the attempt completes.
OK.

> I suggest reading the implementation of a Twisted reactor to see an example 
> of how this is done.
That was the idea, learning.

> Here are a couple links to get you started:
> http://twistedmatrix.com/trac/browser/trunk/twisted/internet/tcp.py
> http://twistedmatrix.com/trac/browser/trunk/twisted/internet/epollreactor.py
Thank you very much!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using text file to get ip address from hostname

2012-09-12 Thread Terry Reedy

On 9/12/2012 10:41 AM, [email protected] wrote:



it's not really homework, i found a lab exercise on the web

> and i;m trying to study with it. maybe not the most efficient way.


i have a file with hostnames ordered line by line.


Key fact for this exercise: open files are iterable.
So your top level structure is...

for line in open():
  

--
Terry Jan Reedy

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


Re: pythonOCC examples doesn't work?

2012-09-12 Thread Terry Reedy

On 12/09/2012 08:19, Dwight Hutto wrote:



The easiest solution is to:

1. Uninstall 64-bit Python


No need to do this, at least not on windows.


2. Install a 32-bit distribution.


in a separate directory. I have had both on my win7 machine so I could 
run (test) python code on both. One can put python-code-dir.pth in the 
site-packages directories of both.


--
Terry Jan Reedy

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


Re: avoid the redefinition of a function

2012-09-12 Thread D'Arcy Cain
On Wed, 12 Sep 2012 18:56:46 +0200
Jabba Laci  wrote:
> (5) install mc
> 
> You can type just "5" as user input and step_5() is called
> automatically. If I use descriptive names like install_java() then
> selecting a menu point would be more difficult. And I don't want users
> to type "java", I want to stick to simple numbers.

Your users shouldn't be working this out in any case.  Consider,
computer time is cheap.  Programmer time is not cheap but it is short
lived.  User time is forever.  Don't be lazy.  Parse the input and
prepare to present proper error messages just in case your users are
not perfect.

-- 
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: [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: avoid the redefinition of a function

2012-09-12 Thread D'Arcy Cain
On Wed, 12 Sep 2012 16:37:11 -0400
Terry Reedy  wrote:
> assures one that the test is being run. (I don't always test first, but 
> I once discovered a test not being run when I modified it in a way that 
> should have made it fail, but it didn't.)

1. Write the test
2. Run the test - make sure it fails
3. write code until test passes
4. Stop writing code

People underestimate the importance of steps 2 and 4.  Number 2 is
important because a test that can't fail is worse than useless.  Number
4 is important because it prevents code that doesn't have a
corresponding test.  If you feel the need to write more code then go
back to step 1.

And run all of your tests every day.  You will sleep better at night.

-- 
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: [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: avoid the redefinition of a function

2012-09-12 Thread D'Arcy Cain
On Wed, 12 Sep 2012 18:04:51 GMT
Alister  wrote:
> No No NO!
> you cant just pass user input to system calls without validating it first
> (google sql injection for examples of the damage unsanitised input can 
> cause, it is not just as SQL problem)

 http://xkcd.com/327/

-- 
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: [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: avoid the redefinition of a function

2012-09-12 Thread Tim Chase
On 09/12/12 16:47, D'Arcy Cain wrote:
> On Wed, 12 Sep 2012 16:37:11 -0400
> Terry Reedy  wrote:
>> assures one that the test is being run. (I don't always test first, but 
>> I once discovered a test not being run when I modified it in a way that 
>> should have made it fail, but it didn't.)
> 
> 1. Write the test
> 2. Run the test - make sure it fails
> 3. write code until test passes
> 4. Stop writing code
> 
> People underestimate the importance of steps 2 and 4.  Number 2 is
> important because a test that can't fail is worse than useless.  Number
> 4 is important because it prevents code that doesn't have a
> corresponding test.  If you feel the need to write more code then go
> back to step 1.
> 
> And run all of your tests every day.  You will sleep better at night.

Though I usually try to do test-driven development, I confess a
fondness for Titus Brown's "Stupidity driven testing"[1] :-)

-tkc

[1]
http://ivory.idyll.org/blog/stupidity-driven-testing.html
(one of my most memorable pearls from PyCon'07)



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


conflicting installations of python

2012-09-12 Thread azandi
I installed python 2.7.3 using macports on Lion, and used port select python 
python27. However python version still shows 2.7.1.

Any ideas why is that happenning? And how to fix it?

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


Re: using subprocess.Popen does not suppress terminal window on Windows

2012-09-12 Thread Dave Angel
On 09/12/2012 11:26 AM, [email protected] wrote:
> Hi, and I'm sorry for using this old thread, but I'm experiencing the same 
> problem, except, that I wan't to execute any shell script or exe without 
> blank terminal window. 
> Is there any way in python to supress blank console screen while script is 
> executing? Multiplatform solution would be nice.
> With best regards, Jānis.
>

it's not clear from your question:  Are you launching non-Python
programs from a python one, using Popen?  If so, I can't help.  It's the
launchee that determines if a console is created, as best as I know. 
However, if you're trying to launch a python program without its getting
a console, then read on.

No need for a multiplatform solution, since the problem is a Windows
one.  Windows will create a console for a new process unless the parent
console is still available (eg. you run it from command line) or unless
the executable is marked with the "no console" flag.  (I don't recall
what that's actually called, I haven't used Windows in a long time).

Anyway, in the Windows version, there are two executables  python.exe
and pythonw.exe.  You want the latter one, either by explicitly naming
it in your launcher (batch file, script, whatever), or by using the .pyw
extension, which is normally associated with the pythonw.exe program.

-- 

DaveA

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


Re: avoid the redefinition of a function

2012-09-12 Thread Dave Angel
On 09/12/2012 12:56 PM, Jabba Laci wrote:
> Thanks for the answers. I decided to use numbers in the name of the
> functions to facilitate function calls. Now if you have this menu
> option for instance:
>
> (5) install mc
>
> You can type just "5" as user input and step_5() is called
> automatically. If I use descriptive names like install_java() then
> selecting a menu point would be more difficult. And I don't want users
> to type "java", I want to stick to simple numbers.
>
> Laszlo

Many of the other answers were interesting and useful, but I don't think
anyone else pointed out the real problem with your code.

If you have a bunch of functions defined in one place, and a bunch of
choices for the user defined in another, you need to make one place
which maps them all together.  That way, when you define a new function,
you also define the part of the menu that tells the user what to type.

For example, you might make a list like the following:

CHOICES = [("install mc", install_mc),
("install java", install_java),
   etc
   ]

And your input routine will be something like:
   for  index, item in iterate(CHOICES):
  print "(", index, ")", item[0]
   choice = int(raw_input())
   function = CHOICES[choice][1]

This isn't exactly the way i'd do it, and of course it's missing all
kinds of error checking.  But the point is that adding a new function to
the menu only requires one place to "know" the number of the function. 
And the number is only indirectly known, as the location in the CHOICES.

I hope this sparks some ideas.


-- 

DaveA

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


Some questions about atexit

2012-09-12 Thread Roy Smith
The atexit docs (http://docs.python.org/library/atexit.html) are very 
confusing.  In one place they say, "The order in which the functions are 
called is not defined".  In another place, "all functions registered are 
called in last in, first out order".  Which is correct?

Also, it's not clear how atexit handlers interact with threading.  Do 
all handlers get called in the main thread?  What if some other thread 
registers a handler?  Does it get called in that thread?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: submit jobs on multi-core

2012-09-12 Thread Jason Friedman
>>> I have a python script in which I have a list of files to input one by one
>>> and for each file I get a number as an output.
>>> I used for loop to submit the file to script.
>>> My script uses one file at a time and returns the output.
>>>
>>> My computers has 8 cores.
>>> Is there any way that I could submit 8 jobs at a time and get all the
>>> output faster ?
>>
> Or if Python 3.2 is an option, the concurrent.futures module would be
> very well suited for this task.

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


Re: Some questions about atexit

2012-09-12 Thread Terry Reedy

On 9/12/2012 8:58 PM, Roy Smith wrote:

The atexit docs (http://docs.python.org/library/atexit.html) are very
confusing.  In one place they say, "The order in which the functions are
called is not defined".  In another place, "all functions registered are
called in last in, first out order".  Which is correct?


Check the tracker (bugs.python.org) for atexit issues, open and closed. 
I believe there was one about order and whether to make a guarantee. The 
doc might have been changed one place and not another. The newer 
statement is probably correct. In any case, the wrong place should be 
corrected.



Also, it's not clear how atexit handlers interact with threading.  Do
all handlers get called in the main thread?  What if some other thread
registers a handler?  Does it get called in that thread?


Don't know about this.

--
Terry Jan Reedy

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


confused in decorate and closure

2012-09-12 Thread 月忧茗
HI,  I have some test code:


def num(num):
def deco(func):
def wrap(*args, **kwargs):
inputed_num = num
return func(*args, **kwargs)
return wrap
return deco


@num(5)
def test(a):
return a + inputed_num

print test(1)


when run this code,  I got an error shows that 'inputed_num' is not defined

My question is:
In wrap function,  is there not a closure that func can got 'inputed_num' ?



Anyway, If not,  how should I do to got my aim:  Initialize some value, and
use this value directly in the main function.


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


Re: avoid the redefinition of a function

2012-09-12 Thread D'Arcy Cain
On Wed, 12 Sep 2012 17:45:52 -0500
Tim Chase  wrote:
> On 09/12/12 16:47, D'Arcy Cain wrote:
> > And run all of your tests every day.  You will sleep better at night.
> 
> Though I usually try to do test-driven development, I confess a
> fondness for Titus Brown's "Stupidity driven testing"[1] :-)

The two are not mutually exclusive.  One of the benefits of proper unit
testing is that while you might make mistakes, they should always be
new ones.

-- 
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: [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: conflicting installations of python

2012-09-12 Thread Ned Deily
In article ,
 azandi  wrote:

> I installed python 2.7.3 using macports on Lion, and used port select python 
> python27. However python version still shows 2.7.1.
> 
> Any ideas why is that happenning? And how to fix it?

Macports installs its ports into a separate root.  You need to ensure 
that the macports bin directory comes first on your shell search path;  
by default, it is /opt/local/bin.  Try something like this:

export PATH=/opt/local/bin:$PATH

Otherwise, use an alias or an absolute path:

/opt/local/bin/python

-- 
 Ned Deily,
 [email protected]

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


Re: using text file to get ip address from hostname

2012-09-12 Thread Jason Friedman
> i need to get an ip address from list of hostnames which are in a textfile.
>
> this is what i have so far
> --
> #!/usr/bin/env python
> #Get the IP Address
>
> import socket
> hostname = 'need it to read from a text file'
> addr = socket.gethostbyname(hostname)
> print 'The address of ', hostname, 'is', addr

$ cat hostnames
google.com
microsoft.com
facebook.com

$ python3
Python 3.2.3 (default, May  3 2012, 15:51:42)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> for line in open("hostnames"):
... hostname = line.strip()
... print("IP address for {0} is {1}.".format(hostname,
socket.gethostbyname(hostname)))
...
IP address for google.com is 74.125.225.33.
IP address for microsoft.com is 64.4.11.37.
IP address for facebook.com is 69.171.237.16.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: conflicting installations of python

2012-09-12 Thread azandi
Hi Ned, 

Thanks for your response, I think the path is fine, since which python outputs 
the location where macports should install python i.e. /opt/local/bin/python.

also 

port list active | fgrep python
python27   @2.7.3  lang/python27

and

port location python27

Port python27 2.7.3_0 is installed as an image in:
/opt/local/var/macports/software/python27/python27-2.7.3_0.darwin_11.x86_64.tbz2


Another problem caused by this installation is that somehow it removes 
os.urandom module:

>>> import os
>>> os.urandom
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'module' object has no attribute 'urandom'


On Wednesday, September 12, 2012 8:07:43 PM UTC-7, Ned Deily wrote:
> In article ,
> 
>  azandi  wrote:
> 
> 
> 
> > I installed python 2.7.3 using macports on Lion, and used port select 
> > python 
> 
> > python27. However python version still shows 2.7.1.
> 
> > 
> 
> > Any ideas why is that happenning? And how to fix it?
> 
> 
> 
> Macports installs its ports into a separate root.  You need to ensure 
> 
> that the macports bin directory comes first on your shell search path;  
> 
> by default, it is /opt/local/bin.  Try something like this:
> 
> 
> 
> export PATH=/opt/local/bin:$PATH
> 
> 
> 
> Otherwise, use an alias or an absolute path:
> 
> 
> 
> /opt/local/bin/python
> 
> 
> 
> -- 
> 
>  Ned Deily,
> 
>  [email protected]

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


Re: conflicting installations of python

2012-09-12 Thread Ned Deily
In article ,
 [email protected] wrote:
> Thanks for your response, I think the path is fine, since which python 
> outputs the location where macports should install python i.e. 
> /opt/local/bin/python.
> 
> also 
> 
> port list active | fgrep python
> python27   @2.7.3  lang/python27
> 
> and
> 
> port location python27
> 
> Port python27 2.7.3_0 is installed as an image in:
> /opt/local/var/macports/software/python27/python27-2.7.3_0.darwin_11.x86_64.tb
> z2

None of the above actually shows what is being executed when you type 
python.  What is the output from the following commands?

which python
ls -l $(which python)
python

> Another problem caused by this installation is that somehow it removes 
> os.urandom module:
> 
> >>> import os
> >>> os.urandom
> Traceback (most recent call last):
>   File "", line 1, in 
> AttributeError: 'module' object has no attribute 'urandom'

Looks like you are using a broken Python installation.

-- 
 Ned Deily,
 [email protected]

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


Re: confused in decorate and closure

2012-09-12 Thread Peter Otten
月忧茗 wrote:

> HI,  I have some test code:
> 
> 
> def num(num):
> def deco(func):
> def wrap(*args, **kwargs):
> inputed_num = num
> return func(*args, **kwargs)
> return wrap
> return deco
> 
> 
> @num(5)
> def test(a):
> return a + inputed_num
> 
> print test(1)
> 
> 
> when run this code,  I got an error shows that 'inputed_num' is not
> defined
> 
> My question is:
> In wrap function,  is there not a closure that func can got 'inputed_num'
> ?
> 
> 
> 
> Anyway, If not,  how should I do to got my aim:  Initialize some value,
> and use this value directly in the main function.

Variable scopes are determined statically. In

> def test(a):
> return a + inputed_num

"inputed_num" is a global variable.

> @num(5)

is not a macro, but a shortcut that tells Python to execute

test = num(5)(test)

and thus does not change the scopes. To get the desired effect you have to 
turn "inputed_num" into an explicit function argument, for example:

>>> def num(n):
... def deco(f):
... def wrap(*args, **kw):
... return f(n, *args, **kw)
... return wrap
... return deco
... 
>>> @num(42)
... def test(n, a):
... return n + a
... 
>>> test(1)
43


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