Skip to content

Commit f1c1404

Browse files
committed
Update to support Python3 and replace urllib2 with the requests library
1 parent 267b800 commit f1c1404

21 files changed

Lines changed: 207 additions & 189 deletions

Test/test_alignment.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
@author: Kay Kasemir
44
"""
5+
from __future__ import print_function
56
import unittest
67
from scan.commands import Set, CommandSequence
78
from scan.alignment import AlignmentScan
@@ -13,7 +14,7 @@ def testBasics(self):
1314
pre=Set("motor_y", 3),
1415
find_command="FindPeak")
1516
cmds = align.createScan()
16-
print CommandSequence(cmds)
17+
print(CommandSequence(cmds))
1718

1819
self.assertEqual(str(cmds), "[Set('Demo:CS:Scan:Fit:Height', 0), Set('motor_y', 3), Loop('motor_x', 0, 10, 0.5, [ Delay(0.5), Log('signal', 'motor_x'), Script('WriteDataToPV', 'motor_x', 'Demo:CS:Scan:Fit:Data:X'), Script('WriteDataToPV', 'signal', 'Demo:CS:Scan:Fit:Data:Y', '-', '1') ]), Script('FindPeak', 'motor_x', 'signal', '-', '1', 'Demo:CS:Scan:Fit:Pos', 'Demo:CS:Scan:Fit:Height', 'Demo:CS:Scan:Fit:Width')]")
1920

Test/test_commands.py

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
from __future__ import print_function
22
import unittest
33
import xml.etree.ElementTree as ET
44
from scan.commands import *
@@ -12,257 +12,257 @@ class CommandTest(unittest.TestCase):
1212
def testXMLEscape(self):
1313
# Basic comment
1414
cmd = Comment("Hello")
15-
print cmd
15+
print(cmd)
1616
self.assertEqual(ET.tostring(cmd.genXML()), "<comment><text>Hello</text></comment>")
1717

1818
# Check proper escape of "less than"
1919
cmd = Comment("Check for current < 10")
20-
print cmd
20+
print(cmd)
2121
self.assertEqual(ET.tostring(cmd.genXML()), "<comment><text>Check for current &lt; 10</text></comment>")
2222

2323
def testDelayCommand(self):
2424
# Basic set
2525
cmd = Delay(47.11)
26-
print cmd
26+
print(cmd)
2727
self.assertEqual(str(cmd), "Delay(47.11)")
2828
self.assertEqual(ET.tostring(cmd.genXML()), "<delay><seconds>47.11</seconds></delay>")
2929

3030
def testConfig(self):
3131
# Basic set
3232
cmd = ConfigLog(True)
33-
print cmd
33+
print(cmd)
3434
self.assertEqual(str(cmd), "ConfigLog(True)")
3535
self.assertEqual(ET.tostring(cmd.genXML()), "<config_log><automatic>true</automatic></config_log>")
3636

3737
def testSetCommand(self):
3838
# Basic set
3939
cmd = Set("some_device", 3.14)
40-
print cmd
40+
print(cmd)
4141
self.assertEqual(str(cmd), "Set('some_device', 3.14)")
4242
self.assertEqual(ET.tostring(cmd.genXML()), "<set><device>some_device</device><value>3.14</value><wait>false</wait></set>")
4343

4444
# Handle numeric as well as string for value
4545
cmd = Set("some_device", "Text")
46-
print cmd
46+
print(cmd)
4747
self.assertEqual(str(cmd), "Set('some_device', 'Text')")
4848
self.assertEqual(ET.tostring(cmd.genXML()), "<set><device>some_device</device><value>\"Text\"</value><wait>false</wait></set>")
4949

5050
# With completion
5151
cmd = Set("some_device", 3.14, completion=True)
52-
print cmd
52+
print(cmd)
5353
self.assertEqual(ET.tostring(cmd.genXML()), "<set><device>some_device</device><value>3.14</value><completion>true</completion><wait>false</wait></set>")
5454

5555
# .. and timeout
5656
cmd = Set("some_device", 3.14, completion=True, timeout=5.0)
57-
print cmd
57+
print(cmd)
5858
self.assertEqual(ET.tostring(cmd.genXML()), "<set><device>some_device</device><value>3.14</value><completion>true</completion><wait>false</wait><timeout>5.0</timeout></set>")
5959

6060
# Setting a readback PV (the default one) enables wait-on-readback
6161
cmd = Set("some_device", 3.14, completion=True, readback=True, tolerance=1, timeout=10.0)
62-
print cmd
62+
print(cmd)
6363
self.assertEqual(ET.tostring(cmd.genXML()), "<set><device>some_device</device><value>3.14</value><completion>true</completion><wait>true</wait><readback>some_device</readback><tolerance>1</tolerance><timeout>10.0</timeout></set>")
6464

6565
# Setting a readback PV (a specific one) enables wait-on-readback
6666
cmd = Set("some_device", 3.14, completion=True, readback="some_device.RBV", tolerance=1, timeout=10.0)
67-
print cmd
67+
print(cmd)
6868
self.assertEqual(ET.tostring(cmd.genXML()), "<set><device>some_device</device><value>3.14</value><completion>true</completion><wait>true</wait><readback>some_device.RBV</readback><tolerance>1</tolerance><timeout>10.0</timeout></set>")
6969

7070
def testSequence(self):
7171
# Nothing
7272
cmd = Sequence()
73-
print cmd
73+
print(cmd)
7474
self.assertEqual(ET.tostring(cmd.genXML()), "<sequence />")
7575

7676
# A few
7777
cmd = Sequence(Comment("One"), Comment("Two"))
78-
print cmd.format()
78+
print(cmd.format())
7979
self.assertEqual(ET.tostring(cmd.genXML()), "<sequence><body><comment><text>One</text></comment><comment><text>Two</text></comment></body></sequence>")
8080

8181
# Sequences are 'flattened'
8282
s1 = Sequence(Comment("One"), Comment("Two"))
8383
s2 = Sequence(Comment("Four"), Comment("Five"))
8484
seq1 = Sequence(s1, Comment("Three"), s2)
85-
print seq1.format()
85+
print(seq1.format())
8686

8787
seq2 = Sequence(Comment("One"), Comment("Two"), Comment("Three"), s2)
88-
print seq2.format()
88+
print(seq2.format())
8989

9090
self.assertEqual(ET.tostring(seq1.genXML()), ET.tostring(seq2.genXML()) )
9191

9292
def testParallel(self):
9393
# Nothing
9494
cmd = Parallel()
95-
print cmd
95+
print(cmd)
9696
self.assertEqual(ET.tostring(cmd.genXML()), "<parallel />")
9797

9898
# A few
9999
cmd = Parallel(Comment("One"), Comment("Two"))
100-
print cmd
100+
print(cmd)
101101
self.assertEqual(ET.tostring(cmd.genXML()), "<parallel><body><comment><text>One</text></comment><comment><text>Two</text></comment></body></parallel>")
102102

103103
# .. as list
104104
cmds = Comment("One"), Comment("Two")
105105
cmd = Parallel(cmds)
106-
print cmd
106+
print(cmd)
107107
self.assertEqual(ET.tostring(cmd.genXML()), "<parallel><body><comment><text>One</text></comment><comment><text>Two</text></comment></body></parallel>")
108108

109109
cmd = Parallel(body=cmds)
110-
print cmd
110+
print(cmd)
111111
self.assertEqual(ET.tostring(cmd.genXML()), "<parallel><body><comment><text>One</text></comment><comment><text>Two</text></comment></body></parallel>")
112112

113113
# With other parameters
114114
cmd = Parallel(cmds, timeout=10)
115-
print cmd
115+
print(cmd)
116116
self.assertEqual(ET.tostring(cmd.genXML()), "<parallel><timeout>10</timeout><body><comment><text>One</text></comment><comment><text>Two</text></comment></body></parallel>")
117117

118118
cmd = Parallel(cmds, errhandler="MyHandler")
119-
print cmd
119+
print(cmd)
120120
self.assertEqual(ET.tostring(cmd.genXML()), "<parallel><body><comment><text>One</text></comment><comment><text>Two</text></comment></body><error_handler>MyHandler</error_handler></parallel>")
121121

122122
cmd = Parallel()
123123
cmd.append(Comment("One"), Comment("Two"))
124-
print cmd
124+
print(cmd)
125125
self.assertEqual(ET.tostring(cmd.genXML()), "<parallel><body><comment><text>One</text></comment><comment><text>Two</text></comment></body></parallel>")
126126

127127
def testLog(self):
128128
# One device
129129
cmd = Log("pv1")
130-
print cmd
130+
print(cmd)
131131
self.assertEqual(ET.tostring(cmd.genXML()), "<log><devices><device>pv1</device></devices></log>")
132132

133133
# Nothing
134134
cmd = Log()
135-
print cmd
135+
print(cmd)
136136
self.assertEqual(ET.tostring(cmd.genXML()), "<log />")
137137

138138
# Several
139139
cmd = Log("pv1", "pv2", "pv3")
140-
print cmd
140+
print(cmd)
141141
self.assertEqual(ET.tostring(cmd.genXML()), "<log><devices><device>pv1</device><device>pv2</device><device>pv3</device></devices></log>")
142142

143143
# .. provided as list
144144
devices_to_log = [ "pv1", "pv2", "pv3" ]
145145
cmd = Log(devices_to_log)
146-
print cmd
146+
print(cmd)
147147
self.assertEqual(ET.tostring(cmd.genXML()), "<log><devices><device>pv1</device><device>pv2</device><device>pv3</device></devices></log>")
148148

149149
def testInclude(self):
150150
cmd = Include("start.scn")
151-
print cmd
151+
print(cmd)
152152
self.assertEqual(ET.tostring(cmd.genXML()), "<include><scan_file>start.scn</scan_file></include>")
153153

154154
cmd = Include("start.scn", "macro=value")
155-
print cmd
155+
print(cmd)
156156
self.assertEqual(ET.tostring(cmd.genXML()), "<include><scan_file>start.scn</scan_file><macros>macro=value</macros></include>")
157157

158158
def testScript(self):
159159
cmd = Script("MyCustomScript")
160-
print cmd
160+
print(cmd)
161161
self.assertEqual(str(cmd), "Script('MyCustomScript')")
162162
self.assertEqual(ET.tostring(cmd.genXML()), "<script><path>MyCustomScript</path></script>")
163163

164164
cmd = Script("MyCustomCommand", "arg1", 42.3)
165-
print cmd
165+
print(cmd)
166166
self.assertEqual(str(cmd), "Script('MyCustomCommand', 'arg1', 42.3)")
167167
self.assertEqual(ET.tostring(cmd.genXML()), "<script><path>MyCustomCommand</path><arguments><argument>arg1</argument><argument>42.3</argument></arguments></script>")
168168

169169
# Arguments already provided as list
170170
cmd = Script("MyCustomCommand", [ "arg1", 42.3 ])
171-
print cmd
171+
print(cmd)
172172
self.assertEqual(str(cmd), "Script('MyCustomCommand', 'arg1', 42.3)")
173173
self.assertEqual(ET.tostring(cmd.genXML()), "<script><path>MyCustomCommand</path><arguments><argument>arg1</argument><argument>42.3</argument></arguments></script>")
174174

175175
def testWait(self):
176176
cmd = Wait('device', 3.14)
177-
print cmd
177+
print(cmd)
178178
self.assertEqual(str(cmd), "Wait('device', 3.14)")
179179
self.assertEqual(ET.tostring(cmd.genXML()), "<wait><device>device</device><value>3.14</value><comparison>EQUALS</comparison></wait>")
180180

181181
cmd = Wait('counts', 1000, comparison='increase by', timeout=5.0, errhandler='someHandler')
182-
print cmd
182+
print(cmd)
183183
self.assertEqual(str(cmd), "Wait('counts', 1000, comparison='increase by', timeout=5, errhandler='someHandler')")
184184
self.assertEqual(ET.tostring(cmd.genXML()), "<wait><device>counts</device><value>1000</value><comparison>INCREASE_BY</comparison><timeout>5.0</timeout><error_handler>someHandler</error_handler></wait>")
185185

186186
def testIf(self):
187187
cmd = If('device', '>', 3.14)
188-
print cmd
188+
print(cmd)
189189
self.assertEqual(str(cmd), "If('device', '>', 3.14, tolerance=0.1)")
190190
self.assertEqual(ET.tostring(cmd.genXML()), "<if><device>device</device><comparison>ABOVE</comparison><value>3.14</value><tolerance>0.1</tolerance><body /></if>")
191191

192192
cmd = If('device', '>', 3.14, [ Comment('BODY') ])
193-
print cmd
193+
print(cmd)
194194
self.assertEqual(str(cmd), "If('device', '>', 3.14, [ Comment('BODY') ], tolerance=0.1)")
195195
self.assertEqual(ET.tostring(cmd.genXML()), "<if><device>device</device><comparison>ABOVE</comparison><value>3.14</value><tolerance>0.1</tolerance><body><comment><text>BODY</text></comment></body></if>")
196196

197197

198198
def testLoop(self):
199199
cmd = Loop('pv1', 1, 10, 0.1)
200-
print cmd
200+
print(cmd)
201201
self.assertEqual(str(cmd), "Loop('pv1', 1, 10, 0.1)")
202202
self.assertEqual(ET.tostring(cmd.genXML()), "<loop><device>pv1</device><start>1</start><end>10</end><step>0.1</step><wait>false</wait><body /></loop>")
203203

204204
cmd = Loop('pv1', 1, 10, 0.1, Delay(5))
205-
print cmd
205+
print(cmd)
206206
self.assertEqual(str(cmd), "Loop('pv1', 1, 10, 0.1, [ Delay(5) ])")
207207
cmd = Loop('pv1', 1, 10, 0.1, Delay(1), Delay(2))
208-
print cmd
208+
print(cmd)
209209
self.assertEqual(str(cmd), "Loop('pv1', 1, 10, 0.1, [ Delay(1), Delay(2) ])")
210210
cmd = Loop('pv1', 1, 10, 0.1, body= [ Delay(1), Delay(2) ])
211-
print cmd
211+
print(cmd)
212212
self.assertEqual(str(cmd), "Loop('pv1', 1, 10, 0.1, [ Delay(1), Delay(2) ])")
213213
self.assertEqual(ET.tostring(cmd.genXML()), "<loop><device>pv1</device><start>1</start><end>10</end><step>0.1</step><wait>false</wait><body><delay><seconds>1</seconds></delay><delay><seconds>2</seconds></delay></body></loop>")
214214

215215
cmd = Loop('pv1', 1, 10, 0.1, Delay(1), Delay(2), readback=True)
216-
print cmd
216+
print(cmd)
217217
self.assertEqual(ET.tostring(cmd.genXML()), "<loop><device>pv1</device><start>1</start><end>10</end><step>0.1</step><wait>true</wait><readback>pv1</readback><tolerance>0.01</tolerance><body><delay><seconds>1</seconds></delay><delay><seconds>2</seconds></delay></body></loop>")
218218

219219
cmd = Loop('pv1', 1, 10, 0.1, completion=True, timeout=10)
220-
print cmd
220+
print(cmd)
221221
self.assertEqual(str(cmd), "Loop('pv1', 1, 10, 0.1, completion=True, timeout=10)")
222222
self.assertEqual(ET.tostring(cmd.genXML()), "<loop><device>pv1</device><start>1</start><end>10</end><step>0.1</step><completion>true</completion><wait>false</wait><timeout>10</timeout><body /></loop>")
223223

224224

225225
def testXMLSequence(self):
226226
cmds = CommandSequence()
227-
print cmds
227+
print(cmds)
228228

229229
self.assertEqual(len(cmds), 0)
230-
print cmds.genSCN()
230+
print(cmds.genSCN())
231231

232232
cmds = CommandSequence(Comment('One'))
233-
print cmds
233+
print(cmds)
234234
self.assertEqual(len(cmds), 1)
235-
print cmds.genSCN()
235+
print(cmds.genSCN())
236236

237237
cmds = CommandSequence(Comment('One'), Comment('Two'))
238-
print cmds
238+
print(cmds)
239239
self.assertEqual(len(cmds), 2)
240-
print cmds.genSCN()
240+
print(cmds.genSCN())
241241
self.assertEqual("""<commands><comment><text>One</text></comment><comment><text>Two</text></comment></commands>""",
242242
cmds.genSCN().replace("\n", "").replace(" ", ""))
243243

244244

245245
cmds = CommandSequence(Comment('One'))
246246
cmds.append(Comment('Two'))
247-
print cmds
247+
print(cmds)
248248
self.assertEqual(len(cmds), 2)
249-
print cmds.genSCN()
249+
print(cmds.genSCN())
250250

251251

252252
cmds = CommandSequence( ( Comment('One'), Comment('Two') ) )
253-
print cmds
253+
print(cmds)
254254
self.assertEqual(len(cmds), 2)
255-
print cmds.genSCN()
255+
print(cmds.genSCN())
256256

257257
cmds = CommandSequence(Comment('Example'), Loop('pos', 1, 5, 0.5, Set('run', 1), Delay(2), Set('run', 0)))
258-
print cmds
258+
print(cmds)
259259

260260
def testCommandSequenceFormat(self):
261261
cmds = CommandSequence(Parallel(
262262
Sequence(Comment('Chain1'), Set('run', 1), Delay(2), Set('run', 0)),
263263
Sequence(Comment('Chain2'), Set('foo', 1), Delay(2), Set('foo', 0))
264264
))
265-
print cmds
265+
print(cmds)
266266
self.assertEqual(str(cmds), "[\n Parallel(\n Sequence(\n Comment('Chain1'),\n Set('run', 1),\n Delay(2),\n Set('run', 0)\n ),\n Sequence(\n Comment('Chain2'),\n Set('foo', 1),\n Delay(2),\n Set('foo', 0)\n )\n )\n]")
267267

268268
def testCommandAbstractMethodsMustBeImplemented(self):

Test/test_data.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
from scan.client.logdata import iterateSamples, getDatetime, parseXMLData, createTable
23

34
# client = ScanClient()
@@ -101,19 +102,19 @@
101102

102103
# client.getData(id) calls this to turn the XML data into a data dict:
103104
data = parseXMLData(xml_text)
104-
print data
105+
print(data)
105106

106107
# Direct access to data dict
107-
print "Times: ", [ str(getDatetime(time)) for time in data['motor_x']['time'] ]
108-
print "Values: ", data['motor_x']['value']
108+
print("Times: ", [ str(getDatetime(time)) for time in data['motor_x']['time'] ])
109+
print("Values: ", data['motor_x']['value'])
109110

110111
# Demo of sample iterator
111112
for s in iterateSamples(data, 'motor_x'):
112-
print "%s (%2d): %s" % (str(getDatetime(s[1])), s[0], str(s[2]))
113+
print("%s (%2d): %s" % (str(getDatetime(s[1])), s[0], str(s[2])))
113114

114115
# Create table, i.e. align samples for different devices by sample ID:
115116
table = createTable(data, 'motor_x', 'motor_y')
116-
print table[0]
117-
print table[1]
117+
print(table[0])
118+
print(table[1])
118119

119120
# With numpy/scipy: plot(table[0], table[1]) etc.

0 commit comments

Comments
 (0)