在Python中定义和使用类

时间:2015-09-03 14:41:35

标签: python class object

我正在学习Python,我有一些代码没有做我认为应该做的事情。我使用的是v3.4.3。最有可能的问题是:

class Router:
    '''A representation of a router'''
    name = ''
    ifc = []

class Interface:
    ifName = ''
    addr = []
    helpers = ''

    def __init__(self,routername,num):
        self.ifc = []
        self.name = routername
        for i in range(num):
           self.ifc.append(Router.Interface())

我打算创建一个具有可变数量接口的路由器对象。当我尝试通过

创建路由器时,此编号是已知的
d=[]
d.append(Router('r0',2))

创建一个路由器d,名称为' r0',并带有两个接口。这部分效果很好。

我尝试做的下一件事涉及路由器对象上的各个接口。

接口可以分配可变数量的IP地址,从0开始,我认为上限可能是16,但这并不重要。我的大多数接口都会分配1到6个IP地址,而且我不知道创建路由器对象时的确切数字,这个数字也会随着时间的推移而变化,实际的IP地址也是如此。

我已经编写了一些代码来演示正在发生的事情以及我预期会发生什么。每次我调用prnt函数时,我都希望它能在一个路由器上打印出一个接口最多两个IP,一个la:

R0,接口0的所需输出。其他应该类似。

Router:  0 Interface:  0 Name:  Vlan100
         0 ('1.1.1.0', '1.1.1.1', '255.255.255.0', 'secondary')
Router:  0 Interface:  0 Name:  Vlan100
         1 ('2.2.2.0', '2.2.2.1', '255.255.255.0', '')

实际上发生的情况是,特定路由器接口的IP数据未应用于接口,而是应用于路由器类。

代码:

def prnt(rtr, ifnum,label):
'''debug routine for showing results'''
    print('\n',label,len(d), len(d[rtr].ifc),len(d[rtr].ifc[ifnum].addr))
    for j in range(len(d[rtr].ifc[ifnum].addr)):
        print('Router: ',rtr,'Interface: ',ifnum,'Name: ',d[rtr].ifc[ifnum].ifName,'\n\t\t',j,d[rtr].ifc[ifnum].addr[j])
    print('\n')
    return

#Define 8 IP networks.

net1 = ('1.1.1.0', '1.1.1.1', '255.255.255.0', 'secondary')
net2 = ('2.2.2.0', '2.2.2.1', '255.255.255.0', '')
net3 = ('3.3.3.0', '3.3.3.1', '255.255.255.0', 'secondary')
net4 = ('4.4.4.0', '4.4.4.1', '255.255.255.0', '')
net5 = ('5.5.5.0', '5.5.5.1', '255.255.255.0', 'secondary')
net6 = ('6.6.6.0', '6.6.6.1', '255.255.255.0', '')
net7 = ('7.7.7.0', '7.7.7.1', '255.255.255.0', 'secondary')
net8 = ('8.8.8.0', '8.8.8.1', '255.255.255.0', '')

# create d, a blank list
d=[]
print('Define list d.  Length of d:',len(d))

#make d an object r0 of the Router class.  Router has two interfaces.
d.append(Router('r0',2))
print ('Append r0. Length of d:',len(d),'Interface count:',len(d[0].ifc))

#make d an object r1 of the Router class. Router has two interfaces.
d.append(Router('r1',2))
print ('Append r1. Length of d:',len(d),'Interface count:','r0:',len(d[0].ifc),'r1:',len(d[1].ifc))

#Name four interfaces, two on each router
d[0].ifc[0].ifName='Vlan100'
d[0].ifc[1].ifName='Vlan101'
d[1].ifc[0].ifName='Vlan200'
d[1].ifc[1].ifName='Vlan201'
print ('Name Interfaces. Length of d:',len(d),'Interface count:',
       'r0:',len(d[0].ifc),'r1:',len(d[1].ifc))

# show the router interface information.  At this point, nothing should print, as
# none of the interfaces have IP addresses attched to them
prnt(0,0,'I expect nothing #1')
prnt(0,1,'I expect nothing #2')
prnt(1,0,'I expect nothing #3')
prnt(1,1,'I expect nothing #4')

#
#The intent is to populate two IP addresses onto each of the four interfaces
#Starting with R0-0, I'll add one IP at a time and print each of the four interfaces
d[0].ifc[0].addr.append(net1)
prnt(0,0,'I expect 1.1 only on r0 ifc 0')
prnt(0,1,'I expect nothing #5')
prnt(1,0,'I expect nothing #6')
prnt(1,1,'I expect nothing #7')
#
d[0].ifc[0].addr.append(net2)
prnt(0,0,'I expect 1.1 and 2.2 on r0 ifc 0')
prnt(0,1,'I expect nothing #8')
prnt(1,0,'I expect nothing #9')
prnt(1,1,'I expect nothing #10')
#     
d[0].ifc[1].addr.append(net3)
prnt(0,0,'I expect 1.1 and 2.2 on r0 ifc 0')
prnt(0,1,'I expect 3.3 on r0 ifc 1')
prnt(1,0,'I expect nothing #11')
prnt(1,1,'I expect nothing #12')
#     
d[0].ifc[1].addr.append(net4)
prnt(0,0,'I expect 1.1 and 2.2 on r0 ifc 0')
prnt(0,1,'I expect 3.3 and 4.4 on r0 ifc 1')
prnt(1,0,'I expect nothing #11')
prnt(1,1,'I expect nothing #12')
#
d[1].ifc[0].addr.append(net5)
prnt(0,0,'I expect 1.1/2.2 on r0 ifc 0')
prnt(0,1,'I expect 3.3/4.4 on r0 ifc 1')
prnt(1,0,'I expect 5.5 only on r1 ifc 0')
prnt(1,1,'I expect nothing #13')
#
d[1].ifc[0].addr.append(net6)
prnt(0,0,'I expect 1.1/2.2 on r0 ifc 0')
prnt(0,1,'I expect 3.3/4.4 on r0 ifc 1')
prnt(1,0,'I expect 5.5/6.6 on r1 ifc 0')
prnt(1,1,'I expect nothing #14')
#
d[1].ifc[1].addr.append(net7)
prnt(0,0,'I expect 1.1/2.2 on r0 ifc 0')
prnt(0,1,'I expect 3.3/4.4 on r0 ifc 1')
prnt(1,0,'I expect 5.5/6.6 on r1 ifc 0')
prnt(1,1,'I expect 7.7 only on r1 ifc 1')
#
d[1].ifc[1].addr.append(net8)
prnt(0,0,'I expect 1.1/2.2 on r0 ifc 0')
prnt(0,1,'I expect 3.3/4.4 on r0 ifc 1')
prnt(1,0,'I expect 5.5/6.6 on r1 ifc 0')
prnt(1,1,'I expect 7.7/8.8 on r1 ifc 1')

我有人告诉我,在课堂上嵌入课程是一个问题,所以我尝试了另一种方法,其中包括:

 class Address:
    '''representation of the data required for a valid interface
    type is either '' (primary) or 'secondary' '''
    sub=''
    gw=''
    mask=''
    typ=''

class Interface:
    '''representation of an interface'''
    ifName = ''
    addr = []
    helpers=''
    def __init__(self,name,ipnum):
        self.ifName = name
        self.addr = [Address(i) for i in range(ipnum)]

class Router:
    ''' representation of a router'''
    name = ''
    ifc = []
    def __init__(self,name,ifnum):
        self.name = name
        self.ifc =[Interface(i) for i in range(ifnum)] 

这是朝着正确方向迈出的一步吗?我有点挂在订单(和步骤)上,我将在其中创建各种对象。对于给定的路由器,我可以先创建路由器对象,然后是接口,然后是地址,还是需要以相反的顺序执行?我有超过100个路由器分布的大量IP空间来处理。请原谅我用OOP术语缺乏精确性。我老了,学习新东西。

0 个答案:

没有答案