Skip to content

Instantly share code, notes, and snippets.

View jmsdnns's full-sized avatar

Jms Dnns jmsdnns

View GitHub Profile
@jmsdnns
jmsdnns / isitfluent.py
Created September 17, 2024 06:13
Fluent interfaces technically return self or this from each chained function. Pandas returns new instances of the same type. Technically, that isn't fluent. I suspect it is meant to be indistinguishable for optimization reasons.
#!/usr/bin/env python
import pandas as pd
def getDataframe(url_table, ind):
df = pd.read_html(url_table)[ind]
return df
@jmsdnns
jmsdnns / rust_parse.log
Last active September 3, 2024 21:43
Pretty printed the way Rust's parse_macro_input! parses a simple struct. Handy to know the full thing for pattern matching.
// rust has pretty print support for printing structures. this is how rust's proc_macros
// parse the following struct:
//
// pub struct Book {
// id: u64,
// title: String,
// pages: u64,
// author: String,
// }
//
@jmsdnns
jmsdnns / simple_orm.rs
Last active August 31, 2024 06:21
Trying out Rust's proc macros by building a very basic ORM
use proc_macro::TokenStream;
use quote::quote;
use syn::{
parse_macro_input, Data::Struct, DataStruct, DeriveInput, Field, Fields::Named, FieldsNamed,
Path, Type, TypePath,
};
#[derive(Debug)]
struct DBModel {
name: String,
@jmsdnns
jmsdnns / vms.justfile
Created August 30, 2024 17:16
A simple config for managing groups of VMs using lima and just
default:
just -l
lsvm:
limactl ls
mkvm id:
limactl create \
--name="{{id}}" \
--vm-type=vz \
@jmsdnns
jmsdnns / async_password_hashing.rs
Last active September 15, 2024 09:12
An async password hasher that puts hashing in a background thread where it can safely block the cpu
// Output:
// HASH: "$argon2id$v=19$m=19456,t=2,p=1$vecn7S39OC1B7Ei0ZBfl1A$q94uWmUFrtuUhERwWCLkCGSO+CRZwGEWUTmJSO8wr0c"
// ACCESS VERIFIED
use anyhow::{Context, Result};
use argon2::{password_hash::SaltString, Argon2, PasswordHash};
async fn hash_password(password: String) -> Result<String> {
tokio::task::spawn_blocking(move || -> Result<String> {
let salt = SaltString::generate(rand::thread_rng());
@jmsdnns
jmsdnns / compose.go
Created August 29, 2024 12:24
basic example of how Go uses composition instead of inheritence
package main
import "fmt"
type Polygon struct {
Sides int
}
func (p *Polygon) NSides() int {
return p.Sides
@jmsdnns
jmsdnns / rust_traits.rs
Last active August 29, 2024 11:42
Basic example for understanding Rust traits
#![allow(clippy::upper_case_acronyms)]
// a trait for describing vehicles that can move on land
trait LandCapable {
// a function with no implementation acts as an interface
//fn drive(&self);
// default imlpementation
fn drive(&self) {
println!("Default drive");
#!/usr/bin/env python
# Output looks like this:
#
# $ python ./kmeans.py
# Centroids: [[14, 17], [17, 7], [3, 15]]
# Done: [[17.5, 16.5], [16.5, 4.166666666666667], [1.8, 1.6]]
# 0: [[15, 14], [18, 14], [20, 20], [17, 18]]
# 1: [[12, 10], [20, 0], [19, 2], [18, 1], [11, 9], [19, 3]]
# 2: [[0, 1], [1, 1], [2, 2], [3, 3], [3, 1]]
@jmsdnns
jmsdnns / mac_gpu.rs
Created August 17, 2024 05:01
Simple demo of how to use Candle with Apple's GPU
use candle_core::{Device, Tensor};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let device = Device::new_metal(0)?;
let a = Tensor::randn(0f32, 1., (2, 3), &device)?;
let b = Tensor::randn(0f32, 1., (3, 4), &device)?;
let c = a.matmul(&b)?;
println!("{c}");
@jmsdnns
jmsdnns / trying_ndarrays.rs
Created August 17, 2024 04:50
Exploring Rust's ndarrays to compare with numpy arrays
use ndarray::prelude::*;
use ndarray::Array;
use ndarray::{concatenate, stack, Axis};
use std::f64::INFINITY as inf;
fn main() {
let a = array![[10.,20.,30.,40.,]];
// [[10., 20., 30., 40.]]
println!("a {:?}", &a);
// [1, 4]