Skip to content

Instantly share code, notes, and snippets.

View dhilst's full-sized avatar
😻

Daniel Hilst dhilst

😻
View GitHub Profile
def get(obj, attr, default=None):
"""
Fetch an attribute deeply nested on an object or dict, return `default` if not found
>>> class Foo: pass
>>> f = Foo()
>>> f.a = Foo()
>>> f.a.b = Foo()
>>> f.a.b.c = True
# test with `nc -kl 3000`
# write to nc terminal to send data
import socket
import sys
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Socket successfully created")
except socket.error as err:
print("socket creation failed with error %s" %(err))
@dhilst
dhilst / foo.rs
Last active April 10, 2020 05:33
Rust simple rate limiter
use std::time::{Duration, Instant};
struct Limiter {
instant: Option<Instant>,
duration: Duration,
}
impl Limiter {
pub fn new(secs: u64) -> Limiter {
Limiter {
@dhilst
dhilst / main.rs
Last active March 29, 2020 17:32
#![allow(unused_variables)]
use futures::future::LocalBoxFuture;
use futures::prelude::*;
use std::path::Path;
use tokio::fs::*;
fn walkdir<'a, C>(path: &'a Path, cb: &'a mut C) -> LocalBoxFuture<'a, ()>
where
C: Fn(&DirEntry),
{
1: ~/.config/nvim/init.vim
2: ~/.local/share/nvim/site/autoload/plug.vim
3: /usr/local/Cellar/neovim/0.4.3/share/nvim/runtime/filetype.vim
4: ~/.vim/plugged/vim-fugitive/ftdetect/fugitive.vim
5: ~/.vim/plugged/vim-javascript/ftdetect/flow.vim
6: ~/.vim/plugged/vim-javascript/ftdetect/javascript.vim
7: ~/.vim/plugged/typescript-vim/ftdetect/typescript.vim
8: ~/.vim/plugged/ultisnips/ftdetect/snippets.vim
use futures::future::Future;
struct MyStruct {
msg: String,
}
impl MyStruct {
fn new(msg: String) -> Self { MyStruct{ msg } }
}
#![allow(unused_imports)]
use super::config::Machine;
use super::email;
use futures::future::join_all;
use log;
use std::io;
use std::process::{Command, ExitStatus};
use tokio::task::spawn_blocking;
fn run_cmd(cmd: &'static str, args: Vec<&str>) -> io::Result<ExitStatus> {
@dhilst
dhilst / foo.py
Last active March 19, 2020 02:00
monkey patch example
from unittest import TestCase, main
from unittest.mock import patch
# import monkeypatch
from logging import getLogger
log = getLogger('foo')
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
/*
* You can get the offset of a member on a struct by dereferencing that member on
* address 0 of such structure.
*/
@dhilst
dhilst / foo.go
Last active January 23, 2020 08:57
package content
import (
"fmt"
"net/http"
"github.com/ponzu-cms/ponzu/management/editor"
"github.com/ponzu-cms/ponzu/system/item"
)