what's the difference of Template.append(...) and Template.prepend(...) in pipes module

2016-09-27 Thread Cpcp Cp
Template.append(cmd, kind) and Template.prepend(cmd, kind) Append a new action at the end.The cmd variable must be a valid bourne shell command. The kind variable consists of two letters. My os is windows 7.But this module is used for POSIX. So,I don't know the doucement said what's difference o

How to call a method returning a value from a main function

2016-09-27 Thread prasanth kotagiri
#!/sur/bin/env python import sys import jwt import argparse from calendar import timegm from datetime import datetime import uuid class TokenGenerator: def __init__(self, args): self.macKey = args.authzSystemMacKey self.authzSystemId = args.authzSystemId self.permissio

How to call this method from main method

2016-09-27 Thread prasanthktgr
#!/sur/bin/env python import sys import jwt import argparse from calendar import timegm from datetime import datetime import uuid class TokenGenerator: def __init__(self, args): self.macKey = args.authzSystemMacKey self.authzSystemId = args.authzSystemId self.permissio

Re: Why searching in a set is much faster than in a list ?

2016-09-27 Thread ast
"ast" a écrit dans le message de news:57eb5a4a$0$3305$426a7...@news.free.fr... Hello I noticed that searching in a set is faster than searching in a list. from timeit import Timer from random import randint l = [i for i in range(100)] s = set(l) t1 = Timer("randint(0, 200) in l", "from __m

Re: Is there a way to change the closure of a python function?

2016-09-27 Thread Chris Angelico
On Wed, Sep 28, 2016 at 3:27 PM, Gregory Ewing wrote: > Lawrence D’Oliveiro wrote: >> >> On Wednesday, September 28, 2016 at 3:35:58 AM UTC+13, Peter Otten wrote: >> >>> is Python actually a "functional language"? >> >> >> Yes >>

Why searching in a set is much faster than in a list ?

2016-09-27 Thread ast
Hello I noticed that searching in a set is faster than searching in a list. from timeit import Timer from random import randint l = [i for i in range(100)] s = set(l) t1 = Timer("randint(0, 200) in l", "from __main__ import l, randint") t2 = Timer("randint(0, 200) in s", "from __main__ import

Re: Is there a way to change the closure of a python function?

2016-09-27 Thread Gregory Ewing
Peng Yu wrote: On Tue, Sep 27, 2016 at 10:01 AM, Chris Angelico wrote: """In some languages, the variable bindings contained in a closure behave just like any other variables. Alas, in python they are read-only.""" This is not true, at least as of Python 3. So in Python 2, this is true? P

Re: Is there a way to change the closure of a python function?

2016-09-27 Thread Gregory Ewing
Lawrence D’Oliveiro wrote: On Wednesday, September 28, 2016 at 3:35:58 AM UTC+13, Peter Otten wrote: is Python actually a "functional language"? Yes . No, not according to what the term "functional language" usually

Re: How to make a foreign function run as fast as possible in Windows?

2016-09-27 Thread eryk sun
On Wed, Sep 28, 2016 at 2:13 AM, wrote: > If the load was distributed by the OS schedules across all cores, > does it means I can't make one core solely running a piece of codes > for me and so I have no contol on its performance? In Unix, Python's os module may have sched_setaffinity() to set t

Re: Nested for loops and print statements

2016-09-27 Thread Steven D'Aprano
On Wednesday 28 September 2016 12:48, Larry Hudson wrote: > As they came through in the newsgroup, BOTH run correctly, because both > versions had leading spaces only. > (I did a careful copy/paste to check this.) Copying and pasting from the news client may not be sufficient to show what whites

Re: Is there a way to change the closure of a python function?

2016-09-27 Thread Jussi Piitulainen
Chris Angelico writes: > On Wed, Sep 28, 2016 at 7:19 AM, Terry Reedy wrote: >> The value of the cell variable is writable from within the body of the >> closure function if declared nonlocal, but not otherwise, and not from >> without. The latter may be what Peng meant by 'change' and the blogge

Re: How to make a foreign function run as fast as possible in Windows?

2016-09-27 Thread Gene Heskett
On Tuesday 27 September 2016 22:13:51 jf...@ms4.hinet.net wrote: > eryk sun at 2016/9/27 11:44:49AM wrote: > > The threads of a process do not share a single core. The OS > > schedules threads to distribute the load across all cores > > hmmm... your answer overthrow all my knowledge about Pyth

Re: Nested for loops and print statements

2016-09-27 Thread Larry Hudson via Python-list
On 09/26/2016 01:57 PM, Cai Gengyang wrote: Ok it works now: for row in range(10): for column in range(10): print("*",end="") but how is it different from --- for row in range

Re: Is there a way to change the closure of a python function?

2016-09-27 Thread Peng Yu
On Tue, Sep 27, 2016 at 10:01 AM, Chris Angelico wrote: > On Wed, Sep 28, 2016 at 12:01 AM, Peng Yu wrote: >> Hi, In many other functional language, one can change the closure of a >> function. Is it possible in python? >> >> http://ynniv.com/blog/2007/08/closures-in-python.html >> > > From the b

Re: How to make a foreign function run as fast as possible in Windows?

2016-09-27 Thread jfong
eryk sun at 2016/9/27 11:44:49AM wrote: > The threads of a process do not share a single core. The OS schedules > threads to distribute the load across all cores hmmm... your answer overthrow all my knowledge about Python threads completely:-( I actually had ever considered using ProcessPoolE

Re: Python C API: How to debug reference leak?

2016-09-27 Thread dl l
When I debug in C++, I see the reference count of a PyObject is 1. I don't know where is referencing this object. How can I find out where is referencing this object? 2016-09-27 15:47 GMT+08:00 dl l : > Thanks for reply. Is there any function in C to get the reference objects > of a object? I wan

Getting IDLE to use correct Tcl/Tk on MacOSX 10.6?

2016-09-27 Thread Gregory Ewing
I don't normally use IDLE, but I had occasion to use it on MacOSX 10.6 to answer someone's question, and of course it didn't work properly due to Apple's broken Tcl/Tk. I followed the advice to install ActiveState Tcl 8.5.18.0, but my Python still wants to use Apple's Tcl. How do I persuade Pyth

Re: Is there a way to change the closure of a python function?

2016-09-27 Thread Lawrence D’Oliveiro
On Wednesday, September 28, 2016 at 4:01:36 AM UTC+13, Chris Angelico wrote: > You can also have multiple closures in the same context, and changes made by > one of them will affect the others. This is the point where it’s probably easier to wrap them all together into methods of a common class.

Re: Is there a way to change the closure of a python function?

2016-09-27 Thread Lawrence D’Oliveiro
On Wednesday, September 28, 2016 at 3:35:58 AM UTC+13, Peter Otten wrote: > is Python actually a "functional language"? Yes . -- https://mail.python.org/mailman/listinfo/python-list

Re: How to make a copy of chained dicts effectively and nicely?

2016-09-27 Thread Lawrence D’Oliveiro
On Wednesday, September 28, 2016 at 2:27:36 AM UTC+13, Jussi Piitulainen wrote: > Lawrence D’Oliveiro writes: >> dict(dict(d1, **d2), **d3) > > Nice expression. But that's not available if the keys are not strings: > > dict({}, **{ 1:3 }) > ==> > Traceback (most recent call last): > File "", li

Re: Nested for loops and print statements

2016-09-27 Thread Gregory Ewing
Cai Gengyang wrote: How are you running the interactive interpreter? Are you using IDLE, or are you running Python in a command window? --- IDLE I don't normally use IDLE on MacOSX, so I had to try it to find out. I think I know what your problem is now. When you type a line into IDLE ending w

Re: Is there a way to change the closure of a python function?

2016-09-27 Thread Ian Kelly
On Tue, Sep 27, 2016 at 3:19 PM, Terry Reedy wrote: > On 9/27/2016 11:01 AM, Chris Angelico wrote: >> >> On Wed, Sep 28, 2016 at 12:01 AM, Peng Yu wrote: >>> >>> Hi, In many other functional language, one can change the closure of a >>> function. Is it possible in python? >>> >>> http://ynniv.com

Re: Is there a way to change the closure of a python function?

2016-09-27 Thread Chris Angelico
On Wed, Sep 28, 2016 at 7:19 AM, Terry Reedy wrote: > The value of the cell variable is writable from within the body of the > closure function if declared nonlocal, but not otherwise, and not from > without. The latter may be what Peng meant by 'change' and the blogger by > 'read-only'. > Not f

Re: Is there a way to change the closure of a python function?

2016-09-27 Thread Terry Reedy
On 9/27/2016 11:01 AM, Chris Angelico wrote: On Wed, Sep 28, 2016 at 12:01 AM, Peng Yu wrote: Hi, In many other functional language, one can change the closure of a function. Is it possible in python? http://ynniv.com/blog/2007/08/closures-in-python.html From the blog post: """In some lang

Re: Can this be easily done in Python?

2016-09-27 Thread Paul Rubin
TUA writes: > TransactionTerms = > that sets the variable TransactionTerms to its own name as string It's conceivably possible using messy introspection hackery, but if you're asking that question you don't want to think about doing it that way. If you describe the actual goal (application) you

Re: Can this be easily done in Python?

2016-09-27 Thread Random832
On Tue, Sep 27, 2016, at 15:58, TUA wrote: > Is the following possible in Python? > > Given how the line below works > > TransactionTerms = 'TransactionTerms' > > > have something like > > TransactionTerms = > > that sets the variable TransactionTerms to its own name as string > representati

Re: event loop vs threads

2016-09-27 Thread Terry Reedy
On 9/27/2016 12:01 AM, srinivas devaki wrote: how does Python switch execution and maintain context i.e function stack etc,.. for co-routines and why is it less costly than switching threads which almost do the same, and both are handled by Python Interpreter itself(event loop for co-routines and

Can this be easily done in Python?

2016-09-27 Thread TUA
Is the following possible in Python? Given how the line below works TransactionTerms = 'TransactionTerms' have something like TransactionTerms = that sets the variable TransactionTerms to its own name as string representation without having to specify it explicitly as in the line above

Re: How to reduce the DRY violation in this code

2016-09-27 Thread Wildman via Python-list
On Tue, 27 Sep 2016 10:30:05 -0700, Paul Rubin wrote: > Chris Angelico writes: >> Can you elaborate on what "GoF builder" means? Presumably it's a >> special case of the builder pattern, > > I think it just means the usual builder pattern, from the Design > Patterns book by the so-called Gang o

Re: How to reduce the DRY violation in this code

2016-09-27 Thread Paul Rubin
Chris Angelico writes: > Can you elaborate on what "GoF builder" means? Presumably it's a > special case of the builder pattern, I think it just means the usual builder pattern, from the Design Patterns book by the so-called Gang of Four (GoF). -- https://mail.python.org/mailman/listinfo/python

Re: Case insensitive replacement?

2016-09-27 Thread Paul Rubin
> needle = "World" > haystack = "Hello, world!" > replacement = "THERE" > result = haystack.replace(needle, replacement, ignore_case=True) > # result would be "Hello, THERE!" >>> import re >>> re.sub('(?i)world','THERE','Hello World') 'Hello THERE' -- https://mail.python.org/mailman/listinfo

Re: How to reduce the DRY violation in this code

2016-09-27 Thread Chris Angelico
On Wed, Sep 28, 2016 at 3:08 AM, Gerald Britton wrote: >> >> I have a class that takes a bunch of optional arguments. They're all >> optional, with default values of various types. For simplicity, let's say >> some are ints and some are floats: class Spam: >> def __init__(self, bashful=10.0, doc=2

Re: How to reduce the DRY violation in this code

2016-09-27 Thread Paul Rubin
Steve D'Aprano writes: > class Spam: > def __init__(self, bashful=10.0, doc=20.0, dopey=30.0, > grumpy=40, happy=50, sleepy=60, sneezy=70): > # the usual assign arguments to attributes dance... > self.bashful = bashful > self.doc = doc > # etc.

Re: Case insensitive replacement?

2016-09-27 Thread MRAB
On 2016-09-27 17:56, Tim Chase wrote: I'd like to do a case-insensitive replacement in a string but want to do it pythonically. Ideally, the code would something like needle = "World" haystack = "Hello, world!" replacement = "THERE" result = haystack.replace(needle, replacement, ignore_

RE: How to reduce the DRY violation in this code

2016-09-27 Thread Gerald Britton
> > I have a class that takes a bunch of optional arguments. They're all > optional, with default values of various types. For simplicity, let's say > some are ints and some are floats: class Spam: > def __init__(self, bashful=10.0, doc=20.0, dopey=30.0, > grumpy=40, happy=50, sleepy=60, sneezy=70)

Case insensitive replacement?

2016-09-27 Thread Tim Chase
I'd like to do a case-insensitive replacement in a string but want to do it pythonically. Ideally, the code would something like needle = "World" haystack = "Hello, world!" replacement = "THERE" result = haystack.replace(needle, replacement, ignore_case=True) # result would be "Hello, T

Re: How to reduce the DRY violation in this code

2016-09-27 Thread Chris Angelico
On Wed, Sep 28, 2016 at 1:49 AM, Steve D'Aprano wrote: > @classmethod > def from_strings(cls, bashful='10.0', doc='20.0', dopey='30.0', > grumpy='40', happy='50', sleepy='60', sneezy='70'): > bashful = float(bashful) > doc = float(doc) > dopey =

Re: How to reduce the DRY violation in this code

2016-09-27 Thread Marko Rauhamaa
Steve D'Aprano : > def __init__(self, bashful=10.0, doc=20.0, dopey=30.0, > grumpy=40, happy=50, sleepy=60, sneezy=70): > [...] > > @classmethod > def from_strings(cls, bashful='10.0', doc='20.0', dopey='30.0', > grumpy='40', happy='50', sleepy='

How to reduce the DRY violation in this code

2016-09-27 Thread Steve D'Aprano
I have a class that takes a bunch of optional arguments. They're all optional, with default values of various types. For simplicity, let's say some are ints and some are floats: class Spam: def __init__(self, bashful=10.0, doc=20.0, dopey=30.0, grumpy=40, happy=50, sleepy=60

Re: How to make a copy of chained dicts effectively and nicely?

2016-09-27 Thread Chris Angelico
On Wed, Sep 28, 2016 at 12:54 AM, Jussi Piitulainen wrote: > I wasn't sure if it makes a copy or just returns the dict. But it's > true: help(dict) says dict(mapping) is a "new dictionary initialized > from a mapping object's (key, value) pairs". Yep. With mutable objects, Python's docs are usual

Re: How to make a copy of chained dicts effectively and nicely?

2016-09-27 Thread Jussi Piitulainen
MRAB writes: > On 2016-09-27 08:32, Jussi Piitulainen wrote: > [snip] > >> Is ChainMap really that bad? Otherwise the following would look somewhat >> nice: >> >> d = dict(ChainMap(d1, d2, d3).items()) >> >> Those come to mind. >> > You can copy a dict just by passing it to 'dict': > > d = dic

Re: Is there a way to change the closure of a python function?

2016-09-27 Thread Chris Angelico
On Wed, Sep 28, 2016 at 12:01 AM, Peng Yu wrote: > Hi, In many other functional language, one can change the closure of a > function. Is it possible in python? > > http://ynniv.com/blog/2007/08/closures-in-python.html > >From the blog post: """In some languages, the variable bindings contained i

Re: Is there a way to change the closure of a python function?

2016-09-27 Thread Ian Kelly
On Tue, Sep 27, 2016 at 8:41 AM, jmp wrote: > On 09/27/2016 04:01 PM, Peng Yu wrote: >> >> Hi, In many other functional language, one can change the closure of a >> function. Is it possible in python? >> >> http://ynniv.com/blog/2007/08/closures-in-python.html >> > > If I understood correctly your

Re: How to make a copy of chained dicts effectively and nicely?

2016-09-27 Thread MRAB
On 2016-09-27 08:32, Jussi Piitulainen wrote: [snip] Is ChainMap really that bad? Otherwise the following would look somewhat nice: d = dict(ChainMap(d1, d2, d3).items()) Those come to mind. You can copy a dict just by passing it to 'dict': d = dict(d1) so I wondered if you can do the

Re: Is there a way to change the closure of a python function?

2016-09-27 Thread jmp
On 09/27/2016 04:01 PM, Peng Yu wrote: Hi, In many other functional language, one can change the closure of a function. Is it possible in python? http://ynniv.com/blog/2007/08/closures-in-python.html If I understood correctly your link: (untested) def func(x): return x+func.y func.y = 1

Re: PyImport_AddModule vs PyImport_Import

2016-09-27 Thread dl l
Thanks. That's helpful. I did not notice the note. 2016-09-27 19:24 GMT+08:00 Ned Batchelder : > On Tuesday, September 27, 2016 at 3:54:01 AM UTC-4, dl l wrote: > > What is the difference between PyImport_AddModule and PyImport_Import? > > > > When need to use PyImport_AddModule? > > > > When nee

Re: Is there a way to change the closure of a python function?

2016-09-27 Thread Peter Otten
Peng Yu wrote: > Hi, In many other functional language, one can change the closure of a > function. Which are those languages, and how do they work? And is Python actually a "functional language"? > Is it possible in python? I'm not sure what you mean. Something like >>> def f(x): ... d

Is there a way to change the closure of a python function?

2016-09-27 Thread Peng Yu
Hi, In many other functional language, one can change the closure of a function. Is it possible in python? http://ynniv.com/blog/2007/08/closures-in-python.html -- Regards, Peng -- https://mail.python.org/mailman/listinfo/python-list

Re: How to make a copy of chained dicts effectively and nicely?

2016-09-27 Thread Peter Otten
Nagy László Zsolt wrote: > The result that I need should be a real dict, not just a ChainMap. (It > is because I have to mutate it.) > > d1 = {'a':1, 'b':2} > d2 = {'c':3, 'd':4} > d3 = {'e':5, 'f':6} > > #1. My first naive approach was: > > > from collections import ChainMap > d = {} > for ke

Re: How to make a copy of chained dicts effectively and nicely?

2016-09-27 Thread Jussi Piitulainen
Lawrence D’Oliveiro writes: > On Tuesday, September 27, 2016 at 8:08:46 PM UTC+13, Nagy László Zsolt wrote: >> d1 = {'a':1, 'b':2} >> d2 = {'c':3, 'd':4} >> d3 = {'e':5, 'f':6} >> >> Is there a version that is as effective as #3, but as clean and nice as #4? > > dict(dict(d1, **d2), **d3) Nice e

Re: How to make a copy of chained dicts effectively and nicely?

2016-09-27 Thread Steve D'Aprano
On Tue, 27 Sep 2016 05:08 pm, Nagy Lc3a1szlc3b3 Zsolt wrote: > #2. Much more effective version, but requires too many lines: > > d= {} > d.update(d1) > d.update(d2) > d.update(d3) new_dict = {} for d in (d1, d2, d3): new_dict.update(d) Or, if you know for sure there's only three dicts: new

Re: PyImport_AddModule vs PyImport_Import

2016-09-27 Thread Ned Batchelder
On Tuesday, September 27, 2016 at 3:54:01 AM UTC-4, dl l wrote: > What is the difference between PyImport_AddModule and PyImport_Import? > > When need to use PyImport_AddModule? > > When need to use PyImport_Import? Does this paragraph from the docs help? > Note > > This function does not load

Re: it looks strange

2016-09-27 Thread Lawrence D’Oliveiro
On Tuesday, September 27, 2016 at 9:09:55 PM UTC+13, Cpcp Cp wrote: > >>> li=[lambda :x for x in range(10)] Try li = [(lambda x : lambda : x)(x) for x in range(10)] print(li[0]()) print(li[9]()) 0 9 -- https://mail.python.org/mailman/listinfo/python-list

Re: it looks strange

2016-09-27 Thread Cpcp Cp
I get it.Thanks! -- https://mail.python.org/mailman/listinfo/python-list

Re: it looks strange

2016-09-27 Thread Antoon Pardon
Op 27-09-16 om 10:09 schreef cpx...@gmail.com: li=[lambda :x for x in range(10)] res=li[0]() print res > 9 > > why? Because there is no nested scope for the x variable.So your list looks like this: [lambda :x, lambda :x, lambda :x, lambda :x, lambda :x, lambda :x, lambda :x, lambda

Re: it looks strange

2016-09-27 Thread Peter Otten
cpx...@gmail.com wrote: li=[lambda :x for x in range(10)] res=li[0]() print res > 9 > > why? Look what happens if you look up x manually: >>> li = [lambda :x for x in range(10)] >>> x 9 So at this point x is 9 and a function written to return the value bound to the name x will

Re: it looks strange

2016-09-27 Thread Jussi Piitulainen
cpx...@gmail.com writes: li=[lambda :x for x in range(10)] res=li[0]() print res > 9 > > why? Because each of the ten functions will report the final value of the same x. There are a couple of tricks to capture the transient value: [lambda w=x: w for x in range(10)] [(lambda w:

it looks strange

2016-09-27 Thread cpxuvs
>>> li=[lambda :x for x in range(10)] >>> res=li[0]() >>> print res 9 why? -- https://mail.python.org/mailman/listinfo/python-list

PyImport_AddModule vs PyImport_Import

2016-09-27 Thread dl l
What is the difference between PyImport_AddModule and PyImport_Import? When need to use PyImport_AddModule? When need to use PyImport_Import? -- https://mail.python.org/mailman/listinfo/python-list

Re: Python C API: How to debug reference leak?

2016-09-27 Thread dl l
Thanks for reply. Is there any function in C to get the reference objects of a object? I want to debug where are referencing the object. 2016-09-27 15:01 GMT+08:00 dieter : > dl l writes: > > I want to check the references of an object. Any way to get the > references > > of an object with Pytho

Re: How to make a copy of chained dicts effectively and nicely?

2016-09-27 Thread Jussi Piitulainen
Nagy László Zsolt writes: > The result that I need should be a real dict, not just a ChainMap. (It > is because I have to mutate it.) > > d1 = {'a':1, 'b':2} > d2 = {'c':3, 'd':4} > d3 = {'e':5, 'f':6} > > #1. My first naive approach was: > > > from collections import ChainMap > d = {} > for key,v

Re: How to make a copy of chained dicts effectively and nicely?

2016-09-27 Thread Lawrence D’Oliveiro
On Tuesday, September 27, 2016 at 8:08:46 PM UTC+13, Nagy László Zsolt wrote: > d1 = {'a':1, 'b':2} > d2 = {'c':3, 'd':4} > d3 = {'e':5, 'f':6} > > Is there a version that is as effective as #3, but as clean and nice as #4? dict(dict(d1, **d2), **d3) -- https://mail.python.org/mailman/listinfo/p

How to make a copy of chained dicts effectively and nicely?

2016-09-27 Thread Nagy László Zsolt
The result that I need should be a real dict, not just a ChainMap. (It is because I have to mutate it.) d1 = {'a':1, 'b':2} d2 = {'c':3, 'd':4} d3 = {'e':5, 'f':6} #1. My first naive approach was: from collections import ChainMap d = {} for key,value in ChainMap(d1, d2, d3).items(): d[key]

Re: Python C API: How to debug reference leak?

2016-09-27 Thread dieter
dl l writes: > I want to check the references of an object. Any way to get the references > of an object with Python C API? Like: gc.get_referrs(), is there similar > API in C lib? "gc" is a module. You can import and access modules from the C API. Thus, you can use "gc.get_referers" from "C" cod