Configuration — Common Python Code Configuration

Functions or variables that are shared by multiple blocks are referred to as common functions or variables, and you can define and modify them in a centralized way by configuring common Python code.

Common Python code can be configured in multiple ways. This page describes the following three ways:

Add common code in a hat block

Generally, blocks that are to be translated into Python code are put under the hat block named when () starts up, and therefore, you can add the common code in the lib of the hat block.

1.png

The transcoding is as follows:

2.png

Advantage: You can see the transcoding in real time when putting blocks together.

Disadvantage: The common code is scattered and depends on some specific blocks.

Modify the transcoding template

When configuring an extension or device extension, click Transcode settings, set Transcoding language support to Python, and modify Python transcoding template.

3.png

Place the add function on top of ### code #$$, as shown in the following:

# generated by mBlock5 for <product>
# codes make you happy
### import #$$
### lib #$$
###
# initialize variables
###{
  (this.$ALL_VARIABLES.length === 0) ? undefined : this.$ALL_VARIABLES.map(n => n + ' = 0').join('\\n')
}#$$
#$$
def add(a, b):
    print('%d+%d=%d'%(a,b,a+b))
### code #$$
###
while True:
  # every tick
  ### loop #$$
#$$

 

Advantage: You can see the transcoding in real time when putting blocks together.

Disadvantage: If two much common code is added, the transcoding template may not be easy to read, and thus may be difficult to maintain.

Define common code in middleware

When configuring an extension, click Basic information and configure the common code in middleware.

4.png

When configuring a device extension, click Connection settings and configure the common code in middleware.

5.png

The following describes how to configure middleware for a device extension:

1. Click + to add middleware.

6.png

2. Set the name of the middleware.

7.png

3. Check Custom.

8.png

4. Edit the code.

9.png

The code shown in the preceding is the strings into which the blocks are translated. You can insert the common code required into the code and then return the code, as shown in the following:

/**
 * Custom Upload Middleware
 * 
 * @param {AppContext} app
 * @param {DeviceContext} device
 * @param {String} code
 * @param {Object} params
 * @param {Function} logH
 * @param {Function} progessH
 */
(app, device, code, params, logH, progessH) => {
    // process code here
    code += 'def add(a,b):
    print('%d+%d=%d'%(a,b,a+b))\n';
    return code;
}

 

Advantage: You can write any code that you don't want others to see in the middleware.

Disadvantage: Transcoding can't be seen in the transcoding area in real time. You can see the transcoding only after clicking Upload to upload a program to a device.

Summary

Way 1: Easy to get started and no more learning required

Recommendation rating: ★★★★★

Way 2: Easy to get started but template maintenance difficulty increased with the increase of common code

Recommendation rating: ★★

Way 3: Not easy to get started and understand, working better for hackers

Recommendation rating: ★

Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.