copycat

download

docs

Home

Welcome to copycat

Let's see how copycat works.

What copycat is?

copycat is an implementation of object prevalence in python.

It is based on the techniques of system snapshotting and transaction journalling. In the prevalent model, the object data is kept in memory in native object format, rather than being marshalled to an RDBMS or other data storage system. A snapshot of data is regularly saved to disk, and in addition to this, all changes are serialised and a log of transactions is also stored on disk.

http://en.wikipedia.org/wiki/Object_prevalence

copycat is a persistence tool which intercepts method calls, logging them into 'actions' to restore object state by re-executing these actions.

Using copycat

Every method invocation will be logged at disk. Not what you want? If so try copy cat decorators.

copycat decorators. Read-only - @readonly - methods doesn't need to be logged on disk for example.

How about thread-safety? copycat by default, provides thread safety using a reentran-lock for every single method invocation. Two methods can't be executed at the same time. If you want to customize this behaviour, check @unlocked decorator

If you are having problems you should check out common mistakes that happen when you first start working with this style of persistence.

Check out how to use Clock to avoid date problems

Only method invocations will be intercepted.

Client and Server architecture?

Check our solution

1 minute tutorial

Create some domain classes

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class WikiPage():
def __init__(self, id, content):
self.id = id
self.content = content
self.history = []
self.last_modify = datetime.datetime.now()


class Wiki():
def __init__(self):
self.pages = {}

def create_page(self, page_id, content):
page = None
if page_id in self.pages:
page = self.pages[page_id]
if not page:
page = WikiPage(page_id, content)
self.pages[page_id] = page
else:
update_page(page_id, content)

return page

Using copycat

1
2
3
4
5
wiki = pr.init_system(Wiki(), "/path/to/files")

wiki.create_page('Home','Welcome')
wiki.create_page('Pywiki','Sample text page')
wiki.create_page('Copycat','Visit: http://bitbucket.org/loogica/copycat')

When you call copycat again, all create_page executions in some other moment, will be re-executed to restore wiki state.

1
2
3
wiki = init_system(Wiki(), "/path/to/files")
for k in wiki.pages:
print k
output:

Home
Pywiki
Copycat

Now, you can use copycat passing object instance or class:

1
wiki = init_system(Wiki(), "/path/to/files")

or

1
wiki = init_system(Wiki, "/path/to/files")