Skip to content

Instantly share code, notes, and snippets.

@dyoo
Last active December 16, 2015 09:49
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 dyoo/5415464 to your computer and use it in GitHub Desktop.
Save dyoo/5415464 to your computer and use it in GitHub Desktop.
Example of revised code to interpret commands.
class p(object):
def __init__(self, *elts):
"""Construct a representation of a path.
(We'll cheat a little by using a list of strings.)"""
self.path = elts
def interp(self, data):
for p in self.path:
data = data[p]
return data
commands = {
'uptime' : p('serverStatus', 'uptime'),
'globalLock_lockTime' : p('serverStatus', 'globalLock', 'lockTime')
## Fill me in with more elements.
}
def interp(data, cmdName):
cmd = commands[cmdName]
return cmd.interp(data)
######################################################################
## For example, let's mock up some sample data and see how this works.
sampleData = {'serverStatus' : {'uptime': 3,
'cpu load' : 1.11,
'globalLock' : { 'lockName' : 'sample lock name',
'lockTime' : 42 }}}
print interp(sampleData, 'uptime')
print interp(sampleData, 'globalLock_lockTime')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment