pythonmicropythonlego-mindstormsspike

I have a python module and want to call all constants


I have a Lego mindstorms 51515 and like to program it with python.

There are some default image in the module I'd like to loop over and use.

import hub
images = dir(hub.Image)
print(images)

Output:

['__class__', '__name__', '__bases__', '__dict__', 'ALL_ARROWS', 'ALL_CLOCKS', 'ANGRY', 'ARROW_E', 'ARROW_N', 'ARROW_NE', 'ARROW_NW', 'ARROW_S', 'ARROW_SE', 'ARROW_SW', 'ARROW_W', 'ASLEEP', 'BUTTERFLY', 'CHESSBOARD', 'CLOCK1', 'CLOCK10', 'CLOCK11', 'CLOCK12', 'CLOCK2', 'CLOCK3', 'CLOCK4', 'CLOCK5', 'CLOCK6', 'CLOCK7', 'CLOCK8', 'CLOCK9', 'CONFUSED', 'COW', 'DIAMOND', 'DIAMOND_SMALL', 'DUCK', 'FABULOUS', 'GHOST', 'GIRAFFE', 'GO_DOWN', 'GO_LEFT', 'GO_RIGHT', 'GO_UP', 'HAPPY', 'HEART', 'HEART_SMALL', 'HOUSE', 'MEH', 'MUSIC_CROTCHET', 'MUSIC_QUAVER', 'MUSIC_QUAVERS', 'NO', 'PACMAN', 'PITCHFORK', 'RABBIT', 'ROLLERSKATE', 'SAD', 'SILLY', 'SKULL', 'SMILE', 'SNAKE', 'SQUARE', 'SQUARE_SMALL', 'STICKFIGURE', 'SURPRISED', 'SWORD', 'TARGET', 'TORTOISE', 'TRIANGLE', 'TRIANGLE_LEFT', 'TSHIRT', 'UMBRELLA', 'XMAS', 'YES', 'get_pixel', 'height', 'set_pixel', 'shift_down', 'shift_left', 'shift_right', 'shift_up', 'width']

All the full names that have only upper case letter should be constants I would like to loop over.

How to do this?

for dynamic_image in images:
    if not dynamic_image.isupper():
        continue
    var = hub.Image.{dynamic_image} # This is the real question on how to do this

Solution

  • I solved it, and the answer is as folling:

    import hub
    import time
    
    images = dir(hub.Image)
    while True:
        for image in images:
            if image.upper() != image:
                continue
            print(image)
            img = hub.Image.__dict__[image]  # This was what I was looking  for
            hub.display.show(img)
            time.sleep(1)