【问题标题】:Python - String with timestamp changingPython - 时间戳更改的字符串
【发布时间】:2015-04-14 23:38:43
【问题描述】:

我正在尝试使用 datetime 包在 Python 中初始化一个字符串变量。我希望该字符串在初始化后保持不变,但出于某种奇怪的原因(至少对我来说很奇怪),它正在发生变化。

基本上,当我尝试时:

timestamp = datetime.datetime.now().strftime('%m-%d-%Y.%I_%M_%S_%p')
base_directory = "C:\\Users\\Ben\\report_" + timestamp + "\\"
print base_directory #prints C:\Users\Ben\report_02-13-2015_02_02_24_PM
time.sleep(5)
logs_directory = base_directory + "logs\\"

当我回去使用 logs_directory 变量时,我希望它是

C:\Users\Ben\report_02-13-2015_02_02_24_PM\logs\

然而,它却是:

C:\Users\Ben\report_02-13-2015_02_02_29_PM\logs\ #!!!

就好像当我访问日志目录变量时,它会返回并再次重新评估 base_directory 变量,而不是检索已经设置的值 base_directory。这是 Python 中的预期功能吗?我不是,因为我似乎无法从 IDLE shell 触发这个问题——它只发生在我从 pydev 运行我的 Python 程序时。但是,如果这是预期的功能,我该如何重写我的代码以达到我期望的结果?谢谢!

我不确定它是否相关,但我正在鼻子单元测试中访问这个变量。这是在 Python 2.7 中。


更新:这里有一些独立代码可以更好地说明正在发生的事情。事实上,它看起来确实在某种程度上与鼻子有关:

import nose, os, sys, time

timestamp = time.strftime('%m-%d-%Y.%I_%M_%S_%p')
base_directory = "C:\\Users\\Ben\\Desktop\\report_" + timestamp + "\\"
logs_directory = base_directory + "logs\\"

def test_me():
    print base_directory
    print logs_directory

if __name__ == '__main__':
    os.makedirs(base_directory)
    os.makedirs(logs_directory)
    print base_directory
    print logs_directory

    time.sleep(5)

    #Without the nocapture argument, nose will internally log all
    #stdout output, and will not output anything to the console.
    argv = (sys.argv[:])
    argv.insert(1, "--nocapture")
    nose.main(argv=argv)  #This will run test_me()

这是输出:

C:\Users\Ben\Desktop\report_02-13-2015.04_27_32_PM\
C:\Users\Ben\Desktop\report_02-13-2015.04_27_32_PM\logs\
C:\Users\Ben\Desktop\report_02-13-2015.04_27_37_PM\
C:\Users\Ben\Desktop\report_02-13-2015.04_27_37_PM\logs\

我期待:

C:\Users\Ben\Desktop\report_02-13-2015.04_27_32_PM\
C:\Users\Ben\Desktop\report_02-13-2015.04_27_32_PM\logs\
C:\Users\Ben\Desktop\report_02-13-2015.04_27_32_PM\
C:\Users\Ben\Desktop\report_02-13-2015.04_27_32_PM\logs\

发生了什么事?为什么鼻子重新评估已经初始化的字符串?如何重写我的代码以使其达到我的预期?谢谢!

【问题讨论】:

  • 这绝对不是正常行为。您不会两次调用该函数吗?
  • 不,我不是。这些是在代码中设置两个目录变量的唯一位置。这里唯一需要注意的是,我正在从一个鼻子单元测试访问 logs_directory 变量(变量在单元测试运行之前被初始化)。我不确定鼻子中是否有某种伪影,或者我的项目设置是否会导致此问题发生。我对 Python 中的鼻子测试非常陌生。
  • 字符串在 Python 中是不可变的。 base_directory 不会改变,除非你给它分配一个新的字符串。无关:如果您不使用仅由 datetime 模块支持的指令,则可以使用 time.strftime('%m-%d-%Y.%I_%M_%S_%p')。使用常规的python 解释器来运行您的脚本。如果问题仍然存在,那么provide the shortest program that demonstrates it
  • @benjammin,这很可能与您在 pydev 中使用鼻子有关,因为字符串无法自​​行更新。

标签: python python-2.7 timestamp python-import nose


【解决方案1】:

我可以重现该问题:

#!/usr/bin/env python
import time

timestamp = time.strftime('%S')

def test_me():
    print('test_me ' + timestamp)
    assert 0

if __name__ == '__main__':
    import nose # $ pip install nose
    print('main ' + timestamp)
    time.sleep(3)
    nose.main()

print('main ' + timestamp)print('test_me ' + timestamp) 显示不同的结果。

nose 在睡眠后内部导入模块,因此timestamp 不同:实际上有两个对象__main__.timestampmodule_name.timestamp

如果模块作为脚本运行并且可以导入,则该模块不应具有可变的全局状态。见Executing the main module twice

这是一个没有nose的例子:

#!/usr/bin/env python
"""Execute main module twice."""
import time

timestamp = time.strftime('%S')

def print_timestamp(prefix):
    print(prefix + timestamp)

if __name__ == '__main__':
    print_timestamp('main')
    time.sleep(3)
    import test_exec_main_twice
    test_exec_main_twice.print_timestamp('module')

将其保存到test_exec_main_twice.py 文件并运行。

【讨论】:

  • 知道了。我不知道鼻子在幕后重新导入模块。我将重构我的代码,以避免这种可怕的双重导入。谢谢!
  • @benjammin: nose 如果它们在其他模块中,则必须导入测试(nosetests 所做的),但我不确定它是否是重新导入运行 @ 的模块的错误987654334@。一个简单的解决方法:不要将测试放入带有nose.main() 的脚本中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-19
  • 1970-01-01
  • 2022-11-04
  • 2015-04-19
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多