Skip to content

Instantly share code, notes, and snippets.

View nicoddemus's full-sized avatar

Bruno Oliveira nicoddemus

View GitHub Profile
@nicoddemus
nicoddemus / ruff.diff
Created October 24, 2023 18:01
Trying out ruff formatter on pytest - 7 files modifed
diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py
index 0288d7a54..54d5956d4 100644
--- a/src/_pytest/_code/code.py
+++ b/src/_pytest/_code/code.py
@@ -486,9 +486,7 @@ class ExceptionInfo(Generic[E]):
.. versionadded:: 7.4
"""
- assert (
- exception.__traceback__
@nicoddemus
nicoddemus / update-mypy-flags.py
Last active October 4, 2023 14:52
Apply allow-untyped-defs
"""
Change the "default" flags for files in a project, so new files will require full type annotations.
If a file has no flags, it adds "allow-untyped-defs".
If a file already has "disallow-untyped-defs", that flag is removed.
Together with this script, one should update the mypy.ini file of a project:
[mypy.app.*]
disallow_untyped_defs = true
@nicoddemus
nicoddemus / README.md
Last active April 15, 2022 19:23
Example on how to structure a package and testing modules

Arrange files like this:

apple/
  __init__.py
  apple.py
tests/
  test_everything.py
from pathlib import Path
import attr
from sqlalchemy import Column
from sqlalchemy import create_engine
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
from sqlalchemy import MetaData
from sqlalchemy import String
from sqlalchemy import Table
# from: https://doc.qt.io/qt-5/qtcharts-nesteddonuts-example.html
import functools
import random
from PyQt5.QtChart import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Widget(QWidget):
@nicoddemus
nicoddemus / oitavas.txt
Last active April 5, 2021 20:42
Soccer score generator
Seed: 173015
0 X 1
1 X 1
0 X 2
1 X 1
2 X 1
1 X 1
1 X 3
2 X 2
@nicoddemus
nicoddemus / foo.py
Last active March 17, 2021 18:07
mypy slow with nested dict call
def GetFields():
fields = dict(
tabs=dict(
_id='tabs',
caption='',
caption_cmd='',
caption_color_expr='',
caption_horizontal_size_policy='Preferred',
caption_vertical_size_policy='Fixed',
cmd='',

Com relação à type-hints, eu também sou bastante fã.

Type hints, quando rodados junto com um type checker como o mypy, trazem muitas vantagens:

  1. Documentação.

Com certeza não substitui a documentação escrita, mas colocar type hints realmente facilita, pois além de não depender das docstrings, ainda temos a ferramenta checando para você, evitando que ela fica desetualizada.

Eu já vi muitas e muitas vezes (e escrevi também) documentação que até estava correta inicialmente, mas depois de um tempo ficou desatualizada. Por exemplo, dizer que um método retorna um dict de str -> int, mas devido à algum refactoring ele às vezes retornava um valor str (ao invés de int), quebrando o cliente. Aqui o type checker vai detectar o problema imediatamente.

@nicoddemus
nicoddemus / cx_freeze_and_distutils.md
Created May 19, 2015 17:45
Problem trying to freeze an executable which imports distutils modules (directly or indirectly)

cxFreeze and distutils

Problem

When trying to freeze a script which imports any distutils module (for example, distutils.dist), the script fails at runtime with an exception like this:

>   from distutils import dist, sysconfig
E   ImportError: cannot import name dist
"""
Verbatim translation of the RangeSlider class from:
https://github.com/ThisIsClark/Qt-RangeSlider/tree/b3e381fd383aa5b02e78caff9a42fc5f4aab63e6
This is a verbatim translation, without any improvements that could be
made to the code, such as using max()/min() in a few places, interval comparisons
(`0 < x < 10` instead of `x > 0 and x < 10`), etc.
The intent is to keep it as close as the original as possible in case we want