Skip to main content

This module calculates the difference from one string to another. If the origin string absorbs the difference, it becomes the other string. Thus, two endpoints could synchronize the strings by passing the difference.

Project description

StrDiffSynch

This module calculates the difference from one string to another. If the origin string absorbs the difference, it becomes the other string.

Thus, two endpoints could synchronize the strings by passing the difference.


Install · Usage ·


Install

StrDiffSynch in PyPI

pip install StrDiffSynch

Usage

StrDiff

One of two strings can change into the other when absorbing the difference from it to the other.

import json
from StrDiffSynch import StrDiff

data1 = json.dumps({"name": "davis", "other": {"age": 18}})
data2 = json.dumps({"name": "davis", "age": 18})
diff = StrDiff(data1, data2)
assert data1 + diff == data2
assert diff + data1 == data2

SynchBox

This class is used for synchronizing big string but not big enough to have to be stored in a database, among two endpoints. We will demonstrate with codes. You can type them in python console.

import asyncio

from StrDiffSynch import SynchBox

At endpoint A, set the SynchBox to contain the string.

synch_boxA = SynchBox('This is big data.')

Update the containing string.

synch_boxA.local_str = 'This is another big data.'

At endpoint B, set the SynchBox to contain a empty string.

synch_boxB = SynchBox('')

Get the string hash at endpoint B.

B_str_hash = synch_boxB.local_str_hash

Now endpoint A is commanded to synchronize endpoint B. Endpoint A needs to know the string hash at endpoint B, to calculate the difference data.

B_synch_data = synch_boxA.handle_remote_synch_command(B_str_hash)

Because no synchronization has happened, 'B_synch_data' would be the raw big string, 'This is another big data.' as demonstration.

Endpoint B gets 'B_synch_data' to synchronize itself.

synch_boxB.handle_local_synch_command(B_synch_data)
assert 'This is another big data.' == B_synch_data == synch_boxB.local_str

Now repeat to synchronize to see what will happen after the initial synchronization.

Well now that initial synchronization has happened, SynchBox will try to find the difference information between these two copy , which is usually much smaller than the raw string.

B_str_hash = synch_boxB.local_str_hash
B_synch_data = synch_boxA.handle_remote_synch_command(B_str_hash)
assert type(B_synch_data) != str and any(['h' == i[0] for i in B_synch_data])

At this step,there is nothing to change in fact.

old_B_local_str = synch_boxB.local_str
synch_boxB.handle_local_synch_command(B_synch_data)
assert synch_boxB.local_str == synch_boxA.local_str == old_B_local_str

Now repeat once more. Let's make some change at endpoint A.

synch_boxA.local_str += 'u28dy2'
B_str_hash = synch_boxB.local_str_hash
B_synch_data = synch_boxA.handle_remote_synch_command(B_str_hash)
assert type(B_synch_data) != str and any(['h' == i[0] for i in B_synch_data])
old_str = synch_boxB.local_str

Apply the change.

synch_boxB.handle_local_synch_command(B_synch_data)
assert synch_boxB.local_str == synch_boxA.local_str != old_str

Vice versa, endpoint B could synchronizes endpoint A. Let's make some change at endpoint B.

synch_boxB.local_str = synch_boxB.local_str[0:3] + synch_boxB.local_str[-3:]
str_hash = synch_boxA.local_str_hash
synch_data = synch_boxB.handle_remote_synch_command(str_hash)
assert type(synch_data) != str and any(['h' == i[0] for i in synch_data])
old_str = synch_boxA.local_str
synch_boxA.handle_local_synch_command(synch_data)
assert synch_boxB.local_str == synch_boxA.local_str != old_str

Well, everything is OK so far. However there is a possibility that the synchronization data can not be applied.

B_str_hash = synch_boxB.local_str_hash
B_synch_data = synch_boxA.handle_remote_synch_command(B_str_hash)
assert type(B_synch_data) != str and any(['h' == i[0] for i in B_synch_data])

Before the synchronization data comes, the string with the hash value B_str_hash changes for some reason.

synch_boxB.local_str = 'Hello world'

The coming data can't be handled.

try:
    synch_boxB.handle_local_synch_command(B_synch_data)
except AssertionError:
    print('Fail to handle_local_synch_command')

If you want to automatically handle this bad situation, you can pass the keyword parameter "strdiff_add_error_handler" to "handle_local_synch_command", to be called to fetch the raw string. For example,

def fetch_raw_string():
    return requests.get('http://www.baidu.com/raw-string')

We will demonstrate an easy example.

def fetch_raw_string():
    return synch_boxA.local_str


synch_boxB.handle_local_synch_command(B_synch_data, fetch_raw_string)
assert synch_boxB.local_str == synch_boxA.local_str

If you are using this module in a coroutine function, you can pass a coroutine function as the error handler to request. For example:

async def fetch_raw_string():
    async with aiohttp.ClientSession().get('http://www.baidu.com/raw-string') as r:
        return await r.text()

We will demonstrate an easy example:

async def fetch_raw_string():
    return synch_boxA.local_str


async def main():
    handle_local_synch_command_res = synch_boxB.handle_local_synch_command(B_synch_data, fetch_raw_string)
    # try to await the result only when necessary.
    if type(handle_local_synch_command_res) == asyncio.Task:
        await handle_local_synch_command_res
    assert synch_boxB.local_str == synch_boxA.local_str


asyncio.get_event_loop().run_until_complete(main())

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

StrDiffSynch-2.2.6.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

StrDiffSynch-2.2.6-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file StrDiffSynch-2.2.6.tar.gz.

File metadata

  • Download URL: StrDiffSynch-2.2.6.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.7.7

File hashes

Hashes for StrDiffSynch-2.2.6.tar.gz
Algorithm Hash digest
SHA256 4b7a270c920058bf64a931746293fd6ef56e611860ad55e1859ebf78f32bf2bf
MD5 f53abb36df296ce18fa62890bf4d1a15
BLAKE2b-256 b3c289a08aaf54b8f74f00840f1c67835cfd1210c0353886c52f45095d71b6dc

See more details on using hashes here.

File details

Details for the file StrDiffSynch-2.2.6-py3-none-any.whl.

File metadata

  • Download URL: StrDiffSynch-2.2.6-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.7.7

File hashes

Hashes for StrDiffSynch-2.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 5f20c1158f597e734684670ccf3d84db7cc371bab7f7dec35a03d1946d9d12c9
MD5 397b98e27013ef8203807dcc45984d6b
BLAKE2b-256 0658df7fc6dbf6d214dbf7a089a543ad28e74c5649cac59acb58df3c0dbddecf

See more details on using hashes here.

Supported by

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