Best Python code snippet using ATX
utils.py
Source:utils.py
...115 sgr_start, sgr_end = sgr, "0"116 else:117 sgr_start, sgr_end = sgr118 return f"\x1B[{sgr_start}m{string}\x1B[{sgr_end}m"119def try_attr(120 obj: Any,121 name: str,122 *,123 default: Any,124) -> Any:125 try:126 return getattr(obj, name)127 except Exception:128 return default129_bool_map = {130 False: ["0", "false", "off", "disable", "no", "n"],131 True: ["1", "true", "on", "enable", "yes", "y"],132}133_bool_map = {v: k for k in _bool_map.keys() for v in _bool_map[k]}134def try_bool(135 obj: Any,136 *,137 default: _T,138) -> bool | _T:139 if isinstance(obj, str):140 try:141 return _bool_map[obj.lower()]142 except KeyError:143 return default144 else:145 try:146 return bool(obj)147 except Exception:148 return default149def try_name(150 obj: Any,151 *,152 default: _T,153) -> str | _T:154 name = try_attr(obj, "__qualname__", default=None) or try_attr(obj, "__name__", default=None)155 if not name:156 return default157 module = try_attr(obj, "__module__", default=None)158 if module and isinstance(module, str) and module not in ("__main__", "builtins"):159 module_names = module.split(".")160 i = len(module_names)161 while i:162 module_test = ".".join(module_names[:i])163 try:164 module_type = sys.modules[module_test]165 except KeyError:166 break167 obj_test = module_type168 for part in name.split("."):169 obj_test = try_attr(obj_test, part, default=None)170 if obj_test is None:171 break172 if obj_test is not obj:173 break174 module = module_test175 i -= 1176 name = f"{module}.{name}"177 return name178def try_repr(179 obj: Any,180 *,181 default: _T,182) -> str | _T:183 try:...
mod_trend_picontrol.py
Source:mod_trend_picontrol.py
1# mod_trend_picontol.py2import xarray as xr3import numpy as np4import mod_loc as loc5def try_attr(attr, ds_attrs):6 '''Check if an attribute is present and print its value'''7 try:8 print(f'{attr} : {ds_attrs[attr]}')9 except:10 print(f'No {attr} attribute')11def info_branching(ds_attrs):12 print('Check info about piControl branching:')13 14 # Check which MIP it is15 try:16 mip = ds_attrs['mip_era']17 except:18 mip = ds_attrs['project_id']19 try_attr('parent_experiment_id', ds_attrs)20 21 if mip == 'CMIP5':22 try_attr('parent_experiment_rip', ds_attrs)23 try_attr('branch_time', ds_attrs)24 elif mip == 'CMIP6':25 try_attr('parent_variant_label', ds_attrs)26 try_attr('branch_time_in_child', ds_attrs)27 try_attr('branch_time_in_parent', ds_attrs)28 try_attr('parent_time_units', ds_attrs)29 30def conv_time_pic_hist(MIP, y_ds, hist_ds_attr, verbose=False):31 ''' Find the time conversion between the piControl and the historical 32 simulations'''33 34 if verbose:35 info_branching(hist_ds_attr)36 37 try:38 if MIP == 'cmip5':39 conv_pic_hist = float(y_ds.time[0]) - float(hist_ds_attr['branch_time'])40 elif MIP == 'cmip6':41 attrs = {'units': hist_ds_attr['parent_time_units']}42 time_flt = [float(hist_ds_attr['branch_time_in_parent'])]...
Users.py
Source:Users.py
1from flask import Flask,request,session,g,redirect,url_for, \2 abort, render_template,flash3#database connection4import os5import sqlite36from passlib.hash import pbkdf2_sha256 #hash7import OTP8path_db = os.getcwd() + '/db/flask_db.db'9DATABASE = path_db10SECRET_KEY = 'development key' #secure sessions of client-side11class Users:12 usersCounter = 013 def __init__(self):14 #print "Constructor for Users!"15 Users.usersCounter += 116 def setName(self, new_name):#an den valeis to self sou girnaei error17 self.name = new_name18 def getName(self):19 return self.name20 21 22 def setUsername(self,new_username):23 self.username = new_username24 def getUsername(self):25 return self.username26 27 28 def setSurname(self, new_surname):29 self.surname = new_surname30 def getSurname(self):31 return self.surname32 33 34 def setAttr(self, new_attr):35 self.attr = new_attr36 def getAttr(self):37 return self.attr38 def login(self): 39 error = None#watch this....40 41 if request.method == 'POST':42 conn = sqlite3.connect(DATABASE)43 in_loop = False44 try_name = request.form['username']45 try_pass = request.form['password']46 try_attr = request.form['attr']47 if not try_name or not try_pass:48 error = "You need to fill all the fields!"49 return render_template('index.html',error=error)50 if try_attr == "admin":51 cursor = conn.execute('select * from ADMINS where USERNAME=?',[try_name])52 elif try_attr == "seller":53 cursor = conn.execute('select * from SELLERS where USERNAME=?',[try_name])54 elif try_attr == "client":55 cursor = conn.execute('select * from CLIENTS where USERNAME=?',[try_name])56 57 for row in cursor:58 in_loop = True59 what_about_pass = pbkdf2_sha256.verify(try_pass,row[2])60 61 if what_about_pass == False:62 error = "Invalid password"63 return render_template('index.html',error=error)64 #if request.form['attr'] == row[3] and what_about_pass == True:#einai autos pou leei...65 #what are u ??66 if request.form['attr'] == "admin" and what_about_pass:67 #session['logged_in'] = True#pige sto flaskr/otp_control68 session['username'] = row[1]69 session['id'] = row[0]70 session['email'] = row[5]71 session['xar'] = 'admin'#to xaraktiristiko tou72 conn.close()#den theloume tin sindesi allo...73 session['otp'] = OTP.function_otp(session['email'])#steile email enan random 5psifio74 75 return render_template('otp.html')76 elif request.form['attr'] == "seller" and what_about_pass:77 #session['logged_in'] = True#pige sto flaskr/otp_control78 session['username'] = row[1]79 session['id'] = row[0]80 session['email'] = row[5]81 session['xar'] = 'seller'#to xaraktiristiko tou82 conn.close()#den theloume tin sindesi allo...83 session['otp'] = OTP.function_otp(session['email'])#steile email enan random 5psifio84 85 return render_template('otp.html')86 elif request.form['attr'] == "client" and what_about_pass:87 #session['logged_in'] = True#pige sto flaskr/otp_control88 session['username'] = row[1]89 session['id'] = row[0]90 session['email'] = row[5]91 session['xar'] = 'client'#to xaraktiristiko tou92 conn.close()#den theloume tin sindesi allo...93 session['otp'] = OTP.function_otp(session['email'])#steile email enan random 5psifio94 95 return render_template('otp.html')96 97 if in_loop == False:98 error = "Invalid username or attritube!"#not in loop99 return render_template('index.html',error=error)100 else:#useless else101 error = None102 return render_template('index.html',error=error)103 104 #return render_template('login.html',error=error)105 return render_template('index.html',error=error)106 ...
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!!