Skip to main content

Basic infrastructure for writing scripts

Project description

Python is an excellent language that makes writing scripts very straightforward. Over the course of writing many scripts, we realized that we were doing some things over and over like creating a logger and accepting command line arguments. Base script is a very simple abstraction that takes care of setting up logging and other basics so you can focus on your application specific logic.

Here are some facilities that Base Script offers:

  • Logging

  • Accepting command-line arguments using argparse

Installation

pip install basescript

Usage

Here is a simple example to get started

Hello World

helloworld.py

from basescript import BaseScript

class HelloWorld(BaseScript):
    def run(self):
        print "Hello world"

if __name__ == '__main__':
    HelloWorld().start()

NOTE: all examples showcased here are available under the
``examples`` directory

Run the above by doing:

python helloworld.py run

Run script with log level set to DEBUG

python helloworld.py --log-level DEBUG run

Run script with custom log file

python helloworld.py --log-level DEBUG --log mylog run

Command line args, Using the logger

The following is a more involved example

adder.py

from basescript import BaseScript

class Adder(BaseScript):
    # The following specifies the script description so that it be used
    # as a part of the usage doc when --help option is used during running.
    DESC = 'Adds numbers'

    def init(self):
        '''
        We can put whatever script initialization we need for our script
        over here. This is preferred to overriding __init__
        '''
        self.a = 10
        self.b = 20

    def define_args(self, parser):
        parser.add_argument('c', type=int, help='Number to add')

    def run(self):
        self.log.info("Starting run of script ...")

        print self.a + self.b + self.args.c

        self.log.info("Script is done")

if __name__ == '__main__':
    Adder().start()
Run the script as follows and observe the usage information shown. Note how the
description appears along with the c argument.
python adder.py --help
usage: adder.py [-h] [--name NAME] [--log LOG] [--log-level LOG_LEVEL]
                [--quiet]
                {run} ...

Adds numbers

optional arguments:
  -h, --help            show this help message and exit
  --name NAME           Name to identify this instance
  --log LOG             Name of log file
  --log-level LOG_LEVEL
                        Logging level as picked from the logging module
  --quiet

commands:
  {run}
python adder.py run --help
usage: adder.py run [-h] c

positional arguments:
  c           Number to add

optional arguments:
  -h, --help  show this help message and exit

Run the script now to see the intended output

python adder.py run 30
60

Run the same with info and higher level logs enabled

python adder.py --log-level INFO 30
2016-04-10 13:48:27,356 INFO Starting run of script ...
60
2016-04-10 13:48:27,356 INFO Script is done
--log-level accepts all the values shown at
log is a log object created using python’s standard logging module. You can

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

basescript-0.1.4.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

basescript-0.1.4-py2-none-any.whl (6.5 kB view details)

Uploaded Python 2

File details

Details for the file basescript-0.1.4.tar.gz.

File metadata

  • Download URL: basescript-0.1.4.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for basescript-0.1.4.tar.gz
Algorithm Hash digest
SHA256 86b17cbe6515b2b2a62782ae4b4a989062b8e68efb3671488cdccfd823c9f0ac
MD5 47a7cd241131968455d7f5c105dfc233
BLAKE2b-256 c8218546f8816dc6d2a9423d93ee586af7229bac0630c4253a6cb3b6fa7f62c3

See more details on using hashes here.

File details

Details for the file basescript-0.1.4-py2-none-any.whl.

File metadata

File hashes

Hashes for basescript-0.1.4-py2-none-any.whl
Algorithm Hash digest
SHA256 d1319ede324856361771a64ccdbc03be5d8cfd3fd53ce0114a7c39326229c35b
MD5 13848fad8c6b539f9d923c2d5df15c9d
BLAKE2b-256 b7e116e029a358054a24263ef01bad1c35c1d51a90eb4491a66efdf68c201a78

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