Skip to content

Instantly share code, notes, and snippets.

View exhuma's full-sized avatar
🐢
Slowly advancing on hobby projects

Michel Albert exhuma

🐢
Slowly advancing on hobby projects
View GitHub Profile
@exhuma
exhuma / lsmw.py
Last active August 21, 2020 05:32
WSGI middleware setting up a queue log handler
import logging
import sys
from logging.handlers import QueueHandler, QueueListener
from queue import Queue
from time import sleep
class FlushingHandler(QueueHandler):
def flush(self):
super().flush()
@exhuma
exhuma / example.py
Created July 16, 2020 08:39
SA relationship updates
class ScannedPort:
label: str = ""
clsas ScannedDevice:
hostname: str = ""
ports: List[ScannedPort] = []
class Port(Base):
hostname = Column(String, ForeignKey("Device.hostname")
label = Column(String)
@exhuma
exhuma / iptypes.py
Created May 8, 2020 08:34
SQLAlchemy types for PostgreSQL inet types
"""
This module defines additional "IP-Address" type decorators for use with
SQLAlchemy and PostgreSQL. These types will convert the PostgreSQL values
to/from Python :py:mod:`ipaddress` classes.
These classes can be used in model definitions::
class MyTable(Base):
...
ip = Column(PgIpAddress, ...)
@exhuma
exhuma / fetchedvalue.py
Created March 5, 2020 14:11
SQLAlchemy demonstration of the "FetchedValue" feature
"""
This file demonstrated using the "FetchedValue" feature of SQLAlchemy for
values which are generated by the DB, without knowing *how* they are generated
in the DB.
"""
from sqlalchemy import Column, DateTime, FetchedValue, Unicode, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
Base = declarative_base()
@exhuma
exhuma / mandelbrot.py
Last active February 2, 2020 10:22
Mandelbrot
# pylint: disable=no-member, no-name-in-module
"""
This program is inspired on exercise 4.4 from the book for 1CB "CNESC
Informatique" (2019-2020): The Mandelbrot set. The exercise demands the
following features:
* Display the Mandelbrot set on the complex plane. Initially between -2.2-1.2j
and 1.0+1.2j and later by coordinates selected by the user (see below).
* Points within the set should be drawn in black, points outside of the set
should be colored based on the number of iterations necessary to determine
@exhuma
exhuma / read_prices.py
Created October 2, 2019 06:52
Scrape Prices from a Web-Page
"""
This module shows an alternative implementation of the code shown in the SO question
https://stackoverflow.com/questions/58188342/looping-through-web-pages
Comments marked with a ♫ symbol are possible improvements to this code which were
left out to keep concepts out of the code which could make it more difficult to
understand for beginners.
"""
from os.path import exists
@exhuma
exhuma / accept.py
Created August 8, 2019 11:45
Parser for the "Accept" header in HTTP requests
"""
This module contains helpers to work with the "Accept" request header
"""
import re
from typing import Generator, Iterable, List, NamedTuple, Tuple
AcceptType = NamedTuple('AcceptType', [
('media_type', str),
('quality', float),
])
@exhuma
exhuma / wincred.py
Last active September 2, 2024 15:57 — forked from mrh1997/wincred.py
Retrieve Windows Credential via Python
"""
Access windows credentials
Credentials must be stored in the Windows Credentials Manager in the Control
Panel. This helper will search for "generic credentials" under the section
"Windows Credentials"
Example usage::
result = get_generic_credential('foobar')
@exhuma
exhuma / ipoccupation.py
Created November 22, 2018 17:09
Class to visualise which blocks in a IP range are occupied by networks and hosts
class IPOccupation:
'''
Class to visualise which blocks in a IP range are occupied by networks and hosts
'''
CHARS = {
0: '\u22c5', # sdot
1: '\u2592', # Medium Shade
2: '\u2588' # Full Block
}
@exhuma
exhuma / namedregistry.py
Created September 17, 2018 12:46
Python ABC to register subclasses with a name for easy retrieval.
class NamedRegistry(ABCMeta):
'''
A metaclass which automatically registers each subclass by name when it is
defined.
Each subclass must define the class-attribute ``NAME``
The classes can be retrieved using ``NamedRegistry.get_query``.
'''