🐍 python playground class

python

fukurou

the supreme coder
ADMIN
Python:
# the PlayGround cls is not a core class but very useful
class enumTimes(Enum):
    DATE = auto()
    DAY = auto()
    YEAR = auto()
    HOUR = auto()
    MINUTES = auto()
    SECONDS = auto()


class PlayGround:
    def __init__(self):
        self.week_days = {1: 'sunday',
                          2: 'monday',
                          3: 'tuesday',
                          4: 'wednesday',
                          5: 'thursday',
                          6: 'friday',
                          7: 'saturday',
                          }
        self.dayOfMonth = {1: "first_of", 2: "second_of", 3: "third_of", 4: "fourth_of", 5: "fifth_of", 6: "sixth_of",
                           7: "seventh_of",
                           8: "eighth_of", 9: "nineth_of", 10: "tenth_of", 11: "eleventh_of", 12: "twelveth_of",
                           13: "thirteenth_of",
                           14: "fourteenth_of", 15: "fifteenth_of", 16: "sixteenth_of", 17: "seventeenth_of",
                           18: "eighteenth_of",
                           19: "nineteenth_of", 20: "twentyth_of", 21: "twentyfirst_of", 22: "twentysecond_of",
                           23: "twentythird_of",
                           24: "twentyfourth_of", 25: "twentyfifth_of", 26: "twentysixth_of", 27: "twentyseventh_of",
                           28: "twentyeighth_of",
                           29: "twentynineth_of", 30: "thirtyth_of", 31: "thirtyfirst_of"}

    def getCurrentTimeStamp(self) -> str:
        '''This method returns the current time (hh:mm)'''
        right_now = datetime.datetime.now()
        return str(right_now.hour) + ":" + str(right_now.minute)

    def getMonthAsInt(self) -> int:
        '''This method returns the current month (MM)'''
        right_now = datetime.datetime.now()
        return right_now.month

    def getDayOfTheMonthAsInt(self) -> int:
        '''This method returns the current day (dd)'''
        right_now = datetime.datetime.now()
        return right_now.day

    def getYearAsInt(self) -> int:
        '''This method returns the current year (yyyy)'''
        right_now = datetime.datetime.now()
        return right_now.year

    def getDayAsInt(self) -> int:
        '''This method returns the current day of the week (1, 2, ... 7)'''
        right_now = datetime.datetime.now()
        return right_now.isoweekday()

    def getMinutes(self) -> str:
        '''This method returns the current minutes (mm)'''
        right_now = datetime.datetime.now()
        return str(right_now.minute)

    def getSeconds(self) -> str:
        '''This method returns the current seconds (ss)'''
        right_now = datetime.datetime.now()
        return str(right_now.second)

    def getDayOfDWeek(self) -> str:
        '''This method returns the current day of the week as a word (monday, ...)'''
        right_now = datetime.datetime.now()
        return calendar.day_name[right_now.weekday()]

    def translateMonthDay(self, day: int) -> str:
        '''This method returns the current day of the month as a word (first_of, ...)'''
        return self.dayOfMonth.get(day, "")

    def getSpecificTime(self, time_variable: enumTimes) -> str:
        '''This method returns the current specific date in words (eleventh_of June 2021, ...)'''
        enum_temp = time_variable.name
        if enum_temp == "DATE":
            right_now = datetime.datetime.now()
            output = self.translateMonthDay(right_now.day) + " " + calendar.month_name[right_now.month] + " " + str(
                right_now.year)
        elif enum_temp == "HOUR":
            output = str(datetime.datetime.now().hour)
        elif enum_temp == "SECONDS":
            output = str(datetime.datetime.now().second)
        elif enum_temp == "MINUTES":
            output = str(datetime.datetime.now().minute)
        elif enum_temp == "YEAR":
            output = str(datetime.datetime.now().year)
        else:
            output = ""
        return output

    def getSecondsAsInt(self) -> int:
        '''This method returns the current seconds'''
        right_now = datetime.datetime.now()
        return right_now.second

    def getMinutesAsInt(self) -> int:
        '''This method returns the current minutes'''
        right_now = datetime.datetime.now()
        return right_now.minute

    def getHoursAsInt(self) -> int:
        '''This method returns the current hour'''
        right_now = datetime.datetime.now()
        return right_now.hour

    def getFutureInXMin(self, extra_minutes: int) -> str:
        '''This method returns the date in x minutes'''
        right_now = datetime.datetime.now()
        final_time = right_now + datetime.timedelta(minutes=extra_minutes)
        return str(final_time)

    def getPastInXMin(self, less_minutes: int) -> str:
        '''This method returns the date x minutes before'''
        right_now = datetime.datetime.now()
        final_time = right_now - datetime.timedelta(minutes=less_minutes)
        return str(final_time)

    def getFutureHour(self, startHour: int, addedHours: int) -> int:
        '''This method returns the hour in x hours from the starting hour'''
        return (startHour + addedHours) % 24

    def getFutureFromXInYMin(self, to_add: int, start: str) -> str:
        '''This method returns the time (hh:mm) in x minutes the starting time (hh:mm)'''
        values = start.split(":")
        times_to_add = (int(values[1]) + to_add) // 60
        new_minutes = (int(values[1]) + to_add) % 60
        new_time = str((int(values[0]) + times_to_add) % 24) + ":" + str(new_minutes)
        return new_time

    def timeInXMinutes(self, x: int) -> str:
        '''This method returns the time (hh:mm) in x minutes'''
        right_now = datetime.datetime.now()
        final_time = right_now + datetime.timedelta(minutes=x)
        return str(final_time.hour) + ":" + str(final_time.minute)

    def isDayTime(self) -> bool:
        '''This method returns true if it's daytime (6-18)'''
        return 5 < datetime.datetime.now().hour < 19

    def smallToBig(self, *a) -> bool:
        for i in range(len(a) - 1):
            if a[i] > a[i + 1]:
                return False
        return True

    def partOfDay(self) -> str:
        '''This method returns which part of the day it is (morning, ...)'''
        hour: int = self.getHoursAsInt()
        if self.smallToBig(5, hour, 12):
            return "morning"
        elif self.smallToBig(11, hour, 17):
            return "afternoon"
        elif self.smallToBig(16, hour, 21):
            return "evening"
        return "night"

    def convertToDay(self, number: int) -> str:
        '''This method converts the week number to the weekday name'''
        return self.week_days.get(number, "")

    def isNight(self) -> bool:
        '''This method returns true if it's night (21-5)'''
        hour: int = self.getHoursAsInt()
        return hour > 20 or hour < 6

    def getTomorrow(self) -> str:
        '''This method returns tomorrow'''
        right_now = datetime.datetime.now()
        if (right_now.weekday == 6):
            return "Monday"
        return calendar.day_name[right_now.weekday() + 1]

    def getYesterday(self) -> str:
        '''This method returns yesterday'''
        right_now = datetime.datetime.now()
        if right_now.weekday == 0:
            return "Sunday"
        return calendar.day_name[right_now.weekday() - 1]

    def getGMT(self) -> int:
        '''This method returns the local GMT'''
        right_now = datetime.datetime.now()
        timezone = int(str(right_now.astimezone())[-6:-3])
        return timezone

    def getLocal(self) -> str:
        '''This method returns the local time zone'''
        return datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo
 
Top