radiocosmology / padloper

MIT License
2 stars 0 forks source link

Make Component Types callable #87

Open jeffiuliano opened 1 year ago

jeffiuliano commented 1 year ago

One typo fix, two suggestions.

  1. added subs property of a component so you can, instead of eg switch1.get_all_subcomponents() write
In [5]: switch1.subs
Out[5]: 
[component ethernet-port: switch-wiz6-snABC_p04 (4192),
 component ethernet-port: switch-wiz6-snABC_p00 (8360),
 component ethernet-port: switch-wiz6-snABC_p02 (16552),
 component ethernet-port: switch-wiz6-snABC_p01 (40972360),
 component ethernet-port: switch-wiz6-snABC_p05 (40980488),
 component ethernet-port: switch-wiz6-snABC_p03 (81936464)]
  1. Made ComponentType instances callable (by adding __call__ fn to the class definition). This allows:
In [2]: t_switch           = ComponentType("switch", "a network switch").add()
ComponentType with name switch already exists in the database.

In [4]: switch1  = t_switch("switch-wiz-snABC").add()
Component with name switch-wiz-snABC already exists in the database.

instead of

In [3]: switch1  = Component("switch-wiz-snABC", t_switch).add()
Component with name switch-wiz-snABC already exists in the database.
ahincks commented 1 year ago

I'm not sure I'm sold on making the class callable. For ComponentType it currently seem benign, but presumably we'd want to make other classes callable too, and for some other classes it would be problematic. E.g., if Component were callable, it would probably break things if you started doing operations on the object before it had actually been added to the DB.

I'm not necessarily opposed to the subs property, but is it mainly to make the call less verbose? I'd certainly be in favour of shortening some of the method names which are too long. I.e., would you be OK with shortening the method to subcomponents() or subcomps() or similar? Or is there a need to additionally have a property?

ahincks commented 1 year ago

(Also, sorry for taking so long to finally comment …)