[Python-Dev] ConfigParser shootout, preliminary entry
Bengt Richter
bokr at oz.net
Tue Oct 19 00:06:35 EDT 2004
More information about the Python-list mailing list
Tue Oct 19 00:06:35 EDT 2004
- Previous message (by thread): python-dev Summary suggestion
- Next message (by thread): [Python-Dev] ConfigParser shootout, preliminary entry
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Mon, 18 Oct 2004 21:22:47 -0300, Carlos Ribeiro <carribeiro at gmail.com> wrote: >On Mon, 18 Oct 2004 22:43:16 GMT, Bengt Richter <bokr at oz.net> wrote: >> Has anyone discussed using the csv module as the basic file interface for config data? > >I believe that CSV files are better suited for regular, table-like >information, not for the type of data that is normally stored on INI >files. Why? [segment] sym=something sam=other maps trivially to seg,segment dat,sym,something dat,sam,other etc. E.g., >>> import csv, sys >>> for line in csv.reader(sys.stdin): print line ... seg,segment dat,sym,something dat,sam,other # a comment segname ,sym,something ,sam,somthing # might be an alternative ^Z ['seg', 'segment'] ['dat', 'sym', 'something'] ['dat', 'sam', 'other'] ['# a comment'] [] ['segname'] ['', 'sym', 'something'] ['', 'sam', 'somthing'] ['# might be an alternative'] Very easy to deal with. If you wanted an object with two-level attribute for access like conf.segment.sym, it would be easy to build, e.g., (admittedly not very robust in this little demo hack ;-) >>> import csv, sys >>> conf = type('ConfData',(),{})() >>> for line in csv.reader(sys.stdin): ... if not line: continue ... if line[0]: ... seg = type('SegData',(),{})() ... setattr(conf, line[0], seg) ... continue ... setattr(seg, line[1], line[2:]) ... segment ,sym,sym value ,sam,sam value seg2 ,a_float,f,1.0 ,an_int,i,123 seg3 ,msg1,how about this? ,msg2,pretty trivial ,msg3, ;-) ^Z ^Z >>> vars(conf).keys() ['segment', 'seg3', 'seg2'] >>> vars(conf.segment).keys() ['sam', 'sym'] >>> vars(conf.seg2).keys() ['a_float', 'an_int'] >>> vars(conf.seg3).keys() ['msg1', 'msg3', 'msg2'] So we have the hierarchy: >>> conf.segment.sam ['sam value'] >>> conf.seg2.an_int ['i', '123'] >>> conf.seg3.msg3 [' ;-)'] Obviously one could have recognized ['i', '123'] and made conf.seg2.an_int == 123 instead of a list. This is very expandable. E.g., it's not hard to imagine what ['img','url=http://www.python.org/pics/pythonHi.gif'] might mean as to how to get and convert such a data spec. Regards, Bengt Richter
- Previous message (by thread): python-dev Summary suggestion
- Next message (by thread): [Python-Dev] ConfigParser shootout, preliminary entry
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list