Comments by "Dr Gamma D" (@DrDeuteron) on "Tech With Tim"
channel.
-
13
-
10
-
7
-
7
-
for me, when I'm reading 545 data fields of space craft telemetry, which is a helluva dunder init. Instead I just cut and paste the attributes from an excel file into a data class and it calls for a single mouse click. Moreover, dataclass comes with "fields", which can be used to describe the fields, the LaTex formulas for them. expected ranges, alarm ranges, broke ranges, and there is an astuple functions, which can be stuck in dunder iter, possible zip with the fields, and in about 3 lines of executable code, you can flag out-of-range telemetry. Very little dynamic code, and lots of static could is much simpler and less bug prone.
3
-
3
-
3
-
3
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
@nirajraut9408 so it calls new and returns a class, otherwise it calls object.__new__() and then tries to instantiate with __init__. His example is bit too simple, in that it just wraps type to show functionality (which needs to be done), but in a real life use case, you may want to choose a class based on the inputs...and only type can do that.
For instance, I have spacecraft telemetry in classes: Current, Voltage, Temperature, and I can tell which one I want by the filename. So I'll do something like:
class Telemetry(type):
klasses = dict(current=Current, voltage=Voltage, temperature=Temperature)
def __new__(cls, name):
return cls.klasses[name].fromfile(name)
Here fromfile are a class method (constructor helpers) that reads the file (in a context manager), and instantiates the object with values.
So in use, I just say:
>>>telemetry = Telemetry(filename)
and get the data unpacked according to protocol w/o having to worry about picking the right object to put it in.
Then I just just say:
>>>telemetry.plot()
and a house keeping plot is generated, without the code explicitly worrying about what kind of telemetry I need to look at.
Note that the whole thing is accomplished with ZERO if/then clauses, reducing cyclomatic complexity.
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1