module generic.iter_rows
¶
Short summary¶
module pysqllike.generic.iter_rows
An class which iterates on any set.
Classes¶
class |
truncated documentation |
---|---|
Defines an iterator which mimic SQL behavior. |
Properties¶
property |
truncated documentation |
---|---|
return _schema |
Methods¶
method |
truncated documentation |
---|---|
evaluate |
|
Initializes the iterator. |
|
iterator, returns this row, it always outputs a list of list |
|
usual |
|
look for column index whose name is name |
|
This function applies a groupby (same behavior as SQL’s version) |
|
This function sorts elements from an IterRow instance. |
|
calls |
|
This function takes an undefined number of arguments. It can be used the following way: |
|
Concatenates this table with another one |
|
This function filters elements from an |
Documentation¶
An class which iterates on any set.
- class pysqllike.generic.iter_rows.IterRow(schema=None, anyset=None, as_dict=True)¶
Bases:
object
Defines an iterator which mimic SQL behavior.
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)
- property Schema¶
return _schema
- __call__()¶
evaluate
- __init__(schema=None, anyset=None, as_dict=True)¶
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)
- __iter__()¶
iterator, returns this row, it always outputs a list of list
- __str__()¶
usual
- _findschema(schema, name)¶
look for column index whose name is name
- Parameters:
name – column name to search
schema – schama
- Returns:
position
- groupby(*nochange, as_dict=True, **changed)¶
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())
- orderby(*nochange, as_dict=True, ascending=True)¶
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 )
- print_schema()¶
calls
print_parent()
on each column
- select(*nochange, as_dict=True, **changed)¶
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)
- unionall(iter, merge_schema=False, as_dict=True)¶
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)
- where(condition, as_dict=True, append_condition=False)¶
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())