Package: qolpy
qolpy v0.2.0Are you tired of making the same module in every project? Not a problem! Qol has your back.A suite of random but useful functions that are aimed at giving you "piece of cake" level comfortability.This package is also availabe as:qol • js/tsqolrus • rustSome functionality is derived from:fstringInstallationpip install qolpyorpip3 install qolpyImporting# full importimportqolpy# partial importsfromqolpyimportrandom_colour,parse_date,num_parse,abbreviate,to_title_case,to_sentence_case,LoggerFunctionsrandom_colourGet a random colour; For those scenarios where you couldn't care less!Returns astringc=random_colour()c_rgb=random_colour("rgb")c_cmyk=random_colour("cmyk")c_hsv=random_colour("hsv")c_hsl=random_colour("hsl")print(c,c_rgb,c_cmyk,c_hsv,c_hsl)# #f7f7f7, rgb(247,247,247), cmyk(0%,0%,0%,3%), hsv(0,0%,97%), hsl(0,0%,97%)ParamsParameterDefault SettingRequired?DefinitionOptionssettinghexNoThe type of colour you would like returnedhex,rgb,cmyk,hsv,hslparse_dateSend in date parameters and receive either an object with their metadata, or a parsed date (e.g2 Sep 2020); American formatting is possible (e.gSep 2 2020).Returns astringorDate_Object. You can spread the args and use kwargs such as below:importdatetimedt=datetime.datetime.now()date_list=[dt.day,dt.weekday(),dt.month,dt.year]pd=parse_date(*date_list,c_format="nll",american=True)pd_full=parse_date(*date_list,c_format="lll")print(pd,pd_full)# April 15 2023, Saturday 15th April, 2023interface"day":{"short":str,"long":str,"ordinal_month":str,"ordinal_week":str,"week_number":int,"month_number":int},"month":{"short":str,"long":str,"ordinal":str,"number":int},"year":{"short":int,"long":int}ParamsParameterDefault SettingRequired?DefinitionOptionsmonthDaynoneYesThe day of the monthtypenumberweekDaynoneYesThe day of the weektypenumbermonthnoneYesThe numeric monthtypenumberyearnoneYesThe full numeric yeartypenumberformatnoneNoThe date format you would liken = numeric, s = shorthand text, l = full text;nns,nnl,sss,ssl,lll,nss,nsl,nls,nllamericanfalseNoWhether or not you would like the format to be 'Americanised'true,falsenum_parseConvert a number into a string as if it's MS Excel!Returns astringnum=num_parse(2100.45)num_europe=num_parse(2100.45,"punct")num_custom=num_parse(2100.45,"-")print(num,num_europe,num_custom)# 2,100.45, 2.100,45, 2-100.45)ParamsParameterDefault SettingRequired?DefinitionOptionsvalueundefinedYesThe number you want to be parsednonesettingcommaNoThe delimiter for the numberspace,comma,punct, any other delimiter as a stringabbreviatename="lEwiS mOsho junior"print(abbreviate(name))# LMJMake anabbreviationof a string; Usually used for names. It returns an upper case abbreviation of the string.ParamsParameterDefault SettingRequired?DefinitiontextnullYesThe string you wish to abbreviatedelimiter" "NoThe character or string that seperates words in the stringto_title_caseMake any stringtitle cased. it returns a string in which every first letter of a word is upper cased with the rest being lower cased.constname="lEwiS mOsho junior"print(to_title_case(name))# Lewis Mosho JuniorParamsParameterDefault SettingRequired?DefinitiontextnullYesThe string you wish to change to title casedelimiter" "NoThe character or string that seperates words in the stringto_sentence_caseMake any stringsentence cased; The current sentence delimiters are:.;:!?It returns a string in which every first letter of the first word of a sentence is capitalised, with the remainder of the senter being lower cased.sentence="heLLo wOrLD, mY NAME is lEwis; i am a Developer."print(to_sentence_case(sentence))# Hello world, my name is lewis; I am a developer.ParamsParameterDefault SettingRequired?DefinitiontxtnullYesThe string you wish to change to sentence casedelimiter" "NoThe character or string that seperates words in the stringLoggerLog code executions, stats, and processing times in any framework in any environment; and chain the logs to see the entire process in the terminal!ReturnsNonecode examplefromqolpyimportLoggerfrom.some_functionimportsome_functionfromfastapiimportFastAPI,Requestapp=FastAPI()logger=Logger()@app.middleware("http")asyncdefhandler(req:Request,call_next):logger.new_log("log",req.method,req.url.path)logger.log("log","some_function","Doing something...")some_function()logger.proc_time()response=awaitcall_next(req)logger.execTime()returnresponse...terminal output[log•aGy5Op]:GET=>/hello|07Oct2023@19:40[log•aGy5Op]:some_function=>Doingsomething...|07Oct2023@19:40[stats•aGy5Op]:some_function=>53.24ms[exec•aGy5Op]:121.07msVariablesVariableDefault SettingRequired?Definitionid_length5NoAnintthat determines the length of the log idamerican_dateFalseNoAboolthat determines whether theparseDateoutput should be americantime_format%d %b %Y @ %H:%MNoAstrthat defines other options for the log time outputThese can be set when initialising theLoggeror dynamically.NOTEthat you can initialise any of them as undefined through the constructor and it will set their default values, however, dynamically they will need a value of their type unless they can be undefined.# Set the americanDate param through the constructorlogger=Logger(american_date=True)//settheamericanDateparamdynamicallylogger.american_date=FalseMethodsMethodTypeDetailsnew_log(config: Union["stats", "log", "error"], process: string, message: string) -> NoneCreate a new log chain; This will change thelog idlog(config: Union["stats", "log", "error"], process: string, message: string) -> NoneAdd a log to the log chain; This will not change thelog idproc_time() -> voidLog the processing time between this call and the previous call to view their processing timeexec_time() -> voidView the entire execution timelaxdecoratorUseful to measure the process time for a single function (sync or async); not chainable but can have inner processes chained.Decorator Examplelogger=Logger()@logger.laxasyncdefapi_call()->str:req=requests.get("https://api.site.com/hello")logger.log("log","api_call","request successfull!")res=req.json()returnres["msg"]asyncio.run(api_call())[log•cy1zD]:api_call=>Executingwithoutargs|07Oct2023@23:52[log•cy1zD]:api_call=>requestsuccessfull!|07Oct2023@23:52[exec•cy1zD]:107.09msIf you add args and/or kwargs, it will log them too regardless of their type:logger=Logger()@logger.laxasyncdefapi_call(uri:str,log_type:str)->str:req=requests.get(uri)logger.log(log_type,"api_call","request successfull!")res=req.json()returnres["msg"]asyncio.run(api_call("https://api.site.com/hello",log_type="log"))[log•cy1zD]:api_call=>Executingwitharg'https://api.site.com/hello'andkwarglog_type='log'|07Oct2023@23:52[log•cy1zD]:api_call=>requestsuccessfull!|07Oct2023@23:52[exec•cy1zD]:107.09msChangelogv0.2.xv0.2.0AddedLoggerclassv0.1.xv0.1.2README updatedRepo links updatedcolordir typing fixed; No more import errorv0.1.1README completedparse_dateerror fixedUsed tp returnmonthobject whennllwas specifically set as an arg forc_formatMoved codebase intosrcfolderv0.1.0Initial releaseExcel number formatting, date parsing, random colour generation,string sentence casing, title casing, and abrreviations added and typed