Skip to content

Instantly share code, notes, and snippets.

View mirekfranc's full-sized avatar
🌚
Rien

Miroslav Franc mirekfranc

🌚
Rien
  • Prague, Czech Republic
View GitHub Profile
@mirekfranc
mirekfranc / reducing_collection_of_predicates.py
Last active August 29, 2015 14:19
Reducing collection of predicates to a single predicate
def all_p (ps):
def __predicate (*args, **kwargs):
return all (p(*args, **kwargs) for p in ps)
return __predicate
def any_p (ps):
def __predicate (*args, **kwargs):
return any (p(*args, **kwargs) for p in ps)
return __predicate
@mirekfranc
mirekfranc / tentative_definition.c
Created April 24, 2015 20:08
One difference between C and C++
#include <stdio.h>
int a; /* tentative definition (in C, not C++)*/
int a = 42;
int main (void)
{
printf ("%d\n", a);
return 0;
}
@mirekfranc
mirekfranc / containter_access.cpp
Created April 24, 2015 20:12
dispatch based on type of the iterator the container supports
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <unordered_set>
#include <forward_list>
#include <iterator>
template<typename C>
@mirekfranc
mirekfranc / users_and_their_shells.cpp
Created April 24, 2015 20:17
Which user uses which shell on unix in C++11
#include <iostream>
#include <map>
#include <string>
#include <sys/types.h>
#include <pwd.h>
int
main (int argc, char *argv[])
{
std::map<std::string, std::string> nameshell;
@mirekfranc
mirekfranc / postgres_in_perl.pl
Created April 24, 2015 20:23
How to access postresql from within perl script
#!/usr/bin/env perl
# yum -y install perl-DBD-Pg
use DBI;
use warnings;
$HOST = 'localhost';
$NAME = 'db';
@USER_RO = ('readonly', 'guest');
@mirekfranc
mirekfranc / reconstruct_etc_passwd.c
Created April 24, 2015 20:34
Reconstructing /etc/passwd based on API, in case of NIS or LDAP, the results will correctly differ
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include <string.h>
#include <unistd.h>
int
main (int argc, char *argv[])
{
const int s = 4096;
@mirekfranc
mirekfranc / report_sizes.c
Created April 24, 2015 20:40
Report file sizes :)
#include <sys/stat.h>
#include <stdio.h>
int
main (int argc, char *argv[])
{
struct stat t;
while (*++argv != NULL)
{
@mirekfranc
mirekfranc / time_format.py
Created April 24, 2015 20:52
HH:MM:SS time formatting
def time_format (seconds):
m, s = divmod (seconds, 60)
h, m = divmod (m, 60)
if h:
return "{0:2}:{1:02}:{2:02}".format (h, m, s)
elif m:
return "{0:5}:{1:02}".format (m, s)
else:
return "{0:8}".format (s)
@mirekfranc
mirekfranc / colors_in_terminal.py
Last active August 29, 2015 14:19
How to do colors in terminal from within python script
#!/usr/bin/env python
import sys, os
from collections import namedtuple
Color = namedtuple ("Color", "PURPLE CYAN DARKCYAN BLUE GREEN YELLOW RED BOLD UNDERLINE END")
color = Color ('\033[95m', '\033[96m', '\033[36m', '\033[94m', '\033[92m', '\033[93m', '\033[91m', '\033[1m', '\033[4m', '\033[0m')
def main ():
@mirekfranc
mirekfranc / meta.hpp
Created April 24, 2015 21:14
experiments with template metaprogramming based on one cppcon talk
#ifndef META_H
#define META_H
namespace mirek
{
#include <climits>
// abs ////////////////////////////////////////////////////////////////////////////////
template<int N>