Skip to content

Instantly share code, notes, and snippets.

@dannguyen
Forked from lecram/escher.py
Created December 14, 2012 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dannguyen/4281710 to your computer and use it in GitHub Desktop.
Save dannguyen/4281710 to your computer and use it in GitHub Desktop.
Just typo fixing
#! /usr/bin/env python
"""{escher} -- one-file key-value storage.
What?
This is a toy application to manage persistent key-value string data.
The file {escher} is *both* the application and its data.
When you run any of the commands below, the file will be executed and,
after data change, it will rewrite itself with updated data.
You can copy the file with whatever name to create multiple datasets.
Remember that the data will be copied with the file. Each file must
be readable, writable, and executable.
Why?
I don't know; this is probably useless.
Usage:
Listing current keys:
$ {escher}
Getting the value of the foo key:
$ {escher} foo
(NOTE: If foo is not in the dataset, exit code will be 1.)
Setting the value of foo to bar:
$ {escher} foo bar
Removing the key foo:
$ {escher} foo ""
(NOTE: If foo is not in the dataset, exit code will be 1.)
Removing everything:
$ {escher} --clean
Printing this help message:
$ {escher} --help
"""
import sys
import os
import re
__doc__ = __doc__.format(escher=os.path.basename(__file__))
data = {}
def lifeboat(msg, code):
print(msg)
exit(code)
args = sys.argv[1:]
print([
lambda : "\n".join(sorted(data.keys())),
lambda key : __doc__ if key == "--help" else
not data.clear() and "X *" if key == "--clean" else
data.get(key, False) or
lifeboat("Error: key not found.", 1),
lambda key, value : not data.update([(key, value)]) and
" ".join([key, "<-", value]) if value else
data.pop(key, True) and "X " + key if key in data else
lifeboat("Error: key not found.", 1),
lambda fu, ti, le : lifeboat("Error: invalid argument list.\n" + __doc__, 2)
][min(len(args), 3)](*args[:3]))
f = open(__file__, "r")
out = re.sub('^data = \{.*\}$', "data = " + str(data),
f.read(), 1, re.MULTILINE)
f.close()
f = open(__file__, "w")
f.write(out)
f.close()
@lecram
Copy link

lecram commented Dec 14, 2012

Thank you. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment