-
Dear @hx2A, we had this conversation in a now closed pull request about another unrelated issue, so I think this is a better place to keep your recommendations:
Now... I'm trying to test the imported mode examples I converted from Processing Python mode... and I can launch them, but I'm not sure if I should try some threading or other strategies... I'm on Linux, not on MacOS, suppose I'd like to run them without blocking (because I think I'm going to try This seems not to be the way of doing it 😆 : from py5_tools import imported
# Wrong!
imported.run_code('.../path_to/my_imported_example.py',
sketch_args=['block=False']) # or , py5_options=['block=False']) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
I really wish py5 had a better testing framework! Yet another thing to work on.
pyautogui sounds like a good approach. I see why you would want
|
Beta Was this translation helpful? Give feedback.
-
This is quite ugly but it might help someone getting started... from pathlib import Path
import time
import os
import pyautogui
# from capture_stdout import capture_stdout # Should try again other time
# from PIL import ImageGrab # didn't work for me
# I also couldn't work out pyautogui scrot integration
import pyscreenshot as ImageGrab
from py5_tools import imported
imported._CODE_FRAMEWORK = """{0}
run_sketch(block=False, py5_options={2}, sketch_args={3})
if {1} and is_dead_from_error:
exit_sketch()
"""
collection_folder = Path('/home/villares/GitHub/py5examples/examples-from-Processing-Python-mode/')
sketch_paths = collection_folder.rglob('*.py') # TODO: skip secondary tab modules?
results_folder = Path.cwd() / 'results'
results_folder.mkdir(exist_ok=True)
def run_sketch(sketch_file_path):
"""Run sketch, grab a snapshot."""
with open(sketch_file_path) as f:
code_lines = f.readlines()
W = H = 128
for li in code_lines:
if li.lstrip().startswith('size('): # TODO: handle full_screen() sketches
# I don't like regex
start, comma = li.find('('), li.find(',')
second_comma = li.find(',', comma + 1)
end = second_comma if second_comma > 0 else li.find(')')
try:
W, H = int(li[start+1:comma]), int(li[comma+1:end])
except ValueError:
print(f'Problem with {sketch_i}:\n'
f'*{li[start+1:comma]}*{li[comma+1:end]}*'
f'\n*{li}*\non {sketch_file_path}')
# for sketches that have to load stuff from relative paths
os.chdir(sketch_file_path.parent) # I'm too lazy to add a proper context manager
imported.run_code(sketch_file_path, py5_options=['location=0,25'])
# time for the sketch to open window and start
time.sleep(10 if sketch_i in slow_sketches else 1)
if sketch_i in move_sketches:
pyautogui.moveTo(W // 2, H // 2 + 53)
if sketch_i in keyboard_sketches:
pyautogui.write('abcde')
# this has to be improved for P3D sketches
im = ImageGrab.grab(bbox=(2, 53, W + 2, H + 53)) # x1,y1,x2,y2 (not w,h)
sketch_name = (f'{sketch_i:03d}'
# f'_{sketch_file_path.parent.parent.parent.stem}'
# f'_{sketch_file_path.parent.parent.stem}'
# f'_{sketch_file_path.parent.stem}'
f'_{sketch_file_path.stem}')
im.save(results_folder / (sketch_name + '.png'))
pyautogui.press('esc') # close sketch, let's hope it has focus...
time.sleep(5 if sketch_i in slow_sketches else 0.1)
slow_sketches = [22, 24, 25, 25]
keyboard_sketches = [37]
move_sketches = [10]
click_sketches = [] # not implemented
global sketch_i
for sketch_i, sketch_file_path in enumerate(sketch_paths):
print(sketch_i, sketch_file_path)
run_sketch(sketch_file_path) |
Beta Was this translation helpful? Give feedback.
I really wish py5 had a better testing framework! Yet another thing to work on.
pyautogui sounds like a good approach. I see why you would want
block=False
. Theimported.run_code()
method can't do that for you though. In that case, the user's code gets added to this template:block=True
, and py5_op…