Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > de.comp.lang.python > #5731
| From | Marc 'BlackJack' Rintsch <marc@rintsch.de> |
|---|---|
| Newsgroups | de.comp.lang.python |
| Subject | Re: Konstanten in Struct-Form in Python |
| Date | 2021-04-28 10:04 +0000 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <s6bbvq$12o0$1@gioia.aioe.org> (permalink) |
| References | <i9a065FttstU1@mid.individual.net> <i9gvkcFalukU1@mid.individual.net> |
On Mon, 22 Feb 2021 07:00:47 +0100, Michael S. wrote:
> Ich habs jetzt so gemacht. Da muss ich in der Anwendung nur die
> CAN-Signals_Klasse instanzieren, dafür halt in der Lib jeweils jedes
> eigene Signal. Aber die Lib wird eh automatisch generiert.
>
> class C_frequency:
> def __init__(self):
> self.ID = "10FC80FC"
> self.BitOffset = 48 self.BitLength = 8 self.Gain = 1
self.ValueOffset
> = 0 self.Unit = "Hz"
> self.Rate = 0.5
>
> class C_OutputVoltage:
> def __init__(self):
> self.ID = "25FF1250"
> self.BitOffset = 32 self.BitLength = 16 self.Gain = 0.1
> self.ValueOffset = 0 self.Unit = "V"
> self.Rate = 0.25
>
> class C_Cable_temp:
> def __init__(self):
> self.ID = "12FA5AFC"
> self.BitOffset = 0 self.BitLength = 8 self.Gain = 1
self.ValueOffset =
> -40 self.Unit = "°C"
> self.Rate = 10
>
> class CAN_Signals:
> def __init__(self):
> self.frequency = C_frequency() self.OutputVoltage =
C_OutputVoltage()
> self.Cable_temp = C_Cable_temp()
Verschiedene Klassen mit genau den gleichen Attributen sieht ”falsch” aus. Da
würde man eher *eine* Klasse schreiben, die die Attribute hat, und davon dann
mehrere Exemplare erstellen.
Mit `collections.namedtuple()` und Namen die sich an PEP8 halten, könnte das
dann so aussehen:
from collections import namedtuple
Signal = namedtuple(
"Signal", "id bit_offset bit_length gain value_offset unit rate"
)
FREQUENCY = Signal("10FC80FC", 48, 8, 1, 0, "Hz", 0.5)
OUTPUT_VOLTAGE = Signal("25FF1250", 32, 16, 0.1, 0, "V", 0.25)
CABLE_TEMPERATURE = Signal("12FA5AFC", 0, 8, 1, -40, "°C", 10)
Ciao,
Marc 'BlackJack' Rintsch
--
“I don't care WHO you
are but your not walking
on the water while I'm fishing”
Back to de.comp.lang.python | Previous | Next — Previous in thread | Find similar
Konstanten in Struct-Form in Python "Michael S." <michaely@bigfoot.de> - 2021-02-19 15:27 +0100
Re: [Python-de] Konstanten in Struct-Form in Python Hartmut Goebel <h.goebel@crazy-compilers.com> - 2021-02-19 15:43 +0100
Re: [Python-de] Konstanten in Struct-Form in Python Peter Otten <__peter__@web.de> - 2021-02-20 09:37 +0100
Re: Konstanten in Struct-Form in Python "Michael S." <michaely@bigfoot.de> - 2021-02-22 07:00 +0100
Re: Konstanten in Struct-Form in Python Marc 'BlackJack' Rintsch <marc@rintsch.de> - 2021-04-28 10:04 +0000
csiph-web