import pickle
data1 = {'firstname': [1, 2.0, 3, 4],
'lastname': ('maiden', u'Smith'),
'account': None}
pickle.dump(data1, open('data.pkl', 'wb'))
data2 = pickle.load(open('data.pkl', 'rb'))
data1 == data2
True
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
john = PicklePerson("John", "60", "Smith")
print john
name: John age: 60 location: Smith
pickle.dump(john, open("pickle_john", "wb"))
zombie_john = pickle.load(open("pickle_john","r"))
zombie_john
name: John age: 60 location: Smith
import cPickle, os
%timeit pickle.dump([data1 for x in xrange(1000)], open("pickle_john", "wb"))
100 loops, best of 3: 5.57 ms per loop
%timeit cPickle.dump([data1 for x in xrange(1000)], open("pickle_john", "wb"))
1000 loops, best of 3: 1.48 ms per loop
os.path.getsize('/Users/antigen/Downloads/Pickle-and-Redis/pickle_john')
4100
import redis
r = redis.StrictRedis(host='localhost', port=6379, db=0)
r.set('foo', 'bar')
True
r.get('foo')
'bar'
%timeit r.set('foo', 'bar')
10000 loops, best of 3: 141 us per loop
%timeit r.get('foo')
10000 loops, best of 3: 123 us per loop
import string
text = """Pipelines are a subclass of the base Redis class that provide support
for buffering multiple commands to the server in a single request. They can be
used to dramatically increase the performance of groups of commands by reducing
the number of back-and-forth TCP packets between the client and server."""
for word in text.split():
r.sadd("persistent", word)
print [r.srandmember('persistent') for i in xrange(10)]
print [r.srandmember('persistent') for i in xrange(10)]
['p', 'P', 'increase', 'and', 'commands', 'for', 'single', 'f', 'a', 'u'] ['for', 'the', 'and', 'the', 'dramatically', 'be', 'f', 'R', 'used', 'packets']
for word in text.split():
r.incr(word)
for word in set(text.split()):
if int(r.get(word)) > 2:
print word
print "\t\t", r.get(word)
and 4 Redis 4 number 4 TCP 4 single 4 are 4 in 4 reducing 4 Pipelines 4 for 4 request. 4 provide 4 support 4 subclass 4 by 4 increase 4 to 8 They 4 between 4 performance 4 dramatically 4 be 4 used 4 multiple 4 that 4 server. 4 base 4 groups 4 buffering 4 class 4 a 9 commands 8 back-and-forth 4 of 16 packets 4 server 4 client 4 can 4 the 20
bob = pickle.dumps(PicklePerson("bob","50","smith"))
print bob
ccopy_reg _reconstructor p0 (c__main__ PicklePerson p1 c__builtin__ object p2 Ntp3 Rp4 (dp5 S'age' p6 S'50' p7 sS'name' p8 S'bob' p9 sS'location' p10 S'smith' p11 sb.
r.set("bob", bob)
True
pickle.loads(r.get("bob"))
name: bob age: 50 location: smith
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_smith = Person(name="Tim",age="40",location="Vancouver")
tim_smiths = Person(name="Tim",age="70",location="Burnaby")
tim_smiths.save()
tim_smith.save()
True
Person.objects.filter(name="Tim")
[<Person:4 {'age': u'70', 'name': u'Tim', 'location': u'Burnaby'}>, <Person:5 {'age': u'40', 'name': u'Tim', 'location': u'Vancouver'}>]
Person.objects.filter(name="Tim", age="40")[0] == tim_smith
True