Created
February 15, 2021 10:32
-
-
Save zeddee/3f2f4b5216b2c1cc901897e3f66fcb98 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pprint import pprint | |
import calendar | |
from enum import Enum, IntEnum, Flag, IntFlag | |
pp = pprint | |
# source: https://levelup.gitconnected.com/python-tricks-i-can-not-live-without-87ae6aff3af8 | |
def sep() -> None: | |
print("=======================================") | |
def sets_demo() -> list: | |
myWord = "NanananananBatman" | |
deduped = set(myWord) # {'N', 't', 'B', 'n', 'a', 'm'} | |
return list(deduped) # ['B', 't', 'm', 'a', 'n', 'N'] | |
def sets_diff() -> set: | |
""" | |
Set objects also support mathematical operations like: | |
union, intersection, difference, and symmetric difference. | |
from python docs: https://docs.python.org/3/tutorial/datastructures.html#sets | |
.. code-block:: python | |
>>> a = set('abracadabra') | |
>>> b = set('alacazam') | |
>>> a # unique letters in a | |
{'a', 'r', 'b', 'c', 'd'} | |
>>> a - b # letters in a but not in b | |
{'r', 'd', 'b'} | |
>>> a | b # letters in a or b or both | |
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'} | |
>>> a & b # letters in both a and b | |
{'a', 'c'} | |
>>> a ^ b # letters in a or b but not both | |
{'r', 'd', 'b', 'm', 'z', 'l'} | |
""" | |
permissions_original = {"is_admin", "can_post", "can_edit", "can_view"} | |
permissions_new = {"is_member", "can_edit", "can_view", "can_edit_settings"} | |
return permissions_original.difference(permissions_new) | |
#return permissions_original, permissions_new | |
def month_find_last_day() -> tuple[int, int]: | |
""" | |
calendar exposes lots of useful stuff re: … well, calendars. | |
calendar.HTMLCalendar() returns a HTML calendar; should experiment with this. | |
calendar.HTMLCalendar(firstweekday=0).formatyearpage(self, theyear, width=3, css='calendar.css', encoding=None) returns a full html page | |
calendar.monthcalendar returns a matrix | |
calendar.month(year, month) returns a calenar as a string | |
""" | |
YEAR=2021 | |
MONTH=2 | |
return calendar.monthrange(YEAR, MONTH) | |
# python has enums! | |
# we can use Enum, IntEnum, Flag, or IntFlag | |
class StatusIntEnum(IntEnum): | |
OPEN = 1 | |
IN_PROGRESS = 2 | |
CLOSED = 3 | |
class Status(Enum): | |
OPEN = "open" | |
IN_PROGRESS = "in progress" | |
CLOSED = "closed" | |
class StatusHandler(): | |
def handle_open(self) -> str: | |
return "Handling open status" | |
def handle_in_progress(self) -> str: | |
return "Handling in-progress status" | |
def handle_closed(self) -> str: | |
return "Handle closed status" | |
handlers = { | |
StatusIntEnum.OPEN.value: handle_open, | |
StatusIntEnum.IN_PROGRESS.value: handle_in_progress, | |
StatusIntEnum.CLOSED.value: handle_closed, | |
Status.OPEN.value: handle_open, | |
Status.IN_PROGRESS.value: handle_in_progress, | |
Status.CLOSED.value: handle_closed, | |
} | |
def handle_status_change(self, status) -> str: | |
if status not in self.handlers: | |
raise Exception(f"No handlers found for status: {status}") | |
handler = self.handlers.get(status) | |
return handler(self) | |
def change_status(): | |
handler = StatusHandler() | |
print("Status:") | |
for i in Status: | |
status = handler.handle_status_change(i.value) | |
print(f"\t{i.name}: {status}") | |
print("StatusIntEnums:") | |
for i in StatusIntEnum: | |
status = handler.handle_status_change(i.value) | |
print(f"\t{i.name}: {status}") | |
if __name__ == "__main__": | |
pp("harro") | |
sep() | |
pp(sets_demo()) | |
sep() | |
pp(sets_diff()) | |
sep() | |
pp(month_find_last_day()) | |
sep() | |
change_status() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment