Python - Tracking one little script with `pdb` - python

Is it possible save the evolution of constants of a program, a small one indeed, in one file ? In other word, is there a way to store the informations sent by pdb?
Suppose for example that we have the following code.
a = 1
b = 2
a = a + b
I would like to have something like the following.
a initialized to 1
b initialized to 2
a + b stored in a

I'm a little confused about what, exactly, you're asking. But it sounds like you want to implement logging.

Related

Associating unique logging-calls to an id across runs

I need to be able to associate a log to an id that stays persistent across runs e.g:
logging.log("Hello") # 1
logging.log("World") # 2
Obviously this only works as long as you don't modify the code
logging.log("Hello") # 1
logging.log("beautiful") # 2
logging.log("World") # 3 <--- ID should be 2 here!!
The solution would seem simple... just create a hash over the log message and you're good to go right?
Sadly, I have to consider following possibility:
logging.log("Hello")
logging.log("World")
...
logging.log("World") # This would assign to the same hash/id as the previous log to 'World'
The only ideas i've come up with to tackle this problem are:
Running static analysis or a diff on the code beforehand and analyzing it
Are there any tools suitable for the job?
Can they be implemented realistically?
Making the user manage the IDs (not very user friendly)
logging.log("10", "Hello")
logging.log("20", "World")
Or is there another solution I'm not seeing?

Nice way to create global counters?

I'm very unfamiliar with Python 2.x and I have some inherited code in many files where I need to count how many times some particular functions are called (to solve a particular problem).
To show what I mean, in another simple language I would do this
Add at the beginning of the code
counter_funcA = 0
counter_funcB = 0
...
In the beginning of function A
counter_funcA += 1
In the beginning of function B
counter_funcB += 1
And so on...
And at the end
print counter_funcA
...
So how would a python expert do this in a nice way?
I guess the operation += is quite atomic -- so there will not be any concurrency issues?

"Re-declared Defined Without Usage" One Script but Not The Other?

This sounds like it should be blindingly obvious but there's something quirky going on.
So I have two scripts. Largely the same but a few variances here and there. They both run big loops and they both use global variables. Yes, I know this is bad. Tidying it up is on my to do list but I'm on a time constraint at the moment.
One script works fine. As expected.
The other works fine... for the most part. Then it bugs out once it iterates through the loop again. It's very strange because the code has very little differences but I did pick up on something which I can't explain.
The variable I use to track my position in the loop is set like this in script 1 (which works flawlessly):
global CurrentMatchScan
while True:
print "=Starting Loop="
Loopcounter = 0
CurrentMatchScan = -1
LoadMatch()
The second script has:
global CurrentMatchScan
while True:
print "=Starting Loop="
Loopcounter = 0
CurrentMatchScan = -1
LoadMatch()
However in the second script PyCharm highlights the = -1 part as
Redclared'CurrentMatchScan' defined above without usage
So the obvious assumption is something further up in the code is using it. I do call a function later on which is placed up there...
def LoadMatch():
nextbox1 = LinkList[CurrentMatchScan + 1]
nextbox2 = LinkList[CurrentMatchScan + 2]
But this is only called once CurrentMatchScan is set... and the first script has the exact same code.
Is my IDE just not noticing it on the other script or something? There's a pretty big issue with the looping on the second one and this is the only difference I can see between the two.
I know this doesn't look like a lot to go on but there's not much else to it that I can see. It's barely actually referenced.
I'd really appreciate anyone who can point out how much of an idiot I'm being and missing something really simple.
/edit:
Based on the fact that it does use CurrentMatchScan and calls LoadMatch() for the first iterations properly I'm starting to doubt this is anything but PyCharm trying to warn me I'm potentially doing something silly.
I think if this was the issue it wouldn't work at all so the warning might be a bit of a red herring when it comes to the issue I'm actually facing.

Python's math module & "Think Python"

I'm stucked on the chapter 3.3 "Math functions" of "Think Python".
It tells me to import math (through the interpreter).
Then print math and that I should get something like this:
<module 'math' from '/usr/lib/python2.5/lib-dynload/math.so'>
Instead I get <module 'math' <built-in>>
Anyway that's not the problem. Though I wasn't able to find a 'math.so' file in my python folder. The most similar file is named test_math.
The problem is that I'm supposed to write this:
>>> ratio = signal_power / noise_power
>>> decibels = 10 * math.log10(ratio)
>>> radians = 0.7
>>> height = math.sin(radians)
When I write the first line it tells me this:
Traceback <most recent call last>:
File "<stdin>", line 1, in <module>
NameError: name 'signal_power' is not defined
On the book says "The first example uses log10 to compute a signal-to-noise ratio in decibels (assuming that signal_power and noise_power are defined)."
So I assume that the problem might be that I didn't defined 'signal_power', but I don't know how to do it and what to assign to it...
This is the first time that I feel that this book is not holding my hand and I'm already lost. To be honest I don't understand this whole chapter.
By the way, I'm using Python2.7 and Windows XP. I may copy and paste the whole chapter if anyone feels that I should do it.
Python is my first language and I already tried to learn it using "Learn Python the hard way" but got stucked on chapter 16. So I decided to use "Think Python" and then go back to "Learn Python the hard way".
You've figured it out - you have to set signal_power's value before using it. As to what you have to set it to - it's not really a Python related question, but 1 is always a safe choice :) While you are at it, don't forget to define noise_power.
You indeed need to assign a value to both signal_power and noise_power. The author likely left them out because the values are arbitrary. Even when supplied with exact values in a text, you should also play around with the values. After all, there's not much point in having you type anything in if the results on the screen are the same as on the page.
signal_power = 100
noise_power = 17
This particular example defines the mathematical relationship between variables. If it were presented as such, it wouldn't be such a mystery that exact values were left out, as they would be supplied by someone when using the formula. The same holds for the code sample.
The book is for an older version of python apparently. But that is unrelated to your actual question.
Try defining signal_power. For example
signal_power = 0
You can't use a variable without defining/declaring it first. When you say ratio = signal_power / noise_power,
you're trying to access two variables that the interpreter has never heard about.
Before telling the machine what to do with the variables, you have to introduce them to it first.
You do this by declaring them:
signal_power = 123
noise_power = 321
You can define them as any number you want, not only 123 and 321, but you must define them before using them.

Controlling input/output of Python interactive shell

I have to evaluate (millions of) Python expressions e.g. (int(a) >> 8 == 4) and b
in my OCaml program. There is pycaml but I failed to get it working.
So I turned to another idea: control the input/output of Python interpreter directly.
Ideally I would like to intercept both the input/output of the interpreter itself.
By sending a = 3 b = 5 a > b to the interpreter, I would then be able to get the result False, as if I have done this by keyboard..
>>> a = 3
>>> b = 5
>>> a > b
False
>>>
However, my code doesn't working as expected (while the same code worked for some interactive program)
let (readme, writeme) = Unix.open_process "python -u";;
let _ = output_string writeme "3 + 5\n" in
let _ = flush writeme in
let result = input_line readme in
print_endline result;;
I tried changing 3 + 5\n to print 3\n, but it still hangs at input_line.
Is there any better way to do this? I would need to evaluate quite a lot of
expressions, so I don't really want to do this via a temp file. Any help appreciated,
Thanks.
I'm not going to comment on the weirdness of the entire concept (driving python to evaluate expressions from o'caml) but it seems like you might want to look into writing a python program that is an eval cycle that reads/writes a string from/to a pipe. Look up the eval command.
You can supply a command to the interpreter through the command line:
$ python -c 'a = 3; b = 5; print a > b'
False
Is that adequate for your needs?
If you're concerned about opening the interpreter repeatedly, you could generate and evaluate many expressions at once. I'm not sure what the upper limit is, but I was able to evaluate and print 200 concatenated copies of a = 3; b = 5; print a > b; without any problem.

Categories

Resources