Skip to content
This repository was archived by the owner on Sep 27, 2024. It is now read-only.

Commit 6d3ca07

Browse files
Add configparser code
1 parent 0c4f050 commit 6d3ca07

File tree

9 files changed

+83
-0
lines changed

9 files changed

+83
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from configparser import ConfigParser
2+
3+
config = ConfigParser()
4+
5+
config['DEFAULT'] = {
6+
'title': 'Hello world',
7+
'compression': 'yes',
8+
'compression_level': '9'
9+
}
10+
11+
config['database'] = {}
12+
database = config['database']
13+
database['host'] = '127.0.0.1'
14+
database['user'] = 'username'
15+
database['pass'] = 'password'
16+
database['keep-alive'] = 'no'
17+
18+
with open('config.ini', 'w') as configfile:
19+
config.write(configfile)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[DEFAULT]
2+
title = Hello world
3+
compression = yes
4+
compression_level = 9
5+
6+
[database]
7+
host = 127.0.0.1
8+
user = username
9+
pass = password
10+
keep-alive = no
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
configparser==4.0.2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from configparser import ConfigParser
2+
config = ConfigParser()
3+
print(config.sections())
4+
5+
config.read('config.ini')
6+
print(config.sections())
7+
8+
print(config['DEFAULT']['title'])
9+
print(config['database']['host'])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[DEFAULT]
2+
title = Hello world
3+
compression = yes
4+
compression_level = 9
5+
6+
[database]
7+
host = 127.0.0.1
8+
user = username
9+
pass = password
10+
keep-alive = no
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
configparser==4.0.2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from configparser import ConfigParser
2+
config = ConfigParser()
3+
config.read('config.ini')
4+
5+
title = config.get('DEFAULT', 'title')
6+
print('title', type(title), title)
7+
8+
username = config.get('database', 'user')
9+
port = config.getint('database', 'port')
10+
keep_alive = config.getboolean('database', 'keep-alive')
11+
timeout = config.getint('database', 'timeout', fallback=60)
12+
13+
print('user', type(username), username)
14+
print('port', type(port), port)
15+
print('keep_alive', type(keep_alive), keep_alive)
16+
17+
print('timeout', type(timeout), timeout)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[DEFAULT]
2+
title = Hello world
3+
compression = yes
4+
compression_level = 9
5+
6+
[database]
7+
host = 127.0.0.1
8+
port = 9000
9+
user = test-username
10+
pass = password
11+
keep-alive = no
12+
timeout = 300
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
configparser==4.0.2

0 commit comments

Comments
 (0)