Skip to content

Instantly share code, notes, and snippets.

View fancellu's full-sized avatar

Dino Fancellu fancellu

View GitHub Profile
@fancellu
fancellu / .block
Last active June 25, 2024 11:37
Force directed graph for D3.js v4 with labelled edges and arrows
license: gpl-3.0
height: 600
@fancellu
fancellu / tokio_spawn.rs
Last active May 31, 2024 22:26
Rust example of tokio::spawn and JoinHandle processing
async fn hello(name: &str) -> String {
// pretend to be doing some work
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
format!("Hello {}", name)
}
fn blocking() -> String {
println!("Blocking");
@fancellu
fancellu / BreakDemo.scala
Created April 25, 2024 17:52
Scala3 boundary/break/label demo (uses Scala 3.4.1)
import scala.annotation.targetName
import scala.util.boundary
import scala.util.boundary.{Label, break}
// Works in Scala 3.4.1
def firstIndex[T](xs: List[T], p: T): Int = {
boundary:
for (x, i) <- xs.zipWithIndex do if (x == p) break(i)
-1
@fancellu
fancellu / add_dicts_list.py
Created April 13, 2024 21:59
Python dictionary list adder
def add_dicts_list(dict_list):
"""
This function adds a list of dictionaries element-wise.
Args:
dict_list: A list of dictionaries with k->int elements.
Returns:
A new dictionary with the sum of the corresponding elements from all dictionaries in dict_list.
"""
@fancellu
fancellu / streamlit HF demo space
Created March 13, 2024 12:20
Streamlit sentiment analysis app on Huggingface spaces
https://huggingface.co/spaces/Fancellu/streamlit-demo
@fancellu
fancellu / image_renamer.py
Created March 2, 2024 12:02
Python Hugging Face image renamer using BeitForImageClassification (useful when you have thousands of badly named images)
from PIL import Image
from transformers import BeitImageProcessor, BeitForImageClassification
import os
# Define the directory containing images
image_dir = "e:/media"
# Load the model and processor
processor = BeitImageProcessor.from_pretrained('microsoft/beit-base-patch16-224-pt22k-ft22k')
model = BeitForImageClassification.from_pretrained('microsoft/beit-base-patch16-224-pt22k-ft22k')
@fancellu
fancellu / tokio_sem.rs
Last active March 2, 2024 12:04
Rust tokio Semaphore example
use std::sync::Arc;
use tokio::sync::{Semaphore, SemaphorePermit};
use tokio::time::sleep;
use tokio::time::Duration;
async fn person(sem: Arc<Semaphore>, name: String) {
println!("Person {}: waiting for a permit", name);
teller(sem, &name).await;
println!("\tPerson {} finished", name);
}
@fancellu
fancellu / tokio_notify_demo.rs
Created February 26, 2024 17:22
Rust tokio demo of Notify usage
use std::sync::Arc;
use tokio::sync::Notify;
use tokio::time::sleep;
use tokio::time::Duration;
// Notify can be thought of a Semaphore with 0 permits
async fn order_packages(package_delivered: Arc<Notify>) {
sleep(Duration::from_secs(2)).await;
println!("Company: Find package");
@fancellu
fancellu / tokio_barrier_demo.rs
Last active March 2, 2024 12:03
Rust tokio barrier demo
use std::sync::Arc;
use tokio::sync::Barrier;
use tokio::sync::BarrierWaitResult;
use tokio::sync::Notify;
use tokio::time::sleep;
use tokio::time::Duration;
// Simulating the filling of boxes with 5 cans, from many tasks
async fn barrier_wait(barrier: Arc<Barrier>, notify: Arc<Notify>, id: usize) -> BarrierWaitResult {
@fancellu
fancellu / rw_lock_demo.rs
Created February 27, 2024 11:36
Rust tokio demo of RwLock
use std::sync::Arc;
use tokio::sync::RwLock;
use tokio::time::sleep;
use tokio::time::Duration;
// Demonstrates RwLock with a shared resourced
async fn use_read_lock(id: i32, lock: Arc<RwLock<String>>) {
let lock = lock.read().await;
println!("reader: {} lock string: {}", id, lock);