Let's make a class that returns a random number for its hash.
>>> import random
>>> class DictDestroyer(object):
... def __hash__(self): return random.randint(0, 2**31)
...
>>> d = DictDestroyer()
>>> hash(d)
520880499
>>> hash(d)
793941724
Now, DictDestroyer lives up to its name:
>>> a = {d:1, d:2, d:3}
>>> import pprint
>>> pprint.pprint(a)
{<__main__.DictDestroyer object at 0x00CE8E90>: 1,
<__main__.DictDestroyer object at 0x00CE8E90>: 2,
<__main__.DictDestroyer object at 0x00CE8E90>: 3}
What is this? One key has multiple values? What happens when data is fetched?
>>> a[d]
1
>>> a[d]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: <__main__.DictDestroyer object at 0x00CE8E90>
Inconsistent behavior. Some times it returns one of the values, other times it gives a key error.
The C source for python dictionaries holds the answer.
I'll have to update later when I figure it out.
Kind of like "hey guys, check it out you can just duct tape down the dead-man's switch on this power tool and use it one handed". In Python.
Thursday, June 23, 2011
Wednesday, June 22, 2011
cron-like functionality (sched module)
The Python standard library includes a handy little scheduler, with which you can do cron-like functionality. The following code will print the current time every 5 seconds forever.
>>> import sched
>>> import datetime
>>> import time
>>> s = sched.scheduler(time.time, time.sleep)
>>> def print_time():
... print datetime.datetime.now()
... s.enter(5, 1, print_time, ())
...
>>> print_time()
2011-06-22 17:56:41.262000
>>> s.run()
2011-06-22 17:56:46.668000
2011-06-22 17:56:51.669000
2011-06-22 17:56:56.669000
2011-06-22 17:57:01.669000
This is a simple example of continuation passing style -- where functions "schedule" each other to run at a later time rather than calling directly.
>>> import sched
>>> import datetime
>>> import time
>>> s = sched.scheduler(time.time, time.sleep)
>>> def print_time():
... print datetime.datetime.now()
... s.enter(5, 1, print_time, ())
...
>>> print_time()
2011-06-22 17:56:41.262000
>>> s.run()
2011-06-22 17:56:46.668000
2011-06-22 17:56:51.669000
2011-06-22 17:56:56.669000
2011-06-22 17:57:01.669000
This is a simple example of continuation passing style -- where functions "schedule" each other to run at a later time rather than calling directly.
Subscribe to:
Posts (Atom)