Parsing a dictionary from a format string

T

Tim Johnson

Currently using python 2.6, but am serving some systems that have
older versions of python (no earlier than.
Question 1:
With what version of python was str.format() first implemented?
Question 2:
Given the following string:
S = 'Coordinates: {latitude}, {longitude}'
Is there a python library that would provide an optimal way
to parse from S the following
{'latitude':"",'longitude':""}
?
Thanks
 
H

Hans Mulder

Currently using python 2.6, but am serving some systems that have
older versions of python (no earlier than.
Question 1:
With what version of python was str.format() first implemented?

That was 2.6, according to the online docs.

Take a look at the documentation that came with your Python
installation. The documentation for str.format ends with:
"New in version 2.6."
Question 2:
Given the following string:
S = 'Coordinates: {latitude}, {longitude}'
Is there a python library that would provide an optimal way
to parse from S the following
{'latitude':"",'longitude':""}
?

Opinions differ. Some people would use the 're' module:

import re

S = 'Coordinates: {latitude}, {longitude}'

keys = re.findall(r'{(\w+)}', S)

print '{' + ', '.join("'" + k + '\':""' for k in keys) + '}'


Other people prefer to use string methods:

S = 'Coordinates: {latitude}, {longitude}'

start = -1
keys = []
while True:
start = S.find('{', start+1)
if start == -1:
break
end = S.find('}', start)
if end > start:
keys.append(S[start+1:end])

print '{' + ', '.join("'" + k + '\':""' for k in keys) + '}'


It might be a matter of taste; it might depend on how familiar
you are with 're'; it might depend on what you mean by 'optimal'.

-- HansM
 
T

Tim Johnson

* Hans Mulder said:
Duh!
It might be a matter of taste; it might depend on how familiar
you are with 're'; it might depend on what you mean by 'optimal'.
As in speed.
## and then there is this - which I haven't tested a lot:
def grabBetween(src,begin,end):
"""Grabs sections of text between `begin' and `end' and returns a list of
0 or more sections of text."""
parts = src.split(begin)
res = []
for part in parts:
L = part.split(end)
if len(L) > 1:
res.append(L[0])
return res

I think later today, I will run some time tests using the `re'
module as well as your function and the one above.

BTW: To be more clear (hopefully) I was checking to see if there was
a compiled method/function to do this.

thanks
 
T

Tim Johnson

* Tim Johnson said:
I think later today, I will run some time tests using the `re'
module as well as your function and the one above.
OK: Functions follow:
def grabBetween(src,begin,end):
"""Grabs sections of text between `begin' and `end' and returns a list of
0 or more sections of text."""
parts = src.split(begin)
res = []
for part in parts:
L = part.split(end)
if len(L) > 1:
res.append(L[0])
return res
def splitExtractDict(src,default):
"""Extract dictionary keys for a format string using
`grabBetween', which uses the `split' string method."""
D = {}
keys = grabBetween(src,'{','}')
for k in keys :
D[k] = default
return D
def reExtractDict(src,default):
"""Extract dictionary keys for a format string using `re'"""
D = {}
keys = re.findall(r'\{([^}]*)\}', src)
for k in keys :
D[k] = default
return D
## From Hans Mulder
def findExtractDict(src,default):
start = -1
keys,D = [],{}
while True:
start = src.find('{', start+1)
if start == -1:
break
end = src.find('}', start)
if end > start:
keys.append(src[start+1:end])
for k in keys :
D[k] = default
return D
###################################################
Now here are results using a small file and a lot of
reps for each function call, just to give some meaningful
times.
###################################################
Using `split' : 0.0309112071991
Using `re.find' : 0.0205819606781
Using `find' : 0.0296318531036
I will note that the last method did not produce
correct results, but I also note that Hans did not
promise tested code :).
It is reasonable to suppose the `re' provides the
faster method.

cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,219
Latest member
KristieKoh

Latest Threads

Top