A simple package for rapidly developing system sejings.
Project description
Sejings
Sejings is meant to be a quick and simple tool to rapidly integrate project sejings. The problem I've seen many times with libraries is that it's often hard to have a way for the user to change aspects or functionality deep in a library. Libraries can often have arguments for a function that are unavailable to a developer because they're hidden behind three function calls.
One attempt that evolves as a codebase grows is to pass along keyword
arguments, which leads to documentation and maintainability issues.
This is evident by the popularity of **kwargs in many data science libraries.
The only solution I've seen that tries to solve this problem is to work
with dictionaries similar to matplotlib's rcParams. Yet this leads to
a tedious and time consuming developer experience by forcing
if xxx is None
statements at the top of method calls.
To show you my solution let's first start of with a rcParams style function example:
def add(*nums, cache=None, cache_path=None):
if cache is None:
cache = dict_settings['cache']
if cache_path is None:
cache_path = dict_settings['cache.path']
result = sum(nums)
if cache:
save_to_cache(result, cache_path)
return result
Obviously this would be computationally the fastest way to accomplish sejings but leaves a lot of work to the developer. That's why this project is meant to be friendly to developers consuming packages and to encourage rapid development.
Usage
Import sejings and create the sejings you need:
from sejings import sejings
sejings.cache = True
sejings.cache.path = '/some/dir/path'
To evaluate sejings passed into a function as an argument use the @extract_sejings decorator. This will evaluate all sejings in the function definition and in the arguments being passed into the function:
from sejings import extract_sejings
@extract_sejings
def add(*nums, cache=sejings.cache, cache_path=sejings.cache.path):
result = sum(nums)
if cache: # True
save_to_cache(result, cache_path) #'/some/dir/path'
return result
A branch is also evaluated when an endpoint is called.
assert sejings.cache()
assert sejings.cache.path() == '/some/dir/path'
def add(*nums, cache=sejings.cache, cache_path=sejings.cache.path):
result = sum(nums)
if cache(): # True
save_to_cache(result, cache_path()) #'/some/dir/path'
return result
In some cases defining arguments as a Sejings object may be desired. This is accomplished by adding the argument name to the @extract_sejings arguments.
@extract_sejings('cache')
def add(*nums, cache=sejings.cache):
result = sum(nums)
if cache(): # True
save_to_cache(result, cache.path()) #'/some/dir/path'
return result
Integrating Into Your Project
I chose sejings as a close to settings word that wouldn't conflict with a potentially very common word in programming. Therefore you are encouraged to import Sejings as a name you see fit. To use matplotlibs rcParams again as an example we could look at an fictional file settings.py:
from pathlib import Path
from sejings import (
extract_sejings as extract_settings,
Sejings
)
my_settings = Sejings(name='rcParams')
my_settings.cache = True
my_settings.cache.path = Path(r'/some/dir/path')
@extract_settings
def add(*nums, cache=my_settings.cache, cache_path=my_settings.cache.path):
result = sum(nums)
if cache: # True
save_to_cache(result, cache_path) #'/some/dir/path'
return result
@TODO
- Context manager
- getitem, setitem
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
File details
Details for the file sejings-0.0.6.tar.gz
.
File metadata
- Download URL: sejings-0.0.6.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e5302e95eef703fd6c61a6db76c3c1dea40769e87ad23747fff64f6a37af142 |
|
MD5 | a7e485b3f8b634a1fcdefcf686b20a88 |
|
BLAKE2b-256 | ad28492279115a58b32dbeb6449d333ee1be36d7ef89337d27fa79fea52b5bb3 |