Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
类创建关注点,python 3.4_Python_Class_Self_Telnetlib - Fatal编程技术网

类创建关注点,python 3.4

类创建关注点,python 3.4,python,class,self,telnetlib,Python,Class,Self,Telnetlib,所以,我正在创建一个自动化测试系统(范围膨胀-最初的意图是对一个特定测试用例进行一次性的bash自动化,它已经转变为创建一个可以做很多其他事情的系统)。我做过一些类似的事情,但不知怎么的,我从来没有使用过类,在我为过去的项目编写的模块中,所有的东西都是在函数级别 因此,我正在寻找一个简单的检查/确认,我正在接近这个权利。它是一个通信模块,旨在通过telnet连接到远程linux机器,然后从linux机器连接到的设备中读取各种i2c数据 我希望确保我正确地创建它,并正确地声明对象,以便类对象中的函

所以,我正在创建一个自动化测试系统(范围膨胀-最初的意图是对一个特定测试用例进行一次性的bash自动化,它已经转变为创建一个可以做很多其他事情的系统)。我做过一些类似的事情,但不知怎么的,我从来没有使用过类,在我为过去的项目编写的模块中,所有的东西都是在函数级别

因此,我正在寻找一个简单的检查/确认,我正在接近这个权利。它是一个通信模块,旨在通过telnet连接到远程linux机器,然后从linux机器连接到的设备中读取各种i2c数据

我希望确保我正确地创建它,并正确地声明对象,以便类对象中的函数/方法正确地执行各自的任务。我想我的思路是对的,但是让更有经验的人看一看并没有什么坏处。特别是,我希望确保正确地设置类进行实例化,并以正确的方式声明self/init

谢谢你抽出时间

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 ')

我原以为Python3不支持老式类,但上面的方法行得通吗?老实说,当我开始将其连接在一起时,我正在编写关于类的3.4文档。类方法最初是独立的函数,我意识到它们可以更干净地组织到自己的与telnet通信相关的类中。它还使得我不必太费劲地处理全局声明。@mu無 旧式类在3.x中不存在。无论你是从<代码>对象< /代码>中显式继承,它都是新样式。如果这是你认为可以改进的工作代码,请考虑一下。如果它不起作用,请更准确地说明问题所在。当它是一组个性化函数时,所讨论的代码工作得很好,但我正在处理全局变量更改,希望使其更健壮一些。