Skip to content

Instantly share code, notes, and snippets.

#include <stdio.h>
#include <dirent.h>
int main(int argc, char* argv[]) {
int file_count = 0;
DIR * dirp;
struct dirent * entry;
dirp = opendir(argc == 2 ? argv[1] : "."); /* There should be error handling after this */
if (dirp == NULL) {
#!/usr/bin/env ruby
n = ARGV.shift.to_i
puts((n.times.map do |i|
" "*(n-1-i) + "*"*(1+2*i)
end + (n/2).times.map do
" "*(n/2) + "#"*(n-1+(n%2))
end).join("\n"))
@hugopeixoto
hugopeixoto / checked_static_cast.hpp
Last active June 16, 2022 03:50
checked static cast - checks during runtime if there is a value overflow/underflow when using static_cast
#ifndef CHECKED_STATIC_CAST_H_
#define CHECKED_STATIC_CAST_H_
#include <limits>
namespace checked_static_cast {
template<typename T, typename U, bool S>
struct StaticCast { };
template<typename T, typename U>
@hugopeixoto
hugopeixoto / yaml_conf.py
Created May 18, 2014 13:06
Retrieves configuration from a directory containing yaml configuration files
def read_yaml_configuration_directory(directory):
config = {}
for root, _, filenames in os.walk(directory):
for fname in filenames:
if fname.endswith(".conf"):
with open(root + "/" + fname) as f:
config.update(yaml.safe_load(f))
return config
@hugopeixoto
hugopeixoto / pid.py
Last active August 29, 2015 14:01
pidfile management
import os
# usage:
# with Pid("/var/run/potato.pid"):
# do_stuff()
def is_running(pid):
try:
os.kill(pid, 0)
except OSError:
@hugopeixoto
hugopeixoto / btc.sh
Last active August 29, 2015 13:57
btc.sh
#!/bin/bash
btce_price() {
python -c 'import json; import sys; print json.load(sys.stdin)["markets"]["btce"]["price"]'
}
AMOUNT=${1:-1.0}
USD=$(curl -sf http://preev.com/pulse/source:btce/unit:btc,usd | btce_price)
EUR=$(curl -sf http://preev.com/pulse/source:btce/unit:btc,eur | btce_price)
@hugopeixoto
hugopeixoto / worker.py
Created March 9, 2014 20:25
metachains_dtc worker
import cloudmanager
import metachains_dtc
import settings
coin = metachains_dtc.Datacoin(
settings.DATACOIN_URL,
settings.DATACOIN_USERNAME,
settings.DATACOIN_PASSWORD)
@hugopeixoto
hugopeixoto / inherited.rb
Created March 5, 2014 23:59
inheritance hooks
class PluginBase
def self.inherited child
# Register 'child' in PluginList
puts "ooh sweet #{child} o' mine"
end
end
class X < PluginBase
end
function safe_component($component) {
return !!preg_match("/^([a-zA-Z0-9_]+\.?)+$/", $component);
}
@hugopeixoto
hugopeixoto / pinger.py
Created December 13, 2013 20:55
Pinger
class Pinger (threading.Thread):
def __init__(self, reporter):
self.reporter = reporter
threading.Thread.__init__()
daemon = True
def run (self):
while True:
self.reporter.ping()
time.sleep(60)