Skip to main content

One of two strings can change into the other when absorbing the difference among them. Remote strings could be synchronized through minimum data.

Project description

StrDiff

One of two strings can change into the other when absorbing the difference among them.

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.

import asyncio

from StrDiffSynch import SynchBox

# LOCAL
# Define a SynchBox instance to contain the string
synch_box = SynchBox('This is big data.')
# update the containing string
synch_box.local_str = 'This is another big data.'
# If a request with the remote string hash value comes to ask to synchronize the string at the remote endpoint, local endpoint handles the request.
# REMOTE
synch_box2 = SynchBox('')
remote_str_hash = synch_box2.local_str_hash
# request though the web
# LOCAL
remote_synch_data = synch_box.handle_remote_synch_command(remote_str_hash)
# response through the web
# REMOTE
# Because no synchronization has happened, remote_synch_data would be the raw big string, 'This is another big data.' as demonstration.
synch_box2.handle_local_synch_command(remote_synch_data)
assert 'This is another big data.' == remote_synch_data == synch_box2.local_str

# Now repeat
# REMOTE
remote_str_hash = synch_box2.local_str_hash
# request though the web
# LOCAL
remote_synch_data = synch_box.handle_remote_synch_command(remote_str_hash)
# response through the web
# REMOTE
# 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.
assert type(remote_synch_data) != str and any(['h' == i[0] for i in remote_synch_data])
synch_box2.handle_local_synch_command(remote_synch_data)  # At this step,there is nothing to change in fact.
assert synch_box2.local_str == synch_box.local_str

# Now repeat
# LOCAL
# make some change
synch_box.local_str += 'u28dy2'
# REMOTE
remote_str_hash = synch_box2.local_str_hash
# request though the web
# LOCAL
remote_synch_data = synch_box.handle_remote_synch_command(remote_str_hash)
# response through the web
# REMOTE
assert type(remote_synch_data) != str and any(['h' == i[0] for i in remote_synch_data])
old_str = synch_box2.local_str
synch_box2.handle_local_synch_command(remote_synch_data)  # At this step,changes happen.
assert synch_box2.local_str == synch_box.local_str != old_str

# Now repeat
# Vice versa, REMOTE could synchronizes LOCAL.
# REMOTE
# make some change
synch_box2.local_str = synch_box2.local_str[0:3] + synch_box2.local_str[-3:]
# LOCAL
str_hash = synch_box.local_str_hash
# request though the web
# REMOTE
synch_data = synch_box2.handle_remote_synch_command(str_hash)
# response through the web
# LOCAL
assert type(synch_data) != str and any(['h' == i[0] for i in synch_data])
old_str = synch_box.local_str
synch_box.handle_local_synch_command(synch_data)  # At this step,changes happen.
assert synch_box2.local_str == synch_box.local_str != old_str

# A bad situation
# REMOTE
remote_str_hash = synch_box2.local_str_hash
# request though the web
# LOCAL
remote_synch_data = synch_box.handle_remote_synch_command(remote_str_hash)
# response through the web
# REMOTE
assert type(remote_synch_data) != str and any(['h' == i[0] for i in remote_synch_data])
# Before the difference information synchronization data comes, the string with the hash value remote_str_hash changes for some reason.
synch_box2.local_str = 'Hello world'
# The coming information can't be handled.
try:
    synch_box2.handle_local_synch_command(remote_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 easily.
def fetch_raw_string():
    return synch_box.local_str


synch_box2.handle_local_synch_command(remote_synch_data, fetch_raw_string)  # At this step,changes happen
assert synch_box2.local_str == synch_box.local_str


# If you are using this module in a coroutine function, to request you can pass a coroutine function as the error handler,
# 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 easily.
async def fetch_raw_string():
    return synch_box.local_str


async def main():
    handle_local_synch_command_res = synch_box2.handle_local_synch_command(remote_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_box2.local_str == synch_box.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.4.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

StrDiffSynch-2.2.4-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: StrDiffSynch-2.2.4.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0.post20200714 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7

File hashes

Hashes for StrDiffSynch-2.2.4.tar.gz
Algorithm Hash digest
SHA256 7ec33378f34daa2ba715d6006a9d10976ec9a65a35ce63713d48127ac3a7d449
MD5 c0d89a30a4cbb3fe0f9bbc1be10a6750
BLAKE2b-256 ea9502f7ac3a7183e16f26ca1c9ec04e2a26062393b36e5c30ea6843ad3b91b1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: StrDiffSynch-2.2.4-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0.post20200714 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7

File hashes

Hashes for StrDiffSynch-2.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 7fd5fa37605f98d98217e311fb11a2227f29527be6d58a91485e193343d1502e
MD5 9cff50236ceff57a2acfb7a18acdaa1c
BLAKE2b-256 e33f7a15b37006604ca3a1f1014f2b7c337e9d37d9a91fd8ebc6e6937ca292fb

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