• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NaturalHistoryMuseum / splitgill / #72

02 Aug 2024 09:12PM UTC coverage: 35.289% (-60.8%) from 96.047%
#72

push

coveralls-python

jrdh
build: swap docker-compose for docker compose

379 of 1074 relevant lines covered (35.29%)

0.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

47.06
/splitgill/utils.py
1
from datetime import datetime, timezone
1✔
2
from itertools import islice
1✔
3
from time import time
1✔
4
from typing import Iterable
1✔
5

6

7
def to_timestamp(moment: datetime) -> int:
1✔
8
    """
9
    Converts a datetime object into a timestamp value. The timestamp returned is an int.
10
    The timestamp value is the number of milliseconds that have elapsed between the UNIX
11
    epoch and the given moment.
12

13
    Any precision greater than milliseconds held within the datetime is simply ignored
14
    and no rounding occurs.
15

16
    :param moment: a datetime object
17
    :return: the timestamp (number of milliseconds between the UNIX epoch and the
18
             moment) as an int
19
    """
20
    return int(moment.timestamp() * 1000)
×
21

22

23
def parse_to_timestamp(
1✔
24
    datetime_string: str, datetime_format: str, tzinfo: datetime.tzinfo = timezone.utc
25
) -> int:
26
    """
27
    Parses the given string using the given format and returns a timestamp.
28

29
    If the datetime object built from parsing the string with the given format doesn't
30
    contain a tzinfo component, then the tzinfo parameter is added as a replacement
31
    value. This defaults to UTC.
32

33
    :param datetime_string: the datetime as a string
34
    :param datetime_format: the format as a string
35
    :param tzinfo: the timezone to use (default: UTC)
36
    :return: the parsed datetime as the number of milliseconds since the UNIX epoch as
37
             an int
38
    """
39
    date = datetime.strptime(datetime_string, datetime_format)
×
40
    # if no timezone info was provided, apply UTC as a default to ensure consistency
41
    if date.tzinfo is None:
×
42
        date = date.replace(tzinfo=tzinfo)
×
43
    return to_timestamp(date)
×
44

45

46
def now() -> int:
1✔
47
    """
48
    Get the current datetime as a timestamp.
49
    """
50
    return int(time() * 1000)
×
51

52

53
def partition(iterable: Iterable, size: int) -> Iterable[list]:
1✔
54
    """
55
    Partitions the given iterable into chunks. Each chunk yielded will be a list which
56
    is at most `size` in length. The final list yielded may be smaller if the length of
57
    the iterable isn't wholly divisible by the size.
58

59
    :param iterable: the iterable to partition
60
    :param size: the maximum size of list chunk to yield
61
    :return: yields lists
62
    """
63
    it = iter(iterable)
×
64
    while chunk := list(islice(it, size)):
×
65
        yield chunk
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc