nested dictionaries and functions in data structures.

Jean-Michel Pichavant jeanmichel at sequans.com
Tue Jan 7 05:21:26 EST 2014



----- Original Message -----
> Hello all.
> 
> I have some questions again. :-)
> 
> I wish to be able to place a function within a data structure. I
> would like to use a dictionary because I could pass it a key and
> then the function could be called. I  couldn't find anything on the
> net to show me how to do this. More then likely, not using the right
> search terms.
> 
> For example:
> 
> funct_call = { 'bhp' : 'bhp_counters (addict[key1][key2])', 'ospf' :
> 'ospf_counters (addict[key1][key2])'}
> 
> I am sure there is a way to do this.
> 
> The other issue is I cannot nest dictionaries. I have seen examples
> and when I apply them to the code below. They do not provide the
> result I want. The program does 3 actions.
> 
> 
> 1. Opens all the files in the directory. Each file begins with
> "data_". The 6 character in the file name is the occurrence of the
> output. Ranging from 1 to 9. The8th character plus the remaining
> part of the file is the output of the command. For example:
> 
> data_1_ospf.txt
> 
> The commands stored in this file are related to OSPF. When I build
> the nested dictionary I want to have "OSPF" as the primary key.
> Nested under "OSPF" is the number of times the command has been
> captured. The file content stored as an array and is the value of
> the 2nd key.  data structure could look like this:
> 
> outputs = { 'ospf' : { '1' : lines_of_file[], '2' : lines of_file[]}
> }
> 
> Below is the code I have used to try and achieve the above. I just
> don't get the nested dictionary effect I am after. Again, I am
> trying to use standard core which some of the examples I have seen
> appeared to use. I am aware of collection module.
> 
> #! /usr/bin/env python
> 
> # Identifying if memory leaks are occurring.
> # goal is to import output to Excel.
> # created on 28 Dec 2013 By Sean Murphy
> 
> import os, sys
> from os.path import exists
> 
> # main code begins
> 
> if len(sys.argv) >= 2:
>     # storing file names into variable from command line.
>     filenames = sys.argv[1:]
> else:
>     filenames = os.listdir(os.getcwd())
> #    print ("Error, must provide at least one file name\n")
> #    quit()
> 
> outputs = {} # empty dictionary (hash)
> capture = "" # key used for the capture version
> command = "" # key for the command output
> 
> for filename in filenames:
>     if exists(filename):
>         fp = open(filename, "r")
>         capture = filename[6]
>         command = filename[8:]
>         # nested dictionary. Command and then number of captures.
>         outputs = {command : { capture :[fp.readlines() } }
>         fp.close()
>     else:
>         print ("error %s doesn't exists\n" % filename)
>         quit()
> 
> print ("%r\n" % outputs.keys())
> for key in sorted(outputs):
>     print (outputs[key].keys ())
> 
> 
> Cheers
> Sean

outputs keeps track of the last loop only because you're assigning a new dict on every loop. You need to update the current dict instead.

try to replace
outputs = {command : { capture :fp.readlines() } }

with (untested code)

if command not in outputs:
  outputs[command] = {}
outputs[command][capture] = fp.readlines()

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


More information about the Python-list mailing list