python 3 compatibility
Stefan Schuermans

Stefan Schuermans commited on 2017-05-27 13:05:50
Showing 5 changed files, with 25 additions and 23 deletions.

... ...
@@ -1,6 +1,6 @@
1
-from display import Display
2
-from distributor import Distributor
3
-from mapping import Mapping
4
-from msg import Msg, MsgDef
5
-from parse import parse_addr, parse_no, parse_two_nos
1
+from pyetherpix.display import Display
2
+from pyetherpix.distributor import Distributor
3
+from pyetherpix.mapping import Mapping
4
+from pyetherpix.msg import Msg, MsgDef
5
+from pyetherpix.parse import parse_addr, parse_no, parse_two_nos
6 6
 
... ...
@@ -17,10 +17,10 @@
17 17
 
18 18
 import socket
19 19
 
20
-from distributor import Distributor
21
-from mapping import Mapping
22
-from msg import Msg, MsgDef
23
-from parse import parse_addr, parse_no, parse_two_nos
20
+from pyetherpix.distributor import Distributor
21
+from pyetherpix.mapping import Mapping
22
+from pyetherpix.msg import Msg, MsgDef
23
+from pyetherpix.parse import parse_addr, parse_no, parse_two_nos
24 24
 
25 25
 
26 26
 class Display(object):
... ...
@@ -20,7 +20,7 @@ import struct
20 20
 
21 21
 from PIL import Image
22 22
 
23
-from mapping import Mapping
23
+from pyetherpix.mapping import Mapping
24 24
 
25 25
 
26 26
 class Distributor(object):
... ...
@@ -66,8 +66,9 @@ class Distributor(object):
66 66
     def data_clear(self):
67 67
         """clear image data, i.e. set entire image to black"""
68 68
         # prepare message buffer with all pixels cleared (black)
69
-        clr = "".join([self._mappings[channel].table[0]
70
-                       for channel in range(Mapping.CHANNELS)])
69
+        clr = bytearray()
70
+        for channel in range(Mapping.CHANNELS):
71
+            clr += self._mappings[channel].table[0]
71 72
         self._buffer = self._udp_hdr + (clr * (self._outputs * self._pixels))
72 73
 
73 74
     def data_image(self, image):
... ...
@@ -77,7 +78,7 @@ class Distributor(object):
77 78
         (width, height) = image.size
78 79
         # collect pixels from image and assemble message in buffer
79 80
         clr = [0] * Mapping.CHANNELS
80
-        data = []
81
+        data = self._udp_hdr
81 82
         for output_pixel_coords in self._pixel_coords:
82 83
             for x_y in output_pixel_coords:
83 84
                 if x_y is None:
... ...
@@ -90,10 +91,10 @@ class Distributor(object):
90 91
                         # get pixel from image
91 92
                         pix = image.getpixel(x_y)
92 93
                 # add pixel to pixel data
93
-                data += [self._mappings[channel].table[pix[channel]]
94
-                                        for channel in range(Mapping.CHANNELS)]
95
-        # build UDP message from pixel data
96
-        self._buffer = self._udp_hdr + "".join(data)
94
+                for channel in range(Mapping.CHANNELS):
95
+                    data += self._mappings[channel].table[pix[channel]]
96
+        # store UDP message
97
+        self._buffer = data
97 98
 
98 99
     def send(self, socket):
99 100
         """send image data to actual distributor module using UDP"""
... ...
@@ -33,21 +33,21 @@ def main(argv):
33 33
     # create display
34 34
     display = pyetherpix.Display(config_file)
35 35
     (width, height) = display.get_size()
36
-    print "width %u, height %u" % (width, height)
36
+    print("width %u, height %u" % (width, height))
37 37
     # prepare "on" image (all white)
38 38
     on = Image.new("RGB", (width, height), "white")
39 39
     # blink
40
-    print "blink"
40
+    print("blink")
41 41
     for i in range(5):
42
-        print "on"
42
+        print("on")
43 43
         display.data_image(on)
44 44
         display.send()
45 45
         time.sleep(0.5)
46
-        print "off"
46
+        print("off")
47 47
         display.data_clear()
48 48
         display.send()
49 49
         time.sleep(0.5)
50
-    print "done"
50
+    print("done")
51 51
     # close display
52 52
     display.close()
53 53
     return 0
... ...
@@ -15,6 +15,7 @@
15 15
 # You should have received a copy of the GNU Lesser General Public License
16 16
 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
 
18
+from __future__ import print_function
18 19
 import sys
19 20
 
20 21
 
... ...
@@ -55,5 +56,5 @@ class MsgDef(Msg):
55 56
         else:
56 57
           prefix = "unknown"
57 58
         if level <= self._level:
58
-          print >>sys.stderr, prefix + ": " + text
59
+          print(prefix + ": " + text, file=sys.stderr)
59 60
 
60 61