This file contains hidden or 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 Product: | |
"""The Product object as needed by my application""" | |
def __init__(self, name: str, price: float, category: str): | |
self.name = name | |
self.price = price | |
self.category_name = category | |
def __repr__(self): | |
# string representation for debugging |
This file contains hidden or 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
// extension from https://gist.github.com/stellasia/a0ed4331999d8bb2d53903564b0fcfa6 | |
use std::fmt::Debug; | |
trait Cachable<T: Debug>: CachableKey { | |
fn serialize_and_print(&self) { | |
/// print serialized data for debugging | |
/// this trait method has a default implementation, that can | |
/// be overwritten in trait implementations if required | |
let data = self.serialize(); |
This file contains hidden or 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
// Item & CachableKey from https://gist.github.com/stellasia/a0ed4331999d8bb2d53903564b0fcfa6 | |
trait Cachable<T>: CachableKey { | |
/// T can be any type (String, HashMap or ...) | |
/// It is used to tell Rust that it will be the return type of the 'serialize' method | |
fn serialize(&self) -> T ; | |
} | |
// implement cache for T=String, ie the serialize method will return String | |
impl Cachable<String> for Item { |
This file contains hidden or 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
use std::collections::HashMap; | |
trait CachableKey { | |
fn get_key(&self) -> String; | |
} | |
trait CachableDataStr: CachableKey { | |
/// Serialize to string | |
/// Requires the trait CachableKey to be implemented on the struct | |
fn serialize(&self) -> String; |
This file contains hidden or 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 CacheMixin: | |
def get_key(self): | |
raise NotImplementedError() | |
def serialize(self): | |
raise NotImplementedError() | |
class Item(CacheMixin): |
This file contains hidden or 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 Transaction: | |
def execute_query(self, query: str): | |
# run query | |
pass | |
def close(self): | |
# close transaction | |
pass | |
This file contains hidden or 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
""" | |
Method decorators | |
""" | |
def decorate(param): | |
def decorated(meth): | |
"""Just attach an attribute to the decorated method""" | |
meth._is_decorated = True | |
meth._param = param |
This file contains hidden or 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 folium | |
center_point = [48.857456, 2.340145] | |
m = folium.Map( | |
location=center_point, | |
zoom_start=13 | |
) | |
# Add simple point markers |