This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Storage do | |
defmacro __using__(opts) do | |
quote bind_quoted: [opts: opts] do | |
@adaptor Keyword.fetch!(opts, :adaptor) | |
@config @adaptor.check_config(opts) | |
def save(io_device), do: @adaptor.save(io_device, @config) | |
def delete(path), do: @adaptor.save(path, @config) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
type StorageAdaptor interface { | |
Save(string) | |
Delete(string) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# storage/adaptor.py | |
class StorageAdaptor: | |
def save(self, *, path: str) -> None: | |
raise NotImplementedError | |
def delete(self, *, path: str) -> None: | |
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1><%= @answer %></h1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Math do | |
@doc """ | |
Calculate the square of a integer | |
This function simualtes an expensive operation by sleeping for x seconds | |
""" | |
def square(x) when is_integer(x) do | |
Process.sleep(x * 1_000) | |
x * x | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime, date | |
class DateConverter: | |
regex = '[0-9]{4}-[0-9]{2}-[0-9]{2}' | |
def to_python(self, value: str): | |
return datetime.strptime(value, '%Y-%m-%d').date() | |
def to_url(self, value: date): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect | |
def preload_redirect_middleware(get_response): | |
def middleware(request): | |
response = get_response(request) | |
if isinstance(response, (HttpResponseRedirect, HttpResponsePermanentRedirect)): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# python 2 | |
class Person(object): | |
def __init__(self, name, age): | |
self.name = name | |
self.age = age | |
class Employee(Person): | |
def __init__(self, *args, **kwargs): | |
self.job_title = kwargs.pop('job_title') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
docker rm $(docker ps -a -q) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
from django.core.cache import cache | |
from django.contrib.auth.models import User | |
def benchmark_redis(n=1): | |
for _ in range(n): | |
cache.get("blank") | |