簡體   English   中英

跨平台Python代碼

[英]Cross platform Python code

編輯

除了下面的其他信息外,並且由於提供了一些有用的輸入,此問題是由於使用strftime%s生成Unix時間戳(這是正在查詢的系統所需要的)引起的。 Strftime%s與Windows平台不兼容,因此我需要使用另一種方法來生成Unix時間戳。 Jez建議了我已經嘗試過的time.time(),但顯然我沒有在整個過程中的某處都做。 我需要將這部分代碼從使用strftime更改為time():

    if (args.startdate):
       from_time=str(int(args.startdate.strftime('%s')) * 1000)
    if (args.enddate):
       to_time=str(int(args.enddate.strftime('%s')) * 1000)

任何幫助或引導非常感謝。 :)

/編輯

我得到了一個Python腳本,該腳本在Apple筆記本電腦上部署時似乎可以正常運行,但是在Windows計算機上運行時卻顯示錯誤消息。 我需要通過遠程執行文件與第三方交談,他只有一台Windows計算機,因此我需要嘗試弄清楚為什么它無法正常工作。 我正在運行2.7供參考。 此部分似乎是導致錯誤的地方:

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-s', "--startdate", help="The start date (included)- format YYYY-MM-DD -- if not specified then earliest available data", type=valid_date)
parser.add_argument('-e', "--enddate", help="The end date (excluded) - format YYYY-MM-DD  -- if not specified then 'now'", type=valid_date)
parser.add_argument('-u', "--user", help="User name to use", default='admin')
parser.add_argument('-p', "--password", help="Password for user", default='redwood')
parser.add_argument('-a', "--attribute", help="Attribute", choices=['temperature', 'motion', 'input-power', 'output-power'], default='motion')
parser.add_argument('host', help='Hostname/IP address of engine to connect to', default='127.0.0.1')
args = parser.parse_args()
    user = args.user    
    passwd = args.password
    if (args.startdate):
       from_time=str(int(args.startdate.strftime('%s')) * 1000)
    if (args.enddate):
       to_time=str(int(args.enddate.strftime('%s')) * 1000)

    scale=1
    unit='seconds since 1.1.1970'
    if (args.attribute == 'temperature'):
       path=temperature_path
       scale = 100
       unit = "Degrees Celsius"
    elif (args.attribute == 'output-power'):
       path=opower_path
       scale = 100
       unit = "Watts"
    elif (args.attribute == 'input-power'):
       path=ipower_path
       scale = 1000
       unit = "Watts"
    else:
       path=motion_path
    print "Epoch Time, Local Time (this machine), Attribute, Value (%s) " % unit
query_stats(args.host)

這是我用來執行的命令:

C:\Python27>python stats_query.py -s 2016-03-18 -e 2016-03-19 -u admin -p admin -a motion 192.168.2.222

這是我收到的錯誤消息:

Traceback (most recent call last):
File "stats_query.py", line 132, in <module>
from_time=str(int(args.startdate.strftime('%s')) * 1000)
ValueError: Invalid format string

如果有人有任何想法,我將不勝感激任何反饋。 抱歉,如果我要問一個愚蠢的問題-我對Python真的不是很熟悉。

如果要在幾秒鍾內打印出來 ,請使用%S (大寫S )。

(可能是評論)

正如其他人已經提到的那樣,並非所有指令都可在所有平台上使用。

Python的文檔列出了所有跨平台的指令: https : //docs.python.org/2/library/datetime.html#strftime-strptime-behavior

不確定函數中的有效時間是什么,但是如果使用類似以下內容的方法:

def valid_time(t):
    format = "%Y-%m-%d"
    datetime.datetime.strptime(t, format)

它超越了您正在描述的問題...之后,由於未定義motion_path ,因此我看到了其他問題。

更新 :以下代碼在使用Python 2.7的Windows7 Professional上運行(文件另存為tryme.py

import argparse
import datetime

def motion_path():
  print "Got here.. no issues"

def query_stats(host):
  print "We'll query the host: %s" % host

def valid_time(t):
  format = "%Y-%m-%d"
  return datetime.datetime.strptime(t, format)

if __name__ == "__main__":
  parser = argparse.ArgumentParser()
  parser.add_argument('-s', "--startdate", help="The start date (included)- format YYYY-MM-DD -- if not specified then earliest available data", type=valid_time)
  parser.add_argument('-e', "--enddate", help="The end date (excluded) - format YYYY-MM-DD  -- if not specified then 'now'", type=valid_time)
  parser.add_argument('-u', "--user", help="User name to use", default='admin')
  parser.add_argument('-p', "--password", help="Password for user", default='redwood')
  parser.add_argument('-a', "--attribute", help="Attribute", choices=['temperature', 'motion', 'input-power', 'output-power'], default='motion')
  parser.add_argument('host', help='Hostname/IP address of engine to connect to', default='127.0.0.1')
  args = parser.parse_args()
  user = args.user  
  epoch = datetime.datetime.utcfromtimestamp(0)
  passwd = args.password
  if (args.startdate):
     from_time=str(int((args.startdate-epoch).total_seconds()))
  if (args.enddate):
     to_time=str(int((args.enddate-epoch).total_seconds()))

  print 'From: %s\nTo: %s\n' %(from_time, to_time)

  scale=1
  unit='seconds since 1.1.1970'
  if (args.attribute == 'temperature'):
     path=temperature_path
     scale = 100
     unit = "Degrees Celsius"
  elif (args.attribute == 'output-power'):
     path=opower_path
     scale = 100
     unit = "Watts"
  elif (args.attribute == 'input-power'):
     path=ipower_path
     scale = 1000
     unit = "Watts"
  else:
     path=motion_path
  print "Epoch Time, Local Time (this machine), Attribute, Value (%s) " % unit
  query_stats(args.host)

用於運行它的命令:

C:\Python27>python tryme.py -s 2016-03-18 -e 2016-03-19 -u admin -p admin -a motion 192.168.2.222

結果:

Epoch Time, Local Time (this machine), Attribute, Value (seconds since 1.1.1970)
We'll query the host: 192.168.2.222

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM