config.INI <-> config.PY

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
Benutzeravatar
jens
Python-Forum Veteran
Beiträge: 8502
Registriert: Dienstag 10. August 2004, 09:40
Wohnort: duisburg
Kontaktdaten:

Ist INI oder .py als config-Datei schneller???

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: utf-8 -*-

""" """

__author__      = "Jens Diemer"
__url__         = "http://www.jensdiemer.de"



import sys, os, time, ConfigParser


class test:
    def __init__(self):
        start_time = time.time()
        for i in xrange(10000):
            self.get_ini_config()
        print time.time() - start_time

        start_time = time.time()
        for i in xrange(10000):
            self.get_py_config()
        print time.time() - start_time

    def get_ini_config(self):
        f = file("config.ini", "rU")
        config = ConfigParser.ConfigParser()
        config.readfp(f)
        f.close()

        base_cfg = {
            "bla1" : config.get("base", "bla1"),
            "bla2" : config.get("base", "bla2")
        }

        dbconfig = {
            "User" : config.get("dbconfig", "User"),
            "Pass" : config.get("dbconfig", "Pass"),
            "Server" : config.get("dbconfig", "Server"),
        }

        return base_cfg, dbconfig

    def get_py_config(self):
        import config
        return config.base, config.dbconfig

#___________________________________________________________________________

if __name__ == '__main__':
    test()
config.py

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: utf-8 -*-

base = {
    "bla1" : "suppe",
    "bla2" : "blabla"
}
dbconfig = {
    "User" : "UserName",
    "Pass" : "Passowrd",
    "Server" : "http://www.eifhei.com"
}
config.ini

Code: Alles auswählen

[base]
bla1 = suppe
bla2 = blabla

[dbconfig]
User = UserName
Pass = Passowrd
Server = http://www.eifhei.com

Ausgabe:

Code: Alles auswählen

1.71900010109
0.0309998989105

GitHub | Open HUB | Xing | Linked in
Bitcoins to: 1JEgSQepxGjdprNedC9tXQWLpS424AL8cd
Benutzeravatar
jens
Python-Forum Veteran
Beiträge: 8502
Registriert: Dienstag 10. August 2004, 09:40
Wohnort: duisburg
Kontaktdaten:

Zweiter versuch:

test.py

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os, time

loops = 20

start_time = time.time()
for i in xrange(loops):
    os.popen("python test_ini.py")
print time.time() - start_time


start_time = time.time()
for i in xrange(loops):
    os.popen("python test_py.py")
print time.time() - start_time
test_ini.py

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: utf-8 -*-

fp = file('config.ini')
config2 = {}
for line in fp:
    try:
        p = line.split('=', 1)
        config2[p[0].strip()] = p[1].strip()
    except:
        pass

print config2
test_py.py

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import config

def get_py_config():
    print config.base, config.dbconfig

get_py_config()
Die Config-Dateien selber sind die selben. Ergebnis:

Code: Alles auswählen

1.29700016975
1.25
Wobei bei der ini auch zweimal die try-except Eintrifft...

GitHub | Open HUB | Xing | Linked in
Bitcoins to: 1JEgSQepxGjdprNedC9tXQWLpS424AL8cd
Benutzeravatar
jens
Python-Forum Veteran
Beiträge: 8502
Registriert: Dienstag 10. August 2004, 09:40
Wohnort: duisburg
Kontaktdaten:

Dritte Variante... In der config.ini sollten ja zumindes Kommentare erlaubt sein!

test.py

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os, time

loops = 20

start_time = time.time()
for i in xrange(loops):
    os.popen("python test_ini.py")
print time.time() - start_time


start_time = time.time()
for i in xrange(loops):
    os.popen("python test_py.py")
print time.time() - start_time
test_ini.py

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: utf-8 -*-

fp = file('config.ini')
config2 = {}
for line in fp:
    if p[0]=="#": continue
    try:
        p = line.split('=', 1)
        config2[p[0].strip()] = p[1].strip()
    except:
        pass

print config2
test_py.py

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import config

print config.config
config.ini

Code: Alles auswählen

# Kommentare sollten aber schon erlaubt sein!
# Oder nicht???

# Config für Bla
bla1 = suppe

# Config mit Bla
bla2 = blabla
User = UserName

# Config zum Bla
Pass = Passowrd
Server = http://www.eifhei.com
config.py

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Kommentare sollten aber schon erlaubt sein!
Oder nicht???
"""

config = {
    # Config für Bla
    "bla1" : "suppe",

    # Config mit Bla
    "bla2" : "blabla"
    "User" : "UserName",

    # Config zum Bla
    "Pass" : "Passowrd",
    "Server" : "http://www.eifhei.com"
}
Ergebnis:

Code: Alles auswählen

1.29700016975
1.28099989891

GitHub | Open HUB | Xing | Linked in
Bitcoins to: 1JEgSQepxGjdprNedC9tXQWLpS424AL8cd
Antworten