Skip to content

Instantly share code, notes, and snippets.

View Stiivi's full-sized avatar

Stefan Urbanek Stiivi

View GitHub Profile
@Stiivi
Stiivi / b2-left_inner_join.py
Created April 17, 2013 21:45
Left inner join in Brewery2 – three different joins: * SQL with iterable * SQL with SQL in the same store * SQL with SQL in another store Demonstrates multiple dispatch.
from brewery import data_object, FieldList, open_store
from brewery import kernel as k
import brewery.base.iterator
import brewery.backends.sql
FACT_DATA = [
[0, 1, 100.0],
[1, 1, 200.0],
[2, 2, 150.0],
[3, 2, 250.0],
@Stiivi
Stiivi / operation_dispatch.py
Last active December 16, 2015 05:39
Type dispatch in Python
k = OperationKernel()
@k.operation("sql", "sql")
def append(l, r):
if l.engine == r.engine:
return "UNION ALL statement"
else:
raise RetryOperation(["sql", "rows"])
@k.operation("sql", "rows")

Cubes Datamart Package

Purpose: sharing of smaller datasets - whole cubes or portions of larger cubes.

Datamart package

Package can be a directory or a ZIP file with following structure:

@Stiivi
Stiivi / sphinx.conf
Created February 21, 2013 08:22
Example sphinx configuration for cubes-search
source CUBE_NAME
{
type = xmlpipe2
xmlpipe_command = slicer-indexer --engine sphinx slicer.ini CUBE_NAME
}
index CUBE_NAME
{
source = CUBE_NAME
path = index/CUBE_NAME
@Stiivi
Stiivi / apriori_toy.py
Created February 7, 2013 10:58
Data toy: Apriori algorithm in Python
from collections import Counter
from itertools import combinations
def distinct_items(transactions, support=None):
"""Returns counted set of distinct items in transactions"""
counter = Counter()
for trans in transactions:
counter.update(trans)
if support is not None:
(
{
on = 1;
replace = "(c)";
with = "\U00a9";
},
{
on = 0;
replace = "(r)";
with = "\U00ae";
@Stiivi
Stiivi / sql_bulk_append.py
Created January 21, 2013 23:09
Classes for "bulk append" operations in SQL such as CREATE TABLE and INSERT from SELECT.
from sqlalchemy.sql.expression import Executable, ClauseElement
from sqlalchemy.ext.compiler import compiles
class CreateTableAsSelect(Executable, ClauseElement):
def __init__(self, table, select):
self.table = table
self.select = select
@compiles(CreateTableAsSelect)
def visit_create_table_as_select(element, compiler, **kw):
import brewery.stores as stores
filename = "data.csv"
source = stores.CSVDataSource(filename, encoding="latin1", infer_fields=True,
read_header=True)
target = stores.SQLDataTarget(url="sqlite:///data.sqlite",
table="data", create=True, fields=source.fields,
replace=True)
@Stiivi
Stiivi / three_dates_model_template.json
Created November 21, 2012 08:48
Three date dimensions in a cube
{
"cubes":[
{
"dimensions": [
"start_date",
"middle_date",
"end_date"
],
"joins": [
{"master":"start_date_id", "detail":"dim_date.id", "alias": "dim_start_date"},
@Stiivi
Stiivi / gist:4043388
Created November 9, 2012 02:43
Rickshaw Presenter Example
# Drill down by two dimensions
result = browser.aggregate(cell, drilldown=["year", "item"])
# Get presenter
rickshaw = cubes.create_presenter("rickshaw_multi_series")
# Create Rickshaw series.
# You can use color_map to map series keys to colors or you can use color_palette to
# cycle through colors