Skip to main content

Python Util Package for inheriting Argparse behavior

Project description

clientwrapper (licensed under Apache 2.0)

Util class enabled CLI/dictionary syntax: allows separation of concerns between CLI and Python code.

installation

pip install clientwrapper

usage

overview

ClientWrapper is a class that allows you to define functions that can be run in both CLI and Python syntax.

run in CLI

Running as a CLI requires all functions with arguments to run in one combined function. The below command will not run as a CLI.

python3 -m yourmodule authenticate --arg1 value1 --arg2 value2
# self.attributes like authentication tokens are erased every time the CLI is run; combine your functions to avoid NullPointerExceptions
python3 -m yourmodule query --argument value # will not work: relying on any attributes in self

Below is an example of how to run as a CLI.

python3 -m yourmodule authenticate_and_query --arg1 value1 --arg2 value2 --argument value

usage example

Define your class here and inherit from ClientWrapper; combined function is required to run as a CLI.

class Impl(ClientWrapper):
    def __init__(self):
        super().__init__("Some API Requests")

    def login(self, username, password):
        print("login called with username: " + username + " and password: " + password)

    def getRequest(self, argument):
        print("getRequest called with argument: " + argument)

    def postRequest(self, **kwargs):
        print("postRequest called with arguments: " + str(kwargs))  

    def login_and_get_request_and_post_request(self, username, password, argument, **kwargs):
        self.login(username, password)
        self.getRequest(argument)
        self.postRequest(**kwargs)

test in CLI

Use CLI syntax to run your functions.

python3 -m yourmodule login_and_get_request_and_post_request --username myusername --password mypassword --argument [1, 2] --arg1 value1 --arg2 value2

test in Python

Use argparse-like syntax in Python for testing purposes. Note that Clientwrapper takes strings, ints, lists, tuples, and dictionaries as arguments but containers must be passed as strings.

impl = Impl()
impl.run([
    'login_and_get_request_and_post_request --username myusername --password mypassword --argument "[1, 2]" --arg1 value1 --arg2 value2'.split()
])
>>> login called with username: myusername and password: mypassword
>>> getRequest called with argument: [1, 2]
>>> postRequest called with arguments: {'arg1': 'value1', 'arg2': 'value2'}

test in CMD prompt as CLI

Your module will require a main.py in order to run as a CLI; here is a simple example.

from src.etc.package import Impl

def main():
    impl = Impl()
    impl.run()

if __name__ == '__main__':
    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

clientwrapper-1.0.1.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

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

clientwrapper-1.0.1-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for clientwrapper-1.0.1.tar.gz
Algorithm Hash digest
SHA256 3bf3150b899e3f2dcc9c72cee4d9ca71b169058f36dc5d934013185aad7662be
MD5 3c163db4ca9b6a26627c04df3e973428
BLAKE2b-256 a95a570b61158b6080628ed8e9671526c506e9bb2325ddd34838f626b9b99726

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for clientwrapper-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0857a1ded3a653b23b1f9244a73a5be23759ef7373cac9cc92bc5529272e4622
MD5 aa7f3d25a5febc9439c6c391cd25a10d
BLAKE2b-256 6fd3762e232ed6c33c98db05c03aa49f9f95fb08582dae076dc82ff0b2fa8db6

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