Skip to main content

Tools to use external web api for download data.

Project description

apistblz

Utility tools for stable api access.

Features

  • Cache returns for functions with API accesses(DownloadOnce).
  • Handle API access and do retry if failed(WaitAndRetry).
  • Control access rate(RateControl).

Download Once

Cache returns for functions with API accesses. When @downloadonce decorated function is called at the first time, returns are chacned in memory. Second time, don't run the function but read returns from memory. Returns can be cashed on disk, too.

Settings

  • Global Settings
    • dumpdir: Directory path for on disk cached files. Default: "./.dlo_dump".
    • force_on_disk: Overide on_disk settings for each functions. Default: False.
    • reportmode: Log report mode. In the current version, stdout only. Default: 'stdout'.
  • Decorator Arguments
    • on_disk: Save and load cached returns on disk. Default: False.
    • is_method: Name cache data on disk with Args strings. For method in class, need to ignore first Args(self or cls) for cache data name. Set True for class instance method and set False for non-class instance method. Default: False(Non-class instalce method).
  • Additional Args for function
    • force_run: Ignore cached data and run the function again. Cached data will be overiden by new Returns. Default: False.
    • not_save_on_disk: Load cached data or run but don't cashe returns in memory nor on disk. Default: False.

Usage

  • Functions with not stable returns, different returns for same args every time.
    • Don't use download once decorator.
  • Functions with not stable returns, but stable at least during the script is running.
    • Default settings.
  • Functions with stable returns. Always return same returns for same args.
    • on_disk: True.
  • Functions with unknown returns, stable or not will be turned out after execution.
    • on_disk: True, not_save_on_disk: True
    • If returns are stable, run function again without "not_save_on_disk: True".
  • Returns are not stable, but no need to update everytime for staging.
    • force_on_disk: True
  • Functions with stable returns, but need to update for some reasons.
    • force_run: True
    • Even with force_run, functions with "not_save_on_disk: True" never save returns.

On Disk Priority: not_save_on_disk > force_on_disk > on_disk.

Usage(Advanced)

Functions has special features with "dlo_cmd" argument.

  • dlo_cmd='is_cached_in_memory': Check returns is cashed in memory or not(bool).
  • dlo_cmd='is_cached_on_disk': Check returns is cashed on disk or not(bool).
  • dlo_cmd='uncache_in_memory': Delect returns in memory(bool).
  • dlo_cmd='uncache_on_disk': Delect returns on disk(bool).
  • dlo_cmd='cache_on_disk': Cache data in memory to disk(bool).

Example

from apitools import downloadonce

# Global Settings
downloadonce.dumpdir = './.dlo_dump'

# Cached in memory
@downloadonce.downloadonce('func01')
def func_in_memory(arg01):
    return True

# Cached in memory and on disk
@downloadonce.downloadonce('func01', on_disk=True)
def func_on_disk(arg01):
    return True

# Basically cached in memory and on disk, but not for once.
func_on_disk(arg01, not_save_on_disk=True)

# Check cache is in memory or not, result is contained in 'result'.
result = func_on_disk(arg01, dlo_cmd='is_cached_in_memory')

Rate Control

Wait if decorated function runs quicker than threthold rate.

Settings

  • Global Settings
    • reportmode: Log report mode. In the current version, stdout only. Default: 'stdout'.
  • Decorator Arguments
    • threshold: Max rate, function run times per second. Default: 1.0
    • tag: Tag for function, rates are counted per tags.

Example

from apitools import ratecontrol

# Restrict 1 times per sec
@ratecontrol.ratecontrol()
def func_01():
    return True

func_01()
func_01() # Wait for 1 sec and run.

# Restrict 1 times per 2sec
@ratecontrol.ratecontrol(threthold=0.5)
def func_02():
    return True

func_02()
func_02() # Wait for 2 sec and run.

# Restrict with tags.
#   func_03 and func_04 are rate controled simultaneously.
@ratecontrol.ratecontrol(threthold=0.5, tag="3 and 4")
def func_03():
    return True

@ratecontrol.ratecontrol(threthold=0.5, tag="3 and 4")
def func_04():
    return True

func_03()
func_04() # Wait for 2 sec and run.

# Global Function
func_03()
ratecontrol.clear("3 and 4") # Clear count history.
func_04() # Run right after func_03.

Wait and Retry

Retry decorated function if failed. To retry, raise Retry in decorated function.

Settings

  • Global Settings
    • reportmode: Log report mode. In the current version, stdout only. Default: 'stdout'.
  • Decoretor Arguments
    • retry: Max retry. Default: 5(times)
  • Raise Argument
    • wait: Wait seconds before retry. Default 5(seconds).

Example

from apitools import wait_and_retry

# Wait 5 seconds if failed, retry 5 times.
# After max retry, raise WaitAndRetryMaxRetryOver exception.
@wait_and_retry.wait_and_retry()
def func_01():
    # Process
    if <Failed and want to retry>:
        raise wait_and_retry.Retry()
    else:
        return <Expected returns>

# Wait 10 seconds if failed, retry 2 times.
# After max retry, raise WaitAndRetryMaxRetryOver exception.
@wait_and_retry.wait_and_retry(retry=2)
def func_02():
    # Process
    if <Failed and want to retry>:
        raise wait_and_retry.Retry(wait=10)
    else:
        return <Expected returns>

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

apistblz-0.1.1.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

apistblz-0.1.1-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file apistblz-0.1.1.tar.gz.

File metadata

  • Download URL: apistblz-0.1.1.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5

File hashes

Hashes for apistblz-0.1.1.tar.gz
Algorithm Hash digest
SHA256 64a7ea9f8855d59acfb423df8f65cada3f3a0b2902e8cdb9a6129f94a5428e45
MD5 eb3d034a03f3d79a43dd874a3606181a
BLAKE2b-256 1c92aec0af654f3497bcb471d4d498d1a4e8e2b5c7345cd1d6c058603bd62edf

See more details on using hashes here.

File details

Details for the file apistblz-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: apistblz-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5

File hashes

Hashes for apistblz-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 93e4dc522f7c5a131e8c9af40b1e420a78993c36f362290321e24d1b36fa2671
MD5 fb29568ba06dc6dcfa507804c06d3ea7
BLAKE2b-256 15ac3adf25ccdfeeb95d0d1c698c76fe0f110c708c0069ba8ccbeff1d0aa4cdd

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