Posts: 4,784
Threads: 76
Joined: Jan 2018
(Aug-15-2022, 11:38 PM)Skaperen Wrote: these users are not coders and probably have no understanding of a traceback I had the same problem once with a drawing language that I created for non programming users. The solution that I used was a very strong arguments checking for the commands that those users could call. They virtually could not call the commands with wrong arguments, so that the commands could not fail. This strategy has some limitations too.
Posts: 4,646
Threads: 1,493
Joined: Sep 2016
(Aug-14-2022, 11:43 PM)bowlofred Wrote: That's exactly what the 'inspect.stack()' line I posted above should do. Did you try it? i don't understand how to use it.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,646
Threads: 1,493
Joined: Sep 2016
Aug-16-2022, 10:23 PM
(This post was last modified: Aug-16-2022, 10:23 PM by Skaperen.)
(Aug-16-2022, 03:18 PM)Gribouillis Wrote: They virtually could not call the commands with wrong arguments if the user specifies it. the code maker (makes Python3 code) uses that name instead of trying to figure out what the user really meant. the user can get things wrong.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 1,583
Threads: 3
Joined: Mar 2020
Aug-16-2022, 10:47 PM
(This post was last modified: Aug-16-2022, 10:49 PM by bowlofred.)
You just call it from within the function that wants to know the way it was called.
import inspect
def foo():
print("hello")
print(f"Called as {inspect.stack()[1][4][0]}")
bar = foo
foo()
bar()
Skaperen and ibreeden like this post
Posts: 12,025
Threads: 484
Joined: Sep 2016
There is some code that I wrote a long time ago here: https://python-forum.io/thread-3347.html...call+stack
which follows the execution path from a given point.
Here, i believe group[0].function will contain the function name of the calling method
It's been a while, but I think that is correct.
Posts: 453
Threads: 16
Joined: Jun 2022
Aug-21-2022, 12:27 PM
(This post was last modified: Aug-21-2022, 12:27 PM by rob101.)
@ Skaperen
I've just been reading this...
https://python-course.eu/advanced-python...ration.php
... which put me in mind of this thread. I'm not too sure if the information from the above link can be applied to what you have in mind, but it maybe it's a step in the right direction.
Sig:
>>> import this
The UNIX philosophy: "Do one thing, and do it well."
"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse
"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Posts: 4,646
Threads: 1,493
Joined: Sep 2016
(Aug-14-2022, 12:59 AM)bowlofred Wrote: You could try inspect.stack()[1][4][0] and see if that is useful. i tried it like this:
Output: lt1a/forums/1 /home/forums 12> cat func_name.py
def foo(*args,**kwargs):
import inspect
me = inspect.stack()[1][4][0]
print('i was called as',repr(me),flush=True)
return me
bar = foo
print(f'woot {foo()},{bar(456)}',flush=True)
lt1a/forums/1 /home/forums 13> python3.8 func_name.py
i was called as "print(f'woot {foo()},{bar(456)}',flush=True)\n"
i was called as "print(f'woot {foo()},{bar(456)}',flush=True)\n"
woot print(f'woot {foo()},{bar(456)}',flush=True)
,print(f'woot {foo()},{bar(456)}',flush=True)
lt1a/forums/1 /home/forums 14>
but that was not what i wanted. i only want the name. with this i would need to parse the line (not too hard) and determine which name this call was (much harder).
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,784
Threads: 76
Joined: Jan 2018
Aug-22-2022, 07:24 AM
(This post was last modified: Aug-22-2022, 07:24 AM by Gribouillis.)
Skaperen Wrote:with this i would need to parse the line (not too hard) and determine which name this call was (much harder). You can walk a line with the ast module to find all the expressions that were called in the line
>>> line = "print(f'woot {foo()},{bar(456)}',flush=True)\n"
>>> import ast
>>> p = ast.parse(line)
>>> for node in ast.walk(p):
... if isinstance(node, ast.Call):
... print(ast.unparse(node.func))
...
print
foo
bar other example
>>> line = "xyz[0](spam, bar.x[1].egg(3))"
>>> p = ast.parse(line)
>>> for node in ast.walk(p):
... if isinstance(node, ast.Call):
... print(ast.unparse(node.func))
...
xyz[0]
bar.x[1].egg Note: ast.unparse() needs Python >= 3.9. For earlier pythons, there is an «astunparse» module in Pypi.
Posts: 4,646
Threads: 1,493
Joined: Sep 2016
short answer: the solution is complex.
since this project design is not yet selected, the ideas that need to have this function discover how it got called will be ruled out. hopefully a simpler design for my project can be worked out.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
|