Package: handymodules
Few handy python modulesHey, I wanna share with You my python modules,I have for now:loggertictochashing passwordsfiles_infodownloading filespassword validationI hope You enjoy it!PS. You can find source codes onmy githubDocs:logger:InfoPackage contains "CreateLogger" class that quickly create logger for us,
instead of importing logging module and adding handlers etc...log file has default name same as file name, with .log.
Also it is in new folder called 'logs'Example: python_file.py -> logs/python_file.logArguments:logger_nameType: strDefault: your file nameDescription: logger name should be unique!Default is your filename (without .py) so it will look like:
mailer/flask_receiver/my_first_file etc.WARNING: If you have file (parent)
with logger, and you are importing in it another local file (child),
then remember to give a new logger name to child!# parent.py:importhandymodulesimportchild# local filelog=handymodules.CreateLogger()# its ok, don't need any argumentslog.info('Starting...')child.function()log.info('Done')# child.py:importhandymoduleslog=handymodules.CreateLogger()# BAD!!!log=handymodules.CreateLogger('child')# Gooddeffunction():# do somethinglog.warning('Something is wrong')In this case, there are two loggers and two files, if you want to have
these two loggers in one file, then you should edit line in childlog=handymodules.CreateLogger('child',log_filename='parent')Remember to use file basename (without .py)stream_handlerType: boolDefault: TrueDescription: add a stream handler to your logger (logs in console)file_handlerType: boolDefault: TrueDescription: add a file handler to your logger. It creates "logs"
folder inside directory where your file is.log_filenameType: strDefault: filenameDescription: Just what it says, default logfile has same name as your
python file, but with .logEg. flask_app.py -> logs/flask_app.logKwargstimed_rotating_fileType: boolDefault: FalseDescription: if file handler should be timed rotating. If True, it
will change every midnightdays_to_keepType: intDefault: 7Description: only if timed rotated file, select how long should logger
keep old daily fileslog_inside_folderType: boolDefault: FalseDescription: if True, logs are saving inside logs and inside one
more folder, eg.False - logs/flask_app.logTrue - logs/flask_app/flask_app.logUseful when using timed rotating for like month or three monthsUsage:importhandymoduleslog=handymodules.CreateLogger(stream_handler=True,file_handler=False)log.info('some info')log.debug('some info')log.warning('some info')log.error('some info')log.critical('some info')try:x+=2except:log.add_file_handler()log.exception('Only when exception!')log.change_level('warning')log.change_format('%(message)s')Sample log file line:2019-03-27 20:52:27,471 - flask_app - INFO - some infotictoc:importtictocastt.tic()t.toc()# more codet.toc()t.tic()t.toc()You can use toc multiple times, it will count since lasttic()functiontoc() output:Elapsed time: 2.562 seconds.Also there is a class with this functionality,You can have now multiple timers.importhandymodulest=handymodules.TicToc()t.tic()t.toc()hashing passwordsTwo functions:importhandymodulesashhashed=h.hash_password('Pa$$w0rd')# store it somewhere!h.validate_hashed_password('Pa$$w0rd',hashed)It's simple and secure, returnsTrueorFalseI am using SHA3-512.files_infoimporthandymodulesfilename=handymodules.get_filename()filepath=handymodules.get_file_abspath(file)it will always return filename that uses it,
and absolute file path to the specific filepassword validationimporthandymodules.password_validationasppassword=p.validate_password()inpasswordvariable You have user password from keyboard.It was taken by getpass so it wasn't visible for others (just like in linux)I recommend to use it with hashing_password,
do not store passwords in plain text!download_fileimporthandymodules.downloading_fileasdurl1='http://example.com/text.txt'url2='http://example.com/zipfile.zip'd.download(url1)d.download_zip(url2)You can simply download any file from web giving url only.Zip default will be unpacked to folder (with same name as package) and deleted,
of course You can change itchangelog:2.1.0 - Added support to Python versions lower than 3.6 (removed f-strings)2.0.1 - updated docs2.0.0 - Changed name to handymodules, added kwargs to CreateLogger,
you can now change logfile name, updated docs