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
class NonFieldErrors: | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self.non_field_errors = [] | |
self.meta.form_template = 'struct.html' | |
def get_form_context(self, value, prefix='', errors=None): | |
context = super().get_form_context(value, prefix, errors) | |
context['non_field_errors'] = self.non_field_errors | |
return context |
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") | |
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
# 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
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
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
<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
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
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
Enum.reject 1..100, fn i -> | |
square_root = Decimal.sqrt(i) | |
remainder = Decimal.rem(square_root, 1) | |
Decimal.eq?(remainder, 0) | |
end |