0

So, i'm 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.

so 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 the linux machine is connected to.

i want to make sure i'm creating it correctly, and declaring objects correctly so the functions/methods in the class object will perform their individual tasks rightly. 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.

thanks for your time.

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\n'
        term.write(node_command)
        print (b'shutting node', node, ' down')

    #device sample retrieval
    def get_voltage_i2c(bus, device, datapoint, test, node):
        i2c_sample =  term.write('i2cget -y -f %d %d 0x 8b w >> /tmp/%s_%d.txt\n' %(bus, device, test, node))

    def get_voltage_i2c(bus, device, datapoint, test, node):
        i2c_sample =  term.write('i2cget -y -f %d %d %d %s %d w >> /tmp/%s_%d.txt\n' %(bus, device, datapoint, test, node))


    def get_voltage_i2c(bus, device, datapoint, test, node):
        i2c_sample =  term.write('i2cget -y -f %d %d %d %s %d w >> /tmp/%s_%d.txt\n' %(bus, device, datapoint, test, node))

    #output-scraper to slurp results from the output files the i2cgets write to. (devnote: incomplete at time of posting)
    def get_sample_results():
        time.sleep(30)
        term.read_very_eager()
        term.write(b'cat ')
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
Jiynx
  • 306
  • 1
  • 6
  • I thought the old style classes were not supported in python 3, will the above work? – Anshul Goyal Apr 24 '15 at 15:20
  • to be honest, i was working off of the 3.4 documentation on classes when i started bolting this together. the class methods were originally separate functions that i realized would be more cleanly organized into their own class pertaining to the telnet communications. it also made it so i didn't have to muck about with global declarations much. – Jiynx Apr 24 '15 at 15:54
  • 1
    @mu無 old-style classes *don't exist in 3.x*. Whether or not you explicitly inherit from `object`, it's new-style. – jonrsharpe Apr 24 '15 at 16:02
  • If this is **working code** that you think could be improved, consider asking on http://codereview.stackexchange.com. If it's **not working**, please be more precise about what the problem is. – jonrsharpe Apr 24 '15 at 16:02
  • the code in question worked pretty well when it was a set of individualized functions, but i was mucking about with global variable changes and wanted to make something that was a little more robust. – Jiynx Apr 24 '15 at 16:42

0 Answers0