inspect.currentframe() can depending on the python implementation return None as stated in the documentation:
CPython implementation detail: This function relies on Python stack frame support in the interpreter, which isn’t guaranteed to exist in all implementations of Python. If running in an implementation without Python stack frame support this function returns
None.
mypy is just letting you know that's the case. To handle this you can just check if the return of currentframe() is None
from types import FrameType frame = inspect.currentframe() if frame is None: raise RuntimeError("unsupported python implementation") # or give a default name fn_name: str = frame.f_code.co_name
python - inspect.getargvalues() throws exception "AttributeError: 'tuple' object has no attribute 'f_code'" - Stack Overflow
final project - AttributeError: 'function' object has no attribute 'method' - CS50 Stack Exchange
python - How to Prevent "AttributeError: 'function' object has no attribute ''" - Stack Overflow
python - AttributeError when using pandas to_sql - Stack Overflow
Videos
This similar question helped me discover the problem.
The Python documentation for the inspect module mentions both "frame records" and "frame objects", and explains the difference.
inspect.currentframe()returns a frame object, butinspect.getouterframes()returns a list of frame records.
The mistake in the code above is not extracting the frame object from the frame record of the calling function, and passing inspect.getouterframes() the frame record instead of the frame object. (Note that inspect.getouterframes() doesn't check that its argument is a frame object.)
Here's the fixed definition of caller_args() (with the change to the assignment to caller_frame):
def caller_args():
frame = inspect.currentframe()
outer_frames = inspect.getouterframes(frame)
caller_frame = outer_frames[1][0]
return inspect.getargvalues(caller_frame)
Which runs as desired:
$ python getargvalues_test_fixed.py
ArgInfo(args=['arg1'], varargs=None, keywords=None, locals={'arg1': 'foo'})
cause of error
AttributeError: 'tuple' object has no attribute 'f_code'
in your function
def caller_args()
is that caller_frame is an array of which you need item [1][0] as argument for
inspect.getargvalues(...)
this works :
currframe = inspect.currentframe()
callerframe = inspect.getouterframes(currframe, 2)
inspect.getargvalues(callerframe[1][0])
Also, the getargvalues function returns 4 values. First three are unimportant in this case, fourth contains JSON like format key/value list of callerframe arguments
_,_,_,values = inspect.getargvalues(callerframe[1][0])
for i in values:
argsstring += str(i) + ' : ' + str(values[i])
My test looks like this :
import inspect
def log(text):
currframe = inspect.currentframe()
callerframe = inspect.getouterframes(currframe, 2)
_,_,_,values = inspect.getargvalues(callerframe[1][0])
argsstring = ''
for i in values:
argsstring += str(i) + ' : ' + str(values[i])
print('name of file : ' + callerframe[1][1])
print('name of function : ' + callerframe[1][3])
print('line number : ' + str(callerframe[1][2]))
print('caller function arguments : ' + argsstring)
def doTest(text):
log(text)
doTest('this is a test')
You are receiving an error because alphabet is a variable defined within a function. It is therefore a local variable which cannot be accessed outside of that function.
I am very confused as to why there is a function there in the first place. I would replace everything under the alphabet function with just the variable containing the characters in order.
There are actually quite a few things wrong with this code - the code in the for loop is flawed as well. Here is my solution, with the added benefit of printing the encoded message as one string and not as a load of characters, as well as dealing with non-alphanumeric characters.
elif option1 == 2:
alphabet = 'abcdefghijklmnopqrstuvwxyz'
message = input('What message would you like to encrypt?')
encoded = []
for character in message:
if character.isalpha():
encoded.append(alphabet[25-alphabet.find(character)])
else:
encoded.append(character)
print(''.join(encoded))
The alphabet variable is a local variable, so you can only call it in the alphabet() function. When you put alphabet.find(), it thought you alphabet was the function, not the variable.
You can do:
elif(option1 == 2):
alphabet = "abcdefghijklmnopqrstuvwxyz"
for char in message:
print(alphabet[25-alphabet.find(char)])
That should work.
morseCodeM={".-":"A",
"-...":"B",
"-.-.":"C",
"-..":"D",
".":"E",
"..-.":"F",
"--.":"G",
"....":"H",
"..":"I",
".---":"J",
"-.-":"K",
".-..":"L",
"--":"M",
"-.":"N",
"---":"O",
".--.":"P",
"--.-":"Q",
".-.":"R",
"...":"S",
"-":"T",
"..-":"U",
"...-":"V",
".--":"W",
"-..-":"x",
"-.--":"Y",
"--..":"Z",
" / ":" "}
def morseToText(inp):
out=""
while(inp!=" "):
for i in morseCodeM:
if(inp.find(i)!=-1): <-----------------------
out=out+morseCodeM[i]
inp2=list(inp)
inp2[inp.find(i):inp.find(i)+(len(i)-1)]=""
inp="".join(inp2)
return outI honestly don't know whats wrong it just gives the error :"AttributeError: 'function' object has no attribute 'find'" on the line with the arrow .
edit: the code is about converting morse code to text