How do I process output from the command line into python? [duplicate] - python

This question already has answers here:
Passing IPython variables as arguments to bash commands
(4 answers)
Running Bash commands in Python
(11 answers)
Closed 2 years ago.
So I found this tool: https://github.com/elceef/dnstwist and I want to pass a list of domains into that tool then take the output and visualize it.
It operates through the command line, but how do I automate entering each domain and process the output automatically as well?
By the way I am using colab, so I would need a solution using jupyter notebooks!
Thanks!

If you want to do this programmatically I would not use the provided cli – although this would also be possible.
Instead you can create a small python script to do this.
Import dwintwist in your script and have a look at the main function in dwintwist.py to see how you call this.
Doesn't look to hard, but I don't know your programming skill level of course.

Related

Automate user input when program runs [duplicate]

This question already has answers here:
Is there any way to pass values to input prompt to the python script which was called by other python script?
(3 answers)
Closed 11 months ago.
Is there a way to auto select a user input option via task scheduler or within the script itself? I'm using a colleagues code and it would save some time if this could be achieved.
Example code below where one of these options needs to be selected.
while True:
choose_report = input(""" Please select the Report type
1) Weekly
2) Monthly
3) Yearly
4) Exit\n
Review type:> """)
I've tried argv but that changes the variables of the script, which is what I could do but I was wondering if there is a quicker way without editing the script variables.
You can do this in a couple of different ways:
Pipe user inputs from a file through stdin. For example: my_script.py < inputs.txt. This will work without needing to change the original script, but is a bit less clear and more fragile if you ever want to change the behavior of the script in the future.
Refactor the script to use argparse, and don't ask for interactive input when running from the task scheduler. Then you can specify all of the choices as script arguments, e.g. my_script.py --report-type=monthly. This will take more work up front, but makes the script very easy to change in the future.

Where to find python .difference() source code? [duplicate]

This question already has answers here:
How to read Python source code directly from IDE
(3 answers)
Finding the source code for built-in Python functions?
(8 answers)
Closed 1 year ago.
I am using macOS, conda python 3.7 with PyCharm CE IDE.
When clicking into the function, the function didn't show any source code.
Therefore, where can I find the .difference() code?
It's written in C to improve performance (pycharm doesn't have access to the cpython source code, so it can't jump to the definition), you find it here: https://github.com/python/cpython/blob/main/Objects/setobject.c#L1481
The main details of the algorithm are from line 1531 and reasonably easy to follow. It basically iterates the first set, checking if each item is in the other set, if it is, add it to a result set, then returns the result set.
The code you're looking for starts here.
First thing it does is check that the two parameters are the same length. Then it goes through the first and checks for elements that are not present in the second, building up the result as it goes. Finally, it returns said result.

Is it possible to call a function inside another script? (Python) [duplicate]

This question already has answers here:
how can I disable multiple instances of a python script?
(7 answers)
Closed 2 years ago.
When I open my program I want it to detect if the program is running already (if I launch it again while it's running). If it is I want to access the already running program and call a function there. But the problem is that I don't know how to access the already running program.
Thanks in advance!
I am confused by what you mean by "the script is the same script", but you can use import to get a function from a different file
otherprogram.py
def func(a):
return a
main.py
from otherprogram import func
print(func(5))

Python: profiling blocks of code [duplicate]

This question already has answers here:
How can I profile Python code line-by-line?
(5 answers)
Closed 7 years ago.
I have a Python function in which I want to find most slowing-down places. Just not I'm using cProfile, but I have an additional functionality.
I don't want to split my function into a dozen of sub-functions: it looks a bit bulky and annoying.
Isn't there instead a way to profile a function line-by-line? Or add something like timer_start(timer_id) and timer_stop(timer_id) before and after each block of code I want to profile execution time?
If you are not using IPython already, you should give it a look. It has magic functions like %lprun which make line-by-line profiling easy. Take a look at Timing and Profiling in IPython

setting $ENV{variable} when calling python [duplicate]

This question already has answers here:
How can I access environment variables in Python?
(16 answers)
Closed 9 years ago.
I'm new to Python and would like reproduce a convenience I used when working in Perl.
When calling a Perl script I usually set some $ENV variables (like VERBOSE, DEVELOP and DEBUG). Inside the called script I recover their values using
my $verbose=$ENV{VERBOSE};
my $develop=$ENV{DEVELOP};
my $debug=$ENV{DEBUG};
This allow print stmts conditional on these variables.
Can I do the same thing in Python? I know thanks to previous responses (Thank you!) to use os.environ[var] within the script to access the values. But I have not been able to figure out how to assign a value to a variable when I call the script from the command line as I could when callinbg a Perl script
Can values be set for such variables on the commandline invoking a python script?
TIA
They are accessible via the os.environ dictionary.
os.environ['VERBOSE']

Categories