blog

Source files for chris.bracken.jp
git clone https://git.bracken.jp/blog.git
Log | Files | Refs | Submodules | README | LICENSE

commit f631380c80f1201da7dc886d901248f6aee7daf1
parent 93d3c1b8b898b316a219e4dc486fffdda91998c6
Author: Chris Bracken <chris@bracken.jp>
Date:   Mon,  9 Jan 2023 11:03:25 -0800

Add decay.py script

Takes the specified file and subjects the contents to erosion,
preserving some fraction of the text and randomly replacing the rest
with whitespace.

This is an experiment to see what happens if you create a blog where
posts fade over time through some form of automation, like decaying logs
on the forest floor. I would say that it's some kind of statement on the
impermanence of human life and ideas, but that sounds a hell of a lot
intellectually snobbier than the whole logs in the forest thing.

This should be wired into publish.sh so post contents are eroded on each
publish.

An improvement would be to use the post date as the start of an
exponential decay function with a particular half-life that I could
specify in the script.

Another improvement would be to delete posts once their body has decayed
to nothingness.

Diffstat:
Adecay.py | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 53 insertions(+), 0 deletions(-)

diff --git a/decay.py b/decay.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 + +import random +import sys + +class Article: + def __init__(self, text): + self.text = text + marker = "+++\n" + header_start = text.find(marker) + header_end = text.find(marker, header_start + len(marker)) + len(marker) + self.header = self.text[:header_end] + self.body = self.text[header_end:] + + +def decay(text, maintain_fraction): + """Returns the input string, intact by only maintain_fraction.""" + chars = list(text) + length = len(chars) + remove_count = int((1 - maintain_fraction) * length) + for i in range(0, remove_count): + pos = random.randrange(length) + chars[pos] = " " + return "".join(chars) + + +def main(): + if len(sys.argv) < 2: + sys.stderr.write("usage: decay.py FILE...") + sys.exit(1) + + # Erode and rewrite each file. + maintain_fraction = 0.98 + for filename in sys.argv[1:]: + # Read the file. + file = open(filename, "r") + text = file.read() + file.close() + + # Erode the text. + article = Article(text) + decayed_body = decay(article.body, maintain_fraction) + decayed_contents = "".join([article.header, decayed_body]) + + # Overwrite with the decayed contents. + file = open(filename, "w") + file.write(decayed_contents) + file.close() + return 0 + + +if __name__ == "__main__": + sys.exit(main())