Skip to content

Instantly share code, notes, and snippets.

View daGrevis's full-sized avatar
⌨️
Keyboard operator

Raitis Stengrevics daGrevis

⌨️
Keyboard operator
View GitHub Profile
@daGrevis
daGrevis / round_to_number.py
Last active January 1, 2016 17:09
Round to number
def round_to_number(value, rounding_number):
remainder = value % rounding_number
rounded_down = value - remainder
if remainder >= rounding_number / 2:
return rounded_down + rounding_number
return rounded_down
assert round_to_number(1234, 500) == 1000
assert round_to_number(1250, 500) == 1500
@daGrevis
daGrevis / pwgen_example.sh
Last active January 2, 2016 05:19
Generates random yet pronounceable password
pwgen --ambiguous --capitalize --numerals --symbols 16
@daGrevis
daGrevis / edit_text_with_editor.py
Last active January 2, 2016 11:39
Edit text with editor
import os
import subprocess
import tempfile
def edit_text_with_editor(text=""):
_, location_for_temp_file = tempfile.mkstemp()
with open(location_for_temp_file, "r+") as temp_file:
temp_file.write(text)
temp_file.flush()
(defmacro if2 [expr cond1 cond2]
`(get ~{true `'cond1 false `'cond2} ~expr))
(println (if2 (= 4 (+ 2 2))
(println "true")
(println "false")))
@daGrevis
daGrevis / quicksort.clj
Last active August 29, 2015 13:56
Quicksort in Clojure
(defn pivot-and-rest [coll]
(let [splitted (split-at (quot (count coll) 2) coll)
second-part (second splitted)]
[(first second-part) (concat (first splitted) (rest second-part))]))
(defn quicksort [coll]
(cond
(empty? coll) []
(= (count coll) 1) coll
:else (let [[p xs] (pivot-and-rest coll)
@daGrevis
daGrevis / bat_widget.lua
Last active August 29, 2015 13:56
Battery widget for awesome (using acpi)
bat_widget = wibox.widget.textbox()
function set_bat(bat_widget)
local s = ""
local output = io.popen("acpi"):read()
if not output then
s = "N/A"
bat_widget:set_text(s)
return
end
local percentage = string.match(output, ".-(%d+)%%")
@daGrevis
daGrevis / closure.py
Created March 5, 2014 09:59
Counter using closure and non-local in Python 3
def make_counter(n):
def inner():
nonlocal n
n = n + 1
return n
return inner
counter = make_counter(0)
@daGrevis
daGrevis / url.regex
Last active August 29, 2015 13:57
Will match 99% URLs
/
(?:(\w+)\:?\/\/)? # Protocol
([^\/?]+) # Optional subdomains, domain and TLD
\/? # Traling slash
([^\?]*) # Path
([^\#]*) # Query
(\#?.*) # Fragment
/xu
# http://regex101.com/r/oV5tN9
@daGrevis
daGrevis / gist:11156962
Last active August 29, 2015 14:00
Decent HN (Stylish CSS)
@namespace url(http://www.w3.org/1999/xhtml);
@-moz-document domain("news.ycombinator.com") {
td[bgcolor="#ff6600"] {
background-color: #efefef !important;
border-radius: 4px;
}
a[href="news"] {
@daGrevis
daGrevis / counter.cljs
Last active August 29, 2015 14:02
Counter w/ ClojureScript and Om
(ns counter.core
(:require [om.core :as om :include-macros true]
[om.dom :as dom :include-macros true]))
(enable-console-print!)
(def app-state (atom {:count 42}))
(defn dec-button [counter owner]
(reify