How is returning the output of a function different from printing it? [duplicate]

print simply prints out the structure to your output device (normally the console). Nothing more. To return it from your function, you would do: def autoparts(): parts_dict = {} list_of_parts = open(‘list_of_parts.txt’, ‘r’) for line in list_of_parts: k, v = line.split() parts_dict[k] = v return parts_dict Why return? Well if you don’t, that dictionary dies …

Read more

How do I print colored text to the terminal in Rust?

You can use the colored crate to do this. Here is a simple example. with multiple colors and formats: use colored::Colorize; fn main() { println!( “{}, {}, {}, {}, {}, {}, and some normal text.”, “Bold”.bold(), “Red”.red(), “Yellow”.yellow(), “Green Strikethrough”.green().strikethrough(), “Blue Underline”.blue().underline(), “Purple Italics”.purple().italic() ); } Sample color output: Each of the format functions (red(), …

Read more

CMake: setting an environmental variable for ctest (or otherwise getting failed test output from ctest/make test automatically)

The built-in test target cannot be modified, but you can add a custom check target which invokes ctest with the –output-on-failure switch in the following way: if (CMAKE_CONFIGURATION_TYPES) add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} –force-new-ctest-process –output-on-failure –build-config “$<CONFIGURATION>”) else() add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} –force-new-ctest-process –output-on-failure) endif() The custom target has to be set up differently for single build type …

Read more

Create Java console inside a GUI panel

Here’s a functioning class. You can install an instance of this into the system out and err using: PrintStream con=new PrintStream(new TextAreaOutputStream(…)); System.setOut(con); System.setErr(con); Updated 2014-02-19: To use EventQueue.invokeLater() to avoid GUI threading issues which can crop up very rarely with the original. Updated 2014-02-27: Better implementation Updated 2014-03-25: Correct recording & deletion of lines …

Read more

Add each array element to the lines of a file in ruby

Either use Array#each to iterate over your array and call IO#puts to write each element to the file (puts adds a record separator, typically a newline character): File.open(“test.txt”, “w+”) do |f| a.each { |element| f.puts(element) } end Or pass the whole array to puts: File.open(“test.txt”, “w+”) do |f| f.puts(a) end From the documentation: If called …

Read more