API¶
This part of the documentation covers all the interfaces of Stilpy. That includes TimeGaps and TimeInterval class.
TimeGaps Objects¶
- class stilpy.timegaps.TimeGaps(iterable: Iterable, tag_loc: str | int, i_tag: Any, f_tag: Any, dt_loc: str | int, *args: str | None, group_by: Any | None = None)[source]¶
Bases:
objectClass representing a collection for time interval objects.
It’s an iterator of TimeInterval Objects.
- Variables:
grouper_tags (List of dicts) – A list containig a collection of dictionarys with the keys passed as
grouped_byparameter and the values finded in theiterablethat makes the different groups.grouped_intervals (List of TimeGaps iterators) – Returns time intervals separated by groups. For every group a
TimeGapsiterator is made an stored in alist. The groups are created using thegroup_byargument passed in the instance.
- Raises:
StopIteration – Raised when the
__next__()method is called but there are no more records to read.AttributeError – Raised when
grouper_tagsorgrouped_intervalsattribute is reasigned.
- ERROR_ITERABLE = "it's not a supported iterable."¶
- __init__(iterable: Iterable, tag_loc: str | int, i_tag: Any, f_tag: Any, dt_loc: str | int, *args: str | None, group_by: Any | None = None) None[source]¶
- Parameters:
iterable (iterable of iterables) –
An iterable object that contains a
listof items. Those items must bedictsor dictlike objects. Lists, tuples and objects with__dict__atribute are accepted as well. Every item, must content the next items inside:A
datetimeobject or a string format datetimeA item that defines if the
datetimeobject or string that we just mentioned is an initial or a final time point of a time interval.
tag_loc (str or int) – Where to find the tag that contains the value representing wich part of the interval defines each element. In a
listor atuplecan be an index. In adictcan be a key that reference the value.i_tag (Any) – The name of the initial time tag in the iterable. It’s needed to know if a specific element of the iterable is an initial time value. Default is
'start'.f_tag (Any) – The name of the final time tag in the iterable. It’s needed to know if a specific element of the iterable is a final time value. Default is
'end'.dt_loc (str or int) – The location, inside each element of the iterable, of the datetime information.
args (str, optional) –
Any argument that will be an attribute of the TimeInterval. For example, a name, an age… This parameter expects the key, or the attribute, depending on the
iterablethat is passed as first argument. Cannot be aint, or aTypeErrorwill be raised.If both records for an interval contains a key with different values, the attribute will be populated with a tuple like
(<start_record_value>, <end_record_value>)group_by (Any, optional) – The tags that you want to use for making the correct pairs between the diferent records of your
iterable. If it isNone, the intervals will be made considering only thei_tagand thef_tag. You can pass alist,tupleor a single value. But every tag you pass must be contained in the iterables inside the mainiterable.
- Raises:
ValueError – If the iterables inside the main
iterableare not supported aValueErrorwill be raised.TypeError: – When
inttypes are passed to theargsparameter as additional attributes forTimeInterval.
- property grouped_intervals: List[TimeGaps]¶
Returns a
listwith aTimeGapsiterator for each group.If there are no groups, a single
TimeGapsobject will be returned, with the same intervals, properties and methods as the initial instance ofTimeGaps.
- property grouper_tags: List[dict]¶
Return a
listof dictionaries with the grouper tags.For example, if the group were made with
'name'and'surname', this property will return something like:{'name': 'Jonh', 'surname': 'Smith', ...}.
- total_duration(default: Any = None) timedelta | Any[source]¶
Returns the total duration of the
TimeIntervalsiterator.If any
TimeIntervalobject is imperfect (startorendatributte is empty ‘’), the defaultargumentpassed to the method will be returned.- Parameters:
default (Any, optional) – The value that will be returned if any
TimeIntervalobject in theTimeGapsiterator hasn’t a valid duration for the sum.- Returns:
timedelta – When every
TimeIntervalobject in theTimeGapsiterator has a validdurationatributte (that’s atimedeltatype) the method will return atimedeltaobject representing the sum of everydurationatributte in theTimeIntervalobjects.Any – If the method can’t reach the sum, it returns the value of the
defaultparameter.
- total_duration_anyway() timedelta[source]¶
Returns the total duration of the
TimeIntervalsiterator.If any
TimeIntervalobject is imperfect (startorendatributte is empty ‘’) the duration of the others intervals will be returned.- Returns:
When every
TimeIntervalobject in theTimeGapsiterator has a validdurationatributte (that’s atimedeltatype) the method will return atimedeltaobject representing the sum of everydurationatributte in theTimeIntervalobjects. If any timeinterval hasn’t a valid duration, that interval will be ignored, but the duration will be returned anyway, with the sum of the intervals that do have a duration.- Return type:
timedelta
TimeInterval Objects¶
- class stilpy.timeinterval.TimeInterval(*args, start: datetime | str = '', end: datetime | str = '', **kwargs: str | None)[source]¶
Bases:
objectClass representing a time interval between two moments.
- Variables:
start (datetime object or an empty str) – The starting point of the time interval. Default is an empty string.
end (datetime object or an empty str) – The ending point of the time interval. Default is an empty string.
duration (timedelta object or and empty str) – The duration of the time interval. If one of the limits (
startorend) it’s not set, thedurationproperty will return an empty string.is_perfect (bool) – It will return
Falseif neitherstartorendare empty strings. Othewise,Truewill be returned.
- __init__(*args, start: datetime | str = '', end: datetime | str = '', **kwargs: str | None) None[source]¶
- Parameters:
start (datetime object or str) –
A datetime object representing the lower limit of the time interval. Default is an empty string. It accepts string formatted datetime like:
’dd-mm-yyyy HH:MM:SS’
’yyyy-mm-dd HH:MM:SS’
Your can use dots, dashes and slashes as separators between the date elements and colons for the time elements. Hours goes from 00 to 23, minutes and seconds from 00 to 59.
end (datetime object or str) –
A datetime object representing the upper limit of the time interval. Default is an empty string. It accepts string formatted datetime like:
’dd-mm-yyyy HH:MM:SS’
’yyyy-mm-dd HH:MM:SS’
Your can use dots, dashes and slashes as separators between the date elements and colons for the time elements. Hours goes from 00 to 23, minutes and seconds from 00 to 59.
**kwargs (Any) – Any argument that must be an attribute of the
TimeIntervalobject. For example, a name, an age…
- Raises:
ValueError – if the
startorendargument passed to the constructor is not a datetime object, a valid formatted datetime or an empty string.
- property duration: timedelta | str¶
Returns the
durationproperty.
- property end: datetime¶
Returns the
endproperty.
- property is_perfect: bool¶
Returns
Trueif the interval hasn’t an emptystartorend.
- property start: datetime¶
Returns the
startproperty
- static str_to_datetime(n_datetime: str) datetime | str[source]¶
Tries to cast short string dates to
dateobjet.- Parameters:
n_datetime (str) – A string format datetime. Date part accepts two formats: ‘yyyy-mm-dd’ and ‘dd-mm-yyyy’. You can use 3 different separators:
'-','/'and'.'. Time part only accepts one format: HH:MM:SS, using colons as separators. Hours goes from 00 to 23, minutes and seconds from 00 to 59.- Returns:
datetime –
datetimeobject ifn_datetimeis a valid datetime formatted string.str – if
n_datetimeis an empty string.
- Raises:
ValueError – if the
n_datetimeargument passed to function is not a valid formatted datetime or an empty string.