-
Notifications
You must be signed in to change notification settings - Fork 1k
Use enums for constants #1743
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
Use enums for constants #1743
Changes from 3 commits
28de82e
52117d3
7283f3c
8e30301
6958071
391bcc5
3fcdc81
44cf07e
d1889b1
be28670
9e9e953
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 |
|---|---|---|
|
|
@@ -3,11 +3,12 @@ | |
| This is the single location for storing default | ||
| values for the servers and clients. | ||
| """ | ||
| import enum | ||
|
|
||
| INTERNAL_ERROR = "Pymodbus internal error" | ||
|
|
||
|
|
||
| class ModbusStatus: # pylint: disable=too-few-public-methods | ||
| class ModbusStatus(int, enum.Enum): | ||
| """These represent various status codes in the modbus protocol. | ||
|
|
||
| .. attribute:: Waiting | ||
|
|
@@ -44,12 +45,8 @@ class ModbusStatus: # pylint: disable=too-few-public-methods | |
| SlaveOn = 0xFF | ||
| SlaveOff = 0x00 | ||
|
|
||
| def __init__(self): | ||
| """Prohibit objects.""" | ||
| raise RuntimeError(INTERNAL_ERROR) | ||
|
|
||
|
|
||
| class Endian: # pylint: disable=too-few-public-methods | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be replaced by the system constant.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure I understood, do you mean |
||
| class Endian(str, enum.Enum): | ||
| """An enumeration representing the various byte endianness. | ||
|
|
||
| .. attribute:: Auto | ||
|
|
@@ -73,12 +70,8 @@ class Endian: # pylint: disable=too-few-public-methods | |
| Big = ">" | ||
| Little = "<" | ||
|
|
||
| def __init__(self): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not permit to instantiate any constant class. |
||
| """Prohibit objects.""" | ||
| raise RuntimeError(INTERNAL_ERROR) | ||
|
|
||
|
|
||
| class ModbusPlusOperation: # pylint: disable=too-few-public-methods | ||
| class ModbusPlusOperation(int, enum.Enum): | ||
| """Represents the type of modbus plus request. | ||
|
|
||
| .. attribute:: GetStatistics | ||
|
|
@@ -95,12 +88,8 @@ class ModbusPlusOperation: # pylint: disable=too-few-public-methods | |
| GetStatistics = 0x0003 | ||
| ClearStatistics = 0x0004 | ||
|
|
||
| def __init__(self): | ||
| """Prohibit objects.""" | ||
| raise RuntimeError(INTERNAL_ERROR) | ||
|
|
||
|
|
||
| class DeviceInformation: # pylint: disable=too-few-public-methods | ||
| class DeviceInformation(int, enum.Enum): | ||
| """Represents what type of device information to read. | ||
|
|
||
| .. attribute:: Basic | ||
|
|
@@ -132,12 +121,8 @@ class DeviceInformation: # pylint: disable=too-few-public-methods | |
| Extended = 0x03 | ||
| Specific = 0x04 | ||
|
|
||
| def __init__(self): | ||
| """Prohibit objects.""" | ||
| raise RuntimeError(INTERNAL_ERROR) | ||
|
|
||
|
|
||
| class MoreData: # pylint: disable=too-few-public-methods | ||
| class MoreData(int, enum.Enum): | ||
| """Represents the more follows condition. | ||
|
|
||
| .. attribute:: Nothing | ||
|
|
@@ -151,7 +136,3 @@ class MoreData: # pylint: disable=too-few-public-methods | |
|
|
||
| Nothing = 0x00 | ||
| KeepReading = 0xFF | ||
|
|
||
| def __init__(self): | ||
| """Prohibit objects.""" | ||
| raise RuntimeError(INTERNAL_ERROR) | ||
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.
Why should we permit objects ? I do not see the need in contrary.
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.
Enum members are singletons by default, we don't need any special logic to guard them, Python handles that for us.
Uh oh!
There was an error while loading. Please reload this page.
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.
It does not prohibit that I can assign the class to a variable and make changes, with unexpected results.
And as far as I know, if you instanciate the class you get a new set of class variables (elements of enum).
Uh oh!
There was an error while loading. Please reload this page.
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.
Actually, I believe no, class variables are static. You can change them, but restricting
__init__won't prevent that anyway.I think I should clarify,
__init__is used to initialise enum members, not the enum itself.Enums are using metaclasses underneath. Say, if somebody does
ModbusStatus(0xFFFF), they will getModbusStatus.WAITING, because enum members are singletons.