Best Python code snippet using avocado_python
models.py
Source:models.py  
1import datetime2import requests3def get_date(timestamp):4    try:5        timestamp = int(timestamp)6        time = datetime.datetime.utcfromtimestamp(timestamp)7        return time.date()8    except ValueError:9        return None10def get_timestamp(date):11    date = datetime.datetime(year=date.year, month=date.month, day=date.day)12    return int((date - datetime.datetime.utcfromtimestamp(0)).total_seconds())13class ContractLine:14    STATUS_RUNNING = '4'15    STATUS_CLOSED = '5'16    def __init__(self, data):17        self.pk = int(data['id'])18        self.ref = data['ref']19        self.product = int(data['fk_product'])20        self.status = data['statut']21        self.label = data['product_label']22        self.description = data['description']23        self.start = get_date(data['date_start'])24        self.end = get_date(data['date_end'])25        self.quantity = int(data['qty'])26        self.unit_price = float(data['subprice'])27    def create(self, dol, contract):28        if self.pk != -1:29            return self30        obj = {31            'fk_product': self.product,32            'statut': self.status,33            'description': self.description,34            'date_start': get_timestamp(self.start),35            'date_end': get_timestamp(self.end),36            'qty': self.quantity,37            'subprice': self.unit_price,38        }39        self.pk = dol.post(f'/contracts/{contract}/lines', obj)40        obj = {41            'datestart': get_timestamp(self.start),42            'dateend': get_timestamp(self.end),43        }44        result = dol.put(f'/contracts/{contract}/lines/{self.pk}/activate', data=obj)45        return self46    def __str__(self):47        result = f'{self.quantity}x {self.ref} - {self.label}'48        result = f'{result} ({self.start} to {self.end})'49        return result50    def __repr__(self):51        return self.__str__()52class Contract:53    STATUS_DRAFT = '0'54    STATUS_VALIDATED = '1'55    def __init__(self, data):56        self.pk = data['id']57        self.ref = data['ref']58        self.status = data['statut']59        self.third_party_id = data['socid']60        self.date = get_date(int(data['date_contrat']))61        self.commercial_signature_id = data['commercial_signature_id']62        self.commercial_suivi_id = data['commercial_suivi_id']63        self.lines = [ContractLine(x) for x in data['lines']]64        self._is_closed = None65        self._is_expired = None66    def is_closed(self):67        if self._is_closed:68            return self._is_closed69        self._is_closed = True70        for line in self.lines:71            if line.status == ContractLine.STATUS_RUNNING:72                self._is_closed = False73                break74        return self._is_closed75    def is_expired(self):76        if self._is_expired:77            return self.is_expired78        if self.is_closed():79            self._is_expired = False80            return self._is_expired81        self._is_expired = False82        for line in self.lines:83            now = datetime.datetime.utcnow().date()84            if line.status == ContractLine.STATUS_RUNNING:85                if line.end and (line.end < now or line.end == now):86                    self._is_expired = True87                    break88        return self._is_expired89    def create(self, dol):90        if self.pk != -1:91            return self92        obj = {93            'socid': self.third_party_id,94            'date_contrat': get_timestamp(self.date),95            'commercial_signature_id': self.commercial_signature_id,96            'commercial_suivi_id': self.commercial_suivi_id,97        }98        self.pk = dol.post('/contracts', obj)99        dol.post(f'/contracts/{self.pk}/validate', {'notrigger': 0})100        for line in self.lines:101            line.create(dol, self.pk)102        result = dol.get(f'/contracts/{self.pk}')103        self = Contract(result)104        return self105    def end(self, dol):106        result = dol.post(f'/contracts/{self.pk}/close', {'notrigger': '0'})107        return result108    def __str__(self):109        result = f'{self.pk}: {self.ref} ({self.date}):'110        for line in self.lines:111            result = f'{result}\n\t{line}'112        return result113    def __repr__(self):...test_zmq_ttl_cache.py
Source:test_zmq_ttl_cache.py  
...60        self._test_in_operator()61    def test_in_operator_without_executor(self):62        self.cache._executor.stop()63        self._test_in_operator()64    def _is_expired(self, key):65        with self.cache._lock:66            _, expiration_time = self.cache._cache[key]67            return self.cache._is_expired(expiration_time, time.time())68    def test_executor(self):69        self.cache.add(1)70        self.assertEqual([1], sorted(self.cache._cache.keys()))71        self.assertFalse(self._is_expired(1))72        time.sleep(0.75)73        self.assertEqual(1, self.cache._update_cache.call_count)74        self.cache.add(2)75        self.assertEqual([1, 2], sorted(self.cache._cache.keys()))76        self.assertFalse(self._is_expired(1))77        self.assertFalse(self._is_expired(2))78        time.sleep(0.75)79        self.assertEqual(2, self.cache._update_cache.call_count)80        self.cache.add(3)81        if 1 in self.cache:82            self.assertEqual([1, 2, 3], sorted(self.cache._cache.keys()))83            self.assertTrue(self._is_expired(1))84        else:85            self.assertEqual([2, 3], sorted(self.cache._cache.keys()))86        self.assertFalse(self._is_expired(2))87        self.assertFalse(self._is_expired(3))88        time.sleep(0.75)89        self.assertEqual(3, self.cache._update_cache.call_count)90        self.assertEqual([3], sorted(self.cache._cache.keys()))...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!!
