Skip to main content

Python decorator to turn a function into a variable definition

Project description

vardef: contained and organized variable definition

Usage Examples 🚩 | Developer Reference 👩‍💻 | Authors 👫

vardef is a simple idea for declaring variables in multiple statements in a contained and organized fashion.

A simple Python decorator built with Heart and designed for Humans

from vardef import vardef

vars_defined = 0

def somevar() -> int:
    global vars_defined
    vars_defined += 1
    return 42

def othervar() -> int:
    global vars_defined
    vars_defined += 1
    return 73

# 2

# 42

# 73

# <class 'int'>

# <class 'int'>
from unittest.mock import Mock
from vardef import vardef

from somewhere import User

def user_with_read_role() -> Mock:
    user_mock = Mock(User)
    user_mock.roles = ["READ"]
    return user_mock

# ['READ']

# <class 'unittest.mock.Mock'>
import pandas as pd

from butterfree.extract import Source
from butterfree.extract.readers import TableReader
from butterfree.clients import SparkClient
from vardef import vardef

spark_client = SparkClient()

def df() -> pd.DataFrame:
    source = Source(
            SELECT * FROM colors
    return source.construct(spark_client)

from vardef import vardef

def buggy() -> int:
    return 4 / 0

# Traceback (most recent call last):
#   File "./", line 5, in <module>
#     def buggy() -> int:
#   File "./vardef/", line 7, in vardef
#     return define_var()
#   File "./", line 6, in buggy
#     return 4 / 0
# ZeroDivisionError: division by zero

Why to use vardef

  • Organization: using a vardef function allows all necessary code to define and initialize a variable to be logically separated from the outer scope. This makes it clear what code is relevant to the rest of the scope and what code is only there to assist with initialization. Also, in this way, auxiliary code is avoided being exposed to be imported or messed up by external agents.

  • Conciseness: a vardef function is a syntax sugar to avoid the need to declare a function which will only be called once to define a variable. With the decorator your code gets concise and also avoids exposing the initializer function.

For further information on how to use the decorator check out our docs!


0.1.0 (2020-12-03)

  • First beta release

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

vardef-0.1.0.tar.gz (6.5 kB view hashes)

Uploaded Source

Built Distribution

vardef-0.1.0-py3-none-any.whl (7.8 kB view hashes)

Uploaded Python 3

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