Skip to content

Instantly share code, notes, and snippets.

View filipkral's full-sized avatar

Filip Kral filipkral

View GitHub Profile
@filipkral
filipkral / logging_to_sqlite_with_multiprocessing.py
Created October 26, 2016 12:46
Logging to SQLite with multiprocessing in Python
import sqlite3
from contextlib import closing
import multiprocessing
def prepare_db(db, tbl, col):
sql = "CREATE TABLE {0} ({1} text);".format(tbl, col)
with closing(sqlite3.connect(db)) as cnn:
cursor = cnn.cursor()
cursor.execute('DROP TABLE IF EXISTS {0};'.format(tbl))
cursor.execute(sql)
@filipkral
filipkral / python-cheat-sheet-basic.py
Last active March 12, 2023 12:30
Basic Python Cheat Sheet
#!/usr/bin/env python
"""Basic Python Cheat Sheet by Filip Kral on 2015/02/16"""
"""
Python is a cross-platform, interpreted, object-oriented programming language.
That means you can run it on Linux, Windows, Mac, and other platforms,
you don't need to compile your code to execute it because it is compiled on
the fly, and you can use classes and objects.
@filipkral
filipkral / convert_sp_geojson.r
Last active May 17, 2021 15:21
Convert between geojson and sp spatial objects in R
# Convert between geojson and sp spatial objects in R
require(rgdal)
# https://stat.duke.edu/~cr173/Sta523_Fa14/spatial_data.html
s <- '{ "type": "MultiPolygon", "coordinates": [
[ [[40, 40], [20, 45], [45, 30], [40, 40]] ],
[ [[20, 35], [10, 30], [10, 10], [30, 5], [45, 20], [20, 35]],
[[30, 20], [20, 15], [20, 25], [30, 20]]
]
]}'
@filipkral
filipkral / reduce_repo_size.sh
Last active October 10, 2017 19:05
Reduce repository size after you accidentally committed a large file.
# given that you accidentally added a large file to git branch feature/adding, then removed it in another commit, you can reduce the repo size by the following commands
# for more details and alternatives see https://confluence.atlassian.com/bitbucket/reduce-repository-size-321848262.html
git checkout develop
git merge --squash feature/adding
git branch -D feature/adding
#git reflog expire --expire=now --all
git gc --prune=now
git commit -m "Merged"
@filipkral
filipkral / read_and_write_spatialite.py
Created March 15, 2016 10:59
Read and Write Spatialite with Python
from osgeo import ogr, osr
def read_splt(db):
drvr = ogr.GetDriverByName('SQLite')
ds = drvr.Open(db, 0)
layer=ds.GetLayer('pts')
for feature in layer:
print(feature)
break
@filipkral
filipkral / snowfall_trivial.r
Last active March 5, 2016 12:09
Trivial example for parallel coputation using R snowfall package
# A trivial example for parallel computation using R snowfall package
require(snowfall)
sfInit(paralell=TRUE, cpus=2)
fn <- function(x){
return(x*x)
}
d <- 1:1000
result <- sfLapply(d, fn)
@filipkral
filipkral / gist:7441074
Created November 13, 2013 00:04
Stay on page. I use this to prevent me from leaving ArcGIS API for JavaScript Sandbox (http://developers.arcgis.com/en/javascript/sandbox/sandbox.html) accidentally by pressing escape when the focus is not on the code editor.
// to open a confirm dialog before you leave the page, enter this into the browser console (or the javascript code)
window.onbeforeunload = function(e){ var e = e || window.event; var msg ="Don't leave!"; if(e){e.returnvalue = msg;} return msg; }
@filipkral
filipkral / importing_osgeo_to_pyscripter.py
Created December 11, 2015 01:46
Importing OSGeo4W osgeo Python package to Pyscripter on Windows
import sys, os
sys.path.insert(0, r'C:\OSGeo4W\apps\Python27\Lib\site-packages')
#sys.path.insert(0, r'C:\OSGeo4W\lib')
sys.path.insert(0, r'C:\OSGeo4W\bin')
#os.environ['PATH'] = os.environ['PATH'] + ';' + r'C:\OSGeo4W\lib'
os.environ['PATH'] = os.environ['PATH'] + ';' + r'C:\OSGeo4W\bin'
@filipkral
filipkral / mini-esri-leaflet.html
Last active September 11, 2015 21:58
Minimal Leaflet Map
<!DOCTYPE html>
<html>
<head>
<title>Minimal Leaflet</title>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.5/leaflet.css" />
<style>
#map{
width: 150px;
height:250px;
}
@filipkral
filipkral / GitBasics
Created November 1, 2014 19:04
Basics of git
# https://confluence.atlassian.com/display/STASH/Basic+Git+commands
# simplest example
git clone https://github.com/some/repo repo
cd repo
# make changes to files
git commit -a -m "Commit message"
# you can check status by `git status`
git push origin master