[英]How to access python class method from main and transfering and accessing the variable in another python file
I have S8Test.py with testFirewallS8 class and some methods. 我有带有testFirewallS8类和一些方法的S8Test.py。 I want to access the method declared inside this class form the main method. 我想从主方法访问此类内部声明的方法。 and set the variable from that method another python file contains the same variable to modify. 并从该方法设置变量,另一个python文件包含要修改的相同变量。 How can I do this: 我怎样才能做到这一点:
#! /usr/bin/env python
__author__ = 'Milson Munakami'
__revision__ = '0.0.2'
import json
import urllib
import httplib
from scapy.all import *
import unittest
import os, sys, socket, struct, select, time
from threading import Thread
import logging
import traceback
from mininet.net import Mininet
from mininet.node import OVSSwitch, OVSKernelSwitch, Controller, RemoteController
from mininet.log import setLogLevel, info
from mininet.cli import CLI
class TestFirewallS8( unittest.TestCase ):
def setUp(self):
self.controllerIp="127.0.0.1"
self.switch = "00:00:00:00:00:00:00:01"
self.destinationIp = "10.0.0.1"
self.startTime_ = time.time()
self.failed = False
self.reportStatus_ = True
self.name_ = "Firewall"
self.log = logging.getLogger("unittest")
"Create an empty network and add nodes to it."
self.net = Mininet( controller=RemoteController )
#Want to move this method call from outside the setUp method because it need to be initiated only once for the whole test but
#it need to access the class variables and pass it to another python file i.e. Events.py to perform some task on the object i.e. self
#self.CreateNet()
def createNet(self):
print "Me"
info( '*** Adding controller\n' )
self.net.addController( 'c0' , controller=RemoteController,ip= "127.0.0.1", port=6633)
info( '*** Adding hosts\n' )
h1 = self.net.addHost( 'h1', ip='10.0.0.1' )
h2 = self.net.addHost( 'h2', ip='10.0.0.2' )
h3 = self.net.addHost( 'h3', ip='10.0.0.3' )
info( '*** Adding switch\n' )
s1 = self.net.addSwitch( 's1' )
info( '*** Creating links\n' )
self.net.addLink( h1, s1 )
self.net.addLink( h2, s1 )
self.net.addLink( h3, s1 )
def setFinalcondition(self):
Precondition.SetFinalcondition(self)
info( '*** Stopping network' )
self.net.stop()
def testCreateFlow(self):
Events.createFlow(self)
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestFirewallS8))
return suite
if __name__ == '__main__':
#How to get run the method of testFirewallS8 class and set the variable of it like self.net
suiteFew = unittest.TestSuite(testCreateFlow)
TestFirewallS8("createNet")
In another Events.py I have: 在另一个Events.py中,我有:
def createFlow(self):
info( '*** Starting network\n')
self.net.start()
info( '*** Testing network connecivity\n')
#self.net.pingAll()
h1 = self.net.get('h1')
h3 = self.net.get('h3')
h1.cmd('ping -c1 %s' % h3.IP())
I'm not completely clear what you're asking, but if you have a class definition: 我不清楚您要问的是什么,但是如果您有一个类定义:
class MyClass(object):
def __init__(self, name):
self.name = name
def big_name(self):
print(self.name.upper())
After you instantiate it ( MyClass('Bob')
), assign it to a variable. 实例化它( MyClass('Bob')
)之后,将其分配给变量。
bob = MyClass('Bob')
You can then execute methods of that class instance by simply accessing it's attributes. 然后,您只需访问其属性即可执行该类实例的方法。
bob.big_name # accesses the "big_name" attribute on "bob": a bound method
bob.big_name() # executes the bound method
Attributes can also be variables, and you can freely access and reassign them as well: 属性也可以是变量,您也可以自由访问和重新分配它们:
bob.name # accesses the "name" attribute on "bob": a string
bob.name = 'Robert' # reassigns the "name" attribute
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.