Skip to content

Instantly share code, notes, and snippets.

@MSoup
Last active May 12, 2020 08:35
Show Gist options
  • Save MSoup/d1035a4ca5ff75176cb7b4726d9dca29 to your computer and use it in GitHub Desktop.
Save MSoup/d1035a4ca5ff75176cb7b4726d9dca29 to your computer and use it in GitHub Desktop.
File based key value store part 1

run as

$ kv-read -k KEY -d FILENAME

Returns json value for user given key.

#!/usr/bin/env python3
import argparse
import json
parser = argparse.ArgumentParser(description='Get Ghibli Films', usage="kv-read -k KEY -d FILENAME")
parser.add_argument('-k', dest='k',required=True,help='request key k from dictionary')
parser.add_argument('-d', dest='d',required=True,help='choose json d to acquire value from')
args = parser.parse_args()
try:
with open(args.d, 'r') as f:
d = json.load(f)
try:
print(d[args.k])
except KeyError:
print(f'Error: No data for key \"{args.k}\" found in data file "{args.d}"')
except:
print(f'Error: Data file "{args.d}" not found')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment