primer
primer

Reputation: 421

passing a list in python to generate url

I have to import my config.py file in my code and then basically pass the REPOS_TO_CHECK values to a function which should be in the form of a list such that when I pass the list to my build_url function which generates a url when the REPOS_TO_CHECK variable is passed to it from the config.py file

I have to generate the url like GET /repos/:owner/:repo GITHUB_URL = 'https://testurl.com'

how do I pass the REPOS_TO_CHECK parameter from the config.py file as a list so that when I pass

def build_url(['owner','repo']):

the url generated will be https://testurl.com/:owner/:repo

:owner, :repo are in the REPOS_TO_CHECK in the config.py file.

I can access the config.py file by importing config.py in my code and then accessing the values by using the config. for example: config.GITHUB_URL ,gives me 'https://testurl.com'
This is my config file:

GITHUB_URL = 'https://testurl.com'
GITHUB_ACCESS_TOKEN = 'access token'
REPOS_TO_CHECK = [
    ('owner1', 'repo1'),
    ('owner2', 'repo2'),]

Upvotes: 0

Views: 128

Answers (1)

Roy Zwambag
Roy Zwambag

Reputation: 76

You can import your config.py as any other file given it's in the same folder: (otherwise navigate to it)

import config

Then you can call the variable like config.REPOS_TO_CHECK

To generate the url you can simply use the variables given in the function call:

If you're using python 3.6 use f strings:

def generate_url(list_here):
    return f'http://test.com/{list_here[0]}/{list_here[1]}'

Else use .format()

def generate_url(list_here):
    return 'http://test.com/{0}/{1}'.format(list_here[0], list_here[1])

Upvotes: 1

Related Questions