Balparda's base library of util methods and classes.
Started in January/2023, by Daniel Balparda. This has stuff I used in my personal projects and then moved to a common library. It is all provided "as-is" without any promise or any presumption that it works or is safe. Use it if you like it but at you own risk and responsibility. Having said that, I make it public in the hope that it will be useful.
Since version 1.7 it is PyPI package:
https://pypi.org/project/balparda_baselib/
Copyright 2025 Daniel Balparda balparda@github.com
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License here.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
To use in your project just do:
pip3 install balparda_baseliband then from balparda_baselib import base for using it.
TODO: this needs quite some work.
from balparda_baselib import base
@base.Timed('Total main() method execution time')
def main():
# will automatically time execution of this decorated method,
# and upon exit will log to info, using the message given
passTODO: this needs quite some work.
from balparda_baselib import base
@base.Timed('Total main() method execution time')
def main():
# will automatically time execution of this decorated method,
# and upon exit will log to info, using the message given
# decimal numbers humanized string conversion, from zero to Tera:
print(base.HumanizedDecimal(11)) # will print '1'
print(base.HumanizedDecimal(12100)) # will print '12.10k'
print(base.HumanizedDecimal(13200000)) # will print '13.20M'
print(base.HumanizedDecimal(15400000000000)) # will print '15.40T'
# byte lengths humanized string conversion, from zero to Terabytes:
print(base.HumanizedBytes(10)) # will print '10b'
print(base.HumanizedBytes(10000)) # will print '9.77kb'
print(base.HumanizedBytes(10000000)) # will print '9.54Mb'
print(base.HumanizedBytes(10000000000000)) # will print '9.09Tb'
# time lengths (in seconds) humanized string conversion, from milliseconds to days:
print(base.HumanizedSeconds(0.00456789)) # will print '4.568 msecs'
print(base.HumanizedSeconds(10)) # will print '10.00 secs'
print(base.HumanizedSeconds(5000)) # will print '1.39 hours'
print(base.HumanizedSeconds(100000)) # will print '1.16 days'ATTENTION: serialization is dangerous, and should be used with care!
TODO: this needs quite some work.
import getpass
from balparda_baselib import base
base.BinSerialize({'a': 1, 'b': 2}, '~/file1.db') # will save the dict to `file1`, compressed
data = base.BinDeSerialize(file_path='~/file1.db') # will load the dict from `file1`
str_password = getpass.getpass(prompt='Password: ')
key = base.DeriveKeyFromStaticPassword(str_password)
base.BinSerialize([1, 2], '~/file2.db', compress=False, key=key) # save list to `file2`, encrypted
data = base.BinDeSerialize(file_path='~/file2.db', compress=False, key=key) # load list from `file2`If you want to develop for this project, first install python 3.11/12/13 and Poetry, but to get the versions you will need, we suggest you do it like this (Linux):
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install git python3 python3-pip pipx python3-dev python3-venv build-essential software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa # install arbitrary python version
sudo apt-get update
sudo apt-get install python3.11 python3.13
sudo apt-get remove python3-poetry
python3.13 -m pipx ensurepath
# re-open terminal
pipx install poetry
poetry --version # should be >=2.1
poetry config virtualenvs.in-project true # creates .venv inside project directory
poetry config pypi-token.pypi <TOKEN> # add your personal PyPI project token, if anyor this (Mac):
brew update
brew upgrade
brew cleanup -s
brew install git python@3.11 python@3.13 # install arbitrary python version
brew uninstall poetry
python3.13 -m pip install --user pipx
python3.13 -m pipx ensurepath
# re-open terminal
pipx install poetry
poetry --version # should be >=2.1
poetry config virtualenvs.in-project true # creates .venv inside project directory
poetry config pypi-token.pypi <TOKEN> # add your personal PyPI project token, if anyNow install the project:
git clone https://github.com/balparda/baselib.git baselib
cd baselib
poetry env use python3.11 # creates the venv, 3.11 for development!
poetry install --sync # HONOR the project's poetry.lock file, uninstalls stray packages
poetry env info # no-op: just to check
poetry run pytest -vvv
# or any command as:
poetry run <any-command>To activate like a regular environment do:
poetry env activate
# will print activation command which you next execute, or you can do:
source .env/bin/activate # if .env is local to the project
source "$(poetry env info --path)/bin/activate" # for other paths
pytest # or other commands
deactivateTo update poetry.lock file to more current versions do poetry update, it will ignore the current lock, update, and rewrite the poetry.lock file.
To add a new dependency you should do:
poetry add "pkg>=1.2.3" # regenerates lock, updates env (adds dep to prod code)
poetry add -G dev "pkg>=1.2.3" # adds dep to dev code ("group" dev)
# also remember: "pkg@^1.2.3" = latest 1.* ; "pkg@~1.2.3" = latest 1.2.* ; "pkg@1.2.3" exactIf you manually added a dependency to pyproject.toml you should very carefully recreate the environment and files:
rm -rf .venv .poetry poetry.lock
poetry env use python3.13
poetry installRemember to check your diffs before submitting (especially poetry.lock) to avoid surprises!
When dependencies change, always regenerate requirements.txt by running:
poetry export --format requirements.txt --without-hashes --output requirements.txt# bump the version!
poetry version minor # updates 1.6 to 1.7, for example
# or:
poetry version patch # updates 1.6 to 1.6.1
# or:
poetry version <version-number>
# (also updates `pyproject.toml` and `poetry.lock`)
# publish to GIT, including a TAG
git commit -a -m "release version 1.7"
git tag 1.7
git push
git push --tags
# prepare package for PyPI
poetry build
poetry publish