Skip to content

Instantly share code, notes, and snippets.

@jsbueno
jsbueno / screeenshot.py
Created January 11, 2018 15:42
Screenshot with PyQt5
# This is all that is needed to take a screenshot with PyQt5
from PyQt5 import QtGui, QtWidgets
app = QtWidgets.QApplication([])
scr = app.primaryScreen()
data = scr.grabWindow(0)
data.save("blip.png")
@jsbueno
jsbueno / unpikle_to_text.py
Created January 5, 2019 02:05
Python script to read a pickle-file to print its contents with as few side-effects (importing, instantiating) as possible
import re, pickle, pprint, sys
from types import ModuleType
from collections.abc import Sequence, Mapping, Set
from contextlib import contextmanager
def pythonize(obj):
if isinstance(obj, (str, bytes)):
return obj
class Cliente:
tabela = "clientes"
def __init__(self, id=None, nome="", telefone="", email=""):
self.id = id
self.nome = nome
self.telefone = telefone
self.email = email
def save(self, con, tabela=""):
@jsbueno
jsbueno / chuva.py
Last active January 12, 2019 13:38
import random
import pygame
LARGURA, ALTURA = TAMANHO = 800, 600
DROP_SIZE = 50
def init():
global screen, raindrop
screen = pygame.display.set_mode(TAMANHO)
@jsbueno
jsbueno / Usage example
Created May 16, 2019 06:04
matplotlib - plot programatic function without explictly generating all Y points into a ndarray
import numpy as np
from matplotlib import pyplot as plt
import math
# This simple example still requires X points in an ndarray, old-style:
x = np.arange(0, 10, .1)
# but for y data, we can use the class in the snippet above:
# replace math.sin for any function you want.
# Not that a scalar function is used, not one that operates on ndarrays (so, not np.sin)
@jsbueno
jsbueno / plot_example.py
Created May 16, 2019 13:31
Example of raw plotting 2d funciton with pygame
import pygame
larg, alt = 800, 600
intervalo = -10, 10
fator_y = 1.0
tela = pygame.display.set_mode((larg, alt))
f_x= lambda x:3 * x ** 2 + 2 * x + 4
@jsbueno
jsbueno / awaitable_class.py
Created June 13, 2019 03:11
Snippet with metaclass enabling a class to have an async __init__ method in Python.
import inspect
from functools import wraps
def async_init_wrapper(func):
def wrapper_stage1(instance):
async def wrapper_stage2(*args, **kw):
value = await func(instance, *args, **kw)
if value is not None:
raise TypeError("__async_init__() should return None")
return instance
"""
Implements a for loop iterator wrapper to be used
in sync-loops inside async functions, so that control
can be yielded to the asyncio-loop after a certain
timeout or at each N iterations.
The original "in line" implementation, idea and needed were presented by
Nikita Melentev at the e-mail to Python-ideas list
stored at:
@jsbueno
jsbueno / itercontext.py
Last active July 12, 2019 14:35
Python recipe to enter an arbitrary number of contexts at once
from contextlib import contextmanager
import sys
@contextmanager
def itercontext(*args):
contexts = [context.__enter__() for context in args]
try:
yield contexts
@jsbueno
jsbueno / gist:5f5d200fd77dd1233c3063ad6ecb2eee
Created August 9, 2019 15:34
Python JSON Encoder able to encode arbitrary precision decimals as numbers
import json, re, uuid
class JsonDecimalAwareEncoder(json.JSONEncoder):
def __init__(self, *args, **kw):
self.original_numbers = {}
super().__init__(*args, **kw)
def default(self, obj):
if isinstance(obj, decimal.Decimal):
key = uuid.uuid4()