annotate tests/suite_12_methods/files/WEB-INF/lib/jsdict.py @ 72:e8b3b336e63e draft default tip

Update version.
author David Barts <n5jrn@me.com>
date Mon, 15 Jul 2019 13:17:48 -0700
parents 88adf10be709
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
71
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
1 #!/usr/bin/env python3
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
3
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
4 class JSDict(dict):
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
5 """
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
6 Make a dict that acts something like a JavaScript object, in that we can
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
7 use both x["name"] and x.name to access something. Note that the latter
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
8 method fails for keys like x["get"] that duplicate dict methods, and keys
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
9 like x["1"] which are not legal Python identifiers.
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
10 """
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
11 def __getattr__(self, name):
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
12 return self[name]
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
13
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
14 def __setattr__(self, name, value):
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
15 self[name] = value
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
16
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
17 def __delattr__(self, name):
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
18 del self[name]
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
19
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
20 @classmethod
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
21 def from_dict(cls, d):
88adf10be709 Add tests.
David Barts <n5jrn@me.com>
parents:
diff changeset
22 return cls(d)