python散装笔记——151: 非官方Python实现

liftword1周前 (05-14)技术文章9

1: IronPython

  • IronPython是一个开源的Python实现,专为.NET和Mono设计,使用C#编写,采用Apache License 2.0授权。它依赖于DLR(动态语言运行时)。目前仅支持Python 2.7版本,Python 3版本正在开发中。
  • 与CPython的差异:
    • 与.NET Framework紧密集成。
    • 默认情况下,字符串是Unicode。
    • 不支持用C语言为CPython编写的扩展。
    • 不受全局解释器锁(GIL)的限制。
    • 性能通常较低,但具体取决于测试。

Hello World

 print "Hello World!"

你也可以使用.NET函数:

 import clr
 
 from System import Console
 
 Console.WriteLine("Hello World!")

外部链接

  • 官方网站
  • GitHub仓库

2: Jython

Jython是一个开源的Python实现,专为JVM设计,使用Java编写,采用Python软件基金会许可。目前仅支持Python 2.7版本,Python 3版本正在开发中。

与CPython的差异:

  • 与JVM紧密集成。
  • 字符串是Unicode。
  • 不支持用C语言为CPython编写的扩展。
  • 不受全局解释器锁(GIL)的限制。
  • 性能通常较低,但具体取决于测试。

Hello World

 print "Hello World!"

你也可以使用Java函数:

 from java.lang import System
 System.out.println("Hello World!")

外部链接

  • 官方网站
  • Mercurial仓库

3: Transcrypt

Transcrypt是一个工具,可以将Python的一个相当广泛的子集预编译为紧凑且可读的JavaScript。它具有以下特点:

  • 使用纯Python语法进行经典面向对象编程,支持多重继承,由CPython的原生解析器解析。
  • 与高质量的Web导向JavaScript库无缝集成,而不是桌面导向的Python库。
  • 基于层次结构的URL模块系统,允许通过PyPi分发模块。
  • Python源代码与生成的JavaScript代码之间有简单的对应关系,便于调试。
  • 多级源码映射和可选的目标代码注释,包含源代码引用。
  • 紧凑的下载包,以KB为单位,而不是MB。
  • 优化的JavaScript代码,使用记忆化(调用缓存)可选地绕过原型查找链。
  • 可以局部开启或关闭运算符重载,便于进行可读的数值计算。

代码大小和速度

经验表明,650KB的Python源代码大致转换为相同量的JavaScript源代码。速度与手写的JavaScript相当,并且如果开启调用记忆化,速度可以超过手写的JavaScript。

与HTML集成

<script src="__javascript__/hello.js"></script>
<h2>Hello demo</h2>

<p>
<div id = "greet">...</div>
<button onclick="hello.solarSystem.greet ()">Click me repeatedly!</button>

<p>
<div id = "explain">...</div>
<button onclick="hello.solarSystem.explain ()">And click me repeatedly too!</button>

与JavaScript和DOM集成

 from itertools import chain
 
 class SolarSystem:
   planets = [list (chain (planet, (index + 1,))) for index, planet in enumerate ((
     ('Mercury', 'hot', 2240),
     ('Venus', 'sulphurous', 6052),
     ('Earth', 'fertile', 6378),
     ('Mars', 'reddish', 3397),
     ('Jupiter', 'stormy', 71492),
     ('Saturn', 'ringed', 60268),
     ('Uranus', 'cold', 25559),
     ('Neptune', 'very cold', 24766)
   ))]
   
   lines = (
     '{} is a {} planet',
     'The radius of {} is {} km',
     '{} is planet nr. {} counting from the sun'
   )
   
   def __init__ (self):
     self.lineIndex = 0
 
   def greet (self):
     self.planet = self.planets [int (Math.random () * len (self.planets))]
     document.getElementById ('greet') .innerHTML = 'Hello {}'.format (self.planet [0])
     self.explain ()
 
   def explain (self):
     document.getElementById ('explain').innerHTML = (
       self.lines [self.lineIndex] .format (self.planet [0], self.planet [self.lineIndex + 1])
     )
 
     self.lineIndex = (self.lineIndex + 1) % 3
     solarSystem = SolarSystem ()

与其他JavaScript库集成

Transcrypt可以与任何JavaScript库结合使用,无需采取特殊措施或语法。文档中给出了与React.js、Riot.js、Fabric.js和Node.js等库结合使用的示例。

Python与JavaScript代码的关系

Python代码:

 class A:
   def __init__ (self, x):
     self.x = x
 
     def show (self, label):
       print ('A.show', label, self.x)
 
 class B:
   def __init__ (self, y):
     alert ('In B constructor')
     self.y = y
 
   def show (self, label):
     print ('B.show', label, self.y)
 
 class C (A, B):
   def __init__ (self, x, y):
     alert ('In C constructor')
     A.__init__ (self, x)
     B.__init__ (self, y)
     self.show ('constructor')
 
   def show (self, label):
     B.show (self, label)
     print ('C.show', label, self.x, self.y)
 
 a = A (1001)
 a.show ('america')
 
 b = B (2002)
 b.show ('russia')
 
 c = C (3003, 4004)
 c.show ('netherlands')
 
 show2 = c.show
 show2 ('copy')

JavaScript代码:

 var A = __class__ ('A', [object], {
   get __init__ () {return __get__ (this, function (self, x) {
     self.x = x;
   });},
   get show () {return __get__ (this, function (self, label) {
     print ('A.show', label, self.x);
   });}
 });
 
 var B = __class__ ('B', [object], {
   get __init__ () {return __get__ (this, function (self, y) {
     alert ('In B constructor');
     self.y = y;
   });},
   get show () {return __get__ (this, function (self, label) {
     print ('B.show', label, self.y);
   });}
 });
 
 var C = __class__ ('C', [A, B], {
   get __init__ () {return __get__ (this, function (self, x, y) {
     alert ('In C constructor');
     A.__init__ (self, x);
     B.__init__ (self, y);
     self.show ('constructor');
   });},
   get show () {return __get__ (this, function (self, label) {
     B.show (self, label);
     print ('C.show', label, self.x, self.y);
   });}
 });
 var a = A (1001);
 a.show ('america');
 var b = B (2002);
 b.show ('russia');
 var c = C (3003, 4004);
 c.show ('netherlands');
 var show2 = c.show;
 show2 ('copy');

外部链接

  • 官方网站
  • GitHub仓库

相关文章

代码示例:Python 调用并执行 JS

经常做爬虫需要扣JS的老铁一定很熟悉这段操作,通过execjs调用并执行 JS 对一些加解密的处理非常有用。在此记录一下相互学习交流。以下是具体步骤:1、安装 execjspip install py...

如何使用 Scrapy 执行 JavaScript

大多数现代网站都使用客户端 JavaScript 框架,例如 React、Vue 或 Angular。在没有服务器端渲染的情况下从动态网站抓取数据通常需要执行 JavaScript 代码。我已经抓取了...

Python爬虫高级之JS渗透登录新浪微博 | 知了独家研究

小伙伴们看到标题可能会想,我能直接自己登陆把登陆后的cookie复制下来加到自定义的请求头里面不香嘛,为什么非要用python模拟登录的过程?如果我们是长期爬取数据,比如每天早上中午和晚上定时爬取新浪...

Python、JavaScript和Rust的Web性能比较

Python使用FastApi测试;Node.JS使用Fastify;Rust则使用Actix。选择的Python和Node框架,是在搜索 "最快的<某语言>api "时得...

在pyside的QWebEngineView中和javascript通信

在pyside2中可以使用QtWebChannel 使QWebEngineView和前端页面的javasccript通信代码:from PySide2.QtWebChannel import QWeb...

网页数据如何获取,带你走近JS逆向,完全入门级!

在这个大数据时代,我们眼睛所看到的百分之九十的数据都是通过页面呈现出现的,不论是PC端、网页端还是移动端,数据渲染还是基于html/h5+javascript进行的,而大多数的数据都是通过请求后台接口...