Mulan programming language 0.0.14.8: WebSocket chat demo; partly compare Python syntax

[Installation: pip install ulangPlease refer to PyPI for usage and routines . The source code is located in Open Source China . Note: Python 3.7 is required, and the source file needs UTF-8 encoding ]

This week , the routine that comes with the Python framework tornado to implement chat with websocket was changed to Mulan. By the way, the Mulan and Python syntax involved in this routine are compared and summarized, including the super syntax reproduced this week.

1 Example demonstration

Chat example

The main Mulan code is as follows (the complete code is here )

type 聊天渠道 : tornado.websocket.WebSocketHandler {
  {
    接收端 = set()
    历史 = []
    历史长度 = 200
  }

  func $get_compression_options {
    // Non-None enables compression with default options.
    return {:}
  }

  func $open {
    聊天渠道.接收端.add($)
  }

  func $on_close {
    聊天渠道.接收端.remove($)
  }

  func 更新历史(消息) {
    // 必须指定类
    聊天渠道.历史.append(消息)
    if len(聊天渠道.历史) > 聊天渠道.历史长度 {
      聊天渠道.历史 = 聊天渠道.历史[-聊天渠道.历史长度 :]
    }
  }

  func 广播(消息) {
    logging.info("发到%d个接收端", len(聊天渠道.接收端))
    for 某接收端 in 聊天渠道.接收端 {
      // TODO: try catch 尚待重现
      //try:
      某接收端.write_message(消息)
      //except:
      //    logging.error("Error sending message", exc_info=True)
    }
  }

  func $on_message(内容) {
    logging.info("收到消息: %r", 内容)
    parsed = tornado.escape.json_decode(内容)
    消息 = {"id": str(uuid.uuid4()), "body": parsed["body"]}
    消息["html"] = tornado.escape.to_basestring(
      $render_string("message.html", message=消息)
    )

    聊天渠道.更新历史(消息)
    聊天渠道.广播(消息)
  }
}

2 Comparison with Python syntax involved in routines

The following are based on the current understanding of Mulan, and may be modified in the future as the reproduction project progresses.

Must (otherwise an error will be reported)

  • If the keyword of a formal parameter is type, it needs to be changed to a positional parameter. As in the original routine:
define("port", default=8888, help="run on the given port", type=int)

The keyword of the last parameter is type, which happens to be Mulan's grammatical keyword. Therefore, it can only be changed to positional parameters (by the way, Chinese cultural package):

func 定义(名称 : str, 默认=nil, 类型=nil, 帮助=nil) {
  define(名称, 默认, 类型, 帮助)
}
定义("port", 默认=8888, 帮助="在此端口运行服务", 类型=int)
  • Mulan has only static methods (staticmethod). When encountering Python's classmethod, replace cls with the class name when referencing class variables. See above 更新历史method.
  • Need tuple () to initialize a tuple, see previously introduced
  • All blocks are changed to {}, replacing the colon in Python
  • class changes type, use a colon to specify the type, including the specified parameter type, super class, etc.
  • Use /* */ for comment section and // for comment line
  • import becomes using
  • None becomes nil
  • def becomes func
  • 【This week】super() becomes super
  • [This week] ** is not supported, use enumerated parameters instead
  • Class variables need to be surrounded by {}
  • Dictionary initialization{:}

Optional

  • $ self variable, see previously introduced
  • Constructor use class name instead of __init__
  • Can be omitted when no parameter function definition ()

Attachment: Code amount statistics

The main part of the code line statistics, the format is: last time -> now.

  • Mulan code size 2965 -> 3114
    • Editor, implementation and testing are all Mulan code: 432 -> 431 (In order to realize the research network service framework for the front and back ends)
    • Mulan test cases, including some useful small programs (such as Tic-Tac-Toe): 2533 -> 2683

 

  • Python code volume (Mulan implementation and testing framework): 2594 -> 2612
    • 分析器/语法分析器.py:1008 -> 1019
    • 分析器/词法分析器.py:204 -> 207
    • 测试/运行所有.py, Check all Mulan test code fragments: 192 -> 194
    • 环境.py, Define the global method: 171 -> 172
    • 分析器/语法成分.py, The enumeration constant extracted from the parser: 81 -> 82
    • 功用/反馈信息.py:65 -> 67
    • 测试/unittest/语法树.pyTo ensure that the generated syntax tree is consistent with the original version: 67 -> 66
    • Unchanged
      • 分析器/语法树.py:202
      • 交互.py, Interactive Environment (REPL): 138
      • 中.py, The main program: 74
      • 功用/调试辅助.py,:57
      • setup.py, 34
      • 分析器/错误.py:17
      • 测试/unittest/交互.py, Interactive environment related tests: 28

Guess you like

Origin www.oschina.net/news/119870/mulan-0-0-14-8-released