module generic.iter_rows

Inheritance diagram of pysqllike.generic.iter_rows

Short summary

module pysqllike.generic.iter_rows

An class which iterates on any set.

source on GitHub

Classes

class

truncated documentation

IterRow

Defines an iterator which mimic SQL behavior.

Properties

property

truncated documentation

Schema

return _schema

Methods

method

truncated documentation

__call__

evaluate

__init__

Initializes the iterator.

__iter__

iterator, returns this row, it always outputs a list of list

__str__

usual

_findschema

look for column index whose name is name

groupby

This function applies a groupby (same behavior as SQL’s version)

orderby

This function sorts elements from an IterRow instance.

print_schema

calls print_parent() on each column

select

This function takes an undefined number of arguments. It can be used the following way:

unionall

Concatenates this table with another one

where

This function filters elements from an IterRow instance.

Documentation

An class which iterates on any set.

source on GitHub

class pysqllike.generic.iter_rows.IterRow(schema=None, anyset=None, as_dict=True)[source]

Bases: object

Defines an iterator which mimic SQL behavior.

source on GitHub

Initializes the iterator.

Parameters
  • schema – list of tuple [ (name, type) ], type can be None id it is unknown or a list of ColumnType

  • anyset – any set or iterator following the previous schema (or None if there is not any)

  • as_dict – in that case, the class iterator returns a list of dictionaries for each row

schema can be None if anyset if a list of dictionaries [ {"col1":value1, ... } ]. In that case, the construction will build the schema from the first row.

IterRow with a list of dictionaries

l = [ {"nom": 10}, {"jean": 40} ]
tbl = IterRow (None, l)

IterRow with a schema

l = [ ("nom", 10), ("jean", 40) ]
schema = [ ("nom", str), ("age", int) ]
tbl = IterRow (schema, l)

source on GitHub

Schema

return _schema

source on GitHub

__call__()[source]

evaluate

source on GitHub

__init__(schema=None, anyset=None, as_dict=True)[source]

Initializes the iterator.

Parameters
  • schema – list of tuple [ (name, type) ], type can be None id it is unknown or a list of ColumnType

  • anyset – any set or iterator following the previous schema (or None if there is not any)

  • as_dict – in that case, the class iterator returns a list of dictionaries for each row

schema can be None if anyset if a list of dictionaries [ {"col1":value1, ... } ]. In that case, the construction will build the schema from the first row.

IterRow with a list of dictionaries

l = [ {"nom": 10}, {"jean": 40} ]
tbl = IterRow (None, l)

IterRow with a schema

l = [ ("nom", 10), ("jean", 40) ]
schema = [ ("nom", str), ("age", int) ]
tbl = IterRow (schema, l)

source on GitHub

__iter__()[source]

iterator, returns this row, it always outputs a list of list

source on GitHub

__str__()[source]

usual

source on GitHub

_findschema(schema, name)[source]

look for column index whose name is name

Parameters
  • name – column name to search

  • schema – schama

Returns

position

source on GitHub

groupby(*nochange, as_dict=True, **changed)[source]

This function applies a groupby (same behavior as SQL’s version)

Parameters
  • nochange – list of fields to keep

  • changed – list of custom fields

  • as_dict – returns results as a list of dictionaries [ { “colname”: value, … } ]

Returns

IterRow

Warning

The function does not guarantee the order of the output columns.

group by

l = [   { "nom":"j", "age": 10, "gender":"M"} ,
        {"nom":"jean", "age":40, "gender":"M"},
        {"nom":"jeanne", "age":2, "gender":"F"} ]
tbl = IterRow (None, l)

iter = tbl.groupby(tbl.gender, len_nom=tbl.nom.len(), avg_age=tbl.age.avg())

source on GitHub

orderby(*nochange, as_dict=True, ascending=True)[source]

This function sorts elements from an IterRow instance.

Parameters
  • nochange – list of columns used to sort

  • ascending – order

  • as_dict – returns results as a list of dictionaries [ { “colname”: value, … } ]

Returns

IterRow

order by

l = [   { "nom":"j", "age": 10, "gender":"M"} ,
        {"nom":"jean", "age":40, "gender":"M"},
        {"nom":"jeanne", "age":2, "gender":"F"} ]
tbl = IterRow(None, l)

iter = tbl.orderby(tbl.nom, tbl.age, ascending=False )

source on GitHub

print_schema()[source]

calls print_parent() on each column

source on GitHub

select(*nochange, as_dict=True, **changed)[source]

This function takes an undefined number of arguments. It can be used the following way:

simple select

tbl = IterRow( ... )
it  = tbl.select ( tbl.name, tbl.age * 2, old = tbl.age )

chained select

tbl = IterRow ( ... )
iter = tbl.select(tbl.nom, age2=tbl.age, age3= tbl.age*0.5)
iter2 = iter.select(iter.nom, age4=iter.age2*iter.age3)
l = list ( iter2 )
Parameters
  • nochange – list of fields to keep

  • changed – list of custom fields

  • as_dict – returns results as a list of dictionaries [ { “colname”: value, … } ]

Returns

IterRow

Warning

The function does not guarantee the order of the output columns.

example with a function

def myf(x,y) :
    return x*2.5 + y
tbl = IterRow ( ... )
iter = tbl.select(tbl.nom, age0= CFT(myf, tbl.age, tbl.age) )
res = list(iter)

source on GitHub

unionall(iter, merge_schema=False, as_dict=True)[source]

Concatenates this table with another one

Parameters
  • iter – IterRow

  • merge_schema – if False, the function expects you find the same schema, otherwise, it merges them (same column name are not duplicated)

  • as_dict – returns results as a list of dictionaries [ { “colname”: value, … } ]

Returns

IterRow

union all

l = [   { "nom":"j", "age": 10, "gender":"M"} ,
        {"nom":"jean", "age":40, "gender":"M"},
        {"nom":"jeanne", "age":2, "gender":"F"} ]
tbl = IterRow (None, l)

iter = tbl.unionall(tbl)

union all with different schema

l = [   { "nom":"j", "age": 10, "gender":"M"} ,
        {"nom":"jean", "age":40, "gender":"M"},
        {"nom":"jeanne", "age":2, "gender":"F"} ]
tbl = IterRow (None, l)

l = [   { "nom":"j", "newage": 10, "gender":"M"} ,
        {"nom":"jean", "newage":40, "gender":"M"},
        {"nom":"jeanne", "newage":2, "gender":"F"} ]
tbl2 = IterRow (None, l)

iter = tbl.unionall(tbl2, merge_schema = True)

source on GitHub

where(condition, as_dict=True, append_condition=False)[source]

This function filters elements from an IterRow instance.

Parameters
  • condition – a ColumnType or an expression of ColumnType

  • append_condition – append the condition to the schema (for debugging purpose)

  • as_dict – returns results as a list of dictionaries [ { “colname”: value, … } ]

Returns

IterRow

where

tbl = IterRow ( ... )
iter = tbl.where(tbl.age == 40)
res = list(iter)

Warning

For operator or, and, not, the syntax is different because they cannot be overriden in Python.

where with or

tbl = IterRow ( ... )
iter = tbl.where( ( tbl.age == 2).Or( tbl.age == 40))
iter2 = tbl.where((tbl.age == 10).Not())

source on GitHub