From 7b1b1619dadfe5dc3c0910c72054e247d54db2bd Mon Sep 17 00:00:00 2001 From: Cameron Dale Date: Tue, 8 Jan 2008 14:48:21 -0800 Subject: [PATCH] Switch from the time module to the datetime module. --- apt_dht_Khashmir/actions.py | 4 +--- apt_dht_Khashmir/db.py | 11 +++++------ apt_dht_Khashmir/khashmir.py | 10 ++++++---- apt_dht_Khashmir/ktable.py | 6 +++--- apt_dht_Khashmir/node.py | 6 +++--- 5 files changed, 18 insertions(+), 19 deletions(-) diff --git a/apt_dht_Khashmir/actions.py b/apt_dht_Khashmir/actions.py index 4214e4e..860205c 100644 --- a/apt_dht_Khashmir/actions.py +++ b/apt_dht_Khashmir/actions.py @@ -1,8 +1,6 @@ ## Copyright 2002-2004 Andrew Loewenstern, All Rights Reserved # see LICENSE.txt for license information -from time import time - from twisted.internet import reactor from khash import intify @@ -263,7 +261,7 @@ class KeyExpirer: self.next_expire = reactor.callLater(self.config['KEINITIAL_DELAY'], self.doExpire) def doExpire(self): - self.store.expireValues(time() - self.config['KE_AGE']) + self.store.expireValues(self.config['KE_AGE']) self.next_expire = reactor.callLater(self.config['KE_DELAY'], self.doExpire) def shutdown(self): diff --git a/apt_dht_Khashmir/db.py b/apt_dht_Khashmir/db.py index 809885a..547fb81 100644 --- a/apt_dht_Khashmir/db.py +++ b/apt_dht_Khashmir/db.py @@ -1,5 +1,5 @@ -from time import time +from datetime import datetime, timedelta from pysqlite2 import dbapi2 as sqlite import os @@ -81,14 +81,13 @@ class DB: def storeValue(self, key, value): """Store or update a key and value.""" - t = "%0.6f" % time() c = self.conn.cursor() - c.execute("INSERT OR REPLACE INTO kv VALUES (?, ?, ?)", (key, value, t)) + c.execute("INSERT OR REPLACE INTO kv VALUES (?, ?, ?)", (key, value, datetime.now())) self.conn.commit() - def expireValues(self, expireTime): - """Expire older values than expireTime.""" - t = "%0.6f" % expireTime + def expireValues(self, expireAfter): + """Expire older values after expireAfter seconds.""" + t = datetime.now() - timedelta(seconds=expireAfter) c = self.conn.cursor() c.execute("DELETE FROM kv WHERE time < ?", (t, )) self.conn.commit() diff --git a/apt_dht_Khashmir/khashmir.py b/apt_dht_Khashmir/khashmir.py index be60243..e48e702 100644 --- a/apt_dht_Khashmir/khashmir.py +++ b/apt_dht_Khashmir/khashmir.py @@ -4,7 +4,7 @@ import warnings warnings.simplefilter("ignore", DeprecationWarning) -from time import time +from datetime import datetime, timedelta from random import randrange from sha import sha import os @@ -37,7 +37,6 @@ class KhashmirBase(protocol.Factory): self.udp = krpc.hostbroker(self) self.udp.protocol = krpc.KRPC self.listenport = reactor.listenUDP(self.port, self.udp) - self.last = time() self._loadRoutingTable() self.expirer = KeyExpirer(self.store, config) self.refreshTable(force=1) @@ -115,7 +114,9 @@ class KhashmirBase(protocol.Factory): method needs to be a properly formed Node object with a valid ID. """ old = self.table.insertNode(n, contacted=contacted) - if old and (time() - old.lastSeen) > self.config['MIN_PING_INTERVAL'] and old.id != self.node.id: + if (old and old.id != self.node.id and + (datetime.now() - old.lastSeen) > + timedelta(seconds=self.config['MIN_PING_INTERVAL'])): # the bucket is full, check to see if old node is still around and if so, replace it ## these are the callbacks used when we ping the oldest node in a bucket @@ -173,7 +174,8 @@ class KhashmirBase(protocol.Factory): pass for bucket in self.table.buckets: - if force or (time() - bucket.lastAccessed >= self.config['BUCKET_STALENESS']): + if force or (datetime.now() - bucket.lastAccessed > + timedelta(seconds=self.config['BUCKET_STALENESS'])): id = newIDInRange(bucket.min, bucket.max) self.findNode(id, callback) diff --git a/apt_dht_Khashmir/ktable.py b/apt_dht_Khashmir/ktable.py index c27d6c3..85abe4a 100644 --- a/apt_dht_Khashmir/ktable.py +++ b/apt_dht_Khashmir/ktable.py @@ -1,7 +1,7 @@ ## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved # see LICENSE.txt for license information -from time import time +from datetime import datetime from bisect import bisect_left from twisted.trial import unittest @@ -174,10 +174,10 @@ class KBucket: self.l = contents self.min = min self.max = max - self.lastAccessed = time() + self.lastAccessed = datetime.now() def touch(self): - self.lastAccessed = time() + self.lastAccessed = datetime.now() def getNodeWithInt(self, num): if num in self.l: return num diff --git a/apt_dht_Khashmir/node.py b/apt_dht_Khashmir/node.py index dabe08c..5f11e51 100644 --- a/apt_dht_Khashmir/node.py +++ b/apt_dht_Khashmir/node.py @@ -1,7 +1,7 @@ ## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved # see LICENSE.txt for license information -from time import time +from datetime import datetime, MINYEAR from types import InstanceType from twisted.trial import unittest @@ -15,7 +15,7 @@ class Node: """encapsulate contact info""" def __init__(self): self.fails = 0 - self.lastSeen = 0 + self.lastSeen = datetime(MINYEAR, 1, 1) self.id = self.host = self.port = '' def init(self, id, host, port): @@ -35,7 +35,7 @@ class Node: return self def updateLastSeen(self): - self.lastSeen = time() + self.lastSeen = datetime.now() self.fails = 0 def msgFailed(self): -- 2.39.5