control-flow

Raise exception if script fails

让人想犯罪 __ 提交于 2019-12-10 21:03:36
问题 I have a python script, tutorial.py. I want to run this script from a file test_tutorial.py, which is within my python test suite. If tutorial.py executes without any exceptions, I want the test to pass; if any exceptions are raised during execution of tutorial.py, I want the test to fail. Here is how I am writing test_tutorial.py, which does not produce the desired behavior: from os import system test_passes = False try: system("python tutorial.py") test_passes = True except: pass assert

Does the construct do .. while(false) contribute to better control flow?

空扰寡人 提交于 2019-12-10 18:17:28
问题 I've recently come across this code: do { if ( ! checkSomething() ) break; // some code if ( ! checkSomeOtherThing() ) break; // some other code } while(false); // some final code The programmer that wrote it, wrote a comment along the lines of "cleaner control flow" . In my opinion, the original code could look better if its refactored into something else. But is there any truth in this statement ? Is this construct any good ? 回答1: If you don't mind loops containing several break statements,

How to “break” out of a case…while in Ruby

放肆的年华 提交于 2019-12-09 14:36:43
问题 So, I've tried break , next and return . They all give errors, exit of course works, but that completely exits. So, how would one end a case...when "too soon?" Example: case x when y; begin <code here> < ** terminate somehow ** > if something <more code> end end (The above is some form of pseudo-code just to give the general idea of what I'm asking [begin...end was used with the hope that break would work]. And, while I'm at it, is there a more elegant way of passing blocks to case...when ?

Avoid Empty Catch Blocks When Expecting Exception

倾然丶 夕夏残阳落幕 提交于 2019-12-08 20:47:49
问题 I am trying to parse dates using SimpleDateFormat . As my service takes in multiple date formats, I have adopted this approach: String[] formats = { "yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "yyyy-MM-dd'T'HH:mm:ss.SSS-HH:mm", "EEE MMM dd HH:mm:ss Z yyyy"}; for (String format : formats) { try { return new SimpleDateFormat(format).parse(dateString); } catch (ParseException e) {} } return null; The rationale behind the try-catch is that if the current date format couldn't

Returning/Stopping the execution of a function on a keypress in Java

淺唱寂寞╮ 提交于 2019-12-08 07:50:47
问题 I have a certain function in my program that I want to stop on the press of a key. I have a native keyboard hook set up for that purpose. Right now, I call System.exit(0) when that key is detected. However, I don't want to exit the program, just stop that operation and return to where it was called. An example is given below. public class Main { public static void main(String[] args) { System.out.println("Calling function that can be stopped with CTRL+C"); foo(); // Should return when CTRL+C

How do I separate stages of multi-machine cluster provisioning?

守給你的承諾、 提交于 2019-12-08 07:16:37
问题 Let's say I have 4 Vagrant boxes. 3 are variations of the same configuration, say, Consul, and one has a completely different configuration, say a database. I need to run a provisioning step with slight variations on the three similarly configured Consul VMs. Then, I need to run a provisioning step against 1 of those Vault VMs. Only after this provisioning step run on 1 of the VMs can I successfully run the next provisioning step, with slight variations, on the three similarly configured

Design issue: to what extent should I rely on exceptions for the flow of control?

你。 提交于 2019-12-07 21:58:34
问题 I am working on a java web application and I have a few questions regarding design. Basically in its current version, it relies heavily on catching exceptions to determine the flow of control . For instance in one of my spring service classes, I have the following method that checks whether an email given as a parameter exists in the database. @Override public boolean validateEmailAddressDoesNotExist(String accountEmailAddress) { try { return !dao.checkIfEmailAddressAlreadyExists

How can I stop a package execution based on a stored procedure output?

混江龙づ霸主 提交于 2019-12-07 04:38:09
问题 I have an SSIS package that the first task executes a stored procedure to verify that the run date is not a holiday. If it is a holiday, then it returns a record set with a count of 1. I want to be able to stop the SSIS if the recordcount is 1 but continue to run if the recordcount is zero. I don't know the best way to implement this. What control flow item should I add to the package? I am relatively new to SSIS so I don't know what item to add. Any help would be great. 回答1: Well one way is

asyncio as_yielded from async generators

你。 提交于 2019-12-06 22:14:43
问题 I'm looking to be able to yield from a number of async coroutines. Asyncio's as_completed is kind of close to what I'm looking for (i.e. I want any of the coroutines to be able to yield at any time back to the caller and then continue), but that only seems to allow regular coroutines with a single return. Here's what I have so far: import asyncio async def test(id_): print(f'{id_} sleeping') await asyncio.sleep(id_) return id_ async def test_gen(id_): count = 0 while True: print(f'{id_}

How do I separate stages of multi-machine cluster provisioning?

只愿长相守 提交于 2019-12-06 14:45:19
Let's say I have 4 Vagrant boxes. 3 are variations of the same configuration, say, Consul, and one has a completely different configuration, say a database. I need to run a provisioning step with slight variations on the three similarly configured Consul VMs. Then, I need to run a provisioning step against 1 of those Vault VMs. Only after this provisioning step run on 1 of the VMs can I successfully run the next provisioning step, with slight variations, on the three similarly configured Vault VMs. I've read through the docs on Vagrant with multiple machines, looping over VM definitions, and