Skip to content

Latest commit

History

19 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

drastic

Reduce the size of your Python3 code and increase its robustness.

Introduction

Drastic uses annotations for type checking and object auto-initialization. It provides the following decorators.

@strict

  • control arguments of a function or method
  • control returned value

@init

  • control arguments of a constructor
  • add some special methods to the class
  • auto-initialize the object with the given values

Installation

This package works only with Python3 since annotations are Python3 features.

Drastic is on the PyPI repository. Thus, you can install it using pip or easy_install:

pip install drastic

or

easy_install drastic

@strict decorator

Let's begin with a simple example:

fromdrasticimportstrict@strictdefhello(name: str):
print("Hello "+name)

The annotation str specifies that name should be a string. If it is not, an ArgumentTypeError is raised. It also works with parameters:

@strictdefhello(name: str="world"):
print("Hello "+name)

Guess what! You can check the returned value:

@strictdefvalue_of(obj: MyObject) ->int:
returnobj.value

It will raise a ReturnTypeError if the returned value is not a int. To allow multiple types, just use a tuple like this:

@strictdefdisplay(value: (int, str)):
print("The value is {0}".format(value))

Sometimes, you will like to allow None values:

fromdrasticimportstrict, nonable@strictdefdisplay(value: (int, str, nonable)):
print("The value is {0}".format(value))

@init decorator

Python object initialization is very verbose:

classUser:
""" Represents an user. """def__init__(self, firstname, lastname, sex, age, weigth, height):
""" Initializes an user. """self.firstname=firstnameself.lastname=lastnameself.sex=sexself.age=ageself.weight=weightself.height=height

It doesn't match the level of laziness of any Python developer. Using Drastic shorten drastically your code:

fromdrasticimportinitclassUser:
""" Represents an user. """@initdef__init__(self, firstname, lastname, sex, age, weigth, height):
""" Initializes an user. """

No, I don't forgot anything. The @init decorator auto-initializes the object!

@init also performs type checking on arguments as @strict does:

classUser:
""" Represents an user. """@initdef__init__(self,
firstname: str="John",
lastname: str="Doe",
sex: str="Unknown",
age: int,
weigth: (int, float),
height: (int, float)):
""" Initializes an user. """

Use a string of keywords to add more initialization constraints:

classUser:
""" Represents an user. """@initdef__init__(self,
firstname: (str, 'string')="John",
lastname: (str, 'string')="Doe",
sex: str="Unknown",
age: (int, 'compare number'),
weigth: (int, float, 'private nonable'),
height: (int, float)):
""" Initializes an user. """

These are the available keywords:

  • nonable: argument value can be None
  • local: do not add the argument as an object property
  • private: add the argument as a private property
  • boolean: property to use when casting the object to bool
  • number: property to use when casting the object to int or float
  • string: use these properties in the string representation of the object (str(user) would return "<User: firsname=John, lastname=Doe>")
  • container: use this property to emulate a container
  • compare: use this property when comparing two objects

Enable/Disable

By default, drastic is enabled. You can disable/enable using drastic.disable() and drastic.enable().

About

Reduce the size of your Python code and increase its robustness.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages