NetRouter应用教程之Python进阶--基于Widora&Openwrt



  • 书接上回:

    2.实际应用中,我们需要输出变量日志,以便于好调试。python可以这样:

    root@Widora:/# python DebugNetRouter.py 
    Hello NetRouter In Python
    ('NetRouter_debug:', 2)
    NetRouter_debug: 4
    

    代码如下:

    print("Hello NetRouter In Python")
    i=2
    print("NetRouter_debug:",i)
    i=4
    print "NetRouter_debug:",i
    

    3.那我要传参数进去怎么玩

    root@Widora:/# python NetRouterInput.py NetRouter
    NetRouter Input: NetRouter
    root@Widora:/# cat NetRouterInput.py 
    import sys
    value=sys.argv[1]
    print "NetRouter Input:",value
    

    4.那我要整个函数,好,满足你

    root@Widora:/# python NetRouterFun.py 
    Hello NetRouter In Python
    

    代码:

    def NetRouter():
            print "Hello NetRouter In Python"
    NetRouter()
    

    我想有一个类:

    root@ubuntu:/home/sdb/NetRouterPython# cat  NetRouterClass.py 
    class NetRouter:
        name=""
        value=""
        def __init__(self,n,a):
            self.name=n
            self.value=a 
        
        def What(self):
            print "name is",self.name,"value is ",self.value
    
    if __name__ == '__main__':
        N=NetRouter("NetRouter","Good")
        N.What()
    

    5.name 神奇用法:
    PS:后面发现开源库,如果没有这样单元测试,直接放弃
    如果单独运行这里python,那么__name__的值为__main__,那么将运行main()这个函数,如此,我们可以在类文件里面写个测试函数。

    if name == "main":
    main()

    我想引用这个类来互相伤害:我要搞多几个文件来互相引用以及我要引用自己的包

    root@ubuntu:/home/sdb/NetRouterPython# cat NetRouterImport.py 
    import NetRouterClass 
    from  NetRouterClass import NetRouter as N
        
    if __name__ == '__main__':
        n=N("Net","go")
        n.What()
    
        nn=NetRouterClass.NetRouter("Router","od")
        nn.What()
    

    就这么神奇~~



  • @netrouter 希望大神继续连载啊~~~


  • administrators

    呼叫大神~~~~~~~