-
Notifications
You must be signed in to change notification settings - Fork 16
[Feature] Add DittoPersonalizedMixin
#385
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
Changes from 21 commits
0765492
c811fbc
2d22847
9e82518
0894136
bfe1dda
1f00d95
789cb1b
5e64570
26553fa
066ae4a
f367007
097d82f
19b9557
7975cc5
30a895c
85ee409
6c02563
68c9da7
2b578f4
6556076
acb279c
7df26eb
e3a86d6
b49e4e0
bb63f1a
2032e14
71c41bf
cefdd82
8a6ac3e
496d14b
eeb9096
58c9244
e124499
137c9c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| from enum import Enum | ||
|
|
||
| from fl4health.clients.basic_client import BasicClient | ||
| from fl4health.mixins.personalized.ditto import DittoPersonalizedMixin, DittoPersonalizedProtocol | ||
|
|
||
|
|
||
| class PersonalizedModes(str, Enum): | ||
| DITTO = "ditto" | ||
|
|
||
|
|
||
| PersonalizedMixinRegistry = {"ditto": DittoPersonalizedMixin} | ||
|
||
|
|
||
|
|
||
| def make_it_personal(client_base_type: type[BasicClient], mode: PersonalizedModes | str) -> type[BasicClient]: | ||
| """A mixed class factory for converting basic clients to personalized versions.""" | ||
| if mode == "ditto": | ||
|
||
|
|
||
| return type( | ||
| f"Ditto{client_base_type.__name__}", | ||
| ( | ||
| PersonalizedMixinRegistry[mode], | ||
| client_base_type, | ||
| ), | ||
| { | ||
| # Special flag to bypass validation | ||
| "_dynamically_created": True | ||
| }, | ||
| ) | ||
| else: | ||
| raise ValueError("Unrecognized personalized mode.") | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "DittoPersonalizedMixin", | ||
| "DittoPersonalizedProtocol", | ||
| "PersonalizedModes", | ||
| "PersonalizedMixinRegistry", | ||
| "make_it_personal", | ||
| ] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it normal to put such things in the
__init__.py? I'm not sure of the co, but this feels kind of weird.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is, why put this in here instead of something like a utility file or something else?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I have seen this adopted in practice, by engineers more experienced than me; and in other libraries. That is, I am treating this as a factory/registry, which I have seen be put in these module-level init files. I think so long as we're not baking in the actual logic here, then we're okay... but i'm also not married to this. If you really prefer to outsource this to some other module, say
factoryorregistry, then I'm not picky. :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main reason I think that these factories/registries are created here is because often all of the classes you need are already being re-imported (i.e., and being exposed in the public api "all")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fair. I honestly have no idea what best practices apply to
__init__.pyfiles. Seems kind of wild west ha.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea me too... I just go with things I have seen before in libraries i've worked in, or have read source code in, and how past teammates have used
__init__.py.