4. 宏指令 -- Highway MVVM

本文介绍了一种文本元素中的属性值替换方法,包括宏指令的绑定方式:单向与双向,并探讨了如何自定义宏指令,实现数值格式转换等功能。

文本元素中属性值替换

<span>hi, i am {{name}}, {{old}} ages old</span>

// 无效。宏指令不能有相邻兄弟节点。
<span>hi, i am {{name}}, {{old}} ages old <strong></strong></span>

宏指令不能有相邻兄弟节点,否则无法生效

4-1. 内建

4-1-1. 绑定

绑定部分参考第二章节2. 绑定,同。

双向绑定-安全{{ prop }}hi, i am {{name}}
双向绑定-非安全{{{ prop }}}hi, i am {{{name}}}
单向绑定-安全[[ prop ]]hi, i am [[name]]
单向绑定-非安全[[[ prop ]]]hi, i am [[[name]]]

4-2. 自定义

自定义宏指令为一个工厂函数

4-2-1. 入参

  • el:<span>hi,user.name</span>el值为$(span)。
  • exp:hi,user.nameexp值为’{{user.name}}’
  • $update: 更新函数。hi, i am {{name}}, {{old}} ages old中有两个宏指令实例,任意一个值发生变化都需要触发更新。
  • $scope: 当前作用域。
  • $ctx: 当前上下文。
// 定义一个宏指令工厂函数,将数值前添加$符,格式为$100$
const money = function ({$exp, $update, $scope, $ctx}) {
  const money = $exp.replace
};

4-2-2. 出参

  • $mount: 生命周期函数,挂载时调用
  • $unmount: 生命周期函数,销毁时调用
  • iterator:text参数,为当前的文本内容;常量时需要返回$value值。

4-2-3. 全局

使用Highway.marco定义,需指定正则表达式和宏指令工厂函数

4-2-4. 局部

指定视图中有效,通过$marcos指定,需指定正则表达式和宏指令工厂函数

4-2-5. 实例

自定义宏指令,将数值转化为百分比格式,指令格式为prop[prop], 其中指定prop为双向绑定,[prop]单向绑定。

examples/macros/customized.html

<div id="app">
  <p>two way bind is: ${ratio}$</p>
  <p>one way bind is: $[ratio]$</p>
  <button hi-on:click="change">Change</button>
</div>

// 自定义指令
const percentage = function ({$exp, $update, $scope, $ctx}) {
    const prop = $exp.replace(/[\\${}\[\] ]/g, '');
    const handler = function () {
      return ($scope.$get(prop) - 0) * 100 + '%';
    };

    // 双向绑定
    if (/\$\{[^\\}]+\}\$/.test($exp)) {
      return {
        $mount() {
          $scope.$watch(prop, $update);
        },
        $unmount() {
          $scope.$unwatch(prop, $update);
          pipeline.destroy();
        },
        $iterator($text) {
          return $text.replace($exp, function () {
            return handler();
          });
        }
      }
    } else {
      return {
        $iterator: {
          $value: handler()
        }
      }
    }
  };

  // 全局
  //Highway.macro('\\$\\{[^$]+\\}\\$|\\$\\[[^$]+\\]\\$', percentage)

  const app = new Highway({
    $el: $('#app'),
    $scope: {
      ratio: 0.95
    },
    // 局部
    $macros: {
      '\\$\\{[^$]+\\}\\$|\\$\\[[^$]+\\]\\$': percentage
    },
    change() {
      this.$scope.$set('ratio', '0.90')
    }
  });
(.venv) PS C:\Users\chen4\PycharmProjects\pythonProject1\.venv\Scripts> pip install gluonnlp Collecting gluonnlp Using cached gluonnlp-0.10.0.tar.gz (344 kB) Preparing metadata (setup.py) ... done Requirement already satisfied: numpy>=1.16.0 in c:\users\chen4\pycharmprojects\pythonproject1\.venv\lib\site-packages (from gluonnlp) (1.26.4) Collecting cython (from gluonnlp) Using cached cython-3.1.2-cp312-cp312-win_amd64.whl.metadata (6.0 kB) Requirement already satisfied: packaging in c:\users\chen4\pycharmprojects\pythonproject1\.venv\lib\site-packages (from gluonnlp) (24.1) Using cached cython-3.1.2-cp312-cp312-win_amd64.whl (2.7 MB) Building wheels for collected packages: gluonnlp DEPRECATION: Building 'gluonnlp' using the legacy setup.py bdist_wheel mechanism, which will be removed in a future version. pip 25.3 will enforce this behaviour change. A possib le replacement is to use the standardized build interface by setting the `--use-pep517` option, (possibly combined with `--no-build-isolation`), or adding a `pyproject.toml` file to the source tree of 'gluonnlp'. Discussion can be found at https://github.com/pypa/pip/issues/6334 Building wheel for gluonnlp (setup.py) ... error error: subprocess-exited-with-error × python setup.py bdist_wheel did not run successfully. │ exit code: 1 ╰─> [136 lines of output] C:\Users\chen4\PycharmProjects\pythonProject1\.venv\Lib\site-packages\setuptools\__init__.py:92: _DeprecatedInstaller: setuptools.installer and fetch_build_eggs are deprecated. !! ******************************************************************************** Requirements should be satisfied by a PEP 517 installer. If you are using pip, you can try `pip install --use-pep517`. By 2025-Oct-31, you need to update your project and remove deprecated calls or your builds will no longer be supported. ******************************************************************************** !! dist.fetch_build_eggs(dist.setup_requires) running bdist_wheel running build running build_py creating build\lib.win-amd64-cpython-312\gluonnlp copying src\gluonnlp\base.py -> build\lib.win-amd64-cpython-312\gluonnlp copying src\gluonnlp\_constants.py -> build\lib.win-amd64-cpython-312\gluonnlp copying src\gluonnlp\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp creating build\lib.win-amd64-cpython-312\gluonnlp\calibration copying src\gluonnlp\calibration\collector.py -> build\lib.win-amd64-cpython-312\gluonnlp\calibration copying src\gluonnlp\calibration\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\calibration creating build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\baidu_ernie_data.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\candidate_sampler.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\classification.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\conll.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\dataloader.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\dataset.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\datasetloader.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\glue.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\intent_slot.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\question_answering.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\registry.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\sampler.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\sentiment.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\stream.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\super_glue.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\transforms.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\translation.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\utils.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\word_embedding_evaluation.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\data creating build\lib.win-amd64-cpython-312\gluonnlp\embedding copying src\gluonnlp\embedding\evaluation.py -> build\lib.win-amd64-cpython-312\gluonnlp\embedding copying src\gluonnlp\embedding\token_embedding.py -> build\lib.win-amd64-cpython-312\gluonnlp\embedding copying src\gluonnlp\embedding\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\embedding creating build\lib.win-amd64-cpython-312\gluonnlp\initializer copying src\gluonnlp\initializer\initializer.py -> build\lib.win-amd64-cpython-312\gluonnlp\initializer copying src\gluonnlp\initializer\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\initializer creating build\lib.win-amd64-cpython-312\gluonnlp\loss copying src\gluonnlp\loss\activation_regularizer.py -> build\lib.win-amd64-cpython-312\gluonnlp\loss copying src\gluonnlp\loss\label_smoothing.py -> build\lib.win-amd64-cpython-312\gluonnlp\loss copying src\gluonnlp\loss\loss.py -> build\lib.win-amd64-cpython-312\gluonnlp\loss copying src\gluonnlp\loss\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\loss creating build\lib.win-amd64-cpython-312\gluonnlp\metric copying src\gluonnlp\metric\length_normalized_loss.py -> build\lib.win-amd64-cpython-312\gluonnlp\metric copying src\gluonnlp\metric\masked_accuracy.py -> build\lib.win-amd64-cpython-312\gluonnlp\metric copying src\gluonnlp\metric\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\metric creating build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\attention_cell.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\bert.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\bilm_encoder.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\block.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\convolutional_encoder.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\elmo.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\highway.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\info.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\language_model.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\lstmpcellwithclip.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\parameter.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\sampled_block.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\seq2seq_encoder_decoder.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\sequence_sampler.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\transformer.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\translation.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\utils.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\model creating build\lib.win-amd64-cpython-312\gluonnlp\optimizer copying src\gluonnlp\optimizer\bert_adam.py -> build\lib.win-amd64-cpython-312\gluonnlp\optimizer copying src\gluonnlp\optimizer\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\optimizer creating build\lib.win-amd64-cpython-312\gluonnlp\utils copying src\gluonnlp\utils\files.py -> build\lib.win-amd64-cpython-312\gluonnlp\utils copying src\gluonnlp\utils\parallel.py -> build\lib.win-amd64-cpython-312\gluonnlp\utils copying src\gluonnlp\utils\parameter.py -> build\lib.win-amd64-cpython-312\gluonnlp\utils copying src\gluonnlp\utils\seed.py -> build\lib.win-amd64-cpython-312\gluonnlp\utils copying src\gluonnlp\utils\version.py -> build\lib.win-amd64-cpython-312\gluonnlp\utils copying src\gluonnlp\utils\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\utils creating build\lib.win-amd64-cpython-312\gluonnlp\vocab copying src\gluonnlp\vocab\bert.py -> build\lib.win-amd64-cpython-312\gluonnlp\vocab copying src\gluonnlp\vocab\elmo.py -> build\lib.win-amd64-cpython-312\gluonnlp\vocab copying src\gluonnlp\vocab\subwords.py -> build\lib.win-amd64-cpython-312\gluonnlp\vocab copying src\gluonnlp\vocab\vocab.py -> build\lib.win-amd64-cpython-312\gluonnlp\vocab copying src\gluonnlp\vocab\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\vocab creating build\lib.win-amd64-cpython-312\gluonnlp\data\batchify copying src\gluonnlp\data\batchify\batchify.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\batchify copying src\gluonnlp\data\batchify\embedding.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\batchify copying src\gluonnlp\data\batchify\language_model.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\batchify copying src\gluonnlp\data\batchify\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\batchify creating build\lib.win-amd64-cpython-312\gluonnlp\data\bert copying src\gluonnlp\data\bert\glue.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\bert copying src\gluonnlp\data\bert\squad.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\bert copying src\gluonnlp\data\bert\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\bert creating build\lib.win-amd64-cpython-312\gluonnlp\data\corpora copying src\gluonnlp\data\corpora\google_billion_word.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\corpora copying src\gluonnlp\data\corpora\large_text_compression_benchmark.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\corpora copying src\gluonnlp\data\corpora\wikitext.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\corpora copying src\gluonnlp\data\corpora\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\corpora creating build\lib.win-amd64-cpython-312\gluonnlp\data\xlnet copying src\gluonnlp\data\xlnet\squad.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\xlnet copying src\gluonnlp\data\xlnet\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\xlnet creating build\lib.win-amd64-cpython-312\gluonnlp\model\train copying src\gluonnlp\model\train\cache.py -> build\lib.win-amd64-cpython-312\gluonnlp\model\train copying src\gluonnlp\model\train\embedding.py -> build\lib.win-amd64-cpython-312\gluonnlp\model\train copying src\gluonnlp\model\train\language_model.py -> build\lib.win-amd64-cpython-312\gluonnlp\model\train copying src\gluonnlp\model\train\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\model\train running egg_info writing src\gluonnlp.egg-info\PKG-INFO writing dependency_links to src\gluonnlp.egg-info\dependency_links.txt writing requirements to src\gluonnlp.egg-info\requires.txt writing top-level names to src\gluonnlp.egg-info\top_level.txt reading manifest file 'src\gluonnlp.egg-info\SOURCES.txt' reading manifest template 'MANIFEST.in' warning: no files found matching '*.py' under directory 'gluonnlp' warning: no previously-included files matching '*' found under directory 'tests' warning: no previously-included files matching '*' found under directory 'scripts' adding license file 'LICENSE' writing manifest file 'src\gluonnlp.egg-info\SOURCES.txt' copying src\gluonnlp\data\fast_bert_tokenizer.c -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\fast_bert_tokenizer.pyx -> build\lib.win-amd64-cpython-312\gluonnlp\data running build_ext Compiling src/gluonnlp/data/fast_bert_tokenizer.pyx because it changed. [1/1] Cythonizing src/gluonnlp/data/fast_bert_tokenizer.pyx building 'gluonnlp.data.fast_bert_tokenizer' extension error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/ [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for gluonnlp Running setup.py clean for gluonnlp Failed to build gluonnlp ERROR: Failed to build installable wheels for some pyproject.toml based projects (gluonnlp) 以上为安装gluonnlp的报错结果
06-27
在使用 `pip install gluonnlp` 时,如果遇到错误提示 **"error: Microsoft Visual C++ 14.0 or greater is required"**,这通常意味着正在安装的库依赖于需要编译的 C/C++ 扩展代码,而当前系统缺少必要的构建工具。Windows 系统默认不包含这些工具,因此需要手动安装。 ### 三级标题:问题原因与解决方法 当尝试通过 pip 安装某些包含原生扩展的 Python 库(如 `gluonnlp`)时,若未安装 Microsoft Visual C++ 构建工具,则会触发该错误。这是因为 pip 需要调用本地编译器来构建部分非纯 Python 编写的组件[^1]。 一种直接的解决方案是访问 [Microsoft Visual C++ Build Tools](https://visualstudio.microsoft.com/visual-cpp-build-tools/) 并下载安装包进行安装。此工具集提供了必要的编译器、库和 SDK,以支持在 Windows 上构建原生代码项目[^2]。安装完成后,重新运行 `pip install gluonnlp` 命令即可继续安装过程。 对于那些希望避免安装完整 Visual Studio 或者 Build Tools 的用户来说,可以考虑寻找预编译的 wheel 文件来绕过编译步骤。例如,从可信源或社区维护的仓库中获取适用于 Windows 的 `.whl` 文件,并使用以下命令进行安装: ```bash pip install <file_path>.whl ``` 这种方法有助于减少环境中的额外依赖项,同时加快安装速度[^3]。 此外,在容器化环境中部署应用时,可以通过预先配置好所需构建工具的基础镜像来规避此类问题。这样可以在保证最小化内存占用的同时完成依赖库的正确安装[^3]。 ### 三级标题:替代方案 如果上述方法仍然无法解决问题,或者您更倾向于使用其他方式管理软件包,那么可以探索 Anaconda 发行版提供的解决方案。Conda 包管理器自带了大量科学计算相关的预编译库,包括 `gluonnlp` 及其依赖项。只需简单执行如下命令即可轻松安装: ```bash conda install -c conda-forge gluonnlp ``` ### 三级标题:注意事项 确保您的操作系统版本兼容所选的构建工具版本,并且已启用所有必要的系统更新。另外,请注意检查 Python 和 pip 是否为最新稳定版,因为旧版本可能会导致兼容性问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值