Skip to content

Commit

Permalink
Plain dicts get converted to AttrDicts when placed in the field attri…
Browse files Browse the repository at this point in the history
…bute of a SkidlBaseObject.
  • Loading branch information
xesscorp committed Apr 26, 2020
1 parent 632763d commit 697aa5f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
6 changes: 2 additions & 4 deletions skidl/AttrDict.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class AttrDict(dict):
attr_obj: The object whose attributes will mirror the entries in
the dictionary. If not specified, then the attributes of the
dict will be used.
**kwargs: Key/value pairs that will be entered into the dict.
"""

def __init__(self, *args, **kwargs):
Expand All @@ -65,11 +66,8 @@ def _setattr_setitem(self, key, value):
__setattr__ = _setattr_setitem
__setitem__ = _setattr_setitem

def __repr__(self):
return "AttrDict({})".format(dict.__repr__(self))

def copy(self, attr_obj):
"""Copy and set the object whose attributes will mirror the copy dict entires."""
"""Copy and set the object whose attributes will mirror the copy dict entries."""
cpy = AttrDict(attr_obj=attr_obj)
for k, v in self.items():
cpy[k] = copy(v)
Expand Down
1 change: 1 addition & 0 deletions skidl/Part.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

from future import standard_library

from .AttrDict import AttrDict
from .baseobj import SkidlBaseObject
from .defines import *
from .erc import dflt_part_erc
Expand Down
4 changes: 1 addition & 3 deletions skidl/SchLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,7 @@ def prettify(s):
if not file_:
file_ = libname + skidl.lib_suffixes[tool]

export_str = (
"from skidl import Pin, Part, AttrDict, Alias, SchLib, SKIDL, TEMPLATE\n\n"
)
export_str = "from skidl import Pin, Part, Alias, SchLib, SKIDL, TEMPLATE\n\n"
export_str += "SKIDL_lib_version = '0.0.1'\n\n"
part_export_str = ",".join([p.export() for p in self.parts])
export_str += "{} = SchLib(tool=SKIDL).add_parts(*[{}])".format(
Expand Down
13 changes: 9 additions & 4 deletions skidl/baseobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,16 @@ def __init__(self):
self.fields = AttrDict(attr_obj=self)

def __setattr__(self, key, value):
super().__setattr__(key, value)
if key == "fields":
# Whatever is assigned to the fields attribute is cast to an AttrDict.
super().__setattr__(key, AttrDict(attr_obj=self, **value))

# Whenever an attribute is changed, then also sync it with the fields dict
# in case it is mirroring one of the dict entries.
self.fields.sync(key)
else:
super().__setattr__(key, value)

# Whenever an attribute is changed, then also sync it with the fields dict
# in case it is mirroring one of the dict entries.
self.fields.sync(key)

@property
def aliases(self):
Expand Down

0 comments on commit 697aa5f

Please sign in to comment.