Skip to content

Instantly share code, notes, and snippets.

@allenyang79
allenyang79 / my_mod.exs
Created March 14, 2018 18:18
elixir pattern match on case
defmodule MyMod do
defstruct ~w|foo bar|a
def hello(v) do
case v do
%{foo: _foo} = v -> IO.puts(_foo)
_ -> :oops
end
end
end
import time
import gevent
import gevent.monkey
from flask import Flask, copy_current_request_context, g, request, _request_ctx_stack
import logging
gevent.monkey.patch_all()
logging.basicConfig(level=0) # logging.DEBUG)
logger = logging.getLogger()
@allenyang79
allenyang79 / error_msg.py
Created February 21, 2018 03:33
python custom ErrorMsg inherit form str
"""Partice to custom string inherit python str
"""
class ErrorMsg(str):
def __new__(cls, *args, **kw):
if len(args) > 0:
message = args[0]
else:
message = kw.get('message')
return str.__new__(cls, message)
@allenyang79
allenyang79 / foo.iex
Created February 14, 2018 06:46
test elixir `with` syntax #elixir
defmodule Foo do
def foo(a,b) do
with {:ok, dob} <- a,
{:ok, name} <- b
do
%{dob: dob, name: name}
else
# nil -> {:error, ...} an example that we can match here too
# err -> err
_ -> :err
@allenyang79
allenyang79 / greenlet-link.py
Last active February 9, 2018 02:48
test greenlet link
ExUnit.start
defmodule Test do
use ExUnit.Case
@moduledoc """
Run with test
## Examples
@allenyang79
allenyang79 / 0_readme.md
Last active February 3, 2018 09:35
macro & use in module

Elixir的macro & use

可以用defmacro __using__來給之用use的modue,module自動產生預設的method

或者新增新的keyword, 雖然非必要不建議新增太多自定的macro elixir的if else或者ExUnit都有用到

@allenyang79
allenyang79 / positive-lookahead.py
Last active December 20, 2017 10:00
regexp check
# abc(?=def) 比對時,會把'def'算進去,但比對結果不會把(?=def)裡的字元組進去,比對出的位置是abc
# abc(:=def) 會比對adcdef,但在match的結果,並不會有def
# abc(?!def) 比對出abc但後面不是def
>>> pattern = r"abc"
>>> re.search(pattern, 'xyzabcdefxyz').group(0)
'abc'
>>>
>>> pattern = r"abc(?=def)"
>>> m = re.search(pattern, 'xyzabcdefxyz').group(0)
@allenyang79
allenyang79 / upgrade.sh
Last active November 28, 2017 07:16
[uprade nvm] #nvm #bash
(
cd "$NVM_DIR"
git fetch origin
git checkout `git describe --abbrev=0 --tags --match "v[0-9]*" origin`
) && . "$NVM_DIR/nvm.sh"
@allenyang79
allenyang79 / snippet.py
Created November 10, 2017 03:40
[flask.get_json] #flask
"""
Custom Flask and Requset.
overwrite Request.on_json_loading_failed and raise a custom exception.
"""
class CustomRequest(Request):
def on_json_loading_failed(self, e):
"""
ref: https://tedboy.github.io/flask/_modules/flask/wrappers.html#Request.on_json_loading_failed