Skip to main content

Custom library.

Project description

customlib

A few tools for day to day work.


Installation:

python -m pip install [--upgrade] customlib

Available tools:

FileHandler

This is a file handler that can be used as a context-manager with thread & file locking.

How to:

from customlib.filehandlers import FileHandler

fh = FileHandler("test_file.txt", "a", encoding="UTF-8")
fh.write("Just testing out this cool new filehandler.\n")
fh.close()

or even beter:

with FileHandler("test_file.txt", "a", encoding="UTF-8") as fh:
    fh.write("Just testing out this cool new filehandler.\n")


FileLocker

This is a file locker that can also be used as a context-manager.

How to:

from customlib.filelockers import FileLocker, LOCK

fl1 = FileLocker()

if __name__ == '__main__':
    fh1 = open("just_a_file.txt", "w", encoding="UTF-8")
    fl1.acquire(fh1, flags=LOCK.EX)
    fh1.write("Just testing the locking system...\n")
    fl1.release(fh1)
    fh1.close()

Lock types:

  • LOCK.EX: int (0x1), exclusive lock
  • LOCK.SH: int (0x2), shared lock

Lock flags:

  • LOCK.NB: int (0x4), non-blocking

Manually unlock (only needed internally):

  • LOCK.UN: int (0), unlock


Vault

This is a handler that makes use of keyring for password safe-keeping.

How to:

from customlib.keyvault import Vault

vault = Vault()
password: str = vault.generate(exclude="\'\"\\", length=16)
vault.set_password(service="test_service", username="test_username", password=password)


if __name__ == '__main__':
    password: str = vault.get_password(service="test_service", username="test_username")
    print(password)

    vault.del_password(service="test_service", username="test_username")

generate params:

  • include: The character set(s) to be used when generating the password.
    • u: ascii_uppercase
    • l: ascii_lowercase
    • d: digits
    • p: punctuation
  • exclude: The characters to be excluded from the password.
  • length: The number of characters our password should have.

This is not so different from keyring, after all, it is making use of its methods. It exists only to serve as a base class for KeyVault!


KeyVault

This is a handler that makes use of keyring for password safe-keeping using encryption.

How to:

from customlib.keyvault import KeyVault

vault = KeyVault()
vault.password(
    value=vault.generate(exclude="\'\"\\", length=16),
    salt=vault.generate(exclude="\'\"\\", length=16)
)

if __name__ == '__main__':
    vault.set_password(service="test_service", username="test_username", password="test_password")

    print(vault.get_password(service="test_service", username="test_username"))

    vault.del_password(service="test_service", username="test_username")

generate params:

  • include: The character set(s) to be used when generating the password.
    • u: ascii_uppercase
    • l: ascii_lowercase
    • d: digits
    • p: punctuation
  • exclude: The characters to be excluded from the password.
  • length: The number of characters our password should have.


ClassRegistry

Immutable class registry handler.

Example:

from customlib.registry import ClassRegistry

@ClassRegistry.register("some_name")
class SomeClass(object):

    def __init__(self, var: str):
        self.var = var


if __name__ == '__main__':
    # instantiating 'SomeClass':
    some_class = ClassRegistry.get("some_name", "var_value")
    print(some_class.var)


MutableClassRegistry

Mutable class registry handler. If a key already exists in __register__ it will be updated.

Example:

from customlib.registry import MutableClassRegistry

@MutableClassRegistry.register("some_name")
class SomeClass(object):

    def __init__(self, var: str):
        self.var = var


if __name__ == '__main__':
    # instantiating 'SomeClass'
    some_class = MutableClassRegistry.get("some_name", "var_value")
    print(some_class.var)


OsSleepInhibitor

With this handler we can prevent the operating system from going to sleep.

Works in Windows and it might work as well in Linux and Darwin systems (not tested!).

How to:

from customlib.systemhandlers import OsSleepInhibitor

if __name__ == '__main__':
    with OsSleepInhibitor(keep_screen_awake=True) as osi:
        print("I just did something that took a lot of time...")


MetaSingleton

Singleton metaclass for restricting non-strict classes to only one instance per runtime.

How to:

from customlib.singletons import MetaSingleton


class CfgParser(object, metaclass=MetaSingleton):
    """test"""


if __name__ == '__main__':
    cfg1 = CfgParser()
    cfg2 = CfgParser()

    print("*" * 80)
    print("cfg1 == cfg2:", cfg1 == cfg2)
    print("cfg1 is cfg2:", cfg1 is cfg2)
cfg1 == cfg2: True
cfg1 is cfg2: True


singleton

Singleton decorator for metaclass. Restrict object to only one instance per runtime.

How to:

from customlib.singletons import singleton


@singleton
class CfgParser(object):
    """test"""


if __name__ == '__main__':
    cfg1 = CfgParser()
    cfg2 = CfgParser()

    print("*" * 80)
    print("cfg1 == cfg2:", cfg1 == cfg2)
    print("cfg1 is cfg2:", cfg1 is cfg2)
cfg1 == cfg2: True
cfg1 is cfg2: True


del_prefix

If target starts with the prefix string and prefix is not empty, return string[len(prefix):]. Otherwise, return a copy of the original string.

How to:

from customlib.utils import del_prefix

a = "some cool string"

if __name__ == '__main__':
    b = del_prefix(target=a, prefix="some ")
    print(b)  # --> "cool string"


del_suffix

If target ends with the suffix string and suffix is not empty, return string[:-len(suffix)]. Otherwise, return a copy of the original string.

How to:

from customlib.utils import del_suffix

a = "some cool string"

if __name__ == '__main__':
    b = del_suffix(target=a, suffix=" string")
    print(b)  # --> "some cool"


WARNING:

As of version v6.0.0 the following modules are no longer in this library:


Note:

This is still work in progress!


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

customlib-6.3.0.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

customlib-6.3.0-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file customlib-6.3.0.tar.gz.

File metadata

  • Download URL: customlib-6.3.0.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for customlib-6.3.0.tar.gz
Algorithm Hash digest
SHA256 35ea01198852329cd4ea6e5a4c3fab28fc78b9f644cf762b994f4f387ffaa57b
MD5 3dd172219b066092bb586edce80fc593
BLAKE2b-256 3e7bdb05cfbd0ab8369e1feace8e3c943811fc067b3874bdd4378fcdc3f94e3a

See more details on using hashes here.

File details

Details for the file customlib-6.3.0-py3-none-any.whl.

File metadata

  • Download URL: customlib-6.3.0-py3-none-any.whl
  • Upload date:
  • Size: 18.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for customlib-6.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8737d8762605d19b5a19af7e1476636f5a22ae173d621dd3ebdc21a34a553ccf
MD5 e395b6c1426b8b42fed6036e79029d32
BLAKE2b-256 1f9448c809bc3cebe73f4ce0df620269ee9e3f087af192237859211916f8f653

See more details on using hashes here.

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