469,317 Members | 1,903 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,317 developers. It's quick & easy.

Perl Dictionary Convert

I have a server program that I am writing an interface to and it returns
data in a perl dictionary. Is there a nice way to convert this to
something useful in Python?

Here is some sample data:

200 data follow
{
Calendar = {
Access = { anyone = lr;};
Class = IPF.Appointment;
Messages = 0;
Size = 0;
UIDNext = 1;
UIDValidity = 287898056;
Unseen = 0;
};
Contacts = {
Class = IPF.Contact;
Messages = 0;
Size = 0;
UIDNext = 1;
UIDValidity = 287898056;
Unseen = 0;
};
}

-Tom

Jul 5 '06 #1
2 2883
data.replace('=', ':').replace(';', ',')
then eval in a namespace object whose __getitem__ method returns its
argument unchanged.
class not_str(str): # take care of that IPF.Contact
def __getattr__(self, attr):return self + '.' + attr

class not_dict(dict):
def __getitem__(self, name):return not_str(name)

eval(data.replace('=',':').replace(';', ','), not_dict())

Tom Grove wrote:
I have a server program that I am writing an interface to and it returns
data in a perl dictionary. Is there a nice way to convert this to
something useful in Python?

Here is some sample data:

200 data follow
{
Calendar = {
Access = { anyone = lr;};
Class = IPF.Appointment;
Messages = 0;
Size = 0;
UIDNext = 1;
UIDValidity = 287898056;
Unseen = 0;
};
Contacts = {
Class = IPF.Contact;
Messages = 0;
Size = 0;
UIDNext = 1;
UIDValidity = 287898056;
Unseen = 0;
};
}

-Tom
Jul 5 '06 #2
In <ma***************************************@python. org>, Tom Grove
wrote:
I have a server program that I am writing an interface to and it returns
data in a perl dictionary. Is there a nice way to convert this to
something useful in Python?
You could write a little parser with pyparsing:

source = """\
{
Calendar = {
Access = { anyone = lr;};
Class = IPF.Appointment;
Messages = 0;
Size = 0;
UIDNext = 1;
UIDValidity = 287898056;
Unseen = 0;
};
Contacts = {
Class = IPF.Contact;
Messages = 0;
Size = 0;
UIDNext = 1;
UIDValidity = 287898056;
Unseen = 0;
};
}
"""

from pyparsing import alphas, alphanums, nums, Dict, Forward, Group, \
Literal, OneOrMore, Suppress, Word

OPEN_BRACE = Suppress('{')
CLOSE_BRACE = Suppress('}')
ASSIGN = Suppress('=')
STATEMENT_END = Suppress(';')
identifier = Word(alphas, alphanums + '.')
number = Word(nums).setParseAction(lambda s, loc, toks: int(toks[0]))
dictionary = Forward()
value = identifier | number | dictionary
item = Group(identifier + ASSIGN + value + STATEMENT_END)
dictionary << Dict(OPEN_BRACE + OneOrMore(item) + CLOSE_BRACE)

result = dictionary.parseString(source)
print result['Contacts']['Class']

Output is: IPF.Contact

Ciao,
Marc 'BlackJack' Rintsch
Jul 6 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by ben moretti | last post: by
6 posts views Thread by Byron | last post: by
1 post views Thread by Edward WIJAYA | last post: by
7 posts views Thread by Miguel Manso | last post: by
reply views Thread by Kirt Loki Dankmyer | last post: by
7 posts views Thread by Andrew Robinson | last post: by
10 posts views Thread by islayer | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.