<div dir="ltr"><div>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.</div>
<div><br></div><div>In my opinion, a concept like this would be best located in the functools module.</div><div><br></div><div><br></div><b>Overview:</b><div> 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''.</div>
<div><br></div><div>>> myset = predicateset(lambda s: s.startswith('a'))<br>>> 'xyz' in myset<br>False<br>>> 'abc' in myset<br>True<br>>> len(myself)<br>Traceback (most recent call last):<br>
[...]<br>TypeError</div><div><b><br></b></div><div><b>Example Uses:</b><br></div><div># Dynamic excludes in searching</div><div>foo_files = search_files('foo', exclude=set(['a.out', 'Makefile']))<br>
</div><div>bar_files = search_files('bar', exclude=predicateset(lambda fname: not fname.endswith('~'))) # exclude *~</div><div><br></div><div># Use in place of a set with an ORM</div><div>validusernames = predicateset(lambda s: re.match(s, '[a-zA-Z0-9]+'))</div>
<div><br></div><div>class Users(db.Model):</div><div> username = db.StringProperty(choices=validusernames)</div><div> password = db.StringProperty()</div><div><br></div><div><br></div><div><br></div></div>