view tests/suite_12_methods/files/WEB-INF/lib/jsdict.py @ 71:88adf10be709 draft

Add tests.
author David Barts <n5jrn@me.com>
date Mon, 15 Jul 2019 13:16:31 -0700
parents
children
line wrap: on
line source

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

class JSDict(dict):
    """
    Make a dict that acts something like a JavaScript object, in that we can
    use both x["name"] and x.name to access something. Note that the latter
    method fails for keys like x["get"] that duplicate dict methods, and keys
    like x["1"] which are not legal Python identifiers.
    """
    def __getattr__(self, name):
        return self[name]

    def __setattr__(self, name, value):
        self[name] = value

    def __delattr__(self, name):
        del self[name]

    @classmethod
    def from_dict(cls, d):
        return cls(d)