[Tutor] string not found in variable
jark AJ
jarkmx at gmail.com
Mon Mar 22 19:18:42 EDT 2021
Thank you so much Dennis and Alan.
Yes Dennis, understand, Somehow I got the idea that using the classes would
make things easier. thank you again!
Couple of follow up questions
1) At what point you would have used classes in this case or what could be
the use case that would decide us to choose classes over functions
2) On the package_check() function, I am calling output = get_packages()
and it does not call the package_check(). tested by printing it. When I
tried to do outside of the function, I was able to get the result back.
Could you please let me know why ? I am planning to use the output to
perform the success/failure action
using python2.7
____
import subprocess
import sys
def call(cmd, shell=False):
"""Function for the subprocess check"""
try:
output = subprocess.check_call(cmd.split(), shell=shell,)
return output
except subprocess.CalledProcessError as error:
return error.returncode
except subprocess.OSError as error:
return error
def get_packages():
"""Function to list the packages required to perform the test.
nc package can be present in /usr/bin or /bin/nc"""
print("test was invoked")
try:
nc_check_1 = call("ls /usr/bin/nc")
nc_check_2 = call("ls /bin/nc")
nc_check = nc_check_1 and nc_check_2
return nc_check
except Exception as error:
sys.exit("failed due to" + str(error))
def package_check():
"""Function to check the packages required to perform the test"""
output = get_packages()
print(output)
__
On Mon, Mar 22, 2021 at 12:17 PM Dennis Lee Bieber <wlfraed at ix.netcom.com>
wrote:
> On Sun, 21 Mar 2021 20:55:43 -0400, jark AJ <jarkmx at gmail.com> declaimed
> the following:
>
> > Yes, Understood, the idea behind using a class instead of a function
> >is that looking to use the same class for any commands that we may need
> to
> >run in future.
>
> You are creating an instance of the class for each command -- and
> only
> invoking it once, then discarding it.
>
> Just create a function, and pass the command to the function.
>
> >for i in range(5):
> > command = Command("nc -zvw 4 www.google.com 443")
> > output, my_check = command.run()
> > print(my_check)
> > print(type(output))
> > print("succeeded!" in my_check)
>
> That creates, runs, and disposes of five instances of Command.
>
> Since the command itself doesn't change inside the loop, it might
> be
> useful to reorder some statements -- if you insist on a class, rather than
> just a function...
>
> >command = Command("nc -zvw 4 www.google.com 443")
> >for i in range(5):
> > output, my_check = command.run()
> > print(my_check)
> > print(type(output))
> > print("succeeded!" in my_check)
>
> This, at least, makes repeated use of the same instance to run the
> same
> command. NOTE: I've not gone back to the class definition to ensure that
> there is no shared state between .run() except for the command to be run.
>
>
> --
> Wulfraed Dennis Lee Bieber AF6VN
> wlfraed at ix.netcom.com
> http://wlfraed.microdiversity.freeddns.org/
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list