Move constants to separate file#86
Conversation
|
The reason I split the constants across modules is because most of them are used only in the modules they are contained. For example the constants in the char module which relate to the format, etc. are used exclusively there. What is the reason to move such constants to a common file? |
|
A few reasons:
IMO especially the second to last argument is important. Although additional constants could have been added in the individual modules as well, they are more clearly structured and readable in a separate one. |
|
I agree with the classes point, they are more readable but terrible otherwise. But this aside - why pollute one namespace with stuff that is used only in another (like the char format constants). Furthermore, about point 2 - if I need to use the char format constants and I don’t know where they are, my first thought would be to look in the chars module for them. IMO, I would expect them to be there because it’s their logical place. Point 3 - fewer lines is not always easier. For example I need to open another file to see the definition of a constant. And that other file is longer and contains a lot of different sets of constants, which makes it hard to follow. Point 4 - spelling mistakes are equally likely in both cases I think. But if the constants are in the same module in which they are used the most, they will be more convenient for reading (again IMO) Lastly, point 5 - other implementations often need a specific subset of functionalities at any given moment. If a third party module wants to do server stuff it needs to understand only the server module. Why then import from a file containing all constants which it does not care about? Also, whether you do |
|
I can probably counter those arguments (and will later on), but what it comes done to is a philosophical question on wheather or not a seperate constants file is needed for a project this size. As you know, I come from Home Assistant and that's the way we do things there and I personally see certain benefits in it which is why I suggested the changes in the first place. Regarding the namespace: With using Point 2: That might be true if constants are used in one module only. However this is not always the case although the grade varies. Point 3: The idea behind constants is that the name should tell you everything you need to know about them. E.g. you shouldn't care if it's Point 4: Spelling in this case is meant for writing Point 5: Your're right here. However to some degree this is managed be giving fitting names like |
|
Point 2: this is exactly the case with the server and char format constants. In fact, the only constants used outside the module they are defined in are the AID and Category (I am not considering the newly added constants for REPR, these clearly need to go in a consts file). Point 3: I don’t see how this is a plus for either side of the argument, in both cases the constant is just a name you use, regardless of where it is defined. Point 4: Looking at this and point 3, I get the impression that you think I am against constants and I am not. My only concern is in combining all constants in a single file, although the majority of them are not used outside a specific module. Obviously there are constants that are used across modules, I don’t oppose placing such in a single file. Let me also add that different projects have different style guides. The same way that you see benefits in the single module approach, I see benefits in keeping constants close to what they refer to. Vice versa, and I think you can agree, both have drawbacks. |
|
To find some common ground here, what do you think of the following:
What's your opinion on the Update: |
|
I am fine with this. As for the CATEGORY, I would prefer it stays in Accessory but I don't mind moving it to consts |
|
I moved CATEGORY back to the |
pyhap/accessory.py
Outdated
| @@ -18,8 +21,8 @@ | |||
| class Category: | |||
There was a problem hiding this comment.
I thought you wanted to change this to dict?
There was a problem hiding this comment.
I would have loved to. However I don't think that it quite works here. At the moment this version seems to be the best.
For the future we could think about what is stored in the accessory. As far as I can tell, it's the category number at the moment. With a change to a dictionary it might make sense to store the constant name and assign the number later when sending the acc infos to HomeKit.
I didn't wanted to have this discussion here which is the reason I changed it back, so this could get merged.
There was a problem hiding this comment.
What is the purpose of having it like e.g. string in the Acc? Readability?
There was a problem hiding this comment.
Using a dictionary might pose problems when importing a category.
...
While I was thinking about it, are their problems with just doing the following?
CAT_OTHER = 1
CAT_BRIDGE = 2
# ...That would solve the import problem and doesn't need a dict.
For comparison the dict variant I thought of was:
CAT_OTHER = 'other'
CAT_BRIDGE = 'bridge'
CATEGORIES = {
CAT_OTHER: 1,
CAT_BRIDGE: 2,
}There was a problem hiding this comment.
No problems, I would just name it CATEGORY_OTHER, etc.
There was a problem hiding this comment.
Would you mind if I move them to the const file?
Codecov Report
@@ Coverage Diff @@
## dev #86 +/- ##
==========================================
- Coverage 36.78% 29.93% -6.86%
==========================================
Files 12 13 +1
Lines 1283 1306 +23
Branches 130 128 -2
==========================================
- Hits 472 391 -81
- Misses 807 915 +108
+ Partials 4 0 -4
|
Storing constants in one or multiple classes is inefficient in python. A better way is to create a separate file and import the necessary constants.
Furthermore I added additional constants for reoccurring variables, mainly for the
HAP representationandproperties.I've left out the
hap_servermodule, since it quite possibly will change with the conversion to async, so constants for it can be added later.As a side notes:
execute as programupdate_advertismentis now update_advertisementI will rebase this PR as soon as #85 is merged. Otherwise there might be merge conflicts.