After I create a class instance from test_script.py, I would like to view these variables. I know that in Pycharm, when running a Python script, you have the ability to view these variables in a variable pane; however, using the Micropython plugin, I am unable to view these variables in a the same way. Does anyone have any other suggestions? This is needed for validation that a class function is working properly.
I've tried calling dir(); however, it appears to only list out the modules that are imported as well as the name of the class instance without listing any of the class variables. Another solution in Python is to use os.environ.items(); however, I don't believe os in Micropython has the environ method built in. The same thing goes for calling %who or %whos.
I understand that you can call the .__dict__.keys() or .__dict__.values() commands on a class object to print out all class variables and their respective values; however, this is a somewhat unorganized and user-unfriendly approach to viewing class variables. I am looking for something that can provide a slightly better visual experience, since I need to validate several methods that are constantly changing these class variables.
Viewing all class variables created within class instance
Re: Viewing all class variables created within class instance
Try:
as a test then maybe you can modify that for your purposes.
Just a guess.
Code: Select all
dir(machine.ADC)
Just a guess.
Re: Viewing all class variables created within class instance
Thanks, @davef. That provides me with a list that looks like:
['__class__','__name__','__bases__','__dict__','CORE_TEMP','CORE_VBAT','CORE_VREF','VREF','read_u16']
The only one of those that is callable on a class instance is '__dict__', which effectively just prints out a large dictionary.
['__class__','__name__','__bases__','__dict__','CORE_TEMP','CORE_VBAT','CORE_VREF','VREF','read_u16']
The only one of those that is callable on a class instance is '__dict__', which effectively just prints out a large dictionary.
Re: Viewing all class variables created within class instance
Call dir() on your instance, not on the class itself. Like
Code: Select all
a = machine.ADC()
dir(a)
Re: Viewing all class variables created within class instance
I tried what you mentioned, though I don't necessarily see how it differs from the previously mentioned suggestion. Here is my output:
Re: Viewing all class variables created within class instance
Without the brackets after ADC, you are not creating an instance of the class i.e. not calling the constructor.
Your first attempt was correct, but you have to supply arguments (as the error says), sorry but I left that out in my sample because i don't know them by heart plus they might be different depending on the board. See e.g. http://docs.micropython.org/en/latest/l ... e.ADC.html
Your first attempt was correct, but you have to supply arguments (as the error says), sorry but I left that out in my sample because i don't know them by heart plus they might be different depending on the board. See e.g. http://docs.micropython.org/en/latest/l ... e.ADC.html
Re: Viewing all class variables created within class instance
For completeness the difference is this:
output:
That y isn't available just by looking at the class itself, only after creating an instance since y gets assigned in the constructor.
Code: Select all
class Foo:
x = 0
def __init__(self, y):
self.y = 0
print(dir(Foo))
print(dir(Foo(1)))
Code: Select all
['__class__', '__init__', '__module__', '__name__', '__qualname__', '__bases__', '__dict__', 'x']
['__class__', '__init__', '__module__', '__qualname__', '__dict__', 'x', 'y']