Skip to content

Python library for writing program output in make compatible way.

License

Notifications You must be signed in to change notification settings

andrewguy9/safeoutput

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

safeoutput

Safeoutput.open returns a file handle like object which can use used to write data to disk. It is meant to be used with python's with syntax.

If an exception is thrown, safeoutput automatically deletes the file. This ensures partially rendered output is not left sitting around.

If your handle leaves scope, without an exception being thrown, the file is atomically flipped into the desired location.

Safeoutput uses temporary files to store writes, so your file contents do not have to fit in memory.

Usage

Python

import sys
import safeoutput

def calc(line):
  return line+1

input_fname = sys.argv[1]
output_fname = sys.argv[2]

with open(input_fname, 'r') as input:
  with safeoutput.open(output_fname) as output:
    for line in input.xreadlines():
      output.write(str(calc(int(line))))

Command line

$ safeoutput
usage: safeoutput [-h] [--binary] FILE

The example below ensures bar is not written until the pipe is closed.

(sleep 10; echo foo ${RANDOM} ) | \
  safeoutput --binary bar

cat bar

About

Python library for writing program output in make compatible way.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published