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

This class contains serval methods to create an INI file.

Python, 32 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
class Ini:
	"Contains all that is needed to write an INI file"
	
	def __init__(self):
		"You can add here the begin of your INI file"
		self.file = ""
		
	def addsection(self, section):
		"Add a section delimiter to the INI file"
		self.file += "\n\n[" + section + "]"
		
	def addkey(self, key, value):
		"Writes a string to the INI file"
		self.file += "\n" + key + "=" + value
		#print key+"="+value ,"[OK - Ajouté]"
		
	def comment(self, comm):
		"Writes a comment to the INI file"
		self.file += "\n ;" + comment
		
	def verb(self, str):
		"Write all that is given as argument to INI file"
		self.file += str
		
	def show(self, action="return"):
		"""Prints or returns the contents of the INI file
		Usage: action='return' returns the contents
		          action='print' prints the contents"""
		if action == "return":
			return self.file
		elif action == "print"
			print self.file

This can be used for a program that needs to write some configuration into an INI file. In fact, this generates an INI file in the file variable. The code is very simple, but very useful because the syntax of the file is made automatically. There's actually no INI parser/reader but it will come.

___Usage example___

w = Ini() w.comment("Hi Everybody") w.addsection("Adv Config Options") w.addkey("Number of rows","25") w.show(action="print")

2 comments

Foo Bear 18 years, 4 months ago  # | flag

Better and fully functional example. I wrote a fully functional example, yours does not even run :(

class Ini(object): def __init__(self): self.ini = []

def add_section(self, section):
    self.ini.append("[%s]" % section)

def add_key(self, key, value):
    self.ini.append("%s=%s" % (key, value))

def add_comment(self, comment):
    self.ini.append(";%s" % comment)

def add_verb(self, verb):
    self.ini.append(verb)

def show(self):
    return "\n".join(self.ini)

if __name__ == "__main__": from ConfigParser import ConfigParser from StringIO import StringIO

w = Ini()
w.add_comment("Hi Everybody")
w.add_section("Adv Config Options")
w.add_key("Number of rows", 25)
print w.show()

fd = StringIO(w.show())
parser = ConfigParser()
parser.readfp(fd)

print
print parser.sections(), parser.options(parser.sections()[0])

rhymes@voodoo:~/downloads $ python iniforger.py ;Hi Everybody [Adv Config Options] Number of rows=25

['Adv Config Options'] ['number of rows']

Foo Bear 18 years, 4 months ago  # | flag

Better and fully functional example. Ehm I forgot pre tags.

class Ini(object):
    def __init__(self):
        self.ini = []

    def add_section(self, section):
        self.ini.append("[%s]" % section)

    def add_key(self, key, value):
        self.ini.append("%s=%s" % (key, value))

    def add_comment(self, comment):
        self.ini.append(";%s" % comment)

    def add_verb(self, verb):
        self.ini.append(verb)

    def show(self):
        return "\n".join(self.ini)

if __name__ == "__main__":
    from ConfigParser import ConfigParser
    from StringIO import StringIO

    w = Ini()
    w.add_comment("Hi Everybody")
    w.add_section("Adv Config Options")
    w.add_key("Number of rows", 25)
    print w.show()

    fd = StringIO(w.show())
    parser = ConfigParser()
    parser.readfp(fd)

    print
    print parser.sections(), parser.options(parser.sections()[0])


rhymes@voodoo:~/downloads $ python -tt iniforger.py
;Hi Everybody
[Adv Config Options]
Number of rows=25

['Adv Config Options'] ['number of rows']
Created by Simon Plante on Sun, 11 Dec 2005 (PSF)
Python recipes (4591)
Simon Plante's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks