Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Luciano Ramalho ramalho

🏠
Working from home
View GitHub Profile
View duvida.py
pessoas = [
{'nome': 'Ana', 'peso': 65.2},
{'nome': 'Juca', 'peso': 89.1},
{'nome': 'Carla', 'peso': 58.3}
]
for pessoa in pessoas:
nome = pessoa['nome']
peso = pessoa['peso']
print(f'{nome:10} {peso:.1f}')
@ramalho
ramalho / table.txt
Created Jun 17, 2020
Strange number protocols
View table.txt
builtins numpy builtins numpy decimal fractions builtins numpy
complex complex64 float float16 Decimal Fraction int uint8
----------------------------------------------------------------------------------------------
numbers.Number • • • • • • • •
----------------------------------------------------------------------------------------------
numbers.Complex • • • • • • •
SupportsComplex • • •
complex(x) (1+0j) (1+0j) (1+0j) (1+0j) (1+0j) (1+0j) (1+0j) (1+0j)
----------------------------------------------------------------------------------------------
numbers.Real • • • • •
@ramalho
ramalho / mymax.py
Last active May 26, 2020
Python `max()` clone, with type hints in 6 overloads
View mymax.py
from typing import Protocol, Any, TypeVar, overload, Callable, Iterable, Union
class Comparable(Protocol):
def __lt__(self, other: Any) -> bool:
...
MISSING = object()
EMPTY_MSG = 'max() arg is an empty sequence'
@ramalho
ramalho / charindex.py
Created May 8, 2020
Functions to create an inverted index to find Unicode characters by name
View charindex.py
"""
``char_index`` builds an inverted index mapping words to sets of Unicode
characters which contain that word in their names. For example::
>>> index = char_index(32, 65)
>>> sorted(index['SIGN'])
['#', '$', '%', '+', '<', '=', '>']
>>> sorted(index['DIGIT'])
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
>>> index['DIGIT'] & index['EIGHT']
View sample.py
from random import shuffle
from typing import Sequence, List, Any
def sample(population: Sequence, size: int) -> List:
if size < 1:
raise ValueError('size must be >= 1')
result = list(population)
shuffle(result)
return result[:size]
@ramalho
ramalho / elide.py
Created Jul 24, 2019
elide: cut text at or before max_len, keeping complete words if possible
View elide.py
#!/usr/bin/env python3
def elide(text, max_len, ellipsis='…'):
if len(text) <= max_len:
return text
cut = max_len
while cut > 0 and text[cut - 1].isalnum():
cut -= 1
@ramalho
ramalho / parameterized_test.exs
Created May 14, 2019 — forked from damonkelley/parameterized_test.exs
Parameterized Tests in Elixir
View parameterized_test.exs
defmodule ParamerizedTest do
use ExUnit.Case
@parameters [
{0, 0},
{1, 1},
{2, 4},
{3, 9},
]
@ramalho
ramalho / faixa_limitada.py
Created Mar 6, 2019
Resposta entre dois valores
View faixa_limitada.py
resposta = -1
while resposta < 0 or resposta > 150:
try:
resposta = int(input('Digite um valor entre 0 e 150: '))
except ValueError:
continue
@ramalho
ramalho / vizinhos.sh
Created Mar 4, 2019
Network neighborhood
View vizinhos.sh
#!/bin/bash
nmap -sn 192.168.15.0/24 | grep report | cut -d " " -f 5,6
@ramalho
ramalho / net-promoter-score.ipynb
Last active May 22, 2018
Net Promoter Score® Calculation
View net-promoter-score.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
You can’t perform that action at this time.