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" code.

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


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] = value

#2. Much more effective version, but requires too many lines:

d= {}
d.update(d1)
d.update(d2)
d.update(d3)

#3. Third version is more compact. It uses a side effect inside a list
comp., so I don't like it either:

d = {}
[d.update(_) for _ in [d1, d2, d3]]


#4. Last version:

d = {}
d.update(ChainMap(d1, d2, d3))

Visually, it is the cleanest and the easiest to understand. However, it
uses ChainMap.__iter__ and that goes over all mappings in a loop written
in pure Python.

Is there a version that is as effective as #3, but as clean and nice as #4?


-- 
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 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/python-list


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,value in ChainMap(d1, d2, d3).items():
> d[key] = value
>
> #2. Much more effective version, but requires too many lines:
>
> d= {}
> d.update(d1)
> d.update(d2)
> d.update(d3)
>
> #3. Third version is more compact. It uses a side effect inside a list
> comp., so I don't like it either:
>
> d = {}
> [d.update(_) for _ in [d1, d2, d3]]

That really should be just a loop:

d = {}
for _ in (d1, d2, d3):
d.update(_)

> #4. Last version:
>
> d = {}
> d.update(ChainMap(d1, d2, d3))
>
> Visually, it is the cleanest and the easiest to understand. However, it
> uses ChainMap.__iter__ and that goes over all mappings in a loop written
> in pure Python.
>
> Is there a version that is as effective as #3, but as clean and nice
> as #4?

Maybe see above.

Or consider also this, which looks straightforward to me:

d = { k:v for d in (d1, d2, d3) for k,v in d.items() }

Is ChainMap really that bad? Otherwise the following would look somewhat
nice:

d = dict(ChainMap(d1, d2, d3).items())

Those come to mind.
-- 
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 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" code.
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
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


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


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: (lambda :w))(x) for x in range(10)]

The former works because the default value of w is whatever x was when
the function object was created.

I think the latter does what you thought you were doing, but it's harder
to read and probably the difference should not matter much in practice.

You can call w x in both tricks if you like.
-- 
https://mail.python.org/mailman/listinfo/python-list


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 return 9

>>> li[0]()
9

>>> x = 42
>>> li[0]()
42

This is called late binding and neither the lambda behaves like any other 
function

>>> def f(): return x
... 
>>> f()
42
>>> x = "whatever"
>>> f()
'whatever'

To get the desired results you can either use default values which are bound 
when the function is created

>>> li = [lambda x=x: x for x in range(9)]
>>> li[0]()
0
>>> li[5]()
5

or a closure:

>>> li = [(lambda x: lambda: x)(x) for x in range(9)]
>>> li[0]()
0
>>> li[5]()
5

This looks less messy when you use a function:

>>> def make_const_func(value):
... return lambda: value
... 
>>> li = [make_const_func(x) for x in range(9)]
>>> li[7]()
7

In both cases the value is bound inside a function and not affected by the 
current binding of the global x. 

In Python 3 the situation is almost the same, except that the x from the 
list comprehension is no longer exposed to the enclosing namespace.


-- 
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 :x, lambda :x, lambda :x] with x having the
value 9, being the last value through the loop. So your code is
quivallent to the following. >>> li=[lambda :x for t in range(10)]
>>> x = 9

What you probably want is the following:

>>> li = [(lambda t: lambda :t)(x) for x in range(10)]

-- 
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 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: 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 or import the module; if the module wasn’t 
> already loaded, you will get an empty module object. Use 
> PyImport_ImportModule() or one of its variants to import a module. Package 
> structures implied by a dotted name for name are not created if not already 
> present. 

https://docs.python.org/2/c-api/import.html#c.PyImport_AddModule


--Ned.
-- 
https://mail.python.org/mailman/listinfo/python-list


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_dict = d1.copy()
new_dict.update(d2, **d3)


Or if you prefer:

new_dict = dict(d1)  # same as copying
new_dict.update(d2, **d3)




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


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 expression. But that's not available if the keys are not strings:

dict({}, **{ 1:3 })
==>
Traceback (most recent call last):
  File "", line 1, in 
TypeError: keyword arguments must be strings

On the other hand, '1' seemed to work (in Python 3.4.3) as key, though 1
cannot possibly be a keyword parameter in a function definition :)

Also related to the use of **, are there any practical limitations to
how many parameters a Python function may have? Or is it guaranteed safe
to spread, say, hundreds of thousands of dictionary keys this way? Just
wondering.
-- 
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 key,value in ChainMap(d1, d2, d3).items():
> d[key] = value
> 
> #2. Much more effective version, but requires too many lines:
> 
> d= {}
> d.update(d1)
> d.update(d2)
> d.update(d3)

Unsolicited advice: you have to reverse the order with respect to ChainMap 
if there are duplicate keys:

>>> a = dict(a=1, b=2)
>>> b = dict(b=3, c=4)
>>> d = {}
>>> d.update(a)
>>> d.update(b)
>>> d == dict(ChainMap(a, b))
False
>>> d == dict(ChainMap(b, a))
True

With that in mind here's another version for your zoo:

>>> class MyChainMap(ChainMap):
... def materialized(self):
... d = {}
... for map in reversed(self.maps):
... d.update(map)
... return d
... 
>>> MyChainMap(b, a).materialized()
{'a': 1, 'c': 4, 'b': 3}
>>> _ == d
True

If you say

> requires too many lines

is counter with "Write once, run anywhere".

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


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: 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):
... def g(y):
... return x + y
... def h(newx):
... nonlocal x
... x = newx
... return g, h
... 
>>> a, b = f(10)
>>> a(42)
52
>>> b(20)
>>> a(42)
62

?
 
> http://ynniv.com/blog/2007/08/closures-in-python.html

A short explanation would have been better than that link.

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


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 need to use PyImport_Import?
>
> Does this paragraph from the docs help?
>
> > Note
> >
> > This function does not load or import the module; if the module wasn’t
> already loaded, you will get an empty module object. Use
> PyImport_ImportModule() or one of its variants to import a module. Package
> structures implied by a dotted name for name are not created if not already
> present.
>
> https://docs.python.org/2/c-api/import.html#c.PyImport_AddModule
>
>
> --Ned.
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


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 = 10
func(5) => 15
func.y = 100
func(5) => 105

implements a closure of a function.


jm

Note: function are objects, and can have attributes, however I rarely 
see usage of these, there could be good reasons for that.




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


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 same with ChainMap:

d = dict(ChainMap(d1, d2, d3))

Yep, you can!

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


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 link:
>
> (untested)
> def func(x):
> return x+func.y
>
> func.y = 10
> func(5) => 15
> func.y = 100
> func(5) => 105
>
> implements a closure of a function.

That is not a closure. A closure is a construct of lexical scoping.
This is an example of a closure:

def f(x):
def g():
return x
return g

We say that the variables of f are "closed" over the function g. An
example of use:

py> g1 = f(42)
py> g2 = f(64)
py> g1()
42
py> g2()
64

Note that each closure has its own value of x.

The link suggests that object methods in Python are closures because
of the self argument, but I disagree with that; closures are
constructs of lexical scoping.
-- 
https://mail.python.org/mailman/listinfo/python-list


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 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.

def makeInc(x):
  def inc(y, moreinc=0):
 # x is "closed" in the definition of inc
 nonlocal x
 x += moreinc
 return y + x
  return inc

The 'nonlocal' keyword is like 'global', applying only to assignments
(the blog post already mentions the possibility of mutating an object
rather than reassigning it), and permitting assignment into a broader
scope than the function's locals. You can also have multiple closures
in the same context, and changes made by one of them will affect the
others.

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


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 = dict(d1)
>
> so I wondered if you can do the same with ChainMap:
>
> d = dict(ChainMap(d1, d2, d3))
>
> Yep, you can!

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".
-- 
https://mail.python.org/mailman/listinfo/python-list


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 usually pretty clear that
you get a brand-new object every time:

>>> x = [1,2,3,4]
>>> y = list(x)
>>> x == y
True
>>> x is y
False
>>> help(list)
class list(object)
 |  list() -> new empty list
 |  list(iterable) -> new list initialized from iterable's items

With immutables, the docs aren't always explicit, since by definition
it can't matter. Sometimes they are, though:

>>> x = 1,2,3
>>> tuple(x) is x
True
>>> help(tuple)
class tuple(object)
 |  tuple() -> empty tuple
 |  tuple(iterable) -> tuple initialized from iterable's items
 |
 |  If the argument is a tuple, the return value is the same object.

Sometimes things get rather interesting.

>>> help(int)
class int(object)
 |  int(x=0) -> integer
 |  int(x, base=10) -> integer
 |
 |  Convert a number or string to an integer, or return 0 if no arguments
 |  are given.  If x is a number, return x.__int__().  For floating point
 |  numbers, this truncates towards zero.
>>> class SubInt(int):
...  def __int__(self): return self
...
>>> x = SubInt(123)
>>> x.__int__() is x
True
>>> int(x) is x
False
>>> type(int(x))


Calling int(x) can return the exact object x, but only if x is an
actual int, not a subclass. If it's a subclass, you get a base integer
with the same value.

In any case, mutables are generally going to be safe: every time you
call the constructor, you get a new object. They won't try to cheat
and return a reference to the same object.

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


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, sneezy=70):
# the usual assign arguments to attributes dance...
self.bashful = bashful
self.doc = doc
# etc.


I also have an alternative constructor that will be called with string
arguments. It converts the strings to the appropriate type, then calls the
real constructor, which calls __init__. Again, I want the arguments to be
optional, which means providing default values:


@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 = float(dopey)
grumpy = int(grumpy)
happy = int(happy)
sleepy = int(sleepy)
sneezy = int(sneezy)
return cls(bashful, doc, dopey, grumpy, happy, sleepy, sneezy)


That's a pretty ugly DRY violation. Imagine that I change the default value
for bashful from 10.0 to (let's say) 99. I have to touch the code in three
places (to say nothing of unit tests):

- modify the default value in __init__
- modify the stringified default value in from_strings
- change the conversion function from float to int in from_strings


Not to mention that each parameter is named seven times.


How can I improve this code to reduce the number of times I have to repeat
myself?





-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


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='60', sneezy='70'):
> [...]
>
> That's a pretty ugly DRY violation. Imagine that I change the default
> value for bashful from 10.0 to (let's say) 99. I have to touch the
> code in three places (to say nothing of unit tests):

OMITTED = object()

def __init__(self, bashful=OMITTED, doc=OMITTED, dopey=OMITTED,
 grumpy=OMITTED, happy=OMITTED, sleepy=OMITTED,
 sneezy=OMITTED):
# the usual assign arguments to attributes dance...
self.bashful = 10.0 if bashful is OMITTED else bashful
self.doc = 20.0 if doc is OMITTED else doc
[...]

@classmethod
def from_strings(cls, bashful=OMITTED, doc=OMITTED dopey=OMITTED,
 grumpy=OMITTED, happy=OMITTED, sleepy=OMITTED,
 sneezy=OMITTED):
if bashful is not OMITTED:
bashful = float(bashful)
if doc is not OMITTED:
doc = float(doc)
[...]

Helper functions will make it look a bit less repetitious.


Marko
-- 
https://mail.python.org/mailman/listinfo/python-list


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 = float(dopey)
> grumpy = int(grumpy)
> happy = int(happy)
> sleepy = int(sleepy)
> sneezy = int(sneezy)
> return cls(bashful, doc, dopey, grumpy, happy, sleepy, sneezy)
>
>
> That's a pretty ugly DRY violation. Imagine that I change the default value
> for bashful from 10.0 to (let's say) 99. I have to touch the code in three
> places (to say nothing of unit tests):
>
> - modify the default value in __init__
> - modify the stringified default value in from_strings
> - change the conversion function from float to int in from_strings
>
>
> Not to mention that each parameter is named seven times.
>

You could go data-driven. Assuming that all your default values are in
the appropriate type (eg you use 10.0 rather than 10, when you want a
float), you could use those directly.

class Spam:
def __init__(self, bashful=10.0, doc=20.0, dopey=30.0,
 grumpy=40, happy=50, sleepy=60, sneezy=70):
for name, default in zip(self.__init__.__defaults__,
self.__init__.__code__.co_varnames[1:]):
setattr(self, name, type(default)(locals()[name]))

Your basic __init__ method is now capable of handling strings as well,
so from_strings can simply construct the object directly. I'm not sure
what the advantage of from_strings is, but assuming you still need it,
you could write it thus:

@classmethod
def from_strings(cls, bashful, doc, dopey, grumpy, happy, sleepy, sneezy):
return cls(bashful, doc, dopey, grumpy, happy, sleepy, sneezy)
from_strings.__func__.__defaults__ = tuple(str(x) for x in
__init__.__defaults__)

No duplication of type names or default values, though there is still
duplication of parameter names. You could eliminate that by going
*args,**kw, but at the expense of introspectability. Actually, you
could probably just use wraps...

@classmethod
@functools.wraps(__init__, assigned=())
def from_strings(cls, *a, **kw): return cls(*a, **kw)
from_strings.__func__.__defaults__ = tuple(str(x) for x in
__init__.__defaults__)

Though I'm not particularly enamoured of this way of doing it.

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


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, THERE!"

As that's not an option, I can do it either with string-hacking:

  try:
index = haystack.upper().find(needle.upper())
  except ValueError:
result = haystack
  else:
result = (
  haystack[:index]
  + replacement
  + haystack[index + len(needle):]
  )

or with regexes:

  import re
  r = re.compile(re.escape(needle), re.I)
  result = r.sub(replacement, haystack)

The regex version is certainly tidier, but it feels a bit like
killing a fly with a rocket-launcher.

Are there other/better options that I've missed?

Also, if it makes any difference, my replacement in this use-case is
actually deletion, so replacement=""

Thanks,

-tkc





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


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):
> # the usual assign arguments to attributes dance...
> self.bashful = bashful
> self.doc = doc
> # etc.


This looks like a situation where the GoF Builder pattern might help

-- 
Gerald Britton, MCSE-DP, MVP
LinkedIn Profile: http://ca.linkedin.com/in/geraldbritton
-- 
https://mail.python.org/mailman/listinfo/python-list


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_case=True)
  # result would be "Hello, THERE!"

As that's not an option, I can do it either with string-hacking:

  try:
index = haystack.upper().find(needle.upper())
  except ValueError:
result = haystack
  else:
result = (
  haystack[:index]
  + replacement
  + haystack[index + len(needle):]
  )

The disadvantage of your "string-hacking" is that you're assuming that 
the uppercase version of a string is the same length as the original:


That's not always the case:

Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:54:25) [MSC v.1900 64 
bit (AMD64)] on win32

Type "help", "copyright", "credits" or "license" for more information.
>>> '\N{LATIN SMALL LETTER SHARP S}'
'ß'
>>> '\N{LATIN SMALL LETTER SHARP S}'.upper()
'SS'
>>>


or with regexes:

  import re
  r = re.compile(re.escape(needle), re.I)
  result = r.sub(replacement, haystack)

The regex version is certainly tidier, but it feels a bit like
killing a fly with a rocket-launcher.

Are there other/better options that I've missed?

Also, if it makes any difference, my replacement in this use-case is
actually deletion, so replacement=""



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


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.

 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 = float(bashful)
 self.doc = float(doc)
 self.grumpy = int(grumpy)
 # etc.

Now the constructor can take strings as keywords.  You could even do
something like:

defaults = { 'bashful':10.0, 'grumpy':20, etc. }

def __init__(self, **kwargs):
   # check for spurious kwargs
   assert(not(set(kwargs)-set(defaults)))

   for k in defaults:
  v = type(defaults[k])(kwargs[k]) if k in kwargs else defaults[k]
  setattr(self, k, v)
-- 
https://mail.python.org/mailman/listinfo/python-list


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=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.
>
>
> This looks like a situation where the GoF Builder pattern might help

Can you elaborate on what "GoF builder" means? Presumably it's a
special case of the builder pattern, but my Google-fu has failed me on
this one.

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


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/python-list


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-list


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 of Four (GoF).

http://c2.com/cgi/wiki?GangOfFour

-- 
 GNU/Linux user #557453
Keyboard not detected! Press any key to continue...
-- 
https://mail.python.org/mailman/listinfo/python-list


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
-- 
https://mail.python.org/mailman/listinfo/python-list


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 GIL scheduling for threading), so
where does the extra overhead for threads come from ?


Thread switching is done by the interpreter, either when a thread calls 
a blocking i/o function or when a time slice expires.  You can read the 
asyncio.base_event code and other submodules for details on how asyncio 
works.


--
Terry Jan Reedy

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


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
> representation without having to specify it explicitly as in the line
> above

What are you trying to do?

If you're trying to avoid saying it twice:

exec('%s=%r'%['TransactionTerms']*2)

If you're trying to avoid having it as a string:

def TransactionTerms(): pass
TransactionTerms = TransactionTerms.__name__
-- 
https://mail.python.org/mailman/listinfo/python-list


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 might get more
usable suggestions.
-- 
https://mail.python.org/mailman/listinfo/python-list


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 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.

def makeInc(x):
  def inc(y, moreinc=0):
 # x is "closed" in the definition of inc
 nonlocal x
 x += moreinc
 return y + x
  return inc


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'.


def makeInc(x):
  def inc(y, moreinc=0):
 # x is "closed" in the definition of inc
 nonlocal x
 x += moreinc
 return y + x
  return inc

f = makeInc(23)
fclose = f.__closure__  # a tuple of 'cells'
fcell = fclose[0]

print(dir(fcell))
# ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__',
# '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
# '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__',
# '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
# '__sizeof__', '__str__', '__subclasshook__', 'cell_contents']
# Note: no mutation method

print('cell content = ', fcell.cell_contents)
# cell content = 23

fcell.cell_contents = 32
### results in
Traceback (most recent call last):
  File "F:\Python\mypy\tem.py", line 14, in 
fcell.cell_contents = 32
AttributeError: attribute 'cell_contents' of 'cell' objects is not writable
# unless one does so from within the closure body with 'nonlocal' 
declaration.  I presume there is a special byte code for this.



--
Terry Jan Reedy

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


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 from entirely without, but it's possible for two functions to
share a cell. I don't know the mechanics of how nonlocal assignment
works, but ultimately, it's updating the cell that both closures see,
so it's going to affect the other function too.

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


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/blog/2007/08/closures-in-python.html
>>>
>>
>> From the blog post:
>>
>> """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.
>>
>> def makeInc(x):
>>   def inc(y, moreinc=0):
>>  # x is "closed" in the definition of inc
>>  nonlocal x
>>  x += moreinc
>>  return y + x
>>   return inc
>
>
> 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'.
>
> def makeInc(x):
>   def inc(y, moreinc=0):
>  # x is "closed" in the definition of inc
>  nonlocal x
>  x += moreinc
>  return y + x
>   return inc
>
> f = makeInc(23)
> fclose = f.__closure__  # a tuple of 'cells'
> fcell = fclose[0]
>
> print(dir(fcell))
> # ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__',
> # '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
> # '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__',
> # '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
> # '__sizeof__', '__str__', '__subclasshook__', 'cell_contents']
> # Note: no mutation method
>
> print('cell content = ', fcell.cell_contents)
> # cell content = 23
>
> fcell.cell_contents = 32
> ### results in
> Traceback (most recent call last):
>   File "F:\Python\mypy\tem.py", line 14, in 
> fcell.cell_contents = 32
> AttributeError: attribute 'cell_contents' of 'cell' objects is not writable
> # unless one does so from within the closure body with 'nonlocal'
> declaration.  I presume there is a special byte code for this.

You could, however, put a mutable object in the cell variable and then
modify it from without. E.g.:

def makeInc(x):
  x = [x]
  def inc(y, moreinc=0):
x[0] += moreinc
return y + x[0]
  return inc

f = makeInc(23)
fcell = f.__closure__[0]
fcell.cell_contents
# [23]
fcell.cell_contents[0] = 42
f(0)
# 42
-- 
https://mail.python.org/mailman/listinfo/python-list


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 with a colon and press
enter, it automatically indents the next line, and it does
it using *tab* characters. You can see this by selecting the
indentation with the mouse. You'll notice that it highlights
in big chunks -- those are tab characters.

This means that any extra indentation you add yourself also
needs to be done by pressing the tab key, not the space bar,
otherwise you risk mixing up tabs and spaces in ways that
Python doesn't like.

--
Greg
--
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 "", line 1, in 
> TypeError: keyword arguments must be strings
> 
> On the other hand, '1' seemed to work (in Python 3.4.3) as key, though 1
> cannot possibly be a keyword parameter in a function definition :)

Pity. I wonder why it bothers to check?
-- 
https://mail.python.org/mailman/listinfo/python-list


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: 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.
-- 
https://mail.python.org/mailman/listinfo/python-list


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 Python to use the new one I've installed?
It's a framework install of Python 3.5 that I compiled
myself. Do I need to rebuild it to get it to pick up the
right Tcl? Or is there an environment variable I can set?

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


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 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 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" code.
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


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 ProcessPoolExecutor to do it.

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?
-- 
https://mail.python.org/mailman/listinfo/python-list


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 blog post:
>
> """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?

> def makeInc(x):
>   def inc(y, moreinc=0):
>  # x is "closed" in the definition of inc
>  nonlocal x
>  x += moreinc
>  return y + x
>   return inc
>
> The 'nonlocal' keyword is like 'global', applying only to assignments
> (the blog post already mentions the possibility of mutating an object
> rather than reassigning it), and permitting assignment into a broader
> scope than the function's locals. You can also have multiple closures
> in the same context, and changes made by one of them will affect the
> others.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list



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


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(10):

   for column in range(10):
  print("*",end="")

SyntaxError: inconsistent use of tabs and spaces in indentation

Why does the example on top work and the example below doesn't work ? The only difference 
is that the "print" statement is one space different from each other. Forgive 
me if i can't explain things clearly over the forum



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.)  Tabs are sometimes handled 
incorrectly/inconsistently in newsgroup postings.  But if you read the Traceback error message, 
it is telling you that you have a mix of tabs and spaces _in your original_.  READ the error 
messages, they are important!


--
 -=- Larry -=-
--
https://mail.python.org/mailman/listinfo/python-list


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 Python threads
> completely:-( I actually had ever considered using ProcessPoolExecutor
> to do it.
>
> 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?

Someone in the know would have to elaborate on whether python knows 
about, or can cooperate with the boot time parameter 'isolcpus ='.

Cheers, Gene Heskett
-- 
"There are four boxes to be used in defense of liberty:
 soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
Genes Web page 
-- 
https://mail.python.org/mailman/listinfo/python-list


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 blogger by
>> 'read-only'.
>>
>
> Not from entirely without, but it's possible for two functions to
> share a cell. I don't know the mechanics of how nonlocal assignment
> works, but ultimately, it's updating the cell that both closures see,
> so it's going to affect the other function too.

Standard example:

def make():
def inquire():
return balance
def deposit(amount):
nonlocal balance
balance += amount
def withdraw(amount):
nonlocal balance
balance -= amount
balance = 0
return inquire, deposit, withdraw

inq1, put1, get1 = make()
inq2, put2, get2 = make()

put1(30) ; get1(10) ; put1(40)
put2(500) ; put2(500) ; put2(500)

assert inq1() == 60
assert inq2() == 1500
-- 
https://mail.python.org/mailman/listinfo/python-list


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 
whitespace is actually used. For example, I use KNode, the KDE news client on 
Linux, and for me, KNode converts tabs to spaces.

(If this is configurable, I don't know how.)

BUT if I choose the following menu command:


View > View Source

I see the raw, unmodified message as posted by the OP, complete with tabs and 
spaces.



> READ the error messages, they are important!

Indeed.

And *believe* the error messages.


-- 
Steven
git gets easier once you get the basic idea that branches are homeomorphic 
endofunctors mapping submanifolds of a Hilbert space.

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


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 the
CPU affinity for all threads in a given process.

In Windows, you can use ctypes to call SetProcessAffinityMask,
SetThreadAffinityMask, or SetThreadIdealProcessor (a hint for the
scheduler). On a NUMA system you can call GetNumaNodeProcessorMask(Ex)
to get the mask of CPUs that are on a given NUMA node. The cmd shell's
"start" command supports "/numa" and "/affinity" options, which can be
combined.
-- 
https://mail.python.org/mailman/listinfo/python-list


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 means.

Languages described as "functional" usually incorporate
all or most of the following characteristics:

* No side effects (new variable bindings may be created, but
  existing ones cannot be changed; no mutable data structures).

* Lazy evaluation by default.

* Syntactic support for currying.

* Syntactic support for case-analysis-style definition of
  functions, by matching the arguments against a series of
  patterns.

* A standard library geared towards a programming style
  that makes heavy use of higher-order functions.

Python has none of these features. It's possible to use a
subset of Python in a functional way, but it goes against the
grain, and without all the above support from the ecosystem
it's clumsier than it would be in a real functional language.

Some parts of Python, such as list comprehensions, have a
functional flavour, but Python is predominantly an imperative
language.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


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?


Python 2 doesn't have "nonlocal", so a nested function can't
rebind variables in an intermediate scope (between local and
module-level).

Of course, anything is changeable if you resort to sufficient
amounts of hackery. In this case I believe the hacking level
required involves ctypes.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


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 s, randint")

t1.repeat(3, 10)
[1.459111982109448, 1.4568229341997494, 1.4329947660946232]

t2.repeat(3, 10)
[0.8499233841172327, 0.854728743457656, 0.8618653348400471]

I tried a search in a tuple, it's not different that in a list.
Any comments ?
--
https://mail.python.org/mailman/listinfo/python-list


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
>> .
>
>
> No, not according to what the term "functional language"
> usually means.
>
> Languages described as "functional" usually incorporate
> all or most of the following characteristics:
>
> * No side effects (new variable bindings may be created, but
>   existing ones cannot be changed; no mutable data structures).

If that's adhered to 100%, the language is useless for any operation
that cannot be handled as a "result at end of calculation" function.
You can't produce intermediate output. You can't even produce
debugging output (at least, not from within the program - you'd need
an external debugger). You certainly can't have a long-running program
that handles multiple requests (eg a web server). That kind of rule is
fine for mathematical systems, and it's fine for certain specific
sub-parts of larger programs, but IMO it's utterly impractical for an
entire programming language.

> * Lazy evaluation by default.

Given that you said "usually means", sure, but only because most
functional programming is abysmal (or impossible - calculating the
squares of all positive integers, then subscripting it) unless
optimized like that.

> * Syntactic support for currying.

Is that really such a big deal? What's the true difference between
syntactic support and stdlib support?

> * Syntactic support for case-analysis-style definition of
>   functions, by matching the arguments against a series of
>   patterns.

Again, why such a big deal? Unless you're deliberately defining
"functional language" as "clone of Haskell" or something, there's no
particular reason for this to be a requirement.

> * A standard library geared towards a programming style
>   that makes heavy use of higher-order functions.

This I would agree with.

> Python has none of these features. It's possible to use a
> subset of Python in a functional way, but it goes against the
> grain, and without all the above support from the ecosystem
> it's clumsier than it would be in a real functional language.
>
> Some parts of Python, such as list comprehensions, have a
> functional flavour, but Python is predominantly an imperative
> language.

Python is predominantly a *practical* language. Since purely
functional programming is fundamentally impractical, Python doesn't
force us into it. You want to work functionally? No problem. Pretend
that "def" is a declaration, make all your functions pure, and either
finish with a single "print(main())" or do your actual work at the
REPL. No problem. Want to make event-driven code? Hide an event loop
(maybe a GUI one, maybe asyncio, whatever) in your main routine, and
do everything through that. Want to use Smalltalk-style "message
passing"? Fits nicely into classes and methods. Want a simple batch
scripting style? Ignore functions and classes, and write your code
flush left. Python doesn't force you into any of these models, ergo
Python isn't a functional language, nor an event-driven language, etc,
etc. It's a general-purpose language.

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


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 __main__ import l, randint")
t2 = Timer("randint(0, 200) in s", "from __main__ import s, randint")

t1.repeat(3, 10)
[1.459111982109448, 1.4568229341997494, 1.4329947660946232]

t2.repeat(3, 10)
[0.8499233841172327, 0.854728743457656, 0.8618653348400471]

I tried a search in a tuple, it's not different that in a list.
Any comments ?


I tried in an array which is supposed to be an optimised list, but it is 
slightly
slower

from array import array

a = array('H', [i for i in range(100)])
t3 = Timer("randint(0, 200) in a", "from __main__ import a, randint")

t3.repeat(3, 10)
[1.636356968114, 1.638082912772461, 1.6018925589704622] 


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


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.permissions = args.permissions
self.configurationId = args.configurationId
self.tokenExpiry = args.tokenExpiryInSeconds

def generate(self):
payload = {
'iss': self.authzSystemId,
'aud': 'qed:' + self.configurationId,
'sub': 'admin:tokengen.py',
'qedp': self.permissions,
'exp': timegm(datetime.utcnow().utctimetuple()) + self.tokenExpiry
}
if self.tokenExpiry <= 300: # less than 5minutes
payload['jti'] = str(uuid.uuid1())
return jwt.encode(payload, self.macKey, algorithm='HS256')

class JWTParms:
 pass



def GenAccessToken(mackey,authid,configid,tokenexp,*perm):
args=JWTParms()
args.configurationId=configid
args.authzSystemMacKey=mackey
args.authzSystemId=authid
args.tokenExpiryInSeconds=tokenexp
args.permissions=perm
tokenGen=TokenGenerator(args)
tok=tokenGen.generate()
return tok

if __name__ == '__main__':
  
GenAccessToken("This_is_a_Test_QED_MAC_Key_Which_Needs_to_be_at_Least_32_Bytes_Long",
 "default", "default", 6,
   "g,m,a,s,c,p,d")

When i am calling the above method from main method it is not returning the 
value but when i use print it is showing the value. Is there any wrong in 
returning the value from a method.

Please help me ASAP
-- 
https://mail.python.org/mailman/listinfo/python-list


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.permissions = args.permissions
self.configurationId = args.configurationId
self.tokenExpiry = args.tokenExpiryInSeconds

def generate(self):
payload = {
'iss': self.authzSystemId,
'aud': 'qed:' + self.configurationId,
'sub': 'admin:tokengen.py',
'qedp': self.permissions,
'exp': timegm(datetime.utcnow().utctimetuple()) + self.tokenExpiry
}
if self.tokenExpiry <= 300: # less than 5minutes
payload['jti'] = str(uuid.uuid1())
return jwt.encode(payload, self.macKey, algorithm='HS256')

class JWTParms:
 pass



def GenAccessToken(mackey,authid,configid,tokenexp,*perm):
args=JWTParms()
args.configurationId=configid
args.authzSystemMacKey=mackey
args.authzSystemId=authid
args.tokenExpiryInSeconds=tokenexp
args.permissions=perm
tokenGen=TokenGenerator(args)
tok=tokenGen.generate()
return tok

if __name__ == '__main__':
  
GenAccessToken("This_is_a_Test_QED_MAC_Key_Which_Needs_to_be_at_Least_32_Bytes_Long",
 "default", "default", 6,
   "g,m,a,s,c,p,d")

when i am calling the above method it is not returning any value but when i use 
print it is printing the value. Is there any wrong in returning the value from 
above method. Please help me ASAP
-- 
https://mail.python.org/mailman/listinfo/python-list


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 of 'Append a new action at 
the end/beginning'.

My English is poor:-),I hope I express clearly what I mean.
-- 
https://mail.python.org/mailman/listinfo/python-list