By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,538 Members | 1,428 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,538 IT Pros & Developers. It's quick & easy.

Perl Dictionary Convert

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.