Python Tips for Beginners. Regex, dictionaries, etc.?

I am writing my second python script to try to parse the contents of a configuration file and would like to get noob advice. I'm not sure if it is better to use regex to parse my script from its few lines? I also read about dictionaries and wondered if this would be good practice. I'm not necessarily looking for the code by simply clicking in the right direction.

Example: My configuration file is as follows.

Job {
  Name = "host.domain.com-foo"
  Client = host.domain.com-fd
  JobDefs = "DefaultJob"
  FileSet = "local"
  Write Bootstrap = "/etc/foo/host.domain.com-foo.bsr"
  Pool = storage-disk1
  }

Should I use regex, line splitting, or maybe a module? If I had several tasks in my configuration file, would I use a dictionary to correlate the task with the pool?

+3
5

, json, pickle yaml, 3. , . , - .

, / , .

+5

, Python.

config.py

job = {
  'Name' : "host.domain.com-foo",
  'Client' : "host.domain.com-fd",
  'JobDefs' : "DefaultJob",
  'FileSet' : "local",
  'Write Bootstrap' : "/etc/foo/host.domain.com-foo.bsr",
  'Pool' : 'storage-disk1'
}

yourscript.py

from config import job

print job['Name']
+8

python, .

Job = { "Name" : "host.domain.com-foo",
        "Client" : "host.domain.com-fd",
        "JobDefs" : "DefaultJob",
        "FileSet" : "local",
        "Write BootStrap" : "/etc/foo/host.domain.com-foo.bsr",
        "Pool" : "storage-disk1" }

, Job [ "Name" ] ..

ConfigParser . , :

[Job]
Name=host.domain.com-foo
Client=host.domain.com-fd
JobDefs=DefaultJob
FileSet=local
Write BootStrap=/etc/foo/host.domain.com-foo.bsr
Pool=storage-disk1

, .

+5

ConfigParser , , Pythonic , python script.

, , pyparsing .

+4

, - . , pyparsing. , , XML. Python .

+2

Source: https://habr.com/ru/post/1710044/


All Articles