Best Python code snippet using molecule_python
metabase_source.py
Source:metabase_source.py
...39 json_body["Content-Type"] = "application/json"40 session = requests.Session()41 session.headers.update(json_body)42 return session43 def _get_data(self, endpoint, params=None, metadata=False):44 print(f"getting data from {self.url}/api/{endpoint}")45 res = self.session.get(f"{self.url}/api/{endpoint}", params=params)46 res_json = res.json()47 if isinstance(res_json, list):48 data = res_json49 else:50 if 'data' in res_json:51 data = res_json.get('data')52 else:53 data = [res_json]54 if params:55 for d in data:56 d['request_params'] = str(params)57 for row in data:58 yield row59 def _get_database_ids(self):60 databases = self._get_data('database')61 database_ids = [d['id'] for d in databases]62 return database_ids63 def _get_fields_endpoints(self):64 return [f"database/{id}/fields" for id in self._get_database_ids()]65 def _get_field_data(self):66 for p in self._get_fields_endpoints():67 data = self._get_data(p)68 for row in data:69 yield row70 def get_table_rows(self, table, params={}):71 """this function returns an interator of rows"""72 #if it's a simple call, we return the result in a list73 simple_endpoints = [dict(endpoint='util/stats', table='stats'),74 dict(endpoint='card', table='cards'),75 dict(endpoint='collection', table='collections'),76 dict(endpoint='dashboard', table='dashboards'),77 dict(endpoint='database', table='databases'),78 dict(endpoint='metric', table='metrics'),79 dict(endpoint='pulse', table='pulses'),80 dict(endpoint='table', table='tables'),81 dict(endpoint='segment', table='segments'),82 dict(endpoint='user', table='users', params={'status': 'all'}),83 dict(endpoint='activity', table='activity'),84 dict(endpoint='util/logs', table='logs'),85 ]86 if table in [e['table'] for e in simple_endpoints]:87 endpoint = [d for d in simple_endpoints if table == d['table']][0]88 data = self._get_data(endpoint.get('endpoint'), params=endpoint.get('params'))89 return data90 #for tables that need more calls, we return a generator91 if table == 'fields':92 return self._get_field_data()93 def tables(self, params={}):94 """95 A list of tables that can be passed to get_table_rows() to get an interator of rows96 Metabase publishes logs to a buffer that keeps a running window.97 depending on how many events are generated in your instance,98 you might want to schedule a read every few minutes or every few days.99 They are set to "append-only" mode, so deduplication will be done by you by your optimal cost scenario100 event_window_tables = ['activity', 'logs']101 pass them to get_endpoint_rows() to get an iterator of rows.102 These are stateful and should be replaced...
pytest_data_test.py
Source:pytest_data_test.py
1# -*- coding: utf-8 -*-2import pytest3from pytest_data import get_data, use_data4def test_only_default():5 data = _get_data({'a': 1})6 assert data == {'a': 1}7def test_module():8 data = _get_data({'a': 1, 'b': 2}, module={'b': 20, 'c': 30})9 assert data == {'a': 1, 'b': 20, 'c': 30}10def test_cls():11 data = _get_data({'a': 1, 'b': 2}, cls={'b': 20, 'c': 30})12 assert data == {'a': 1, 'b': 20, 'c': 30}13def test_function():14 data = _get_data({'a': 1, 'b': 2}, function={'b': 20, 'c': 30})15 assert data == {'a': 1, 'b': 20, 'c': 30}16def test_module_and_cls():17 data = _get_data({'a': 1, 'b': 2}, module={'b': 20, 'c': 30}, cls={'c': 300, 'd': 400})18 assert data == {'a': 1, 'b': 20, 'c': 300, 'd': 400}19def test_module_and_function():20 data = _get_data({'a': 1, 'b': 2}, module={'b': 20, 'c': 30}, function={'c': 300, 'd': 400})21 assert data == {'a': 1, 'b': 20, 'c': 300, 'd': 400}22def test_cls_and_function():23 data = _get_data({'a': 1, 'b': 2}, cls={'b': 20, 'c': 30}, function={'c': 300, 'd': 400})24 assert data == {'a': 1, 'b': 20, 'c': 300, 'd': 400}25def test_module_and_cls_and_function():26 data = _get_data({'a': 1, 'b': 2}, module={'b': 20, 'c': 30}, cls={'c': 300, 'd': 400}, function={'d': 4000, 'e': 5000})27 assert data == {'a': 1, 'b': 20, 'c': 300, 'd': 4000, 'e': 5000}28def test_list_only_default():29 data = _get_data([{'a': 1}])30 assert data == [{'a': 1}]31def test_list_mix_raises_module():32 with pytest.raises(ValueError):33 _get_data({'a': 1}, module=[{'b': 20}])34 with pytest.raises(ValueError):35 _get_data([{'a': 1}], module={'b': 20})36def test_list_mix_raises_cls():37 with pytest.raises(ValueError):38 _get_data({'a': 1}, cls=[{'b': 20}])39 with pytest.raises(ValueError):40 _get_data([{'a': 1}], cls={'b': 20})41def test_list_mix_raises_function():42 with pytest.raises(ValueError):43 _get_data({'a': 1}, function=[{'b': 20}])44 with pytest.raises(ValueError):45 _get_data([{'a': 1}], function={'b': 20})46def test_list_module():47 data = _get_data([{'a': 1, 'b': 2}], module=[{'b': 20, 'c': 30}])48 assert data == [{'a': 1, 'b': 20, 'c': 30}]49def test_list_cls():50 data = _get_data([{'a': 1, 'b': 2}], cls=[{'b': 20, 'c': 30}])51 assert data == [{'a': 1, 'b': 20, 'c': 30}]52def test_list_function():53 data = _get_data([{'a': 1, 'b': 2}], function=[{'b': 20, 'c': 30}])54 assert data == [{'a': 1, 'b': 20, 'c': 30}]55def test_list_more_values_module():56 data = _get_data([{'a': 1, 'b': 2}], module=[{'b': 20, 'c': 30}, {'b': 21, 'c': 31}])57 assert data == [{'a': 1, 'b': 20, 'c': 30}, {'a': 1, 'b': 21, 'c': 31}]58def test_list_more_values_cls():59 data = _get_data([{'a': 1, 'b': 2}], cls=[{'b': 20, 'c': 30}, {'b': 21, 'c': 31}])60 assert data == [{'a': 1, 'b': 20, 'c': 30}, {'a': 1, 'b': 21, 'c': 31}]61def test_list_more_values_function():62 data = _get_data([{'a': 1, 'b': 2}], function=[{'b': 20, 'c': 30}, {'b': 21, 'c': 31}])63 assert data == [{'a': 1, 'b': 20, 'c': 30}, {'a': 1, 'b': 21, 'c': 31}]64def test_list_more_values_module_and_cls():65 data = _get_data(66 [{'a': 1, 'b': 2}],67 module=[{'b': 20, 'c': 30}, {'b': 21, 'c': 31}],68 cls=[{'c': 300, 'd': 400}],69 )70 assert data == [71 {'a': 1, 'b': 20, 'c': 300, 'd': 400},72 {'a': 1, 'b': 21, 'c': 300, 'd': 400},73 ]74def test_list_more_values_module_and_function():75 data = _get_data(76 [{'a': 1, 'b': 2}],77 module=[{'b': 20, 'c': 30}, {'b': 21, 'c': 31}, {'b': 22, 'c': 32}],78 function=[{'c': 300, 'd': 400}, {'c': 301, 'd': 401}],79 )80 assert data == [81 {'a': 1, 'b': 20, 'c': 300, 'd': 400},82 {'a': 1, 'b': 21, 'c': 301, 'd': 401},83 {'a': 1, 'b': 22, 'c': 300, 'd': 400},84 ]85def test_list_more_values_cls_and_function():86 data = _get_data(87 [{'a': 10, 'b': 20}, {'a': 11, 'b': 21}],88 cls=[{'b': 200, 'c': 300}],89 function=[{'c': 3000, 'd': 4000}, {'c': 3001, 'd': 4001}],90 )91 assert data == [92 {'a': 10, 'b': 200, 'c': 3000, 'd': 4000},93 {'a': 11, 'b': 200, 'c': 3001, 'd': 4001},94 ]95def test_list_more_values_module_cls_and_function():96 data = _get_data(97 [{'a': 10, 'b': 20}, {'a': 11, 'b': 21}],98 module=[{'b': 200, 'c': 300}, {'b': 201, 'c': 301}, {'b': 202, 'c': 302}],99 cls=[{'c': 3000, 'd': 4000}],100 function=[{'d': 40000, 'e': 50000}, {'d': 40001, 'e': 50001}],101 )102 assert data == [103 {'a': 10, 'b': 200, 'c': 3000, 'd': 40000, 'e': 50000},104 {'a': 11, 'b': 201, 'c': 3000, 'd': 40001, 'e': 50001},105 {'a': 10, 'b': 202, 'c': 3000, 'd': 40000, 'e': 50000},106 ]107def _get_data(default, module=None, cls=None, function=None):108 class request:109 class module:110 if module:111 foo = module112 class cls:113 if cls:114 foo = cls115 class function:116 if function:117 foo = function118 return get_data(request, 'foo', default)119def test_use_data_func_stays_same():120 def test_func():121 pass...
pymetservices.py
Source:pymetservices.py
1import requests2import re3DEFAULT_BASE = 'http://metservice.com/publicData'4TOWN_SLUG_REGEX = re.compile(r'\/([\w-]+)$')5def _get_data(endpoint: str, base: str):6 try:7 resp = requests.get(base + endpoint)8 resp.raise_for_status()9 return resp.json()10 except requests.HTTPError:11 # TODO: error handling?12 return None13def _city_data(city: str, endpoint: str, base: str):14 return _get_data(endpoint.format(city), base)15def get_cities_list(base = DEFAULT_BASE):16 resp = _get_data("/webdata/towns-cities", base)17 search = resp["layout"]["search"]18 towns = {}19 for island in search:20 for region in island["items"]:21 for town in region["children"]:22 town_url = town["url"]23 towns[town["label"]] = TOWN_SLUG_REGEX.search(town_url).group(1)24 return towns25def getLocalForecast(city: str, base: str = DEFAULT_BASE):26 return _get_data(f'/localForecast{city}', base)27def getSunProtectionAlert(city: str, base: str = DEFAULT_BASE):28 return _get_data(f'/sunProtectionAlert{city}', base)29def getOneMinObs(city: str, base: str = DEFAULT_BASE):30 return _get_data(f'/oneMinObs_{city}', base)31def getHourlyObsAndForecast(city: str, base: str = DEFAULT_BASE):32 return _get_data(f'/hourlyObsAndForecast_{city}', base)33def getLocalObs(city: str, base: str = DEFAULT_BASE):34 return _get_data(f'/localObs_{city}', base)35def getTides(city: str, base: str = DEFAULT_BASE):36 return _get_data(f'/tides_{city}', base)37def getWarnings(city: str, base: str = DEFAULT_BASE):38 return _get_data(f'/warningsForRegion3_urban.{city}', base)39def getRises(city: str, base: str = DEFAULT_BASE):40 return _get_data(f'/riseSet_{city}', base)41def getPollen(city: str, base: str = DEFAULT_BASE):42 return _get_data(f'/pollen_town_{city}', base)43def getDaily(city: str, base: str = DEFAULT_BASE):...
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!!