How can we import everything (*) from an external task file when using blocks? #1893
Replies: 1 comment
-
This is great! I'm glad you discovered the import functionality.
With The block users have to import just one function. And then they would specify which function they really want to call, by name: Create a file called from pybricks.tools import wait
# Example function
def hello(text):
print("Hi,", text)
# Another example function
def beep(hub, count):
for i in range(count):
hub.speaker.beep()
wait(50)
# This helper function will select one of the above functions
# and call it by passing on any of the arguments you give.
def function(name, *args, **kwargs):
return globals()[name](*args, **kwargs) So block users still achieve a single import. The beep example function illustrates how Python functions can make use of the devices that are set up by the blocks. If you are going to use multitasking, this will look a bit different. Let me know if you'd like an example for that too. |
Beta Was this translation helpful? Give feedback.
-
As an FLL coach moving our team to PyBricks, we're hoping to setup a method to help the varying levels of skills on our team work together in coding. Our more advanced members will use python, implementing a core robot library to provide all the various functions. Then individual members can work on mission ideas using either Python or blocks. We'd rather not require the missions to import each and every function from the robot library. A Python mission file would simply:
from our_robot import *
The trouble is we can't figure out a way to do that using blocks. When we try supplying * in the External Task setup block, it replaces it with _2 in the equivalent python.
Beta Was this translation helpful? Give feedback.
All reactions