Skip to main content

A set of useful utilities for python programs

Project description

Tests Codecov Python PyPI

ccautils

a set of utilities for python3.6+ programs and scripts.

Install

Install for the user:

pip3 install ccautils --user

Install for a virtual environment:

pip install ccautils

Development

I use poetry to manage these utilities. Clone this repository and install poetry, then install the dependancies.

git clone https://github.com/ccdale/ccautils.git
cd ccautils
poetry install

Testing

To run the tests you must have pytest, nox and poetry installed.

install nox into your python user environment.

pip install nox --user

Run the tests with

nox -rs tests

Run the linter with

nox -rs lint

Run the console ask tests with

nox -rs tests -- -sm ask

Error Utilities(#headdd)

See the code for how to use these Exception helpers.

Miscellaneous Utilities

Usage

import ccautils.utils as UT

addToString(xstr, xadd)

Code

Returns a string with xadd appended to xstr. If xadd is a list, all str members of the list will be appended in order.

UT.addToString("hello", [" ", "world"])

> "hello world"

delimitString(xstr, delimeter=" - ")

Code

xstr can be a list or a string. If it is a string, it is spit apart at spaces and delimeted with delimeter. If it is a list, each member is delimeted with delimeter.

UT.delimitString(["bright", "world"], " ")

> "bright world"

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

> "I.wandered.lonely.as.an.artichoke"

makeDictFromString(istr)

Code

Constructs a dictionary from a string of parameters. Leading and trailing whitespace is stripped.

istr should be in the form someparam=somevalue,someotherparam=otherval

UT.makeDictFromString("sparam=sval, soparam = soval")

> {"sparam": "sval", "soparam": "soval"}

askMe(q, default)

Code

Requests input from the user. Poses the question q. Returns the users input or default if no input given.

UT.askMe("press 5, please", "8")

> press 5, please: 5
> 5

padStr(xstr, xlen=2, pad=" ", padleft=True)

Code

Returns xstr padded to the required length, either on the left (padleft is True) or the right (padleft is False)

UT.padStr("23", 5, "0")

> "00023"

reduceTime(unit, secs)

Code

Divides secs by unit returning a tuple of (units, remainder)

Raises a ValueError if unit is zero.

UT.reduceTime(3600, 3700)

> (1, 100)

displayValue(val, label, zero=True)

Code

Pluralises label if val > 1 or val is 0.

Will return an empty string if val == 0 and zero == True

UT.displayValue(12, "table")

> "12 tables"

secondsFromHMS(shms)

Code

converts HMS strings into integer seconds

UT.secondsFromHMS("01:01:23.43")
# 1 hour, 1 minute, 23 seconds + 0.43 second

> 3683

hms(secs, small=True, short=True, single=False, colons=False)

Code

Convert secs to days, hours, minutes and seconds

if small is True then only return the higher values if they are > zero

if short is True then the labels are their short form

if single is True then the labels are single letters

if colons is True then the output is of the form 01:03:23

UT.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"

File Utilities

Usage

import ccautils.fileutils as FT

fileExists(fqfn)

Code

Tests for the existence of the fully-qualified (absolute) file name fqfn

Returns: True if fqfn exists, else False

fn = "/home/chris/output.csv"
if FT.fileExists(fn):
    # do something
else:
    raise(f"File {fn} does not exist")

dirExists(fqdn)

Code

Tests for the existence of the fully-qualified (absolute) directory name fqdn

Returns: True if fqdn exists, else False

dn = "/home/chris"
if FT.dirExists(dn):
    # do something
else:
    raise(f"Directory {dn} does not exist")

dfExists(fqdfn)

Code

Tests to see if the file fqdfn exists, if not checks if fqdfn is a directory that exists.

Returns: True if fqdfn exists, else False

dn = "/home/chris"
if FT.dfExists(dn):
    # do something
else:
    raise(f"File / Directory {dn} does not exist")

makePath(pn)

Code

Makes the path pn including any missing parent directories. Does nothing if path pn already exists.

Returns: None

dn = "/home/chris/appdir/subdir"
FT.makePath(dn)

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

ccautils-0.4.1.tar.gz (23.1 kB view details)

Uploaded Source

Built Distribution

ccautils-0.4.1-py3-none-any.whl (21.9 kB view details)

Uploaded Python 3

File details

Details for the file ccautils-0.4.1.tar.gz.

File metadata

  • Download URL: ccautils-0.4.1.tar.gz
  • Upload date:
  • Size: 23.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0 CPython/3.8.1 Linux/5.5.7-1-MANJARO

File hashes

Hashes for ccautils-0.4.1.tar.gz
Algorithm Hash digest
SHA256 acf52548348824c0876c236134e55d86dd5d939125e450cf23cf4ec700d05169
MD5 47b1d03dde4424db6561d8927232d63e
BLAKE2b-256 92646976f9373ce91d47d68e5e9e7d75ba221939e8e0fd1c1a57a694275a7850

See more details on using hashes here.

Provenance

File details

Details for the file ccautils-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: ccautils-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 21.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0 CPython/3.8.1 Linux/5.5.7-1-MANJARO

File hashes

Hashes for ccautils-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a20027a3508685e4362aa23b47ae79de3a90f064b247ad3e4442a5c178065e35
MD5 e2671fa91ee54f0dcb3d3ff78a13ce0a
BLAKE2b-256 f69bea8d7c89d96acb77f51997ad52df04fb25b4aa391017bafba3dcd4dee9c6

See more details on using hashes here.

Provenance

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