Skip to content

Instantly share code, notes, and snippets.

View fabien-michel's full-sized avatar

Fabien MICHEL fabien-michel

View GitHub Profile
@fabien-michel
fabien-michel / lru_cache_class_method.py
Created October 30, 2019 08:15
Context manager for caching a class method
class LruCacheClassMethod():
def __init__(self, klass, method_name):
self.klass = klass
self.method_name = method_name
self.original_method = getattr(self.klass, self.method_name)
def __enter__(self):
new_method = functools.lru_cache()(self.original_method)
setattr(self.klass, self.method_name, new_method)
{
"blop_f": 4.2,
"blop_i": 42,
"blop_fs": "4.2",
"blop_is": "42",
}
[
{"id":1, "value":"One"},
{"id":2, "value":"Two"},
{"id":3, "value":"Three"},
{"id":4, "value":"Four"},
{"id":5, "value":"Five"},
{"id":6, "value":"Six"},
{"id":7, "value":"Seven"},
{"id":8, "value":"Height"},
{"id":9, "value":"Nine"},
[{
"id": 1,
"title": "The Shawshank Redemption ",
"year": "1998",
"votes": 194865,
"rating": "7.5",
"rank": 1
}, {
"id": 2,
"title": "The Godfather",
@fabien-michel
fabien-michel / bench_merge.rb
Last active April 25, 2016 09:54
test merge! on hstore
class BenchMerge
# class CreateAnimals < ActiveRecord::Migration
# def change
# enable_extension 'hstore'
#
#
# create_table :animals do |t|
# t.hstore :data
# t.timestamps null: false
@fabien-michel
fabien-michel / datepicker_input.rb
Last active July 14, 2022 15:23
Activeadmin datepicker returning ISO formatted date but displaying in a custom date format
# Usage : f.input :my_date_field, as: :datepicker, datepicker_options: {dateFormat: 'mm-dd-yy'}
# Default dateFormat french : dd/mm/yy
class DatepickerInput < ActiveAdmin::Inputs::DatepickerInput
def to_html
input_wrapping do
# New field options inherit from given input_html_options
new_input_html_options = input_html_options
[
{"id":1, "value":"A"},
{"id":2, "value":"B"},
{"id":3, "value":"C"}
]