diff --git a/.gitignore b/.gitignore index be374a06..38fd6020 100644 --- a/.gitignore +++ b/.gitignore @@ -30,4 +30,7 @@ dist *.pyc **/.DS_Store .mypy_cache -.ipynb_checkpoints \ No newline at end of file +.ipynb_checkpoints +*.whl +*.dylib +*.so diff --git a/README.md b/README.md index ad7d8d7f..83617ed2 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,10 @@ 介绍python在一些比较普遍通用领域,如时间处理,数据读写,信息安全,数据处理,简单运维,简单人机交互方面的应用 ++ 扩展篇 + + 介绍在python中使用其他语言的工具 + 目前更新是在2023年的4月4日,现在更新好后内容上来说对之前的有所删减,希望在未来3年中可以填完这个大坑 @@ -146,4 +150,7 @@ ## 更新信息 -+ 2023-03-31 开始重新整理针对python 3.10版本的python攻略 \ No newline at end of file ++ 2023-03-31 开始重新整理针对python 3.10版本的python攻略 ++ 2023-10-20 技巧篇新增使用第三方工具提升运行时性能的3篇相关文章 ++ 2023-10-22 新增底层篇 ++ 2023-10-23 新增扩展篇 \ No newline at end of file diff --git a/_sidebar.md b/_sidebar.md index 8356d2b8..abcd7c1a 100644 --- a/_sidebar.md +++ b/_sidebar.md @@ -98,8 +98,6 @@ * [结语](语法篇/结语.md) -* [底层篇](底层篇/README.md) - * [Cpython的虚拟机和解释器](底层篇/Cpython的虚拟机和解释器.md) * [技巧篇](技巧篇/README.md) * [设计模式实现](技巧篇/设计模式实现/README.md) @@ -161,4 +159,9 @@ * [调用子进程](基础应用篇/调用子进程.md) * [结语](输入输出篇/结语.md) +* [*底层篇](底层篇/README.md) + * [Cpython的虚拟机和解释器](底层篇/Cpython的虚拟机和解释器.md) +* [*扩展篇](扩展篇/README.md) + * [Javascript扩展](扩展篇/Javascript扩展.md) + * [Fortain扩展](扩展篇/Fortain扩展.md) \ No newline at end of file diff --git "a/\346\211\251\345\261\225\347\257\207/Fortain\346\211\251\345\261\225.md" "b/\346\211\251\345\261\225\347\257\207/Fortain\346\211\251\345\261\225.md" new file mode 100644 index 00000000..5463a664 --- /dev/null +++ "b/\346\211\251\345\261\225\347\257\207/Fortain\346\211\251\345\261\225.md" @@ -0,0 +1,205 @@ +# Fortain扩展 + + +我们可以简单的使用Fortain扩展python要感谢`numpy`.`numpy`许多算法实现是使用`Fortran`,`f2py`就是`numpy`中用于将`Fortran`代码编译为python可以使用的动态链接库的工具. + +## 是否有必要用Fortran写扩展 + +这个要看应用的领域,Fortran是专为计算设计的编程语言,多维数组是内置的数据类型,可以说在向量计算矩阵计算上它有天然的优势,而且语法相比C语言更加接近python,也没有复杂的语法和底层概念,用它写扩展可以专注于算法.因此如果一个模块完全是做计算的,那它就很适合用这门语言写扩展. + +但同时它的劣势也就在于太过小众,如果一个人已经回C/C++,甚至不用是精通,他往往也没有动力再学一门新语言.同时Fortran除了写计算逻辑优秀以外几乎可以说一无是处.也没什么现代的编程技巧,因此除非是用作写算法模块,否则确实不值得专门学个这. + +那为啥还要写这一节? + +因为科学计算是python最重要的应用领域,几乎没有之一;数学家,物理学家往往都不关心怎么编程,只关心算法本身.如果是年级大点的数学家,物理学家,几乎都学过Fortran,而年轻的可能没学过,但Fortran好上手的特性非常适合作为第一门高性能计算编程语言.因此这个需求和这个用户群体非常契合.这就值得介绍一下了. + +## 简单fortain语法 + +相对比价现代,支持面又比较广的是fortran95标准.其基本特点是: + ++ 完整的结构化和模块化 ++ 矩阵运算支持 ++ 简单的子程序接口,方便传递矩阵 ++ 功能强大而简单的Namelist输入输出 ++ 对并行计算提供特别支持 ++ 编译代码执行效率高 ++ 内置函数较少 + +### 基本规则 + ++ 大小写不敏感 ++ 自由格式 ++ `&`为换行连写 ++ `!`为注释语句 ++ 程序中任何地方用`stop`可推出程序 ++ 建议每个域(program、module、function、 subrouting)第一行都写`implicit none` + + +## 使用f2py编译fortran的pyton扩展 + +此处给出一个简单的求平方和的例子用于演示`demo.f95`: + +```fortran +subroutine sum_of_square(x, y, n) + implicit none + integer, intent(in) :: n + integer :: i + real(kind=8), intent(in) :: x(n) + real(kind=8), intent(out) :: y + y = 0 + do i=1, n + y = y + x(i)**2 + end do +end +``` + +使用命令`f2py -c -m file_path`就可以很简单的将其编译为python模块 + + +```python +!f2py -c -m demo demo.f95 --quiet +``` + + /Users/mac/micromamba/envs/py3.10/lib/python3.10/site-packages/setuptools/_distutils/cmd.py:66: SetuptoolsDeprecationWarning: setup.py install is deprecated. + !! + + ******************************************************************************** + Please avoid running ``setup.py`` directly. + Instead, use pypa/build, pypa/installer, pypa/build or + other standards-based tools. + + See https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html for details. + ******************************************************************************** + + !! + self.initialize_options() + + + +```python +from demo import sum_of_square +``` + + +```python +sum_of_square([1,2,3,4]) +``` + + + + + 30.0 + + + +## 在python解释器中调用编译工具 + +如果有需要在python环境中编译Fortran代码,那需要使用numpy中的`f2py`子模块 + +编译Fortran代码需要使用`f2py.compile(source, modulename='untitled', extra_args='', verbose=1, source_fn=None,extension=".f")`方法, + + +```python +from numpy import f2py +``` + + +```python +with open("demo.f95","rb") as f: + source = f.read() +``` + + +```python +f2py.compile(source, modulename='demo1',extension='.f95') +``` + + running build + running config_cc + INFO: unifing config_cc, config, build_clib, build_ext, build commands --compiler options + running config_fc + INFO: unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options + running build_src + INFO: build_src + INFO: building extension "demo1" sources + INFO: f2py options: [] + INFO: f2py:> /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10/demo1module.c + creating /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10 + Reading fortran codes... + Reading file '/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmp9w3ykm9l.f95' (format:free) + Post-processing... + Block: demo1 + Block: sum_of_square + Applying post-processing hooks... + character_backward_compatibility_hook + Post-processing (stage 2)... + Building modules... + Building module "demo1"... + Generating possibly empty wrappers" + Maybe empty "demo1-f2pywrappers.f" + Constructing wrapper function "sum_of_square"... + y = sum_of_square(x,[n]) + Wrote C/API module "demo1" to file "/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10/demo1module.c" + INFO: adding '/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10/fortranobject.c' to sources. + INFO: adding '/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10' to include_dirs. + copying /Users/mac/micromamba/envs/py3.10/lib/python3.10/site-packages/numpy/f2py/src/fortranobject.c -> /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10 + copying /Users/mac/micromamba/envs/py3.10/lib/python3.10/site-packages/numpy/f2py/src/fortranobject.h -> /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10 + INFO: adding '/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10/demo1-f2pywrappers.f' to sources. + INFO: build_src: building npy-pkg config files + running build_ext + INFO: customize UnixCCompiler + INFO: customize UnixCCompiler using build_ext + INFO: get_default_fcompiler: matching types: '['gnu95', 'nag', 'nagfor', 'absoft', 'ibm', 'intel', 'gnu', 'g95', 'pg']' + INFO: customize Gnu95FCompiler + INFO: Found executable /usr/local/bin/gfortran + INFO: customize Gnu95FCompiler + INFO: customize Gnu95FCompiler using build_ext + INFO: building 'demo1' extension + INFO: compiling C sources + INFO: C compiler: clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /Users/mac/micromamba/envs/py3.10/include -fPIC -O2 -isystem /Users/mac/micromamba/envs/py3.10/include -ftrapping-math + + creating /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/var + creating /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/var/folders + creating /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/var/folders/j_ + creating /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn + creating /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T + creating /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc + creating /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10 + INFO: compile options: '-DNPY_DISABLE_OPTIMIZATION=1 -I/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10 -I/Users/mac/micromamba/envs/py3.10/lib/python3.10/site-packages/numpy/core/include -I/Users/mac/micromamba/envs/py3.10/include/python3.10 -c' + INFO: clang: /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10/fortranobject.c + INFO: clang: /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10/demo1module.c + INFO: compiling Fortran sources + INFO: Fortran f77 compiler: /usr/local/bin/gfortran -Wall -g -ffixed-form -fno-second-underscore -fPIC -O3 -funroll-loops + Fortran f90 compiler: /usr/local/bin/gfortran -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops + Fortran fix compiler: /usr/local/bin/gfortran -Wall -g -ffixed-form -fno-second-underscore -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops + INFO: compile options: '-I/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10 -I/Users/mac/micromamba/envs/py3.10/lib/python3.10/site-packages/numpy/core/include -I/Users/mac/micromamba/envs/py3.10/include/python3.10 -c' + INFO: gfortran:f90: /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmp9w3ykm9l.f95 + INFO: gfortran:f77: /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10/demo1-f2pywrappers.f + INFO: /usr/local/bin/gfortran -Wall -g -Wall -g -undefined dynamic_lookup -bundle /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10/demo1module.o /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10/fortranobject.o /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmp9w3ykm9l.o /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10/demo1-f2pywrappers.o -L/usr/local/Cellar/gcc/13.1.0/bin/../lib/gcc/current/gcc/x86_64-apple-darwin20/13 -L/usr/local/Cellar/gcc/13.1.0/bin/../lib/gcc/current/gcc/x86_64-apple-darwin20/13/../../.. -L/usr/local/Cellar/gcc/13.1.0/bin/../lib/gcc/current/gcc/x86_64-apple-darwin20/13/../../.. -lgfortran -o ./demo1.cpython-310-darwin.so + Removing build directory /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc + + + + + + + 0 + + + + +```python +from demo1 import sum_of_square +``` + + +```python +sum_of_square([1,2,3,4]) +``` + + + + + 30.0 + + diff --git "a/\346\211\251\345\261\225\347\257\207/Fortain\346\211\251\345\261\225/Fortain\346\211\251\345\261\225.ipynb" "b/\346\211\251\345\261\225\347\257\207/Fortain\346\211\251\345\261\225/Fortain\346\211\251\345\261\225.ipynb" new file mode 100644 index 00000000..b11c7671 --- /dev/null +++ "b/\346\211\251\345\261\225\347\257\207/Fortain\346\211\251\345\261\225/Fortain\346\211\251\345\261\225.ipynb" @@ -0,0 +1,324 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Fortain扩展\n", + "\n", + "\n", + "我们可以简单的使用Fortain扩展python要感谢`numpy`.`numpy`许多算法实现是使用`Fortran`,`f2py`就是`numpy`中用于将`Fortran`代码编译为python可以使用的动态链接库的工具." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 是否有必要用Fortran写扩展\n", + "\n", + "这个要看应用的领域,Fortran是专为计算设计的编程语言,多维数组是内置的数据类型,可以说在向量计算矩阵计算上它有天然的优势,而且语法相比C语言更加接近python,也没有复杂的语法和底层概念,用它写扩展可以专注于算法.因此如果一个模块完全是做计算的,那它就很适合用这门语言写扩展.\n", + "\n", + "但同时它的劣势也就在于太过小众,如果一个人已经回C/C++,甚至不用是精通,他往往也没有动力再学一门新语言.同时Fortran除了写计算逻辑优秀以外几乎可以说一无是处.也没什么现代的编程技巧,因此除非是用作写算法模块,否则确实不值得专门学个这.\n", + "\n", + "那为啥还要写这一节?\n", + "\n", + "因为科学计算是python最重要的应用领域,几乎没有之一;数学家,物理学家往往都不关心怎么编程,只关心算法本身.如果是年级大点的数学家,物理学家,几乎都学过Fortran,而年轻的可能没学过,但Fortran好上手的特性非常适合作为第一门高性能计算编程语言.因此这个需求和这个用户群体非常契合.这就值得介绍一下了." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 简单fortain语法\n", + "\n", + "相对比价现代,支持面又比较广的是fortran95标准.其基本特点是:\n", + "\n", + "+ 完整的结构化和模块化\n", + "+ 矩阵运算支持\n", + "+ 简单的子程序接口,方便传递矩阵 \n", + "+ 功能强大而简单的Namelist输入输出 \n", + "+ 对并行计算提供特别支持\n", + "+ 编译代码执行效率高\n", + "+ 内置函数较少\n", + "\n", + "### 基本规则\n", + "\n", + "+ 大小写不敏感\n", + "+ 自由格式\n", + "+ `&`为换行连写\n", + "+ `!`为注释语句\n", + "+ 程序中任何地方用`stop`可推出程序\n", + "+ 建议每个域(program、module、function、 subrouting)第一行都写`implicit none`\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 使用f2py编译fortran的pyton扩展\n", + "\n", + "此处给出一个简单的求平方和的例子用于演示`demo.f95`:\n", + "\n", + "```fortran\n", + "subroutine sum_of_square(x, y, n) \n", + " implicit none\n", + " integer, intent(in) :: n \n", + " integer :: i\n", + " real(kind=8), intent(in) :: x(n) \n", + " real(kind=8), intent(out) :: y \n", + " y = 0\n", + " do i=1, n \n", + " y = y + x(i)**2\n", + " end do \n", + "end\n", + "```\n", + "\n", + "使用命令`f2py -c -m file_path`就可以很简单的将其编译为python模块" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "/Users/mac/micromamba/envs/py3.10/lib/python3.10/site-packages/setuptools/_distutils/cmd.py:66: SetuptoolsDeprecationWarning: setup.py install is deprecated.\n", + "!!\n", + "\n", + " ********************************************************************************\n", + " Please avoid running ``setup.py`` directly.\n", + " Instead, use pypa/build, pypa/installer, pypa/build or\n", + " other standards-based tools.\n", + "\n", + " See https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html for details.\n", + " ********************************************************************************\n", + "\n", + "!!\n", + " self.initialize_options()\n" + ] + } + ], + "source": [ + "!f2py -c -m demo demo.f95 --quiet" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "from demo import sum_of_square" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "30.0" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sum_of_square([1,2,3,4])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 在python解释器中调用编译工具\n", + "\n", + "如果有需要在python环境中编译Fortran代码,那需要使用numpy中的`f2py`子模块\n", + "\n", + "编译Fortran代码需要使用`f2py.compile(source, modulename='untitled', extra_args='', verbose=1, source_fn=None,extension=\".f\")`方法," + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "from numpy import f2py" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "with open(\"demo.f95\",\"rb\") as f:\n", + " source = f.read()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "running build\n", + "running config_cc\n", + "INFO: unifing config_cc, config, build_clib, build_ext, build commands --compiler options\n", + "running config_fc\n", + "INFO: unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options\n", + "running build_src\n", + "INFO: build_src\n", + "INFO: building extension \"demo1\" sources\n", + "INFO: f2py options: []\n", + "INFO: f2py:> /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10/demo1module.c\n", + "creating /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10\n", + "Reading fortran codes...\n", + "\tReading file '/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmp9w3ykm9l.f95' (format:free)\n", + "Post-processing...\n", + "\tBlock: demo1\n", + "\t\t\tBlock: sum_of_square\n", + "Applying post-processing hooks...\n", + " character_backward_compatibility_hook\n", + "Post-processing (stage 2)...\n", + "Building modules...\n", + " Building module \"demo1\"...\n", + " Generating possibly empty wrappers\"\n", + " Maybe empty \"demo1-f2pywrappers.f\"\n", + " Constructing wrapper function \"sum_of_square\"...\n", + " y = sum_of_square(x,[n])\n", + " Wrote C/API module \"demo1\" to file \"/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10/demo1module.c\"\n", + "INFO: adding '/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10/fortranobject.c' to sources.\n", + "INFO: adding '/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10' to include_dirs.\n", + "copying /Users/mac/micromamba/envs/py3.10/lib/python3.10/site-packages/numpy/f2py/src/fortranobject.c -> /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10\n", + "copying /Users/mac/micromamba/envs/py3.10/lib/python3.10/site-packages/numpy/f2py/src/fortranobject.h -> /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10\n", + "INFO: adding '/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10/demo1-f2pywrappers.f' to sources.\n", + "INFO: build_src: building npy-pkg config files\n", + "running build_ext\n", + "INFO: customize UnixCCompiler\n", + "INFO: customize UnixCCompiler using build_ext\n", + "INFO: get_default_fcompiler: matching types: '['gnu95', 'nag', 'nagfor', 'absoft', 'ibm', 'intel', 'gnu', 'g95', 'pg']'\n", + "INFO: customize Gnu95FCompiler\n", + "INFO: Found executable /usr/local/bin/gfortran\n", + "INFO: customize Gnu95FCompiler\n", + "INFO: customize Gnu95FCompiler using build_ext\n", + "INFO: building 'demo1' extension\n", + "INFO: compiling C sources\n", + "INFO: C compiler: clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /Users/mac/micromamba/envs/py3.10/include -fPIC -O2 -isystem /Users/mac/micromamba/envs/py3.10/include -ftrapping-math\n", + "\n", + "creating /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/var\n", + "creating /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/var/folders\n", + "creating /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/var/folders/j_\n", + "creating /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn\n", + "creating /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T\n", + "creating /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc\n", + "creating /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10\n", + "INFO: compile options: '-DNPY_DISABLE_OPTIMIZATION=1 -I/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10 -I/Users/mac/micromamba/envs/py3.10/lib/python3.10/site-packages/numpy/core/include -I/Users/mac/micromamba/envs/py3.10/include/python3.10 -c'\n", + "INFO: clang: /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10/fortranobject.c\n", + "INFO: clang: /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10/demo1module.c\n", + "INFO: compiling Fortran sources\n", + "INFO: Fortran f77 compiler: /usr/local/bin/gfortran -Wall -g -ffixed-form -fno-second-underscore -fPIC -O3 -funroll-loops\n", + "Fortran f90 compiler: /usr/local/bin/gfortran -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops\n", + "Fortran fix compiler: /usr/local/bin/gfortran -Wall -g -ffixed-form -fno-second-underscore -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops\n", + "INFO: compile options: '-I/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10 -I/Users/mac/micromamba/envs/py3.10/lib/python3.10/site-packages/numpy/core/include -I/Users/mac/micromamba/envs/py3.10/include/python3.10 -c'\n", + "INFO: gfortran:f90: /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmp9w3ykm9l.f95\n", + "INFO: gfortran:f77: /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10/demo1-f2pywrappers.f\n", + "INFO: /usr/local/bin/gfortran -Wall -g -Wall -g -undefined dynamic_lookup -bundle /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10/demo1module.o /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10/fortranobject.o /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmp9w3ykm9l.o /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc/src.macosx-10.9-x86_64-3.10/demo1-f2pywrappers.o -L/usr/local/Cellar/gcc/13.1.0/bin/../lib/gcc/current/gcc/x86_64-apple-darwin20/13 -L/usr/local/Cellar/gcc/13.1.0/bin/../lib/gcc/current/gcc/x86_64-apple-darwin20/13/../../.. -L/usr/local/Cellar/gcc/13.1.0/bin/../lib/gcc/current/gcc/x86_64-apple-darwin20/13/../../.. -lgfortran -o ./demo1.cpython-310-darwin.so\n", + "Removing build directory /var/folders/j_/p0q1k_mj4cs0dqn0sqnshsrr0000gn/T/tmpb8to7mdc\n", + "\n" + ] + }, + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f2py.compile(source, modulename='demo1',extension='.f95')" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "from demo1 import sum_of_square" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "30.0" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sum_of_square([1,2,3,4])" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.11" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git "a/\346\211\251\345\261\225\347\257\207/Fortain\346\211\251\345\261\225/demo.f95" "b/\346\211\251\345\261\225\347\257\207/Fortain\346\211\251\345\261\225/demo.f95" new file mode 100644 index 00000000..7d599976 --- /dev/null +++ "b/\346\211\251\345\261\225\347\257\207/Fortain\346\211\251\345\261\225/demo.f95" @@ -0,0 +1,11 @@ +subroutine sum_of_square(x, y, n) + implicit none + integer, intent(in) :: n + integer :: i + real(kind=8), intent(in) :: x(n) + real(kind=8), intent(out) :: y + y = 0 + do i=1, n + y = y + x(i)**2 + end do +end \ No newline at end of file diff --git "a/\346\211\251\345\261\225\347\257\207/Javascript\346\211\251\345\261\225.md" "b/\346\211\251\345\261\225\347\257\207/Javascript\346\211\251\345\261\225.md" new file mode 100644 index 00000000..a22e9fbf --- /dev/null +++ "b/\346\211\251\345\261\225\347\257\207/Javascript\346\211\251\345\261\225.md" @@ -0,0 +1,56 @@ +# Javascript扩展 + +在python中要调用js可以使用第三方库[PyMiniRacer](https://github.com/sqreen/PyMiniRacer),使用`pip install py-mini-racer`安装. + +由于js本身也是个解释型脚本语言,py-mini-racer干的事其实就是把v8引擎嵌入了python中,我们可以直接使用`MiniRacer`类的实例方法`eval`,`call`以及`execute`来执行js代码文本.无论哪种方式运行过程都保存在实例中. + + +```python +from py_mini_racer import py_mini_racer +ctx = py_mini_racer.MiniRacer() +``` + +> 执行代码 + +使用`eval`接口就可以执行代码,代码在`MiniRacer`对象中执行,其中定义变量包括函数我们并不能在python中获得,通常两个用途: + ++ 执行表达式,表达式的值会被python获取 ++ 执行函数,类,模块,变量等的申明,然后使用`call`接口调用函数获取结果 + + +```python +ctx.eval("1+1") +``` + + + + + 2 + + + +> 调用函数 + + +```python +ctx.eval("const fun = () => ({ foo: 1 })") +``` + + +```python +ctx.call("fun") +``` + + + + + {'foo': 1} + + + +## PyMiniRacer的安装 + +这个项目已经很久没人维护了,目前官方只支持x86_64平台上windows,macos和linux环境,如果你希望在arm64平台使用可以参考下面的方法: + ++ linux:可以直接下载[网友编译好的wheel包](https://blog.csdn.net/KongDong/article/details/130182040),这个包本项目下也有备份 ++ macos:直接安装`py-mini-racer`,然后将[libmini_racer.dylib](https://jfds-1252952517.cos.ap-chengdu.myqcloud.com/akshare/software/pyminiracer/libmini_racer.dylib)方到安装好的`py-mini-racer`项目根目录下.这个动态链接库本项目也有备份 diff --git "a/\346\211\251\345\261\225\347\257\207/Javascript\346\211\251\345\261\225/Javascript\346\211\251\345\261\225.ipynb" "b/\346\211\251\345\261\225\347\257\207/Javascript\346\211\251\345\261\225/Javascript\346\211\251\345\261\225.ipynb" new file mode 100644 index 00000000..0766fee6 --- /dev/null +++ "b/\346\211\251\345\261\225\347\257\207/Javascript\346\211\251\345\261\225/Javascript\346\211\251\345\261\225.ipynb" @@ -0,0 +1,144 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "375f7503-0d45-483a-9da8-ed367998cc65", + "metadata": {}, + "source": [ + "# Javascript扩展\n", + "\n", + "在python中要调用js可以使用第三方库[PyMiniRacer](https://github.com/sqreen/PyMiniRacer),使用`pip install py-mini-racer`安装.\n", + "\n", + "由于js本身也是个解释型脚本语言,py-mini-racer干的事其实就是把v8引擎嵌入了python中,我们可以直接使用`MiniRacer`类的实例方法`eval`,`call`以及`execute`来执行js代码文本.无论哪种方式运行过程都保存在实例中." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "6c911313-9916-418a-bd6a-ab093f6753e1", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "from py_mini_racer import py_mini_racer\n", + "ctx = py_mini_racer.MiniRacer()" + ] + }, + { + "cell_type": "markdown", + "id": "984c4717-aa97-4faf-84e9-7d1e65599e3b", + "metadata": {}, + "source": [ + "> 执行代码\n", + "\n", + "使用`eval`接口就可以执行代码,代码在`MiniRacer`对象中执行,其中定义变量包括函数我们并不能在python中获得,通常两个用途:\n", + "\n", + "+ 执行表达式,表达式的值会被python获取\n", + "+ 执行函数,类,模块,变量等的申明,然后使用`call`接口调用函数获取结果" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "3d3aa7ce-391d-4174-8a1c-c59798d3ed7c", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ctx.eval(\"1+1\")" + ] + }, + { + "cell_type": "markdown", + "id": "03936c38-63f5-489a-a2cf-607151d6746d", + "metadata": { + "tags": [] + }, + "source": [ + "> 调用函数" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "90fc4e9e-4cb6-45c2-8826-21a193612717", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "ctx.eval(\"const fun = () => ({ foo: 1 })\")" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "fb35754d-e419-4dc4-8d60-9bed75198d3a", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'foo': 1}" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ctx.call(\"fun\")" + ] + }, + { + "cell_type": "markdown", + "id": "360d9daf-71a8-4697-b2f7-ebe459c7e957", + "metadata": {}, + "source": [ + "## PyMiniRacer的安装\n", + "\n", + "这个项目已经很久没人维护了,目前官方只支持x86_64平台上windows,macos和linux环境,如果你希望在arm64平台使用可以参考下面的方法:\n", + "\n", + "+ linux:可以直接下载[网友编译好的wheel包](https://blog.csdn.net/KongDong/article/details/130182040),这个包本项目下也有备份\n", + "+ macos:直接安装`py-mini-racer`,然后将[libmini_racer.dylib](https://jfds-1252952517.cos.ap-chengdu.myqcloud.com/akshare/software/pyminiracer/libmini_racer.dylib)方到安装好的`py-mini-racer`项目根目录下.这个动态链接库本项目也有备份" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.11" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git "a/\346\211\251\345\261\225\347\257\207/README.md" "b/\346\211\251\345\261\225\347\257\207/README.md" new file mode 100644 index 00000000..506cb5a6 --- /dev/null +++ "b/\346\211\251\345\261\225\347\257\207/README.md" @@ -0,0 +1,9 @@ +# 扩展篇 + +在python语境下所谓`扩展(Extending)`指的是在python解释器里使用其他语言的技术. + +我们知道编程语言分为编译型和解释型两种,不同类型的语言在python中进行扩展的形式也是不一样的 + ++ 编译型的需要先将对应语言的源码编译为python可以导入的动态链接库,然后直接导入python运行时 ++ 解释型,导入目标语言的运行时,将代码脚本文本或文件通过python的运行时导入目标语言的运行运行,再将结果包装为python对象返回 +