Create name value pairs in python
Question: I have a python script that has the following output stored in a variable
called jvmData:

    
    
    Stats name=jvmRuntimeModule, type=jvmRuntimeModule#
    {
    name=HeapSize, ID=1, description=The total memory (in KBytes) in the Java virtual machine run time., unit=KILOBYTE, type=BoundedRangeStatistic, lowWaterMark=1048576, highWaterMark=1048576, current=1048576, integral=0.0, lowerBound=1048576, upperBound=2097152
    
    name=FreeMemory, ID=2, description=The free memory (in KBytes) in the Java virtual machine run time., unit=KILOBYTE, type=CountStatistic, count=348466
    
    name=UsedMemory, ID=3, description=The amount of used memory (in KBytes) in the Java virtual machine run time., unit=KILOBYTE, type=CountStatistic, count=700109
    
    name=UpTime, ID=4, description=The amount of time (in seconds) that the Java virtual machine has been running., unit=SECOND, type=CountStatistic, count=3706565
    
    name=ProcessCpuUsage, ID=5, description=The CPU Usage (in percent) of the Java virtual machine., unit=N/A, type=CountStatistic, count=0
    }
    

What I would like to do is simply print out name/value pairs for the important
parts, which in this case would simply be:

    
    
    HeapSize=1048576
    FreeMemory=348466
    UsedMemory=700109
    UpTime=3706565
    ProcessCpuUsage=0
    

Im not at all good with python :) The only solution in my head seems very
long-winded? Split the lines, throw away first, second and last lines, then
loop through each line with different cases (sometimes current, sometimes
count) for finding the length of string, etc etc

Perhaps (well definitely) I am missing something some nice function I can use
to put these into the equivalent of a java hashmap or something?


Answer: The "equivalent of a java HashMap" would be called a dictionary in python. As
for how to parse this, just iterate over the lines that contain the data, make
a dict of all key/value pairs in the line and have a special case for the
`HeapSize`:

    
    
    jvmData = "..." #the string holding the data
    jvmLines = jvmData.split("\n") #a list of the lines in the string
    lines = [l.strip() for l in jvmLines if "name=" in l] #filter all data lines
    result = {}
    for line in lines:
        data = dict(s.split("=") for s in line.split(", "))
        #the value is in "current" for HeapSize or in "count" otherwise
        value = data["current"] if data["name"] == "HeapSize" else data["count"]
        result[data["name"]] = value
    

As you seem to be stuck on Jython2.1, here's a version that should work with
it (obviously untested). Basically the same as above, but with the list
comprehension and generator expression replaced by `filter` and `map`
respectively, and without using the ternary `if/else` operator:

    
    
    jvmData = "..." #the string holding the data
    jvmLines = jvmData.split("\n") #a list of the lines in the string
    lines = filter(lambda x: "name=" in x, jvmLines) #filter all data lines
    result = {}
    for line in lines:
        data = dict(map(lambda x: x.split("="), line.split(", ")))
        if data["name"] == "HeapSize":
            result[data["name"]] = data["current"]
        else:
            result[data["name"]] = data["count"]
    

