Skip to content

Instantly share code, notes, and snippets.

View whchi's full-sized avatar
:octocat:

whchi whchi

:octocat:
View GitHub Profile
@whchi
whchi / file_system_manager.py
Created January 4, 2023 06:00
python laravel-like filesystem feature
import abc
from io import BytesIO, StringIO
from typing import Any, TypeVar
WriteContentType = TypeVar('WriteContentType', StringIO, BytesIO, str)
class FileSystemManager(abc.ABC):
_instance: Any = None
@whchi
whchi / get_all_files.py
Last active December 30, 2022 10:43
get files with depth and exclude items
def get_all_files(src_dir: str,
depth: int = 0,
exclude_files: List[str] = [],
exclude_dirs: List[str] = []) -> List[str]:
"""
Args:
depth: walk depth of directories,
0 will walk through same directory
-1 will walk through all directories
Returns:
@whchi
whchi / gdload.sh
Created December 23, 2022 13:27
down google dirve file using id hash
function gdload () {
CONFIRM=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate "https://docs.google.com/uc?export=download&id=$1" -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')
wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$CONFIRM&id=$1" -O $2
rm -rf /tmp/cookies.txt
}
@whchi
whchi / sqlalchemy_paginator.py
Last active December 20, 2022 02:07
python sqlalchemy paginator class example
import math
from typing import Any, Dict
from sqlalchemy.orm import Query
class Paginator:
@staticmethod
def paginate(query: Query, page: int, per_page: int) -> Dict[str, Any]:
@whchi
whchi / range.ts
Created August 30, 2022 02:58
typescript range function
function range(start: number, end: number, step=1) {
return {
[Symbol.iterator]() {
return this;
},
next() {
if (start < end) {
start = start + step;
return {value: start, done: false}
}
@whchi
whchi / app_Logger_GoogleChatHandler.php
Created May 31, 2022 07:27
laravel log to google chat
<?php
namespace App\Logger;
use Arr;
use Illuminate\Support\Facades\Http;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
class GoogleChatHandler extends AbstractProcessingHandler
@whchi
whchi / flat.php
Created May 19, 2022 02:47
flat array with key value
function flatten($array, $prefix = '') {
$return = [];
foreach ($array as $key => $value) {
if (is_array($value)) {
$return = array_merge($return, flatten($value, $prefix . $key . '_'));
} else {
$return[$prefix . $key] = $value;
}
}
return $return;
@whchi
whchi / download.js
Created May 5, 2022 06:41
download via ajax
let response = getSheetApi; // with responseType arraybuffer or blob
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'filename.ext');
document.body.appendChild(link);
link.click();
link.remove();
@whchi
whchi / laravel_get_group_latest_the_easist_way.php
Created April 14, 2022 02:34
laravel get group latest the easist way
<?php
$query = Model::conditions();
$query->whereRaw('id IN (SELECT max(id) from table group by other_id)');
$query->get();
@whchi
whchi / google_sheet_get_by_tab.gs
Created January 24, 2022 06:29
google sheet's app script get by tab name
function doGet() {
const tab = e.parameter.tab;
const spreadSheet = SpreadsheetApp.openById('your-sheet-id');
const tabs = {
tab1: { idx: 0, titleRange: 'A1:H1', dataRange: 'A2:H' },
tab2: { idx: 1, titleRange: 'A1:E1', dataRange: 'A2:E' },
tab3: { idx: 2, titleRange: 'A1:G1', dataRange: 'A2:C' },
tab4: { idx: 3, titleRange: 'A1:C1', dataRange: 'A2:C' },
tab5: { idx: 4, titleRange: 'A1:D1', dataRange: 'A2:D' },
};