Classes

Type Casting

File Handling

class nits.file.File

An abstract class simplifying file access through the use of only two functions:

  • read (file)
  • write (data, file):
classmethod read(filename)

return file elements in a generator

classmethod write(data, filename)

write data to filename

class nits.file.Text

Instantiate the File class for a simple text file

classmethod read(filename, comment=None, blanklines=False, strip=True)
  • comment: ignore comments
  • blanklines: ignore blank lines
  • strip: strip write space
classmethod write(data, filename, eol='\n')

write data to filename

class nits.file.CSV

Instantiate the File class for Comma Separated Values (CSV)

classmethod read(filename, header=True, comment=None, fields=None)
  • header: is first line the header?
  • fields: optional list of field values
classmethod write(data, filename=None, fields=None, header=True, append=False, delimiter=', ')
  • fields: optional list of field values
  • header: display header on first line?
  • append: add to existing file?
  • delimiter: what character to use for separating elements

Reporting

class nits.reporter.Reporter(name=None, verbose=False)

A simple timestamping logging class

abort(message, target=None)

report error and exit

say(message, target=None)

report message

warn(message, target=None)

report warning

Functions

Time

Description:
Convenience functions and constants to deal with python’s eclectic date-time packaging conventions
class nits.time.Test_Time(methodName='runTest')

Regression tests for time

nits.time.date2unix(d)

convert python datetime to UNIX time format

nits.time.file2time(f)

get last modification time of file

nits.time.now2str(format='%Y%m%d %H:%M:%S')

current time in string format

nits.time.now2time()

current time in datetime format

nits.time.now2unix()

current time in UNIX format

nits.time.str2unix(s, format='%Y%m%d %H:%M:%S')

parse string to UNIX time format

nits.time.time2file(f, time)

stamp modification time on file

nits.time.unix2date()

Construct a naive UTC datetime from a POSIX timestamp.

nits.time.unix2str(u, format='%Y%m%d %H:%M:%S', zone_offset=0)

create string from UNIX time forfmat

Command Line Scripts

Repeating Commands

Description:
Run the same command on a bunch of files
Usage:
repeatit -c <command> <files>… [-d]
Text replacement options within (-c) command string:
  • %f gets replaced with the file name
  • %n gets replaced with the file name up to the last extension
Options:
-h –help show this screen -c, –command <command> command -f, –files <files> file list -d, –dontwait do not wait for them to complete
Example(s):

Print names of text files:

repeatit -c “echo %f” *.txt

Unzip files in subfolders in place:

repeatit -c “cd %f;unzip *.zip” *

Executing Commands in Folders

Description:
Run another program from this folder
Environment variables:
  • DOITPATH: assign program folder
  • PYTHON: the python executable name (e.g. python3)
Usage:
doit [<command>] [<parameter>…]
Example(s):
doit something.py else.py -v –folder stuff