Re: Getting a dictionnary from a module's variables

2017-07-28 Thread Gregory Ewing

ast wrote:

dir(config) provides a list, some processing is needed to
remove some __identifiers__ and get a dict

Is there a simple way to do that, without processing ?


Here's an alternative that leverages the import machinery.

d = {}
exec("from config import *", d)
del d['__builtins__']

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


Re: zipapp should not include temporary files?

2017-07-28 Thread Irmen de Jong
On 28/07/2017 18:36, Irmen de Jong wrote:
> On 27/07/2017 00:03, Paul Moore wrote:
>> If you want to create a feature request for a filter function on 
>> bugs.python.org and assign it to me, I'll take a look at it. \
> 
> 
> I will do this, thanks in advance.

Should have included a link perhaps. Here it is: 
http://bugs.python.org/issue31072

Irmen

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


Re: Installation Python 3.6.x on Windows using command line installer (without GUI)

2017-07-28 Thread Irmen de Jong
On 27/07/2017 20:55, Andreas Jung wrote:
> 
> I need to installed Python 3.6.x on Windows as part of an automated process 
> without user-interaction. Recently Python releases provided MSI files for 
> installation using the "msiexec" utility however there are no more MSI 
> release files available for Python 3.6.X. Are there any alternatives?
> 
> -aj
> 

https://docs.python.org/3/using/windows.html#installing-without-ui

Irmen

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


Re: zipapp should not include temporary files?

2017-07-28 Thread Irmen de Jong
On 27/07/2017 00:03, Paul Moore wrote:
> On Wednesday, 26 July 2017 18:37:15 UTC+1, Irmen de Jong  wrote:

>> What do you think? Should the zipapp module perhaps be improved to 
>> automatically skip
>> obvious temporary files or perhaps allow to provide a filter function?

> If you want to create a feature request for a filter function on 
> bugs.python.org and assign it to me, I'll take a look at it. \


I will do this, thanks in advance.

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


Re: how to group by function if one of the group has relationship with another one in the group?

2017-07-28 Thread Ho Yeung Lee
actually i used in this application
if same color is neighbor like connected then group them

i use for segmentation of words in screen capture

https://stackoverflow.com/questions/45294829/how-to-group-by-function-if-any-one-of-the-group-members-has-neighbor-relationsh

i asked here too, but i do not know how to use partial and do not know what 
center is.


On Tuesday, July 25, 2017 at 5:00:25 PM UTC+8, Peter Otten wrote:
> Ho Yeung Lee wrote:
> 
> > from itertools import groupby
> > 
> > testing1 = [(1,1),(2,3),(2,4),(3,5),(3,6),(4,6)]
> > def isneighborlocation(lo1, lo2):
> > if abs(lo1[0] - lo2[0]) == 1  or lo1[1] == lo2[1]:
> > return 1
> > elif abs(lo1[1] - lo2[1]) == 1  or lo1[0] == lo2[0]:
> > return 1
> > else:
> > return 0
> > 
> > groupda = groupby(testing1, isneighborlocation)
> > for key, group1 in groupda:
> > print key
> > for thing in group1:
> > print thing
> > 
> > expect output 3 group
> > group1 [(1,1)]
> > group2 [(2,3),(2,4]
> > group3 [(3,5),(3,6),(4,6)]
> 
> groupby() calculates the key value from the current item only, so there's no 
> "natural" way to apply it to your problem.
> 
> Possible workarounds are to feed it pairs of neighbouring items (think 
> zip()) or a stateful key function. Below is an example of the latter:
> 
> $ cat sequential_group_class.py
> from itertools import groupby
> 
> missing = object()
> 
> class PairKey:
> def __init__(self, continued):
> self.prev = missing
> self.continued = continued
> self.key = False
> 
> def __call__(self, item):
> if self.prev is not missing and not self.continued(self.prev, item):
> self.key = not self.key
> self.prev = item
> return self.key
> 
> def isneighborlocation(lo1, lo2):
> x1, y1 = lo1
> x2, y2 = lo2
> dx = x1 - x2
> dy = y1 - y2
> return dx*dx + dy*dy <= 1
> 
> items = [(1,1),(2,3),(2,4),(3,5),(3,6),(4,6)]
> 
> for key, group in groupby(items, key=PairKey(isneighborlocation)):
> print key, list(group)
> 
> $ python sequential_group_class.py 
> False [(1, 1)]
> True [(2, 3), (2, 4)]
> False [(3, 5), (3, 6), (4, 6)]

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


Re: how to group by function if one of the group has relationship with another one in the group?

2017-07-28 Thread Ho Yeung Lee
actually i used in this application
if same color is neighbor like connected then group them

i use for segmentation of words in screen capture

https://stackoverflow.com/questions/45294829/how-to-group-by-function-if-any-one-of-the-group-members-has-neighbor-relationsh

i asked here too, but i do not know how to use partial and do not know what 
center is.



On Wednesday, July 26, 2017 at 2:06:08 PM UTC+8, ast wrote:
> "Ho Yeung Lee"  a écrit dans le message de 
> news:ef0bd11a-bf55-42a2-b016-d93f3b831...@googlegroups.com...
> > from itertools import groupby
> >
> > testing1 = [(1,1),(2,3),(2,4),(3,5),(3,6),(4,6)]
> > def isneighborlocation(lo1, lo2):
> >if abs(lo1[0] - lo2[0]) == 1  or lo1[1] == lo2[1]:
> >return 1
> >elif abs(lo1[1] - lo2[1]) == 1  or lo1[0] == lo2[0]:
> >return 1
> >else:
> >return 0
> >
> > groupda = groupby(testing1, isneighborlocation)
> > for key, group1 in groupda:
> >print key
> >for thing in group1:
> >print thing
> >
> > expect output 3 group
> > group1 [(1,1)]
> > group2 [(2,3),(2,4]
> > group3 [(3,5),(3,6),(4,6)]
> 
> Its not clear to me how you build the groups
> 
> Why (1,1) is not in group2 since (1,1) is
> a neighbor to both (2,3) and (2,4) ?

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


Re: Falsey Enums

2017-07-28 Thread Ethan Furman

On 07/28/2017 01:13 AM, Ben Finney wrote:

Ethan Furman writes:



class X(Enum):
 Falsey = 0
 Truthy = 1
 Fakey = 2
 def __bool__(self):
 return bool(self.value)


I am surprised this is not already the behaviour of an Enum class,
without overriding the ‘__bool__’ method.

What would be a good reason not to have this behaviour by default for
‘Enum.__bool__’? (i.e. if this were reported as a bug on the ‘enum.Enum’
implementation, what would be good reasons not to fix it?)


It matches the docs. ;)

  
https://docs.python.org/3/library/enum.html#boolean-value-of-enum-classes-and-members

  Enum members that are mixed with non-Enum types (such as int, str, etc.)
  are evaluated according to the mixed-in type’s rules; otherwise, all
  members evaluate as True. To make your own Enum’s boolean evaluation
  depend on the member’s value add the following to your class:

  Enum classes always evaluate as True.

The rationale is in PEP 435, in the functional API section.

  The reason for defaulting to 1 as the starting number and not 0 is that 0 is 
False
  in a boolean sense, but enum members all evaluate to True .


Which still begs the question of:  why?  According to memory, and as Rustom guessed, an Enum member is a thing, and all 
things in Python default to being True.  If `bool(thing) == False` is a desirable characteristic then extra steps must 
be taken.  Either:


- subclass a type that already has that characteristic, such as int or float, or
- add your own __bool__ method (__nonzero__ in Python 2)

Looked at another way:  The value attribute is just that -- an attribute.  Depending on the use-case that attribute can 
be irrelevant (which is why we have auto(), etc.), so by default Enum does not use the .value attribute in figuring out 
if a member is something vs nothing (or truthy vs falsey).


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


Re: Recent Spam problem

2017-07-28 Thread Skip Montanaro
Yes, it's more "leaky," though that's not quite the term I'd use. Instead,
I'd say there are fewer checks. On the mailing list side of things, you
have all the Postfix bells and whistles, which stop a ton of crap, much of
it before the message is officially entered into the mail.python.org
system. Behind that is a SpamBayes instance to pick up any loose ends.

The Usenet gateway feeds into the system behind everything except the
SpamBayes instance. It gets only sporadic attention from me. If I'm not
paying attention, stuff which starts to "leak" through doesn't get trained
as spam so it can help minimize the chances that later versions of the same
crap get through.

One thing which never got produced was an easy way for a list moderator to
say, "Hey, this got through and it's spam." Sorting through "unsure"
messages and retraining automatically using some Mailman/SpamBayes conduit
would be a nice addition to the overall system. If you wanted to write
software, that's where I'd focus my efforts.

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


Re: Getting a dictionnary from a module's variables

2017-07-28 Thread ast


"ast"  a écrit dans le message de 
news:597b31fb$0$4823$426a3...@news.free.fr...

I answer to myself

import config
dico = {k:v for k, v in vars(conf).items() if not (k.startswith('__') or 
k.endswith('__'))}

that's not so difficult 


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


Getting a dictionnary from a module's variables

2017-07-28 Thread ast

Hello

I have a file conf.py which only contains some
variables definition like that:

a = 7
b = 9
c = 3

In my main program I would like to get a dictionnary
dico = {'a' :7,'b':9, 'c':3}

I tried:

import conf

dico = vars(conf)
but there is among a huge amount of stuff to remove

dir(config) provides a list, some processing is needed to
remove some __identifiers__ and get a dict

Is there a simple way to do that, without processing ?



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


Re: Falsey Enums

2017-07-28 Thread Chris Angelico
On Fri, Jul 28, 2017 at 8:28 PM, Steve D'Aprano
 wrote:
> On Fri, 28 Jul 2017 05:52 pm, Ethan Furman wrote:
>
>> class X(Enum):
>>  Falsey = 0
>>  Truthy = 1
>>  Fakey = 2
>>  def __bool__(self):
>>  return bool(self.value)
>
> Thanks Ethan.
>
> Like Ben, I'm surprised that's not the default behaviour.

Because members of an Enum are considered to be "things". If you want
them to behave more like integers, instead subclass IntEnum:

>>> class Y(IntEnum):
...  Falsey = 0
...  Truthy = 1
...  File_Not_Found = 2
...
>>> Y.Falsey

>>> bool(Y.Falsey)
False
>>> bool(Y.Truthy)
True

Among other differences, this means that zero is considered falsey,
and that the enumerated variables compare equal to the corresponding
integers.

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


Re: Falsey Enums

2017-07-28 Thread Steve D'Aprano
On Fri, 28 Jul 2017 05:52 pm, Ethan Furman wrote:

> class X(Enum):
>  Falsey = 0
>  Truthy = 1
>  Fakey = 2
>  def __bool__(self):
>  return bool(self.value)

Thanks Ethan.

Like Ben, I'm surprised that's not the default behaviour.



-- 
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: Falsey Enums

2017-07-28 Thread Rustom Mody
On Friday, July 28, 2017 at 1:45:46 PM UTC+5:30, Ben Finney wrote:
> Ethan Furman  writes:
> 
> > class X(Enum):
> > Falsey = 0
> > Truthy = 1
> > Fakey = 2
> > def __bool__(self):
> > return bool(self.value)
> 
> I am surprised this is not already the behaviour of an Enum class,
> without overriding the ‘__bool__’ method.
> 
> What would be a good reason not to have this behaviour by default for
> ‘Enum.__bool__’? (i.e. if this were reported as a bug on the ‘enum.Enum’
> implementation, what would be good reasons not to fix it?)


Enums are for abstracting away from ints (typically small) to more meaningful 
names.
In python's terms that means whether X.Truthy should mean 0 — the value —
or "Truthy" — the name — is intentionally left ambiguous/undecided.

Observe:

>>> print (X.Truthy)
X.Truthy# So Truthy is well Truthy
>>> X.Truthy
  # No! Truthy is 1

# In other words
>>> repr(X.Truthy)
''
>>> str(X.Truthy)
'X.Truthy'
>>> 



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


Re: Falsey Enums

2017-07-28 Thread Ben Finney
Ethan Furman  writes:

> class X(Enum):
> Falsey = 0
> Truthy = 1
> Fakey = 2
> def __bool__(self):
> return bool(self.value)

I am surprised this is not already the behaviour of an Enum class,
without overriding the ‘__bool__’ method.

What would be a good reason not to have this behaviour by default for
‘Enum.__bool__’? (i.e. if this were reported as a bug on the ‘enum.Enum’
implementation, what would be good reasons not to fix it?)

-- 
 \ “As scarce as truth is, the supply has always been in excess of |
  `\   the demand.” —Josh Billings |
_o__)  |
Ben Finney

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


Re: Falsey Enums

2017-07-28 Thread Ethan Furman

On 07/27/2017 07:15 PM, Steve D'Aprano wrote:


I has some Enums:

from enum import Enum
class X(Enum):
 Falsey = 0
 Truthy = 1
 Fakey = 2


and I want bool(X.Falsey) to be False, and the others to be True. What should I
do?


class X(Enum):
Falsey = 0
Truthy = 1
Fakey = 2
def __bool__(self):
return bool(self.value)

--
~Ethan~

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


Re: Falsey Enums

2017-07-28 Thread Paul Rubin
Dan Sommers  writes:
> def __bool__(self):
> return False if self == X.Falsey else True

return self != X.Falsey
-- 
https://mail.python.org/mailman/listinfo/python-list