patternpythonMinor
Automated test system
Viewed 0 times
automatedsystemtest
Problem
I am creating an automated test system (scope bloat - the original intent was a one-off bash automation of a particular test case that has turned into creating a system that will do many other things). I've done a few things similar to this, but somehow never got to using classes; everything was down at the functions level in modules I wrote for past projects.
I'm looking for a simple check/confirmation that I'm approaching this right. It's a communications module intended to connect via telnet to a remote Linux machine, then pull various I2C reads from a device that the Linux machine is connected to.
I want to make sure that I am creating this correctly correctly, and declaring objects correctly so the functions/methods in the class object will perform their individual tasks correctly. I think I'm on the right track but having people a little more experienced take a look couldn't hurt. In particular, I want to make sure I'm setting the class up for instantiation correctly, and declaring
Note: the code here worked reasonably well when it was separate functions not gathered under a unifying class structure, but I was mucking about with global declarations which made me nervous, and it took more tweaking than I was happy with.
```
class Communications: #logs in to the remote as root.
def __init__(self):
self.term = telnetlib.open('10.100.100.103')
print ('logging in now')
term.read_until(b"ogin: ")
term.write('root\n')
time.sleep(3)
term.read_very_eager()
#device power-up and power-down
def node_startup(node):
if node == 1:
node_command = 'node_on 1\n'
elif node == 2:
node_command = 'node_on 2\n'
term.write(node_command)
print (b'powering node', node, 'on\n')
def node_shutdown(node):
if node == 1:
node_command = 'node_off 1\n'
elif node == 2:
node_command = 'node_off 2
I'm looking for a simple check/confirmation that I'm approaching this right. It's a communications module intended to connect via telnet to a remote Linux machine, then pull various I2C reads from a device that the Linux machine is connected to.
I want to make sure that I am creating this correctly correctly, and declaring objects correctly so the functions/methods in the class object will perform their individual tasks correctly. I think I'm on the right track but having people a little more experienced take a look couldn't hurt. In particular, I want to make sure I'm setting the class up for instantiation correctly, and declaring
self/__init__ in the right way.Note: the code here worked reasonably well when it was separate functions not gathered under a unifying class structure, but I was mucking about with global declarations which made me nervous, and it took more tweaking than I was happy with.
```
class Communications: #logs in to the remote as root.
def __init__(self):
self.term = telnetlib.open('10.100.100.103')
print ('logging in now')
term.read_until(b"ogin: ")
term.write('root\n')
time.sleep(3)
term.read_very_eager()
#device power-up and power-down
def node_startup(node):
if node == 1:
node_command = 'node_on 1\n'
elif node == 2:
node_command = 'node_on 2\n'
term.write(node_command)
print (b'powering node', node, 'on\n')
def node_shutdown(node):
if node == 1:
node_command = 'node_off 1\n'
elif node == 2:
node_command = 'node_off 2
Solution
First thing I noticed is you don't seem to be checking if your commands are executed. There's no Exception handling to be found.
Second, avoid the use of magic numbers.
Etcetera. Ofcourse, those variables should be declared on the top of your file. Although Python does not support
Second, avoid the use of magic numbers.
telnetlib.open('10.100.100.103') should be telnetlib.open(TARGET_IP)time.sleep(3) should be time.sleep(SLEEP_INIT)Etcetera. Ofcourse, those variables should be declared on the top of your file. Although Python does not support
constant by default, there's a workaround if you're interested.Context
StackExchange Code Review Q#87865, answer score: 3
Revisions (0)
No revisions yet.