Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
tev
plugin_event
Commits
4dc872d2
Commit
4dc872d2
authored
Mar 23, 2017
by
LE GAC Renaud
Browse files
Update the module dataframe to make public and export get_column_names and to_items_per_year.
parent
aa63c785
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
95 additions
and
79 deletions
+95
-79
docs/api/dataframes.rst
docs/api/dataframes.rst
+3
-1
docs/api/generated/plugin_event.get_column_names.rst
docs/api/generated/plugin_event.get_column_names.rst
+6
-0
docs/api/generated/plugin_event.to_items_per_year.rst
docs/api/generated/plugin_event.to_items_per_year.rst
+6
-0
modules/plugin_event/__init__.py
modules/plugin_event/__init__.py
+3
-1
modules/plugin_event/dataframes.py
modules/plugin_event/dataframes.py
+77
-77
No files found.
docs/api/dataframes.rst
View file @
4dc872d2
...
...
@@ -15,6 +15,7 @@ A collection of classes and functions to build and to manipulate the sources.
debug_df
expand_per_year
full_name
get_column_names
get_items
get_items_per_year
get_items_small
...
...
@@ -29,4 +30,5 @@ A collection of classes and functions to build and to manipulate the sources.
normalize_history_data
query_history
Timer
to_extjs_gridcolumns
\ No newline at end of file
to_extjs_gridcolumns
to_items_per_year
\ No newline at end of file
docs/api/generated/plugin_event.get_column_names.rst
0 → 100644
View file @
4dc872d2
plugin_event.get_column_names
=============================
.. currentmodule:: plugin_event
.. autofunction:: get_column_names
\ No newline at end of file
docs/api/generated/plugin_event.to_items_per_year.rst
0 → 100644
View file @
4dc872d2
plugin_event.to_items_per_year
==============================
.. currentmodule:: plugin_event
.. autofunction:: to_items_per_year
\ No newline at end of file
modules/plugin_event/__init__.py
View file @
4dc872d2
...
...
@@ -12,6 +12,7 @@ from dataframes import (active_period,
debug_df
,
expand_per_year
,
full_name
,
get_column_names
,
get_items
,
get_items_per_year
,
get_items_small
,
...
...
@@ -26,7 +27,8 @@ from dataframes import (active_period,
normalize_history_data
,
query_history
,
Timer
,
to_extjs_gridcolumns
)
to_extjs_gridcolumns
,
to_items_per_year
)
from
event
import
Event
...
...
modules/plugin_event/dataframes.py
View file @
4dc872d2
...
...
@@ -32,75 +32,6 @@ HISTORY_QUERY_FIELDS = [
"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
):
"""Determine the period of activity for a person / object when the
domain and/or project and/or team and or category is fixed.
...
...
@@ -333,6 +264,31 @@ def full_name(first_name, last_name):
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
):
"""Return the DataFrame with active items belonging to the event id_event.
...
...
@@ -450,7 +406,7 @@ def get_items(**kwargs):
# columns name for the DataFrame
# resolve ambiguities for code, category and note
columns
=
_
get_column_names
(
fields
)
columns
=
get_column_names
(
fields
)
# the DataFrame
df
=
(
db2df
(
db
,
query
,
fields
,
columns
)
...
...
@@ -532,7 +488,7 @@ def get_items_per_year(**kwargs):
* one column for each key of the history.data dictionary
"""
return
_ge
t_items_per_year
(
get_items
,
**
kwargs
)
return
t
o
_items_per_year
(
get_items
,
**
kwargs
)
def
get_items_small
(
**
kwargs
):
...
...
@@ -668,7 +624,7 @@ def get_items_small_per_year(**kwargs):
* one column for each key of the history.data dictionary
"""
return
_ge
t_items_per_year
(
get_items_small
,
**
kwargs
)
return
t
o
_items_per_year
(
get_items_small
,
**
kwargs
)
def
get_objectlike_items
(
**
kwargs
):
...
...
@@ -765,7 +721,7 @@ def get_objectlike_items(**kwargs):
# columns name for the DataFrame
# resolve ambiguities for code, category and note
columns
=
_
get_column_names
(
fields
)
columns
=
get_column_names
(
fields
)
# the DataFrame
df
=
(
db2df
(
db
,
query
,
fields
,
columns
)
...
...
@@ -836,7 +792,7 @@ def get_objectlike_items_per_year(**kwargs):
* one column for each key of the history.data dictionary
"""
return
_ge
t_items_per_year
(
get_objectlike_items
,
**
kwargs
)
return
t
o
_items_per_year
(
get_objectlike_items
,
**
kwargs
)
def
get_peoplelike_items
(
**
kwargs
):
...
...
@@ -937,7 +893,7 @@ def get_peoplelike_items(**kwargs):
# columns name for the DataFrame
# resolve ambiguities for code, category and note
columns
=
_
get_column_names
(
fields
)
columns
=
get_column_names
(
fields
)
# the DataFrame
df
=
(
db2df
(
db
,
query
,
fields
,
columns
)
...
...
@@ -1035,7 +991,7 @@ def get_people_per_year(**kwargs):
# compute the beginning and ending of each year
# it is temporarily columns which are used in coverage computation
df
=
(
_ge
t_items_per_year
(
get_peoplelike_items
,
**
kwargs
)
df
=
(
t
o
_items_per_year
(
get_peoplelike_items
,
**
kwargs
)
.
assign
(
period
=
lambda
x
:
x
.
apply
(
lambda
y
:
...
...
@@ -1160,7 +1116,7 @@ def get_peoplelike_items_per_year(**kwargs):
* one column for each key of the history.data dictionary
"""
return
_ge
t_items_per_year
(
get_peoplelike_items
,
**
kwargs
)
return
t
o
_items_per_year
(
get_peoplelike_items
,
**
kwargs
)
def
is_end
(
year_start
,
year_end
,
period_end
):
...
...
@@ -1388,3 +1344,47 @@ def to_extjs_gridcolumns(df):
li
.
append
(
cfg
)
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment