Skip to main content

Extension of Python set() which is able to synchronize sets of comparable objects

Project description

When synchronizing two collections of objects, you quickly end up with code like this:

old_coll = get_some_items()
new_coll = get_some_other_items()
old_coll_map = {get_the_id(i): i for i in old_coll}
new_coll_map = {get_the_id(i): i for i in new_coll}
only_in_old, only_in_new, outdated, updated = [], [], [], []
for k, old_item in old_coll_map.items():
    if k in new_coll_map:
        new_item = new_coll_map[k]
        old_changekey = get_the_changekey(old_item)
        new_changekey = get_the_changekey(new_item)
        if old_changekey > new_changekey:
            outdated.append(old_item)
            updated.append(new_item)
        elif new_changekey > old_changekey:
            outdated.append(new_item)
            updated.append(old_item)
    else:
        only_in_old.append(old_item)
# And we still haven't built the 'only_in_new' list...

SyncSet is an extension of the standard Python set() which supports this pattern with a one-liner:

only_in_old, only_in_new, outdated, updated = old_coll.diff(new_coll)

With SyncSet, you can easily do set operations on sets of mutable and immutable objects that, in addition to the normal unique ID of set members, have a changekey attribute (a timestamp, autoincrement value, revision ID, hash etc.). Via set operations and a custom diff() method, you can do one- or two-way synchronization of comparable object sets via the OneWaySyncSet and TwoWaySyncSet classes, respectively. Examples are syncing files, web pages, contacts or calendar items.

All standard set() and dict() methods are supported, except for a handful which raise UndefinedBehaviorError because the method doesn’t make sense (> operator, for example). Items in the set are required to implement the very simple interface SyncSetMember.

https://badge.fury.io/py/syncset.svg https://api.codacy.com/project/badge/Grade/a35900e707cc4b71b40745d7553c26df https://secure.travis-ci.org/ecederstrand/py-syncset.png https://coveralls.io/repos/github/ecederstrand/py-syncset/badge.svg?branch=

Usage

Let’s say we want to maintain a local copy of some web pages. We let the Last-Modified HTTP header decide when a page has changed. We’ll use date values in the following, for the sake of brevity.

Our URL caching code could have lots of extra functionality. Let’s assume here that our main class is WebPage.

First, we want to tell syncset what we consider a unique ID and a revision (changekey). We create a minimal wrapper class that inherits SyncSetMember and makes url the unique ID and last_modified the changekey.

import syncset
from datetime import date


class WebPage:
   def __init__(self, url, last_modified):
      self.url = url
      self.last_modified = last_modified
      self.body = ''

   def __repr__(self):
      return self.__class__.__name__ + repr((self.url, self.last_modified))


class SyncableWebPage(WebPage, syncset.SyncSetMember):
   def get_id(self):
      return self.url

   def get_changekey(self):
      return self.last_modified

We want to sync these URLs:

foo = "http://example.com/foo.html"
bar = "http://example.com/bar.html"
baz = "http://example.com/baz.html"

This is our outdated copy:

old_urls = syncset.OneWaySyncSet()
old_urls.add(SyncableWebPage(foo, date(2012, 1, 1)))
old_urls.add(SyncableWebPage(bar, date(2011, 12, 8)))

This is the server version, after fetching the latest Last-Modified header in an HTTP HEAD request:

new_urls = syncset.OneWaySyncSet()
new_urls.add(SyncableWebPage(foo, date(2016, 2, 1)))
new_urls.add(SyncableWebPage(bar, date(2011, 12, 8)))
new_urls.add(SyncableWebPage(baz, date(2012, 2, 15)))

Now, let’s find the difference between the two. diff() returns four SyncSet objects:

only_in_old, only_in_new, outdated_in_old, updated_in_new = old_urls.diff(new_urls)
print(only_in_old)
OneWaySyncSet([])
print(only_in_new)

OneWaySyncSet(
  [SyncableWebPage('http://mysrv/baz.html', datetime.date(2012, 2, 15))]
)

print(outdated_in_old)

OneWaySyncSet(
  [SyncableWebPage('http://mysrv/foo.html', datetime.date(2012, 1, 1))]
)

print(updated_in_new)

OneWaySyncSet(
  [SyncableWebPage('http://mysrv/foo.html', datetime.date(2012, 2, 1))]
)

As you can see, foo needs to be updated, bar is unchanged and baz is new on the server. After issuing HTTP GET requests on foo and baz to get the updated content, let’s update the local copy:

old_urls.update(new_urls)
print(old_urls)

OneWaySyncSet([
  SyncableWebPage('http://example.com/foo.html', datetime.date(2016, 2, 1)),
  SyncableWebPage('http://example.com/bar.html', datetime.date(2011, 12, 8)),
  SyncableWebPage('http://example.com/baz.html', datetime.date(2012, 2, 15))
])

This updates foo and adds baz.

Similarly, a TwoWaySyncSet class exists that implements two-way synchronization. Both versions implement all the normal set() operations, using either one-way or two-way synchronization logic.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

syncset-2.0.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

syncset-2.0.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file syncset-2.0.0.tar.gz.

File metadata

  • Download URL: syncset-2.0.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.5.2

File hashes

Hashes for syncset-2.0.0.tar.gz
Algorithm Hash digest
SHA256 24fe3028714cafa414bb357d5b502a2625dabef53ec5eacb784cf1dc156763ea
MD5 9031fb5d1d5d2dc7607e13c9ddcff27a
BLAKE2b-256 df3d9b9c0e72e56557cf591da709ac82109cdf8f7b91fca8b059b4dec2a42fbf

See more details on using hashes here.

File details

Details for the file syncset-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: syncset-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.5.2

File hashes

Hashes for syncset-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0964e0d3486792138af5d0415c8c4d8f2a9df0c685496faf89191aac93be9c0f
MD5 530cd5aeaea2771af64294603f3f9505
BLAKE2b-256 f9c6c7a5efaf0696ca0754c479dcc2f39d03c247690fe3ee9dc95722bfa9309b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page