Package: ccautils
ccautilsa set of utilities for python3.6+ programs and scripts.InstallDevelopmentTestingError UtilitiesMiscellaneous UtilitiesUsageFile UtilitiesUsageInstallInstall for the user:pip3 install ccautils --userInstall for a virtual environment:pip install ccautilsDevelopmentI usepoetryto manage these utilities.
Clone this repository and installpoetry, then install the dependancies.git clone https://github.com/ccdale/ccautils.git
cd ccautils
poetry installTestingTo run the tests you must havepytest,noxandpoetryinstalled.install nox into your python user environment.pip install nox --userRun the tests withnox -rs testsRun the linter withnox -rs lintRun the console ask tests withnox -rs tests -- -sm askError Utilities(#headdd)See thecodefor how to use these
Exception helpers.Miscellaneous UtilitiesUsageimport ccautils.utils as UTaddToStringdelimitStringmakeDictFromStringaskMepadStrreduceTimedisplayValuesecondsFromHMShmsaddToString(xstr, xadd)CodeReturns a string withxaddappended toxstr.  Ifxaddis a list, allstrmembers of the list will be appended in order.UT.addToString("hello", [" ", "world"])

> "hello world"delimitString(xstr, delimeter=" - ")Codexstrcan be a list or a string.  If it is a string, it is spit apart at
spaces and delimeted withdelimeter.  If it is a list, each member is
delimeted withdelimeter.UT.delimitString(["bright", "world"], " ")

> "bright world"

UT.delimitString("I wandered lonely as an artichoke", ".")

> "I.wandered.lonely.as.an.artichoke"makeDictFromString(istr)CodeConstructs a dictionary from a string of parameters. Leading and trailing
whitespace is stripped.istrshould be in the formsomeparam=somevalue,someotherparam=othervalUT.makeDictFromString("sparam=sval, soparam = soval")

> {"sparam": "sval", "soparam": "soval"}askMe(q, default)CodeRequests input from the user.  Poses the questionq. Returns the users
input ordefaultif no input given.UT.askMe("press 5, please", "8")

> press 5, please: 5
> 5padStr(xstr, xlen=2, pad=" ", padleft=True)CodeReturnsxstrpadded to the required length, either on the
left (padleftis True) or the right (padleftis False)UT.padStr("23", 5, "0")

> "00023"reduceTime(unit, secs)CodeDividessecsbyunitreturning a tuple of (units,remainder)Raises aValueErrorifunitis zero.UT.reduceTime(3600, 3700)

> (1, 100)displayValue(val, label, zero=True)CodePluraliseslabelifval> 1 orvalis 0.Will return an empty string ifval== 0 andzero== TrueUT.displayValue(12, "table")

> "12 tables"secondsFromHMS(shms)Codeconverts HMS strings into integer secondsUT.secondsFromHMS("01:01:23.43")
# 1 hour, 1 minute, 23 seconds + 0.43 second

> 3683hms(secs, small=True, short=True, single=False, colons=False)CodeConvertsecsto days, hours, minutes and secondsifsmallis True then only return the higher values if they are > zeroifshortis True then the labels are their short formifsingleis True then the labels are single lettersifcolonsis True then the output is of the form01:03:23UT.hms(67)

> "1 min and 7 secs"

UT.hms(67, short=False)

> "1 minute and 7 seconds"

UT.hms(67, small=False, short=False)

> "0 days, 0 hours, 1 minute and 7 seconds"

secs = 86400 + 7200 + 300 + 34
UT.hms(secs, single=True)

> "1d 2h 5m 34s"

secs = 345
UT.hms(secs, colons=True)

> "05:45"

secs = 86400 + 7200 + 300 + 34
UT.hms(secs, colons=True)

> "01:02:05:34"fuzzyExpires(dt)CodeGiven adatetimeobject, computes the difference between now and that
time.  Returns a tuple of (ts: unix timestamp ofdt,op: string)The returned string gives the approximate time left between now and thedtobject or the string 'EXPIRED'."""It returns 2 hours 20 minutes and some seconds."""
    ts = int(time.time())
    ts += (3600 * 2) + (60 * 20)
    dt = datetime.datetime.fromtimestamp(ts)
    gotts, gotstr = UT.fuzzyExpires(dt)


    """It returns 1 year and 2 months."""
    ts = int(time.time())
    ts += (86400 * 365) + (86400 * 70)
    dt = datetime.datetime.fromtimestamp(ts)
    gotts, gotstr = UT.fuzzyExpires(dt)File UtilitiesUsageimport ccautils.fileutils as FTfileExistsdirExistsdfExistsmakePathfileExists(fqfn)CodeTests for the existence of the fully-qualified (absolute) file namefqfnReturns:Trueiffqfnexists, elseFalsefn = "/home/chris/output.csv"
if FT.fileExists(fn):
    # do something
else:
    raise(f"File {fn} does not exist")dirExists(fqdn)CodeTests for the existence of the fully-qualified (absolute) directory namefqdnReturns:Trueiffqdnexists, elseFalsedn = "/home/chris"
if FT.dirExists(dn):
    # do something
else:
    raise(f"Directory {dn} does not exist")dfExists(fqdfn)CodeTests to see if the filefqdfnexists, if not checks iffqdfnis
a directory that exists.Returns:Trueiffqdfnexists, elseFalsedn = "/home/chris"
if FT.dfExists(dn):
    # do something
else:
    raise(f"File / Directory {dn} does not exist")makePath(pn)CodeMakes the pathpnincluding any missing parent directories.  Does
nothing if pathpnalready exists.Returns: Nonedn = "/home/chris/appdir/subdir"
FT.makePath(dn)