flow-control

Stop execution of a script called with execfile

[亡魂溺海] 提交于 2020-01-01 04:13:06
问题 Is it possible to break the execution of a Python script called with the execfile function without using an if/else statement? I've tried exit() , but it doesn't allow main.py to finish. # main.py print "Main starting" execfile("script.py") print "This should print" # script.py print "Script starting" a = False if a == False: # Sanity checks. Script should break here # <insert magic command> # I'd prefer not to put an "else" here and have to indent the rest of the code print "this should not

Use of “if/elseif/else” versus “if/else{if/else}”

≡放荡痞女 提交于 2019-12-30 11:08:07
问题 I find myself very commonly using a pattern like this: if (a > b) { foo(); } elseif (c > d) { bar(); } else { baz(); } The point here being that the second condition is not obviously connected to the first, unless you're carefully following the program logic. Is this a very bad thing? Would it be preferable to phrase the above as if (a > b) { foo(); } else { if (c > d) { bar(); } else { baz(); } } for maintainability reasons? Is there a better pattern that I'm missing entirely? The "not

Python test if object exists

主宰稳场 提交于 2019-12-22 01:26:15
问题 I consider it bad style to use try: except: for flow control, but I can't figure out how to write the following code to test if a DB field in Django exists. This is the "dirty" code which works: @receiver(pre_save, sender=UserProfile) def create_user_profile(sender, instance=None, **kwargs): try: print str(instance.bank_account) except: print 'No account' I would rather wanna do something like this, but I get an exception when the if statement is run and the object does not exist: @receiver

Ruby: What is the difference between a for loop and an each loop? [duplicate]

别等时光非礼了梦想. 提交于 2019-12-21 15:04:42
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: for vs each in Ruby Let's say that we have an array, like sites = %w[stackoverflow stackexchange serverfault] What's the difference between for x in sites do puts x end and sites.each do |x| puts x end ? They seem to do the same exact thing, to me, and the syntax of the for loop is clearer to me. Is there a difference? In what situations would this be a big deal? 回答1: There is a subtle difference regarding

Ruby: What is the difference between a for loop and an each loop? [duplicate]

▼魔方 西西 提交于 2019-12-21 15:03:04
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: for vs each in Ruby Let's say that we have an array, like sites = %w[stackoverflow stackexchange serverfault] What's the difference between for x in sites do puts x end and sites.each do |x| puts x end ? They seem to do the same exact thing, to me, and the syntax of the for loop is clearer to me. Is there a difference? In what situations would this be a big deal? 回答1: There is a subtle difference regarding

How do I break an outer loop from an inner one in Perl?

泄露秘密 提交于 2019-12-21 03:28:06
问题 Suppose I have a piece of Perl code like: foreach my $x (@x) { foreach my $y (@z) { foreach my $z (@z) { if (something()) { # I want to break free! } # do stuff } # do stuff } # do stuff } If something() is true, I would like to break ('last') all the loops. how can I do that? I thought of two options, both of which I don't like: Using something GOTO Adding a boolean variable which will mark something() is true, check this var in each of the loops before they resume and last() if it's true.

Possible to combine assignment and comparison in an expression?

痞子三分冷 提交于 2019-12-20 03:25:14
问题 In C, it's common to assign and compare in a single expression: n = n_init; do { func(n); } while ((n = n.next) != n_init); As I understand it this can be expressed in Rust as: n = n_init; loop { func(n); n = n.next; if n == n_init { break; } } Which works the same as the C version (assuming the body of the loop doesn't use continue ). Is there a more terse way to express this in Rust, or is the example above ideal? For the purposes of this question, assume ownership or satisfying the borrow

C++ Try Catch inside loop

拟墨画扇 提交于 2019-12-13 12:30:08
问题 I have this C++ program with the following general structure 1st while (condition A == true) //some code for 1st loop 2nd while (condition B == true) //some code for 2nd loop try //some code for try catch //condition B == false (supposed to leave 2nd loop and go back to first loop) I want it to get out of 2nd loop when there's an exception and go back to 1st loop until condition B is tue again. As described above it doesn't work as I expect. What seems to be happening is that code gets stuck

Is it possible to step by a different amount each iteration without creating a special iterator?

喜你入骨 提交于 2019-12-12 18:14:47
问题 In C, a for loop has an optional increment section which I sometimes miss in Rust: for (uint i = 0; i < max; i = step_function(i, j, k)) { /* many lines of code! */ } This could be written in Rust as: let mut i: u32 = 0; while (i < max) { // // many lines of code! // i = step_function(i, j, k); } ... however this will introduce bugs if continue exists somewhere in the "many lines of code" . My personal preference is also to keep the increment at the top of the loop. Without creating a special

Linux command to send binary file to serial port with HW flow control?

独自空忆成欢 提交于 2019-12-12 14:43:16
问题 I need to send binary file to rs232 device (printer) which not always can accept data and so it has BUSY output signal, which i connect to CTS pin. I try to use it with minicom , and hardware flow control works just fine. But i can't send raw file using minicom, it works wrong ant i think minicom not loves zeroes in file. At other hand, i try to use cat file > /dev/ttyS5 , along with stty -F /dev/ttyS5 clocal or stty -F /dev/ttyS5 crtscts . None of this work: HW control not work at all, data