Skip to content

Instantly share code, notes, and snippets.

View wonderbeyond's full-sized avatar
🎯
Focusing

Wonder wonderbeyond

🎯
Focusing
View GitHub Profile
@wonderbeyond
wonderbeyond / suppress_output.py
Created October 20, 2021 02:37
[Python] suppress stdout and stderr messages
"""See also https://www.codeforests.com/2020/11/05/python-suppress-stdout-and-stderr/"""
import os
import sys
from contextlib import contextmanager
@contextmanager
def suppress_output(suppress_stdout=True, suppress_stderr=True):
orig_streams = (sys.stdout, sys.stderr)
null_stream = open(os.devnull, "w")
@wonderbeyond
wonderbeyond / slack_log.py
Created July 1, 2021 08:14
Do remote log from slack
import requests
webhook_url = "<your-slack-webhook-url>"
def slack_log(message, webhook_url=webhook_url):
requests.post(
webhook_url,
json={"text": message}
)
@wonderbeyond
wonderbeyond / index.md
Last active July 15, 2021 09:29
k8s cheatsheet

Get running pod names

kubectl -n fission-function get pods --field-selector status.phase=Running -o name

Get all logs of specified pods

KEYWORD="poolmgr-python-default"
NS="fission-function"
@wonderbeyond
wonderbeyond / update-json-by-jsonpath.py
Last active March 7, 2023 04:58
[python][jsonpath-ng] Use JSONPath to update a specific location of a JSON document.
from jsonpath_ng import jsonpath, parse
def mod_func(orig, data, field):
data[field] *= 2
data = {
"foo": [
{"baz": 1},
{"baz": 2},
],
@wonderbeyond
wonderbeyond / netplan.yaml
Last active May 6, 2021 03:33
ubuntu server (20.04, 18.04) netplan conf example
# /etc/netplan/01-netcfg.yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
addresses:
- 172.23.10.197/24
- 172.23.10.198/24
@wonderbeyond
wonderbeyond / index.md
Last active February 1, 2021 07:08
Django-rest-framework metadata endpoint example
class ExampleViewSet(PartialUpdateModelMixin, viewsets.ReadOnlyModelViewSet):
    ...

    @action(detail=False, methods=["GET"], url_path="_meta")
    def api_meta(self, request):
        meta = {
            "fields": {
                "types": {
 "choices": Example.ExampleType.choices
@wonderbeyond
wonderbeyond / index.md
Last active May 25, 2023 17:53
Deploy Django site under a sub-path (virtual location) of a shared domain

Your Django project maybe required to hosted under a shared domain, but use a separate subpath.

To make django generate right URLs and avoid cookie conflicts, do settings below:

FORCE_SCRIPT_NAME = '/A-site/'
CSRF_COOKIE_PATH = LANGUAGE_COOKIE_PATH = SESSION_COOKIE_PATH = FORCE_SCRIPT_NAME

LOGIN_URL = '{0}api-auth/login/'.format(FORCE_SCRIPT_NAME)
LOGOUT_URL = '{0}api-auth/logout/'.format(FORCE_SCRIPT_NAME)
@wonderbeyond
wonderbeyond / index.md
Last active January 15, 2021 06:13
实战中 DRF 应用的基础依赖选型参考

@keywords: django-rest-framework, django, restful

  • gunicorn~=20.0.4
  • Django~=3.1.1
  • django-deprecate-fields~=0.1.0
  • psycopg2-binary~=2.8.5
  • django-cors-headers~=3.5.0
  • django-storages[boto3]~=1.9.1
  • djangorestframework~=3.12.2
  • djangorestframework-queryfields~=1.0.0
@wonderbeyond
wonderbeyond / index.md
Created December 1, 2020 07:10
fixed fcitx punc data

Updated file /usr/share/fcitx/data/punc.mb.zh_CN

. 。
, ,
? ?
" “ ”
: :
; ;
' ‘ ’