Skip to content

Instantly share code, notes, and snippets.

@Semnodime
Semnodime / kmeansExample.py
Last active May 13, 2024 14:02 — forked from iandanforth/kmeansExample.py
A pure python3 compatible implementation of K-Means clustering. Optional cluster visualization using plot.ly.
"""
This is a pure Python3 implementation of the K-means Clustering algorithm.
It is based on a GitHub Gist which uses Python2:
https://gist.github.com/iandanforth/5862470
I have refactored the code and to assure the code obeys Python Enhancement Proposals (PEPs) rules.
After reading through this code you should understand clearly how K-means works.
This script specifically avoids using numpy or other more obscure libraries.
It is meant to be *clear* not fast.
I have also added integration with the plot.ly plotting library.
So you can see the clusters found by this algorithm. To install plotly run:
@Semnodime
Semnodime / aiohttp_demo.py
Created September 28, 2018 01:25
Small python3.6 demo of a single async request utilizing the aiohttp framework. The point is to understand what is going on.
"""Small demo of a single async request utilizing the aiohttp framework. The point is to understand what is going on."""
import aiohttp
import asyncio
async def main():
async with aiohttp.ClientSession() as session:
# session.get() returns a context manager that can be used by `await` and `async with`
_req_context_manager = session.get('http://localhost:10000')
# print(_req_context_manager)
@Semnodime
Semnodime / How to solve coding errors.txt
Created September 28, 2018 03:13
How to solve coding errors. Talking with a duck.
A good way to find issues in your program is to talk to a duck.
Explain it in a way a duck can understand!
If that is too silly, file a well documented stack overflow issue but before actually sending the question,
think a short period of time about something totally different… and then try to answer the question yourself in a well documented way.
If you thereby solve an issue which turns out to not be a silly coding error, then post the documented problem and your own solution if you found one.
@Semnodime
Semnodime / imreconstruct.py
Last active September 16, 2022 21:46
MatLab imreconstruct in OpenCV Python for Morphological Reconstruction
import cv2
import numpy as np
def imreconstruct(marker: np.ndarray, mask: np.ndarray, radius: int = 1):
"""Iteratively expand the markers white keeping them limited by the mask during each iteration.
:param marker: Grayscale image where initial seed is white on black background.
:param mask: Grayscale mask where the valid area is white on black background.
:param radius Can be increased to improve expansion speed while causing decreased isolation from nearby areas.
@Semnodime
Semnodime / space2exchange.css
Last active November 9, 2020 04:15
Stylus Userstyle: Display Content Only. (for StackOverflow & StackExchange Post Edit Page)
@-moz-document regexp("https?://(\\w*\\.)?stack(exchange|overflow)\\.com/posts/\\d+/edit") {
#sidebar,
#left-sidebar,
#footer {
display: none;
}
#content,
#mainbar,
.container {
@Semnodime
Semnodime / main.py
Last active July 28, 2021 14:27
CRC Calculation for the Nordic nRF24L01
"""Small snippet that demonstrates how to calculate the crc for packets transmitted via the Nordic NRF24L01."""
def bin2hex(x):
def bin2hex_helper(r):
while r:
yield r[0:2].upper()
r = r[2:]
hex_data = hex(int(x, 2))[2:]
@Semnodime
Semnodime / filter_signal.py
Created January 30, 2022 11:01
Script to preprocess IQ-signals in-place: Remove noise and combine nearby signal parts into one.
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
description = """Script to preprocess IQ-signals in-place: Remove noise and combine nearby signal parts into one."""
def list_strong_signal_indices(signal, threshold: int):
"""
Return a list of all indices where the magnitude of the I-part is not smaller than the threshold.
@Semnodime
Semnodime / weird.py
Created January 12, 2023 21:17
weird_python
(... == ...,) != ([...,0],[0,...]) # Python can be weird, if you want it to be….
True
@Semnodime
Semnodime / pdf_flatedecode.py
Last active March 31, 2024 20:07 — forked from averagesecurityguy/pdf_flatedecode.py
Decompress FlateDecode Objects in PDF
#!/usr/bin/env python3
import re
import sys
import zlib
def main(filename:str):
"""This script will find each FlateDecode stream in the given PDF document using a regular expression, unzip it, and print out the unzipped data."""
with open(filename, 'rb') as pdf_file:
@Semnodime
Semnodime / nansi
Created April 10, 2024 10:34
Strip ANSI Color Codes from Input
sed -r "s/\x1B\[[0-9;]*[mK]//g"