Cleanly and optionally redirect stderr or stdout to file

女生的网名这么多〃 提交于 2019-12-12 04:59:40
问题 I have a Python3 script and I want to optionally redirect stdout and stderr to a file. Something like this: # variable declarations if log_output: output_file = open('output.txt', 'w') sys.stdout = output_file if log_errors: errors_file = open('errors.txt', 'w') sys.stderr = errors_file # code that uses variables declared above but may exit suddenly #at the end if log_output: output_file.close() if log_errors: errors_file.close() This works, unless my code in the middle decides to quit. Then

execute multiple processes from a master process

六月ゝ 毕业季﹏ 提交于 2019-12-11 18:33:13
问题 I want to create multiple processes from one master process. I know I want to use a function from the exec family, but it does not seem to be preforming in the way I intended it to. It seems that exec() is a blocking call, or maybe I am just using it wrong. Anyway, on to the code: const char* ROUTERLOCATION = "../../router"; int main(int argc, char** argv) { manager manager; vector<string> instructions = manager.readFile(argv[1]); ... //file gives me the number of proceses i want to spawn and

Understanding JavaScript setTimeout and setInterval

ぐ巨炮叔叔 提交于 2019-12-11 03:20:18
问题 I need a bit of help understanding and learning how to control these functions to do what I intend for them to do So basically I'm coming from a Java background and diving into JavaScript with a "Pong game" project. I have managed to get the game running with setInteval calling my main game loop every 20ms, so that's all ok. However I'm trying to implement a "countdown-to-begin-round" type of feature that basically makes a hidden div visible between rounds, sets it's innerHTML = "3" // then

Tools for generating a control flow graph from source code

杀马特。学长 韩版系。学妹 提交于 2019-12-10 22:09:34
问题 I need a tool to generate a control flow graph from java source code. Are there such tools available? Is there a possibility to also generate source code if I have a control flow graph? 回答1: See my company's (Semantic Designs) Java Front End. It will compute control flow graphs for methods from source code. The Java Front End is built on top of DMS Software Reengineering Toolkit, which provides generic machinery for manipulating (parsing, analyzing [e.g., extracting control flow graphs]),

Raise exception if script fails

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