The same structure as Recordtype in Python 3.9

46 views Asked by At

I have the following recordtype

from recordtype import recordtype

sending_interval = datetime.timedelta(days = 1)

last_sent_time = datetime.datetime.now() - sending_interval

thermometer = recordtype('thermometer', 'sent, last_sent_time, name, group, where')

thermometers =  { 
                 "29FF069605000009": thermometer(False, last_sent_time, "therm1", "White_warehouse", "on therm 1"),
                 "294FFF95050000D3": thermometer(False, last_sent_time, "therm2", "White_warehouse", "on therm 2"),
                 "29670D9605000069": thermometer(False, last_sent_time, "therm3", "White_warehouse", "on therm 3"),
                 "29FBCA950500005E": thermometer(False, last_sent_time, "therm4", "White_warehouse", "on therm 4"),
                 "29FED096050000FC": thermometer(False, last_sent_time, "therm5", "White_warehouse", "on therm 5"),
                 "2966C1680B0000CD": thermometer(False, last_sent_time, "therm6", "White_warehouse", "on therm 6" ),
                 "2932D49505000089": thermometer(False, last_sent_time, "therm7", "White_warehouse", "on therm 7"),
                 "2984C29605000054": thermometer(False, last_sent_time, "therm8", "Group2",  "on therm 8"),
                 "292C5A690B00003F": thermometer(False, last_sent_time, "therm9", "Group3",  "on therm 9"),
                 "296D00960500004A": thermometer(False, last_sent_time, "therm10", "Small_warehouse","on therm 10"),
                 "29B7D49605000000": thermometer(False, last_sent_time, "therm11", "Small_warehouse","on therm 11"),
                 "White_warehouse":  thermometer(False, last_sent_time, "White_warehouse_average", "", "in white warehouse"),
                 "Small_warehouse":  thermometer(False, last_sent_time, "Small_warehouse_average", "", "in small warehouse")
                }

I use the recordtype as follows in source code:

if not mac_address in thermometers:

thermometers[mac_address].where

thermometers[mac_address].name

if thermometers[mac_address].group

for group_name, avg_t  in avg_temperatures.items():
 ...
   print ("Average temperature {} is {}. ".format(thermometers[group_name].where, str(avg_t)))
 ...
   thermometers[group_name].last_sent_time = datetime.datetime.now()
 ...
   thermometers[group_name].sent = False
 ...
   if ((not thermometers[group_name].sent):
 ...
   thermometers[group_name].last_sent_time > sending_interval)
 ...
   save_to_database(group_name, thermometers[group_name].name, avg_t, tm)

It works fine with Python 3.9, but when I run it with Python 3.10 I have the following error message.

File "C:\Python310\lib\site-packages\recordtype.py", line 35, in <module> from collections import Mapping as _Mapping ImportError: cannot import name 'Mapping' from 'collections' (C:\Python310\lib\collections\__init__.py)

Does it mean that Python 3.10 does not support recordtype? How would you rewrite my recordtype structure that does the same functionality?

1

There are 1 answers

2
Tasos Papastylianou On BEST ANSWER

Any reason you don't like a simple class for this?

 class thermometer:
     def __init__( sent, last_sent_time, name, group, where ):
         self.sent           = sent
         self.last_sent_time = last_sent_time
         self.name           = name
         self.group          = group
         self.where          = where