Best Python code snippet using autotest_python
views.py
Source:views.py
1import flask2from flask import render_template, request3from app import app4import viz5import psycopg26conn = psycopg2.connect(dbname='Open_Policing')7cur = conn.cursor()8def generateQuery(stop_date_MIN, stop_date_MAX, driver_gender, driver_age_MIN, driver_age_MAX, \9driver_race_TUPLE, violation_TUPLE, search_conducted, search_type_TUPLE, stop_outcome_TUPLE, \10officer_gender, officer_age_MIN, officer_age_MAX, officer_race_TUPLE, officer_rank_TUPLE, out_of_state):11 add_where = True12 query = 'select county_name, count(*) from fl_stops'13 if stop_date_MIN is not None:14 query = query + ' where stop_date >= ' + str(stop_date_MIN)15 add_where = False16 if stop_date_MAX is not None:17 if add_where:18 query = query + ' where'19 add_where = False20 else:21 query = query + ' AND'22 query = query + ' stop_date <= ' + str(stop_date_MAX)23 if driver_gender is not None:24 if add_where:25 query = query + ' where'26 add_where = False27 else:28 query = query + ' AND'29 query = query + ' driver_gender IS ' + driver_gender30 if driver_age_MIN is not None:31 if add_where:32 query = query + ' where'33 add_where = False34 else:35 query = query + ' AND'36 query = query + ' driver_age >= ' + driver_age_MIN37 if driver_age_MAX is not None:38 if add_where:39 query = query + ' where'40 add_where = False41 else:42 query = query + ' AND'43 query = query + ' driver_age <= ' + driver_age_MAX44 if driver_race_TUPLE is not None:45 if add_where:46 query = query + ' where'47 add_where = False48 else:49 query = query + ' AND'50 query = query + ' driver_race IN ' + driver_race_TUPLE51 if violation_TUPLE is not None:52 if add_where:53 query = query + ' where'54 add_where = False55 else:56 query = query + ' AND'57 query = query + ' violation SIMILAR TO ' + '%(' + '|'.join(violation_TUPLE) + ')%'58 if search_conducted is not None:59 if add_where:60 query = query + ' where'61 add_where = False62 else:63 query = query + ' AND'64 query = query + ' search_conducted = ' + search_conducted65 if search_type_TUPLE is not None:66 if add_where:67 query = query + ' where'68 add_where = False69 else:70 query = query + ' AND'71 query = query + ' search_type IN ' + search_type_TUPLE72 if stop_outcome_TUPLE is not None:73 if add_where:74 query = query + ' where'75 add_where = False76 else:77 query = query + ' AND'78 query = query + ' stop_outcome IN ' + stop_outcome_TUPLE79 if officer_gender is not None:80 if add_where:81 query = query + ' where'82 add_where = False83 else:84 query = query + ' AND'85 query = query + ' officer_gender = ' + officer_gender86 if officer_age_MIN is not None:87 if add_where:88 query = query + ' where'89 add_where = False90 else:91 query = query + ' AND'92 query = query + ' officer_age >= ' + officer_age_MIN93 if officer_age_MAX is not None:94 if add_where:95 query = query + ' where'96 add_where = False97 else:98 query = query + ' AND'99 query = query + ' officer_age <= ' + officer_age_MAX100 if officer_race_TUPLE is not None:101 if add_where:102 query = query + ' where'103 add_where = False104 else:105 query = query + ' AND'106 query = query + ' officer_race IN ' + officer_race_TUPLE107 if officer_rank_TUPLE is not None:108 if add_where:109 query = query + ' where'110 add_where = False111 else:112 query = query + ' AND'113 query = query + ' officer_rank IN ' + officer_rank_TUPLE114 if out_of_state is not None:115 if add_where:116 query = query + ' where'117 add_where = False118 else:119 query = query + ' AND'120 query = query + ' out_of_state = ' + out_of_state121 query = query + ' group by county_name order by county_name;'122 cur.execute(query)123 counties_counts = cur.fetchall()124 counties_dict = dict(counties_counts)125 return counties_dict126@app.route('/')127@app.route('/index')128def index():129 stop_date_MIN = request.args.get('stop_date_MIN', default=None, type=str)130 stop_date_MAX = request.args.get('stop_date_MAX', default=None, type=str)131 driver_gender = request.args.get('driver_gender', default=None, type=str)132 driver_age_MIN = request.args.get('driver_age_MIN', default=None, type=int)133 driver_age_MAX = request.args.get('driver_age_MAX', default=None, type=int)134 driver_race_TUPLE = request.args.getlist('driver_race_TUPLE', type=str)135 violation_TUPLE = request.args.getlist('violation_TUPLE', type=str)136 search_conducted = request.args.get('search_conducted', default=None, type=bool)137 search_type_TUPLE = request.args.get('search_type', default=None, type=tuple)138 stop_outcome_TUPLE = request.args.getlist('stop_outcome_TUPLE', type=str)139 officer_gender = request.args.get('officer_gender', default=None, type=str)140 officer_age_MIN = request.args.get('officer_age_MIN', default=None, type=int)141 officer_age_MAX = request.args.get('officer_age_MAX', default=None, type=int)142 officer_race_TUPLE = request.args.getlist('officer_race_TUPLE', type=str)143 officer_rank_TUPLE = request.args.getlist('officer_rank_TUPLE', type=str)144 out_of_state = request.args.get('out_of_state', default=None, type=bool)145 count_dict = generateQuery(stop_date_MIN, stop_date_MAX, driver_gender, driver_age_MIN, driver_age_MAX, \146 driver_race_TUPLE, violation_TUPLE, search_conducted, search_type_TUPLE, stop_outcome_TUPLE, \147 officer_gender, officer_age_MIN, officer_age_MAX, officer_race_TUPLE, officer_rank_TUPLE, out_of_state)148 viz.generate(count_dict)#viz.generate(count_dict)149 #return str(stop_date_MIN) + str(stop_date_MAX) + str(driver_gender) + str(driver_age_MIN) + str(driver_age_MAX) + \150 # str(driver_race_TUPLE) + str(violation_TUPLE) + str(search_conducted) + str(search_type_TUPLE) + \151 # str(stop_outcome_TUPLE) + str(officer_gender) + str(officer_age_MIN) + str(officer_age_MAX) + \152 # str(officer_race_TUPLE) + str(officer_rank_TUPLE) + str(out_of_state)153 return render_template('index.html')154#@app.route('/results/map.html')155#def show_map():...
__init__.py
Source:__init__.py
...60 c.append(p + '=%s')61 params.append(q)62 conds.append('(' + ' AND '.join(c) + ')')63 conds = '(' + ' OR '.join(conds) + ')'64 sel.add_where([conds] + params)65def find(params, columns=None, limit=None, sort=None):66 srid = config.get('alkis.crs').split(':')[1]67 sel = db.select_statement()68 sel.add_table(index.main_index, 'fs')69 joins = 070 has_name = False71 if columns:72 for col in columns:73 sel.add_column('fs.' + col)74 else:75 sel.add_column('fs.*')76 for k, v in params.items():77 if k == 'gml_id':78 if isinstance(v, (tuple, list)):79 cond = ','.join('%s' for _ in v)80 sel.add_where(['fs.gml_id IN (' + cond + ')'] + list(v))81 elif v is not None:82 sel.add_where(['fs.gml_id = %s', v])83 else:84 sel.add_where('fs.gml_id is NULL')85 elif k in ('gemarkungsnummer', 'flurnummer', 'zaehler', 'nenner', 'flurstuecksfolge'):86 if k == 'gemarkungsnummer':87 k = 'gemarkung_id'88 if v is not None:89 sel.add_where(['fs.' + k + '= %s', v])90 else:91 sel.add_where('fs.' + k + ' is NULL')92 elif k == '_fsnumber':93 _add_fsnumber(sel, v)94 elif k == 'gemeinde':95 sel.add_where(db.star_like('fs.gemeinde', v))96 elif k == 'strasse':97 sel.add_where('addr.fs_id = fs.gml_id')98 adresse.add_filter(sel, 'addr', util.pick(params, ['strasse', 'hausnummer']))99 joins += 1100 elif k in ('vorname', 'nachnameoderfirma'):101 if not has_name:102 sel.add_table(index.name_index, 'na')103 sel.add_where('na.fs_id = fs.gml_id')104 joins += 1105 has_name = True106 if k == 'nachnameoderfirma':107 sel.add_where(db.star_like('na.nachname', v))108 if k == 'vorname':109 sel.add_where(db.star_like('na.vorname', v))110 elif k == 'minflaeche':111 sel.add_where(['amtlicheflaeche >= %s', v])112 elif k == 'maxflaeche':113 sel.add_where(['amtlicheflaeche <= %s', v])114 elif k in ('bounds', 'bounds_within'):115 # WKB or WKT?116 if v.startswith((chr(0), chr(1), '0')):117 expr = '%s'118 else:119 expr = _f('ST_GeomFromText(%s,{srid})')120 verb = 'ST_Within' if k == 'bounds_within' else 'ST_Intersects'121 sel.add_where([_f('{verb}(wkb_geometry,{expr})'), v])122 elif k == 'x':123 try:124 x = float(params['x'])125 y = float(params['y'])126 except (KeyError, ValueError):127 x = y = 0128 point = _f("ST_GeomFromText('POINT({x} {y})',{srid})", x, y)129 sel.add_where(_f('ST_Contains(wkb_geometry,{point})'))130 if joins:131 sel.distinct(True)132 if sort:133 sel.add_sort(sort)134 if limit:135 sel.limit(limit)136 # debug.pp(sel.sql())137 for r in sel.fetch():138 for f in 'buchung', 'nutzung', 'nutzung_areas', 'lage', 'gebaeude':139 if f in r:140 r[f] = json.loads(r[f])141 if 'wkt_geometry' in r:142 r['geometry'] = r['wkt_geometry']143 yield r...
test_delete_statement.py
Source:test_delete_statement.py
...37 self.assertTrue(six.text_type(ds).startswith('DELETE FROM table'), six.text_type(ds))38 self.assertTrue(str(ds).startswith('DELETE FROM table'), str(ds))39 def test_where_clause_rendering(self):40 ds = DeleteStatement('table', None)41 ds.add_where(Column(db_field='a'), EqualsOperator(), 'b')42 self.assertEqual(six.text_type(ds), 'DELETE FROM table WHERE "a" = %(0)s', six.text_type(ds))43 def test_context_update(self):44 ds = DeleteStatement('table', None)45 ds.add_field(MapDeleteClause('d', {1: 2}, {1: 2, 3: 4}))46 ds.add_where(Column(db_field='a'), EqualsOperator(), 'b')47 ds.update_context_id(7)48 self.assertEqual(six.text_type(ds), 'DELETE "d"[%(8)s] FROM table WHERE "a" = %(7)s')49 self.assertEqual(ds.get_context(), {'7': 'b', '8': 3})50 def test_context(self):51 ds = DeleteStatement('table', None)52 ds.add_where(Column(db_field='a'), EqualsOperator(), 'b')53 self.assertEqual(ds.get_context(), {'0': 'b'})54 def test_range_deletion_rendering(self):55 ds = DeleteStatement('table', None)56 ds.add_where(Column(db_field='a'), EqualsOperator(), 'b')57 ds.add_where(Column(db_field='created_at'), GreaterThanOrEqualOperator(), '0')58 ds.add_where(Column(db_field='created_at'), LessThanOrEqualOperator(), '10')59 self.assertEqual(six.text_type(ds), 'DELETE FROM table WHERE "a" = %(0)s AND "created_at" >= %(1)s AND "created_at" <= %(2)s', six.text_type(ds))60 ds = DeleteStatement('table', None)61 ds.add_where(Column(db_field='a'), EqualsOperator(), 'b')62 ds.add_where(Column(db_field='created_at'), InOperator(), ['0', '10', '20'])63 self.assertEqual(six.text_type(ds), 'DELETE FROM table WHERE "a" = %(0)s AND "created_at" IN %(1)s', six.text_type(ds))64 ds = DeleteStatement('table', None)65 ds.add_where(Column(db_field='a'), NotEqualsOperator(), 'b')66 self.assertEqual(six.text_type(ds), 'DELETE FROM table WHERE "a" != %(0)s', six.text_type(ds))67 def test_delete_conditional(self):68 where = [WhereClause('id', EqualsOperator(), 1)]69 conditionals = [ConditionalClause('f0', 'value0'), ConditionalClause('f1', 'value1')]70 ds = DeleteStatement('table', where=where, conditionals=conditionals)71 self.assertEqual(len(ds.conditionals), len(conditionals))72 self.assertEqual(six.text_type(ds), 'DELETE FROM table WHERE "id" = %(0)s IF "f0" = %(1)s AND "f1" = %(2)s', six.text_type(ds))73 fields = ['one', 'two']74 ds = DeleteStatement('table', fields=fields, where=where, conditionals=conditionals)...
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!!