Skip to content

Instantly share code, notes, and snippets.

View HacKanCuBa's full-sized avatar
⛷️
Also in gitlab.com/hackancuba

Iván || HacKan HacKanCuBa

⛷️
Also in gitlab.com/hackancuba
View GitHub Profile
@HacKanCuBa
HacKanCuBa / cache_helpers.py
Last active March 17, 2024 07:04
Cache handy helpers
"""Handy cache helpers.
These are not yet production ready, as I haven't toroughly tested them, but close.
---
Mandatory license blah blah:
Copyright (C) 2023 HacKan (https://hackan.net)
This Source Code Form is subject to the terms of the Mozilla Public
@HacKanCuBa
HacKanCuBa / sqlalchemy_helpers.py
Last active March 17, 2024 07:04
SQLAlchemy handy helper functions
import functools
from contextlib import asynccontextmanager, contextmanager
from time import monotonic
from typing import Annotated, Any, AsyncGenerator, Generator, Hashable, Iterable, Literal, Optional, Sized, Union, overload
from sqlalchemy import event
from sqlalchemy.dialects.mysql.asyncmy import AsyncAdapt_asyncmy_cursor
from sqlalchemy.engine import URL, Connection, Engine, Row, create_engine
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy.orm import Session, sessionmaker
@HacKanCuBa
HacKanCuBa / pickle_deal.py
Created March 1, 2022 21:54
How pickle changes according to how the interpreter gets called
"""Pickle dumps test.
When pickle dumps a class, it will include the FQDN for the module. In this example,
we see how an imported class gets dumped including its module information, whereas
a local one has the current module name, which could be `__main__` or other depending
on how the interpreter was called!
If you save this file as `pickle_deal.py` and then in a python terminal you import it
`import pickle_deal`, you will see the following printed:
b'\x80\x04\x95)\x00\x00\x00\x00\x00\x00\x00\x8c\x0bpickle_deal\x94\x8c\x07Decimal\x94\x93\x94\x8c\x07123.456\x94\x85\x94R\x94.'
@HacKanCuBa
HacKanCuBa / logo_turtle.py
Last active July 17, 2023 20:11
Logo (programming language) turtle excercise
"""Logo (programming language) turtle exercise."""
import typing
import unittest
from dataclasses import dataclass
from enum import Enum
from itertools import zip_longest
from unittest import TestCase
@HacKanCuBa
HacKanCuBa / encoder.py
Last active January 20, 2022 15:01
Encode with custom alphabet
def encode_int(number: int, *, alphabet: bytes) -> bytes:
"""Encode given number using the characters from the alphabet."""
if number < 0:
raise ValueError('number must be positive')
if len(alphabet) != len(set(alphabet)):
raise ValueError('characters in the alphabet must be unique')
if number == 0:
return alphabet[0:1]
@HacKanCuBa
HacKanCuBa / minisign.pub
Last active September 20, 2020 22:51
My minisign public key (cross posted as a snippet https://gitlab.com/-/snippets/2017082 )
untrusted comment: HacKan minisign public key 8FE49E3814424F5C
RWRcT0IUOJ7kj6AFLyI3pHmT6dhr+WN8C2FR6HguMmEK0MnsSImqSmjg
@HacKanCuBa
HacKanCuBa / blake2signer.py
Last active December 6, 2022 00:04
Blake2Signer: use BLAKE2 in keyed hashing mode to sign and verify data. DEPRECATED BY https://blake2signer.hackan.net | https://gitlab.com/hackancuba/blake2signer | https://pypi.org/project/blake2signer
# ---
# DEPRECATED BY: https://gitlab.com/hackancuba/blake2signer
# ---
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# by HacKan (https://hackan.net), 2020.
# This software is provided as-is. You are free to use, share, modify
# and share modifications under the terms of that license, even with
@HacKanCuBa
HacKanCuBa / time_calc.py
Last active August 19, 2020 20:30
Calculate and sum time differences easily
# Copyright © 2020 HacKan
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
#
# This software is provided as-is. You are free to use, share, modify
# and share modifications under the terms of that license. Attribution
# is not required to share but is appreciated.
"""Calculate and sum time differences easily.
@HacKanCuBa
HacKanCuBa / hashlib_timing.py
Created March 30, 2020 19:49
Measure execution time of hashing functions from hashlib in Python3
"""Time hashlib hashing functions.
Useful to help decide which one to use if time is of the escence. I still recommend
blake2 or sha384.
Copyright © 2020 HacKan <@hackancuba>
This program is free software. It comes without any warranty, to
the extent permitted by applicable law. You can redistribute it
and/or modify it under the terms of the Do What The Fuck You Want
@HacKanCuBa
HacKanCuBa / find-https-debian-archives.py
Last active July 4, 2023 19:37 — forked from eighthave/find-https-debian-archives.py
Script to find official Debian mirrors that support HTTPS
#!/usr/bin/env python3
"""Find Debian HTTPS archives.
Script based on https://gist.github.com/eighthave/7285154
I made it asynchronous and parallel, so overall I measured it to be 6 times faster or more.
Requires Python 3.7+
Additional resources not exactly related to this script but could be helpful for