Commit f15fd09c by Peter Weidenbach

time structure feature added

parent 53ff55f7
......@@ -99,3 +99,8 @@ ENV/
# mypy
.mypy_cache/
# Pydev
.project
.pydevproject
language: python
python:
- "3.5"
- "3.6"
# command to install dependencies
install:
- "python setup.py -q install"
- "pip install pytest-pep8"
# command to run tests
script: pytest
\ No newline at end of file
# common_helper_filter
generate nice structured output
Generate nice structured output of data.
## time
Examples:
´´´python
from common_helper_filter import time_format
print(time_format(122))
>> 2m, 2s
print(time_format(122).format(description='short'))
>> 2m, 2s
print(time_format(122).format(description='long'))
>> 2 minutes, 2 seconds
print(time_format(122).format(description='none'))
>> 2:2
print(time_format(63072000))
>> 2y, 0d, 0h, 0m, 0s
´´´
\ No newline at end of file
from .time import time_format
__all__ = [
'time_format',
]
from collections import namedtuple
TimeStruct = namedtuple('time', ['y', 'd', 'h', 'm', 's'])
class time_format():
description = {
'long': [' years, ', ' days, ', ' hours, ', ' minutes, ', ' seconds'],
'short': ['y, ', 'd, ', 'h, ', 'm, ', 's'],
'none': [':', ':', ':', ':', '']
}
def __init__(self, seconds):
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
years, days = divmod(days, 365)
self.time = TimeStruct(y=years, d=days, h=hours, m=minutes, s=seconds)
def __str__(self):
return self.format()
def __repr__(self):
return self.__str__()
def format(self, description='short'):
pointer = 0
output = ''
while pointer < len(self.time) - 1:
if self.time[pointer] > 0:
break
pointer += 1
while pointer < len(self.time) - 1:
output += '{:.0f}{}'.format(self.time[pointer], self.description[description][pointer])
pointer += 1
output += '{:.0f}{}'.format(self.time[pointer], self.description[description][pointer])
return output
[pytest]
addopts = --pep8 -v
pep8ignore =
*.py E501
from setuptools import setup, find_packages
VERSION = 0.1
setup(
name="common_helper_filter",
version=VERSION,
packages=find_packages(),
description="Helper functions for nice data structuring.",
author="Fraunhofer FKIE",
author_email="peter.weidenbach@fkie.fraunhofer.de",
url="http://www.fkie.fraunhofer.de",
license="GPL-3.0"
)
import pytest
from common_helper_filter import time_format
@pytest.mark.parametrize('input_seconds, description, expected_output', [
(65, 'none', '1:5'),
(65, 'short', '1m, 5s'),
(65, 'long', '1 minutes, 5 seconds'),
(8643665.1234, 'short', '100d, 1h, 1m, 5s'),
(63072000, 'long', '2 years, 0 days, 0 hours, 0 minutes, 0 seconds'),
(0, 'none', '0')
])
def test_time_format_different_descriptions(input_seconds, description, expected_output):
assert time_format(input_seconds).format(description=description) == expected_output
def test_time_format_simple():
assert '{}'.format(time_format(65)) == '1m, 5s'
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