Skip to main content

Library containing various Python support classes and functions.

Project description

webkitcorepy

Provides a number of utilities intended to support intermediate to advanced Python programming.

Requirements

The mock, requests and six libraries.

Usage

Version representation

from webkitcorepy import Version
version = Version(1, 2, 3)

Unicode stream management across Python 2 and 3

from webkitcorepy import BytesIO, StringIO, UnicodeIO, unicode

Encoding and decoding byte strings and unicode strings

from webkitcorepy import string_utils

string_utils.encode(...)
string_utils.decode(...)

Automatically install libraries on import.

from webkitcorepy import AutoInstall
AutoInstall.register(Package('requests', Version(2, 24)))
import requests

Mocking basic time and sleep calls

import time
from webkitcorepy import mocks

with mocks.Time:
    stamp = time.time()
    time.sleep(5)

Capturing stdout, stderr and logging output for testing

capturer = OutputCapture()
with capturer:
    print('data\n')
assert capturer.stdout.getvalue() == 'data\n'

Capturing stdout, stderr and logging output for testing

capturer = OutputCapture()
with capturer:
    print('data\n')
assert capturer.stdout.getvalue() == 'data\n'

Timeout context:

import time

from webkitcorepy import Timeout

with Timeout(5, handler=RuntimeError('Exceeded 5 second timeout')):
    time.sleep(4)

subprocess.run replacement:

import sys

from webkitcorepy import run

result = run([sys.executable, '-c', 'print("message")'], capture_output=True, encoding='utf-8')

Mocking of subprocess commands:

from webkitcorepy import mocks, run

with mocks.Subprocess(
    'ls', completion=mocks.ProcessCompletion(returncode=0, stdout='file1.txt\nfile2.txt\n'),
):
    result = run(['ls'], capture_output=True, encoding='utf-8')
    assert result.returncode == 0
    assert result.stdout == 'file1.txt\nfile2.txt\n'

The mocking system for subprocess also supports other subprocess APIs based on Popen:

with mocks.Subprocess(
    'ls', completion=mocks.ProcessCompletion(returncode=0, stdout='file1.txt\nfile2.txt\n'),
):
    assert subprocess.check_output(['ls']) == b'file1.txt\nfile2.txt\n'
    assert subprocess.check_call(['ls']) == 0

For writing integration tests, the mocking system for subprocess supports mocking multiple process calls at the same time:

with mocks.Subprocess(
    mocks.Subprocess.CommandRoute('command-a', 'argument', completion=mocks.ProcessCompletion(returncode=0)),
    mocks.Subprocess.CommandRoute('command-b', completion=mocks.ProcessCompletion(returncode=-1)),
):
    result = run(['command-a', 'argument'])
    assert result.returncode == 0

    result = run(['command-b'])
    assert result.returncode == -1

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

webkitcorepy-1.0.1.tar.gz (58.4 kB view details)

Uploaded Source

Built Distribution

webkitcorepy-1.0.1-py3-none-any.whl (110.0 kB view details)

Uploaded Python 3

File details

Details for the file webkitcorepy-1.0.1.tar.gz.

File metadata

  • Download URL: webkitcorepy-1.0.1.tar.gz
  • Upload date:
  • Size: 58.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.6

File hashes

Hashes for webkitcorepy-1.0.1.tar.gz
Algorithm Hash digest
SHA256 625116398050c6c6114f3a67114dc28f9764aa8e64637da1e2404678fa865a15
MD5 14779a2ceb7e1ced271da459f4d1ec1f
BLAKE2b-256 40a0e8356cc961cc2b8afd084eb639cd6bb28990c7a9282ab4056703b2f91ffe

See more details on using hashes here.

File details

Details for the file webkitcorepy-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: webkitcorepy-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 110.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.6

File hashes

Hashes for webkitcorepy-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b5a94a9bc0b3d519a89a4e4bc628da495fba00a9a8abd1cf5ae868da5469bd25
MD5 9c2493f6e618c610fa2ac54223102d01
BLAKE2b-256 918c28bc3e0211baff057d93765c3e2ab8fdc08f2b9ee327cc72ed99ed2c9eba

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