Skip to content
This repository has been archived by the owner on May 31, 2019. It is now read-only.

Suggestion: keyworddict #13

Open
jml opened this issue Sep 25, 2013 · 1 comment
Open

Suggestion: keyworddict #13

jml opened this issue Sep 25, 2013 · 1 comment

Comments

@jml
Copy link
Collaborator

jml commented Sep 25, 2013

Not 100% sure this is a good idea, but here we go.

The natural way to use maps in Clojure is to use keywords for the keys. In Python, the natural way is to use strings for keys. When writing Python code that works with Clojure code via edn, it becomes tedious to make dicts with Keywords as keys.

So, perhaps it's worth adding a convenience class which is a dict where all of the keys are keywords, but can be accessed & updated using string syntax, but are stored internally as keywords.

These examples are meant to be illustrative, not prescriptive.

>>> x = keyworddict()
>>> x.with_pair('foo', 'bar')
>>> repr(x)
{Keyword(Symbol('foo')): 'bar'}
>>> x['foo']
'bar'
>>> dumps(x)
{:foo "bar"}
>>> x.with_pair(42, 'qux')
Traceback (most recent call last):
  ...
ValueError: Invalid keyword: 42

And also,

>>> x = keyworddict(prefix="myproject")
>>> x.with_pair('foo', 'bar')
>>> repr(x)
{Keyword(Symbol('foo', 'myproject')): 'bar'}
>>> x['foo']
'bar'
>>> dumps(x)
{:myproject/foo "bar"}
@jml
Copy link
Collaborator Author

jml commented Sep 25, 2013

If we publish a richer Keyword object, then we could also do things like:

>>> x = keyworddict()
>>> x.with_pair('foo', 'bar')
>>> Keyword('foo').get(x)
'bar'

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant