Skip to content

A possible way to improve code readability and to facilitate code maintenance/refactor ? #2572

Open
@denis-migdal

Description

@denis-migdal

Currently, for performances reasons, some parts of the standard Python library is implemented in JS.

However, this has several limitations :

  • the JS code isn't necessary easy to read.
  • if Brython changes its helper functions or internal representations, this would require to update all of this JS code.
  • this may be source of errors.

My suggestion would then to write such code in Python, and to transform them into JS with slightly different ast2js rules. Brython would distribute the pre-generated JS files, i.e. not requiring the developers to generate them.

We could then define some standard python classes/functions with Python code, e.g.:

from JS import eval

class str:
     def replace(self, search: str, value: str) -> str:
          # do stuff
          assert True
          # can also write JS code :
          JS.eval("console.log('hi ;')"); 
          # maybe doing some stuff with f-string :
         return JS.eval(f"${self}.replaceAll(${search}, ${value})"); 

Would generate something like :

// do all the Brython class/method declaration suff.

function replace(list, search, value) {
         // python stuff converted into JS - with some different ast2js rules
         _b_.assert(true);
          // JS.eval() calls are directly replaced by the JS code.
         console.log('hi ;');
         return _b_.jsobj2pyobj( _b_.pyobj2jsobj(self).replaceAll( _b_.pyobj2jsobj(search), _b_.pyobj2jsobj(value) ) )
}

In few weeks, I plan to use this in SBrython, so I may write lot of python files that could be reused by Brython. For the JS conversion, I assume that Brython would prefer to write its own ast2js rules instead of relying on SBrython.

@PierreQuentel What do you think ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions