[Python-ideas] Predicate Sets
Ian Foote
ian at feete.org
Mon Jan 20 01:04:59 CET 2014
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 19/01/14 23:41, Daniel da Silva wrote:
> Below is a description of a very simple but immensely useful class
> called a "predicate set". In combination with the set and list
> comprehensions they would allow another natural layer of reasoning
> with mathematical set logic in Python.
>
> In my opinion, a concept like this would be best located in the
> functools module.
>
>
> *Overview:* Sets in mathematics can be defined by a list of
> elements without repetitions, and alternatively by a predicate
> (function) that determines inclusion. A predicate set would be a
> set-like class that is instantiated with a predicate function that
> is called to determine ``a in the_predicate_set''.
>
>>> myset = predicateset(lambda s: s.startswith('a')) 'xyz' in
>>> myset
> False
>>> 'abc' in myset
> True
>>> len(myself)
> Traceback (most recent call last): [...] TypeError * * *Example
> Uses:* # Dynamic excludes in searching foo_files =
> search_files('foo', exclude=set(['a.out', 'Makefile'])) bar_files =
> search_files('bar', exclude=predicateset(lambda fname: not
> fname.endswith('~'))) # exclude *~
>
> # Use in place of a set with an ORM validusernames =
> predicateset(lambda s: re.match(s, '[a-zA-Z0-9]+'))
>
> class Users(db.Model): username =
> db.StringProperty(choices=validusernames) password =
> db.StringProperty()
>
>
Hi Daniel,
That's an interesting idea. I'm not sure it would be used enough to
include in the standard library though. Have you considered releasing
an implementation on PyPI? That has the advantage that people can
start using it earlier than would be possible if it was added to the
standard library.
Regards,
Ian
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.14 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
iQEcBAEBAgAGBQJS3GgrAAoJEODsV4MF7PWzI1EH/0FKiJYKZgRd6iW04Ic9NPXw
QL+EKQU0UdRjCvP9IWrBSdGYnmB06YHdwyeLPpk0+amGSzXpsMGNRHtAXhxjba00
1Q9UKHnVcIj3kgjfYg+LKezMVJHQF4vE+umrbMQFeWBt7FEKfqseCbyDRIZAm9I8
G/dOzP3dxC4lktlCtLv6sfVD8D648A9wMNX5879SoUKjX+Qs0ySZ9CVxhBbyFVgP
kXLG1/9NlmkyJmWsL6hHwWYI9WwnJ433Ts74bqmwOaTDlGdmmZNHfQT5kIHzRK8V
g8XXZWxct8EVvTjyL+//n+DuSsFEDxhXTX0gGXMs0xDXunbDBHWNggs9G2B+GI0=
=WsXD
-----END PGP SIGNATURE-----
More information about the Python-ideas
mailing list