Skip to content

Instantly share code, notes, and snippets.

View villares's full-sized avatar
💥

Alexandre B A Villares villares

💥
View GitHub Profile
import py5
import PySimpleGUI as sg
bg = True
values = {'-Text-': '', '-Hue-': 0, '-BG-': False}
def setup():
global window
py5.size(600, 600)
py5.color_mode(py5.HSB)
@villares
villares / dict_csv.py
Last active November 10, 2022 21:34
JSON IBGE / CSV
import csv
from urllib.request import Request, urlopen
# I used the publish feature from a Google Spreadsheet
url = "https://docs.google.com/spreadsheets/d/e/2PACX-1vT7etkg--Axh_CJlmZdlJ3rrE71rZ10kNQpbqUr8rcPL8WLNepgaIJMN6ZbUHbscjBv014rjaB9Qkwy/pub?gid=0&single=true&output=csv" # change to your own!
response = urlopen(Request(url))
content = response.read().decode('utf-8')
data = csv.DictReader(content.splitlines())
cols = data.fieldnames
@villares
villares / image_helper.py
Created November 8, 2022 21:44
for use with PySimpleGUI
import PIL.Image
import io
def image_as_png_bytes(file_path, resize=None):
"""
Open an image file and convert it into PNG formated bytes, resize optional.
Return tuple (bytes, <dict from PIL.Image.info>)
"""
img = PIL.Image.open(file_path)
@villares
villares / BoardMaker.md
Created November 3, 2022 15:04
Fazedor de tabuleiros
# py5coding.org imported mode
particulas = []
def setup():
size(900, 900)
background(0)
color_mode(HSB)
# for _ in range(100):
# p = Particula(width / 2, height / 2)
@villares
villares / demo_doctest.py
Created September 26, 2022 13:13
Exemplo de doctest para o Clube de Leitura Python
# Executa os testes no arquivo de documentaçao `demo_doctest.txt`
# Note que lá tem um `import` do módulo a ser testado no começo
import doctest
print('início dos testes')
doctest.testfile("demo_doctest.txt")
print('terminou')
@villares
villares / demo_tkinter_filedialog.py
Last active September 25, 2022 19:48
Minimal example of tkinter OS file/dir selection dialogs
import tkinter as tk
import tkinter.ttk as ttk
from tkinter import filedialog
root = tk.Tk()
style = ttk.Style(root)
style.theme_use("clam")
@villares
villares / merge_pdfs_side_by_side.py
Last active October 3, 2022 01:48
PDF merging and PDF scaling down to 4 per page
#!/usr/bin/env python
import sys
# Requires pdfrw2 - https://pypi.org/project/pdfrw2/
from pdfrw import PdfReader, PdfWriter, PdfDict, PdfName, PageMerge
def write_side_by_side(input_a, input_b, output_file):
"""
Write to the output file a PDF with the combined contents of 2
input files. Each page gets 2 pages from each of the input files,
scaled down by a factor of 0.5, side by side in a 4 pages up layout.
@villares
villares / littletable_read_csv.py
Last active September 26, 2022 00:25
Read CSV from page and draw using py5
# Using Paul McGuire's littletable https://pypi.org/project/littletable/
# Needs py5 imported mode https://py5.ixora.io for drawing
import csv
import littletable as lt
# Reading a CSV from a GitHub repo, but it can be used
# to read from a Google Spreadsheet if you use the 'publish' feature
url = "https://raw.githubusercontent.com/villares/Resources-for-teaching-programming/main/II%20-%20Books%20%26%20References.csv" # change to your own!
# I'm counting now on a JPype enviroment provided by importing py5 (so I can use javax.swing pane)
# see the previous revision if you don't want that!
def get_arduino(port=None):
"""
This is a PyFirmata 'helper' that tries to connect to an Arduino
compatible board.
If no port is informed, using pyserial's serial.tools.list_ports.comports(),
if one port is found, tries that one. If more than one port is found,