Best Python code snippet using pandera_python
api.py
Source:api.py
...91 return r.iter_lines()92 else:93 return r.text94 def _make_request(self, query_text, start, stop, mode, stream, limit):95 start = self._to_unix(start)96 stop = self._to_unix(stop)97 ts = self._to_unix('now', milliseconds=True)98 ts = str(ts)99 body = json.dumps({100 'query': query_text,101 'from': start,102 'to': stop,103 'mode': {'type': mode},104 'limit': limit105 })106 if self.api_key and self.api_secret:107 msg = self.api_key + body + ts108 sig = hmac.new(self.api_secret.encode(),109 msg.encode(),110 hashlib.sha256).hexdigest()111 headers = {112 'Content-Type': 'application/json',113 'x-logtrust-apikey': self.api_key,114 'x-logtrust-sign': sig,115 'x-logtrust-timestamp': ts116 }117 elif self.oauth_token:118 headers = {119 'Content-Type': 'application/json',120 'Authorization': 'Bearer ' + self.oauth_token}121 elif self.jwt:122 headers = {123 'Content-Type': 'application/json',124 'Authorization': 'jwt ' + self.jwt}125 else:126 raise Exception('No credentials found')127 r = requests.post(128 self.end_point,129 data=body,130 headers=headers,131 stream=stream132 )133 return r134 @staticmethod135 def _null_decorator(f):136 def null_f(v):137 if v == '':138 return None139 else:140 return f(v)141 return null_f142 def _make_type_map(self):143 funcs = {144 'timestamp': lambda t: datetime.datetime.strptime(t.strip(), '%Y-%m-%d %H:%M:%S.%f'),145 'str': str,146 'int8': int,147 'int4': int,148 'float8': float,149 'float4': float,150 'bool': lambda b: b == 'true'151 }152 self._map = defaultdict(lambda: str, {t:self._null_decorator(f) for t,f in funcs.items()})153 def _get_types(self,linq_query,start):154 """155 Gets types of each column of submitted156 """157 # so we don't have stop ts in future as required by API V2158 stop = self._to_unix(start)159 start = stop - 1160 response = self._query(linq_query, start=start, stop=stop, mode='json/compact', limit=1)161 try:162 data = json.loads(response)163 check_status(data)164 except ValueError:165 raise Exception('API V2 response error')166 col_data = data['object']['m']167 type_dict = { k:self._map[v['type']] for k,v in col_data.items() }168 return type_dict169 @staticmethod170 def _to_unix(date, milliseconds=False):171 """172 Convert date to a unix timestamp in seconds173 date: A unix timestamp in second, a python datetime object,174 or string in form 'YYYY-mm-dd'175 """176 if date is None:177 return None178 elif date == 'now':179 epoch = datetime.datetime.now().timestamp()180 elif type(date) == str:181 epoch = pd.to_datetime(date).timestamp()182 elif type(date) == datetime.datetime:183 epoch = date.replace(tzinfo=timezone.utc).timestamp()184 elif isinstance(date, (int,float)):...
dos2unix.py
Source:dos2unix.py
...7"""8import pickle9from pickle import UnpicklingError10# Private Functions11def _to_unix(path):12 """ converts a file to unix endings """13 original = path14 destination = original.replace(".pkl", "_unix.pkl")15 content = ''16 outsize = 017 with open(original, 'rb') as inpath:18 content = inpath.read()19 with open(destination, 'wb') as output:20 for line in content.splitlines():21 outsize += len(line) + 122 output.write(line + str.encode('\n'))23 return destination24# Functions25def pickle_load(path):26 """ Load pickle paths in Python 3 """27 try:28 data = pickle.load(open(path, "rb"))29 return data30 except UnpicklingError:31 unix_path = path.replace(".pkl", "_unix.pkl")32 try:33 data = pickle.load(open(unix_path, "rb"))34 return data35 except FileNotFoundError:36 path = _to_unix(path)37 data = pickle.load(open(path, "rb"))...
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!!