ConfigParser.items sorting

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Wed Oct 28 09:14:46 CET 2009


On Tue, 27 Oct 2009 23:21:00 -0700, Dean McClure wrote:

> Hi,
> 
> Just wondering how I can get the items() command from ConfigParser to
> not resort all the item pairs that it presents.
> 
> I am trying to get it to read some data in order:
> 
> [Relay Info]
> relay_name: IPC
> relay_current_range: [60, 64, 68, 72, 76, 80, 84, 88, 92, 96, 100, 104,
> 108, 112, 116]
> relay_current_mutliplier: [1/8, 1/4, 1/2, 1, 2, 4] 
> relay_i: arcfc/(relay_current_range*relay_current_mutliplier)

Do you realize multiplier is mispelled?


> so I can input the data and then eval() the equation at the end 

Just what the world needs, another code injection vulnerability. 

http://cwe.mitre.org/top25/#CWE-78

I really hope you can trust the source of the INI file.



> but when
> I go
> config.items('Relay Info')
> It mixes everything up, is there a way to stop this?

Not in Python 2.5 or older.



> Here is my selection code
> 
> variables = sorted(config.items('Relay Info')) #Get inputs from user for
> each variable 
> for variable in variables:
> 	if variable[0] == 'relay_name':
> 		vars()[variable[0]] = variable[1]
> 	else:
> 		vars()[variable[0]] = 'not a real thing this is just a 
fake that will never turn up to establish the variable'
> 		if variable[1][0] == '[' and variable[1][-1] == ']':
> 			if variable[0] != 'Any':
> 				while (variable[1].count(vars()[variable
[0]]) < 1):
> 					vars()[variable[0]] = raw_input
(str(variable)[1:-1] + "\n") if
> 					variable[1].count(vars()[variable
[0]]) < 1:
> 						print 'Setting 
unavailable'
> 			else:
> 				vars()[variable[0]] = raw_input(str
(variable)[1:-1] + "\n")
> 		else:
> 			vars()[variable[0]] = variable[1]
> 		vars()[variable[0]] = float(eval(vars()[variable[0]]))


Are we  supposed to understand that?





-- 
Steven



More information about the Python-list mailing list