`

Penrose Tiling in Obfuscated Python

 
阅读更多

Who says you can’t write obfuscated Python?

Here’s a Python script which renders some Penrose tiling. Yes, this is valid Python code:

_                                 =\
                                """if!
                              1:"e,V=100
                            0,(0j-1)**-.2;
                           v,S=.5/  V.real,
                         [(0,0,4      *e,4*e*
                       V)];w=1          -v"def!
                      E(T,A,              B,C):P
                  ,Q,R=B*w+                A*v,B*w+C
            *v,A*w+B*v;retur              n[(1,Q,C,A),(1,P
     ,Q,B),(0,Q,P,A)]*T+[(0,C            ,R,B),(1,R,C,A)]*(1-T)"f
or!i!in!_[:11]:S       =sum([E          (*x)for       !x!in!S],[])"imp
  ort!cair               o!as!O;      s=O.Ima               geSurfac
   e(1,e,e)               ;c=O.Con  text(s);               M,L,G=c.
     move_to                ,c.line_to,c.s                et_sour
       ce_rgb                a"def!z(f,a)                :f(-a.
        imag,a.       real-e-e)"for!T,A,B,C!in[i       !for!i!
          in!S!if!i[""";exec(reduce(lambda x,i:x.replace(chr
           (i),"\n "[34-i:]),   range(   35),_+"""0]]:z(M,A
             );z(L,B);z         (L,C);         c.close_pa
             th()"G             (.4,.3             ,1);c.
             paint(             );G(.7             ,.7,1)
             ;c.fil             l()"fo             r!i!in
             !range             (9):"!             g=1-i/
             8;d=i/          4*g;G(d,d,d,          1-g*.8
             )"!def     !y(f,a):z(f,a+(1+2j)*(     1j**(i
             /2.))*g)"!for!T,A,B,C!in!S:y(M,C);y(L,A);y(M
             ,A);y(L,B)"!c.st            roke()"s.write_t
             o_png('pen                        rose.png')
             """                                       ))

When this program runs, it outputs a 1000×1000 image file to penrose.png, consisting of about 2212 Penrose tiles rendered with a 3D relief effect. Here’s a slice of the image (click to enlarge):

The script requires Pycairo. It only runs on Python <= 2.7; Python 3 is not supported. It started life as a regular Python script, but in my effort to make the code more compact, I got a bit carried away.

Penrose tilings are cool because they cover the entire plane in an aperiodic way — a shifted copy of the image never matches the original. They were invented by Sir Roger Penrose after a series of attempts to tile the plane with pentagonal shapes. For an explanation of the algorithm behind this script, see my next post, Penrose Tiling Explained.

Python never got much credit as an obfuscated programming language, compared to C or Perl. It seems a contest never took place, and there aren’t too many examples of obfuscated Python on the web: You’ll find a few examples in the official Python FAQ and on various pages such as here andhere. There was also a talk at PyCon 2011.

I believe this is the first example of obfuscated Python which outputs a high-resolution image. If you know of any others, let me know in the comments!

  • 大小: 217.2 KB
0
0
分享到:
评论

相关推荐

    Python-penrosePenrose贴画的python生成包

    在这个特定的案例中,"Python-penrosePenrose贴画的python生成包"是一个利用Python编写的库,专门用于生成Penrose贴画。Penrose贴画是由数学家Roger Penrose提出的非周期性瓷砖图案,它展示了不规则的几何形状如何...

    On the Level-2 Condition Number for Moore-Penrose Inversion In Hilbert Space

    希尔伯特空间中Moore-Penrose广义逆的二层条件数,刁怀安,魏益民,条件数理论是数值分析中的一个重要研究课题,它能揭示问题对于输入数据的敏感性。当一个问题的条件数很大时,该问题称为不适定问

    penrose工具以及脚本

    Penrose工具和脚本是为了解决开源项目Penrose在使用和开发过程中遇到的复杂性和学习曲线问题而设计的。Penrose项目是一个高度模块化、功能丰富的软件,它可能涉及图形处理、数学建模或者计算可视化等多个领域。由于...

    15投影矩阵与Moore-Penrose逆[借鉴].pdf

    投影矩阵与Moore-Penrose逆矩阵 投影矩阵是线性代数中一个重要的概念,它是将一个向量投影到一个子空间的变换矩阵。投影矩阵可以被用来解决许多线性代数问题,如最小二乘法、奇异值分解等。 一、投影矩阵的定义和...

    Moore-Penrose广义逆矩阵与线性方程组的解

    Moore-Penrose广义逆矩阵与线性方程组的解

    python+RELM+OS-ELM+FOS-ELM+代码实现-实例验证+算法比较.rar

    极限学习机ELM是一类Single-hidden Layer Feedforward Neural Network(SLFNs)算法,由Huang等基于 Moore-Penrose 广义逆的理论提出,主要针对SLFNs中存在的学习速率慢,迭代时间长,学习参数如学习步长、学习率...

    Penrose:彭罗斯瓷砖渲染

    初始化为: from model import PenroseModelfrom starting_states import dart_starfrom view import load_textures , draw_model#Penrose model takes initial set of tiles as the argument. Look at starting_...

    Python实现矩阵论代码

    广义逆计算: 提供计算矩阵的广义逆的功能,包括Moore-Penrose广义逆、Drazin广义逆等,用于解决矩阵方程组的求解和矩阵的伪逆问题。 实现技术: 编程语言: Python 依赖库: NumPy、SciPy等科学计算库 适用范围: ...

    矩阵加权Moore-Penrose逆及加权最小二乘问题的条件数

    矩阵加权Moore-Penrose逆及加权最小二乘问题的条件数,王淑璠,郑兵,一个问题的解的条件数,是这个问题的解对于该问题数据扰动的敏感性的一个测度, 它是矩阵扰动分析和数值分析中的一个重要课题。�

    论文研究 - Moore-Penrose逆和半线性方程

    假设存在Moore-Penrose逆AT(AAT)-1(A表示A的转置矩阵),只要矩阵AAT的行列式不为零,就为真,并且满足以下关于非线性项的条件。 我们证明了半线性方程对所有问题都有解。 此外,这些解决方案可以从以下定点关系...

    碰撞的Penrose流程

    在本文中,我们研究了在Kerr时空中使用旋转粒子的碰撞Penrose过程(CPP)。 最近的研究表明,两个自旋粒子之间的碰撞会在质心中心产生很高的能量。 在这里,我们显式计算观察者在无限远处测得的能量提取和效率。 ...

    新型递归神经网络求解时变矩阵Moore-Penrose逆.pdf

    1. **Moore-Penrose逆**:Moore-Penrose逆是线性代数中的一个概念,对于任何矩阵A,如果存在矩阵X满足特定的Penrose方程,那么X被称为A的Moore-Penrose逆,记作A+。它是唯一存在的,并且在矩阵A为满秩时,可以视为...

    AdS时空中Penrose不等式的全息论证

    我们提出全息论证来支持AdS Penrose不等式,该不等式推测出视在视线范围内的总质量较低。 这种不平等通常被视为对宇宙审查制度的考验。 我们进一步发现表面视界的面积定律与彭罗斯不等式之间存在联系。 最后,我们...

    Python-PyScatWave用CuPyPyTorch快速散射变换

    3. **非线性操作**:散射变换的核心是非线性的摩尔-彭罗斯逆(Moore-Penrose pseudo-inverse)或绝对值操作。这些操作保证了散射系数的稳定性和不变性。 4. **级联与构建散射树**:通过重复上述过程,形成多层散射...

    论文研究 - 平面空间宇宙学作为Penrose的Weyl曲率假说和引力熵的模型

    FSC被证明是Penrose的Weyl曲率假说及其引力熵概念的出色模型。 FSC的假设允许在宇宙膨胀开始时的最小熵,并严格定义宇宙的时间箭。 这与通货膨胀模型形成鲜明对比,通货膨胀模型似乎违反了早期宇宙中热力学的第二...

    Perturbation and conditions for weighted Moore-Penrose inverse and weighted linear least squares problem

    Perturbation and conditions for weighted Moore-Penrose inverse and weighted linear least squares problem,王淑璠,郑兵,It is well known that the normwise relative condition numbers measure the ...

    pywonderland:使用Python进行数学仙境之旅

    使用Python进行数学仙境之旅 一组python脚本,用于绘制美丽的人物并为数学中有趣... 拼贴,例如Penrose拼贴,Ammann-Beenker拼贴等。 完美随机抽样算法的三部曲。 看 。 。 。 。 。 。 。 例如E8根系统,Mandel

    cut-and-project-tiling:剪切并投影的切片生成器

    众所周知的彭罗斯(Penrose)平铺就是可以通过这种方式生成的许多平铺。 及其工作原理的说明。 查看一小。要求此小程序包括切片生成器的WebAssembly实现。 那篇文章是用Rust编写的,并使用wasm-bindgen和wasm-pack...

    15投影矩阵与Moore-Penrose逆(20210924234023)[借鉴].pdf

    15投影矩阵与Moore-Penrose逆(20210924234023)[借鉴].pdf

    全对称矩阵的满秩分解及Moore-Penrose逆 (2009年)

    给出全对称矩阵中具有轴对称结构矩阵(延拓矩阵)的满秩分解及Moore-Penrose逆与原矩阵的满秩分解及Moore-Penrose逆的定量关系,从而可节省这类具有该对称结构矩阵的满秩分解及Moore-Pen- rose逆的计算量和存储量。

Global site tag (gtag.js) - Google Analytics