From 9cbe2346ec874e71ae0ac9de269af18eef7f5623 Mon Sep 17 00:00:00 2001
From: Christian Taedcke <hacking@taedcke.com>
Date: Fri, 5 Feb 2016 00:12:38 +0100
Subject: [PATCH 5/8] Convert some python examples to python 3.

---
 examples/python/adc.py         | 26 +++++++++----------
 examples/python/ex_pinout.py   | 14 +++++------
 examples/python/ex_utils.py    |  2 +-
 examples/python/example.py     |  8 +++---
 examples/python/example_io.py  | 12 ++++-----
 examples/python/example_pin.py | 46 +++++++++++++++++-----------------
 examples/python/multicore.py   | 22 ++++++++--------
 7 files changed, 65 insertions(+), 65 deletions(-)

diff --git a/examples/python/adc.py b/examples/python/adc.py
index 2719952..1cb0749 100644
--- a/examples/python/adc.py
+++ b/examples/python/adc.py
@@ -25,8 +25,8 @@ if __name__ == "__main__":
   sim.dmanSingleDeviceApplication()
   dev = sim.loadDevice(proc, elffile)
   #dev.SetClockFreq(250) # clock frequency is 4MHz by default
-  print "before simulation start:"
-  print "  value 'adc_value'=%d (before init)" % sim.getWordByName(dev, "adc_value")
+  print("before simulation start:")
+  print("  value 'adc_value'=%d (before init)" % sim.getWordByName(dev, "adc_value"))
   
   a0 = XPin(dev, "A0", 'a')
   aref = XPin(dev, "AREF", 'a')
@@ -36,28 +36,28 @@ if __name__ == "__main__":
   # hwad.cpp: adSample= (int)((float)adSample/(float)adref*INT_MAX);
   
   sim.dmanStart()
-  print "simulation start: (t=%dns)" % sim.getCurrentTime()
+  print("simulation start: (t=%dns)" % sim.getCurrentTime())
   a0.SetAnalog(123)
   
-  print "run till main function ..."
+  print("run till main function ...")
   bpaddr = dev.Flash.GetAddressAtSymbol("main")
   dev.BP.AddBreakpoint(bpaddr)
   sim.doRun(30000)
   if not dev.PC == bpaddr:
-      print "error: main function not arrived!"
+      print("error: main function not arrived!")
   dev.BP.RemoveBreakpoint(bpaddr)
-  print "simulation main entrance: (t=%dns)" % sim.getCurrentTime()
-  print "  value 'adc_value'=%d (after init)" % sim.getWordByName(dev, "adc_value")
+  print("simulation main entrance: (t=%dns)" % sim.getCurrentTime())
+  print("  value 'adc_value'=%d (after init)" % sim.getWordByName(dev, "adc_value"))
   
   sim.doRun(sim.getCurrentTime() + 120000)
-  print "simulation break: (t=%dns)" % sim.getCurrentTime()  
-  print "  value 'conversions'=%d" % sim.getWordByName(dev, "conversions")
-  print "  value 'adc_value'=%d (simulation break)" % sim.getWordByName(dev, "adc_value")
+  print("simulation break: (t=%dns)" % sim.getCurrentTime())  
+  print("  value 'conversions'=%d" % sim.getWordByName(dev, "conversions"))
+  print("  value 'adc_value'=%d (simulation break)" % sim.getWordByName(dev, "adc_value"))
   
   sim.doRun(sim.getCurrentTime() + 330000)
-  print "simulation end: (t=%dns)" % sim.getCurrentTime()  
-  print "  value 'conversions'=%d" % sim.getWordByName(dev, "conversions")
-  print "  value 'adc_value'=%d (simulation end)" % sim.getWordByName(dev, "adc_value")
+  print("simulation end: (t=%dns)" % sim.getCurrentTime())  
+  print("  value 'conversions'=%d" % sim.getWordByName(dev, "conversions"))
+  print("  value 'adc_value'=%d (simulation end)" % sim.getWordByName(dev, "adc_value"))
   
   sim.dmanStop()
   del dev
diff --git a/examples/python/ex_pinout.py b/examples/python/ex_pinout.py
index c3e25f0..2be2220 100644
--- a/examples/python/ex_pinout.py
+++ b/examples/python/ex_pinout.py
@@ -13,7 +13,7 @@ class XPin(pysimulavr.Pin):
     
   def SetInState(self, pin):
     pysimulavr.Pin.SetInState(self, pin)
-    print "%s set to '%s' (t=%dns)" % (self.name, pin.toChar(), sim.getCurrentTime())
+    print("%s set to '%s' (t=%dns)" % (self.name, pin.toChar(), sim.getCurrentTime()))
 
 if __name__ == "__main__":
 
@@ -27,11 +27,11 @@ if __name__ == "__main__":
   #dev.SetClockFreq(100)
   
   if doVCD:
-    print "all registrered trace values:\n ",
-    print "\n  ".join(sim.getAllRegisteredTraceValues())
+    print("all registrered trace values:\n ", end=' ')
+    print("\n  ".join(sim.getAllRegisteredTraceValues()))
     sigs = ("IRQ.VECTOR9", "PORTA.PORT")
     sim.setVCDDump(splitext(basename(argv[0]))[0] + ".vcd", sigs)
-    print "-" * 20
+    print("-" * 20)
     
   xpin = XPin("port A.0")
   # watch out, that this Net instance will not be deleted until simulation is
@@ -43,11 +43,11 @@ if __name__ == "__main__":
     
   sim.dmanStart()
   
-  print "simulation start: (t=%dns)" % sim.getCurrentTime()
+  print("simulation start: (t=%dns)" % sim.getCurrentTime())
   sim.doRun(15000000)
-  print "simulation end: (t=%dns)" % sim.getCurrentTime()
+  print("simulation end: (t=%dns)" % sim.getCurrentTime())
 
-  print "value 'timer2_ticks'=%d" % sim.getWordByName(dev, "timer2_ticks")
+  print("value 'timer2_ticks'=%d" % sim.getWordByName(dev, "timer2_ticks"))
   
   sim.dmanStop()
   del dev
diff --git a/examples/python/ex_utils.py b/examples/python/ex_utils.py
index 4ea6659..cad4bad 100644
--- a/examples/python/ex_utils.py
+++ b/examples/python/ex_utils.py
@@ -34,7 +34,7 @@ class SimulavrAdapter(object):
   def getAllRegisteredTraceValues(self):
     os = pysimulavr.ostringstream()
     pysimulavr.DumpManager.Instance().save(os)
-    return filter(None, [i.strip() for i in os.str().split("\n")])
+    return [_f for _f in [i.strip() for i in os.str().split("\n")] if _f]
 
   def dmanSingleDeviceApplication(self):
     pysimulavr.DumpManager.Instance().SetSingleDeviceApp()
diff --git a/examples/python/example.py b/examples/python/example.py
index 57fb0f9..f588594 100644
--- a/examples/python/example.py
+++ b/examples/python/example.py
@@ -1,4 +1,4 @@
-# -*- coding: UTF-8 -*-
+# -*- coding: utf-8 -*-
 # Python test script as demonstration of using pysimulavr in unit tests
 from unittest import TestSuite, TextTestRunner, TestCase, defaultTestLoader
 from sys import argv
@@ -35,11 +35,11 @@ class TestBaseClass(TestCase, SimulavrAdapter):
     self.assertEqual(self.device.PC_size, 2)
     self.doStep()
     self.doStep()
-    self.assertEqual(self.device.PC, 0x8c / 2)
+    self.assertEqual(self.device.PC, 0x8c // 2)
     
   def test_04(self):
     "check flash and data symbols"
-    self.assertEqual(self.device.Flash.GetAddressAtSymbol("main"), 0xfc / 2)
+    self.assertEqual(self.device.Flash.GetAddressAtSymbol("main"), 0xfc // 2)
     self.assertEqual(self.device.data.GetAddressAtSymbol("timer2_ticks"), 0x100)
     
   def addr2word(self, addr):
@@ -90,7 +90,7 @@ class TestBaseClass(TestCase, SimulavrAdapter):
     bpaddr = self.device.Flash.GetAddressAtSymbol("main")
     self.device.BP.AddBreakpoint(bpaddr)
     self.doRun(10000)
-    self.assertEqual(self.device.PC, 0xc2 / 2)
+    self.assertEqual(self.device.PC, 0xc2 // 2)
     self.doStep(4) # call to main
     self.assertEqual(self.device.PC, bpaddr)
     self.doStep(4) # 4 steps more, do nothing because of breakpoint
diff --git a/examples/python/example_io.py b/examples/python/example_io.py
index 7dfe817..84e2940 100644
--- a/examples/python/example_io.py
+++ b/examples/python/example_io.py
@@ -18,7 +18,7 @@ class XPin(pysimulavr.Pin):
     
   def SetInState(self, pin):
     pysimulavr.Pin.SetInState(self, pin)
-    print "%s='%s' (t=%dns)" % (self.name, pin.toChar(), sim.getCurrentTime())
+    print("%s='%s' (t=%dns)" % (self.name, pin.toChar(), sim.getCurrentTime()))
 
 if __name__ == "__main__":
 
@@ -33,7 +33,7 @@ if __name__ == "__main__":
   a7 = XPin(dev, "A7", "H")
   
   sim.dmanStart()
-  print "simulation start: (t=%dns)" % sim.getCurrentTime()
+  print("simulation start: (t=%dns)" % sim.getCurrentTime())
   sim.doRun(sim.getCurrentTime() + 7000000)
   a1.SetPin("L")
   sim.doRun(sim.getCurrentTime() + 5000000)
@@ -41,11 +41,11 @@ if __name__ == "__main__":
   sim.doRun(sim.getCurrentTime() + 2000000)
   a1.SetPin("H")
   sim.doRun(sim.getCurrentTime() + 1000000)
-  print "simulation end: (t=%dns)" % sim.getCurrentTime()
+  print("simulation end: (t=%dns)" % sim.getCurrentTime())
   
-  print "value 'timer2_ticks'=%d" % sim.getWordByName(dev, "timer2_ticks")
-  print "value 'port_val'=0x%x" % sim.getWordByName(dev, "port_val")
-  print "value 'port_cnt'=%d" % sim.getWordByName(dev, "port_cnt")
+  print("value 'timer2_ticks'=%d" % sim.getWordByName(dev, "timer2_ticks"))
+  print("value 'port_val'=0x%x" % sim.getWordByName(dev, "port_val"))
+  print("value 'port_cnt'=%d" % sim.getWordByName(dev, "port_cnt"))
   
   sim.dmanStop()
   del dev
diff --git a/examples/python/example_pin.py b/examples/python/example_pin.py
index fc685c3..4184807 100644
--- a/examples/python/example_pin.py
+++ b/examples/python/example_pin.py
@@ -5,30 +5,30 @@ class XPin(pysimulavr.Pin):
   
   def SetInState(self, pin):
     pysimulavr.Pin.SetInState(self, pin)
-    print "<pin: id=%s, in=%s/%0.2fV, out=%s, aPin=%0.2fV>" % (id(self),
+    print("<pin: id=%s, in=%s/%0.2fV, out=%s, aPin=%0.2fV>" % (id(self),
                                                                pin.toChar(),
                                                                pin.GetAnalogValue(vcc),
                                                                self.toChar(),
-                                                               self.GetAnalogValue(vcc)),
+                                                               self.GetAnalogValue(vcc)), end=' ')
     
 def printPin(pid, pin, withID = False):
   if withID:
-    print "  pin %d: id=%s, (char)pin='%s', (bool)pin=%s, pin.GetAnalogValue(vcc)=%0.2fV" % (pid,
+    print("  pin %d: id=%s, (char)pin='%s', (bool)pin=%s, pin.GetAnalogValue(vcc)=%0.2fV" % (pid,
                                                                                              id(pin),
                                                                                              pin.toChar(),
                                                                                              pin.toBool(),
-                                                                                             pin.GetAnalogValue(vcc))
+                                                                                             pin.GetAnalogValue(vcc)))
   else:
-    print "  pin %d: (char)pin='%s', (bool)pin=%s, pin.GetAnalogValue(vcc)=%0.2fV" % (pid,
+    print("  pin %d: (char)pin='%s', (bool)pin=%s, pin.GetAnalogValue(vcc)=%0.2fV" % (pid,
                                                                                       pin.toChar(),
                                                                                       pin.toBool(),
-                                                                                      pin.GetAnalogValue(vcc))
+                                                                                      pin.GetAnalogValue(vcc)))
 
 if __name__ == "__main__":
 
   vcc = 5.0
-  print "set vcc=%0.2fV ..." % vcc
-  print "create 2 pins ..."
+  print("set vcc=%0.2fV ..." % vcc)
+  print("create 2 pins ...")
   p1 = XPin()
   p1.outState = pysimulavr.Pin.LOW
   printPin(1, p1, True)
@@ -37,45 +37,45 @@ if __name__ == "__main__":
   p2.outState = pysimulavr.Pin.TRISTATE
   printPin(2, p2, True)
   
-  print "\ncreate net ..."
+  print("\ncreate net ...")
   n = pysimulavr.Net()
-  print "  add pin 1:",
+  print("  add pin 1:", end=' ')
   n.Add(p1)
-  print ""
-  print "  add pin 2:",
+  print("")
+  print("  add pin 2:", end=' ')
   n.Add(p2)
-  print ""
+  print("")
   
   printPin(1, p1)
   printPin(2, p2)
 
-  print "\nset pin 2 output to PULLUP:",
+  print("\nset pin 2 output to PULLUP:", end=' ')
   p2.SetPin("h")
-  print ""
+  print("")
   printPin(1, p1)
   printPin(2, p2)
 
-  print "\nset pin 1 output to HIGH:",
+  print("\nset pin 1 output to HIGH:", end=' ')
   p1.SetPin("H")
-  print ""
+  print("")
   printPin(1, p1)
   printPin(2, p2)
 
-  print "\nset pin 2 output to TRISTATE:",
+  print("\nset pin 2 output to TRISTATE:", end=' ')
   p2.SetPin("t")
-  print ""
+  print("")
   printPin(1, p1)
   printPin(2, p2)
 
-  print "\nset pin 1 output to TRISTATE:",
+  print("\nset pin 1 output to TRISTATE:", end=' ')
   p1.SetPin("t")
-  print ""
+  print("")
   printPin(1, p1)
   printPin(2, p2)
 
-  print "\nset pin 2 output to LOW:",
+  print("\nset pin 2 output to LOW:", end=' ')
   p2.SetPin("L")
-  print ""
+  print("")
   printPin(1, p1)
   printPin(2, p2)
   
diff --git a/examples/python/multicore.py b/examples/python/multicore.py
index 1924b3c..ead927b 100644
--- a/examples/python/multicore.py
+++ b/examples/python/multicore.py
@@ -6,44 +6,44 @@ import pysimulavr
 if __name__ == "__main__":
 
   # get systemclock instance
-  print "multicore example:"
+  print("multicore example:")
   sc = pysimulavr.SystemClock.Instance()
   
   # create core A: clock generator 250Hz with device clock 4MHz
-  print "  create core A ..."
+  print("  create core A ...")
   devA = pysimulavr.AvrFactory.instance().makeDevice("atmega16")
   devA.Load("multicore_a.elf")
   devA.SetClockFreq(250) # clock period in ns!
   sc.Add(devA)
   
   # create core B: count rising edges, device clock 10MHz, calculated cnt_res = 156,25 nominal!
-  print "  create core B ..."
+  print("  create core B ...")
   devB = pysimulavr.AvrFactory.instance().makeDevice("atmega16")
   devB.Load("multicore_b.elf")
   devB.SetClockFreq(100) # clock period in ns!
   sc.Add(devB)
 
   # create net: connect core A, Port B3 to core B, Port D2
-  print "  connect core A with core B ..."
+  print("  connect core A with core B ...")
   n = pysimulavr.Net()
   n.Add(devA.GetPin("B3"))
   n.Add(devB.GetPin("D2"))
 
   # get addresses for cnt_res and cnt_irq variables
   a = devB.data.GetAddressAtSymbol("cnt_irq")
-  print "  core B: address(cnt_irq)=0x%x" % a
+  print("  core B: address(cnt_irq)=0x%x" % a)
   b = devB.data.GetAddressAtSymbol("cnt_res")
-  print "  core B: address(cnt_res)=0x%x" % a
+  print("  core B: address(cnt_res)=0x%x" % a)
   
   # run simulation, stop after given time and check values
-  print "  run simulation ..."
+  print("  run simulation ...")
   sc.RunTimeRange(4000000)
-  print "  t= 4ms, cnt_irq=%d, cnt_res=%3d" % (devB.getRWMem(a), devB.getRWMem(b))
+  print("  t= 4ms, cnt_irq=%d, cnt_res=%3d" % (devB.getRWMem(a), devB.getRWMem(b)))
   sc.RunTimeRange(4000000)
-  print "  t= 8ms, cnt_irq=%d, cnt_res=%3d" % (devB.getRWMem(a), devB.getRWMem(b))
+  print("  t= 8ms, cnt_irq=%d, cnt_res=%3d" % (devB.getRWMem(a), devB.getRWMem(b)))
   sc.RunTimeRange(12000000)
-  print "  t=20ms, cnt_irq=%d, cnt_res=%3d" % (devB.getRWMem(a), devB.getRWMem(b))
+  print("  t=20ms, cnt_irq=%d, cnt_res=%3d" % (devB.getRWMem(a), devB.getRWMem(b)))
   sc.RunTimeRange(12000000)
-  print "  t=32ms, cnt_irq=%d, cnt_res=%3d" % (devB.getRWMem(a), devB.getRWMem(b))
+  print("  t=32ms, cnt_irq=%d, cnt_res=%3d" % (devB.getRWMem(a), devB.getRWMem(b)))
   
 # EOF
-- 
2.24.0

