processing / processing/libprocessing

Python sketches are not Pythonic

オープン
#230 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
Rust
スター
69
フォーク
14
平均マージ
15日 22時間
マージ済み PR(30日)
3

説明

Sorry for the vague title, feel free to update it.

Using https://github.com/processing/processing-examples-mewnala/blob/6e416d80852ee9e147b70ae93cbf8f6dda871ba3/Basics/Structure/Redraw/Redraw.py as an example, a mewnala sketch structure and common techniques has several issues that makes it less ideal as Python code:

  • Sharing state between frames requires the use of global
  • Even when wildcard import is removed, some variables are injected at the runtime and linters catch them as "undefined":
  • setup function is not meaningful, the same results can be achieved just without. Using setup to initialize variables etc. causes even more undefined issues because how scopes work in Python.

As an alternative, a class based approach can solve all these issues:

from mewnala import (
    Line,
    Sketch as BaseSketch,
)

class Sketch(BaseSketch):
    def __init__(self, width=640, height=480):
        super().__init__()

        self.width = width
        self.height = height

    def setup(self):
        self.size = (self.width, self.height)
        self.stroke = 255
        self.loop = False

        self.y = 180

    def draw(self):
        self.background = 0

        self.y -= 4
        if self.y < 0:
            self.y = self.height

        line = Line(0, self.y, self.width, self.y)
        line.draw()

    def mouse_pressed(self):
        self.redraw()


if __name__ == "__main__":
    sketch = Sketch(width=1920, height=1080)
    sketch.run()

No more magic imports, no more undefined variables, and now the sketch has a Python-native syntax feel much more natural comparing to the previous version.

I removed the setter function calls too, it's a Java-native pattern and feels weird with Python.

We can go one step further and eliminate the setup() What does it do for the sketch can be covered by __init__ in a class context.

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、リンクされている Basics/Structure/Redraw/Redraw.py の例と、参照されている libprocessing issue #150 を読んでください。現在の sketch のライフサイクルと、提案されているクラスベースのアプローチを比較し、どの API 変更がスコープに含まれるのか、また更新された例で完了をどのように示すのかを明確にしてください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
developer-experience
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
38/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。