Last active
December 16, 2015 09:49
-
-
Save dyoo/5415464 to your computer and use it in GitHub Desktop.
Example of revised code to interpret commands.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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