Skip to content

Instantly share code, notes, and snippets.

View stellasia's full-sized avatar
🚀

Estelle Scifo stellasia

🚀
View GitHub Profile
@stellasia
stellasia / domain.py
Last active March 7, 2022 10:13
Cypher Map Projection Example: OGM
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
@stellasia
stellasia / cache.rs
Last active February 25, 2022 16:22
Medium_From_Python_To_Rust_Trait_Cache_3
// 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();
@stellasia
stellasia / generics.rs
Last active February 25, 2022 16:22
Medium_From_Python_To_Rust_Cache_with_Generics_1
// 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 {
@stellasia
stellasia / cache.rs
Last active February 25, 2022 16:30
Medium_From_Python_To_Rust_Trait_Cache_2
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;
@stellasia
stellasia / cache.py
Last active February 25, 2022 16:29
Medium_From_Python_To_Rust_Trait_Cache
class CacheMixin:
def get_key(self):
raise NotImplementedError()
def serialize(self):
raise NotImplementedError()
class Item(CacheMixin):
@stellasia
stellasia / transaction.py
Last active February 24, 2022 14:19
Medium_From_Python_To_Rust_Transaction
class Transaction:
def execute_query(self, query: str):
# run query
pass
def close(self):
# close transaction
pass
"""
Method decorators
"""
def decorate(param):
def decorated(meth):
"""Just attach an attribute to the decorated method"""
meth._is_decorated = True
meth._param = param
@stellasia
stellasia / basic_folium_map.py
Created January 8, 2021 13:07
Create a map with Python and folium
import folium
center_point = [48.857456, 2.340145]
m = folium.Map(
location=center_point,
zoom_start=13
)
# Add simple point markers