python - 如果有条件,模块对象在 sleep 状态下不可调用(python)

标签 python import

我在 python 脚本的顶部导入了以下模块:

import os
import sys
import time
import datetime
import random
import pprint
from random import randint
from time import sleep
from datetime import datetime, timedelta

但是,我的这部分代码仍然遇到模块对象错误:

def check(low,high):
    with open('done.txt', 'r+') as done:
        ts = time.time()
        sttime = datetime.fromtimestamp(ts).strftime('%Y%m%d_%H:%M:%S - ')
        done_completed = open('done_completed.txt', "a")
        for line in done:
            now = datetime.now()
            now_time = now.time()
            if now_time >= time(23,30) and now_time <= time(06,30):
                print "sleeping"
                sleep(5000)
            else:
                done_id = line.strip()[20:]
                then = datetime.strptime(line.strip()[:17], '%Y%m%d_%H:%M:%S')

(在所有这些下面还有另一个 if elif elif,但我认为它与错误无关)

(是的,我是Python初学者)

错误是:

  File "/home/joe/Desktop/follomatic/follomatic.py", line 85, in check
if now_time >= time(23,30) and now_time <= time(06,30):
TypeError: 'module' object is not callable

有人看出哪里出了问题吗?我是否没有指定以正确的方式调用模块?

谢谢:)

最佳答案

Python 的 datetime 模块有点像老鼠巢。 :) 如果您使用 from 导入,可能会变得更加困惑。

这段代码应该可以澄清一些事情。请注意,这不是一种很好的做事方式。通常最好使用 datetime.datetime 对象而不是 datetime.time 对象,否则如果您需要处理包括午夜(或多天)的时间间隔,事情会变得困惑。但我编写这段代码是为了大致对应您问题中的代码,并且我想让它可以在一天中的任何时间轻松测试。

    #!/usr/bin/env python

    ''' datetime.time manipulation demo

        From http://stackoverflow.com/q/28605732/4014959

        Written by PM 2Ring 2015.02.19
    '''

    from time import sleep
    import datetime

    now = datetime.datetime.now()
    now_time = now.time()

    #A datetime.time example
    t1 = datetime.time(5, 30)
    print 't1 is', t1

    now = datetime.datetime.now()
    now_time = now.time()
    print 'now is %s, now_time is %s' % (now, now_time)

    begin_datetime = now + datetime.timedelta(seconds=5)
    end_datetime = now + datetime.timedelta(seconds=10)

    begin_time = begin_datetime.time()
    end_time = end_datetime.time()

    print 'begin', begin_time
    print 'end', end_time

    for i in xrange(15):
        now_time = datetime.datetime.now().time()
        print now_time, begin_time <= now_time <= end_time
        sleep(1)

典型输出

t1 is 05:30:00
now is 2015-02-19 23:44:39.152786, now_time is 23:44:39.152786
begin 23:44:44.152786
end 23:44:49.152786
23:44:39.152905 False
23:44:40.153999 False
23:44:41.155191 False
23:44:42.156398 False
23:44:43.156614 False
23:44:44.157810 True
23:44:45.159028 True
23:44:46.160231 True
23:44:47.161444 True
23:44:48.162660 True
23:44:49.163869 False
23:44:50.165076 False
23:44:51.166650 False
23:44:52.167842 False
23:44:53.169053 False

关于python - 如果有条件,模块对象在 sleep 状态下不可调用(python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28605732/

相关文章:

python - Python 中多个项目的列表按值排序

python - 字符串在表中每隔 n 个空格分割一行

python - 如何用条件逐行替换

python - 在带有 Python 的 Tk 中,我如何指定一个框架或 Canvas 以随我的窗口调整大小?

java - 为什么我不必导入一些类?

database - 如何删除 Magento 中的所有目录产品

hadoop - 如何使用Sqoop仅导入新数据?

python - 如何从列名中删除非 ASCII 字符和空格

python - 当模块名称中包含 '-' 破折号或连字符时如何导入模块?

python - 为什么 pytest 总是说 "ImportError: attempted relative import with no known parent package"