可以用defmacro __using__來給之用use的modue,module自動產生預設的method
或者新增新的keyword, 雖然非必要不建議新增太多自定的macro elixir的if else或者ExUnit都有用到
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() |
"""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) |
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 |
import mock | |
import gevent | |
import random | |
def do_job(name='noname', sleep=10): | |
print "begin...%s" % sleep | |
for i in range(sleep): | |
gevent.sleep(i) | |
print "sleep...%s" % i | |
if random.randint(0, 3) == 0: | |
print "oops" |
ExUnit.start | |
defmodule Test do | |
use ExUnit.Case | |
@moduledoc """ | |
Run with test | |
## Examples |
# 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) |
( | |
cd "$NVM_DIR" | |
git fetch origin | |
git checkout `git describe --abbrev=0 --tags --match "v[0-9]*" origin` | |
) && . "$NVM_DIR/nvm.sh" |
""" | |
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 |