473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 3541
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ben moretti | last post by:
hi i'm learning python, and one area i'd use it for is data management in scientific computing. in the case i've tried i want to reformat a data file from a normalised list to a matrix with some...
6
by: Byron | last post by:
Hello, I am a newbie and would like to know if it is possible to convert a string back to a dictionary? For example, I can convert a dictionary to a string by doing this: >>> names =...
1
by: Edward WIJAYA | last post by:
Hi, I am new to Python, and I like to learn more about it. Since I am used to Perl before, I would like to know what is Python equivalent of Perl code below: $filename = $ARGV;
41
by: Xah Lee | last post by:
here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge($pairings) takes a list of...
7
by: Miguel Manso | last post by:
Hi, list. I'm into a psicological doubt that I would like to share with you (you'll know why later on this mail). I'm a programmer with 5 year of experience into Perl. I'm on that point where...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
7
by: manstey | last post by:
Hi, How do I convert a string like: a="{'syllable': u'cv-i b.v^ y^-f', 'ketiv-qere': 'n', 'wordWTS': u'8'}" into a dictionary: b={'syllable': u'cv-i b.v^ y^-f', 'ketiv-qere': 'n', 'wordWTS':...
7
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
10
by: islayer | last post by:
i am trying to write a dictionary using perl, the program would use a primary .pl file, and a text file, designated .dat i have no programing background, and am trying to teach myself perl, and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.