0

A little bit of an ugly question, but I didn't find existing SO posts which cover it.

Right now I need to use an existing python tool available on this github

This is a rather big piece of code with a lot of dependencies which I don't want to mess with. In a nutshell one can run its module by passing the command line arguments, for example:

timesearch.py timesearch -r "subreddit1" -l "1466812800" -up "1498348800"

Now, I need to run this tool a bunch of times using a for loop, passing over different argument values each time. The tool also prints out some output into command line when you run it - and I would like to intercept and print it out from my python script as well. Finally, I need to ensure that before I move on in my loop and run the tool another time that current execution of the timesearch tool is completed.

One side note here - I do need to ensure that the timesearch is executed using same environment which I use to run my main script with for loop.

I am trying to understand what is the best way to do it.

If I just go for this it doesn't work:

import os
#for loop will go here
os.system('python timesearch.py timesearch -r "ethereum" -l "1466812800" -up "1498348800"')

It fails due to several reasons - it doesn't use the environment in which I am writing my script with a loop, it also doesn't capture the print output of timesearch.

Any advice on how to achieve it?

Just to highlight - I can't just go and pull function I need in timesearch, since it calls the __init__ to set up some things based on the arguments you pass.

1 Answer 1

1

I wouldn't call python script with os.system. There is basically one function which you need to use: main(sys.argv[1:])

https://github.com/voussoir/timesearch/blob/master/timesearch/__init__.py#L435.

Sign up to request clarification or add additional context in comments.

2 Comments

Can we be sure that the init part won't be messed up? I mean will init be called each time I call timesearch.main?
What do you mean by init part? Is it the part outside the function body? I believe you don't need to create new ArgumentParser every time you call main function.

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.