Skip to main content

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

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

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

print(vars_defined)
# 2

print(somevar)
# 42

print(othervar)
# 73

print(type(somevar))
# <class 'int'>

print(type(othervar))
# <class 'int'>
from unittest.mock import Mock
from vardef import vardef

from somewhere import User


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

print(user_with_read_role.roles)
# ['READ']

print(type(user_with_read_role))
# <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()

@vardef
def df() -> pd.DataFrame:
    source = Source(
        readers=[TableReader(
            id="colors",
            database="datalake_colors",
            table="colors",
        )],
        query="""
            SELECT * FROM colors
        """,
    )
    return source.construct(spark_client)

df.createOrReplaceTempView("colors")
...
from vardef import vardef


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

# Traceback (most recent call last):
#   File "./buggy.py", line 5, in <module>
#     def buggy() -> int:
#   File "./vardef/__init__.py", line 7, in vardef
#     return define_var()
#   File "./buggy.py", 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!

Changelog

0.1.0 (2020-12-03)

  • First beta release

Metadata

Release files for vardef 0.1.0

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

Source distribution (sdist)

Source distribution for vardef 0.1.0
File Size Uploaded Provenance Actions
vardef-0.1.0.tar.gz 6.5 kB Without provenance Details for vardef-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for vardef 0.1.0
File Interpreter ABI Platform Actions
vardef-0.1.0-py3-none-any.whl Python 3 none any Details for vardef-0.1.0-py3-none-any.whl

Total release size: 14.3 kB

Release files / vardef-0.1.0.tar.gz

Download URL vardef-0.1.0.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
540ed9bb0c1679b11b758628e7810329ab596c65b08035e8ac9a54f94a4d2fd3
BLAKE2b-256 checksum
How to use checksums
d5650159bf393795f2a86c57e898cb0e0edaae748e87373d9c48e6e8d9c2355e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.7.7

Release files / vardef-0.1.0-py3-none-any.whl

Download URL vardef-0.1.0-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
73896b9184aeac688ced5454c53e7f87bb9438350c840dafb3a9d0e14bf6d822
BLAKE2b-256 checksum
How to use checksums
0e08259e2cf6a2e26af2b6dc910c9131b6682c02f30e376251752d8fdfe6d4b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.7.7

Security for vardef 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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