Commit 4dc872d2 authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Update the module dataframe to make public and export get_column_names and to_items_per_year.

parent aa63c785
...@@ -15,6 +15,7 @@ A collection of classes and functions to build and to manipulate the sources. ...@@ -15,6 +15,7 @@ A collection of classes and functions to build and to manipulate the sources.
debug_df debug_df
expand_per_year expand_per_year
full_name full_name
get_column_names
get_items get_items
get_items_per_year get_items_per_year
get_items_small get_items_small
...@@ -29,4 +30,5 @@ A collection of classes and functions to build and to manipulate the sources. ...@@ -29,4 +30,5 @@ A collection of classes and functions to build and to manipulate the sources.
normalize_history_data normalize_history_data
query_history query_history
Timer Timer
to_extjs_gridcolumns to_extjs_gridcolumns
\ No newline at end of file to_items_per_year
\ No newline at end of file
plugin_event.get_column_names
=============================
.. currentmodule:: plugin_event
.. autofunction:: get_column_names
\ No newline at end of file
plugin_event.to_items_per_year
==============================
.. currentmodule:: plugin_event
.. autofunction:: to_items_per_year
\ No newline at end of file
...@@ -12,6 +12,7 @@ from dataframes import (active_period, ...@@ -12,6 +12,7 @@ from dataframes import (active_period,
debug_df, debug_df,
expand_per_year, expand_per_year,
full_name, full_name,
get_column_names,
get_items, get_items,
get_items_per_year, get_items_per_year,
get_items_small, get_items_small,
...@@ -26,7 +27,8 @@ from dataframes import (active_period, ...@@ -26,7 +27,8 @@ from dataframes import (active_period,
normalize_history_data, normalize_history_data,
query_history, query_history,
Timer, Timer,
to_extjs_gridcolumns) to_extjs_gridcolumns,
to_items_per_year)
from event import Event from event import Event
......
...@@ -32,75 +32,6 @@ HISTORY_QUERY_FIELDS = [ ...@@ -32,75 +32,6 @@ HISTORY_QUERY_FIELDS = [
"year_start"] "year_start"]
def _get_column_names(fields):
"""Snippet to get list of column names from a list of database field.
Rules are applied to resolve ambiguities when the field name is used
in different table, *e.g* code, category or note.
Args:
fields (list): list of gluon.dal.Field
Returns:
list
"""
# columns name for the DataFrame from database fields
# resolve ambiguities for code, category and note
columns = []
for field in fields:
name = field.name
if name in ("code", "category", "note"):
name = "%s_%s" % (field._table._tablename, name)
columns.append(name)
return columns
def _get_items_per_year(func, **kwargs):
"""Snippet transforming a of active items between year_start and year_end
to a list of active items per year.
The year range is defined by the keyword ``year_start`` and ``year_end``.
The underlying logic deal with all cases.
Args:
func (reference):
reference to a function generating the dataframe for active items.
**kwargs (dict):
key, value pair to select item, the event and the year range.
They are use by the function func.
Returns:
pandas.DataFrame
"""
is_start = \
"year_start" in kwargs and kwargs["year_start"] not in (None, '')
is_end = "year_end" in kwargs and kwargs["year_end"] not in (None, '')
if not is_start and not is_end:
kwargs["year_start"] = DATE_MIN.year
kwargs["year_end"] = DATE_MAX.year
elif is_start and not is_end:
kwargs["year_end"] = kwargs["year_start"]
elif is_end and not is_start:
kwargs["year_start"] = kwargs["year_end"]
ystart = int(kwargs["year_start"])
yend = int(kwargs["year_end"])
df = (func(**kwargs)
.reset_index()
.pipe(expand_per_year, ystart, yend))
return df
def active_period(**kwargs): def active_period(**kwargs):
"""Determine the period of activity for a person / object when the """Determine the period of activity for a person / object when the
domain and/or project and/or team and or category is fixed. domain and/or project and/or team and or category is fixed.
...@@ -333,6 +264,31 @@ def full_name(first_name, last_name): ...@@ -333,6 +264,31 @@ def full_name(first_name, last_name):
return first_name.str.cat(last_name, sep=" ") return first_name.str.cat(last_name, sep=" ")
def get_column_names(fields):
"""Snippet to get list of column names from a list of database field.
Rules are applied to resolve ambiguities when the field name is used
in different table, *e.g* code, category or note.
Args:
fields (list): list of gluon.dal.Field
Returns:
list
"""
# columns name for the DataFrame from database fields
# resolve ambiguities for code, category and note
columns = []
for field in fields:
name = field.name
if name in ("code", "category", "note"):
name = "%s_%s" % (field._table._tablename, name)
columns.append(name)
return columns
def get_items(**kwargs): def get_items(**kwargs):
"""Return the DataFrame with active items belonging to the event id_event. """Return the DataFrame with active items belonging to the event id_event.
...@@ -450,7 +406,7 @@ def get_items(**kwargs): ...@@ -450,7 +406,7 @@ def get_items(**kwargs):
# columns name for the DataFrame # columns name for the DataFrame
# resolve ambiguities for code, category and note # resolve ambiguities for code, category and note
columns = _get_column_names(fields) columns = get_column_names(fields)
# the DataFrame # the DataFrame
df = (db2df(db, query, fields, columns) df = (db2df(db, query, fields, columns)
...@@ -532,7 +488,7 @@ def get_items_per_year(**kwargs): ...@@ -532,7 +488,7 @@ def get_items_per_year(**kwargs):
* one column for each key of the history.data dictionary * one column for each key of the history.data dictionary
""" """
return _get_items_per_year(get_items, **kwargs) return to_items_per_year(get_items, **kwargs)
def get_items_small(**kwargs): def get_items_small(**kwargs):
...@@ -668,7 +624,7 @@ def get_items_small_per_year(**kwargs): ...@@ -668,7 +624,7 @@ def get_items_small_per_year(**kwargs):
* one column for each key of the history.data dictionary * one column for each key of the history.data dictionary
""" """
return _get_items_per_year(get_items_small, **kwargs) return to_items_per_year(get_items_small, **kwargs)
def get_objectlike_items(**kwargs): def get_objectlike_items(**kwargs):
...@@ -765,7 +721,7 @@ def get_objectlike_items(**kwargs): ...@@ -765,7 +721,7 @@ def get_objectlike_items(**kwargs):
# columns name for the DataFrame # columns name for the DataFrame
# resolve ambiguities for code, category and note # resolve ambiguities for code, category and note
columns = _get_column_names(fields) columns = get_column_names(fields)
# the DataFrame # the DataFrame
df = (db2df(db, query, fields, columns) df = (db2df(db, query, fields, columns)
...@@ -836,7 +792,7 @@ def get_objectlike_items_per_year(**kwargs): ...@@ -836,7 +792,7 @@ def get_objectlike_items_per_year(**kwargs):
* one column for each key of the history.data dictionary * one column for each key of the history.data dictionary
""" """
return _get_items_per_year(get_objectlike_items, **kwargs) return to_items_per_year(get_objectlike_items, **kwargs)
def get_peoplelike_items(**kwargs): def get_peoplelike_items(**kwargs):
...@@ -937,7 +893,7 @@ def get_peoplelike_items(**kwargs): ...@@ -937,7 +893,7 @@ def get_peoplelike_items(**kwargs):
# columns name for the DataFrame # columns name for the DataFrame
# resolve ambiguities for code, category and note # resolve ambiguities for code, category and note
columns = _get_column_names(fields) columns = get_column_names(fields)
# the DataFrame # the DataFrame
df = (db2df(db, query, fields, columns) df = (db2df(db, query, fields, columns)
...@@ -1035,7 +991,7 @@ def get_people_per_year(**kwargs): ...@@ -1035,7 +991,7 @@ def get_people_per_year(**kwargs):
# compute the beginning and ending of each year # compute the beginning and ending of each year
# it is temporarily columns which are used in coverage computation # it is temporarily columns which are used in coverage computation
df = (_get_items_per_year(get_peoplelike_items, **kwargs) df = (to_items_per_year(get_peoplelike_items, **kwargs)
.assign( .assign(
period=lambda x: period=lambda x:
x.apply(lambda y: x.apply(lambda y:
...@@ -1160,7 +1116,7 @@ def get_peoplelike_items_per_year(**kwargs): ...@@ -1160,7 +1116,7 @@ def get_peoplelike_items_per_year(**kwargs):
* one column for each key of the history.data dictionary * one column for each key of the history.data dictionary
""" """
return _get_items_per_year(get_peoplelike_items, **kwargs) return to_items_per_year(get_peoplelike_items, **kwargs)
def is_end(year_start, year_end, period_end): def is_end(year_start, year_end, period_end):
...@@ -1388,3 +1344,47 @@ def to_extjs_gridcolumns(df): ...@@ -1388,3 +1344,47 @@ def to_extjs_gridcolumns(df):
li.append(cfg) li.append(cfg)
return li return li
def to_items_per_year(func, **kwargs):
"""Snippet transforming a of active items between ``year_start``
and ``year_end`` to a list of active items per year.
The year range is defined by the keyword ``year_start`` and ``year_end``.
The underlying logic deal with all cases.
Args:
func (reference):
reference to a function generating the dataframe for active items.
**kwargs (dict):
key, value pair to select item, the event and the year range.
They are use by the function func.
Returns:
pandas.DataFrame
"""
is_start = \
"year_start" in kwargs and kwargs["year_start"] not in (None, '')
is_end = "year_end" in kwargs and kwargs["year_end"] not in (None, '')
if not is_start and not is_end:
kwargs["year_start"] = DATE_MIN.year
kwargs["year_end"] = DATE_MAX.year
elif is_start and not is_end:
kwargs["year_end"] = kwargs["year_start"]
elif is_end and not is_start:
kwargs["year_start"] = kwargs["year_end"]
ystart = int(kwargs["year_start"])
yend = int(kwargs["year_end"])
df = (func(**kwargs)
.reset_index()
.pipe(expand_per_year, ystart, yend))
return df
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment