0

I run my bash script my_file.sh in a python file as follows:

import subprocess

def rest_api():

    params = {
        'query': 'indepedence day',
        'formats': '["NEWSPAPER"]',
    }

    subprocess.call(['bash', 
                     'my_file.sh',
                     f'QUERY={params.get("query")}',
                     f'DOC_TYPE={params.get("formats")}',
                     f'LANGUAGE={params.get("lang")}', # returns None!
                    ])

if __name__ == '__main__':
    rest_api()

Several of my input arguments in subprocess.call do not normally exist in a dictionary params={} (here I provided f'LANGUAGE={params.get("lang")}' as one example). I handle such unavailability in my_file.sh to initialize with something, for instance:

if [ -z "$LANGUAGE" ]; then LANGUAGE="${LANGUAGE:-[]}"; fi

What I want is to apply some sort of if else statement in subprocess.call function with this logic:

if params.get("lang") is None, do not even send it as an input to bash file, e.g., treat it as I never provided such input for my_file.sh.

Therefore, I tried to rewrote my code like this:

subprocess.call(['bash', 
                         'my_file.sh',
                         f'QUERY={params.get("query")}',
                         f'DOC_TYPE={params.get("formats")}',
                         if params.get("lang"): f'LANGUAGE={params.get("lang")}', # syntax Error
                        ])

which is wrong I get the following invalid syntax error:

Traceback (most recent call last):
  File "nationalbiblioteket_logs.py", line 13, in <module>
    from url_scraping import *
  File "/home/xenial/WS_Farid/DARIAH-FI/url_scraping.py", line 17, in <module>
    from utils import *
  File "/home/xenial/WS_Farid/DARIAH-FI/utils.py", line 53
    if params.get("lang"): f'LANGUAGE={params.get("lang")}',
     ^
SyntaxError: invalid syntax

Do I have a wrong understanding of applying if else statement for the input arguments of a python function or is there an easier or cleaner way doing it?

Cheers,

1
  • if statements and conditional expressions are two entirely different things that share the same set of keywords. You can't have an conditional expression without the else, and you can't use an if statement inside another expression. Commented Jan 6, 2023 at 18:53

2 Answers 2

2

You can specify the default when calling .get(), so use an empty string.

f'LANGUAGE={params.get("lang", "")}'
Sign up to request clarification or add additional context in comments.

2 Comments

This isn't what you asked for you in your question.
It solves the root problem, since the bash script handles empty values, but not None.
0

If you don't want the LANGUAGE= argument at all when no value is provided, you need to build the list dynamically.

    cmd = ['bash', 
           'my_file.sh',
           f'QUERY={params.get("query")}',
           f'DOC_TYPE={params.get("formats")}']
    if (lang := params.get("lang")) is not None:
        cmd += [f'LANGUAGE={lang}']
    
    subprocess.call(cmd)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.