0

I'm using a config file to inform my Python script of a few key-values, for use in authenticating the user against a website.

I have three variables: the URL, the user name, and the API token.

I've created a config file with each key on a different line, so:

url:<url string>
auth_user:<user name>
auth_token:<API token>

I want to be able to extract the text after the key words into variables, also stripping any "\n" that exist at the end of the line. Currently I'm doing this, and it works but seems clumsy:

with open(argv[1], mode='r') as config_file:
    lines = config_file.readlines()

for line in lines:
    url_match = match('jira_url:', line)
    if url_match:
        jira_url = line[9:].split("\n")[0]
    user_match = match('auth_user:', line)
    if user_match:
        auth_user = line[10:].split("\n")[0]
    token_match = match('auth_token', line)
    if token_match:
        auth_token = line[11:].split("\n")[0]

Can anybody suggest a more elegant solution? Specifically it's the ... = line[10:].split("\n")[0] lines that seem clunky to me.

I'm also slightly confused why I can't reuse my match object within the for loop, and have to create new match objects for each config item.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61

4 Answers4

0

you could use a .yml file and read values with yaml.load() function:

import yaml
with open('settings.yml') as file:
     settings = yaml.load(file, Loader=yaml.FullLoader)

now you can access elements like settings["url"] and so on

s_frix
  • 323
  • 2
  • 11
0

If the format is always <tag>:<value> you can easily parse it by splitting the line at the colon and filling up a custom dictionary:

config_file = open(filename,"r")
lines = config_file.readlines()
config_file.close()

settings = dict()
for l in lines:
    elements = l[:-1].split(':')
    settings[elements[0]] = ':'.join(elements[1:])

So, you get a dictionary that has the tags as keys and the values as values. You can then just refer to these dictionary entries in your pogram. (e.g.: if you need the auth_token, just call settings["auth_token"]

Martin Wettstein
  • 2,771
  • 2
  • 9
  • 15
  • Thanks, but that doesn't work when one of the values is a URL, which contains a colon. As might some of the values to the left of the key! – Wanstronian Sep 30 '21 at 11:43
  • The code I wrote handles colons to the right of the key without any problems. That's what `":".join(` is for. It joins the mistakenly split elements back together. But if there are values to the left _and_ to the right of the colon may contain a colon, then it's quite a dysfunctional config file. How would you know which colon separates the key and value, then? – Martin Wettstein Sep 30 '21 at 19:45
0

if you can add 1 line for config file, configparser is good choice

https://docs.python.org/3/library/configparser.html

[1] config file : 1.cfg

[DEFAULT]     # configparser's config file need section name
url:<url string>
auth_user:<user name>
auth_token:<API token>

[2] python scripts

import configparser

config = configparser.ConfigParser()
config.read('1.cfg')
print(config.get('DEFAULT','url'))
print(config.get('DEFAULT','auth_user'))
print(config.get('DEFAULT','auth_token'))

[3] output

<url string>
<user name>
<API token>

also configparser's methods is useful

whey you can't guarantee config file is always complete

jiwooJeong
  • 36
  • 1
0

You have a couple of great answers already, but I wanted to step back and provide some guidance on how you might approach these problems in the future. Getting quick answers sometimes prevents you from understanding how those people knew about the answers in the first place.

When you zoom out, the first thing that strikes me is that your task is to provide config, using a file, to your program. Software has the remarkable property of solve-once, use-anywhere. Config files have been a problem worth solving for at least 40 years, so you can bet your bottom dollar you don't need to solve this yourself. And already-solved means someone has already figured out all the little off-by-one and edge-case dramas like stripping line endings and dealing with expected input. The challenge of course, is knowing what solution already exists. If you haven't spent 40 years peeling back the covers of computers to see how they tick, it's difficult to "just know". So you might have a poke around on Google for "config file format" or something.

That would lead you to one of the most prevalent config file systems on the planet - the INI file. Just as useful now as it was 30 years ago, and as a bonus, looks not too dissimilar to your example config file. Then you might search for "read INI file in Python" or something, and come across configparser and you're basically done.

Or you might see that sometime in the last 30 years, YAML became the more trendy option, and wouldn't you know it, PyYAML will do most of the work for you.

But none of this gets you any better at using Python to extract from text files in general. So zooming in a bit, you want to know how to extract parts of lines in a text file. Again, this problem is an age-old problem, and if you were to learn about this problem (rather than just be handed the solution), you would learn that this is called parsing and often involves tokenisation. If you do some research on, say "parsing a text file in python" for example, you would learn about the general techniques that work regardless of the language, such as looping over lines and splitting each one in turn.

Zooming in one more step closer, you're looking to strip the new line off the end of the string so it doesn't get included in your value. Once again, this ain't a new problem, and with the right keywords you could dig up the well-trodden solutions. This is often called "chomping" or "stripping", and with some careful search terms, you'd find rstrip() and friends, and not have to do awkward things like splitting on the '\n' character.

Your final question is about re-using the match object. This is much harder to research. But again, the "solution" wont necessarily show you where you went wrong. What you need to keep in mind is that the statements in the for loop are sequential. To think them through you should literally execute them in your mind, one after one, and imagine what's happening. Each time you call match, it either returns None or a Match object. You never use the object, except to check for truthiness in the if statement. And next time you call match, you do so with different arguments so you get a new Match object (or None). Therefore, you don't need to keep the object around at all. You can simply do:

if match('jira_url:', line):
    jira_url = line[9:].split("\n")[0]
if match('auth_user:', line):
    auth_user = line[10:].split("\n")[0]

and so on. Not only that, if the first if triggered then you don't need to bother calling match again - it will certainly not trigger any of other matches for the same line. So you could do:

if match('jira_url:', line):
    jira_url = line[9:].rstrip()
elif match('auth_user:', line):
    auth_user = line[10:].rstrip()

and so on.

But then you can start to think - why bother doing all these matches on the colon, only to then manually split the string at the colon afterwards? You could just do:

tokens = line.rstrip().split(':')
if token[0] == 'jira_url':
    jira_url = token[1]
elif token[0] == 'auth_user':
    auth_user = token[1]

If you keep making these improvements (and there's lots more to make!), eventually you'll end up re-writing configparse, but at least you'll have learned why it's often a good idea to use an existing library where practical!

Heath Raftery
  • 3,643
  • 17
  • 34
  • Thanks for the slightly patronising reply :-). I was quite clear I'm a newbie to Python but have plenty of dev experience. And the best way (I have found) to learn how to develop is not to spend long hours researching everything around a situation, but to find something that works and then understand it. But thank you for ultimately helping out, it was very useful and is now added to my burgeoning Python knowledge! – Wanstronian Sep 30 '21 at 11:50