如何在python中的另一个程序中添加class方法的变量?
How to add a variable of a method of a class in aonther program in python?
我在 ns3.py 中有一个名为 WIFISegment 的 class:
class WIFISegment( object ):
"""Equivalent of radio WiFi channel.
Only Ap and WDS devices support SendFrom()."""
def __init__( self ):
# Helpers instantiation.
self.channelhelper = ns.wifi.YansWifiChannelHelper.Default()
self.phyhelper = ns.wifi.YansWifiPhyHelper.Default()
self.wifihelper = ns.wifi.WifiHelper.Default()
self.machelper = ns.wifi.NqosWifiMacHelper.Default()
# Setting channel to phyhelper.
self.channel = self.channelhelper.Create()
self.phyhelper.SetChannel( self.channel )
def add( self, node, port=None, intfName=None, mode=None ):
"""Connect Mininet node to the segment.
Will create WifiNetDevice with Mac type specified in
the MacHelper (default: AdhocWifiMac).
node: Mininet node
port: node port number (optional)
intfName: node tap interface name (optional)
mode: TapBridge mode (UseLocal or UseBridge) (optional)"""
# Check if this Mininet node has assigned an underlying ns-3 node.
if hasattr( node, 'nsNode' ) and node.nsNode is not None:
# If it is assigned, go ahead.
pass
else:
# If not, create new ns-3 node and assign it to this Mininet node.
node.nsNode = ns.network.Node()
allNodes.append( node )
# Install new device to the ns-3 node, using provided helpers.
device = self.wifihelper.Install( self.phyhelper, self.machelper, node.nsNode ).Get( 0 )
mobilityhelper = ns.mobility.MobilityHelper()
# Install mobility object to the ns-3 node.
mobilityhelper.Install( node.nsNode )
# If port number is not specified...
if port is None:
# ...obtain it automatically.
port = node.newPort()
# If interface name is not specified...
if intfName is None:
# ...obtain it automatically.
intfName = Link.intfName( node, port ) # classmethod
# In the specified Mininet node, create TBIntf bridged with the 'device'.
tb = TBIntf( intfName, node, port, node.nsNode, device, mode )
return tb
这个 class 有一个方法叫做 def add( self, node, port=None, intfName=None, mode=None ) 并且在这个方法中我们定义了 mobilityhelper。
我想知道我是否可以在另一个程序中使用 mobilityhelper。
例如,我写了另一个程序,在我的程序中导入 WIFISegment,然后定义 wifi = WIFISegment() 并且我可以使用它的方法 "add" 如下 wifi.add(h1)(h1 是这里的主机)。
我的问题是如何在我的其他程序中使用 add() 方法的 mobilityhelper。因为每次都需要设置新的机动性
谢谢
法扎内
显而易见的方法是return它:
return tb, mobilityhelper
并像这样使用它:
original_ret_value, your_mobilityhelper = wifi.add(h1)
但这会破坏与旧代码的兼容性(add
returned TBIntf
,但现在是 returns 元组)。您可以添加可选参数来指示 add
方法是否应该 return mobilityhelper 或不:
def add( self, node, port=None, intfName=None, mode=None, return_mobilityhelper=False ):
...
if return_mobilityhelper:
return tb, mobilityhelper
else:
return tb
现在,如果您像以前一样使用 add
,它的行为方式与 wifi.add(h1)
之前一样。但是你可以使用新参数,然后得到你的mobilityhelper
whatever, mobilityhelper = wifi.add(h1, return_mobilityhelper=True)
或
returned_tuple = wifi.add(h1, return_mobilityhelper=True)
mobilityhelper = returned_tuple[1]
另一种方式是修改一个参数(一个列表)
def add( self, node, port=None, intfName=None, mode=None, out_mobilityhelper=None):
...
if hasattr(out_mobilityhelper, "append"):
out_mobilityhelper.append(mobilityhelper)
return tb
它仍然与您的旧代码兼容,但您可以将列表传递给参数,然后提取您的 mobilityhelper
mobhelp_list = []
wifi.add(h1, out_mobilityhelper=mobhelp_list)
mobilityhelper = mobhelp_list[0]
mobilityhelper 在class WIFISegment 添加方法中定义如下(如上一个问题所述):
mobilityhelper = ns.mobility.MobilityHelper()
# Install mobility object to the ns-3 node.
mobilityhelper.Install( node.nsNode )
当我们使用 wifi.add() 时,它会安装在每个主机 (node.nsNode) 上。
现在在我的其他程序中我想使用这个 mobilityhelper,但要安装一种新型的移动工具,如下所示:
mobilityhelper.SetPositionAllocator ("ns3::GridPositionAllocator",
"MinX", ns.core.DoubleValue(0.0),
"MinY", ns.core.DoubleValue(0.0),
"DeltaX", ns.core.DoubleValue(130.0),
"DeltaY", ns.core.DoubleValue(100.0),
"GridWidth", ns.core.UintegerValue(2),
"LayoutType", ns.core.StringValue("RowFirst"))
mobilityhelper.SetMobilityModel("ns3::ConstantPositionMobilityModel")
# Install mobility object to the ns-3 node.
mobilityhelper.Install( h1 )
现在想知道如何在我的新程序中使用该移动性并为我的节点安装新的移动性。
希望我不会让你感到困惑。
我在 ns3.py 中有一个名为 WIFISegment 的 class:
class WIFISegment( object ):
"""Equivalent of radio WiFi channel.
Only Ap and WDS devices support SendFrom()."""
def __init__( self ):
# Helpers instantiation.
self.channelhelper = ns.wifi.YansWifiChannelHelper.Default()
self.phyhelper = ns.wifi.YansWifiPhyHelper.Default()
self.wifihelper = ns.wifi.WifiHelper.Default()
self.machelper = ns.wifi.NqosWifiMacHelper.Default()
# Setting channel to phyhelper.
self.channel = self.channelhelper.Create()
self.phyhelper.SetChannel( self.channel )
def add( self, node, port=None, intfName=None, mode=None ):
"""Connect Mininet node to the segment.
Will create WifiNetDevice with Mac type specified in
the MacHelper (default: AdhocWifiMac).
node: Mininet node
port: node port number (optional)
intfName: node tap interface name (optional)
mode: TapBridge mode (UseLocal or UseBridge) (optional)"""
# Check if this Mininet node has assigned an underlying ns-3 node.
if hasattr( node, 'nsNode' ) and node.nsNode is not None:
# If it is assigned, go ahead.
pass
else:
# If not, create new ns-3 node and assign it to this Mininet node.
node.nsNode = ns.network.Node()
allNodes.append( node )
# Install new device to the ns-3 node, using provided helpers.
device = self.wifihelper.Install( self.phyhelper, self.machelper, node.nsNode ).Get( 0 )
mobilityhelper = ns.mobility.MobilityHelper()
# Install mobility object to the ns-3 node.
mobilityhelper.Install( node.nsNode )
# If port number is not specified...
if port is None:
# ...obtain it automatically.
port = node.newPort()
# If interface name is not specified...
if intfName is None:
# ...obtain it automatically.
intfName = Link.intfName( node, port ) # classmethod
# In the specified Mininet node, create TBIntf bridged with the 'device'.
tb = TBIntf( intfName, node, port, node.nsNode, device, mode )
return tb
这个 class 有一个方法叫做 def add( self, node, port=None, intfName=None, mode=None ) 并且在这个方法中我们定义了 mobilityhelper。
我想知道我是否可以在另一个程序中使用 mobilityhelper。 例如,我写了另一个程序,在我的程序中导入 WIFISegment,然后定义 wifi = WIFISegment() 并且我可以使用它的方法 "add" 如下 wifi.add(h1)(h1 是这里的主机)。
我的问题是如何在我的其他程序中使用 add() 方法的 mobilityhelper。因为每次都需要设置新的机动性
谢谢 法扎内
显而易见的方法是return它:
return tb, mobilityhelper
并像这样使用它:
original_ret_value, your_mobilityhelper = wifi.add(h1)
但这会破坏与旧代码的兼容性(add
returned TBIntf
,但现在是 returns 元组)。您可以添加可选参数来指示 add
方法是否应该 return mobilityhelper 或不:
def add( self, node, port=None, intfName=None, mode=None, return_mobilityhelper=False ):
...
if return_mobilityhelper:
return tb, mobilityhelper
else:
return tb
现在,如果您像以前一样使用 add
,它的行为方式与 wifi.add(h1)
之前一样。但是你可以使用新参数,然后得到你的mobilityhelper
whatever, mobilityhelper = wifi.add(h1, return_mobilityhelper=True)
或
returned_tuple = wifi.add(h1, return_mobilityhelper=True)
mobilityhelper = returned_tuple[1]
另一种方式是修改一个参数(一个列表)
def add( self, node, port=None, intfName=None, mode=None, out_mobilityhelper=None):
...
if hasattr(out_mobilityhelper, "append"):
out_mobilityhelper.append(mobilityhelper)
return tb
它仍然与您的旧代码兼容,但您可以将列表传递给参数,然后提取您的 mobilityhelper
mobhelp_list = []
wifi.add(h1, out_mobilityhelper=mobhelp_list)
mobilityhelper = mobhelp_list[0]
mobilityhelper 在class WIFISegment 添加方法中定义如下(如上一个问题所述):
mobilityhelper = ns.mobility.MobilityHelper()
# Install mobility object to the ns-3 node.
mobilityhelper.Install( node.nsNode )
当我们使用 wifi.add() 时,它会安装在每个主机 (node.nsNode) 上。
现在在我的其他程序中我想使用这个 mobilityhelper,但要安装一种新型的移动工具,如下所示:
mobilityhelper.SetPositionAllocator ("ns3::GridPositionAllocator",
"MinX", ns.core.DoubleValue(0.0),
"MinY", ns.core.DoubleValue(0.0),
"DeltaX", ns.core.DoubleValue(130.0),
"DeltaY", ns.core.DoubleValue(100.0),
"GridWidth", ns.core.UintegerValue(2),
"LayoutType", ns.core.StringValue("RowFirst"))
mobilityhelper.SetMobilityModel("ns3::ConstantPositionMobilityModel")
# Install mobility object to the ns-3 node.
mobilityhelper.Install( h1 )
现在想知道如何在我的新程序中使用该移动性并为我的节点安装新的移动性。
希望我不会让你感到困惑。