How Can We Help?

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:

  • Way 1: Add common code in hat blocks
  • Way 2: Modify the transcoding template
  • Way 3: Define common code in middleware

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.

The transcoding is as follows:

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.

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

1    # generated by mBlock5 for <product>
2    # codes make you happy
3    ### import #$$
4    ### lib #$$
5    ###
6    # initialize variables
7    ###{
8         (this.$ALL_VARIABLES.length === 0) ? undefined : this.$ALL_VARIABLES.map(n => n + ‘ = 0’).join(‘\\n’)
9    }#$$
10   #$$
11   def add(a, b):
12         print(‘%d+%d=%d’%(a,b,a+b))
13   ### code #$$
14   ###
15   while True:
16        # every tick
17        ### loop #$$
18   #$$

 

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.

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

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

  1. Click + to add middleware.

      2. Set the name of the middleware.

3. Check Custom.

4. Edit the code.

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:

1    /**
2    * Custom Upload Middleware
3    *
4    * @param {AppContext} app
5    * @param {DeviceContext} device
6    * @param {String} code
7    * @param {Object} params
8    * @param {Function} logH
9    * @param {Function} progessH
10  */
11   (app, device, code, params, logH, progessH) => {
12         // process code here
13         code += ‘def add(a,b):
14         print(‘%d+%d=%d’%(a,b,a+b))\n’;
15         return code;
16   }

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: ★