Skip to content

Instantly share code, notes, and snippets.

View jupiterbjy's full-sized avatar
💭
Meow

jupiterbjy

💭
Meow
  • E8
  • 07:46 (UTC +09:00)
View GitHub Profile
@jupiterbjy
jupiterbjy / NewtonDividedDiffInterpolation.py
Last active June 16, 2021 11:32
Hair length calculation using Newton's Divided Difference Interpolation
"""
Calculating hair length with given coords using Newton's Divided Differential interpolation.
If interpolated function's shape does not comply with actual hair curve, (i.e. drawn in GeoGebra)
Start giving more accurate coordinates.
"""
from decimal import Decimal
from typing import Iterable, Tuple, Sequence
from fractions import Fraction
# from matplotlib import pyplot
@jupiterbjy
jupiterbjy / execution_server.py
Created June 16, 2021 11:55
Server-side script to support running python script as [discord command](https://github.com/jupiterbjy/Meow_py/tree/main/Meowpy/BotComponents/PythonExecution). Designed for separate machine using OverlayFS.
#!/usr/local/bin/python3.9
"""
Only tested to run on raspberry pi, but shouldn't be limited to it only.
Listens for incoming connections and execute received python codes.
Obviously this is very dangerous. This is to be run on overlayFS with Raspbian in mind.
"""
import subprocess
@jupiterbjy
jupiterbjy / memo.cpp
Created June 21, 2021 16:48
Memo sheet
#include <iostream>
#include <vector>
#include <algorithm> // for_each
using namespace std;
// forward declaration
class A;
// forward declaration
void friend_function_demo(A a);
@jupiterbjy
jupiterbjy / watchdog_demo.py
Created September 13, 2021 17:00
Code that look for newly created .h5 files in given path
import time
import traceback
import h5py
import queue
from typing import Union
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler, DirCreatedEvent, FileCreatedEvent
@jupiterbjy
jupiterbjy / string_search_in_files.py
Created December 8, 2021 14:12
Search function for searching or replacing files that have the search keyword recursively. Made this to help myself change asyncio project into anyio. Pretty sure this is reinvention of wheel but whatever!
"""
Test use-case
>>> len(list(file_search_gen("./", "asyncio", file_extensions=(".py",))))
6
>>> for f in file_search_gen("./", "asyncio", "REPLACE_THIS", file_extensions=(".py",)):
... print(f.as_posix())
...
telethon_examples/interactive_telegram_client.py
@jupiterbjy
jupiterbjy / pure_python_asyncio_file_server.py
Last active January 10, 2022 09:04
For answering https://stackoverflow.com/questions/70647470 - Pure isn't better! Just use 3rd party libraries if you can.
import asyncio
import pathlib
import textwrap
from pprint import pprint
ROOT = pathlib.Path(__file__).parent
async def read_all(r: asyncio.StreamReader):
@jupiterbjy
jupiterbjy / eafp_lbyl_demo.py
Created January 23, 2022 08:38
Demonstration of EAFP style being more effective then conventional LBYL style in python
from timeit import timeit
dataset = ['Not int']
dataset.extend(range(10000))
def if_checking():
for data in dataset:
if isinstance(data, int):
"""
Nursery cancellation demo of websocket
"""
import itertools
import trio
import fastapi
import hypercorn
from hypercorn.trio import serve
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Websocket test</title>
</head>
<body>
<button id="start">Start connection</button>
<button id="close" disabled>Close connection</button>
<input type="text" id="input_" value="INPUT_YOUR_UUID">
@jupiterbjy
jupiterbjy / contextmanager_basic_demo.py
Created February 22, 2022 07:09
Demonstration of contextmanager in python. It's more than a gimmick when it comes to resource management and cleanup.
"""
Context manager Demo
"""
from contextlib import contextmanager
# generator implementation
@contextmanager
def mock_open(*args, **kwargs):