Welcome, guest | Sign In | My Account | Store | Cart

Many use XML to store configuration data, some use INI files others use Windows Registries and some like JSON. But given the fact that Python is such a powerful language, its easy to create/use such representations. PyDON stands for Python Dictionary Object Notation. It LOOKS similar to JSON but doesn't require you to create any parsers. Plus it can contain Python code which is even better :-).

More on this here > http://lostp.99k.org/files/PyDON.pdf

Python, 35 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#pydon file - configure.pdn
configuration = {
   "Username" : "lostp",
   "Password" : "password",
   "Address" : { 
                 "HouseNumber" : 158,
                 "Place" : "Bangalore",
                 "IsOwner?" : True
               }
  }
#equivalent in XML would be 
# <configuration>
#    <Username>lostp</Username>
#    <Password>password</Password>
#    <Address>
#        <HouseNumber type="int">158</HouseNumber>
#        <Place>Bangalore</HouseNumber>
#        <IsOwner type="bool">True</IsOwner>
#    </Address>
# </configuration>

#And the file can be used as so> 
#demo.py

#!/usr/bin/python
execfile("configure.pdn")
username = configure["Username"]
passwd = configure["Password"]
houseno = configure["Address"]["HouseNumber"]
place = configure["Address"]["Place"]
isowner = configure["Address"]["IsOwner?"]

#and done. This can be used wherever you intend to use "XML" such as in CLIENT-SERVER communication
#You can get rid of the XMLRPC altogether between PythonClient-PythonServer using PyDON
#enjoy.

2 comments

bruce wernick 10 years, 10 months ago  # | flag

Nice and simple, taking advantage of the py dictionary - I like.

I needed a flexible data structure and INI seemed OK but turned out to be not so good because it lacked the depth. I ended up using XML after considering adapting the Delphi DFM format.

Thanks, this one is worth keeping...

Lost Protocol (author) 10 years, 8 months ago  # | flag

@ bruce ,

You're welcome. And thanks for the comment :-)

Created by Lost Protocol on Tue, 21 Dec 2010 (MIT)
Python recipes (4591)
Lost Protocol's recipes (2)

Required Modules

  • (none specified)

Other Information and Tasks