processing python模式_在纯python中实现processing.org的处理(在jython上运行processing.py)...

ppython

Implementation of processing.org's processing in pure python (processing.py runs on jython, this one runs on pure python). No dependency, no import and no run().

Info

Release name: omri

Stability: alpha, highly volatile, more to be completed

Running

cd ppython/ppython

python ppython.py /file.py

History

Had this project lying around. Removed it from the fossils graveyard when Dr. Omri Har-Shemesh came to our Python meetup. I was inspired by his high physics background, I was reminded of this in my cupboard and decided to open it to the world.

Please don't see the source code (ppython.py) else you'll chase me out of the Python kingdom.

Why a pure python version?

To simply take advantage of the huge number of machine learning, data science and normal packages available.

How is this different from p5py?

no dependency

requires only tkinter

no imports

you only run your code as it should. no from x import *

no run()

no need to call run() at the end

Dependencies

>>> dependencies is None

True

Absolutely no dependency.

Gallery

Here are some snippets:

Sound Bars

python ppython.py snippets/soundbars.py

ppython_soundbars.gif

code:

def setup():

pass

def draw():

background(255, 255, 255)

for i in range(20):

ry = random(2, 10)*10

fill(i*10, 100, 20)

rect(10 + i*10, 150-ry, 10, ry)

fill(255, 0, 0)

Microsoft Logo

python ppython.py snippets/winlogo.py

ppython_winlogo.jpg

code:

def setup():

size = 100

noStroke()

fill(245, 80, 35)

rect(10, 10, size, size)

fill(125, 185, 0)

rect(120, 10, size, size)

fill(0, 160, 240)

rect(10, 120, size, size)

fill(255, 185, 0)

rect(120, 120, size, size)

def draw():

pass

Paint

python ppython.py snippets/paint.py

ppython_paint.gif

code:

def setup():

pass

def draw():

fill(255, 0, 0)

ellipse(mouseX, mouseY, 20, 20)

Line Graph

python ppython.py snippets/line_graph.py

ppython_linegraph.jpg

code:

coords = [

(20, 30),

(50, 35),

(80, 40),

(110, 50),

(130, 70),

(150, 90),

(180, 110),

(210, 120),

(240, 150),

(270, 200)

]

def setup():

global coords

noStroke()

for coord in coords:

x = coord[0]; y = coord[1]

fill(255, 175, 150)

ellipse(x-5, y-5, 10, 10)

stroke(255, 175, 150)

strokeSize(3)

for i, coord in enumerate(coords):

if i+1 < len(coords):

x = coord[0]; y = coord[1]

x2 = coords[i+1][0]; y2 = coords[i+1][1]

line(x, y, x2, y2)

def draw():

pass

Brownian Motion

python ppython.py snippets/brownian_motion.py

ppython_brownian_motion.gif

code:

class Particle:

def __init__(self, x, y, r=10, vx=1, vy=1, randv=False):

self.x = x

self.y = y

self.r = r

self.vx = vx

self.vy = vy

if randv:

rand1 = random(0, 1)

rand2 = random(0, 1)

if rand1:

self.vx *= -1

if rand2:

self.vy *= -1

def draw(self):

noStroke()

fill(140,70,20)

ellipse(self.x, self.y, self.r, self.r)

def update(self):

self.x += self.vx

self.y += self.vy

def check_bounds(self):

global width

if self.x > width-self.r or self.x-self.r < 0:

self.vx *= -1

if self.y > height-self.r or self.y-self.r < 0:

self.vy *= -1

def run(self):

self.update()

self.check_bounds()

self.draw()

particles = [Particle(

random(0, width), random(0, width), vx=2, vy=2, randv=True)

for i in range(200)]

def setup():

pass

def draw():

global particles

background(255, 255, 255)

for p in particles:

p.run()

Grid Triangles

python ppython.py snippets/rand_triangles.py

ppython_rand_triangles.jpg

code:

def triangle(x1, y1, x2, y2, x3, y3):

beginShape()

vertex(x1, y1)

vertex(x2, y2)

vertex(x3, y3)

endShape()

def setup():

background(255)

noStroke()

for x in range(0, width, 50):

for y in range(0, width, 50):

fill(random(0, 255), random(0, 255), random(0, 255))

triangle(

random(x, x+50),

random(y, y+50),

random(x, x+50),

random(y, y+50),

random(x, x+50),

random(y, y+50)

)

def draw():

pass

Docs

constants

mouseX

position of x-coord of mouse

mouseY

position of y-coord of mouse

width

width of the canvas

height

height of the canvas

main functions

setup()

Used to initialise what will be used only once

draw()

code to be looped is placed in it

graphics

line()

line(x1, y1, x2, y2)

rect()

rect(x, y, width, height)

ellipse()

ellipse(x, y, width, height)

beginShape()

beginShape()

vertex(x1, y1)

vertex(x2, y2)

vertex(x3, y3)

endShape()

fill control

background()

background(r, g, b)

background(r, g, b, a)

background(grayness) # background(100) equals background(100, 100, 100)

fill()

fill(r, g, b)

fill(r, g, b, a)

fill(grayness) # fill(100) equals fill(100, 100, 100)

Used to colour shapes

stroke()

stroke(r, g, b)

stroke(r, g, b, a)

stroke(grayness) # stroke(100) equals stroke(100, 100, 100)

Used to colour lines and boarders of shapes

strokeSize()

strokeSize(thickness)

Used to define borders of shapes

noFill()

noFill()

Removes filling

noStroke()

noStroke()

Removes strokes

utility

random()

random(end) # same as random(0, end)

random(start, end)

random() # returns arbitrary value from 0 to 1

Returns integer inclusive of start, exclusive of end

dist

dist(x1, x2, y1, y2)

Returns 2D distance between 2 coordinates.

要在Java调用Python代码,可以使用Jython库。Jython是一个用于在Java虚拟机上运行Python代码的库。它允许您在Java导入和调用Python模块。 下面是一个使用Jython库在Java调用Python代码的例子: 1. 首先,您需要将Jython库添加到Java项目。您可以从Jython官方网站(https://www.jython.org/download)下载Jython安装包并解压缩它。 2. 然后,您需要在Java代码导入Jython库: ```java import org.python.core.PyObject; import org.python.util.PythonInterpreter; ``` 3. 接下来,您可以使用以下代码在Java调用Python代码: ```java // 创建Python解释器 PythonInterpreter interpreter = new PythonInterpreter(); // 执行Python代码 interpreter.exec("print('Hello, World!')"); // 调用Python函数 PyObject someFunction = interpreter.get("some_function"); PyObject result = someFunction.__call__(); // 将Python对象转换为Java对象 String strResult = (String) result.__tojava__(String.class); ``` 4. 如果您想在Java使用Runtime.getRuntime()来运行Python代码,可以使用以下代码: ```java // 创建Runtime对象 Runtime rt = Runtime.getRuntime(); // 创建Process对象来运行Python脚本 Process pr = rt.exec("python script.py"); // 获取脚本输出 BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream())); String line = null; while ((line = input.readLine()) != null) { System.out.println(line); } ``` 在此示例,您需要将“script.py”替换为您要运行Python脚本的名称或路径。请注意,如果Python脚本需要输入,您还需要使用Process对象的输出流将输入发送到脚本。 希望这可以帮助您在Java调用Python代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值