Perl Dictionary Convert

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom Grove

    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

  • faulkner

    #2
    Re: Perl Dictionary Convert

    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__(sel f, attr):return self + '.' + attr

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

    eval(data.repla ce('=',':').rep lace(';', ','), 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

    Comment

    • Marc 'BlackJack' Rintsch

      #3
      Re: Perl Dictionary Convert

      In <mailman.7824.1 152131388.27775 .python-list@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).setP arseAction(lamb da s, loc, toks: int(toks[0]))
      dictionary = Forward()
      value = identifier | number | dictionary
      item = Group(identifie r + ASSIGN + value + STATEMENT_END)
      dictionary << Dict(OPEN_BRACE + OneOrMore(item) + CLOSE_BRACE)

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

      Output is: IPF.Contact

      Ciao,
      Marc 'BlackJack' Rintsch

      Comment

      Working...