Skip to content

Instantly share code, notes, and snippets.

View gunchev's full-sized avatar

Doncho N. Gunchev gunchev

View GitHub Profile
@gunchev
gunchev / cpp_version_macro.cpp
Last active August 29, 2022 16:26
Using C++ preprocessor to detect C++ standard
#include <cstdio>
int main() {
// Also see https://en.cppreference.com/w/cpp/feature_test
// and https://en.cppreference.com/w/User:D41D8CD98F/feature_testing_macros
#ifndef __cplusplus
#error C++ is required
#elif __cplusplus < 199711L
puts("before C++98?!?");
#elif __cplusplus < 201103L
@gunchev
gunchev / unix_csv.py
Last active January 24, 2022 08:59
Python CSV with unix line terminators
#!/usr/bin/python
# -*- coding: utf-8 -*-
import csv
def main():
csv.register_dialect('unix', lineterminator='\n', quoting=csv.QUOTE_MINIMAL)
with open('unix.csv', 'w') as csv_file:
csv_writer = csv.writer(csv_file, dialect='unix')
@gunchev
gunchev / timed_log_colored.py
Last active December 13, 2021 14:35
Python 2 and 3 TimedFunction and TimedMethod plus colored log on terminal (black Linux terminal, konsole/terminator tested)
#!/usr/bin/python
# -*- encoding: utf-8 -*-
"""Timed function and method decorators, portions from the python decorator module
Install 'termcolor' (pip install --user termcolor) for colored output.
"""
from __future__ import absolute_import, print_function, division, unicode_literals
@gunchev
gunchev / terminalsize.py
Last active December 8, 2021 14:23 — forked from jtriley/terminalsize.py
Get current terminal size on Linux, Mac, and Windows
#!/usr/bin/python
'''
Get width and height of console
works on Linux, OS X, Windows, Cygwin(Windows) originally retrieved from:
http://stackoverflow.com/questions/566746/how-to-get-console-window-width-in-python
updated by Doncho Nikolaev Gunchev <dgunchev@gmail.com> to work with python 2 and 3, make pylint happy.
/*usr/bin/true; A="$(readlink -f -- "$0")"; function x { rm "$A.bin"; }; trap x EXIT; g++ --std=c++14 -Wall -Wno-unused-label "$A" -o "$A.bin"; "$A.bin"; E=$?; exit $E; # */
// This can be used as script - chmod +x this.cpp; ./this.cpp
#include <cstdio>
constexpr const size_t strlen_const(const char* s) {
return '\0' == *s ? 0 : strlen_const(s + 1) + 1;
}
int main() {
https://stackoverflow.com/a/25891133/1136400
@gunchev
gunchev / create_new_file_python2.py
Last active January 6, 2021 16:13
Exclusively create a new binary file in python 2, like 'x' mode in python 3
#!/usr/bin/python2
# -*- coding: utf-8 -*-
"""
>>> f1 = create_file('/tmp/test.bin')
>>> f2 = create_file('/tmp/test.bin')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in create_file
OSError: [Errno 17] File exists: '/tmp/test.bin'
>>> f1.close()