[pytango] 392/483: Add examples
Sandor Bodo-Merle
sbodomerle-guest at moszumanska.debian.org
Thu Sep 28 19:15:04 UTC 2017
This is an automated email from the git hooks/post-receive script.
sbodomerle-guest pushed a commit to annotated tag bliss_8.10
in repository pytango.
commit 07001557f936f5e7f78f534caf0ac9f4fcb01f27
Author: tiagocoutinho <tiagocoutinho at 4e9c00fd-8f2e-0410-aa12-93ce3db5e235>
Date: Thu May 22 07:12:02 2014 +0000
Add examples
git-svn-id: http://svn.code.sf.net/p/tango-cs/code/bindings/PyTango/trunk@25674 4e9c00fd-8f2e-0410-aa12-93ce3db5e235
---
examples/Clock/Clock.py | 21 ++++++++++
examples/TuringMachine/TuringMachine.py | 69 +++++++++++++++++++++++++++++++++
examples/TuringMachine/turing_client.py | 18 +++++++++
3 files changed, 108 insertions(+)
diff --git a/examples/Clock/Clock.py b/examples/Clock/Clock.py
new file mode 100644
index 0000000..faa24a6
--- /dev/null
+++ b/examples/Clock/Clock.py
@@ -0,0 +1,21 @@
+import time
+from PyTango.server import run
+from PyTango.server import Device, DeviceMeta
+from PyTango.server import attribute, command
+
+
+class Clock(Device):
+ __metaclass__ = DeviceMeta
+
+ @attribute
+ def time(self):
+ """The time attribute"""
+ return time.time()
+
+ @command(dtype_in=str, dtype_out=str)
+ def strftime(self, format):
+ return time.strftime(format)
+
+
+if __name__ == "__main__":
+ run([Clock])
diff --git a/examples/TuringMachine/TuringMachine.py b/examples/TuringMachine/TuringMachine.py
new file mode 100644
index 0000000..994c72b
--- /dev/null
+++ b/examples/TuringMachine/TuringMachine.py
@@ -0,0 +1,69 @@
+import json
+from PyTango import DevState
+from PyTango.server import Device, DeviceMeta, run
+from PyTango.server import attribute, command, device_property
+
+class TuringMachine(Device):
+ __metaclass__ = DeviceMeta
+
+ blank_symbol = device_property(dtype=str, default_value=" ")
+ initial_state = device_property(dtype=str, default_value="init")
+
+ def init_device(self):
+ Device.init_device(self)
+ self.__tape = {}
+ self.__head = 0
+ self.__state = self.initial_state
+ self.__final_states = []
+ self.__transition_function = None
+ self.set_state(DevState.RUNNING)
+
+ @attribute(dtype=(str,))
+ def final_states(self):
+ return self.__final_states
+
+ @final_states.write
+ def final_states(self, final_states):
+ self.__final_states = final_states
+
+ @attribute(dtype=str)
+ def transition_function(self):
+ return self.__transition_function
+
+ @transition_function.write
+ def transition_function(self, func_str):
+ self.__transition_function = tf = {}
+ for k, v in json.loads(func_str).items():
+ tf[tuple(str(k).split(","))] = map(str, v)
+ print tf
+
+ @attribute(dtype=str)
+ def tape(self):
+ s, keys = "", self.__tape.keys()
+ min_used, max_used = min(keys), max(keys)
+ for i in range(min_used, max_used):
+ s += self.__tape.get(i, self.__blank_symbol)
+ return s
+
+ @command
+ def step(self):
+ char_under_head = self.__tape.get(self.__head, self.blank_symbol)
+ x = self.__state, char_under_head
+ if x in self.__transition_function:
+ y = self.__transition_function[x]
+ self.__tape[self.__head] = y[1]
+ if y[2] == "R":
+ self.__head += 1
+ elif y[2] == "L":
+ self.__head -= 1
+ self.__state = y[0]
+ print self.__state
+
+ def dev_state(self):
+ if self.__state in self.__final_states:
+ return DevState.ON
+ else:
+ return DevState.RUNNING
+
+
+run([TuringMachine])
diff --git a/examples/TuringMachine/turing_client.py b/examples/TuringMachine/turing_client.py
new file mode 100644
index 0000000..0b8d751
--- /dev/null
+++ b/examples/TuringMachine/turing_client.py
@@ -0,0 +1,18 @@
+from turing_machine import TuringMachine
+
+transition_function = {("init","0"):("init", "1", "R"),
+ ("init","1"):("init", "0", "R"),
+ ("init"," "):("final"," ", "N"),
+ }
+
+t = TuringMachine("010011 ", final_states=["final"],
+ transition_function=transition_function)
+
+print("Input on Tape:")
+print(t.get_tape_str())
+
+while not t.final():
+ t.step()
+
+print("Result of the Turing machine calculation:")
+print(t.get_tape_str())
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/pytango.git
More information about the debian-science-commits
mailing list