Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 99 lines (75 sloc) 2.61 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
# -*- coding: utf-8 -*-
#
# Lazyboy examples
#
# © 2009 Digg, Inc. All rights reserved.
# Author: Ian Eure <ian@digg.com>
#
# This example assumes the following schema:
#
# <Keyspaces>
# <Keyspace Name="UserData">
# <ColumnFamily CompareWith="BytesType" Name="Users"/>
# </Keyspace>
# </Keyspaces>
#


from lazyboy import *
from lazyboy.key import Key


# Define your cluster(s)
connection.add_pool('UserData', ['localhost:9160'])

# This can be used for convenience, rather than repeating the Keyspace
# and CF.
class UserKey(Key):
    """This is a key class which defaults to storage in the Users CF
in the UserData Keyspace."""
    def __init__(self, key=None):
        Key.__init__(self, "UserData", "Users", key)



# Subclass Record to create an object of the correct type.
class User(record.Record):
    """A class representing a user in Cassandra."""

    # Anything in here _must_ be set before the object is saved
    _required = ('username',)

    def __init__(self, *args, **kwargs):
        """Initialize the record, along with a new key."""
        record.Record.__init__(self, *args, **kwargs)
        self.key = UserKey()


# Create an empty object
u = User()

# Set the key. A UUID will be generated for you
print u.key
# -> {'keyspace': 'UserData', 'column_family': 'Users',
# 'key': 'da6c8e19174f40cfa6d0b65a08eef62f',
# 'super_column': None}

# If you want to store records in a SuperColumn, set key.super_column:
superkey = u.key.clone(super_column="scol_a")

data = {'username': 'ieure', 'email': 'ian@digg.com'}

# The object is a dict. All these are equivalent.
u.update(data)
u.update(data.items())
u.update(**data)
for k in data:
    u[k] = data[k]

# Arguments to __init__ are passed to update()
u_ = User(data)
print u_ # -> {'username': 'ieure', 'email': 'ian@digg.com'}

# You can see if it's been modified.
print u.is_modified() # -> True

# Save to Cassandra
u.save() # -> {'username': 'ieure', 'email': 'ian@digg.com'}

print u.is_modified() # -> False

# Load it in a new instance.
#u_ = User().load(key)
u_ = User().load(u.key.clone()) # PATCH

print u_ # -> {'username': 'ieure', 'email': 'ian@digg.com'}

print u.is_modified() # -> False
del u['username']
print u.valid() # -> False
print u.missing() # -> ('username',)
try:
    u.save() # -> ('Missing required field(s):', ('username',))
except Exception, e:
    print e

# Discard modifications
u.revert()
print u.is_modified() # -> False
print u.valid() # -> True
Something went wrong with that request. Please try again.