Best Python code snippet using avocado_python
app.py
Source:app.py
...71 meta = f'<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1.0">'72 favicon = f'<link rel="shortcut icon" href="../static/favicon/favicon.ico">'73 74 title = f'<title>Urban Life Tactics | WFL.00</title>'75 folium_map.get_root().html.add_child(folium.Element(meta))76 folium_map.get_root().html.add_child(folium.Element(favicon))77 folium_map.get_root().html.add_child(folium.Element(title))78 font_css = f'<style>@import url("https://fonts.googleapis.com/css2?family=Bebas+Neue&display=swap");</style>'79 folium_map.get_root().html.add_child(folium.Element(font_css))80 81 script_credits = f'<style> #credits {{position: absolute;z-index: 999;bottom: 5px;width: 300px;}}</style>'82 img_credits = f'<img id="credits" src="../static/images/credits.png">'83 folium_map.get_root().html.add_child(folium.Element(script_credits))84 folium_map.get_root().html.add_child(folium.Element(img_credits))85 86 srcipt_enter = f'<style> #enter {{position: absolute;z-index: 999;bottom: 75px;width: 150px;right:75px;overflow: hidden;-webkit-transform: scale(1);transform: scale(1);-webkit-transition: .3s ease-in-out;transition: .3s ease-in-out;filter:grayscale();}}</style>'87 srcipt_enter_hover = f'<style> #enter:hover {{-webkit-transform: scale(2);transform: scale(1.2);filter: none;}}</style>'88 folium_map.get_root().html.add_child(folium.Element(srcipt_enter))89 folium_map.get_root().html.add_child(folium.Element(srcipt_enter_hover))90 91 script_bro = f'<style> .bro {{position: absolute;font-size: 15px;top: 40%;left: 350px;transform: translate(-50%, -50%);z-index: 999;}}</style>'92 script_demo = f'<style> #demo {{position: absolute;opacity:0}}</style>'93 folium_map.get_root().html.add_child(folium.Element(script_bro))94 folium_map.get_root().html.add_child(folium.Element(script_demo))95 script_left= f'<style> .left {{position: absolute;font-family:"Bebas Neue";font-size: 100px;top: 12%;left: 175px;z-index: 999;}}</style>'96 folium_map.get_root().html.add_child(folium.Element(script_left))97 98 script_bro_input = f'<style> .bro input {{height:25px;width: 68px;text-align: center;font-size: 15px;border:1px solid #000000; background-color:#000000; color:white; border-radius:4px;display: inline-block;vertical-align: middle; margin-bottom:10px;}}</style>'99 folium_map.get_root().html.add_child(folium.Element(script_bro_input))100 form_int = f'<form action = "/result_wlfcero" method="POST" id="nameform">'101 demo_poly = f'<input type="text" id="demo" name="demo" value = {poly}>'102 iter_area = f'<div class="left">INPUTS</div>'103 104 iter_poly = f'<div class="bro"><div><label> Life time lapse:_ </label><input type="number" id="iter" name="iter" value = "50"></div>'105 va1_poly = f'<div><label> Speed Weight:_ </label><input type="number" id="va1" name="va1" value = "1" min="1" ></div>'106 va2_poly = f'<div><label> Vegetation Weight:_ </label><input type="number" id="va2" name="va2" value = "1" min="1" ></div>'107 va3_poly = f'<div><label> Climate Weight:_ </label><input type="number" id="va3" name="va3" value = "1" min="1"></div>'108 va4_poly = f'<div><label> Openness Weight:_ </label><input type="number" id="va4" name="va4" value = "1" min="1"></div>'109 va5_poly = f'<div><label> Closeness Weight:_ </label><input type="number" id="va5" name="va5" value = "1" min="1"></div>'110 va6_poly = f'<div><label> Betweenness Weight:_ </label><input type="number" id="va6" name="va6" value = "1" min="1"></div></div></form>'111 button_finish=f'<input type="image" form="nameform" id="enter" src="../static/images/enter.png">'112 folium_map.get_root().html.add_child(folium.Element(form_int))113 folium_map.get_root().html.add_child(folium.Element(demo_poly))114 folium_map.get_root().html.add_child(folium.Element(iter_area))115 folium_map.get_root().html.add_child(folium.Element(iter_poly))116 folium_map.get_root().html.add_child(folium.Element(va1_poly))117 folium_map.get_root().html.add_child(folium.Element(va2_poly))118 folium_map.get_root().html.add_child(folium.Element(va3_poly))119 folium_map.get_root().html.add_child(folium.Element(va4_poly))120 folium_map.get_root().html.add_child(folium.Element(va5_poly))121 folium_map.get_root().html.add_child(folium.Element(va6_poly))122 folium_map.get_root().html.add_child(folium.Element(button_finish))123 minimap = plugins.MiniMap(position='topright')124 folium_map.add_child(minimap)125 folium_map.save('templates/03_map_wlfcero.html')126 return render_template('03_map_wlfcero.html')127@app.route("/result_wlfcero", methods=['GET', 'POST'])128def result_wlfcero():129 poly = request.form['demo']130 iter = int(request.form['iter'])131 va1 = int(request.form['va1'])132 va2 = int(request.form['va2'])133 va3 = int(request.form['va3'])134 va4 = int(request.form['va4'])135 va5 = int(request.form['va5'])136 va6 = int(request.form['va6'])137 location, a , df= test_wlfcero.getEdges(poly, iter, va1, va2, va3, va4, va5, va6)138 folium_map = folium.Map(location=location,tiles="cartodbpositron", zoom_start=16)139 140 title = f'<title>Urban Life Tactics | WFL.00</title>'141 folium_map.get_root().html.add_child(folium.Element(title))142 script_credits = f'<style> #credits {{position: absolute;z-index: 999;bottom: 5px;width: 300px;}}</style>'143 img_credits = f'<img id="credits" src="../static/images/credits.png">'144 folium_map.get_root().html.add_child(folium.Element(script_credits))145 folium_map.get_root().html.add_child(folium.Element(img_credits))146 147 minimap = plugins.MiniMap()148 folium_map.add_child(minimap)149 lines = a150 features = [151 {152 "type": "Feature",153 "geometry": {154 "type": "LineString",155 "coordinates": line["coordinates"],156 },157 "properties": {158 "times": line["dates"],159 "popup": line["popup"],160 "style": {161 "color": line["color"],162 # "weight": line["weight"] if "weight" in line else 5,163 },164 },165 }166 for line in lines167 168 ]169 plugins.TimestampedGeoJson(170 {171 "type": "FeatureCollection",172 "features": features,173 },174 period="PT1S",175 add_last_point=False,176 auto_play=False,177 loop=False,178 max_speed=10,179 loop_button=True,180 date_options="ss",181 # 'YYYY-MM-DD HH:mm:ss',182 time_slider_drag_update=True,183 duration="P2M",184 ).add_to(folium_map)185 #specify the min and max values of your data186 colormap = branca.colormap.linear.YlOrRd_09.scale(0, 1)187 colormap = colormap.to_step(index=[0, 0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9, 1])188 colormap.caption = 'Traffic'189 colormap.add_to(folium_map)190 folium_map.save('templates/04_result_wlfcero.html')191 return render_template('04_result_wlfcero.html')192@app.route('/wlfuno', methods=['GET', 'POST'])193def wlfuno():194 return render_template("02_map_wlfuno.html")195@app.route('/trans_wflone', methods=['GET', 'POST'])196def trans_wflone():197 parameters = {198 'name',199 "travel_time",200 'speed_kph',201 'length',202 'widths',203 'width_deviations',204 'openness',205 'closeness400',206 'closeness_global',207 'betweenness_metric_n',208 'straightness',209 'food',210 'education',211 'transport',212 'shop',213 'vegetation',214 'value_temperature',215 'value_windSpeed',216 'value_windDirection',217 'value_humidity',218 'value_skyCover',219 'value_earthTemperature',220 'value_precipitationWater',221 'value_directIlluminance',222 'value_diffuseIlluminance',223 'value_irradiation',224 }225 poly = request.form['demo']226 print(poly)227 column = request.form['browser']228 print(column)229 proj_streets = transic_wflone.getEdges(poly)230 # folium_map = folium.Map(location=(40.425986238284544, -3.698757358239611),tiles="cartodbpositron", zoom_start=15)231 folium_map = proj_streets.explore(232 column =column,233 cmap='Spectral',234 tooltip_kwds=dict(labels=True),235 tooltip=parameters,236 popup=parameters,237 k=10,238 name="graph",239 tiles=None,240 style_kwds=dict(weight=5),241 )242 folium.TileLayer('cartodbpositron',opacity=1).add_to(folium_map )243 meta = f'<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1.0">'244 favicon = f'<link rel="shortcut icon" href="../static/favicon/favicon.ico">'245 246 title = f'<title>Urban Life Tactics | WFL.01</title>'247 folium_map.get_root().html.add_child(folium.Element(meta))248 folium_map.get_root().html.add_child(folium.Element(favicon))249 folium_map.get_root().html.add_child(folium.Element(title))250 font_css = f'<style>@import url("https://fonts.googleapis.com/css2?family=Bebas+Neue&display=swap");</style>'251 folium_map.get_root().html.add_child(folium.Element(font_css))252 253 script_credits = f'<style> #credits {{position: absolute;z-index: 999;bottom: 5px;width: 300px;}}</style>'254 img_credits = f'<img id="credits" src="../static/images/credits.png">'255 folium_map.get_root().html.add_child(folium.Element(script_credits))256 folium_map.get_root().html.add_child(folium.Element(img_credits))257 258 srcipt_enter = f'<style> #enter {{position: absolute;z-index: 999;bottom: 75px;width: 150px;right:75px;overflow: hidden;-webkit-transform: scale(1);transform: scale(1);-webkit-transition: .3s ease-in-out;transition: .3s ease-in-out;filter:grayscale();}}</style>'259 srcipt_enter_hover = f'<style> #enter:hover {{-webkit-transform: scale(2);transform: scale(1.2);filter: none;}}</style>'260 folium_map.get_root().html.add_child(folium.Element(srcipt_enter))261 folium_map.get_root().html.add_child(folium.Element(srcipt_enter_hover))262 263 script_bro = f'<style> .bro {{position: absolute;font-size: 15px;top: 40%;left: 350px;transform: translate(-50%, -50%);z-index: 999;}}</style>'264 script_demo = f'<style> #demo {{position: absolute;opacity:0}}</style>'265 folium_map.get_root().html.add_child(folium.Element(script_bro))266 folium_map.get_root().html.add_child(folium.Element(script_demo))267 script_left= f'<style> .left {{position: absolute;font-family:"Bebas Neue";font-size: 100px;top: 12%;left: 175px;z-index: 999;}}</style>'268 folium_map.get_root().html.add_child(folium.Element(script_left))269 270 script_bro_input = f'<style> .bro input {{height:25px;width: 68px;text-align: center;font-size: 15px;border:1px solid #000000; background-color:#000000; color:white; border-radius:4px;display: inline-block;vertical-align: middle; margin-bottom:10px;}}</style>'271 folium_map.get_root().html.add_child(folium.Element(script_bro_input))272 form_int = f'<form action = "/result_wflone" method="POST" id="nameform">'273 demo_poly = f'<input type="text" id="demo" name="demo" value = {poly}>'274 iter_area = f'<div class="left">INPUTS</div>'275 276 iter_poly = f'<div class="bro"><div><label> Life time lapse:_ </label><input type="number" id="iter" name="iter" value = "50"></div>'277 va1_poly = f'<div><label> Speed Weight:_ </label><input type="number" id="va1" name="va1" value = "1" min="1" ></div>'278 va2_poly = f'<div><label> Vegetation Weight:_ </label><input type="number" id="va2" name="va2" value = "1" min="1" ></div>'279 va3_poly = f'<div><label> Climate Weight:_ </label><input type="number" id="va3" name="va3" value = "1" min="1"></div>'280 va4_poly = f'<div><label> Openness Weight:_ </label><input type="number" id="va4" name="va4" value = "1" min="1"></div>'281 va5_poly = f'<div><label> Closeness Weight:_ </label><input type="number" id="va5" name="va5" value = "1" min="1"></div>'282 va6_poly = f'<div><label> Betweenness Weight:_ </label><input type="number" id="va6" name="va6" value = "1" min="1"></div></div></form>'283 button_finish=f'<input type="image" form="nameform" id="enter" src="../static/images/enter.png">'284 folium_map.get_root().html.add_child(folium.Element(form_int))285 folium_map.get_root().html.add_child(folium.Element(demo_poly))286 folium_map.get_root().html.add_child(folium.Element(iter_area))287 folium_map.get_root().html.add_child(folium.Element(iter_poly))288 folium_map.get_root().html.add_child(folium.Element(va1_poly))289 folium_map.get_root().html.add_child(folium.Element(va2_poly))290 folium_map.get_root().html.add_child(folium.Element(va3_poly))291 folium_map.get_root().html.add_child(folium.Element(va4_poly))292 folium_map.get_root().html.add_child(folium.Element(va5_poly))293 folium_map.get_root().html.add_child(folium.Element(va6_poly))294 folium_map.get_root().html.add_child(folium.Element(button_finish))295 minimap = plugins.MiniMap(position='topright')296 folium_map.add_child(minimap)297 folium_map.save('templates/03_map_wlfone.html')298 return render_template('03_map_wlfone.html')299@app.route("/result_wflone", methods=['GET', 'POST'])300def result_wflone():301 poly = request.form['demo']302 iter = int(request.form['iter'])303 va1 = int(request.form['va1'])304 va2 = int(request.form['va2'])305 va3 = int(request.form['va3'])306 va4 = int(request.form['va4'])307 va5 = int(request.form['va5'])308 va6 = int(request.form['va6'])309 location, a = test_wflone.getEdges(poly, iter, va1, va2, va3, va4, va5, va6 )310 folium_map = folium.Map(location=location,tiles="cartodbpositron", zoom_start=16)311 312 title = f'<title>Urban Life Tactics | WFL.01</title>'313 folium_map.get_root().html.add_child(folium.Element(title))314 script_credits = f'<style> #credits {{position: absolute;z-index: 999;bottom: 5px;width: 300px;}}</style>'315 img_credits = f'<img id="credits" src="../static/images/credits.png">'316 folium_map.get_root().html.add_child(folium.Element(script_credits))317 folium_map.get_root().html.add_child(folium.Element(img_credits))318 319 minimap = plugins.MiniMap()320 folium_map.add_child(minimap)321 lines = a322 features = [323 {324 "type": "Feature",325 "geometry": {326 "type": "LineString",327 "coordinates": line["coordinates"],328 },329 "properties": {330 "times": line["dates"],331 "popup": line["popup"],332 "style": {333 "color": line["color"],334 # "weight": line["weight"],335 },336 },337 }338 for line in lines339 340 ]341 plugins.TimestampedGeoJson(342 {343 "type": "FeatureCollection",344 "features": features,345 },346 period="PT1S",347 add_last_point=False,348 auto_play=False,349 loop=False,350 max_speed=10,351 loop_button=True,352 date_options="ss",353 # 'YYYY-MM-DD HH:mm:ss',354 time_slider_drag_update=True,355 duration="P2M",356 ).add_to(folium_map)357 #specify the min and max values of your data358 colormap = branca.colormap.linear.YlOrRd_09.scale(0, 1)359 colormap = colormap.to_step(index=[0, 0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9, 1])360 colormap.caption = 'Traffic'361 colormap.add_to(folium_map)362 folium_map.save('templates/04_result_wflone.html')363 return render_template('04_result_wflone.html')364@app.route('/wlfsegundo', methods=['GET', 'POST'])365def wlfsegundo():366 return render_template("02_map_wlfsegundo.html")367@app.route('/trans_wlftwo', methods=['GET', 'POST'])368def trans_wlftwo():369 parameters = {370 'name',371 "travel_time",372 'speed_kph',373 'length',374 'widths',375 'width_deviations',376 'openness',377 'closeness400',378 'closeness_global',379 'betweenness_metric_n',380 'straightness',381 'food',382 'education',383 'transport',384 'shop',385 'vegetation',386 'value_temperature',387 'value_windSpeed',388 'value_windDirection',389 'value_humidity',390 'value_skyCover',391 'value_earthTemperature',392 'value_precipitationWater',393 'value_directIlluminance',394 'value_diffuseIlluminance',395 'value_irradiation',396 }397 poly = request.form['demo']398 print(poly)399 column = request.form['browser']400 print(column)401 proj_streets = transic_wfltwo.getEdges(poly)402 # folium_map = folium.Map(location=(40.425986238284544, -3.698757358239611),tiles="cartodbpositron", zoom_start=15)403 folium_map = proj_streets.explore(404 column =column,405 cmap='Spectral',406 tooltip_kwds=dict(labels=True),407 tooltip=parameters,408 popup=parameters,409 k=10,410 name="graph",411 tiles=None,412 style_kwds=dict(weight=5),413 )414 folium.TileLayer('cartodbpositron',opacity=1).add_to(folium_map )415 meta = f'<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1.0">'416 favicon = f'<link rel="shortcut icon" href="../static/favicon/favicon.ico">'417 418 title = f'<title>Urban Life Tactics | WFL.02</title>'419 folium_map.get_root().html.add_child(folium.Element(meta))420 folium_map.get_root().html.add_child(folium.Element(favicon))421 folium_map.get_root().html.add_child(folium.Element(title))422 font_css = f'<style>@import url("https://fonts.googleapis.com/css2?family=Bebas+Neue&display=swap");</style>'423 folium_map.get_root().html.add_child(folium.Element(font_css))424 425 script_credits = f'<style> #credits {{position: absolute;z-index: 999;bottom: 5px;width: 300px;}}</style>'426 img_credits = f'<img id="credits" src="../static/images/credits.png">'427 folium_map.get_root().html.add_child(folium.Element(script_credits))428 folium_map.get_root().html.add_child(folium.Element(img_credits))429 430 srcipt_enter = f'<style> #enter {{position: absolute;z-index: 999;bottom: 75px;width: 150px;right:75px;overflow: hidden;-webkit-transform: scale(1);transform: scale(1);-webkit-transition: .3s ease-in-out;transition: .3s ease-in-out;filter:grayscale();}}</style>'431 srcipt_enter_hover = f'<style> #enter:hover {{-webkit-transform: scale(2);transform: scale(1.2);filter: none;}}</style>'432 folium_map.get_root().html.add_child(folium.Element(srcipt_enter))433 folium_map.get_root().html.add_child(folium.Element(srcipt_enter_hover))434 435 script_bro = f'<style> .bro {{position: absolute;font-size: 15px;top: 40%;left: 350px;transform: translate(-50%, -50%);z-index: 999;}}</style>'436 script_demo = f'<style> #demo {{position: absolute;opacity:0}}</style>'437 folium_map.get_root().html.add_child(folium.Element(script_bro))438 folium_map.get_root().html.add_child(folium.Element(script_demo))439 script_left= f'<style> .left {{position: absolute;font-family:"Bebas Neue";font-size: 100px;top: 12%;left: 175px;z-index: 999;}}</style>'440 folium_map.get_root().html.add_child(folium.Element(script_left))441 442 form_int = f'<form action = "/result_wfltwo" method="POST" id="nameform">'443 demo_poly = f'<input type="text" id="demo" name="demo" value = {poly}>'444 iter_area = f'<div class="left">INPUTS</div>'445 446 iter_poly = f'<div class="bro"><div><label> Nº Iterations:</label><input type="number" id="iter" name="iter" value = "1000"></div>'447 pt_poly = f'<div><label> Population Threshold:</label><input type="number" id="pt" name="pt" value = "0.85" ></div>'448 jt_poly = f'<div><label> Junction Type:</label><input type="number" id="jt" name="jt" value = "1" ></div>'449 bs_poly = f'<div><label> Branching Steps:</label><input type="number" id="bs" name="bs" value = "2"></div>'450 ga_poly = f'<div><label> Growth Angle:</label><input type="number" id="ga" name="ga" value = "2"></div>'451 sbs_poly = f'<div><label> Street Block Size:</label><input type="number" id="sbs" name="sbs" value = "100" ></div></div></form>'452 button_finish=f'<input type="image" form="nameform" id="enter" src="../static/images/enter.png">'453 folium_map.get_root().html.add_child(folium.Element(form_int))454 folium_map.get_root().html.add_child(folium.Element(demo_poly))455 folium_map.get_root().html.add_child(folium.Element(iter_area))456 folium_map.get_root().html.add_child(folium.Element(iter_poly))457 folium_map.get_root().html.add_child(folium.Element(pt_poly))458 folium_map.get_root().html.add_child(folium.Element(jt_poly))459 folium_map.get_root().html.add_child(folium.Element(bs_poly))460 folium_map.get_root().html.add_child(folium.Element(ga_poly))461 folium_map.get_root().html.add_child(folium.Element(sbs_poly))462 folium_map.get_root().html.add_child(folium.Element(button_finish))463 minimap = plugins.MiniMap(position='topright')464 folium_map.add_child(minimap)465 folium_map.save('templates/03_map_wlftwo.html')466 return render_template('03_map_wlftwo.html')467@app.route("/result_wfltwo", methods=['GET', 'POST'])468def result_wfltwo():469 poly = request.form['demo']470 iter = int(request.form['iter'])471 pt = float(request.form['pt'])472 jt = int(request.form['jt'])473 bs = int(request.form['bs'])474 ga = int(request.form['ga'])475 sbs = int(request.form['sbs'])476 location, a = test_wlftwo.getEdges(poly, iter, pt, jt, bs, ga, sbs)477 folium_map = folium.Map(location=location,tiles="cartodbpositron", zoom_start=16)478 479 title = f'<title>Urban Life Tactics | WFL.02</title>'480 folium_map.get_root().html.add_child(folium.Element(title))481 script_credits = f'<style> #credits {{position: absolute;z-index: 999;bottom: 5px;width: 300px;}}</style>'482 img_credits = f'<img id="credits" src="../static/images/credits.png">'483 folium_map.get_root().html.add_child(folium.Element(script_credits))484 folium_map.get_root().html.add_child(folium.Element(img_credits))485 486 minimap = plugins.MiniMap()487 folium_map.add_child(minimap)488 489 lines = a490 features = [491 {492 "type": "Feature",493 "geometry": {494 "type": "LineString",495 "coordinates": line["coordinates"],496 },497 "properties": {498 "times": line["dates"],...
grid.py
Source:grid.py
2import random3class Grid(tk.Canvas):4 def __init__(self, *args):5 tk.Canvas.__init__(self, *args, bg='white', width=800, height=800, relief='groove')6 self.get_root().grid = self7 8 self.get_root().coord_data = {}9 self.get_root().coord_data['start'] = None10 self.get_root().coord_data['finish'] = None11 12 self.highlight_cache = []13 self.in_search = False14 self.draw()15 16 self.mouse_x, self.mouse_y = 1, 117 self.nearest_cache = 1, 118 self.fill_cache = ""19 20 self.bind("<Motion>", self.motion)21 self.bind("<B1-Motion>", self.place_move)22 self.bind("<ButtonPress-1>", self.place)23 self.bind("<ButtonRelease-1>", self.place_release)24 self.mainloop()25 26 def get_root(self):27 return self.master.master.get_root()28 29 def draw(self, data=None):30 self.reset() 31 self.delete(tk.ALL)32 33 self.get_root().coord_data['start'] = None34 self.get_root().coord_data['finish'] = None35 36 self.w, self.h = 800, 80037 self.wl, self.hl = self.get_root().options['grid_size'][0], self.get_root().options['grid_size'][1]38 self.ws, self.hs = self.w/self.wl, self.h/self.hl39 40 41 self.grid = {}42 for w in range(self.wl):43 self.grid[w] = {}44 for h in range(self.hl):45 self.grid[w][h] = {}46 if data:47 self.grid[w][h]['state'] = data[w][h]48 else:49 self.grid[w][h]['state'] = 050 if self.grid[w][h]['state'] == 0:51 self.grid[w][h]['obj'] = self.create_rectangle(self.ws, self.hs, 0, 0, fill='', width=1)52 elif self.grid[w][h]['state'] == 1:53 self.grid[w][h]['obj'] = self.create_rectangle(self.ws, self.hs, 0, 0, fill='red', width=1)54 self.get_root().coord_data['start'] = w, h55 elif self.grid[w][h]['state'] == 2:56 self.grid[w][h]['obj'] = self.create_rectangle(self.ws, self.hs, 0, 0, fill='green', width=1)57 self.get_root().coord_data['finish'] = w, h58 else:59 self.grid[w][h]['obj'] = self.create_rectangle(self.ws, self.hs, 0, 0, fill='black', width=1)60 self.move(self.grid[w][h]['obj'], (w*self.ws)+0.45, (h*self.hs)+0.5)61 self.reset()62 63 def motion(self, event):64 self.mouse_x, self.mouse_y = event.x, event.y65 66 def get_nearest(self, x1, y1):67 68 69 70 return int(x1/self.ws), int(y1/self.hs)71 72 def place_move(self, event):73 74 if not self.in_search:75 76 try:77 78 if self.get_root().mode == "wall":79 w, h = self.get_nearest(event.x, event.y)80 if (w, h) == self.get_root().coord_data['start']:81 self.get_root().coord_data['start'] = None82 elif (w, h) == self.get_root().coord_data['finish']:83 self.get_root().coord_data['finish'] = None84 self.itemconfig(self.grid[w][h]['obj'], fill='black')85 self.grid[w][h]['state'] = 386 else:87 self.mouse_x, self.mouse_y = event.x, event.y88 89 except Exception as e:90 pass91 92 def place(self, event):93 if not self.in_search:94 95 try:96 97 self.reset()98 99 self.nearest_cache = None100 if self.get_root().mode == "start":101 try:102 self.itemconfig(self.grid[self.get_root().coord_data['start'][0]][self.get_root().coord_data['start'][1]]['obj'], fill='')103 except TypeError:104 pass105 w, h = self.get_nearest(event.x, event.y)106 self.itemconfig(self.grid[w][h]['obj'], fill='red')107 self.grid[w][h]['state'] = 1108 self.get_root().coord_data['start'] = w, h109 110 self.clear_cursor()111 112 elif self.get_root().mode == "finish":113 try:114 self.itemconfig(self.grid[self.get_root().coord_data['finish'][0]][self.get_root().coord_data['finish'][1]]['obj'], fill='')115 except TypeError:116 pass117 w, h = self.get_nearest(event.x, event.y)118 self.itemconfig(self.grid[w][h]['obj'], fill='green')119 self.grid[w][h]['state'] = 2120 self.get_root().coord_data['finish'] = w, h121 122 self.clear_cursor()123 124 elif self.get_root().mode == "wall":125 w, h = self.get_nearest(event.x, event.y)126 self.itemconfig(self.grid[w][h]['obj'], fill='black')127 self.grid[w][h]['state'] = 3128 129 except Exception as e:130 pass131 132 133 def place_release(self, event):134 if not self.in_search:135 self.reset()136 137 def clear_cursor(self):138 self.nearest_cache = -1, -1139 self.get_root().mainframe.deselect_all()140 141 def get(self):142 grid = []143 for w in self.grid:144 grid.append([])145 for h in self.grid[w]:146 if self.grid[w][h]['state'] == 3:147 grid[w].append(1)148 else:149 grid[w].append(0)150 return grid151 152 def reset(self):153 try:154 if not self.in_search:155 self.get_root().status_var.set("\n\n")156 self.nearest_cache = -1, -1157 for coords in self.highlight_cache:158 self.itemconfig(self.grid[coords[0]][coords[1]]['obj'], fill='')159 self.highlight_cache = []160 161 ## full clear, useful for bugs/errors162 for w in self.grid:163 for h in self.grid[w]:164 if (self.grid[w][h]['state']) == 0:165 self.itemconfig(self.grid[w][h]['obj'], fill='')166 except Exception as e:167 pass168 169 170 def finish_search(self, route):171 try:172 for coords in route:173 if not coords == self.get_root().coord_data['start']:174 if not coords == self.get_root().coord_data['finish']:175 self.itemconfig(self.grid[coords[0]][coords[1]]['obj'], fill='red')176 self.get_root().status_var.set("{}\nRoute length {}".format(self.get_root().status_var.get().split("\n")[0], len(route)))177 except Exception:178 pass179 180 def highlight(self, coords):181 try:182 if not coords == self.get_root().coord_data['start']:183 if not coords == self.get_root().coord_data['finish']:184 self.highlight_cache.append(coords) if coords not in self.highlight_cache else self.highlight_cache185 self.itemconfig(self.grid[coords[0]][coords[1]]['obj'], fill='pink')186 self.get_root().status_var.set("Visited {}\n".format(len(self.highlight_cache)))187 except Exception:188 pass189 190 def export_walls(self):191 grid = []192 for w in self.grid:193 grid.append([])194 for h in self.grid[w]:195 grid[w].append(str(self.grid[w][h]['state']))196 grid[w] = ",".join(grid[w])197 grid = "\n".join(grid)198 return grid199 200 def random_draw(self):201 self.reset() 202 self.delete(tk.ALL)203 204 self.get_root().coord_data['start'] = None205 self.get_root().coord_data['finish'] = None206 207 self.w, self.h = 800, 800208 self.wl, self.hl = self.get_root().options['grid_size'][0], self.get_root().options['grid_size'][1]209 self.ws, self.hs = self.w/self.wl, self.h/self.hl210 211 212 self.grid = {}213 for w in range(self.wl):214 self.grid[w] = {}215 for h in range(self.hl):216 self.grid[w][h] = {}217 self.grid[w][h]['state'] = random.choice([0, 0, 0, 0, 3])218 if self.grid[w][h]['state'] == 0:219 self.grid[w][h]['obj'] = self.create_rectangle(self.ws, self.hs, 0, 0, fill='', width=1)220 elif self.grid[w][h]['state'] == 1:221 self.grid[w][h]['obj'] = self.create_rectangle(self.ws, self.hs, 0, 0, fill='red', width=1)222 self.get_root().coord_data['start'] = w, h223 elif self.grid[w][h]['state'] == 2:224 self.grid[w][h]['obj'] = self.create_rectangle(self.ws, self.hs, 0, 0, fill='green', width=1)225 self.get_root().coord_data['finish'] = w, h226 else:227 self.grid[w][h]['obj'] = self.create_rectangle(self.ws, self.hs, 0, 0, fill='black', width=1)228 self.move(self.grid[w][h]['obj'], (w*self.ws)+0.45, (h*self.hs)+0.5)229 self.reset()230 231 def mainloop(self):232 233 if not self.in_search:234 235 try:236 237 try:238 self.itemconfig(self.grid[self.nearest_cache[0]][self.nearest_cache[1]]['obj'], fill='{}'.format(self.fill_cache))239 except Exception:240 pass241 242 w, h = self.get_nearest(self.mouse_x, self.mouse_y)243 self.fill_cache = self.itemcget(self.grid[w][h]['obj'], "fill")244 self.nearest_cache = w, h245 246 247 248 if self.get_root().mode == "start":249 self.itemconfig(self.grid[w][h]['obj'], fill='red')250 elif self.get_root().mode == "finish":251 self.itemconfig(self.grid[w][h]['obj'], fill='green')252 elif self.get_root().mode == "wall":253 self.itemconfig(self.grid[w][h]['obj'], fill='black')254 255 except Exception:256 pass257 258 259 self.after(1, self.mainloop)...
main.py
Source:main.py
...18 # bst.print(traversal_type=Traversal.POSTORDER)19 # bst.print(traversal_type=Traversal.INORDER)20 # print('\n')21 # print('After deleting: ')22 # bst.set_root(bst.delete_node(bst.get_root(), 2))23 # bst.set_root(bst.delete_node(bst.get_root(), 73))24 # bst.set_root(bst.delete_node(bst.get_root(), 3))25 # bst.set_root(bst.delete_node(bst.get_root(), 1))26 # bst.set_root(bst.delete_node(bst.get_root(), 4))27 # bst.print()28 # bst.set_root(bst.delete_node(bst.get_root(), 7))29 # bst.set_root(bst.delete_node(bst.get_root(), 5))30 # bst.set_root(bst.delete_node(bst.get_root(), 10))31 # bst.print()32 bt.set_root(bt.clear_all_roots(bt.get_root()))33 bt.print()34 # bst.set_root(bst.delete_node(bst.get_root(), 1))35 # bst.set_root(bst.delete_node(bst.get_root(), 2))36 # bst.print()37 # bst1 = BinarySearchTree()38 # bst1.insert(3)39 # bst1.insert(5)40 # bst1.print()41 # bst1.root = bst1.delete_node(bst1.get_root(), 3)42 # bst1.root = bst1.delete_node(bst1.get_root(), 5)43 # bst1.print()44 tree = BinaryTree()45 tree.add_to_tree(tree.get_root(), 3)46 tree.add_to_tree(tree.get_root(), 1)47 tree.add_to_tree(tree.get_root(), 2)48 tree.add_to_tree(tree.get_root(), 4)49 tree.add_to_tree(tree.get_root(), 5)50 tree.add_to_tree(tree.get_root(), 10)51 tree.add_to_tree(tree.get_root(), 7)52 tree.add_to_tree(tree.get_root(), 73)53 tree.add_to_tree(tree.get_root(), 23)54 tree.print()55 tree.print(traversal_type=Traversal.REVERSE)56 tr = BinaryTree()57 tr.add(3)58 tr.add(1)59 tr.add(2)60 tr.add(4)61 tr.add(5)62 tr.print()63 # tr.delete(3)64 # tr.delete(1)65 # tr.delete(4)66 # tr.delete(2)67 tr.print()...
WeightedQuickUnion.py
Source:WeightedQuickUnion.py
2 def __init__(self, N):3 self.lst = list(range(N))4 self.sizes = [1] * N56 def get_root(self, ind):7 while ind != self.lst[ind]:8 ind = self.lst[ind]9 return ind1011 def find(self, a, b):12 return self.get_root(a) == self.get_root(b)1314 def union(self, a, b):15 if self.sizes[self.get_root(a)] < self.sizes[self.get_root(b)]:16 self.lst[self.get_root(a)] = self.get_root(b)17 self.sizes[self.get_root(b)] += self.sizes[self.get_root(a)]18 else:19 self.lst[self.get_root(b)] = self.get_root(a)20 self.sizes[self.get_root(a)] += self.sizes[self.get_root(b)]2122first = WeightedQuickUnion(10)2324print(first.union(4,3))25print(first.lst)26
...
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!!