Skip to main content

Build Status PyPI version

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):
        super(Adder, self).__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 https://docs.python.org/2/library/logging.html#logging-levels.

log is a log object created using python’s standard logging module. You can read more about it at https://docs.python.org/2/library/logging.html.

Release files for basescript 0.1.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for basescript 0.1.11
File Size Uploaded
basescript-0.1.11.tar.gz 12.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for basescript 0.1.11
File Interpreter ABI Platform
basescript-0.1.11-py2-none-any.whl Python 2 none any Details

Total release size: 27.9 kB

Release files / basescript-0.1.11.tar.gz

Download URL basescript-0.1.11.tar.gz
Size 12.2 kB
Tags Source
SHA-256 checksum
How to use checksums
3b6491eac2aae996beb2081ba30f906d6993b19d5f09d42320c041ed71696a8f
BLAKE2b-256 checksum
How to use checksums
4cd36e7cf1e400f2c30e29dba8ad95e91938d7fb7331aa04769e436af3b3b794
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / basescript-0.1.11-py2-none-any.whl

Download URL basescript-0.1.11-py2-none-any.whl
Size 15.7 kB
Tags Python 2
SHA-256 checksum
How to use checksums
e08574cb52dec555750d8421c9c868d70152c81f7e2e00872c37c87c041d149d
BLAKE2b-256 checksum
How to use checksums
6bfe3ccfc601fff17ec199e47312d788172cf3b43caa9b50e6d5c1d7466d6002
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.3.9

2 release files

0.3.8

1 release file

0.3.7

1 release file

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.17

2 release files

0.1.16

2 release files

0.1.15

2 release files

This release

0.1.11 This release

2 release files

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.4

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.0.1

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page