Skip to content

Instantly share code, notes, and snippets.

View kpsychas's full-sized avatar

Konstantinos Psychas kpsychas

View GitHub Profile
@kpsychas
kpsychas / test_ipynb.py
Last active August 2, 2016 16:39
Customizable run of ipython notebook from command line. Requires runipy (https://github.com/paulgb/runipy) library
#!/usr/bin/env python
import argparse
from runipy.notebook_runner import NotebookRunner
from IPython.nbformat.current import read
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--notebook',
@kpsychas
kpsychas / abstract_init.py
Last active July 19, 2016 19:44
Recipe that uses abstract properties and abstract initialization in Python 2
#!/usr/bin/env python
from abc import ABCMeta, abstractmethod, abstractproperty
def get_fromcls(cls, num):
return cls.myinit(num)
class Abs(object):
__metaclass__ = ABCMeta
@kpsychas
kpsychas / save_fig.m
Last active October 19, 2015 20:54
A file that can act as a template to create a non trivial Matlab plot and save it to png
close all; clear all;
t = -5*pi:1e-3:5*pi;
y1 = sin(t);
y2 = cos(t);
y3 = 0.6*y1 + 0.8*y2;
y4 = 0.8*y1 + 0.6*y2;
figure('Name','Graph','Color','w','Position',[10 30 600 400]);
set(subplot(2,1,1),'ylabel',text('string', 'Amplitude'),...

Working with 2 remotes

This tutorial will show how to setup a local repository for working with 2 remote repositories. The assumption is that we want to pull and push certain features to a public repository, while having a private repository for independent development, that will only merge changes from the public.

Setting up public repository

First clone the public repository and move into the project folder.

@kpsychas
kpsychas / label_count.py
Last active December 2, 2015 02:15
Count number of labels in a Brain XML file (http://www.thebrain.com/)
#!/usr/bin/env python
'''
Count number of labels in a Brain XML file
(http://www.thebrain.com/)
'''
import xml.etree.ElementTree as ET
if __name__ == '__main__':
tree = ET.parse('MyBrain.xml')
@kpsychas
kpsychas / strategy_recipe.py
Last active November 22, 2019 06:56
Strategy pattern example in python with two different ways of providing a strategy
import types
from functools import partial
class Resource1(object):
def __init__(self, value, update_strategy):
self.value = value
self.update = types.MethodType(update_strategy, self)
class Resource2(object):
@kpsychas
kpsychas / equality_override.py
Last active August 3, 2016 19:39
Example of overriding object equality in python
from collections import Counter
class A(object):
def __init__(self, name, val):
self.name = name
self.val = val
def __repr__(self):
return 'A(val={})'.format(self.val)
@kpsychas
kpsychas / bridge_pattern.py
Last active November 22, 2019 07:01
The bridge design pattern retrieved from stackoverflow archived documentation
class Spellbook:
def cast(self, enemy):
raise NotImplementedError
class SpellbookFire(Spellbook):
def cast(self, enemy):
print("I've lit the {} on fire.".format(enemy))
@kpsychas
kpsychas / git_loc_stats.py
Last active May 16, 2017 18:58
Python script for plotting LoC statistics
#!/usr/bin/python
"""
Display the per-commit size of the current git branch.
source: http://stackoverflow.com/questions/23907/how-can-i-graph-the-lines-of-code-history-for-git-repo
"""
import subprocess
import re
import sys
@kpsychas
kpsychas / fancy_plot.py
Last active March 21, 2020 17:19
Fancy Plot
import matplotlib.pyplot as plt
import numpy as np
def myplot(q, t, label, color=None, mean_in_legend=False, linewidth=0.5):
if mean_in_legend:
mean_total_q = np.dot(t, q) / t.sum()
plt.plot(t.cumsum(), q, color=color,
label='{} {:.2f}'.format(label, mean_total_q),
linewidth=linewidth)