Skip to content

Instantly share code, notes, and snippets.

@warabanshi
warabanshi / Vagrantfile
Last active November 18, 2021 05:15
See if the environment is working on WSL2 in Vagrantfile
def is_wsl
File.exists?('/proc/version') && /WSL[0-9]/.match?(IO.readlines('/proc/version').first)
end
@warabanshi
warabanshi / EnvInterpolation.py
Last active November 9, 2021 04:38
Extend configparser interpolation to parse environment variables
"""
This code expects there's a config.ini file under the same directory.
And the file is expected like below contents
See also below if default values isn't needed
https://stackoverflow.com/questions/26586801/configparser-and-string-interpolation-with-env-variable
-- config.ini --
[testdayo]
envuser=${USERNAME:-defusername}
@warabanshi
warabanshi / extract_zip_and_download_a_file.py
Created March 30, 2021 01:39
download zip file and retrieve an office file that the name start with "source"
import io, requests, sys, zipfile
from wsgiref import simple_server
url_base = 'http://s3.amazonaws.com/xxxxxxxx'
s3_link = 'xxxxx.zip'
url = f"{url_base}/{s3_link}"
def get_content_type(filename):
@warabanshi
warabanshi / read-linebreak-csv.py
Created November 25, 2020 06:24
read CSV string which includeds line break in a row through csv.reader()
import csv, io
text = """
12345,12345,revise,one line comment
12345,13245,revise,"State officials were flying over southeastern Utah looking for sheep as part of a routine task. Instead they found something straight out of a sci-fi movie.
From a helicopter, officers from the Utah Department of Public Safety spotted a large metal monolith — a single block of metal — last week. It was sitting in Utah's Red Rock Country in the southeast. Officials have no idea how or when it got there — or who might have placed it."
""".strip()
def output_lines(c1, c2, c3, c4):
@warabanshi
warabanshi / descriptor.py
Created November 10, 2020 01:34
see parameters of a descriptor
class Attribute(object):
def __init__(self, param=None, name='default'):
self.param = param
self.name = name
def __get__(self, instance, objtype):
return getattr(instance, self.get_private_name())
def __set__(self, instance, val):
@warabanshi
warabanshi / inspect.py
Created November 10, 2020 00:58
inspect stuck frame
def get_stuckframe1():
get_stuckframe2()
def get_stuckframe2():
curframe = inspect.currentframe()
calframe = inspect.getouterframes(curframe, 2)
print('inspect = %s' % "\n".join([str(t) for t in calframe]))
if __name__ == '__main__':
get_stuchframe1()
@warabanshi
warabanshi / get_subclasses.py
Last active October 28, 2020 06:59
create subclass' instance from base class and target class name
def all_subclasses(model):
"""Gets the model from a given key."""
return set(model.__subclasses__()).union([s for c in model.__subclasses__() for s in all_subclasses(c)])
def get_model_from_key(model, key):
models = {klass.__name__: klass for klass in all_subclasses(model)}
return models.get(key, None)
class BaseModel(object):
pass
@warabanshi
warabanshi / get_full_path.sh
Created January 28, 2020 01:29
get full path of `target_dir` placed under the current directory
$ echo $(cd target_dir; pwd)
@warabanshi
warabanshi / gist:a4f03019739dd32fd5172b4449415e50
Created October 30, 2019 06:28
send curl request with session id
$ curl -v -X POST -F "id=something" -F "pass=something" https://example.com/login
> ...
> Cookie: PHPSESSID=xxxxxxxxxxxxxxxxxxxxxx
> ...
# use returned session id
$ curl --cookie "PHPSESSID=xxxxxxxxxxxxxxxxxxxxxx" https://example.com/something
$ grep $'\u200b' target.file