Python - Undo script if script fails [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Sometimes when I run a function in my python script and if the function gives me an output which is not desired, I need to undo this step and then try running the function again with different parameters. Is there a way that I can have the script undo what it has done if my function gives me a wrong output.For only one set of parameters, the desired output is acheived.
PS: By running the function means making permanent changes.
Example:
def function():
for i in range(parameters):
value = perform_operation(parameters[i]) #makes permanent changes
if value != some_value:
undo(perform_operation())

You need to create another function than cleans what was done in the main function for example (delete newly created files, remove installed packages, etc ...) and use it.
def main_func():
proc = subprocess.Popen('touch test test2')
return proc.returncode
def clean_main_func():
subprocess.Popen('rm test test2')
def __name__ == '__main__':
result = main_func()
if main_func() != 0 :
clean_main_func()
or you can raise an error then catch it:
def main_func():
proc = subprocess.Popen('touch test test2')
if proc.returncode !=0 :
raise Error
def clean_main_func():
subprocess.Popen('rm test test2')
def __name__ == '__main__':
try:
result = main_func()
except Error:
clean_main_func()
It's juste an example , and hope it answer your question.

Related

if line similar to var then execute function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
for event in longpoll.listen():
if event.type == VkEventType.MESSAGE_NEW and event.to_me:
if event.from_user:
request = event.text
line = request.lower()
user = event.user_id
if str(line) == "!add <args>" :
print("Data has updated.")
i need if var line has arguments, like !add setting, function executes
The question "how to make if the line similar to /change admin the function was performed?" is not clear. Also the code expect to have indent.
If understood correctly you need to check if the line has "/change admin" or you want to check if there is "/change" and "admin" in line, although both being same.
If above is the case, considering line has "/change <admin/others>"
if "/change admin" in line: # if "/change" in line and line.split(' ')[1] == "admin":
print("Admin Changed")
else:
print("Usage")
If you are running a command line, suggest you to use, sys module. Using sys.argv for catching command line args.
Also look into arg_parser if looking to make some tool or utility to run command line, that gives fancy usage.

sys.exit() from with a function when dict is not empty [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
i'm wondering if this is the most pythonic way to gracefully exit a script from within a function if a condition matches. I'm checking if a dictionary contains anything. if it does, i need the script to stop.
So here mydict is defined in an earlier function. Now i want to check if it has entries.
def check_missing():
if bool(mydict) is True:
print("Dict is not empty, exiting.")
sys.exit()
else:
pass
check_missing()
is there a better way to do this? It does work, but I'm not confident it's "pythonic"
There is more functions after this to continue running if this check passes
To keep your function pythonic, I would suggest that you separate the dictionary validation from the control flow of the program. This will make your code easier to test and it will be possible for other parts of your code to call the function without causing the program to exit -- for instance, if you wanted to display a different error message or ask a user to re-enter an input.
To accomplish this, you can restructure your code like so:
import sys
def is_valid_dict(value):
if value: # This is the same as calling `bool(value)`
raise ValueError("The dict is invalid because it is NOT empty")
if __name__ == "__main__":
# This code will be run when the program is executed directly, but not when
# it is imported. This makes it much easier to test.
example_dicts = [
{}, # A dict that is empty
{"a": 1}, # A dict that is _NOT_ empty
]
for value in example_dicts:
print(f"Testing: {value!r}")
try:
is_valid_dict(value)
except ValueError:
sys.exit(" -> EXIT: The dict is NOT empty!")
print(" -> OKAY: The dict is empty.")
print() # Print a blank line
Running this in the terminal produces this output:
$ python3 stack_question.py
Testing: {}
-> OKAY: The dict is empty.
Testing: {'a': 1}
-> EXIT: The dict is NOT empty!
If you don't want to import modules, you can always use quit() or exit(), but it is preferred that you use sys.exit() because quit and exit require the site module, and sys can always be imported.

Close Only One Chrome Tab on TaskBar [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have 2 chrome tabs like that:
Tabs
I want to close one of them.I will use python to close.How can I close?
Using psutil module this can be achieved
Given code will kill first instance of process found. (If you want to kill all instance you can remove/comment return statement inside try block)
import psutil
def kill_process_by_name(processName):
# Iterate over all running processes
for proc in psutil.process_iter():
# Get process detail as dictionary
pinfo = proc.as_dict(attrs=['pid', 'name', 'create_time'])
# Check if process name contains the given name string.
try:
if processName.lower() in pinfo['name'].lower():
print(pinfo)
p = psutil.Process(pinfo['pid'])
p.terminate()
return
except Exception as e:
pass
#ignore any exception
return
kill_process_by_name( processName='chrome')

Call function at any time on input, python 3.x [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
If you made a function like this:
def show():
print(something)
How would you make it so that, on any input, if the user typed a specific thing, this would be called? I want multiple functions like this to be able to be called whenever the user wants. Would I just have to have it as an option every time I ask for an input?
EDIT: sorry, this wasn't very clear. Say I have a variable in a game, like money. When I ask for an input as the game goes along, the inputs being about unrelated things, I want the user to be able to type eg. gold and the show() function will activate. Then the input will go on as usual. Would I be able to do this without just have it as an option for each input, eg.
variable = input("type something")
if variable == "gold":
do stuff
elif variable == other things
do other things
Do you mean something like this:
def thing1(): # Random command
print('stuff')
def thing2(): # Random command
print('More stuff')
def check(command):
'''Checks if the users command is valid else it does a default command'''
if command == 'thing1':
thing1()
elif command == 'thing2':
thing2()
else:
default_thing()
while True: # Loop going on forever
userinput = input('') # Lets the user input their command
check(userinput)
you could put all your functions in a dictionary: {"<some user input>":func_to_call} and see if it matches any
something like:
def gold():
print("gold")
input_options = {"gold":gold}
variable = input("type something:")
if variable in input_options:
input_options[variable]()

Doing something after for loop finishes in same function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is this possible? I'm doing an bukkit plugin now (in Python, yes :D), and I'm forced to do this within one function, so I can't separate it and call it later... For example, if I have loop that loops through players on server and adds everyone except one player, I want it to finish, and then teleport i.e. "Player1" to random player. At the moment, it teleports "Player1" to random player every time because of for loop... I'll give you just little of code, since It looks messy in preview due to many things that are not involved in problem and could be confusable to you... Here it is:
listica = []
for p1 in org.bukkit.Bukkit.getWorld(nextvalue).getPlayers():
if p1.getName() not in listica:
try:
listica.remove(event.getPlayer().getName())
randomtarget = choice(listica)
randomtargetreal = org.bukkit.Bukkit.getPlayer(randomtarget)
event.getPlayer().teleport(randomtargetreal)
event.getPlayer().sendMessage("%sYou teleported to: %s%s"% (bukkit.ChatColor.GREEN, bukkit.ChatColor.DARK_GREEN, randomtarget))
except ValueError:
randomtarget = choice(listica)
randomtargetreal = org.bukkit.Bukkit.getPlayer(randomtarget)
if event.getPlayer().getLocation() != randomtargetreal.getLocation():
event.getPlayer().teleport(randomtargetreal)
event.getPlayer().sendMessage("%sYou teleported to: %s%s"%(bukkit.ChatColor.GREEN, bukkit.ChatColor.DARK_GREEN, randomtarget))
What I want is:
run for loop:
when there is no more players to add a.k.a it finishes
do try loop
P.S. I can't do it in separate function.
Thanks in advance! :)
Do you mean:
def func(args):
for item in loop:
do something
try: # note indentation
something else

Categories