Package: kwkey
PEP 472 -- Support for indexing with keyword argumentsThis package provides experimental support for indexing with keyword
argments. This is proposed inPEP
637. See also PEP 637's
predecessor,PEP 472.We can writefn(1,2,a=3,b=4)and some users wish to be able to writed[1,2,a=3,b=4]particularly for use in data science and annotations.Helper functionoThe module provide a helper functionfromkwkeysimportosuch thatd[o(1,2,a=3,b=4)]works as well as can be reasonably expected, or so the author thinks.In module kwkey/example_jfine.py there's an implementation of a class
PointMap. It is a subclass of dict which supports namedtuple style
access to keys.My approach uses a helper class K for implementing these
experiments. Some wish for the syntax of Python to be extended, to
allow keyword arguments in the d[...] syntax.If the syntax is extended, I believe a helper class such as K would be
a good idea.  Steven D'Aprano believe that K is irrelevant, except for
experimentation.This package, once refined and production ready, could be used to
allow the the style of the new syntax to be used with current Python,
viad[o(1,2,a=3,b=4)]instead ofd[1,2,a=3,b=4]Items-key dualityThis package, from version 0.0.2, also provides items-key
duality. Roughly speaking, duality is writingkey[items]instead ofitems[key]. Python already supports something like this:>>>fromoperatorimportitemgetterTypeError:'operator.itemgetter'objectisnotsubscriptable>>>itemgetter(3)('abcde')'d'To provide duality we need a special classXsuch thatX(1,2,a=3,b=4)[items]is equivalent (for the chosen future semantics of keyword arguments)
to:items[1,2,a=3,b=4]In current Python the following three statements are already valid
syntax.X(1,2,a=3,b=4)[items]=valval=X(1,2,a=3,b=4)[items]delX(1,2,a=3,b=4)[items]PEP 637 proposes that we allow keywords inside the square brackets[...]. Using items-key duality, we can move the keywords to outside
the square brackets, into a context where they are allowed.Semantics of keyword argumentsFor this to work, a suitable classXmust be provided, that
implements the desired semantics. This package provides three such
classes.ClassA- the current semanticsHere's an example. For more, see filekwkey/test_duality.py.>>>A(1,2)[log]='val'log:setitem(*((1,2),'val'),**{})This means that the following three statements are equivalent.log[(1,2)]='val'A((1,2))[log]='val'log.__setitem__((1,2),'val')ClassB- the semantics proposed by D'Aprano and von Rossum>>>B(1,2,a=3,b=4)[log]log:getitem(*((1,2),),**{'a':3,'b':4})The previous setitem call is equivalent tosetitem((1,2),'val',a=3,b=4)Whennokeywordsarepresent,thisgivethesamesemanticsasclass`A`.### Class `C` - the semantics propose by FineHerethesemanticsofkeywordsdependonaspecialattributeof`items`,whichwecall`keyfn`.(Strictlyspeaking,itshouldbeanattribureof`type(items)`.#### DefaultThedefaultistogivethesamesemanticsasclass`A`.```python>>>C((1,2))[log]='val'log:setitem(*((1,2),'val'),**{})The previous setitem call is equivalent tosetitem((1,2),'val')NoneIf the special attributekeyfnisNonewe get>>>C(1,2,a=1,b=2)[nokey]='val'log:setitem(*('val',1,2),**{'a':3,'b':4})The previous setitem call is equivalent tosetitem('val',1,2,a=3,b=4)User suppliedkeyfnThe classtype(items)can supply its ownkeyfn, to provide custom
behaviour. Here's an example (seekwkey/test_duality.pyfor
details).>>>C(1,2,a=1,b=2)[k_log]='val'log:setitem(*(K(1,2,a=1,b=2),'val'),**{})The previous setitem call is equivalent tosetitem(K(1,2,a=1,b=2),'val')whereKis a new class, introduced by packagekwkey.DiscussionFor discussion of PEP 472 and PEP 637, visit thePython
ideasmiling list.To test installation, do$ python3 -m kwkey.test
kwkey TestResults(failed=0, attempted=15)
kwkey.duality TestResults(failed=0, attempted=1)
kwkey.example_jfine TestResults(failed=0, attempted=7)
kwkey.jfine TestResults(failed=0, attempted=0)
kwkey.sdaprano TestResults(failed=0, attempted=0)
kwkey.test TestResults(failed=0, attempted=0)
kwkey.test_duality TestResults(failed=0, attempted=65)
kwkey.test_jfine TestResults(failed=0, attempted=19)
kwkey.test_K TestResults(failed=0, attempted=8)
kwkey.test_sdaprano TestResults(failed=0, attempted=25)
kwkey.tools TestResults(failed=0, attempted=0)and check for no failures. (The number of tests will change over time.)