Forcing a callback to be executed first - python

I've got the following codesnippet in a program using secure multiparty computation:
c = self.runtime.open(b) # open result
c.addCallback(self.determine)
j = self.compute(i)
return j
Now the function determine sets a boolean to either false or true, depending on the value of c. This boolean is then used by the function compute.
I thought that callbacks are always executed first, before the rest of the program is. However, I'm getting an error from compute that the boolean is undefined.
How can I force the callback to be executed before compute is executed?
Because I'm working within a secure multiparty computation framework, I have to work with callbacks since the value for c is a shared secret. However, the problem would also appear without secret shares I think. The language is Python.
The code for determine and compute would be something like this:
def determine(c):
global computeB
computeB = False
if c == 1:
computeB = True
else:
computeB = False
return c
def compute(i):
if computeB:
do this
else:
do this
return result

The callback gets executed when it gets executed. There is no point in "making" it execute earlier.
I guess you are dealing with twisted so heres a tutorial http://krondo.com/?page_id=1327 but it is helpfull even for understanding async programming in general, which you obviously need.

I'm not a pro in async but I think you want to yield your first function and tell your routine to wait before it goes on.
yield self.runtime.open(b) # open result
j = self.compute(i)
return j

Related

Python: Logical flow for setup and validation

I am having trouble figuring out how to structure the logical flow of my python application's initial setup module. I have a number of functions that perform small steps in finding that required config files are present, validating them if they are there, creating them if they are not, testing a login with the validated credentials, and finally, if everything checks out, starting the main application.
A simplified list of the functions is below:
def config_files_present(config_path):
config present = is_the_file_there
return config_present
def validate_config_file(config_file):
valid_email = code validating email returns True or False
valid_fqdn = code validating fqdn returns True or False
valid_config = valid_email is True and valid_fqdn is True
return valid_config
def write_config(config_path):
code with user input to collect credentials and write config.ini
config_file_written = True
return config_file_written
def test_login(config_file):
code to login with config_file and
return mailbox_object
etc
The (simplified) happy path is easier for me to visualize:
ready = False
while not ready:
config_present = config_files_present(config_path)
if not config_present:
write_config_file = write_config(config_path)
if write_config_file:
valid_config = validate_config_file(config_file)
if valid_config:
login_test = test_login(config_file)
if login_test:
ready = True
run the main app
The complicated path is more complicated. I have mapped out how it should work, but can't figure out how to program it.
Trying to represent the above flow in the else clauses of each if feels like it will turn into a bottomless recursive spiral. I suspect that there is probably an established best practice or structure to do this kind of thing, but I don't know what it is.
Do I power through this recursive mess, or is there a better way to structure this logical flow?
EDIT: Going to close this question. Because each function has only 1 possible next step if it evaluates to True, I should be able to call the next function in sequence within the function, provided the original logical test evaluates to true. Then, in my else clauses, I will just kick it back to the step that needs to be repeated.
Going to close this question. Because each function has only 1 possible next step if it evaluates to True, I should be able to call the next function in sequence within the function, provided the original logical test evaluates to true. Then, in my else clauses, I will just kick it back to the step that needs to be repeated.

Handling dependency of functions in python

I want to write a script for automation of sequence of events, where the execution of the next sequence depends upon the success of the previous step. There are basically 8 functions which I want to call onne by one and if one fails, I want to exit at that point. So how can i handle this in python?
Basic if, and elif statements can overcome your needs.
Say you had a function that returned a variable.
def f(z):
data = z
return data
You can analyse the result with a if. Assign a new variable to the function f:
x = f(0)
if x == 0:
#do something
So if x is equal to 0, continue with code. But what is c = 1?
import os
x = f(1)
if c != 0:
os._exit(0)
os._exit(0) quits the program.
I highly recommend taking a look at pytest (or one of the other Python testing frameworks).
This guide should get you up and running quickly:
http://pytest.org/latest/getting-started.html

how can I detect infinite loops in python

I am learning Python 3 and working on an exercise that calls for writing a Python program which simulates/reads a BASIC program as input. I am stuck on writing the part of the Python program that should detect infinite loops. Here is the code I have so far:
def execute(prog):
while True:
location = 0
if prog[location] == len(prog) - 1:
break
return "success"
getT = prog[location].split()
T = len(getT) - 1
location = findLine(prog, T)
visited = [False] * len(prog)
Here, prog is a list of strings containing the BASIC program (strings are in the form of 5 GOTO 30, 10 GOTO 20, etc.).
T is the target string indicated in prog[location].
If the BASIC program has an infinite loop, then my Python program will have an infinite loop. I know that if any line is visited twice, then it loops forever, and my program should return "infinite loop".
A hint given by the tutorial assistant says "initialize a list visited = [False] * len(prog) and change visited[i] to True when prog[i] is visited. Each time through the loop, one value updates in visited[]. Think about how you change a single value in a list. Then think about how you identify which value in visited[] needs to change."
So this is the part I am stuck on. How do I keep track of which strings in prog have been visited/looped through?
I'm not sure I agree that visiting a line twice proves an infinite loop. See the comments under the question. But I can answer the actual question.
Here's the hint:
A hint given by the tutorial assistant says "initialize a list visited = [False] * len(prog) and change visited[i] to True when prog[i] is visited. Each time through the loop, one value updates in visited[]. Think about how you change a single value in a list. Then think about how you identify which value in visited[] needs to change."
This is saying you should have two lists, one that contains the program, and one that contains true/false flags. The second one is to be named visited and initially contains False values.
The Python code is just like the hint says:
visited = [False] * len(prog)
This uses the * list operator, "list repetition", to repeat a length-1 list and make a new list of a longer length.
To change visited[i] to True is simple:
visited[i] = True
Then you can do something like this:
if visited[i]:
print("We have already visited line {}".format(i))
print("Infinite loop? Exiting.")
sys.exit(1)
Note that we are testing for the True value by simply saying if visited[i]:
We could also write if visited[i] == True: but the shorter form is sufficient and is customary in the Python community. This and other customary idioms are documented here: http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html
For a program this small, it's not too bad to keep two lists like this. For larger and complex programs, I prefer to keep everything together in one place. This would use a "class" which you might not have learned yet. Something like this:
class ProgramCode(object):
def __init__(self, statement):
self.code = statement
self.visited = False
prog = []
with open(input_basic_program_file, "rt") as f:
for line in f:
prog.append(ProgramCode(line))
Now instead of two lists, we have a single list where each item is a bit of BASIC code and a visited flag.
P.S. The above shows an explicit for loop that repeatedly uses .append() to add to a list. An experienced Python developer would likely use a "list comprehension" instead, but I wanted to make this as easy to follow as possible.
Here's the list comprehension. Don't worry if it looks weird now; your class will teach this to you eventually.
with open(input_basic_program_file, "rt") as f:
prog = [ProgramCode(line) for line in f]
I know of no automatic way of infinite loop detection in Python, but by using divide and conquer methods and testing individual functions, you can find the offending function or block of code and then proceed to debug further.
If the Python program outputs data, but you never see that output, that's a good indicator you have an infinite loop. You can test all your functions in the repl, and the function that does "not come back" [to the command prompt] is a likely suspect.
You can write output under a debug variable of some sort, to be shut off when everything works. This could be a member variable of a Python class to which your code would have to have access to at any time, or you could have a module-scoped variable like Debug=1 and use debug levels to print varying amounts of debug info, like 1 a little, 2 more, 3, even more, and 4 verbose.
As an example, if you printed the value of a loop counter in a suspected function, then eventually that loop counter would keep printing well beyond the count of data (test records) you were using to test.
Here is a combination I came up with using parts of J. Carlos P.'s answer with the hints that steveha gave and using the hint that the instructions gave:
def execute(prog):
location = 0
visited = [False] * len(prog)
while True:
if location==len(prog)-1:
return "success"
findT = prog[location].split()
T = findT[- 1]
if visited[location]:
return "infinite loop"
visited[location] = True
location = findLine(prog, T)

How to abort a function manually while still returning the result in Python

In R it is possible to put on.exit(return(results_so_far)) in a function, so when a user aborts the current function (in my case in Emacs), the result will still be stored.
def myfunc():
on.exit(return(results))
results = []
for i in range(1000):
# do something
results.append(something)
return(results)
res = myfunc()
It means that it will be possible to run some iterations and allow the function to be cancelled manually (e.g. leave a function running overnight and immediately obtain the results gathered so far in the morning).
I have looked, but I have yet to find a solution in Python. Ideas?
I think you can use a try...finally clause, as in:
def myfunc():
try:
results = []
for i in range(1000):
# do something
results.append(something)
finally:
return(results)
Note that the finally clause is executed whether there is an error or interrupt or not.

Return continuous result from a single function call

I have got stuck with a problem.
It goes like this,
A function returns a single result normally. What I want is it to return continuous streams of result for a certain time frame(optional).
Is it feasible for a function to repeatedly return results for a single function call?
While browsing through the net I did come across gevent and threading. Will it work if so any heads up how to solve it?
I just need to call the function carry out the work and return results immediately after every task is completed.
Why you need this is not specified in the question, so it is hard to know what you need, but I will give you a general idea, and code too.
You could return in that way: return var1, var2, var3 (but that's not what you need I think)
You have multiple options: either blocking or non-blocking. Blocking means your code will no longer execute while you are calling the function. Non-blocking means that it will run in parallel. You should also know that you will definitely need to modify the code calling that function.
That's if you want it in a thread (non-blocking):
def your_function(callback):
# This is a function defined inside of it, just for convenience, it can be any function.
def what_it_is_doing(callback):
import time
total = 0
while True:
time.sleep(1)
total += 1
# Here it is a callback function, but if you are using a
# GUI application (not only) for example (wx, Qt, GTK, ...) they usually have
# events/signals, you should be using this system.
callback(time_spent=total)
import thread
thread.start_new_thread(what_it_is_doing, tuple(callback))
# The way you would use it:
def what_I_want_to_do_with_each_bit_of_result(time_spent):
print "Time is:", time_spent
your_function(what_I_want_to_do_with_each_bit_of_result)
# Continue your code normally
The other option (blocking) involves a special kind of functions generators which are technically treated as iterators. So you define it as a function and acts as an iterator. That's an example, using the same dummy function than the other one:
def my_generator():
import time
total = 0
while True:
time.sleep(1)
total += 1
yield total
# And here's how you use it:
# You need it to be in a loop !!
for time_spent in my_generator():
print "Time spent is:", time_spent
# Or, you could use it that way, and call .next() manually:
my_gen = my_generator()
# When you need something from it:
time_spent = my_gen.next()
Note that in the second example, the code would make no sense because it is not really called at 1 second intervals, because there's the other code running each time it yields something or .next is called, and that may take time. But I hope you got the point.
Again, it depends on what you are doing, if the app you are using has an "event" framework or similar you would need to use that, if you need it blocking/non-blocking, if time is important, how your calling code should manipulate the result...
Your gevent and threading are on the right track, because a function does what it is programmed to do, either accepting 1 var at a time or taking a set and returning either a set or a var. The function has to be called to return either result, and the continuous stream of processing is probably taking place already or else you are asking about a loop over a kernel pointer or something similar, which you are not, so ...
So, your calling code which encapsulates your function is important, the function, any function, eg, even a true/false boolean function only executes until it is done with its vars, so there muse be a calling function which listens indefinitely in your case. If it doesn't exist you should write one ;)
Calling code which encapsulates is certainly very important.
Folks aren't going to have enough info to help much, except in the super generic sense that we can tell you that you are or should be within in some framework's event loop, or other code's loop of some form already- and that is what you want to be listening to/ preparing data for.
I like "functional programming's," "map function," for this sort of thing. I think. I can't comment at my rep level or I would restrict my speculation to that. :)
To get a better answer from another person post some example code and reveal your API if possible.

Categories

Resources