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

add getters to m.submodules #156

Merged
merged 3 commits into from
Jul 19, 2019

Conversation

nakengelhardt
Copy link
Contributor

This allows retrieving a named submodule after assigning it:

>>> m1 = Module()
>>> m1.submodules.m2 = Module()
>>> m1.submodules.m2
<nmigen.hdl.dsl.Module object at 0x7f2acc2cf240>

It also disallows setting the same submodule name twice:

>>> m1 = Module()
>>> m1.submodules.m2 = Module()
>>> m1.submodules.m2 = Module()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/nengel/nmigen/nmigen/hdl/dsl.py", line 85, in __setattr__
    self._builder._add_submodule(submodule, name)
  File "/home/nengel/nmigen/nmigen/hdl/dsl.py", line 432, in _add_submodule
    raise ValueError("Submodule named '{}' already exists".format(name))
ValueError: Submodule named 'm2' already exists

Item notation is also supported:

>>> m1 = Module()
>>> m1.submodules['m2'] = Module()
>>> m1.submodules['m2']
<nmigen.hdl.dsl.Module object at 0x7f52834464a8>
>>> m1.submodules.m2
<nmigen.hdl.dsl.Module object at 0x7f52834464a8>

@@ -124,7 +130,8 @@ def __init__(self):
self._ctrl_stack = []

self._driving = SignalDict()
self._submodules = []
self._submodules = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: should this be something like self._named_submodules?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, let me change that

@codecov
Copy link

codecov bot commented Jul 19, 2019

Codecov Report

Merging #156 into master will increase coverage by 0.05%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #156      +/-   ##
==========================================
+ Coverage   80.67%   80.73%   +0.05%     
==========================================
  Files          32       32              
  Lines        5217     5232      +15     
  Branches     1129     1133       +4     
==========================================
+ Hits         4209     4224      +15     
  Misses        873      873              
  Partials      135      135
Impacted Files Coverage Δ
nmigen/hdl/dsl.py 99.68% <100%> (+0.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 81e5983...ccd8b20. Read the comment docs.

self._anon_submodules.append(submodule)
else:
if name in self._named_submodules:
raise ValueError("Submodule named '{}' already exists".format(name))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I wasn't sure if this should be a ValueError or an AttributeError or maybe a SyntaxError?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NameError?

@whitequark whitequark merged commit 698b005 into m-labs:master Jul 19, 2019
@nakengelhardt nakengelhardt deleted the add-submodules-get branch July 20, 2019 04:16
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

Successfully merging this pull request may close these issues.

None yet

2 participants