import pickle pickle.dump("abcdefg", open("pickle_test", "wb")) print open("pickle_test", "r").read() data1 = {'a': [1, 2.0, 3, 4+6j], 'b': ('string', u'Unicode string'), 'c': None} pickle.dump(data1, open('data.pkl', 'wb')) data2 = pickle.load(open('data.pkl', 'rb')) data1 == data2 class PicklePerson(object): def __init__(self, name, age, location): self.name = name self.age = age self.location = location def __repr__(self): return "name: " + self.name + "\n" + "age: " + self.age + \ "\n" + "location: " + self.location todd = PicklePerson("Todd", "30", "Raleigh") print todd pickle.dump(todd, open("pickle_todd", "wb")) recovered_todd = pickle.load(open("pickle_todd","r")) recovered_todd def f(x): return x+1 pickle.dump(f, open("pickle_good","wb")) try: with open("pickle_bad","wb") as f: pickle.dump(lambda x: x+1, f) except pickle.PicklingError: print "Can't pickle :-(" class NotPickable(object): def __init__(self, x): self.attr = x o = NotPickable(open('Pickle and Redis.ipynb', 'r+w')) try: with open("pickle_bad","wb") as f: pickle.dumps(o) except TypeError: print "Can't pickle :-(" import cPickle, os %timeit pickle.dump([data1 for x in range(1000)], open("pickle_todd", "wb")) %timeit cPickle.dump([data1 for x in range(1000)], open("pickle_todd", "wb")) os.path.getsize('/Users/tdhopper/Dropbox/PyCarolinas 2012/pickle_todd') import redis r = redis.StrictRedis(host='localhost', port=6379, db=0) r.set('foo', 'bar') r.get('foo') %timeit r.set('foo', 'bar') %timeit r.get('foo') import string text = """ Redis typically holds the whole dataset in RAM. Versions up to 2.4 could be configured to use virtual memory but this is now deprecated. Persistence is reached in two different ways: One is called snapshotting, and is a semi-persistent durability mode where the dataset is asynchronously transferred from memory to disk from time to time, written in RDB dump format. Since version 1.1 the safer alternative is AOF, an append-only file (a journal) that is written as operations modifying the dataset in memory are processed. Redis is able to rewrite the append-only file in the background in order to avoid an indefinite growth of the journal.""" # Strip punctuation: http://stackoverflow.com/a/2402306/982745 text_list = [word.translate(None, string.punctuation) for word in text.split()] for word in text_list: r.delete(word) # In case these words are already in redis, delete them. for word in text_list: r.sadd("persistence", word) print [r.srandmember('persistence') for i in range(10)] # Get ten random words print [r.srandmember('persistence') for i in range(10)] # Get ten more random words for word in text_list: r.incr(word) # Print most used words in this document: for word in set(text_list): if int(r.get(word)) > 2: print word print "\t\t", r.get(word) bob = pickle.dumps(PicklePerson("bob","50","durham")) print bob r.set("bob", bob) pickle.loads(r.get("bob")) import redisco from redisco import connection_setup, models redisco.connection_setup(host='localhost', port=6379, db=0) class Person(models.Model): name = models.Attribute(required=True) age = models.Attribute(required=False) location = models.Attribute(required=False) for x in Person.objects.filter(name="Tim"): x.delete() tim_hopper = Person(name="Tim",age="26",location="Morrisville") tim_smith = Person(name="Tim",age="75",location="Chapel Hill") tim_hopper.save() tim_smith.save() Person.objects.filter(name="Tim") Person.objects.filter(name="Tim", age="26")[0] == tim_hopper