Skip to content

Instantly share code, notes, and snippets.

View leorochael's full-sized avatar

Leonardo Rochael Almeida leorochael

View GitHub Profile
@leorochael
leorochael / fixture_for_catching_warnings.py
Last active February 11, 2021 14:56
Fixture for when you're getting warnings deep in your code during test
"""
Add this fixture to a file containing a test, or to your global `conftest.py`, to turn warnings into errors.
This way the test will break at the warning point, and your traceback will likely contain all functions or methods
responsible for creating the conditions for the warning being raised.
For instance, in the case of a `SettingWithCopyWarning` from pandas, the function that tries to mutate a dataframe
view/slice might not be the function that created the dataframe view/slice in the first place, but that function
is probably in the stack somewhere.
# Based on the description found at https://dicasdeprogramacao.com.br/algoritmo-para-validar-cpf/
def get_cpf_mod_11_digit(digits):
weights = range(len(digits) + 1, 1, -1)
weighted = [digit * weight for digit, weight in zip(digits, weights)]
weighted_sum = sum(weighted)
extra_digit = weighted_sum * 10 % 11 % 10
return extra_digit
def int_to_cpf_with_digits(num):
assert num < 1000000000
@leorochael
leorochael / debug_on_sigint.py
Last active August 8, 2019 18:17
Python snippet to add a SIGINT signal handler (i.e. CTRL+C) that prints a traceback and offers to start pdb
import signal
def may_debug_handler(sig, frame):
# allow double CTRL+C to really break interrupt:
signal.signal(signal.SIGINT, signal.default_int_handler)
c = input('(d)ebug? (i)nterrupt? ')
print 'got:', c
if c == 'd':
breakpoint()
@leorochael
leorochael / odoo_printed_recordset_resolver.py
Last active February 3, 2017 18:18
Resolver of the printable representation of Odoo recordsets
# Paste this in ipython_odoo use it like this:
# r.res.users(1, 2, 3,)
#
# the "res.users(1, 2, 3,)" above is the printable representation of a recordset, sometimes output in logging.
class Resolver(object):
_completables = None
def __init__(self, env, name_parts=()):
self._env = env
self._name_parts = name_parts
"""
This file can be used to demonstrate that the exit status of the process
that is run by a os.exec*() call will be taken as the exit status of the
original process, as can be expected from the fact that os.exec() completely
replaces the original process.
Use it like this on bash:
python test_exit.py die 2 ; echo $?
python test_exit.py sub 2 ; echo $?
@leorochael
leorochael / buildout.cfg
Last active April 20, 2016 19:13
Using IPython with anybox.recipe.odoo
[buildout]
parts = odoo
find-links = http://download.gna.org/pychart/
[odoo]
version = git http://github.com/odoo/odoo.git odoo 8.0 depth=1
recipe = anybox.recipe.odoo:server
eggs =
nose
ipython
@leorochael
leorochael / robotlib.py
Created December 17, 2012 22:07
Keyword Library for the [Robot Framework](http://code.google.com/p/robotframework/) providing keywords `IPython` and `PDB Set Trace` keywords. Use it like this: *** Settings *** Library package.where.i.placed.robotlib.Support *** Test cases *** My Test Case [Some Steps...] IPython [Some Other Steps...] PDB Set Trace
'''
Robot library helpers for debugging
'''
import sys
import pdb
import IPython