Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code readability #765

Merged
merged 2 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@

def _final_version(parsed_version):
for part in parsed_version:
if (part[:1] == '*') and (part not in _final_parts):
if (part.startswith('*')) and (part not in _final_parts):
return False
return True
index = setuptools.package_index.PackageIndex(
Expand Down
16 changes: 9 additions & 7 deletions src/icalendar/parser_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ def from_unicode(value: ICAL_TYPE, encoding='utf-8') -> bytes:
:return: The bytes representation of the value
"""
if isinstance(value, bytes):
value = value
return value
elif isinstance(value, str):
try:
value = value.encode(encoding)
return value.encode(encoding)
except UnicodeEncodeError:
value = value.encode('utf-8', 'replace')
return value
return value.encode('utf-8', 'replace')
else:
return value


def to_unicode(value: ICAL_TYPE, encoding='utf-8-sig') -> str:
Expand All @@ -29,10 +30,11 @@ def to_unicode(value: ICAL_TYPE, encoding='utf-8-sig') -> str:
return value
elif isinstance(value, bytes):
try:
value = value.decode(encoding)
return value.decode(encoding)
except UnicodeDecodeError:
value = value.decode('utf-8-sig', 'replace')
return value
return value.decode('utf-8-sig', 'replace')
else:
return value


def data_encode(data: Union[ICAL_TYPE, dict, list], encoding=DEFAULT_ENCODING) -> bytes:
Expand Down
2 changes: 1 addition & 1 deletion src/icalendar/prop.py
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,7 @@ def __new__(cls, month:Union[str, int]):
month_index = int(month)
leap = False
else:
if not month[-1] == "L" and month[:-1].isdigit():
if month[-1] != "L" and month[:-1].isdigit():
raise ValueError(f"Invalid month: {month!r}")
month_index = int(month[:-1])
leap = True
Expand Down
10 changes: 5 additions & 5 deletions src/icalendar/tests/prop/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ def test_prop_vDDDLists(self):
from icalendar.prop import vDDDLists

dt_list = vDDDLists.from_ical("19960402T010000Z")
self.assertTrue(isinstance(dt_list, list))
self.assertIsInstance(dt_list, list)
self.assertEqual(len(dt_list), 1)
self.assertTrue(isinstance(dt_list[0], datetime))
self.assertIsInstance(dt_list[0], datetime)
self.assertEqual(str(dt_list[0]), "1996-04-02 01:00:00+00:00")

p = "19960402T010000Z,19960403T010000Z,19960404T010000Z"
Expand All @@ -45,7 +45,7 @@ def test_prop_vDDDLists(self):
self.assertEqual(dt_list.to_ical(), b"20000101T000000,20001111T000000")

instance = vDDDLists([])
self.assertFalse(instance == "value")
self.assertNotEqual(instance, "value")

def test_prop_vDate(self):
from icalendar.prop import vDate
Expand Down Expand Up @@ -149,7 +149,7 @@ def test_prop_vRecur(self):
self.assertEqual(vRecur(r).to_ical(), b"FREQ=DAILY;COUNT=10;INTERVAL=2")

r = vRecur.from_ical(
"FREQ=YEARLY;INTERVAL=2;BYMONTH=1;BYDAY=-SU;" "BYHOUR=8,9;BYMINUTE=30"
"FREQ=YEARLY;INTERVAL=2;BYMONTH=1;BYDAY=-SU;BYHOUR=8,9;BYMINUTE=30"
)
self.assertEqual(
r,
Expand All @@ -165,7 +165,7 @@ def test_prop_vRecur(self):

self.assertEqual(
vRecur(r).to_ical(),
b"FREQ=YEARLY;INTERVAL=2;BYMINUTE=30;BYHOUR=8,9;BYDAY=-SU;" b"BYMONTH=1",
b"FREQ=YEARLY;INTERVAL=2;BYMINUTE=30;BYHOUR=8,9;BYDAY=-SU;BYMONTH=1",
)

r = vRecur.from_ical("FREQ=WEEKLY;INTERVAL=1;BYWEEKDAY=TH")
Expand Down
6 changes: 3 additions & 3 deletions src/icalendar/tests/test_icalendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_long_lines(self):
)
self.assertEqual(
Contentlines.from_ical(
"A faked\r\n long line\r\nAnd another " "lin\r\n\te that is folded\r\n"
"A faked\r\n long line\r\nAnd another lin\r\n\te that is folded\r\n"
),
["A faked long line", "And another line that is folded", ""],
)
Expand Down Expand Up @@ -112,7 +112,7 @@ def test_contentline_class(self):
)

c = Contentline(
"ATTENDEE;CN=Max Rasmussen;ROLE=REQ-PARTICIPANT:" "MAILTO:[email protected]"
"ATTENDEE;CN=Max Rasmussen;ROLE=REQ-PARTICIPANT:MAILTO:[email protected]"
)
self.assertEqual(
c.parts(),
Expand All @@ -124,7 +124,7 @@ def test_contentline_class(self):
)
self.assertEqual(
c.to_ical().decode("utf-8"),
"ATTENDEE;CN=Max Rasmussen;ROLE=REQ-PARTICIPANT:" "MAILTO:[email protected]",
"ATTENDEE;CN=Max Rasmussen;ROLE=REQ-PARTICIPANT:MAILTO:[email protected]",
)

# and back again
Expand Down
4 changes: 2 additions & 2 deletions src/icalendar/tests/test_unit_caselessdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ def test_CaselessDict(self):
self.assertEqual(ncd.get("key1"), "val1")
self.assertEqual(ncd.get("key3", "NOT FOUND"), "val3")
self.assertEqual(ncd.get("key4", "NOT FOUND"), "NOT FOUND")
self.assertTrue("key4" in ncd)
self.assertIn("key4", ncd)

del ncd["key4"]
self.assertFalse("key4" in ncd)
self.assertNotIn("key4", ncd)

ncd.update({"key5": "val5", "KEY6": "val6", "KEY5": "val7"})
self.assertEqual(ncd["key6"], "val6")
Expand Down
2 changes: 1 addition & 1 deletion src/icalendar/tests/test_unit_parser_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_parser_tools_to_unicode(self):
self.assertEqual(to_unicode(b"\xc6\xb5"), "\u01b5")
self.assertEqual(to_unicode(b"\xc6\xb5", encoding="ascii"), "\u01b5")
self.assertEqual(to_unicode(1), 1)
self.assertEqual(to_unicode(None), None)
self.assertIsNone(to_unicode(None))

def test_parser_tools_from_unicode(self):
self.assertEqual(from_unicode("\u01b5", encoding="ascii"), b"\xc6\xb5")
Expand Down
12 changes: 6 additions & 6 deletions src/icalendar/tests/test_unit_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ def test_tools_UIDGenerator(self):

txt = uid.to_ical()
length = 15 + 1 + 16 + 1 + 11
self.assertTrue(len(txt) == length)
self.assertTrue(b"@example.com" in txt)
self.assertEqual(len(txt), length)
self.assertIn(b"@example.com", txt)

# You should at least insert your own hostname to be more compliant
uid = g.uid("Example.ORG")
txt = uid.to_ical()
self.assertTrue(len(txt) == length)
self.assertTrue(b"@Example.ORG" in txt)
self.assertEqual(len(txt), length)
self.assertIn(b"@Example.ORG", txt)

# You can also insert a path or similar
uid = g.uid("Example.ORG", "/path/to/content")
txt = uid.to_ical()
self.assertTrue(len(txt) == length)
self.assertTrue(b"-/path/to/[email protected]" in txt)
self.assertEqual(len(txt), length)
self.assertIn(b"-/path/to/[email protected]", txt)


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion src/icalendar/timezone/equivalent_timezone_ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def generate_tree(
with file.open("w") as f:
f.write(f"'''This file is automatically generated by {Path(__file__).name}'''\n")
f.write("import datetime\n\n")
f.write(f"\nlookup = ")
f.write("\nlookup = ")
pprint(lookup, stream=f)
f.write("\n\n__all__ = ['lookup']\n")

Expand Down
14 changes: 7 additions & 7 deletions src/icalendar/timezone/tzp.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,16 @@ def clean_timezone_id(self, tzid: str) -> str:
"""
return tzid.strip("/")

def timezone(self, id: str) -> Optional[datetime.tzinfo]:
def timezone(self, tz_id: str) -> Optional[datetime.tzinfo]:
"""Return a timezone with an id or None if we cannot find it."""
_unclean_id = id
id = self.clean_timezone_id(id)
tz = self.__provider.timezone(id)
_unclean_id = tz_id
tz_id = self.clean_timezone_id(tz_id)
tz = self.__provider.timezone(tz_id)
if tz is not None:
return tz
if id in WINDOWS_TO_OLSON:
tz = self.__provider.timezone(WINDOWS_TO_OLSON[id])
return tz or self.__provider.timezone(_unclean_id) or self.__tz_cache.get(id)
if tz_id in WINDOWS_TO_OLSON:
tz = self.__provider.timezone(WINDOWS_TO_OLSON[tz_id])
return tz or self.__provider.timezone(_unclean_id) or self.__tz_cache.get(tz_id)

def uses_pytz(self) -> bool:
"""Whether we use pytz at all."""
Expand Down
Loading