64
Brewery Documentation Release 0.5.0 Stefan Urbanek February 21, 2011

Brewery: The Data Framework - Library Reference

Embed Size (px)

DESCRIPTION

Reference documentation (auto-generated) for Brewery python framework. Brewery is a framework for data mining, data quality monitoring, data cleansing and data analysis.

Citation preview

Page 1: Brewery: The Data Framework - Library Reference

Brewery DocumentationRelease 0.5.0

Stefan Urbanek

February 21, 2011

Page 2: Brewery: The Data Framework - Library Reference
Page 3: Brewery: The Data Framework - Library Reference

CONTENTS

1 Introduction 3

2 Installation 5

3 Data Streams 73.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73.2 Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

4 Data Quality 114.1 API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

5 Data Pipes and Data Processing Streams 135.1 Data Processing Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135.2 Running Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

6 Node Reference 156.1 Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156.2 Record Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176.3 Field Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226.4 Targets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

7 Command Line Tools 297.1 brewery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297.2 mongoaudit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

8 Examples 338.1 Merge multiple CSV (or XLS) Files with common subset of columns into one CSV . . . . . . . . . . 338.2 Stream: Append Sources, Clean, Store and Audit . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

9 Brewery API 419.1 Datastores and Data Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419.2 Data Quality API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489.3 Data Pipes and Data Processing Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

10 Contact and Getting Help 53

11 Indices and tables 55

Python Module Index 57

Index 59

i

Page 4: Brewery: The Data Framework - Library Reference

ii

Page 5: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

Contents:

CONTENTS 1

Page 6: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

2 CONTENTS

Page 7: Brewery: The Data Framework - Library Reference

CHAPTER

ONE

INTRODUCTION

Brewery is a framework for:

• data streams - streaming structured data from various sources into various targets. Example of existing streams:CSV, XLS, Google Spreadsheet, relational SQL database, MongoDB, directory with YAML files, HTML, ...

• data quality monitoring

• data mining

3

Page 8: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

4 Chapter 1. Introduction

Page 9: Brewery: The Data Framework - Library Reference

CHAPTER

TWO

INSTALLATION

Main project source repository is being hosted at Bitbucket: https://bitbucket.org/Stiivi/brewery

Clone mercurial repository from bitbucket:

hg clone https://bitbucket.org/Stiivi/brewery

Or if you prefer git, then you can clone it from Github:

git clone git://github.com/Stiivi/brewery-py.git

Note: File packages for download will be available on first release.

Install:

python setup.py install

5

Page 10: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

6 Chapter 2. Installation

Page 11: Brewery: The Data Framework - Library Reference

CHAPTER

THREE

DATA STREAMS

3.1 Overview

Data streams provide interface for common way of reading from and writing to various structured data sources. Withstreams you can easily read CSV file and merge it with Excel spreadsheet or Google spreadsheet, then perform cleans-ing and write it to a relational database table or create a report.

Figure 3.1: Example of data streams.

The streams can be compared to file-like stream objects where structured data is being passed instead of bytes. Thereare two ways how to look at structured data: as a set of lists of values or as a set of key-value pairs (set of dictionaries).Some sources provide one or the other way of looking at the data, some processing is better with the list form, anothermight be better with the dictionary form. Brewery allows you to use the form which is most suitable for you.

3.2 Metadata

At any time you are able to retrieve stream metadata: list of fields being streamed. For more information see docu-mentation for brewery.ds.Field where you can find list of field attributes, data storage types, analytical typesand more.

7

Page 12: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

Figure 3.2: Example of streaming data as sequence of rows - tuples of values.

Figure 3.3: Example of streaming data as sequence of records - dictionaries with key-value pairs.

8 Chapter 3. Data Streams

Page 13: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

3.2.1 Data Sources

Data source Description Dataset referencecsv Comma separated values (CSV) file/URI resource file path, file-like object, URLxls MS Excel spreadsheet file path, URLgdoc Google Spreadsheet spreadsheet key or namerdb Relational database table (using DB API 2 interface) connection + table namemongodb MongoDB database collection connection + table nameyamldir Directory containing yaml files - one file per record directoryjsondir Directory containing json files - one file per record directory

See Also:

Class brewery.ds.DataSource Data source documentation

3.2.2 Data Targets

Data target Descriptioncsv Comma separated values (CSV) file/URI resourcerdb Relational database tablemongodb MongoDB database collectionyamldir Directory containing yaml files - one file per recordjsondir Directory containing json files - one file per recordhtml HTML file or a string target

relational database table non-relational database collection

See Also:

Class brewery.ds.DataTarget Data target documentation

3.2.3 API

Data sources should implement:

• initialize() - delayed initialisation: get fields if they are not set, open file stream, ...

• rows() - returns iterable with value tuples

• records() - returns iterable with dictionaries of key-value pairs

Should provide property fields, optionally might provide assignment of this property.

Data targets should implement:

• initialize() - create dataset if required, open file stream, open db connection, ...

• append(object) - appends object as row or record depending whether it is a dictionary or a list

See Also:

Module brewery.ds API Documentation for data streams objects

3.2. Metadata 9

Page 14: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

10 Chapter 3. Data Streams

Page 15: Brewery: The Data Framework - Library Reference

CHAPTER

FOUR

DATA QUALITY

Functions and classes for measuring data quality.

Example of auditing a CSV file:

import brewery.ds as dsimport brewery.dq as dq

# Open a data streamsrc = ds.CSVDataSource("data.csv")src.initialize()

# Prepare field statisticsstats = {}fields = src.field_names

for field in fields:stats[field] = dq.FieldStatistics(field)

record_count = 0

# Probe valuesfor row in src.rows():

for i, value in enumerate(row):stats[fields[i]].probe(value)

record_count += 1

# Finalize statisticsfor stat in stats.items():

finalize(record_count)

Auditing using brewery.ds.StreamAuditor:

# ... suppose we have initialized source stream as src

# Create autitor stream target and initialize field listauditor = ds.StreamAuditor()auditor.fields = src.fieldsauditor.initialize()

# Perform audit for each row from source:for row in src.rows():

auditor.append(row)

11

Page 16: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

# Finalize results, close files, etc.auditor.finalize()

# Get the field statisticsstats = auditor.field_statistics

4.1 API

See Also:

Module brewery.dq API Documentation for data quality

12 Chapter 4. Data Quality

Page 17: Brewery: The Data Framework - Library Reference

CHAPTER

FIVE

DATA PIPES AND DATA PROCESSINGSTREAMS

Data processing stream is a network of data processing nodes connected by data pipes. There are several data process-ing node types:

• source nodes - provide data from data sources such as CSV files or database tables

• target nodes - nodes for consuming data and storing them or creating data visualisations

• record nodes - perform operations on whole records, such as merging, joining, aggregations

• field nodes - perform operations on particuliar fields, such as text substitution, field renaming, deriving newfields, restructuring

See Also:

Stream class documentation: brewery.pipes.Stream

5.1 Data Processing Streams

Figure 5.1: Example of a processing stream:• load YAML fiels from a directory - each file represents one record

• Strip all string fields.

• Remove duplicates and store unique records in a SQL database table

• Perform data audit and pretty-print it using formatted text printer

13

Page 18: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

# Prepare nodes

nodes = {"source": pipes.YamlDirectorySourceNode(path = "data/donations"),"strip": pipes.StringStripNode(),"distinct": pipes.DistinctNode(keys = ["year", "receiver", "project"]),"target": pipes.SQLTableTarget(url = "postgres://localhost/data", table = "donations"),"audit": pipes.AuditNode(),"print": pipes.FormattedPrinterNode(output = "audit.txt")

}

# Configure nodes

nodes["source"].fields = ds.fieldlist([ ("year", "integer"),("receiver", "string"),("project", "string"),("requested_amount", "float"),("received_amount", "float"),("source_comment", "string")])

nodes["print"].header = u"field nulls empty\n" \"-----------------------------------------------"

nodes["print"].format = u"{field_name:<30.30} {null_record_ratio:3.2%} {empty_string_count:>10}"

connections = [ ("source", "strip"),("strip", "distinct"),("distinct", "target"),("strip", "audit"),("audit", "print")]

# Create and run stream

stream = pipes.Stream(nodes, connections)stream.initialize()stream.run()stream.finalize()

The created audit.txt file will contain:

field nulls empty-----------------------------------------------year 0.00% 0receiver 0.00% 5project 0.51% 0requested_amount 0.70% 0received_amount 6.40% 0source_comment 99.97% 0

See Also:

For more information about nodes see Node Reference

5.2 Running Streams

Streams are being run using Stream.run(). Stream has to be initialized before running. Stream is run in parallel -each node is run in separate thread.

14 Chapter 5. Data Pipes and Data Processing Streams

Page 19: Brewery: The Data Framework - Library Reference

CHAPTER

SIX

NODE REFERENCE

6.1 Sources

6.1.1 CSV Source

Synopsis: Read data from a comma separated values (CSV) file.

Class: CSVSourceNode

Source node that reads comma separated file from a filesystem or a remote URL.

It is recommended to configure node fields before running. If you do not do so, fields are read from the file header ifspecified by read_header flag. Field storage types are set to string and analytical type is set to typeless.

Table 6.1: Attributes

attribute descriptionresource File name or URL containing comma separated valuesfields fields contained in the fileread_header flag determining whether first line contains header or notskip_rows number of rows to be skippedencoding resource data encoding, by default no conversion is performeddelimiter record delimiter character, default is comma ‘,’quotechar character used for quoting string values, default is double quote

6.1.2 Record List Source

Synopsis: Provide list of dict objects as data source.

15

Page 20: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

Class: RecordListSourceNode

Source node that feeds records (dictionary objects) from a list (or any other iterable) object.

Table 6.2: Attributes

attribute descriptionlist List of records represented as dictionaries.fields Fields in the list.

6.1.3 Row List Source

Synopsis: Provide list of lists or tuples as data source.

Class: RowListSourceNode

Source node that feeds rows (list/tuple of values) from a list (or any other iterable) object.

Table 6.3: Attributes

attribute descriptionlist List of rows represented as lists or tuples.fields Fields in the list.

6.1.4 Data Stream Source

Synopsis: Generic data stream data source node.

Class: StreamSourceNode

Generic data stream source. Wraps a brewery.ds data source and feeds data to the output.

The source data stream should configure fields on initialize().

Note that this node is only for programatically created processing streams. Not useable in visual, web or other streammodelling tools.

Table 6.4: Attributes

attribute descriptionstream Data stream object.

16 Chapter 6. Node Reference

Page 21: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

6.1.5 YAML Directory Source

Synopsis: Read data from a directory containing YAML files

Class: YamlDirectorySourceNode

Source node that reads data from a directory containing YAML files.

The data source reads files from a directory and treats each file as single record. For example, following directory willcontain 3 records:

data/contract_0.ymlcontract_1.ymlcontract_2.yml

Optionally one can specify a field where file name will be stored.

Table 6.5: Attributes

attribute descriptionpath Path to a directoryextension file extension to look for, default is yml. If none is given, then all regular files in the directory are read.filename_field name of a new field that will contain file name

6.2 Record Operations

6.2.1 Aggregate Node

Synopsis: Aggregate values grouping by key fields.

Class: AggregateNode

Aggregate

Table 6.6: Attributes

attribute descriptionkeys List of fields according to which records are groupedrecord_count_field Name of a field where record count will be stored. Default is record_count

6.2. Record Operations 17

Page 22: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

6.2.2 Append

Synopsis: Concatenate input streams.

Class: AppendNode

Sequentialy append input streams. Concatenation order reflects input stream order. The input streams should havesame set of fields.

6.2.3 Data Audit

Synopsis: Perform basic data audit.

Class: AuditNode

Node chcecks stream for empty strings, not filled values, number distinct values.

Audit note passes following fields to the output:

• field_name - name of a field from input

• record_count - number of records

• null_count - number of records with null value for the field

• null_record_ratio - ratio of null count to number of records

• empty_string_count - number of strings that are empty (for fields of type string)

• distinct_count - number of distinct values (if less than distinct threshold). Set to None if there are more distinctvalues than distinct_threshold.

Table 6.7: Attributes

attribute descriptiondis-tinct_threshold

number of distinct values to be tested. If there are more than the threshold, then values are notincluded any more and result distinct_values is set to None

18 Chapter 6. Node Reference

Page 23: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

6.2.4 Distinct Node

Synopsis: Pass only distinct records (discard duplicates) or pass only duplicates

Class: DistinctNode

Node will pass distinct records with given distinct fields.

If discard is False then first record with distinct keys is passed to the output. This is used to find all distinct keyvalues.

If discard is True then first record with distinct keys is discarded and all duplicate records with same key values arepassed to the output. This mode is used to find duplicate records. For example: there should be only one invoice perorganisation per month. Set distinct_fields to organisaion and month, sed discard to True. Running this node shouldgive no records on output if there are no duplicates.

Table 6.8: Attributes

attribute descriptiondis-tinct_fields

List of key fields that will be considered when comparing records

discard Field where substition result will be stored. If not set, then original field will be replaced with newvalue.

6.2.5 Merge Node

Synopsis: Merge two or more streams

Class: MergeNode

Merge two or more streams (join)

Warning: Not yet implemented

Inputs are joined in a star-like fashion: one input is considered master and others are details adding information tothe master. By default master is the first input. Joins are specified as list of tuples: (input_tag, master_input_key,other_input_key).

Following configuration code shows how to add region and category details:

node.keys = [ [1, "region_code", "code"][2, "category_code", "code"] ]

6.2. Record Operations 19

Page 24: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

Master input should have fields region_code and category_code, other inputs should have code field with respectivevalues equal to master keys.

node.keys = [ [1, "region_code", "code"][2, ("category_code", "year"), ("code", "year")] ]

As a key you might use either name of a sigle field or list of fields for compound keys. If you use compound key, bothkeys should have same number of fields. For example, if there is categorisation based on year:

The detail key might be omitted if it the same as in master input:

node.keys = [ [1, "region_code"][2, "category_code"] ]

Master input should have fields region_code and category_code, input #1 should have region_code field and input #2should have category_code field.

Note: Current implementation performs only inner join between datasets, that means that only those input recordsare joined that will have matching keys.

How does it work: all records from detail inputs are read first. Then records from master input are read and joinedwith cached input records. It is recommended that the master dataset set is the largest from all inputs.

Table 6.9: Attributes

attribute descriptionjoins Join specification (see node documentation)master Tag (index) of input dataset which will be considered as masterfield_maps Specification of which fields are passed from input and how they are going to be (re)named

6.2.6 Sample Node

Synopsis: Pass data sample from input to output.

Class: SampleNode

Create a data sample from input stream. There are more sampling possibilities:

• fixed number of records

• % of records, random (not yet implemented)

• get each n-th record (not yet implemented)

Node can work in two modes: pass sample to the output or discard sample and pass the rest. The mode is controlledthrough the discard flag. When it is false, then sample is passed and rest is discarded. When it is true, then sample isdiscarded and rest is passed.

20 Chapter 6. Node Reference

Page 25: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

Table 6.10: Attributes

attribute descriptionsample_size Size of the sample to be passed to the outputdiscard flag whether the sample is discarded or included

6.2.7 Select

Synopsis: Select records by a predicate function.

Class: SelectNode

Select records that will be selected by a predicate function.

Example: configure a node that will select records where amount field is greater than 100

def select_greater_than(value, threshold):return value > threshold

node.function = select_greater_thannode.fields = ["amount"]node.kwargs = {"threshold": 100}

The discard flag controls behaviour of the node: if set to True, then selection is inversed and fields that functionevaluates as True are discarded. Default is False - selected records are passed to the output.

Table 6.11: Attributes

attribute descriptionfunction Predicate function. Should be a callable object.fields List of field names to be passed to the function.discard flag whether the selection is discarded or includedkwargs Keyword arguments passed to the predicate function

6.2.8 Set Select

Synopsis: Select records by a predicate function.

Class: SetSelectNode

Select records where field value is from predefined set of values.

Use case examples:

6.2. Record Operations 21

Page 26: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

• records from certain regions in region field

• recprds where quality status field is low or medium

Table 6.12: Attributes

attribute descriptionfield Field to be tested.value_set set of values that will be used for record selectiondiscard flag whether the selection is discarded or included

6.3 Field Operations

6.3.1 Binning

Synopsis: Derive a field based on binned values (histogram)

Class: BinningNode

Derive a bin/category field from a value.

Warning: Not yet implemented

Binning modes:

• fixed width (for example: by 100)

• fixed number of fixed-width bins

• n-tiles by count or by sum

• record rank

6.3.2 Coalesce Value To Type Node

Synopsis: Coalesce Value to Type

Class: CoalesceValueToTypeNode

Coalesce values of selected fields, or fields of given type to match the type.

• string, text

22 Chapter 6. Node Reference

Page 27: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

– Strip strings

– if non-string, then it is converted to a unicode string

– Change empty strings to empty (null) values

• float, integer

– If value is of string type, perform string cleansing first and then convert them to respective numbersor to null on failure

Table 6.13: Attributes

attribute descriptionfields List of fields to be cleansed. If none given then all fields of known storage type are cleansedtypes List of field types to be coalesced (if no fields given)empty_values dictionary of type -> value pairs to be set when field is considered empty (null) - not yet used

6.3.3 Field Map

Synopsis: Rename or drop fields from the stream.

Class: FieldMapNode

Node renames input fields or drops them from the stream.

Table 6.14: Attributes

attribute descriptionmap_fields Dictionary of input to output field name.drop_fields List of fields to be dropped from the stream.

6.3.4 String Strip

Synopsis: Strip characters.

Class: StringStripNode

Strip spaces (orother specified characters) from string fields.

6.3. Field Operations 23

Page 28: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

Table 6.15: Attributes

attribute descriptionfields List of string fields to be stripped. If none specified, then all fields of storage type string are strippedchars Characters to be stripped. By default all white-space characters are stripped.

6.3.5 Text Substitute

Synopsis: Substitute text in a field using regular expression.

Class: TextSubstituteNode

Substitute text in a field using regular expression.

Table 6.16: Attributes

at-tribute

description

field Field containing a string or text value where substition will be appliedde-rived_field

Field where substition result will be stored. If not set, then original field will be replaced with newvalue.

substi-tutions

List of substitutions: each substition is a two-element tuple (pattern, replacement) where pattern is aregular expression that will be replaced using replacement

6.3.6 Value Threshold

Synopsis: Bin values based on a threshold.

Class: ValueThresholdNode

Create a field that will refer to a value bin based on threshold(s). Values of range type can be compared against one ortwo thresholds to get low/high or low/medium/high value bins.

Note: this node is not yet implemented

The result is stored in a separate field that will be constructed from source field name and prefix/suffix.

For example:

• amount < 100 is low

• 100 <= amount <= 1000 is medium

• amount > 1000 is high

24 Chapter 6. Node Reference

Page 29: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

Generated field will be amount_threshold and will contain one of three possible values: low, medium, hight

Another possible use case might be for binning after data audit: we want to measure null record count and we setthresholds:

• ratio < 5% is ok

• 5% <= ratio <= 15% is fair

• ratio > 15% is bad

We set thresholds as (0.05, 0.15) and values to ("ok", "fair", "bad")

Table 6.17: Attributes

attribute descriptionthresholds List of fields of range type and threshold tuples (field, low, high) or (field, low)bin_names Names of bins based on threshold. Default is low, medium, highprefix field prefix to be used, default is none.suffix field suffix to be used, default is ‘_bin’

6.4 Targets

6.4.1 CSV Target

Synopsis: Write rows as comma separated values into a file

Class: CSVTargetNode

Node that writes rows into a comma separated values (CSV) file.

Attributes

• resource: target object - might be a filename or file-like object

• write_headers: write field names as headers into output file

• truncate: remove data from file before writing, default: True

Table 6.18: Attributes

attribute descriptionresource Target object - file name or IO object.write_headers Flag determining whether to write field names as file headers.truncate If set to True all data from file are removed. Default True

6.4. Targets 25

Page 30: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

6.4.2 Database Table Target

Synopsis: Feed data rows into a relational database table

Class: DatabaseTableTargetNode

Feed data rows into a relational database table.

Table 6.19: Attributes

at-tribute

description

url Database URL in form: adapter://user:password@host/databaseconnec-tion

SQLAlchemy database connection - either this or url should be specified

table table nametruncate If set to True all data table are removed prior to node execution. Default is False - data are

appended to the tablecreate create table if it does not exist or notreplace Set to True if creation should replace existing table or not, otherwise node will fail on attempt to create

a table which already existsoptions other SQLAlchemy connect() options

6.4.3 Formatted Printer

Synopsis: Print input using a string formatter to an output IO stream

Class: FormattedPrinterNode

Target node that will print output based on format.

Refer to the python formatting guide:

http://docs.python.org/library/string.html

Example:

Consider we have a data with information about donations. We want to pretty print two fields: project and re-quested_amount in the form:

Hlavicka - makovicka 27550.0Obecna kniznica - symbol moderneho vzdelavania 132000.0Vzdelavanie na europskej urovni 60000.0

Node for given format is created by:

26 Chapter 6. Node Reference

Page 31: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

node = FormattedPrinterNode(format = u"{project:<50.50} {requested_amount:>20}")

Following format can be used to print output from an audit node:

node.header = u"field nulls empty distinct\n" \"------------------------------------------------------------"

node.format = u"{field_name:<30.30} {null_record_ratio: >7.2%} "\"{empty_string_count:>10} {distinct_count:>10}"

Output will look similar to this:

field nulls empty distinct------------------------------------------------------------file 0.00% 0 32source_code 0.00% 0 2id 9.96% 0 907receiver_name 9.10% 0 1950project 0.05% 0 3628requested_amount 22.90% 0 924received_amount 4.98% 0 728source_comment 99.98% 0 2

Table 6.20: Attributes

attribute descriptionformat Format string to be used. Default is to print all field values separated by tab character.target IO object. If not set then sys.stdout will be used. If it is a string, then it is considered a filename.delimiter Record delimiter. By default it is new line character.header Header string - will be printed before printing first recordfooter Footer string - will be printed after all records are printed

6.4.4 Record List Target

Synopsis: Store data as list of dictionaries (records)

Class: RecordListTargetNode

Target node that stores data from input in a list of records (dictionary objects) object.

To get list of fields, ask for output_fields.

Table 6.21: Attributes

attribute descriptionrecords Created list of records represented as dictionaries.

6.4. Targets 27

Page 32: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

6.4.5 Row List Target

Synopsis: Store data as list of tuples

Class: RowListTargetNode

Target node that stores data from input in a list of rows (as tuples).

To get list of fields, ask for output_fields.

Table 6.22: Attributes

attribute descriptionrows Created list of tuples.

6.4.6 Data Stream Target

Synopsis: Generic data stream data target node.

Class: StreamTargetNode

Generic data stream target. Wraps a brewery.ds data target and feeds data from the input to the target stream.

The data target should match stream fields.

Note that this node is only for programatically created processing streams. Not useable in visual, web or other streammodelling tools.

Table 6.23: Attributes

attribute descriptionstream Data target object.

28 Chapter 6. Node Reference

Page 33: Brewery: The Data Framework - Library Reference

CHAPTER

SEVEN

COMMAND LINE TOOLS

7.1 brewery

Tool for performing brewery framework functionality from command line.

Usage:

brewery command [command_options]

Commands are:

<not yet>

7.2 mongoaudit

Audit mongo database collections from data quality perspective.

Usage:

mongoaudit [-h] [-H HOST] [-p PORT] [-t THRESHOLD] [-f {text,json}] database collection

Here is a foo:

Argument Description-h, --help show this help message and exit-H HOST, --host HOST host with MongoDB server-p PORT, --port PORT port where MongoDB server is listening-t THRESHOLD, --threshold THRESHOLD threshold for number of distinct values (default is 10)-f {text,json}, --format {text,json} output format (default is text)

The threshold is number of distict values to collect, if distinct values is greather than threshold, no more values arebeing collected and distinct_overflow will be set. Set to 0 to get all values. Default is 10.

29

Page 34: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

7.2.1 Measured values

Probe Descriptionfield name of a field which statistics are being presentedrecord_count total count of records in datasetvalue_count number of records in which the field exist. In RDB table this is equal to record_count, in document

based databse, such as MongoDB it is number of documents that have a key present (being null ornot)

value_ratio ratio of value count to record count, 1 for relational databasesnull_record_ratioratio of null value count to record countnull_value_ratioratio of null value count to present value count (same as null_record_ration for relational

databases)null_count number of records where field is nullnull_value_ratioratio of recordsunique_storage_typeif there is only one storage type, then this is set to that typedis-tinct_threshold

7.2.2 Example output

Text output:

flow:storage type: unicodepresent values: 1257 (10.09%)null: 0 (0.00% of records, 0.00% of values)empty strings: 0distinct values:

’spending’’income’

pdf_link:storage type: unicodepresent values: 22 (95.65%)null: 0 (0.00% of records, 0.00% of values)empty strings: 0

JSon output:

{ ..."pdf_link" : {

"unique_storage_type" : "unicode","value_ratio" : 0.956521739130435,"distinct_overflow" : [

true],"key" : "pdf_link","null_value_ratio" : 0,"null_record_ratio" : 0,"record_count" : 23,"storage_types" : [

"unicode"],"distinct_values" : [],"empty_string_count" : 0,"null_count" : 0,"value_count" : 22

30 Chapter 7. Command Line Tools

Page 35: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

},...

}

Note: This tool will change into generic data source auditing tool and will support all datastores that brewery willsupport, such as relational databases or plain structured files.

7.2. mongoaudit 31

Page 36: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

32 Chapter 7. Command Line Tools

Page 37: Brewery: The Data Framework - Library Reference

CHAPTER

EIGHT

EXAMPLES

Contents:

8.1 Merge multiple CSV (or XLS) Files with common subset ofcolumns into one CSV

Situation: We have multiple CSV files, for example with grant listing, from various sources and from various years.The files have couple common columns, such as grant receiver, grant amount, however they might contain moreadditional information.

Files we have:

• grants_2008.csv contains receiver, amount, date

• grants_2009.csv contains id, receiver, amount, contract_number, date

• other_grants_2008.csv contains receiver, subject, requested_amount, amount, date

Objective: Create one CSV file by sequentially merging all input CSV files and using all columns.

Based on our source files we want output csv with fields:

• receiver

• amount

• date

• id

• contract_number

• subject

• requested_amount

In addition, we would like to know where the record comes from, therefore we add file which will contain originalfilename.

8.1.1 Code

Import brewery and all other necessary packages:

import brewery.ds as dsimport sys

33

Page 38: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

Specify sources:

sources = [{"name": "grants_2008.csv", "fields": ["receiver", "amount", "date"]},{"name": "grants_2009.csv", "fields": ["id", "receiver", "amount", "contract_number", "date"]}.{"name": "other_grants_2008.csv", "fields": ["receiver", "subject", "requested_amount", "amount",

"date"]}]

It is highly recommended to explicitly name fileds contained within source files and do not rely on source headers. Inthis case we also make sure, that the field (column) names are normalised. That means that if in one file receiver islabeled just as “receiver” and in another it is “grant receiver” we will get the same field.

You can store sources in an external file, for example as json or yaml and read it in your script.

Now collect all the fields:

# We want to store filename as origin of each rowall_fields = ["file"]

# Go through source definitions and collect the fieldsfor source in sources:

fields = source.get("fields")for field in fields:

if field not in all_fields:all_fields.append(field)

Prepare the output stream into merged.csv and specify fields we have found in sources and want to write intooutput:

out = ds.CSVDataTarget("merged.csv")out.fields = ds.fieldlist(all_fields)out.initialize()

Go through all sources and merge them:

for source in sources:path = source["path"]fields = source.get("fields")

# Initialize data source: skip reading of headers - we are preparing them ourselves# use XLSDataSource for XLS filessrc = ds.CSVDataSource(path, read_header = False)src.fields = ds.fieldlist(fields)src.initialize()

for record in src.records():

# Add file reference into ouput - to know where the row comes fromrecord["file"] = pathout.append(record)

# Close the source streamsrc.finalize()

Now you have a sparse CSV files which contains all rows from source CSV files in one merged.csv.

You can have a directory with YAML files (one per record/row) as output instead of one CSV just by changing datastream target. See brewery.ds.YamlDirectoryDataTarget for more information.

34 Chapter 8. Examples

Page 39: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

out = ds.YamlDirectoryDataTarget("merged_grants")

Directory merged_grants must exist before running the script.

Or directly into a SQL database. The following will initialize SQL table target stream which will remove all existingrecords from the table before inserting. Note that the table grants must exist in database opendata and mustcontain columns with names equal to fields specified in all_fields. See brewery.ds.SQLDataTarget formore information.

out = ds.SQLDataTarget(url = "postgres://localhost/opendata",table = "grants",truncate = True)

Refer to source streams and source targets in the API documentation for more information about possibilities.

See Also:

Module brewery.ds List of varous data sources and data targets.

Function brewery.ds.fieldlist() All streams use list of brewery.ds.Field objects for field metadata.This function will convert list of strings into list of instances of Field class.

8.2 Stream: Append Sources, Clean, Store and Audit

Situation: We have two directories containing YAML files with donations of same structure (or at least same subsetof fields that we are interested in):

donations/source1/record_0.ymlrecord_1.ymlrecord_2.yml...

donations/source2/record_0.ymlrecord_1.ymlrecord_2.yml...

Some numeric fields are represented as strings, contain leading or trailing spaces, spaces between numbers.

Objective: We want to create a CSV file donations.csv that will contain records from both directories. Moreoverwe want to clean the fields: strip spaces from strings and convert mumbers stored as strings into numbers. Also wewant to know, how many of fields are filled in.

8.2.1 Solution

Problem can be solved using following data stream:

The stream consists of following nodes (from left to right):

• two YAML directory sources (YAML Directory Source)

• Append - sequentially concatenate streams

• Coalesce Value To Type Node - fix field values according to specified type, for example convert strings intointegers for fields of type integer

• CSV Target

8.2. Stream: Append Sources, Clean, Store and Audit 35

Page 40: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

Figure 8.1: Data stream.

• Data Audit

• Formatted Printer

8.2.2 Code

Import brewery pipes:

import brewery.pipes as pipes

Create a dictionary containing nodes. We will refer to the nodes by name later.

nodes = {"source1": pipes.YamlDirectorySourceNode(path = "donations/source1"),"source2": pipes.YamlDirectorySourceNode(path = "donations/source2"),"append": pipes.AppendNode(),"clean": pipes.CoalesceValueToTypeNode(),"output": pipes.CSVTargetNode(resource = "donations.csv"),"audit": pipes.AuditNode(distinct_threshold = None),"print": pipes.FormattedPrinterNode()

}

Connect the nodes:

connections = [ ("source1", "append"),("source2", "append"),("append", "clean"),("clean", "output"),("clean", "audit"),("audit", "print")]

Specify fields that we are going to process from sources. Also specify their types for automated cleansing. Formore information about fields see brewery.ds.Field and brewery.ds.FieldList. If you are not creatingFieldList object directly, then make sure that you convert an array using brewery.ds.fieldlist().

fields = [ "file",("source_code", "string"),

36 Chapter 8. Examples

Page 41: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

("id", "string"),("receiver_name", "string"),("project", "string"),("requested_amount", "float"),("received_amount", "float"),("source_comment", "string")

]

nodes["source1"].fields = ds.fieldlist(fields)nodes["source2"].fields = ds.fieldlist(fields)

Configure printer node (Formatted Printer) to create nicely aligned text output:

nodes["print"].header = u"field nulls empty distinct\n" \"------------------------------------------------------------"

nodes["print"].format = u"{field_name:<30.30} {null_record_ratio: >7.2%} "\"{empty_string_count:>10} {distinct_count:>10}"

Crate brewery.pipes.Steram and run it:

stream = pipes.Stream(nodes, connections)stream.initialize()stream.run()stream.finalize()

Stream will create the donations.csv and will produce a report on standard output that will look something likethis:

field nulls empty distinct------------------------------------------------------------file 0.00% 0 32source_code 0.00% 0 2id 9.96% 0 907receiver_name 9.10% 0 1950project 0.05% 0 3628requested_amount 22.90% 0 924received_amount 4.98% 0 728source_comment 99.98% 0 2

8.2.3 Improvement

We know how complete (non-null) our fields are. However, are they complete enough? Say we want at least 95%completeness. We can learn from our report which fields are complete enough or not, based on the nulls reportcoulmn. We still have to read the number and decide.

To aid our decision, in addition to percentage of nulls we add a flag whether the field is ok or not based on threshold.If the field null percent is greater than 5% the field quality fails and we mark it as fail, otherwise the field test passesand we mark it as ok. To derive the flag we insert a Value Threshold node.

nodes = {"source1": pipes.YamlDirectorySourceNode(path = "donations/source1"),"source2": pipes.YamlDirectorySourceNode(path = "donations/source2"),"append": pipes.AppendNode(),"clean": pipes.CoalesceValueToTypeNode(),"output": pipes.CSVTargetNode(resource = "donations.csv"),"audit": pipes.AuditNode(distinct_threshold = None),"threshold": pipes.ValueThresholdNode(), # <-- this was is added

8.2. Stream: Append Sources, Clean, Store and Audit 37

Page 42: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

Figure 8.2: Updated data stream with value threshold node.

"print": pipes.FormattedPrinterNode()}

Rewire nodes:

connections = [ ("source1", "append"),("source2", "append"),("append", "clean"),("clean", "output"),("clean", "audit"), # \("audit", "threshold"), # |-- rewired("threshold", "print") # /]

We consider field to be ok when null count is less than 5%, otherwise test fails. Therefore we configure threshold nodelike this:

nodes["threshold"].thresholds = [ ["null_record_ratio", 0.05] ]nodes["threshold"].bin_names = ("ok", "fail")

Update report template to include new derived field:

nodes["print"].header = u"field nulls status distinct\n" \"------------------------------------------------------------"

nodes["print"].format = u"{field_name:<30.30} {null_record_ratio: >7.2%} "\"{null_record_ratio_bin:>10} {distinct_count:>10}"

The output should look like this:

field nulls status distinct------------------------------------------------------------file 0.00% ok 32source_code 0.00% ok 2id 9.96% fail 907receiver_name 9.10% fail 1950project 0.05% ok 3628requested_amount 22.90% fail 924received_amount 4.98% ok 728source_comment 99.98% fail 2

38 Chapter 8. Examples

Page 43: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

See Also:

• YAML Directory Source

• Append

• Coalesce Value To Type Node

• CSV Target

• Data Audit

• Formatted Printer

• Value Threshold

8.2. Stream: Append Sources, Clean, Store and Audit 39

Page 44: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

40 Chapter 8. Examples

Page 45: Brewery: The Data Framework - Library Reference

CHAPTER

NINE

BREWERY API

Contents:

9.1 Datastores and Data Streams

For introduction on Data Streams see Data Streams

class brewery.ds.Field(name, label=None, storage_type=’unknown’, analytical_type=’typeless’, con-crete_storage_type=None, missing_values=None)

Metadata - information about a field in a dataset or in a datastream.

Attributes

• name - field name

• label - optional human readable field label

• storage_type - Normalized data storage type. The data storage type is abstracted

• concrete_storage_type (optional, recommended) - Data store/database dependent storagetype - this is the real name of data type as used in a database where the field comes fromor where the field is going to be created (this might be null if unknown)

• analytical_type - data type used in data mining algorithms

• missing_values (optional) - Array of values that represent missing values in thedataset for given field

Storage types:

•string - names, labels, short descriptions; mostly implemeted as VARCHAR type in database, or canbe found as CSV file fields

•text - longer texts, long descriptions, articles

•integer - discrete values

•float

•boolean - binary value, mostly implemented as small integer

•date

Analytical types:

41

Page 46: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

Type Descriptionset Values represent categories, like colors or contract . types. Fields of this type might be

numbers which represent for example group numbers, but have no mathematicalinterpretation. For example addition of group numbers 1+2 has no meaning.

or-dered_set

Similar to set field type, but values can be ordered in a meaningful order.

dis-crete

Set of integers - values can be ordered and one can perform arithmetic operations onthem, such as: 1 contract + 2 contracts = 3 contracts

flag Special case of set type where values can be one of two types, such as 1 or 0, ‘yes’ or‘no’, ‘true’ or ‘false’.

range Numerical value, such as financial amount, temperaturede-fault

Analytical type is not explicitly set and default type for fields storage type is used.Refer to the table of default types.

type-less

Field has no analytical relevance.

Default analytical types:

• integer is discrete

• float is range

• unknown, string, text, date are typeless

class brewery.ds.FieldList(fields=None)Create a list of Field objects from a list of strings, dictionaries or tuples

How fields are consutrcuted:

•string: field name is set

•tuple: (field_name, storaget_type, analytical_type), the field_name is obligatory, rest is optional

•dict: contains key-value pairs for initializing a Field object

For strings and in if not explicitly specified in a tuple or a dict case, then following rules apply:

•storage_type is set to unknown

•analytical_type is set to typeless

append(field)Add field to list of fields.

Parameters

• field - Field object, str, tuple or dict object

If field is not a Field object, then construction of new field is as follows:

•str: field name is set

•tuple: (field_name, storaget_type, analytical_type), the field_name is obligatory, rest is optional

•dict: contains key-value pairs for initializing a Field object

For strings and in if not explicitly specified in a tuple or a dict case, then following rules apply:

•storage_type is set to unknown

•analytical_type is set to typeless

copy(fields=None)Return a shallow copy of the list.

42 Chapter 9. Brewery API

Page 47: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

Parameters

• fields - list of fields to be copied.

field(name)Return a field with name name

fields(names=None)Return a tuple with indexes of fields from fieldlist in a data row.

index(field)Return index of a field

indexes(fields)Return a tuple with indexes of fields from fields in a data row. Fields should be a list of Field objectsor strings

names(indexes=None)Return names of fields in the list.

Parameters

• indexes - list of indexes for which field names should be collected. If set to None then allfield names are collected - this is default behaviour.

brewery.ds.fieldlist(fields)Create a FieldList from a list of strings, dictionaries or tuples.

How fields are consutrcuted:

•string: field name is set

•tuple: (field_name, storaget_type, analytical_type), the field_name is obligatory, rest is optional

•dict: contains key-value pairs for initializing a Field object

For strings and in if not explicitly specified in a tuple or a dict case, then following rules apply:

•storage_type is set to unknown

•analytical_type is set to typeless

class brewery.ds.DataStreamShared methods for data targets and data sources

field_namesReturns list of field names. This is shourt-cut for extracting field.name attribute from list of field objectsreturned by fields().

fieldsInformation about fields: tuple of Field objects representing fields passed through the receiv-ing stream - either read from data source (DataSource.rows()) or written to data target(DataTarget.append()).

Subclasses should implement fields property getter. Implementing fields setter is optional.

Implementation of fields setter is recommended for DataSource subclasses such as CSV files or type-less document based database. For example: explicitly specify field names for CSVs without headersor for specify field analytical or storage types for further processing. Setter is recommended also forDataTarget subclasses that create datasets (new CSV file, non-existing tables).

finalize()Subclasses might put finalisation code here, for example:

•closing a file stream

9.1. Datastores and Data Streams 43

Page 48: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

•sending data over network

•writing a chart image to a file

Default implementation does nothing.

initialize()Delayed stream initialisation code. Subclasses might override this method to implement file or handleopening, connecting to a database, doing web authentication, ... By default this method does nothing.

The method does not take any arguments, it expects pre-configured object.

class brewery.ds.DataSourceInput data stream - for reading.

read_fields(limit=0, collapse=False)Read field descriptions from data source. You should use this for datasets that do not provide metadatadirectly, such as CSV files, document bases databases or directories with structured files. Does nothingin relational databases, as fields are represented by table columns and table metadata can obtained fromdatabase easily.

Note that this method can be quite costly, as by default all records within dataset are read and analysed.

After executing this method, stream fields is set to the newly read field list and may be configured (setmore appropriate data types for example).

Arguments

• limit: read only specified number of records from dataset to guess field properties

• collapse: whether records are collapsed into flat structure or not

Returns: tuple with Field objects. Order of fields is datastore adapter specific.

records()Return iterable object with dict objects. This is one of two methods for reading from data source. Sub-classes should implement this method.

rows()Return iterable object with tuples. This is one of two methods for reading from data source. Subclassesshould implement this method.

class brewery.ds.DataTargetOutput data stream - for writing.

append(object)Append an object into dataset. Object can be a tuple, array or a dict object. If tuple or array is used, thenvalue position should correspond to field position in the field list, if dict is used, the keys should be validfield names.

class brewery.ds.CSVDataSource(resource, read_header=True, dialect=None, encoding=None,detect_encoding=False, detect_header=False, sample_size=200,skip_rows=None, **reader_args)

Creates a CSV data source stream.

Attributes

• resource: file name, URL or a file handle with CVS data

• read_header: flag determining whether first line contains header or not. True bydefault.

• encoding: source character encoding, by default no conversion is performed.

• detect_encoding: read sample from source and determine whether source is UTF8 or not

44 Chapter 9. Brewery API

Page 49: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

• detect_headers: try to determine whether data source has headers in first row or not

• sample_size: maximum bytes to be read when detecting encoding and headers in file. Bydefault it is set to 200 bytes to prevent loading huge CSV files at once.

• skip_rows: number of rows to be skipped. Default: None

Note: avoid auto-detection when you are reading from remote URL stream.

initialize()Initialize CSV source stream:

1.perform autodetection if required:

(a) detect encoding from a sample data (if requested)

(b) detect whether CSV has headers from a sample data (if requested)

2.create CSV reader object

3.read CSV headers if requested and initialize stream fields

class brewery.ds.XLSDataSource(resource, sheet=None, encoding=None, skip_rows=None,read_header=True)

Creates a XLS spreadsheet data source stream.

Attributes

• resource: file name, URL or file-like object

• sheet: sheet index number (as int) or sheet name (as str)

• read_header: flag determining whether first line contains header or not. True bydefault.

initialize()Initialize XLS source stream:

class brewery.ds.MongoDBDataSource(collection, database=None, host=None, port=None, ex-pand=False, **mongo_args)

Creates a MongoDB data source stream.

Attributes

• collection: mongo collection name

• database: database name

• host: mongo database server host, default is localhost

• port: mongo port, default is 27017

• expand: expand dictionary values and treat children as top-level keys with dot ‘.’separated key path to the child..

initialize()Initialize Mongo source stream:

class brewery.ds.GoogleSpreadsheetDataSource(spreadsheet_key=None, spread-sheet_name=None, worksheet_id=None,worksheet_name=None, query_string=’‘,username=None, password=None)

Creates a Google Spreadsheet data source stream.

Attributes

9.1. Datastores and Data Streams 45

Page 50: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

• spreadsheet_key: The unique key for the spreadsheet, this usually in the the form‘pk23...We’ or ‘o23...423.12„,3’.

• spreadsheet_name: The title of the spreadsheets.

• worksheet_id: ID of a worksheet

• worksheet_name: name of a worksheet

• query_string: optional query string for row selection

• username: Google account user name

• password: Google account password

You should provide either spreadsheet_key or spreadsheet_name, if more than one spreadsheet with given nameare found, then the first in list returned by Google is used.

For worksheet selection you should provide either worksheet_id or worksheet_name. If more than one work-sheet with given name are found, then the first in list returned by Google is used. If no worksheet_id norworksheet_name are provided, then first worksheet in the workbook is used.

For details on query string syntax see the section on sq underhttp://code.google.com/apis/spreadsheets/reference.html#list_Parameters

initialize()Connect to the Google documents, authenticate.

class brewery.ds.YamlDirectoryDataSource(path, extension=’yml’, expand=False, file-name_field=None)

Creates a YAML directory data source stream.

The data source reads files from a directory and treats each file as single record. For example, following directorywill contain 3 records:

data/contract_0.ymlcontract_1.ymlcontract_2.yml

Optionally one can specify a field where file name will be stored.

Attributes

• path: directory with YAML files

• extension: file extension to look for, default is yml,if none is given, then all regular files inthe directory are read

• expand: expand dictionary values and treat children as top-level keys with dot ‘.’separated key path to the child.. Default: False

• filename_field: if present, then filename is streamed in a field with given name, or if recordis requested, then filename will be in first field.

class brewery.ds.YamlDirectoryDataTarget(path, filename_template=’record_${__index}.yml’,expand=False, filename_start_index=0, trun-cate=False)

Creates a directory data target with YAML files as records.

Attributes

• path: directory with YAML files

• extension: file extension to use

46 Chapter 9. Brewery API

Page 51: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

• expand: expand dictionary values and treat children as top-level keys with dot ‘.’ separatedkey path to the child.. Default: False

• filename_template: template string used for creating file names. ${key} is replacedwith record value for key. __index is used for auto-generated file index from file-name_start_index. Default filename template is record_${__index}.yml which re-sults in filenames record_0.yml, record_1.yml, ...

• filename_start_index - first value of __index filename template value, by default 0

• filename_field: if present, then filename is taken from that field.

• truncate: remove all existing files in the directory. Default is False.

class brewery.ds.SQLDataSource(connection=None, url=None, table=None, statement=None,schema=None, **options)

Creates a relational database data source stream.

Attributes

• url: SQLAlchemy URL - either this or connection should be specified

• connection: SQLAlchemy database connection - either this or url should be specified

• table: table name

• statement: SQL statement to be used as a data source (not supported yet)

• options: SQL alchemy connect() options

Note: avoid auto-detection when you are reading from remote URL stream.

initialize()Initialize source stream:

class brewery.ds.SQLDataTarget(connection=None, url=None, table=None, schema=None, trun-cate=False, create=False, statement=None, replace=False, **op-tions)

Creates a relational database data target stream.

Attributes

• url: SQLAlchemy URL - either this or connection should be specified

• connection: SQLAlchemy database connection - either this or url should be specified

• table: table name

• truncate: whether truncate table or not

• create: whether create table on initialize() or not

• replace: Set to True if creation should replace existing table or not, otherwise initializationwill fail on attempt to create a table which already exists.

• options: other SQLAlchemy connect() options

Note: avoid auto-detection when you are reading from remote URL stream.

initialize()Initialize source stream:

class brewery.ds.StreamAuditor(distinct_threshold=10)Target stream for auditing data values from stream. For more information about probed value properties, pleaserefer to brewery.dq.FieldStatistics

9.1. Datastores and Data Streams 47

Page 52: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

append(obj)Probe row or record and update statistics.

field_statisticsReturn field statistics as dictionary: keys are field names, values are brewery.dq.FieldStatisticsobjects

9.2 Data Quality API

For introduction on Data Quality see Data Quality

class brewery.dq.FieldStatistics(key=None, distinct_threshold=10)Data quality statistics for a dataset field

Attributes

• field: name of a field for which statistics are being collected

• value_count: number of records in which the field exist. In relationad database table thisis equal to number of rows, in document based databse, such as MongoDB, it is number ofdocuments that have a key present (being null or not)

• record_count: total count of records in dataset. This should be set explicitly on finalisation.Seet FieldStatistics.finalize(). In relational database this should be the sameas value_count.

• value_ratio: ratio of value count to record count, 1 for relational databases

• null_count: number of records where field is null

• null_value_ratio: ratio of records with nulls to total number of probed values =null_value_ratio / value_count

• null_record_ratio: ratio of records with nulls to total number of records = null_value_ratio/ record_count

• empty_string_count: number of empty strings

• storage_types: list of all encountered storage types (CSV, MongoDB, XLS might have dif-ferent types within a field)

• unique_storage_type: if there is only one storage type, then this is set to that type

• distict_values: list of collected distinct values

• distinct_threshold: number of distict values to collect, if count of distinct values is greatherthan threshold, collection is stopped and distinct_overflow will be set. Set to 0 to get allvalues. Default is 10.

dict()Return dictionary representation of receiver.

finalize(record_count=None)Compute final statistics.

Parameters

• record_count: final number of records in probed dataset. SeeFieldStatistics() for more information.

probe(value)Probe the value:

48 Chapter 9. Brewery API

Page 53: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

•increase found value count

•identify storage type

•probe for null and for empty string

•probe distinct values: if their count is less than distinct_threshold. If there are more distinctvalues than the distinct_threshold, then distinct_overflow flag is set and list of distinct valueswill be empty

class brewery.dq.FieldTypeProbe(field)Probe for guessing field data type

Attributes:

• field: name of a field which statistics are being presented

• storage_types: found storage types

• unique_storage_type: if there is only one storage type, then this is set to that type

unique_storage_typeReturn storage type if there is only one. This should always return a type in relational databases, but doesnot have to in databases such as MongoDB.

9.3 Data Pipes and Data Processing Streams

For introduction on Pipes see Data Pipes and Data Processing Streams

See Also:

For complete information about nodes see Node Reference

class brewery.pipes.Stream(nodes=None, connections=None)Creates a data stream.

Parameters

• nodes - dictionary with keys as node names and values as nodes

• connections - list of two-item tuples. Each tuple contains source and target node or sourceand target node name.

• stream - another stream or

add(node, name=None)Add a node into the stream.

connect(source, target)Connects source node and target node. Nodes can be provided as objects or names.

initialize()Initializes the data processing stream:

•sorts nodes based on connection dependencies

•creates pipes between nodes

•initializes each node

•initializes pipe fields

9.3. Data Pipes and Data Processing Streams 49

Page 54: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

node(node)Returns a node in the stream or node with name. This method is used for coalescing in other methods,where you can pass either node name or node object.

Parameters

• node - node object or node name

node_sources(node)Return nodes that provide data for node.

node_targets(node)Return nodes that node passes data into.

remove(node)Remove a node from the stream. Also all connections will be removed.

remove_connection(source, target)Remove connection between source and target nodes, if exists.

run()Run all nodes in the stream.

Each node is being wrapped and run in a separate thread.

When an exception occurs, the stream is stopped and all catched exceptions are stored in attribute excep-tions.

sorted_nodes()Return topologically sorted nodes.

Algorithm:

L = Empty list that will contain the sorted elementsS = Set of all nodes with no incoming edgeswhile S is non-empty do

remove a node n from Sinsert n into Lfor each node m with an edge e from n to m do

remove edge e from the graphif m has no other incoming edges then

insert m into Sif graph has edges then

raise exception: graph has at least one cycleelse

return proposed topologically sorted order: L

update(dictionary)Adds nodes and connections specified in the dictionary. Dictionary might contain node names instead ofreal classes. You can use this method for creating stream from a dictionary that was created from a JSONfile, for example.

class brewery.pipes.NodeCreates a new data processing node.

Attributes

• inputs: input pipes

• outputs: output pipes

• description: custom node annotation

50 Chapter 9. Brewery API

Page 55: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

classmethod class_dictionary()Return a dictionary containing node name as key and node class as value.

configure(config, safe=False)Configure node.

Parameters

• config - a dictionary containing node attributes as keys and values as attribute values. Keytype is ignored as it is used for node creation.

• safe - if set to True only non-protected attributes are set. Attempt to set protected attributewill result in an exception. Use safe when you are configuring nodes from a user interfaceor a custom tool. Default is False: all attributes can be set.

Note: Handling of safe flag is not yet implemented, that means that any attribute can be set.

finalize()Finalizes the node. Default implementation does nothing.

initialize()Initializes the node. Initialization is separated from creation. Put any Node subclass initialization in thismethod. Default implementation does nothing.

inputReturn single node imput if exists. Convenience property for nodes which process only one input. Raisesexception if there are no inputs or are more than one imput.

input_fieldsReturn fields from input pipe, if there is one and only one input pipe.

output_field_namesConvenience method for gettin names of fields generated by the node. For more information seebrewery.pipes.output_fields()

output_fieldsReturn fields passed to the output by the node. Subclasses should override this method. Default imple-mentation raises a NotImplementedError.

put(obj)Put row into all output pipes. Convenience method.

put_record(obj)Put record into all output pipes. Convenience method. Not recommended to be used.

run()Main method for running the node code. Subclasses should implement this method.

classmethod subclasses(abstract=False)Get all subclasses of node.

Parameters

• abstract: If set to True all abstract classes are included as well. Default is False

class brewery.pipes.SourceNodeAbstract class for all source nodes

All source nodes should provide an attribute or implement a property (@property) called output_fields.

class brewery.pipes.TargetNodeAbstract class for all target nodes

9.3. Data Pipes and Data Processing Streams 51

Page 56: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

class brewery.pipes.Pipe(buffer_size=1000, queue_size=1)Create a pipe for transfer of structured data between processing nodes.

Pipe passes structured data between processing nodes and node threads by using Queue object. Data are notbeing send as they come, but they are buffered instead. When buffer is full or when pipe flush() is requeted,then the buffer is send through the queue.

If receiving node is finished with source data and does not want anything any more, it should send stop()to the pipe. In most cases, stream runner will send stop() to all input pipes when node run() method isfinished.

If sending node is finished, it should send flush() to the pipe, however this is not necessary in most cases, asthe method for running stream flushes outputs automatically on when node run() method is finished.

Parameters

• buffer_size: number of data objects (rows or records) to be collected before they can beacquired by receiving object. Default is 1000.

• queue_size: number of buffers in a processing queue. Default is 1. Set to 0 for unlimited.

flush()Send all remaining data objects into the pipe buffer and signalize end of source.

put(obj)Put data object into the pipe buffer. When buffer is full it is enqueued and receiving node can get allbuffered data objects.

records()Get data objects from pipe as records (dict objects). This is convenience method with performance costs.Nodes are recommended to process rows instead.

rows()Get data object from pipe. If there is no buffer ready, wait until source object sends some data.

stop()Close the pipe from target node: no more data needed.

52 Chapter 9. Brewery API

Page 57: Brewery: The Data Framework - Library Reference

CHAPTER

TEN

CONTACT AND GETTING HELP

If you have questions, problems or suggestions, you can send a message to Google group or write to me (StefanUrbanek - author).

53

Page 58: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

54 Chapter 10. Contact and Getting Help

Page 59: Brewery: The Data Framework - Library Reference

CHAPTER

ELEVEN

INDICES AND TABLES

• genindex

• modindex

• search

55

Page 60: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

56 Chapter 11. Indices and tables

Page 61: Brewery: The Data Framework - Library Reference

PYTHON MODULE INDEX

bbrewery.dq, 48brewery.ds, 41

57

Page 62: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

58 Python Module Index

Page 63: Brewery: The Data Framework - Library Reference

INDEX

Aadd() (brewery.pipes.Stream method), 49append() (brewery.ds.DataTarget method), 44append() (brewery.ds.FieldList method), 42append() (brewery.ds.StreamAuditor method), 47

Bbrewery.dq (module), 48brewery.ds (module), 41

Cclass_dictionary() (brewery.pipes.Node class method), 50configure() (brewery.pipes.Node method), 51connect() (brewery.pipes.Stream method), 49copy() (brewery.ds.FieldList method), 42CSVDataSource (class in brewery.ds), 44

DDataSource (class in brewery.ds), 44DataStream (class in brewery.ds), 43DataTarget (class in brewery.ds), 44dict() (brewery.dq.FieldStatistics method), 48

FField (class in brewery.ds), 41field() (brewery.ds.FieldList method), 43field_names (brewery.ds.DataStream attribute), 43field_statistics (brewery.ds.StreamAuditor attribute), 48FieldList (class in brewery.ds), 42fieldlist() (in module brewery.ds), 43fields (brewery.ds.DataStream attribute), 43fields() (brewery.ds.FieldList method), 43FieldStatistics (class in brewery.dq), 48FieldTypeProbe (class in brewery.dq), 49finalize() (brewery.dq.FieldStatistics method), 48finalize() (brewery.ds.DataStream method), 43finalize() (brewery.pipes.Node method), 51flush() (brewery.pipes.Pipe method), 52

GGoogleSpreadsheetDataSource (class in brewery.ds), 45

Iindex() (brewery.ds.FieldList method), 43indexes() (brewery.ds.FieldList method), 43initialize() (brewery.ds.CSVDataSource method), 45initialize() (brewery.ds.DataStream method), 44initialize() (brewery.ds.GoogleSpreadsheetDataSource

method), 46initialize() (brewery.ds.MongoDBDataSource method),

45initialize() (brewery.ds.SQLDataSource method), 47initialize() (brewery.ds.SQLDataTarget method), 47initialize() (brewery.ds.XLSDataSource method), 45initialize() (brewery.pipes.Node method), 51initialize() (brewery.pipes.Stream method), 49input (brewery.pipes.Node attribute), 51input_fields (brewery.pipes.Node attribute), 51

MMongoDBDataSource (class in brewery.ds), 45

Nnames() (brewery.ds.FieldList method), 43Node (class in brewery.pipes), 50node() (brewery.pipes.Stream method), 49node_sources() (brewery.pipes.Stream method), 50node_targets() (brewery.pipes.Stream method), 50

Ooutput_field_names (brewery.pipes.Node attribute), 51output_fields (brewery.pipes.Node attribute), 51

PPipe (class in brewery.pipes), 51probe() (brewery.dq.FieldStatistics method), 48put() (brewery.pipes.Node method), 51put() (brewery.pipes.Pipe method), 52put_record() (brewery.pipes.Node method), 51

Rread_fields() (brewery.ds.DataSource method), 44records() (brewery.ds.DataSource method), 44

59

Page 64: Brewery: The Data Framework - Library Reference

Brewery Documentation, Release 0.5.0

records() (brewery.pipes.Pipe method), 52remove() (brewery.pipes.Stream method), 50remove_connection() (brewery.pipes.Stream method), 50rows() (brewery.ds.DataSource method), 44rows() (brewery.pipes.Pipe method), 52run() (brewery.pipes.Node method), 51run() (brewery.pipes.Stream method), 50

Ssorted_nodes() (brewery.pipes.Stream method), 50SourceNode (class in brewery.pipes), 51SQLDataSource (class in brewery.ds), 47SQLDataTarget (class in brewery.ds), 47stop() (brewery.pipes.Pipe method), 52Stream (class in brewery.pipes), 49StreamAuditor (class in brewery.ds), 47subclasses() (brewery.pipes.Node class method), 51

TTargetNode (class in brewery.pipes), 51

Uunique_storage_type (brewery.dq.FieldTypeProbe at-

tribute), 49update() (brewery.pipes.Stream method), 50

XXLSDataSource (class in brewery.ds), 45

YYamlDirectoryDataSource (class in brewery.ds), 46YamlDirectoryDataTarget (class in brewery.ds), 46

60 Index