1
0
mirror of https://github.com/b4tman/sync_ics2gcal synced 2026-02-04 15:25:04 +00:00

8 Commits

Author SHA1 Message Date
d2d43d02da Merge branch 'converter/pydantic' of https://github.com/b4tman/sync_ics2gcal into converter/pydantic 2021-10-21 09:58:14 +03:00
2e114db5c9 + bench_converter 2021-10-21 09:57:17 +03:00
2ab96d1b00 travis ci: dist = focal 2021-10-21 09:52:27 +03:00
15951ba200 try pydantic for converter
---
x2 slower:
before: best: _82001700 ns,     avg: _85379966.2 ns,    median: _84408700.0 ns
after__: best: 162860900 ns,     avg: 175015097.0 ns,    median: 171212750.0 ns
2021-10-17 17:10:44 +03:00
d03e5691ee add pydantic 2021-10-17 17:07:36 +03:00
3686bc29ee + bench_converter 2021-10-15 14:53:20 +03:00
9603718c83 remove install from requirements.txt 2021-10-15 09:37:57 +03:00
f7a84cc58c use POETRY_PYPI_TOKEN_PYPI to publish
https://python-poetry.org/docs/repositories/
2021-10-15 09:34:07 +03:00
7 changed files with 261 additions and 31 deletions

View File

@@ -36,8 +36,6 @@ jobs:
uses: snok/install-poetry@v1 uses: snok/install-poetry@v1
- name: Install deps - name: Install deps
run: poetry install run: poetry install
- name: Install deps form requirements.txt
run: poetry run pip install -r requirements.txt
- name: Lint with flake8 - name: Lint with flake8
run: | run: |
# stop the build if there are Python syntax errors or undefined names # stop the build if there are Python syntax errors or undefined names

View File

@@ -28,7 +28,6 @@ jobs:
run: poetry build run: poetry build
- name: Publish - name: Publish
env: env:
REPO_USERNAME: __token__ POETRY_PYPI_TOKEN_PYPI: ${{ secrets.pypi_token }}
REPO_PASSWORD: ${{ secrets.pypi_token }}
run: | run: |
poetry publish -n -u $REPO_USERNAME -p $REPO_PASSWORD poetry publish -n

View File

@@ -1,4 +1,6 @@
language: python language: python
os: linux
dist: focal
python: python:
- "3.6" - "3.6"

74
poetry.lock generated
View File

@@ -20,6 +20,18 @@ docs = ["furo", "sphinx", "zope.interface", "sphinx-notfound-page"]
tests = ["coverage[toml] (>=5.0.2)", "hypothesis", "pympler", "pytest (>=4.3.0)", "six", "mypy", "pytest-mypy-plugins", "zope.interface"] tests = ["coverage[toml] (>=5.0.2)", "hypothesis", "pympler", "pytest (>=4.3.0)", "six", "mypy", "pytest-mypy-plugins", "zope.interface"]
tests_no_zope = ["coverage[toml] (>=5.0.2)", "hypothesis", "pympler", "pytest (>=4.3.0)", "six", "mypy", "pytest-mypy-plugins"] tests_no_zope = ["coverage[toml] (>=5.0.2)", "hypothesis", "pympler", "pytest (>=4.3.0)", "six", "mypy", "pytest-mypy-plugins"]
[[package]]
name = "autopep8"
version = "1.5.7"
description = "A tool that automatically formats Python code to conform to the PEP 8 style guide"
category = "dev"
optional = false
python-versions = "*"
[package.dependencies]
pycodestyle = ">=2.7.0"
toml = "*"
[[package]] [[package]]
name = "cachetools" name = "cachetools"
version = "4.2.4" version = "4.2.4"
@@ -55,6 +67,14 @@ category = "dev"
optional = false optional = false
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*"
[[package]]
name = "dataclasses"
version = "0.8"
description = "A backport of the dataclasses module for Python 3.6"
category = "main"
optional = false
python-versions = ">=3.6, <3.7"
[[package]] [[package]]
name = "fire" name = "fire"
version = "0.4.0" version = "0.4.0"
@@ -293,6 +313,22 @@ category = "dev"
optional = false optional = false
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
[[package]]
name = "pydantic"
version = "1.8.2"
description = "Data validation and settings management using python 3.6 type hinting"
category = "main"
optional = false
python-versions = ">=3.6.1"
[package.dependencies]
dataclasses = {version = ">=0.6", markers = "python_version < \"3.7\""}
typing-extensions = ">=3.7.4.3"
[package.extras]
dotenv = ["python-dotenv (>=0.10.4)"]
email = ["email-validator (>=1.0.3)"]
[[package]] [[package]]
name = "pyflakes" name = "pyflakes"
version = "2.3.1" version = "2.3.1"
@@ -415,7 +451,7 @@ python-versions = ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*"
name = "typing-extensions" name = "typing-extensions"
version = "3.10.0.2" version = "3.10.0.2"
description = "Backported and Experimental Type Hints for Python 3.5+" description = "Backported and Experimental Type Hints for Python 3.5+"
category = "dev" category = "main"
optional = false optional = false
python-versions = "*" python-versions = "*"
@@ -454,8 +490,8 @@ testing = ["pytest (>=4.6)", "pytest-checkdocs (>=2.4)", "pytest-flake8", "pytes
[metadata] [metadata]
lock-version = "1.1" lock-version = "1.1"
python-versions = "^3.6" python-versions = ">=3.6.1"
content-hash = "712c4284d2a0349b959dad1bfec496daa5020135d340ef3859b641d567276b33" content-hash = "9145569d3597a35a93b05b114ac7bcdba29e1b1fb215793b427b0e40fbe52bef"
[metadata.files] [metadata.files]
atomicwrites = [ atomicwrites = [
@@ -466,6 +502,10 @@ attrs = [
{file = "attrs-21.2.0-py2.py3-none-any.whl", hash = "sha256:149e90d6d8ac20db7a955ad60cf0e6881a3f20d37096140088356da6c716b0b1"}, {file = "attrs-21.2.0-py2.py3-none-any.whl", hash = "sha256:149e90d6d8ac20db7a955ad60cf0e6881a3f20d37096140088356da6c716b0b1"},
{file = "attrs-21.2.0.tar.gz", hash = "sha256:ef6aaac3ca6cd92904cdd0d83f629a15f18053ec84e6432106f7a4d04ae4f5fb"}, {file = "attrs-21.2.0.tar.gz", hash = "sha256:ef6aaac3ca6cd92904cdd0d83f629a15f18053ec84e6432106f7a4d04ae4f5fb"},
] ]
autopep8 = [
{file = "autopep8-1.5.7-py2.py3-none-any.whl", hash = "sha256:aa213493c30dcdac99537249ee65b24af0b2c29f2e83cd8b3f68760441ed0db9"},
{file = "autopep8-1.5.7.tar.gz", hash = "sha256:276ced7e9e3cb22e5d7c14748384a5cf5d9002257c0ed50c0e075b68011bb6d0"},
]
cachetools = [ cachetools = [
{file = "cachetools-4.2.4-py3-none-any.whl", hash = "sha256:92971d3cb7d2a97efff7c7bb1657f21a8f5fb309a37530537c71b1774189f2d1"}, {file = "cachetools-4.2.4-py3-none-any.whl", hash = "sha256:92971d3cb7d2a97efff7c7bb1657f21a8f5fb309a37530537c71b1774189f2d1"},
{file = "cachetools-4.2.4.tar.gz", hash = "sha256:89ea6f1b638d5a73a4f9226be57ac5e4f399d22770b92355f92dcb0f7f001693"}, {file = "cachetools-4.2.4.tar.gz", hash = "sha256:89ea6f1b638d5a73a4f9226be57ac5e4f399d22770b92355f92dcb0f7f001693"},
@@ -482,6 +522,10 @@ colorama = [
{file = "colorama-0.4.4-py2.py3-none-any.whl", hash = "sha256:9f47eda37229f68eee03b24b9748937c7dc3868f906e8ba69fbcbdd3bc5dc3e2"}, {file = "colorama-0.4.4-py2.py3-none-any.whl", hash = "sha256:9f47eda37229f68eee03b24b9748937c7dc3868f906e8ba69fbcbdd3bc5dc3e2"},
{file = "colorama-0.4.4.tar.gz", hash = "sha256:5941b2b48a20143d2267e95b1c2a7603ce057ee39fd88e7329b0c292aa16869b"}, {file = "colorama-0.4.4.tar.gz", hash = "sha256:5941b2b48a20143d2267e95b1c2a7603ce057ee39fd88e7329b0c292aa16869b"},
] ]
dataclasses = [
{file = "dataclasses-0.8-py3-none-any.whl", hash = "sha256:0201d89fa866f68c8ebd9d08ee6ff50c0b255f8ec63a71c16fda7af82bb887bf"},
{file = "dataclasses-0.8.tar.gz", hash = "sha256:8479067f342acf957dc82ec415d355ab5edb7e7646b90dc6e2fd1d96ad084c97"},
]
fire = [ fire = [
{file = "fire-0.4.0.tar.gz", hash = "sha256:c5e2b8763699d1142393a46d0e3e790c5eb2f0706082df8f647878842c216a62"}, {file = "fire-0.4.0.tar.gz", hash = "sha256:c5e2b8763699d1142393a46d0e3e790c5eb2f0706082df8f647878842c216a62"},
] ]
@@ -602,6 +646,30 @@ pycodestyle = [
{file = "pycodestyle-2.7.0-py2.py3-none-any.whl", hash = "sha256:514f76d918fcc0b55c6680472f0a37970994e07bbb80725808c17089be302068"}, {file = "pycodestyle-2.7.0-py2.py3-none-any.whl", hash = "sha256:514f76d918fcc0b55c6680472f0a37970994e07bbb80725808c17089be302068"},
{file = "pycodestyle-2.7.0.tar.gz", hash = "sha256:c389c1d06bf7904078ca03399a4816f974a1d590090fecea0c63ec26ebaf1cef"}, {file = "pycodestyle-2.7.0.tar.gz", hash = "sha256:c389c1d06bf7904078ca03399a4816f974a1d590090fecea0c63ec26ebaf1cef"},
] ]
pydantic = [
{file = "pydantic-1.8.2-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:05ddfd37c1720c392f4e0d43c484217b7521558302e7069ce8d318438d297739"},
{file = "pydantic-1.8.2-cp36-cp36m-manylinux1_i686.whl", hash = "sha256:a7c6002203fe2c5a1b5cbb141bb85060cbff88c2d78eccbc72d97eb7022c43e4"},
{file = "pydantic-1.8.2-cp36-cp36m-manylinux2014_i686.whl", hash = "sha256:589eb6cd6361e8ac341db97602eb7f354551482368a37f4fd086c0733548308e"},
{file = "pydantic-1.8.2-cp36-cp36m-manylinux2014_x86_64.whl", hash = "sha256:10e5622224245941efc193ad1d159887872776df7a8fd592ed746aa25d071840"},
{file = "pydantic-1.8.2-cp36-cp36m-win_amd64.whl", hash = "sha256:99a9fc39470010c45c161a1dc584997f1feb13f689ecf645f59bb4ba623e586b"},
{file = "pydantic-1.8.2-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:a83db7205f60c6a86f2c44a61791d993dff4b73135df1973ecd9eed5ea0bda20"},
{file = "pydantic-1.8.2-cp37-cp37m-manylinux1_i686.whl", hash = "sha256:41b542c0b3c42dc17da70554bc6f38cbc30d7066d2c2815a94499b5684582ecb"},
{file = "pydantic-1.8.2-cp37-cp37m-manylinux2014_i686.whl", hash = "sha256:ea5cb40a3b23b3265f6325727ddfc45141b08ed665458be8c6285e7b85bd73a1"},
{file = "pydantic-1.8.2-cp37-cp37m-manylinux2014_x86_64.whl", hash = "sha256:18b5ea242dd3e62dbf89b2b0ec9ba6c7b5abaf6af85b95a97b00279f65845a23"},
{file = "pydantic-1.8.2-cp37-cp37m-win_amd64.whl", hash = "sha256:234a6c19f1c14e25e362cb05c68afb7f183eb931dd3cd4605eafff055ebbf287"},
{file = "pydantic-1.8.2-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:021ea0e4133e8c824775a0cfe098677acf6fa5a3cbf9206a376eed3fc09302cd"},
{file = "pydantic-1.8.2-cp38-cp38-manylinux1_i686.whl", hash = "sha256:e710876437bc07bd414ff453ac8ec63d219e7690128d925c6e82889d674bb505"},
{file = "pydantic-1.8.2-cp38-cp38-manylinux2014_i686.whl", hash = "sha256:ac8eed4ca3bd3aadc58a13c2aa93cd8a884bcf21cb019f8cfecaae3b6ce3746e"},
{file = "pydantic-1.8.2-cp38-cp38-manylinux2014_x86_64.whl", hash = "sha256:4a03cbbe743e9c7247ceae6f0d8898f7a64bb65800a45cbdc52d65e370570820"},
{file = "pydantic-1.8.2-cp38-cp38-win_amd64.whl", hash = "sha256:8621559dcf5afacf0069ed194278f35c255dc1a1385c28b32dd6c110fd6531b3"},
{file = "pydantic-1.8.2-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:8b223557f9510cf0bfd8b01316bf6dd281cf41826607eada99662f5e4963f316"},
{file = "pydantic-1.8.2-cp39-cp39-manylinux1_i686.whl", hash = "sha256:244ad78eeb388a43b0c927e74d3af78008e944074b7d0f4f696ddd5b2af43c62"},
{file = "pydantic-1.8.2-cp39-cp39-manylinux2014_i686.whl", hash = "sha256:05ef5246a7ffd2ce12a619cbb29f3307b7c4509307b1b49f456657b43529dc6f"},
{file = "pydantic-1.8.2-cp39-cp39-manylinux2014_x86_64.whl", hash = "sha256:54cd5121383f4a461ff7644c7ca20c0419d58052db70d8791eacbbe31528916b"},
{file = "pydantic-1.8.2-cp39-cp39-win_amd64.whl", hash = "sha256:4be75bebf676a5f0f87937c6ddb061fa39cbea067240d98e298508c1bda6f3f3"},
{file = "pydantic-1.8.2-py3-none-any.whl", hash = "sha256:fec866a0b59f372b7e776f2d7308511784dace622e0992a0b59ea3ccee0ae833"},
{file = "pydantic-1.8.2.tar.gz", hash = "sha256:26464e57ccaafe72b7ad156fdaa4e9b9ef051f69e175dbbb463283000c05ab7b"},
]
pyflakes = [ pyflakes = [
{file = "pyflakes-2.3.1-py2.py3-none-any.whl", hash = "sha256:7893783d01b8a89811dd72d7dfd4d84ff098e5eed95cfa8905b22bbffe52efc3"}, {file = "pyflakes-2.3.1-py2.py3-none-any.whl", hash = "sha256:7893783d01b8a89811dd72d7dfd4d84ff098e5eed95cfa8905b22bbffe52efc3"},
{file = "pyflakes-2.3.1.tar.gz", hash = "sha256:f5bc8ecabc05bb9d291eb5203d6810b49040f6ff446a756326104746cc00c1db"}, {file = "pyflakes-2.3.1.tar.gz", hash = "sha256:f5bc8ecabc05bb9d291eb5203d6810b49040f6ff446a756326104746cc00c1db"},

View File

@@ -19,17 +19,19 @@ classifiers = [
] ]
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "^3.6" python = ">=3.6.1"
google-auth = "2.2.1" google-auth = "2.2.1"
google-api-python-client = "2.23.0" google-api-python-client = "2.23.0"
icalendar = "4.0.7" icalendar = "4.0.7"
pytz = "2021.1" pytz = "2021.1"
PyYAML = "5.4.1" PyYAML = "5.4.1"
fire = "0.4.0" fire = "0.4.0"
pydantic = "^1.8.2"
[tool.poetry.dev-dependencies] [tool.poetry.dev-dependencies]
pytest = "^6.2.5" pytest = "^6.2.5"
flake8 = "^3.9.2" flake8 = "^3.9.2"
autopep8 = "^1.5.7"
[tool.poetry.scripts] [tool.poetry.scripts]
sync-ics2gcal = "sync_ics2gcal.sync_calendar:main" sync-ics2gcal = "sync_ics2gcal.sync_calendar:main"

View File

@@ -5,11 +5,56 @@ from typing import Union, Dict, Callable, Optional
from icalendar import Calendar, Event from icalendar import Calendar, Event
from pytz import utc from pytz import utc
import pydantic
from .gcal import EventData, EventList from .gcal import EventData, EventList
DateDateTime = Union[datetime.date, datetime.datetime] DateDateTime = Union[datetime.date, datetime.datetime]
class GCal_DateDateTime(pydantic.BaseModel):
date: Optional[str] = pydantic.Field(default=None)
date_time: Optional[str] = pydantic.Field(alias='dateTime', default=None)
timezone: Optional[str] = pydantic.Field(alias='timeZone', default=None)
@pydantic.root_validator(allow_reuse=True)
def check_only_date_or_datetime(cls, values):
date = values.get('date', None)
date_time = values.get('date_time', None)
assert (date is None) != (date_time is None), \
'only date or date_time must be provided'
return values
@classmethod
def create_from(cls, value: DateDateTime) -> 'GCal_DateDateTime':
key: str = 'date'
str_value: str = ''
if type(value) is datetime.datetime:
key = 'date_time'
str_value = format_datetime_utc(value)
else:
str_value = value.isoformat()
return cls(**{key: str_value})
class Config:
allow_population_by_field_name = True
class GCal_Event(pydantic.BaseModel):
created: Optional[str] = None
updated: Optional[str] = None
summary: Optional[str] = None
description: Optional[str] = None
location: Optional[str] = None
start: GCal_DateDateTime
end: GCal_DateDateTime
transparency: Optional[str] = None
ical_uid: str = pydantic.Field(alias='iCalUID')
class Config:
allow_population_by_field_name = True
def format_datetime_utc(value: DateDateTime) -> str: def format_datetime_utc(value: DateDateTime) -> str:
"""utc datetime as string from date or datetime value """utc datetime as string from date or datetime value
@@ -19,7 +64,7 @@ def format_datetime_utc(value: DateDateTime) -> str:
Returns: Returns:
utc datetime value as string in iso format utc datetime value as string in iso format
""" """
if not isinstance(value, datetime.datetime): if type(value) is datetime.date:
value = datetime.datetime( value = datetime.datetime(
value.year, value.month, value.day, tzinfo=utc) value.year, value.month, value.day, tzinfo=utc)
value = value.replace(microsecond=1) value = value.replace(microsecond=1)
@@ -85,7 +130,7 @@ class EventConverter(Event):
return format_datetime_utc(self.decoded(prop)) return format_datetime_utc(self.decoded(prop))
def _gcal_start(self) -> Dict[str, str]: def _gcal_start(self) -> GCal_DateDateTime:
""" event start dict from icalendar event """ event start dict from icalendar event
Raises: Raises:
@@ -96,9 +141,9 @@ class EventConverter(Event):
""" """
value = self.decoded('DTSTART') value = self.decoded('DTSTART')
return gcal_date_or_dateTime(value) return GCal_DateDateTime.create_from(value)
def _gcal_end(self) -> Dict[str, str]: def _gcal_end(self) -> GCal_DateDateTime:
"""event end dict from icalendar event """event end dict from icalendar event
Raises: Raises:
@@ -110,13 +155,17 @@ class EventConverter(Event):
result = None result = None
if 'DTEND' in self: if 'DTEND' in self:
value = self.decoded('DTEND') value = self.decoded('DTEND')
result = gcal_date_or_dateTime(value) result = GCal_DateDateTime.create_from(value)
elif 'DURATION' in self: elif 'DURATION' in self:
start_val = self.decoded('DTSTART') start_val = self.decoded('DTSTART')
duration = self.decoded('DURATION') duration = self.decoded('DURATION')
end_val = start_val + duration end_val = start_val + duration
if type(start_val) is datetime.date:
if type(end_val) is datetime.datetime:
end_val = datetime.date(
end_val.year, end_val.month, end_val.day)
result = gcal_date_or_dateTime(end_val, check_value=start_val) result = GCal_DateDateTime.create_from(end_val)
else: else:
raise ValueError('no DTEND or DURATION') raise ValueError('no DTEND or DURATION')
return result return result
@@ -138,6 +187,18 @@ class EventConverter(Event):
if ics_prop in self: if ics_prop in self:
gcal_event[prop] = func(ics_prop) gcal_event[prop] = func(ics_prop)
def _get_prop(self, prop: str, func: Callable[[str], str]):
"""get property from ical event if exist else None
Arguments:
prop -- property name
func -- function to convert
"""
if prop not in self:
return None
return func(prop)
def to_gcal(self) -> EventData: def to_gcal(self) -> EventData:
"""Convert """Convert
@@ -145,24 +206,19 @@ class EventConverter(Event):
dict - google calendar#event resource dict - google calendar#event resource
""" """
event = { kwargs = {
'iCalUID': self._str_prop('UID'), 'ical_uid': self._str_prop('UID'),
'start': self._gcal_start(), 'start': self._gcal_start(),
'end': self._gcal_end() 'end': self._gcal_end(),
'summary': self._get_prop('SUMMARY', self._str_prop),
'description': self._get_prop('DESCRIPTION', self._str_prop),
'location': self._get_prop('LOCATION', self._str_prop),
'created': self._get_prop('CREATED', self._datetime_str_prop),
'updated': self._get_prop('LAST-MODIFIED', self._datetime_str_prop),
'transparency': self._get_prop('TRANSP', lambda prop: self._str_prop(prop).lower()),
} }
self._put_to_gcal(event, 'summary', self._str_prop) return GCal_Event(**kwargs).dict(by_alias=True, exclude_defaults=True)
self._put_to_gcal(event, 'description', self._str_prop)
self._put_to_gcal(event, 'location', self._str_prop)
self._put_to_gcal(event, 'created', self._datetime_str_prop)
self._put_to_gcal(
event, 'updated', self._datetime_str_prop, 'LAST-MODIFIED')
self._put_to_gcal(
event,
'transparency',
lambda prop: self._str_prop(prop).lower(), 'TRANSP')
return event
class CalendarConverter: class CalendarConverter:

105
tests/bench_converter.py Normal file
View File

@@ -0,0 +1,105 @@
from typing import Iterable, List, Tuple, Union, Optional
from uuid import uuid4
import datetime
from itertools import islice
from dataclasses import dataclass
import time
import statistics
import functools
from sync_ics2gcal import CalendarConverter
@dataclass
class IcsTestEvent:
uid: str
start_date: Union[datetime.datetime, datetime.date]
end_date: Union[datetime.datetime, datetime.date, None] = None
duration: Optional[datetime.timedelta] = None
created: Union[datetime.datetime, datetime.date, None] = None
updated: Union[datetime.datetime, datetime.date, None] = None
@staticmethod
def _format_datetime(value: Union[datetime.datetime, datetime.date]):
result: str = ''
if isinstance(value, datetime.datetime):
result += f'DATE-TIME:{value.strftime("%Y%m%dT%H%M%SZ")}'
else:
result += f'DATE:{value.strftime("%Y%m%d")}'
return result
def render(self) -> str:
result: str = ''
result += 'BEGIN:VEVENT\r\n'
result += f'UID:{self.uid}\r\n'
result += f'DTSTART;VALUE={IcsTestEvent._format_datetime(self.start_date)}\r\n'
if self.end_date is not None:
result += f'DTEND;VALUE={IcsTestEvent._format_datetime(self.end_date)}\r\n'
else:
result += f'DURATION:P{self.duration.days}D\r\n'
if self.created is not None:
result += f'CREATED:{self.created.strftime("%Y%m%dT%H%M%SZ")}\r\n'
if self.updated is not None:
result += f'LAST-MODIFIED:{self.updated.strftime("%Y%m%dT%H%M%SZ")}\r\n'
result += 'END:VEVENT\r\n'
return result
@dataclass
class IcsTestCalendar:
events: List[IcsTestEvent]
def render(self) -> str:
result: str = ''
result += 'BEGIN:VCALENDAR\r\n'
for event in self.events:
result += event.render()
result += 'END:VCALENDAR\r\n'
return result
def gen_test_calendar(events_count: int) -> IcsTestCalendar:
def gen_events() -> Iterable[IcsTestEvent]:
for i in range(10000000):
uid = f'{uuid4()}@test.com'
start_date = datetime.datetime.now() + datetime.timedelta(hours=i)
end_date = start_date + datetime.timedelta(hours=1)
event: IcsTestEvent = IcsTestEvent(
uid=uid, start_date=start_date, end_date=end_date, created=start_date, updated=start_date)
yield event
events: List[IcsTestEvent] = list(islice(gen_events(), events_count))
result: IcsTestCalendar = IcsTestCalendar(events)
return result
test_calendar: IcsTestCalendar = gen_test_calendar(1000)
ics_test_calendar: str = test_calendar.render()
converter = CalendarConverter()
converter.loads(ics_test_calendar)
def bench(num_iters=1000):
def make_wrapper(func):
@functools.wraps(func)
def wrapper(*args, **kw):
times = []
for _ in range(num_iters):
t0 = time.perf_counter_ns()
result = func(*args, **kw)
t1 = time.perf_counter_ns()
times.append(t1 - t0)
best = min(times)
avg = round(sum(times) / num_iters, 2)
median = statistics.median(times)
print(
f'{func.__name__} x {num_iters} => best: {best} ns, \tavg: {avg} ns, \tmedian: {median} ns')
return result
return wrapper()
return make_wrapper
@bench(num_iters=500)
def events_to_gcal():
converter.events_to_gcal()