[Tutor] Fwd: Sys.argv read parameters

Dave Angel davea at davea.name
Thu Apr 18 19:34:09 CEST 2013


On 04/18/2013 01:14 PM, Danilo Chilene wrote:
>
>    <SNIP>

The following code has little to do with anything that happened before. 
  So you should start a new thread for this new project.

>
> 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']"

A literal string that happens to have j in it has nothing at all to do 
with the j that you got from json.

>                  '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'

The above loop makes no sense at all for several reasons.  I'd be amazed 
if it happens to produce the output you show below.  Please use 
copy/paste to show us the code you're actually running.

>
>
> 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'].
>

commands[command] is produced from a literal string.  If you want it to 
be an int 27806, you could type that literal instead.

commands = {
                   'uptime' : 27806

Or perhaps you could do

commands = {
                  'uptime' : j['serverStatus']['uptime']

if the values in j are already set up before you initialize the dict 
commands.

> Thanks for the help so far!
>


-- 
DaveA


More information about the Tutor mailing list