1

In Python I am using the RRDTool Python wrapper to store and read values into / from a RRD-Database.

The RRDTool for Python is a wrapper for the C-based source / command-line utility of rrdtool.

After creating a database I want to read out it's header using the python command:

header_info = rrdtool.info('database_file_name.rrd') 

which is equal to the command line utility:

rrdtool info database_file_name.rd

and which would print header-infos like that:

filename = "database_file_name.rrd"
rrd_version = 5
...
ds[datasource_identifier_1].index = 0
ds[datasource_identifier_1].type = "GAUGE"
...
ds[datasource_identifier_2].index = 1
ds[datasource_identifier_2].type = "GAUGE"
...

In python the output of the command line tool is wrapped inside a single big dictionary with the following schema:

key: value
"filename" : "database_file_name.rrd"
"ds[datasource_identifier_1].index" : "0"
"ds[datasource_identifier_2].type" : "GAUGE"

I am now trying to figure out how to split up that dictionary so that I could access it like that:

index = dictionary["ds"]["datasource_identifier_1"]["index"]

But I do not have a clue how to do that using python. I would guess this can be done with iteration over the original dictionary and split up those keys using "[","]" and "." as triggers and create a new dictionary then.

How can I do that in Python?

Simon Kemper
  • 635
  • 1
  • 7
  • 16

1 Answers1

1

We need to parse the keys to see if they look like ds[some_identifier].type etc.

def process_dict(dictionary):
    import re    
    rgx = re.compile(r"^(ds)\[(.+?)\]\.(index|type)$")

    processed = {}

    for k, v in dictionary.items():
        # does k have the format ds[some_key].index etc
        m = rgx.search(k)
        if m:
            # create the embedded path
            ds, ident, attr = m.groups()
            processed.setdefault(ds, {}).setdefault(ident, {})[attr] = v
        else:
            processed[k] = v

    return processed
donkopotamus
  • 22,114
  • 2
  • 48
  • 60