简体   繁体   中英

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. 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. 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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM