Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

from __future__ import division 

 

try: 

import configparser 

except ImportError: 

import ConfigParser as configparser 

 

__all__ = ["read_conf_file"] 

 

 

def read_conf_file(filename): 

"""Read the new type of configuration file. 

 

This function reads the new type of configuration file that contains sections. It also 

has the capability to take parameters as math expressions and lists. String entries in 

list parameters do not need to be surrounded by quotes. An example file is shown below: 

 

| [section] 

| # Floating point parameter 

| var1 = 1.0 

| # String parameter 

| var2 = help 

| # List of strings parameter 

| var3 = [good, to, go] 

| # List of floats parameter 

| var4 = [1, 2, 4] 

| # Boolean parameter 

| var5 = True 

| # Floating point math expression parameter 

| var6 = 375. / 30. 

| # Set of tuples 

| var7 = (test1, 1.0, 30.0), (test2, 4.0, 50.0) 

 

Parameters 

---------- 

filename : str 

The configuration file name. 

 

Returns 

------- 

dict 

A dictionary from the configuration file. 

""" 

config = configparser.ConfigParser() 

config.read(filename) 

 

from collections import defaultdict 

config_dict = defaultdict(dict) 

math_ops = "+,-,*,/".split(',') 

import re 

paren_match = re.compile(r'\(([^\)]+)\)') 

 

for section in config.sections(): 

for key, _ in config.items(section): 

try: 

value = config.getboolean(section, key) 

except ValueError: 

try: 

value = config.getfloat(section, key) 

except ValueError: 

value = config.get(section, key) 

 

# Handle parameters with math operations 

check_math = [op for op in math_ops if op in value] 

if len(check_math): 

try: 

value = eval(value) 

except NameError: 

pass 

 

try: 

# Handle lists from the configuration 

if value.startswith('['): 

value = value.strip('[]') 

try: 

value = [float(x) for x in value.split(',')] 

except ValueError: 

value = [str(x.strip()) for x in value.split(',')] 

if len(value) == 1: 

if value[0] == '': 

value = [] 

if value.startswith('('): 

val_parts = [] 

matches = paren_match.findall(value) 

for match in matches: 

parts_list = [] 

parts = match.split(',') 

for part in parts: 

try: 

parts_list.append(float(part)) 

except ValueError: 

parts_list.append(str(part.strip())) 

val_parts.append(tuple(parts_list)) 

value = val_parts 

except AttributeError: 

# Above was a float 

pass 

 

config_dict[section][key] = value 

 

return config_dict