delta没有在类中定义

时间:2017-02-28 19:57:58

标签: python django

class timetravel(object):
    """
    Fast forward in time with simulated CRON jobs (used for unit testing)
    """
    delta = None

def __init__(self, *args):
    self.initial_now = datetime.now()
    if isinstance(args[0], timedelta):
        self.delta = delta
    elif isinstance(args[0], datetime):
        self.now = args[0]
        if args[0] > self.initial_now:
            self.delta = args[0] - self.initial_now
        else:
            self.delta = self.initial_now - args[0]
    else:
        print("Please provide a datetime or timedelta object as argument")
   #self.now = self.initial_now
   #self.freezer = freeze_time(self.now)
   #self.freezer.start()

def __enter__(self):
    # Traveling in the past, we don't run cron jobs backward
    if self.now < self.initial_now:
        self.now = self.now - self.delta
        self.freezer = freeze_time(self.now)
        self.freezer.start()
    # Traveling in the future, run cron jobs
    else:
        self.now = self.initial_now
        self.freezer = freeze_time(self.now)
        self.freezer.start()
        hours   = self.delta.total_seconds() / 60 / 60
        weekday = self.now.weekday()
        month   = self.now.month
        year    = self.now.year
        for i in range(1, int(hours)+1):
            call_command('cron', hourly=True, verbosity=0)
            if weekday != self.now.weekday():
                weekday = self.now.weekday()
                call_command('cron', daily=True)
            if self.now.weekday() == 6 and weekday != self.now.weekday():
                call_command('cron', weekly=True)
            if self.now.month != month:
                month = self.now.month
                call_command('cron', monthly=True)
            if self.now.year != year:
                year = self.now.year
                call_command('cron', yearly=True)
            self.freezer.stop()
            self.now = self.now + timedelta(hours=1)
            self.freezer = freeze_time(self.now)
            self.freezer.start()
    return self.now

def __exit__(self, *args):
    self.freezer.stop()

如果我创建一个功能

 def function(self):

        with timetravel(datetime(2017,03,01)) as now:
        ...

当我运行python manage.py test时,我获得了

Traceback (most recent call last):
  File "...", line 42, in test_timetravel
    with timetravel(datetime.timedelta(days=2)) as now:
  File ".../utils/testing.py", line 23, in __init__
    self.delta = delta
NameError: global name 'delta' is not defined

我不能说它为什么不起作用。

  

2-我该如何解决这个问题?事实上,因为我是编程新手。那么什么是我们必须在timetravel中作为参数插入的一个很好的例子呢?

1 个答案:

答案 0 :(得分:0)

... // Get the root JsonObject JsonObject je = jsontree.getAsJsonObject(); // Get the property tableRows as a JsonArray JsonArray ja = je.getAsJsonArray("tableRows"); for (Object o : ja) { ... // Warning JSON is case sensitive so use mailContent instead of mailcontent String mail = person.get("mailContent").getAsString(); ... } 是一个类属性,可以从100 Test mail content 123 0 200 Test mail content 123 0 300 Test mail content 123 0 的实例访问。您可以覆盖此属性delta,然后它将成为实例属性。

因此,在您的代码中,您只需删除这些行:

self.delta