Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add mapping methods to types.SimpleNamespace #84465

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

Closed
rhettinger opened this issue Apr 14, 2020 · 11 comments
Closed

Add mapping methods to types.SimpleNamespace #84465

rhettinger opened this issue Apr 14, 2020 · 11 comments
Labels
3.9 only security fixes stdlib Python modules in the Lib dir

Comments

@rhettinger
Copy link
Contributor

BPO 40284
Nosy @rhettinger, @vstinner, @ericsnowcurrently, @vedgar, @shihai1991
Files
  • Dotable.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-12-22.02:54:50.082>
    created_at = <Date 2020-04-14.21:10:49.117>
    labels = ['library', '3.9']
    title = 'Add mapping methods to types.SimpleNamespace'
    updated_at = <Date 2020-12-22.02:54:50.081>
    user = 'https://github.com/rhettinger'

    bugs.python.org fields:

    activity = <Date 2020-12-22.02:54:50.081>
    actor = 'rhettinger'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-12-22.02:54:50.082>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2020-04-14.21:10:49.117>
    creator = 'rhettinger'
    dependencies = []
    files = ['49061']
    hgrepos = []
    issue_num = 40284
    keywords = []
    message_count = 10.0
    messages = ['366447', '366448', '366449', '366451', '366453', '366463', '366485', '366486', '366499', '366822']
    nosy_count = 6.0
    nosy_names = ['rhettinger', 'vstinner', 'v+python', 'eric.snow', 'veky', 'shihai1991']
    pr_nums = []
    priority = 'normal'
    resolution = 'rejected'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue40284'
    versions = ['Python 3.9']

    @rhettinger
    Copy link
    Contributor Author

    types.SimpleNamespace() would be much more usable if it were more substitutable for dictionaries. In particular, it would be wonderful to be able to use object_hook=SimpleNamespace in json.load():

    Current:

         catalog = json.load(f)
         print(catalog['clothing']['mens']['shoes']['extra_wide']['quantity'])

    Proposed:

         catalog = json.load(f, object_hook=SimpleNamespace)
         print(catalog.clothing.mens.shoes.extra_wide.quantity])

    @rhettinger rhettinger added 3.9 only security fixes stdlib Python modules in the Lib dir labels Apr 14, 2020
    @rhettinger
    Copy link
    Contributor Author

    Only the magic methods need to be added: __getitem__, __setitem__, and __delitem__, __contains__, __len__, and __iter__.

    The non-dunder names risk incursion into user-space names.

    >>> SimpleNamespace(username1='value1', username2='value2')
    namespace(username1='value1', username2='value2')
    >>> dir(_)
    ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'username1', 'username2']

    @vstinner
    Copy link
    Member

    I would prefer that SimpleNamespace remains as simple as it is:

    https://docs.python.org/dev/library/types.html#types.SimpleNamespace
    "A simple object subclass that provides attribute access to its namespace"

    If you want to add the mapping protocol, I suggest you to create your own subclass and add the methods that you want.

    @rhettinger
    Copy link
    Contributor Author

    I would prefer that SimpleNamespace remains as simple as it is

    This doesn't affect the simplicity of the current API at all. If you don't need the feature, you won't even notice the extension.

    If you want to add the mapping protocol, I suggest you to create
    your own subclass and add the methods that you want

    I do that for myself. However, users would greatly benefit from having this an option.

    We don't ask users to write their own defaultdicts from scratch even though that is simple dict subclass. Providing this tool would instantly benefit a broad class of json users.

    @vstinner
    Copy link
    Member

    Providing this tool would instantly benefit a broad class of json users.

    I'm not saying that there is no need for such tool. I am just asking to leave SimpleNamespace unchanged.

    @rhettinger
    Copy link
    Contributor Author

    I'm not saying that there is no need for such tool.
    I am just asking to leave SimpleNamespace unchanged.

    I really don't see the downside. It doesn't impair SimpleNamespace in any way. Why would we add another type with substantially the same capabilities as SimpleNamespace? That would be a complete waste.

    @vpython
    Copy link
    Mannequin

    vpython mannequin commented Apr 15, 2020

    Yes, I laud this effort. I don't care if it is called SimpleNamespace (which I didn't use because it was insufficient), or anything else, but it would be extremely handy to have this battery.

    I eventually found one called Dotable (or did I rename it to that?) which after a fair bit of study I finally understood, and then was able to add a few tweaks to make it work the way that was most convenient for me.

    While I agree with Guido that these are not standard Python semantics, they are, as pointed out by Raymond, far more convenient to use for nested structures.

    And as far as using CoffeeScript, I don't know of a CoffeeScript to Python conversion: the rest of the semantics of Python are more preferable than Javascript. I just wish Brendan Eich had consulted with Guido before inventing Javascript.

    @vpython
    Copy link
    Mannequin

    vpython mannequin commented Apr 15, 2020

    Here's what I have.

    Maybe it would be better if parse and dump were under or dunder names, although I think parse was in the original implementation I found.

    Is this the derived from the same original as PyPI dotable? Not sure.

    @vedgar
    Copy link
    Mannequin

    vedgar mannequin commented Apr 15, 2020

    I think there is a teaching moment here. I think it's important that no object in Python standard library conflates "item namespace" with "attr namespace". (Maybe that isn't true because of some specialized objects, but surely general ones such as SimpleNamespace shouldn't commit that mistake.)

    On the other hand, we do have json in standard library, and JSON is primarily based on JavaScript Object Notation (yes, I know it's been extended to fit more languages, but still, its primal DNA shows). And the conflating of attributes and items is so natural in JS (a['b']===a.b) that it took them more than a decate to realize that separating Map from Object might be a good idea. :-)

    So, maybe we should also have something like JSNamespace in stdlib (presumably in json module). But SimpleNamespace shouldn't be it. As Guido once said, a bucket has a handle, and it contains sand, but it doesn't contain a handle.

    @vstinner
    Copy link
    Member

    Raymond started a thread on python-dev:
    https://mail.python.org/archives/list/[email protected]/message/JOMND56PJGRN7FQQLLCWONE5Z7R2EKXW/

    It seems like most core developers are against modifying types.SimpleNamespace, the trend is more about adding a different class.

    About a different class, so far, I see no consensus on an API. I suggest to close this issue (which is about types.SimpleNamespace) and continue the discussion on python-dev.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @SimonBiggs
    Copy link

    Only the magic methods need to be added: getitem, setitem, and delitem, contains, len, and iter.

    Out of interest @rhettinger, what was your implementation for the above? I would absolutely love to be able to use the following:

    In particular, it would be wonderful to be able to use object_hook=SimpleNamespace in json.load():

    Cheers,
    Simon

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants