Skip to content

Instantly share code, notes, and snippets.

View domodomodomo's full-sized avatar

domodomodomo domodomodomo

View GitHub Profile
@domodomodomo
domodomodomo / detect_file_updates.py
Last active May 30, 2024 18:58
When a directory on the server is updated, notify the client's browser via WebSocket using watchdog.
"""
pip install fastapi uvicorn starlette watchdog
uvicorn detect_file_updates:app --reload
"""
import asyncio
import os
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.responses import HTMLResponse
@domodomodomo
domodomodomo / vuepress-webcard.vue
Last active November 22, 2019 15:46
VuePress web card.
<!--
## usage
### (1)
Specify a relative path.
The Single File Component does not resolve the relative path to the figure, so the img tag is required.
```markdown
<vuepress-card href="../relative/path/to/directory/">
<img src="../relative/path/to/directory/img.jpg">
</vuepress-card>
// original
// https://github.com/vuejs/vuepress/blob/ba7f586e2e8173e5e30c345ea6a92ff213925a2e/packages/%40vuepress/theme-default/util/index.js#L188-L215
/**
* @param { Route } route
* @param { Array<string|string[]> | Array<SidebarGroup> | [link: string]: SidebarConfig } config
* @returns { base: string, config: SidebarConfig }
*/
export function resolveMatchingConfig (regularPath, config) {
if (Array.isArray(config)) {
@domodomodomo
domodomodomo / snake_to_kebab.py
Last active October 5, 2019 09:11
Convert Python attributes from snake case to lower camel case. This gist might be useful when you use an application supporting GraphQL like Ariadne and Graphene.
def snake_to_kebab(identifier):
if isinstance(identifier, list):
container = range(len(identifier))
for element in container:
snake_to_kebab(identifier[element])
elif isinstance(identifier, dict):
container = [key for key in dict.keys()]
for element in container:
snake_to_kebab(getattr(identifier, element))
"""script for scraping images.
1) overview
input: site URL
output: image files
2) usage
$ python scrape_image.py https://pt.wikipedia.org/wiki/Veneza
3) prerequisite
$ pip3 install requests, bs4
4) concept
1) Get thumnail URL.
"""Check the obj is immutable or not by adding new attribute or sequence.
USAGE:
isimmutable(var)
WARNING:
This code cannot precisely check immutability,
because Python has the mechanisms to cutomize attribute access,
such as property and descriptor.
import random
import time
N = 100000
lst1 = [random.randint(0, 9) for _ in range(N)]
lst2 = lst1.copy()
# insert
start1 = time.time()
"""Compare two list."""
def equal_list(lst1, lst2):
lst1 = lst1.copy()
for element in lst2:
try:
lst1.remove(element)
except ValueError:
return False
import collections
def sample():
# point 1. 属性はデコレータに文字列で与える。
@immutable(
'x1', 'y1', 'x2', 'y2',
'is_rectangle', 'is_line', 'is_dot')
class Region(object):
# point 2. __init__ ではなく、__new__ を使う。
@domodomodomo
domodomodomo / immutable_metaclass.py
Last active November 4, 2019 08:02
Make an object immutable without namedtuple.
"""
# 概要
immutable なオブジェクトを生成するメタクラス Immutable
属性参照は速いけど
namedtuple: 0.07781907100070384
自作したクラス: 0.04243788100029633
インスタンス化は遅い