2011-02-14 85 views
-2

可能重複:
Why am I not getting any output?print語句

如果我需要把打印命令和語句來檢查代碼,然後我怎麼把它插入請解釋?我通過下面的代碼提供一個例子嗎?

service = ('u', 'urn:schemas-upnp-org:service:SwitchPower:1') 
binary_light_type = 'urn:schemas-upnp-org:device:BinaryLight:1' 

def on_new_device(dev): 
    """ Callback triggered when a new device is found. 
    """ 
    print 'Got new device:', dev.udn 
    print "Type 'list' to see the whole list" 

    if not dev: 
     return 

def get_switch_service(device): 
    return device.services[service[1]] 

def create_control_point(): 
    """ Creates the control point and binds callbacks to device events. 
    """ 

    c = ControlPoint() 

    c.subscribe('new_device_event', on_new_device) 
    c.subscribe('removed_device_event', on_removed_device) 
    return c 

def main(): 
    """ Main loop iteration receiving input commands. 
    """ 

    c = create_control_point() 
    c.start() 
    run_async_function(_handle_cmds, (c,)) 
    reactor.add_after_stop_func(c.stop) 
    reactor.main() 


def _exit(c): 
    """ Stops the _handle_cmds loop 
    """ 

    global running_handle_cmds 
    running_handle_cmds = False 


def _search(c): 
    """ Start searching for devices of type upnp:rootdevice and repeat 
    search every 600 seconds (UPnP default) 
    """ 

    c.start_search(600, 'upnp:rootdevice') 


def _get_status(c): 
    """ Gets the binary light status and print if it's on or off. 
    """ 

    try: 
     service = get_switch_service(c.current_server) 
     status_response = service.GetStatus() 
     if status_response['ResultStatus'] == '1': 
      print 'Binary light status is on' 
     else: 
      print 'Binary light status is off' 
    except Exception, e: 
     if not hasattr(c, 'current_server') or not c.current_server: 
      print 'BinaryLight device not set.Please use set_light <n>' 
     else: 
      print 'Error in get_status():', e 


def _get_target(c): 
    """ Gets the binary light target and print if it's on or off. 
    """ 
    try: 
     service = get_switch_service(c.current_server) 
     status_response = service.GetTarget() 
     if status_response['RetTargetValue'] == '1': 
      print 'Binary light target is on' 
     else: 
      print 'Binary light target is off' 
    except Exception, e: 
     if not hasattr(c, 'current_server') or not c.current_server: 
      print 'BinaryLight device not set.Please use set_light <n>' 
     else: 
      print 'Error in get_target():', e 

def _stop(c): 
    """ Stop searching 
    """ 

    c.stop_search() 

def _list_devices(c): 
    """ Lists the devices that are in network. 
    """ 

    k = 0 
    for d in c.get_devices().values(): 
     print 'Device no.:', k 
     print 'UDN:', d.udn 
     print 'Name:', d.friendly_name 
     print 'Device type:', d.device_type 
     print 'Services:', d.services.keys() # Only print services name 
     print 'Embedded devices:', [dev.friendly_name for dev in \ 
      d.devices.values()] # Only print embedded devices names 
     print 

     k += 1 





if __name__ == '__main__': 

    main() 
+0

請格式化(我會這樣做,但我沒有編輯權限) – 2011-02-14 21:04:31

+0

請給出更多細節。不清楚你的問題是什麼。 – Ikke 2011-02-14 21:05:02

回答

0

嗯,我不認爲逗號做你想要的。如果dev.udn是一個字符串,你可以做打印「富」 + dev.udn,但除此之外,你想用C風格%符號,因此,例如,如果dev.udn是一個int:

print 'Got new device: %d' % dev.udn 
相關問題