Skip to content

Instantly share code, notes, and snippets.

View fnielsen's full-sized avatar

Finn Årup Nielsen fnielsen

View GitHub Profile
@fnielsen
fnielsen / afinn.py
Last active May 6, 2021 11:41
Simplest sentiment analysis in Python with AFINN
#!/usr/bin/python
#
# (originally entered at https://gist.github.com/1035399)
#
# License: GPLv3
#
# To download the AFINN word list do:
# wget http://www2.imm.dtu.dk/pubdb/views/edoc_download.php/6010/zip/imm6010.zip
# unzip imm6010.zip
#
@fnielsen
fnielsen / defaultarguments.rb
Last active December 18, 2015 01:18
My first ruby program
def func1(f=nil)
print("In func1a\n")
return f
end
def func2(f=func1)
print("In func2a\n")
return f()
end
@fnielsen
fnielsen / defaultarguments.py
Last active December 18, 2015 01:18
Python evaluation of default arguments
def func1(f1=None):
print("In func1a")
return f1
def func2(f2=func1):
print("In func2a")
return f2()
def func1(f1=func2):
print("In func1b")
#!/usr/bin/python
# http://rainbowdash.net/notice/2764341
text = "@zeldatra I'm surprised you got my hair spot on though, " + \
"considering how I exaggerate it so much. " + \
"Thanks! Although I dunno why?"
import os.path
import re
@fnielsen
fnielsen / python-cli.py
Created July 10, 2013 18:17
Python interactive command-line interface to Python with coloring of '5'
import blessings, re, readline, rlcompleter
readline.parse_and_bind("tab: complete") # For tab completion
_term = blessings.Terminal() # For coloring text output
while True:
expr = raw_input(">>> ")
try:
_ = eval(expr)
print(re.sub('5', _term.bold_red_on_green('5'), str(_),
@fnielsen
fnielsen / mydocopter
Last active December 19, 2015 14:19
Demonstration program for Python docopt module. Here with fitting of a polynomial.
#!/usr/bin/env python
"""
mydocopter.
Usage: mydocopter [options] <filename>
Options:
-v --verbose Log messages
-o OUTPUT --output=OUTPUT Output file
-a <a> Initial coefficient for second order term [default: 1.]
@fnielsen
fnielsen / gist:6007683
Created July 16, 2013 10:48
Estonia Python e-election system.
#!/usr/bin/python2.7
# -*- coding: UTF8 -*-
"""
Copyright: Eesti Vabariigi Valimiskomisjon
(Estonian National Electoral Committee), www.vvk.ee
Written in 2004-2013 by Cybernetica AS, www.cyber.ee
This work is licensed under the Creative Commons
Attribution-NonCommercial-NoDerivs 3.0 Unported License.
@fnielsen
fnielsen / gist:6070697
Created July 24, 2013 13:43
Generators as pipes
def peak_to_peak(iterable):
"""
From an input iterable find the present maximum range. Only yield when the range gets larger
"""
it = iter(iterable)
first_value = it.next()
the_min = first_value
the_max = first_value
while True:
value = it.next()
@fnielsen
fnielsen / altman.py
Created July 31, 2013 23:21
This is a modified version of the timing code by Patrick Altman "Try / Except Performance in Python: A Simple Test" http://paltman.com/2008/01/18/try-except-performance-in-python-a-simple-test/
"""
This is a modified version of the timing code by
Patrick Altman "Try / Except Performance in Python: A Simple Test"
http://paltman.com/2008/01/18/try-except-performance-in-python-a-simple-test/
"""
import time
def time_me(function):
@fnielsen
fnielsen / python_control_strutures.py
Created September 9, 2013 09:04
Example on Python control flow/structures
xs = [ float(i)/64.0 for i in range(-150, 41) ]
ys = [ float(i)/16.0 for i in range(-25,26) ]
for y in ys:
s = ''
for x in xs:
z = 0j; i = 0
while i < 10:
z = z**2 + x+y*1j
if abs(z) > 2:
break # Get out of inner loop