Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8023 > unrolled thread

Parsing a dictionary from a format string

Started byTim Johnson <tim@johnsons-web.com>
First post2011-06-20 10:14 -0800
Last post2011-06-20 14:39 -0800
Articles 4 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Parsing a dictionary from a format string Tim Johnson <tim@johnsons-web.com> - 2011-06-20 10:14 -0800
    Re: Parsing a dictionary from a format string Hans Mulder <hansmu@xs4all.nl> - 2011-06-20 22:08 +0200
      Re: Parsing a dictionary from a format string Tim Johnson <tim@johnsons-web.com> - 2011-06-20 12:49 -0800
      Re: Parsing a dictionary from a format string Tim Johnson <tim@johnsons-web.com> - 2011-06-20 14:39 -0800

#8023 — Parsing a dictionary from a format string

FromTim Johnson <tim@johnsons-web.com>
Date2011-06-20 10:14 -0800
SubjectParsing a dictionary from a format string
Message-ID<mailman.187.1308593682.1164.python-list@python.org>
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
-- 
Tim 
tim at johnsons-web dot com or akwebsoft dot com
http://www.akwebsoft.com

[toc] | [next] | [standalone]


#8029

FromHans Mulder <hansmu@xs4all.nl>
Date2011-06-20 22:08 +0200
Message-ID<4dffa8b9$0$49179$e4fe514c@news.xs4all.nl>
In reply to#8023
On 20/06/11 20:14:46, Tim Johnson wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#8031

FromTim Johnson <tim@johnsons-web.com>
Date2011-06-20 12:49 -0800
Message-ID<mailman.198.1308602942.1164.python-list@python.org>
In reply to#8029
* Hans Mulder <hansmu@xs4all.nl> [110620 12:15]:
> On 20/06/11 20:14:46, Tim Johnson wrote:
> >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?
 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
-- 
Tim 
tim at johnsons-web dot com or akwebsoft dot com
http://www.akwebsoft.com

[toc] | [prev] | [next] | [standalone]


#8036

FromTim Johnson <tim@johnsons-web.com>
Date2011-06-20 14:39 -0800
Message-ID<mailman.199.1308609532.1164.python-list@python.org>
In reply to#8029
* Tim Johnson <tim@johnsons-web.com> [110620 13:00]:
> 
> 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
-- 
Tim 
tim at johnsons-web dot com or akwebsoft dot com
http://www.akwebsoft.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web