This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@-moz-document regexp("https?://(\\w*\\.)?stack(exchange|overflow)\\.com/posts/\\d+/edit") { | |
#sidebar, | |
#left-sidebar, | |
#footer { | |
display: none; | |
} | |
#content, | |
#mainbar, | |
.container { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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:] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(... == ...,) != ([...,0],[0,...]) # Python can be weird, if you want it to be…. | |
True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sed -r "s/\x1B\[[0-9;]*[mK]//g" |
OlderNewer