captain-coredump / weatherflow-udp

WeatherFlow Personal Weather Station driver for weewx, via UDP broadcast packets
GNU General Public License v3.0
53 stars 18 forks source link

python3 compatibility #7

Open vinceskahan opened 5 years ago

vinceskahan commented 5 years ago

Tom's got an alpha of weewx 4.0 running, which will be the one that moves to python3. Everybody with drivers and extensions needs to check their stuff for compatibility and make the generally minor edits needed. I ran your driver through 2to3 and it came up with some minor patches needed (untested).

My 'recollection' is that this should work with both versions of python as a result (untested).

--- weatherflowudp.py   (original)
+++ weatherflowudp.py   (refactored)
@@ -138,7 +138,7 @@

 """

-from __future__ import with_statement
+
 import math
 import time
 import weewx.units
@@ -192,7 +192,7 @@
             # weewx.METRICWX = mm/mps ; weewx.METRIC = cm/kph
             'usUnits' : weewx.METRICWX}

-    for pkt_weewx, pkt_label in sensor_map.iteritems():
+    for pkt_weewx, pkt_label in sensor_map.items():
         if pkt_label.replace("-","_") in pkt:
            packet[pkt_weewx] = pkt[pkt_label.replace("-","_")]

@@ -205,7 +205,7 @@
             serial_number = pkt['serial_number'].replace("-","_")
             pkt_type = pkt['type']
             pkt_label = serial_number + "." + pkt_type
-            pkt_keys = pkt.keys()
+            pkt_keys = list(pkt.keys())
             for i in pkt_keys:
                 pkt_item = i + "." + pkt_label
                 packet[pkt_item] = pkt[i]
@@ -268,7 +268,7 @@
         self._sensor_map = stn_dict.get('sensor_map', {})
         loginf('sensor map is %s' % self._sensor_map)
         loginf('*** Sensor names per packet type')
-        for pkt_type in fields.keys():
+        for pkt_type in list(fields.keys()):
             loginf('packet %s: %s' % (pkt_type,fields[pkt_type]))

     def hardware_name(self):
captain-coredump commented 5 years ago

Thanks, Vince!

Back in January, I was asked to develop a WeeWX UDP driver for an upcoming PWS product named Atmocom. I started by forking this WeatherFlow UDP driver, and the finished product wound up having many of the to-do list features from here like a complete wee_config console dialog for building a sensor_map table. Long story short, I am planning for the next version of this driver to be v2.0, and will be back-porting all of the improvements and added features from the Atmocom driver back into the WeatherFlow driver. I will also add the above revisions, and run both this and the Atmocom driver through the code validator to ensure Python3/WeeWX4 compatibility.

If Tom is already up to an alpha of WeeWX 4.0 and it needs Python3, I guess that it's time for me to get to work on building a Python3 test environment and see how the WeeWX "python3" branch works.....

vinceskahan commented 5 years ago

Careful with python versions, my udp listener doesn't "hear" anything with Raspbian's 3.5.3 version of python. Ubuntu's 3.6.7 worked fine there. A self-compiled 3.7.0 on Raspbian also worked.

A nice one-liner to compile and altinstall python3.7.0 is HERE - I just cut+pasted the command up to and including the 'make altinstall' command in the middle. Then call python3.7 explicitly if /usr/local/bin is in your $PATH.

Re: weewx, this'll get you a Simulator running....

#---------------------------------------

# prerequisites
apt-get install -y python3-dev python3-pip python3-configobj python3-serial python3-pil python3-usb
pip3 install pyephem
pip3 install cheetah3

# webserver
apt-get install -y nginx
systemctl enable nginx.service
systemctl start nginx.service

# download weewx
cd /var/tmp
apt-get install -y git
git clone https://github.com/weewx/weewx.git
cd weewx
git checkout python3

# install weewx
python3 setup.py build
python3 setup.py install --no-prompt
sed -i 's:debug = 0:debug = 1:' /home/weewx/weewx.conf

# want webserver docroot to point to weewx files
ln -s /var/www/html /home/weewx/public_html

# hook into startup sequence
cp util/systemd/weewx.service /lib/systemd/system
systemctl enable weewx.service

# start it up
systemctl start weewx.service
systemctl status weewx.service