You are not logged in.

#1 2010-01-18 01:23:31

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

python tool for keeping track of strings

I wrote this just now. It associates keys to strings; basically a centralized means of storing values.

#!/usr/bin/env python
from cPickle import load, dump
from sys import argv
from os.path import expanduser

strings_file = expanduser('~/lib/cfg-strings')

try:
  with open(strings_file) as f:
    strings = load(f)
except:
  strings = {}

if len(argv) < 2:
  print('''usage:
  {0} dump
  {0} get  <key>
  {0} del  <key>
  {0} set  <key> <val>'''.format(argv[0]))
elif len(argv) == 2:
  if argv[1] == 'dump':
    for k in strings.keys(): print(k + ': ' + strings[k])
elif len(argv) == 3:
  if argv[1] == 'get':
    if argv[2] in strings.keys():
      print(strings[argv[2]])
  elif argv[1] == 'del':
    if argv[2] in strings.keys():
      del(strings[argv[2]])
elif len(argv) == 4:
  if argv[1] == 'set':
    strings[argv[2]] = argv[3]

with open(strings_file, 'w') as f:
  dump(strings, f)

Replace '~/lib/cfg-strings' with your preferred destination for the pickle file.

As an example, I have this at the end of my .xinitrc:

exec $(cfg get wm)

so all I have to do is type "cfg set wm ..." to change my window manager. Note that on my system, the script is named 'cfg', so you'll want to change that depending on what you call it.

To be honest, though, I think everyone has written something like this at least once.

Last edited by Peasantoid (2010-01-18 01:29:14)

Offline

#2 2010-01-18 03:33:43

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: python tool for keeping track of strings

With so many parsing options, you might want to look at OptionParser (in the python library) or my (shameless plug) opterator decorator to make it a bit more maintainable.

Dusty

Offline

#3 2010-01-18 18:58:47

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: python tool for keeping track of strings

Dusty wrote:

With so many parsing options, you might want to look at OptionParser (in the python library) or my (shameless plug) opterator decorator to make it a bit more maintainable.

Well, not to put the diss on either of those, but a) this doesn't really need to be maintained and b) the way I'm doing it now is far more suited given that the tool's function is really very simple. I'm one of those people who belongs to the 'simple, not clever' school of thought. (Also, OptionParser is a damn pain. Your opterator seems pretty slick even at first glance, though.)

Offline

#4 2010-01-18 22:24:02

Yannick_LM
Member
Registered: 2008-12-22
Posts: 142

Re: python tool for keeping track of strings

@dusty : the "optparse decorator" is truly a great idea smile

Offline

#5 2010-02-14 09:20:57

mikesd
Member
From: Australia
Registered: 2008-02-01
Posts: 785
Website

Re: python tool for keeping track of strings

Nice idea Peasantoid! I have wanted something similar for myself for a while now however wasn't exactly sure how best to do this. Here's my version. It is based on yours though as I prefer plain text for the settings file so I used JSON.

#!/usr/bin/python

import json
import os.path
import sys

SETTINGS_FILE = os.path.expanduser('~/configs/settings.json')

def dump(s):
    print json.dumps(s, sort_keys = True, indent=2)

def get(s, key):
    if s.has_key(key):
        print s[key]

def set(s, key, val):
    s[key] = val
    save(s)

def delete(s, key):
    if s.has_key(key):
        del s[key]
        save(s)

def save(s):
    json.dump(s, open(SETTINGS_FILE, 'w'))

def usage():
    str = [
        "usage: %s dump (default)",
        "       %s get <key>",
        "       %s set <key> <val>",
        "       %s delete <key>"
    ]

    for x in str:
        print x % sys.argv[0]

def main():
    try:
        settings = json.load(open(SETTINGS_FILE))
    except:
        settings = {}

    a = sys.argv
    n = len(a)
    if n == 1 or (n == 2 and a[1] == 'dump'):
        dump(settings)
    elif n == 3 and a[1] == 'get':
        get(settings, a[2])
    elif n == 3 and a[1] == 'delete':
        delete(settings, a[2])
    elif n == 4 and a[1] == 'set':
        set(settings, a[2], a[3])
    else:
        usage()

if __name__ == "__main__":
    main()

Offline

Board footer

Powered by FluxBB