Skip to content

Instantly share code, notes, and snippets.

View co89757's full-sized avatar

gueuz co89757

  • United States
View GitHub Profile
@co89757
co89757 / parseargs.py
Created July 19, 2017 19:22
argparse module of Python usage example
## Demonstrate how to use argparse in Python
import argparse
parser = argparse.ArgumentParser(
description= "description of the command",
epilog = "something optional you want to print at the end of summary"
)
## ------ .add_argument() method -----
# keyword arguments for this method:
@co89757
co89757 / emailer.go
Created June 5, 2017 06:48
Email sending using Go
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"log"
"net/smtp"
@co89757
co89757 / makefile.makefile
Created May 16, 2017 06:59
Generic makefile
CXX ?= g++
# path #
SRC_PATH = src
BUILD_PATH = build
BIN_PATH = $(BUILD_PATH)/bin
# executable #
BIN_NAME = runner
@co89757
co89757 / conv_num_str.c
Last active August 3, 2016 05:46
Redis number-string conversion optimized
/* Return the number of digits of 'v' when converted to string in radix 10.
* See ll2string() for more information. */
uint32_t digits10(uint64_t v) {
if (v < 10) return 1;
if (v < 100) return 2;
if (v < 1000) return 3;
if (v < 1000000000000UL) {
if (v < 100000000UL) {
if (v < 1000000) {
if (v < 10000) return 4;
@co89757
co89757 / glob_match.c
Created August 3, 2016 05:39
glob pattern matching
/* Glob-style pattern matching. */
int stringmatchlen(const char *pattern, int patternLen,
const char *string, int stringLen, int nocase)
{
while(patternLen) {
switch(pattern[0]) {
case '*':
while (pattern[1] == '*') {
pattern++;
patternLen--;
@co89757
co89757 / remove_if.c
Created August 3, 2016 00:55
Remove linked list node with 2nd level pointer
void remove_if(node ** head, remove_fn rm)
{
for (node** curr = head; *curr; )
{
node * entry = *curr;
if (rm(entry))
{
*curr = entry->next;
free(entry);
}
@co89757
co89757 / readlne.js
Created July 24, 2016 07:23
interactively read from stdin with a prompt
var readline = require('readline');
var rl = readline.createInterface(process.stdin, process.stdout);
rl.setPrompt('guess> ');
rl.prompt();
rl.on('line', function(line) {
if (line === "right") rl.close();
rl.prompt();
}).on('close',function(){
process.exit(0);
});
@co89757
co89757 / optparse_sample.py
Created June 25, 2016 04:55
parsing command line options with optparse
#!/usr/bin/python
# -*- coding: utf-8 -*-
from optparse import OptionParser
## parser.add_option('<short-hand>', "<long-hand>", dest = <binding attribute>, type=<type>, action=<action>, default=<default_value> )
## four most important option attributes:
# action("store"(default), 'store_false/true' (setting boolean flag), 'store_const', 'append' (append this opt's arg to a list), 'callback' (invoke a callback function) ),
# type ("string","int",'float',... )
# dest (the attribute name of parsed options object)
# help (help string for this option)
@co89757
co89757 / print_stacktrace.cc
Last active November 20, 2017 21:23
Print pretty stack trace for C++ under LINUX
// stacktrace.h (c) 2008, Timo Bingmann from http://idlebox.net/
// published under the WTFPL v2.0
#ifndef _STACKTRACE_H_
#define _STACKTRACE_H_
#include <stdio.h>
#include <stdlib.h>
#include <execinfo.h>
#include <cxxabi.h>
@co89757
co89757 / zero-length-array.md
Created May 3, 2016 05:13
variable length array hack in C

Your intution about "why not use an array of size 1" is spot on.

The code is doing the "C struct hack" wrong, because declarations of zero length arrays are a constraint violation. This means that a compiler can reject your hack right off the bat at compile time with a diagnostic message that stops the translation.

If we want to perpetrate a hack, we must sneak it past the compiler.

The right way to do the "C struct hack" (which is compatible with C dialects going back to 1989 ANSI C, and probably much earlier) is to use a perfectly valid array of size 1:

struct someData
{