printf-debugging

python - is there no better way to get the expression in a debug function

拜拜、爱过 提交于 2019-12-11 16:08:42
问题 in c code I frequently use printf debugging macros like #define DPRINT_INT(i) fprintf(stderr,"%s has the value %i at line %i", #i,i, __LINE__) and then i can do things like DPRINT_INT(height) where it will print the variable or things like DPRINT_INT(from_cm_to_inch(get_average(heights))) and it will print out the whole expression for the name. To do this for python, since python doesn't have c-like macros I pass a string and use inspect to get the calling functions environment to call eval

C Programming: seg faults, printf, and related quirks

て烟熏妆下的殇ゞ 提交于 2019-12-10 10:20:18
问题 As many young programmers do, I learned the usefulness of inserting numerous print-to-console statements of "here1," "here2," and so on at different points in code to figure out when my programs are going awry. This brute force debugging technique has saved me many, many times throughout my CS studies. However, when I started programming in C, I stumbled onto an interesting problem. If I were to try and run void* test; printf("hello world"); test[5] = 234; Of course I get a segfault for not

Type overloading macro

我怕爱的太早我们不能终老 提交于 2019-12-09 12:15:52
问题 I have a bunch of printf debug helper macros and it would be pretty cool to have to not specify the type, is there anything you can do to allow something like macro overloading in c(can be gcc specific if its available in gcc 4.3). I thought maybe typeof but apparently that doesn't work. example macro(I also have some ascii terminal color stuff that I can't remember of the top of my head) #ifdef _DEBUG #define DPRINT_INT(x) printf("int %s is equal to %i at line %i",#x,x,__LINE__); . . . #else

C Programming: seg faults, printf, and related quirks

Deadly 提交于 2019-12-05 18:52:01
As many young programmers do, I learned the usefulness of inserting numerous print-to-console statements of "here1," "here2," and so on at different points in code to figure out when my programs are going awry. This brute force debugging technique has saved me many, many times throughout my CS studies. However, when I started programming in C, I stumbled onto an interesting problem. If I were to try and run void* test; printf("hello world"); test[5] = 234; Of course I get a segfault for not malloc'ing memory for testChar. However, you would think logically that "hello world" would be printed

Type overloading macro

末鹿安然 提交于 2019-12-03 13:57:45
I have a bunch of printf debug helper macros and it would be pretty cool to have to not specify the type, is there anything you can do to allow something like macro overloading in c(can be gcc specific if its available in gcc 4.3). I thought maybe typeof but apparently that doesn't work. example macro(I also have some ascii terminal color stuff that I can't remember of the top of my head) #ifdef _DEBUG #define DPRINT_INT(x) printf("int %s is equal to %i at line %i",#x,x,__LINE__); . . . #else #define DPRINT_INT(x) . . . #endif Try this; it uses gcc's __builtin methods, and automatically

How do I dump an object's fields to the console?

元气小坏坏 提交于 2019-11-29 19:14:29
When I'm running a simple Ruby script, what's the easiest way to dump an object's fields to the console? I'm looking for something similar to PHP's print_r() that will work with arrays as well. Possibly: puts variable.inspect You might find a use for the methods method which returns an array of methods for an object. It's not the same as print_r , but still useful at times. >> "Hello".methods.sort => ["%", "*", "+", "<", "<<", "<=", "<=>", "==", "===", "=~", ">", ">=", "[]", "[]=", "__id__", "__send__", "all?", "any?", "between?", "capitalize", "capitalize!", "casecmp", "center", "chomp",

Ubuntu (14 & 16) Bash errors with printf loops from input containing lowercase “n” characters

血红的双手。 提交于 2019-11-29 16:34:40
I have some bash scripts I have been running on Ubuntu 14.04 and 16.04 for well over a year now. Some recent Ubuntu update has broken bash and I cannot figure out how to sort this out. Example: #!/bin/bash INPUT=myinput.txt OUTPUT=myoutput.txt ACTION1="0;" cat $INPUT | while read LINE do printf "\t\"~${LINE}\"\t\t$ACTION1\n" >> $OUTPUT done my script then loops through the file doing the printf statement as follows but this is the output produced "~jetmo" 0; "~spamme" 0; "~baidu" 0; example contents of myinput.txt jetmon spammen baidu Input lines containing a lowercase n character gets

How do I dump an object's fields to the console?

我只是一个虾纸丫 提交于 2019-11-28 13:42:00
问题 When I'm running a simple Ruby script, what's the easiest way to dump an object's fields to the console? I'm looking for something similar to PHP's print_r() that will work with arrays as well. 回答1: Possibly: puts variable.inspect 回答2: You might find a use for the methods method which returns an array of methods for an object. It's not the same as print_r , but still useful at times. >> "Hello".methods.sort => ["%", "*", "+", "<", "<<", "<=", "<=>", "==", "===", "=~", ">", ">=", "[]", "[]=",

Ubuntu (14 & 16) Bash errors with printf loops from input containing lowercase “n” characters

元气小坏坏 提交于 2019-11-28 11:16:51
问题 I have some bash scripts I have been running on Ubuntu 14.04 and 16.04 for well over a year now. Some recent Ubuntu update has broken bash and I cannot figure out how to sort this out. Example: #!/bin/bash INPUT=myinput.txt OUTPUT=myoutput.txt ACTION1="0;" cat $INPUT | while read LINE do printf "\t\"~${LINE}\"\t\t$ACTION1\n" >> $OUTPUT done my script then loops through the file doing the printf statement as follows but this is the output produced "~jetmo" 0; "~spamme" 0; "~baidu" 0; example

What is “p” in Ruby?

安稳与你 提交于 2019-11-28 08:52:23
I'm sure it's a silly question to those who know, but I can't find an explanation of what it does or what it is. CSV.open('data.csv', 'r') do |row| p row end What does " p row " do? p() is a Kernel method It writes obj.inspect to the standard output. Because Object mixes in the Kernel module, the p() method is available everywhere. It's common, btw, to use it in poetry mode , meaning that the parens are dropped. The CSV snippet can be written like... CSV.open 'data.csv', 'r' do |row| p row end It's documented here with the rest of the Kernel module. Kernel#p is the little debugging brother of