[Tutor] Fwd: Sys.argv read parameters

Danilo Chilene bicofino at gmail.com
Thu Apr 18 19:14:08 CEST 2013


On Thu, Apr 18, 2013 at 10:31 AM, Dave Angel <davea at davea.name> wrote:

> On 04/18/2013 09:21 AM, Danilo Chilene wrote:
>
>> On Wed, Apr 17, 2013 at 8:21 PM, Dave Angel <davea at davea.name> wrote:
>>
>>
>>>   ---------- Forwarded message ----------
>>>
>>>> From: Danilo Chilene <bicofino at gmail.com>
>>>> Date: Wed, Apr 17, 2013 at 2:17 PM
>>>> Subject: Re: [Tutor] Sys.argv read parameters
>>>> To: Danny Yoo <dyoo at hashcollision.org>
>>>>
>>>>
>>>> Hello Danny,
>>>>
>>>> The part that is changing is just the sys.argv[1]
>>>> The vars will have always the same content.
>>>>
>>>> What I don't want is to have like 255 'ifs' if I have 255 vars.
>>>>
>>>>
>>>>  Please don't top post. Put your response AFTER the part you're quoting.
>>>
>>>
>>>
>>>  On Wed, Apr 17, 2013 at 5:03 PM, Danny Yoo <dyoo at hashcollision.org>
>>>> wrote:
>>>>
>>>>
>>>>> What's the part that's "changing"?  What's the part that stays the
>>>>> same?
>>>>>
>>>>>
>>>>>  Are all the strings going to be identical except for the one letter?
>>>  If
>>> so, then read the better responses here, like those from Danny Yoo
>>>
>>>
>>>  The strings will always be the same except for the letter.
>>
>>
> Then in that case, why do you use any if-test?  Just substitute the
> letter, as Danny pointed out.
>
> You never said whether you accept more than one letter from the command
> line.  I've been assuming you do, since you used a loop.
>
> for letter in sys.argv[1].upper():
>     print "This is uppercase of one of the input letters, %s" % letter


Below is the final code:

import requests, json, sys

r = requests.get('http://napmongo01.cvc.com.br:28017/_status')
j = r.json()

arg = sys.argv[1]

commands = {
                'uptime' : "j['serverStatus']['uptime']"
                'globalLock_lockTime' :
"j['serverStatus']['globalLock']['lockTime']"
                'globalLock_currentQueue_total' =
"j['serverStatus']['globalLock']['currentQueue']['total']"
                'globalLock_currentQueue_readers' =
"j['serverStatus']['globalLock']['currentQueue']['readers']"
                'globalLock_currentQueue_writers' =
"j['serverStatus']['globalLock']['currentQueue']['writers']"
                'mem_bits' = "j['serverStatus']['mem']['bits']"
                'mem_resident' = "j['serverStatus']['mem']['resident']"
                'mem_virtual' = "j['serverStatus']['mem']['virtual']"
                'connections_current' =
"j['serverStatus']['connections']['current']"
                'connections_available' =
"j['serverStatus']['connections']['available']"
                'extra_info_heap_usage' =
round(("j['serverStatus']['extra_info']['heap_usage_bytes'])/(1024*124), 2)"
                'extra_info_page_faults' =
"j['serverStatus']['extra_info']['page_faults']"
                'indexCounters_btree_accesses' =
"j['serverStatus']['indexCounters']['btree']['accesses']"
                'indexCounters_btree_hits' =
"j['serverStatus']['indexCounters']['btree']['hits']"
                'indexCounters_btree_misses' =
"j['serverStatus']['indexCounters']['btree']['misses']"
                'indexCounters_btree_resets' =
"j['serverStatus']['indexCounters']['btree']['resets']"
                'indexCounters_btree_missRatio' =
"j['serverStatus']['indexCounters']['btree']['missRatio']"
                'backgroundFlushing_flushes' =
"j['serverStatus']['backgroundFlushing']['flushes']"
                'backgroundFlushing_total_ms' =
"j['serverStatus']['backgroundFlushing']['total_ms']"
                'backgroundFlushing_average_ms' =
"j['serverStatus']['backgroundFlushing']['average_ms']"
                'backgroundFlushing_last_ms' =
"j['serverStatus']['backgroundFlushing']['last_ms']"
                'cursors_totalOpen' =
"j['serverStatus']['cursors']['totalOpen']"
                'cursors_clientCursors_size' =
"j['serverStatus']['cursors']['clientCursors_size']"
                'cursors_timedOut' =
"j['serverStatus']['cursors']['timedOut']"
                'opcounters_insert' =
"j['serverStatus']['opcounters']['insert']"
                'opcounters_query' =
"j['serverStatus']['opcounters']['query']"
                'opcounters_update' =
"j['serverStatus']['opcounters']['update']"
                'opcounters_delete' =
"j['serverStatus']['opcounters']['delete']"
                'opcounters_getmore' =
"j['serverStatus']['opcounters']['getmore']"
                'opcounters_command' =
['server_status']['opcounters']['command']""
                'asserts_regular' =
"j['serverStatus']['asserts']['regular']"
                'asserts_warning' =
"j['serverStatus']['asserts']['warning']"
                'asserts_msg' = "j['serverStatus']['asserts']['msg']"
                'asserts_user' = "j['serverStatus']['asserts']['user']"
                'asserts_rollovers' =
"j['serverStatus']['asserts']['rollovers']"
                'network_inbound_traffic_mb' =
("j['serverStatus']['network']['bytesIn'])/(1024*1024)"
                'network_outbound_traffic_mb' =
("j['serverStatus']['network']['bytesOut'])/(1024*1024)"
                'network_requests' =
"j['serverStatus']['network']['numRequests']"
                'write_backs_queued' =
"j['serverStatus']['writeBacksQueued']"
                'logging_commits' = "j['serverStatus']['dur']['commits']"
                'logging_journal_writes_mb' =
"j['serverStatus']['dur']['journaledMB']"
                'logging_datafile_writes_mb' =
"j['serverStatus']['dur']['writeToDataFilesMB']"
                'logging_commits_in_writelock' =
"j['serverStatus']['dur']['commitsInWriteLock']"
                'logging_early_commits' =
"j['serverStatus']['dur']['earlyCommits']"
                'logging_log_buffer_prep_time_ms' =
"j['serverStatus']['dur']['timeMs']['prepLogBuffer']"
                'logging_journal_write_time_ms' =
"j['serverStatus']['dur']['timeMs']['writeToJournal']"
                'logging_datafile_write_time_ms' =
"j['serverStatus']['dur']['timeMs']['writeToDataFiles']"
}

for command in commands:
    if arg in commands:
        print commands[command]
    else:
        print 'Invalid command'


Below the output:
(danilochilene at notebico - ~/lixo @14:06:56)
2: python mongo-test.py uptime
j['serverStatus']['uptime']

(danilochilene at notebico - ~/lixo @14:07:00)
2: python -i mongo-test.py uptime
j['serverStatus']['uptime']
>>> j['serverStatus']['uptime']
27806
>>>

My only issue now is commands[command] (as the example above) have the same
output as j['serverStatus']['uptime'].

Thanks for the help so far!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130418/09365b10/attachment-0001.html>


More information about the Tutor mailing list