Package: customlib
customlibA few tools for day to day work.Installation:python -m pip install [--upgrade] customlibAvailable tools:FileHandlerThis is a file handler that can be used as a context-manager with thread & file locking.How to:fromcustomlib.filehandlersimportFileHandlerfh=FileHandler("test_file.txt","a",encoding="UTF-8")fh.write("Just testing out this cool new filehandler.\n")fh.close()or even beter:withFileHandler("test_file.txt","a",encoding="UTF-8")asfh:fh.write("Just testing out this cool new filehandler.\n")FileLockerThis is a file locker that can also be used as a context-manager.How to:fromcustomlib.filelockersimportFileLocker,LOCKfl1=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 lockLOCK.SH: int (0x2), shared lockLock flags:LOCK.NB: int (0x4), non-blockingManually unlock (only needed internally):LOCK.UN: int (0), unlockVaultThis is a handler that makes use of keyring for password safe-keeping.How to:fromcustomlib.keyvaultimportVaultvault=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")generateparams:include: The character set(s) to be used when generating the password.u: ascii_uppercasel: ascii_lowercased: digitsp: punctuationexclude: The characters to be excluded from the password.length: The number of characters our password should have.This is not so different fromkeyring, after all, it is making use of its methods.
It exists only to serve as a base class forKeyVault!KeyVaultThis is a handler that makes use of keyring for password safe-keeping using encryption.How to:fromcustomlib.keyvaultimportKeyVaultvault=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")generateparams:include: The character set(s) to be used when generating the password.u: ascii_uppercasel: ascii_lowercased: digitsp: punctuationexclude: The characters to be excluded from the password.length: The number of characters our password should have.ClassRegistryImmutable class registry handler.Example:fromcustomlib.registryimportClassRegistry@ClassRegistry.register("some_name")classSomeClass(object):def__init__(self,var:str):self.var=varif__name__=='__main__':# instantiating 'SomeClass':some_class=ClassRegistry.get("some_name","var_value")print(some_class.var)MutableClassRegistryMutable class registry handler.
If a key already exists in__register__it will be updated.Example:fromcustomlib.registryimportMutableClassRegistry@MutableClassRegistry.register("some_name")classSomeClass(object):def__init__(self,var:str):self.var=varif__name__=='__main__':# instantiating 'SomeClass'some_class=MutableClassRegistry.get("some_name","var_value")print(some_class.var)OsSleepInhibitorWith this handler we can prevent the operating system from going to sleep.Works inWindowsand it might work as well inLinuxandDarwinsystems (not tested!).How to:fromcustomlib.systemhandlersimportOsSleepInhibitorif__name__=='__main__':withOsSleepInhibitor(keep_screen_awake=True)asosi:print("I just did something that took a lot of time...")MetaSingletonSingleton metaclass for restrictingnon-strictclasses to only one instance per runtime.How to:fromcustomlib.singletonsimportMetaSingletonclassCfgParser(object,metaclass=MetaSingleton):"""test"""if__name__=='__main__':cfg1=CfgParser()cfg2=CfgParser()print("*"*80)print("cfg1 == cfg2:",cfg1==cfg2)print("cfg1 is cfg2:",cfg1iscfg2)cfg1 == cfg2: True
cfg1 is cfg2: TruesingletonSingleton decorator formetaclass.
Restrict object to only one instance per runtime.How to:fromcustomlib.singletonsimportsingleton@singletonclassCfgParser(object):"""test"""if__name__=='__main__':cfg1=CfgParser()cfg2=CfgParser()print("*"*80)print("cfg1 == cfg2:",cfg1==cfg2)print("cfg1 is cfg2:",cfg1iscfg2)cfg1 == cfg2: True
cfg1 is cfg2: Truedel_prefixIftargetstarts with theprefixstring andprefixis not empty, returnstring[len(prefix):].
Otherwise, return a copy of the original string.How to:fromcustomlib.utilsimportdel_prefixa="some cool string"if__name__=='__main__':b=del_prefix(target=a,prefix="some ")print(b)# --> "cool string"del_suffixIftargetends with thesuffixstring andsuffixis not empty, returnstring[:-len(suffix)].
Otherwise, return a copy of the original string.How to:fromcustomlib.utilsimportdel_suffixa="some cool string"if__name__=='__main__':b=del_suffix(target=a,suffix=" string")print(b)# --> "some cool"WARNING:As of versionv6.0.0the following modules are no longer in this library:logging(nowlogpie)config(nowcfgpie)sqlite(nowsqlitepie)Note:This is still work in progress!