Assistance with re module script
<p>I posted this question earlier <a href="http://stackoverflow.com/questions/5344547/approach-for-parsing-file-and-creating-dynamic-data-structure-for-use-by-another">Approach for parsing file and creating dynamic data structure for use by another program</a> and got some good suggestions. </p>

<p>So now I have something working and I want to see if there is a better way of doing it. </p>

<p>Background Information:</p>

<p>I basically need to parse a static properties file that I cannot modify at this time. Read some values from it and put it into a data structure that I can use in some scripts that I do have control of. The problem I'm facing is that I want to group "like" key/values together so I can access them in a loop later in my scripts. For example, my property file looks something like this actually.  </p>

<pre><code>somerandom.propname=value1
myhostname.APP_HOME=c:\\apps\\apphome
myhostname.DomainName=sampleDomain
myhostname.data.dir=D:\\data\\
sampleDomain.host=myhostname
sampleDomain.port=80
sampleDomain.sslport=443
sampleDomain.ClusterManagedServer1.port=8080
sampleDomain.ClusterManagedServer2.port=8012
sampleDomain.ClusterManagedServer1.sslport=8021
sampleDomain.ClusterManagedServer2.sslport=8022
sampleDomain.ClusterManagedServer1.name=MS1
sampleDomain.ClusterManagedServer2.name=MS2
sampleDomain.ClusterManagedServer1.host=myhostname
sampleDomain.ClusterManagedServer2.host=myHOST2
sampleDomain.ClusterManagedServer3.port=8031
sampleDomain.ClusterManagedServer4.port=8042
sampleDomain.ClusterManagedServer3.sslport=8043
sampleDomain.ClusterManagedServer4.sslport=8053
sampleDomain.ClusterManagedServer3.name=MS3
sampleDomain.ClusterManagedServer4.name=MS4
sampleDomain.ClusterManagedServer3.host=myhostHOST3
sampleDomain.ClusterManagedServer4.host=myhostHOST4
sampleDomain.AdminAccount=samplesuperadmin
sampleDomain.ServerName=myhostname222
sampleDomain.ServerPort=8008
sampleDomain.HeapSize=1234m

somerandom.propname=value2
anotherhostname.APP_HOME=c:\\apps\\apphome
anotherhostname.DomainName=sampleDomain
anotherhostname.data.dir=D:\\data\\
testDomain.host=anotherhostname
testDomain.port=80
testDomain.sslport=443
testDomain.ClusterManagedServer1.port=8080
testDomain.ClusterManagedServer2.port=8012
testDomain.ClusterManagedServer1.sslport=8021
testDomain.ClusterManagedServer2.sslport=8022
testDomain.ClusterManagedServer1.name=MS1
testDomain.ClusterManagedServer2.name=MS2
testDomain.ClusterManagedServer1.host=anotherhostname
testDomain.ClusterManagedServer2.host=anotherHOST2
testDomain.ClusterManagedServer3.port=8031
testDomain.ClusterManagedServer4.port=8042
testDomain.ClusterManagedServer3.sslport=8043
testDomain.ClusterManagedServer4.sslport=8053
testDomain.ClusterManagedServer3.name=MS3
testDomain.ClusterManagedServer4.name=MS4
testDomain.ClusterManagedServer3.host=anotherHOST3
testDomain.ClusterManagedServer4.host=anotherHOST4
testDomain.AdminAccount=superadminaccount
testDomain.ServerName=myservernamehere
testDomain.ServerPort=80
testDomain.HeapSize=1355m
</code></pre>

<p>So what I'm trying to do is match up all values of the "ClusterManagedServer#" where #=the number into some structure so I can loop thru it and for each host confgure the name, port, etc. </p>

<p>I have the following code working (just a proof of concept) and I have to use the existing modules that come with an earlier version of python so I can't download any modules and install them.</p>

<pre><code>import re
from socket import gethostname

class Ddict(dict):
    def __init__(self, default=None):
        self.default = default

    def __getitem__(self, key):
        if not self.has_key(key):
            self[key] = self.default()
        return dict.__getitem__(self, key)


propertyfile_name = 'SystemConfig.properties'

hname=gethostname()
print 'hostname is', hname

print '\ngetting domainname\n'
RE4 =(hname+'.DomainName=(\S+)(?:\s+|\Z)')

with open(propertyfile_name) as f:
    for (x) in re.findall(RE4,f.read()):
        dName=x
        print 'domain name is: ',dName

RE = (dName+'.ClusterManagedServer(\d+)\.host=(\S+)(?:\s+|\Z)')
RE1 =(dName+'.ClusterManagedServer(\d+)\.name=(\S+)(?:\s+|\Z)')
RE2 =(dName+'.ClusterManagedServer(\d+)\.sslport=(\d+)\s+' )
RE3 =(dName+'.ClusterManagedServer(\d+)\.port=(\d+)\s+' )

dico = Ddict( dict )

print '\n\nGetting values from property file:'


with open(propertyfile_name) as f:
    for (server,h) in re.findall(RE,f.read()):
        dico[server]['host']=h    

with open(propertyfile_name) as f:
    for (server,n) in re.findall(RE1,f.read()):
        dico[server]['name']=n    
        #print dico 

with open(propertyfile_name) as f:
    for (server,s) in re.findall(RE2,f.read()):
        dico[server]['sslport']=s    
        #print dico 

with open(propertyfile_name) as f:
    for (server,p) in re.findall(RE3,f.read()):
        dico[server]['port']=p   
        #print dico 

#print '\ndico is:'
#print dico

print '\n**for loop to print out values in dictionary **\n'
keys = dico.keys()
keys.sort()
for k in keys:
     print '\n'
     #print k
     #print dico[k]
     print dico[k]['name']
     print dico[k]['sslport']
     print dico[k]['host']
     print dico[k]['port']
     print '\n'
</code></pre>

<p>My question is, is there a better way to parse the file and get what I need instead of having to open the file 4 times to get the specific values?</p>

<p>Thanks</p>
