Best Python code snippet using pandera_python
sundial.py
Source:sundial.py  
1# coding: utf-82import pytz3from datetime import date, datetime, timedelta4from astral import Astral5from app.config import config, logger6from app.helpers import Singleton7class Sundial(metaclass=Singleton):8    """9    Calculates sunrise/sunset schedule.10    Because there is not enough light when the sun rises (or almost sets),11    it considers it is `DAY` when time of the day is between sunrise + offset12    and sunset - offset.13    Offset can be configured in the `config.ini`. See `DAY_LIGHT_OFFSET`14    """15    DAY = 116    NIGHT = 217    def __init__(self):18        city_name = config.get('ASTRA_CITY')19        a = Astral()20        a.solar_depression = 'civil'21        self.__city = a[city_name]22        self.__today = None23        self.__sun = None24        self.__mode = None25        self.__timezone = pytz.timezone(self.__city.timezone)26        self.is_day()27    def is_day(self, delta=None):28        if self.__today != date.today():29            # Date change, retrieve today schedule30            self.__sun = self.__city.sun(date=date.today(), local=True)31            self.__today = date.today()32            logger.debug('Dawn:    %s' % str(self.__sun['dawn']))33            logger.debug('Sunrise: %s' % str(self.__sun['sunrise']))34            logger.debug('Sunset:  %s' % str(self.__sun['sunset']))35            logger.debug('Dusk:    %s' % str(self.__sun['dusk']))36        datetime_check = datetime.now(tz=self.__timezone)37        beginning_of_day = self.__sun['sunrise'] + timedelta(minutes=int(38            config.get('DAY_LIGHT_OFFSET')))39        end_of_day = self.__sun['sunset'] - timedelta(minutes=int(40            config.get('DAY_LIGHT_OFFSET')))41        if delta:42            datetime_check = datetime_check - delta43        is_day_ = beginning_of_day <= datetime_check < end_of_day44        self.__mode = self.DAY if is_day_ else self.NIGHT45        return is_day_46    @property47    def mode(self):48        return self.__mode49    @property50    def human_readable_mode(self):51        return 'Day' if self.__mode == self.DAY else 'Night'52    @property53    def sun(self):...cache.py
Source:cache.py  
1from datetime import datetime2import os3import time4BIN_DELIMITER = '|%|%|'5class FileCache:6    def __init__(self, basepath):7        self.basepath = basepath8    def insert(self, key, *blobs):9        if os.path.exists(os.path.join(self.basepath, key)):10            self.remove(key)11        with open(os.path.join(self.basepath, key), 'wb') as f:12            for i, blob in enumerate(blobs):13                f.write(blob)14                if i < len(blobs)-1:15                    f.write(BIN_DELIMITER)16    def get(self, key):17        if not os.path.exists(os.path.join(self.basepath, key)):18            return None19        with open(os.path.join(self.basepath, key), 'rb') as f:20            ret = f.read()21        return ret.split(BIN_DELIMITER)22    def remove(self, key):23        os.remove(os.path.join(self.basepath, key))24class MemCache:25    def __init__(self):26        self._items = {}27    def insert(self, key, *blobs):28        self._items[key] = blobs29    def get(self, key):30        return self._items.get(key)31    def remove(self, key):32        if self._items.has_key(key):33            del self._items[key]34class TTLCache:35    def __init__(self, backing_cache):36        self.backing_cache = backing_cache37    def insert(self, ttl, key, *blobs):38        if not ttl:39            ttl = 6040        timestamp = str(time.time() + (ttl * 60))41        self.backing_cache.insert(key, timestamp, *blobs)42    def get(self, key, datetime_check=None):43        try:44            timestamp, item = self.backing_cache.get(key)45            item_expiry = datetime.fromtimestamp(float(timestamp))46        except (ValueError, TypeError):47            return None48        if not datetime_check:49            datetime_check = datetime.fromtimestamp(time.time())50        if item_expiry < datetime_check:51            self.remove(key)52            return None53        else:54            return item55    def remove(self, key):...test.py
Source:test.py  
1from notifier import datetime_check2def test_module():3    assert datetime_check(0, '01.01.2005', '07:30') == 'The first date has been reached! ( 01.01.2005 - 07:30 )'4    assert datetime_check(1, '24.07.1995', '16:45') == 'The second date has been reached! ( 24.07.1995 - 16:45 )'5    assert datetime_check(2, '04.05.2012', '21:00') == 'The third date has been reached! ( 04.05.2012 - 21:00 )'6    assert datetime_check(3, '04.09.2024', '09:00') == 'The fourth date will reach! ( 04.09.2024 - 09:00 )'7    print('All test cases PASSED!')8if __name__ == '__main__':...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
