Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mixed tuples and dictionaries #20

Open
clayrisser opened this issue Jun 27, 2017 · 4 comments
Open

Mixed tuples and dictionaries #20

clayrisser opened this issue Jun 27, 2017 · 4 comments

Comments

@clayrisser
Copy link

Is it possible to do mixed tuples and dictionaries???

There are times when I need to use special characters for the key, and tuples can't handle that.

@brennv
Copy link
Owner

brennv commented Jul 1, 2017

I think what you're after is possible with some exceptions:
https://stackoverflow.com/questions/30503539/namedtuple-and-unicode-string

@clayrisser
Copy link
Author

I want to use a forward slash in a tuple

@brennv
Copy link
Owner

brennv commented Jul 7, 2017

This brings up a good point that's missing from the docs. We are currently limited to valid Python identifiers.

>>> import namedtupled as nt 
>>> nt.map({'/x':3})

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/../lib/python3.6/site-packages/namedtupled/namedtupled.py", line 11, in mapper
    return namedtuple_wrapper(_nt_name, **mapping)
  File "/../lib/python3.6/site-packages/namedtupled/namedtupled.py", line 18, in namedtuple_wrapper
    wrap = namedtuple(_nt_name, kwargs)
  File "/../lib/python3.6/collections/__init__.py", line 400, in namedtuple
    'identifiers: %r' % name)
ValueError: Type names and field names must be valid identifiers: '/x'

Another use case that would cause issues:

>>> nt.map({2: 'a'})

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/../lib/python3.6/site-packages/namedtupled/namedtupled.py", line 11, in mapper
    return namedtuple_wrapper(_nt_name, **mapping)
TypeError: namedtuple_wrapper() keywords must be strings

It might be helpful if we added a pre-flight check for valid Python identifiers:

>>> '/x'.isidentifier()
False
>>> '2'.isidentifier()
False
>>> 'meow.jpg'.isidentifier()
False

But in the end, using invalid Python identifiers would not be possible without some sort of character substitution schema. As an example:

{'example.com/pic-clicks': {'meow.jpg': 675}} 

Might be mapped to something like this, which is not ideal:

>>> example_dot_com_slash_pic-clicks.meow_dot_jpg
675

@clayrisser
Copy link
Author

Yeah, it should at least be mentioned in the docs. Adding some sort of mapping capabilities might not be a bad solution.

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

No branches or pull requests

2 participants