Best Python code snippet using slash
genotypes.py
Source:genotypes.py
1from collections import namedtuple2Genotype = namedtuple('Genotype', 'normal normal_concat reduce reduce_concat')3PRIMITIVES = [4 'none',5 'max_pool_3x3',6 'avg_pool_3x3',7 'skip_connect',8 'sep_conv_3x3',9 'sep_conv_5x5',10 'dil_conv_3x3',11 'dil_conv_5x5'12]13PDARTS_TS_CIFAR10 = Genotype(14 normal=[('sep_conv_3x3', 0), 15 ('sep_conv_3x3', 1), 16 ('sep_conv_3x3', 0), 17 ('dil_conv_3x3', 2), 18 ('skip_connect', 0), 19 ('dil_conv_3x3', 3), 20 ('skip_connect', 2), 21 ('dil_conv_5x5', 4)], 22 normal_concat=range(2, 6), 23 reduce=[('avg_pool_3x3', 0), 24 ('dil_conv_3x3', 1), 25 ('avg_pool_3x3', 0), 26 ('dil_conv_5x5', 2), 27 ('skip_connect', 0), 28 ('sep_conv_3x3', 3), 29 ('sep_conv_3x3', 0), 30 ('dil_conv_3x3', 2)], 31 reduce_concat=range(2, 6))32PDARTS_TS_CIFAR100 = Genotype(33 normal=[('sep_conv_3x3', 0), 34 ('sep_conv_5x5', 1), 35 ('skip_connect', 0), 36 ('sep_conv_3x3', 1), 37 ('sep_conv_3x3', 0), 38 ('skip_connect', 2), 39 ('sep_conv_3x3', 0), 40 ('sep_conv_3x3', 1)], 41 normal_concat=range(2, 6), 42 reduce=[('dil_conv_3x3', 0), 43 ('avg_pool_3x3', 1), 44 ('dil_conv_3x3', 1), 45 ('sep_conv_5x5', 2), 46 ('sep_conv_5x5', 1), 47 ('sep_conv_5x5', 2), 48 ('avg_pool_3x3', 0), 49 ('dil_conv_5x5', 2)], 50 reduce_concat=range(2, 6))51DARTS_MINUS_TS_CIFAR10_NEW = Genotype(52 normal=[('sep_conv_3x3', 1),53 ('sep_conv_3x3', 0),54 ('sep_conv_3x3', 0),55 ('sep_conv_5x5', 1),56 ('sep_conv_3x3', 0),57 ('skip_connect', 1),58 ('skip_connect', 0),59 ('skip_connect', 1)],60 normal_concat=range(2, 6),61 reduce=[('sep_conv_3x3', 1),62 ('sep_conv_3x3', 0),63 ('skip_connect', 2),64 ('max_pool_3x3', 1),65 ('skip_connect', 2),66 ('skip_connect', 3),67 ('skip_connect', 2),68 ('skip_connect', 3)],69 reduce_concat=range(2, 6))70DARTS_MINUS_TS_CIFAR100_NEW = Genotype(71 normal=[('sep_conv_3x3', 0),72 ('sep_conv_3x3', 1),73 ('sep_conv_3x3', 0),74 ('skip_connect', 1),75 ('dil_conv_5x5', 0),76 ('skip_connect', 1),77 ('skip_connect', 0),78 ('skip_connect', 1)],79 normal_concat=range(2, 6),80 reduce=[('sep_conv_3x3', 0),81 ('sep_conv_5x5', 1),82 ('skip_connect', 2),83 ('avg_pool_3x3', 1),84 ('skip_connect', 2),85 ('skip_connect', 3),86 ('skip_connect', 2),87 ('skip_connect', 3)],88 reduce_concat=range(2, 6))89DARTS_MINUS_CIFAR10 = Genotype(90 normal=[('sep_conv_3x3', 0),91 ('sep_conv_3x3', 1),92 ('sep_conv_3x3', 0),93 ('skip_connect', 1),94 ('skip_connect', 0),95 ('skip_connect', 1),96 ('skip_connect', 1),97 ('skip_connect', 0)],98 normal_concat=range(2, 6),99 reduce=[('sep_conv_3x3', 0),100 ('sep_conv_3x3', 1),101 ('skip_connect', 2),102 ('max_pool_3x3', 0),103 ('skip_connect', 2),104 ('skip_connect', 3),105 ('skip_connect', 2),106 ('skip_connect', 3)],107 reduce_concat=range(2, 6))108DARTS_MINUS_CIFAR100 = Genotype(109 normal=[('skip_connect', 0),110 ('sep_conv_3x3', 1),111 ('dil_conv_3x3', 0),112 ('skip_connect', 1),113 ('sep_conv_3x3', 0),114 ('skip_connect', 1),115 ('skip_connect', 1),116 ('skip_connect', 0)],117 normal_concat=range(2, 6),118 reduce=[('sep_conv_3x3', 0),119 ('avg_pool_3x3', 1),120 ('skip_connect', 2),121 ('avg_pool_3x3', 1),122 ('skip_connect', 2),123 ('skip_connect', 3),124 ('skip_connect', 2),125 ('skip_connect', 3)],126 reduce_concat=range(2, 6))127DARTS_MINUS_TS_CIFAR10 = Genotype(128 normal=[('sep_conv_3x3', 0),129 ('sep_conv_3x3', 1),130 ('sep_conv_5x5', 0),131 ('sep_conv_3x3', 1),132 ('dil_conv_3x3', 0),133 ('dil_conv_5x5', 1),134 ('skip_connect', 0),135 ('skip_connect', 1)],136 normal_concat=range(2, 6),137 reduce=[('max_pool_3x3', 1),138 ('max_pool_3x3', 0),139 ('skip_connect', 2),140 ('max_pool_3x3', 1),141 ('skip_connect', 2),142 ('skip_connect', 3),143 ('skip_connect', 2),144 ('skip_connect', 3)],145 reduce_concat=range(2, 6))146DARTS_MINUS_TS_CIFAR100 = Genotype(147 normal=[('sep_conv_3x3', 0),148 ('dil_conv_3x3', 1),149 ('skip_connect', 1),150 ('dil_conv_5x5', 0),151 ('skip_connect', 1),152 ('dil_conv_5x5', 0),153 ('skip_connect', 1),154 ('dil_conv_3x3', 0)],155 normal_concat=range(2, 6),156 reduce=[('sep_conv_3x3', 0),157 ('max_pool_3x3', 1),158 ('skip_connect', 2),159 ('sep_conv_3x3', 1),160 ('skip_connect', 2),161 ('skip_connect', 3),162 ('skip_connect', 2),163 ('skip_connect', 3)],164 reduce_concat=range(2, 6))165NASNet = Genotype(166 normal=[167 ('sep_conv_5x5', 1),168 ('sep_conv_3x3', 0),169 ('sep_conv_5x5', 0),170 ('sep_conv_3x3', 0),171 ('avg_pool_3x3', 1),172 ('skip_connect', 0),173 ('avg_pool_3x3', 0),174 ('avg_pool_3x3', 0),175 ('sep_conv_3x3', 1),176 ('skip_connect', 1),177 ],178 normal_concat=[2, 3, 4, 5, 6],179 reduce=[180 ('sep_conv_5x5', 1),181 ('sep_conv_7x7', 0),182 ('max_pool_3x3', 1),183 ('sep_conv_7x7', 0),184 ('avg_pool_3x3', 1),185 ('sep_conv_5x5', 0),186 ('skip_connect', 3),187 ('avg_pool_3x3', 2),188 ('sep_conv_3x3', 2),189 ('max_pool_3x3', 1),190 ],191 reduce_concat=[4, 5, 6],192)193AmoebaNet = Genotype(194 normal=[195 ('avg_pool_3x3', 0),196 ('max_pool_3x3', 1),197 ('sep_conv_3x3', 0),198 ('sep_conv_5x5', 2),199 ('sep_conv_3x3', 0),200 ('avg_pool_3x3', 3),201 ('sep_conv_3x3', 1),202 ('skip_connect', 1),203 ('skip_connect', 0),204 ('avg_pool_3x3', 1),205 ],206 normal_concat=[4, 5, 6],207 reduce=[208 ('avg_pool_3x3', 0),209 ('sep_conv_3x3', 1),210 ('max_pool_3x3', 0),211 ('sep_conv_7x7', 2),212 ('sep_conv_7x7', 0),213 ('avg_pool_3x3', 1),214 ('max_pool_3x3', 0),215 ('max_pool_3x3', 1),216 ('conv_7x1_1x7', 0),217 ('sep_conv_3x3', 5),218 ],219 reduce_concat=[3, 4, 6]220)221DARTS_V1 = Genotype(222 normal=[('sep_conv_3x3', 1),223 ('sep_conv_3x3', 0),224 ('skip_connect', 0),225 ('sep_conv_3x3', 1),226 ('skip_connect', 0),227 ('sep_conv_3x3', 1),228 ('sep_conv_3x3', 0),229 ('skip_connect', 2)],230 normal_concat=[2, 3, 4, 5],231 reduce=[('max_pool_3x3', 0),232 ('max_pool_3x3', 1),233 ('skip_connect', 2),234 ('max_pool_3x3', 0),235 ('max_pool_3x3', 0),236 ('skip_connect', 2),237 ('skip_connect', 2),238 ('avg_pool_3x3', 0)],239 reduce_concat=[2, 3, 4, 5])240DARTS_V2 = Genotype(241 normal=[('sep_conv_3x3', 0),242 ('sep_conv_3x3', 1),243 ('sep_conv_3x3', 0),244 ('sep_conv_3x3', 1),245 ('sep_conv_3x3', 1),246 ('skip_connect', 0),247 ('skip_connect', 0),248 ('dil_conv_3x3', 2)],249 normal_concat=[2, 3, 4, 5],250 reduce=[('max_pool_3x3', 0),251 ('max_pool_3x3', 1),252 ('skip_connect', 2),253 ('max_pool_3x3', 1),254 ('max_pool_3x3', 0),255 ('skip_connect', 2),256 ('skip_connect', 2),257 ('max_pool_3x3', 1)],258 reduce_concat=[2, 3, 4, 5])259DARTS = DARTS_V2260DARTS_CIFAR10_TS_1ST = Genotype(261 normal=[('sep_conv_3x3', 0),262 ('dil_conv_5x5', 1),263 ('skip_connect', 0),264 ('sep_conv_5x5', 1),265 ('skip_connect', 0),266 ('dil_conv_3x3', 1),267 ('skip_connect', 0),268 ('dil_conv_3x3', 1)],269 normal_concat=range(2, 6),270 reduce=[('max_pool_3x3', 0),271 ('max_pool_3x3', 1),272 ('skip_connect', 2),273 ('max_pool_3x3', 0),274 ('max_pool_3x3', 1),275 ('skip_connect', 2),276 ('skip_connect', 2),277 ('skip_connect', 3)],278 reduce_concat=range(2, 6))279DARTS_CIFAR100_TS_1ST = Genotype(280 normal=[('sep_conv_3x3', 0),281 ('sep_conv_3x3', 1),282 ('skip_connect', 0),283 ('skip_connect', 1),284 ('skip_connect', 0),285 ('skip_connect', 1),286 ('skip_connect', 0),287 ('skip_connect', 1)],288 normal_concat=range(2, 6),289 reduce=[('avg_pool_3x3', 0),290 ('avg_pool_3x3', 1),291 ('skip_connect', 2),292 ('avg_pool_3x3', 0),293 ('skip_connect', 2),294 ('avg_pool_3x3', 0),295 ('skip_connect', 2),296 ('avg_pool_3x3', 0)],297 reduce_concat=range(2, 6))298DARTS_CIFAR10_TS_18_V1 = Genotype(299 normal=[('sep_conv_3x3', 0),300 ('sep_conv_3x3', 1),301 ('skip_connect', 0),302 ('sep_conv_5x5', 1),303 ('sep_conv_3x3', 0),304 ('sep_conv_3x3', 1),305 ('skip_connect', 0),306 ('dil_conv_5x5', 1)],307 normal_concat=range(2, 6),308 reduce=[('avg_pool_3x3', 0),309 ('avg_pool_3x3', 1),310 ('avg_pool_3x3', 0),311 ('skip_connect', 2),312 ('skip_connect', 2),313 ('skip_connect', 3),314 ('avg_pool_3x3', 0),315 ('skip_connect', 2)],316 reduce_concat=range(2, 6))317DARTS_CIFAR10_TS_18_V2 = Genotype(318 normal=[('sep_conv_3x3', 0),319 ('sep_conv_3x3', 1),320 ('skip_connect', 0),321 ('sep_conv_5x5', 1),322 ('sep_conv_3x3', 0),323 ('sep_conv_3x3', 1),324 ('skip_connect', 0),325 ('dil_conv_5x5', 1)],326 normal_concat=range(2, 6),327 reduce=[('avg_pool_3x3', 0),328 ('avg_pool_3x3', 1),329 ('avg_pool_3x3', 0),330 ('skip_connect', 2),331 ('skip_connect', 2),332 ('avg_pool_3x3', 1),333 ('avg_pool_3x3', 0),334 ('skip_connect', 2)],335 reduce_concat=range(2, 6))336MY_DARTS_CIFAR10 = Genotype(337 normal=[('skip_connect', 0),338 ('sep_conv_3x3', 1),339 ('skip_connect', 0),340 ('dil_conv_5x5', 2),341 ('skip_connect', 0),342 ('dil_conv_5x5', 1),343 ('skip_connect', 0),344 ('sep_conv_3x3', 1)],345 normal_concat=range(2, 6),346 reduce=[('sep_conv_3x3', 1),347 ('max_pool_3x3', 0),348 ('skip_connect', 2),349 ('avg_pool_3x3', 0),350 ('skip_connect', 2),351 ('skip_connect', 3),352 ('skip_connect', 3),353 ('skip_connect', 2)],354 reduce_concat=range(2, 6))355DARTS_CIFAR10_TS_50 = Genotype(356 normal=[('sep_conv_3x3', 0), 357 ('sep_conv_3x3', 1),358 ('sep_conv_3x3', 0), 359 ('sep_conv_3x3', 1), 360 ('sep_conv_3x3', 0), 361 ('skip_connect', 1), 362 ('skip_connect', 0), 363 ('dil_conv_3x3', 1)],364 normal_concat=range(2, 6), 365 reduce=[('dil_conv_3x3', 0), 366 ('sep_conv_3x3', 1),367 ('max_pool_3x3', 0), 368 ('skip_connect', 2), 369 ('skip_connect', 2), 370 ('skip_connect', 3), 371 ('skip_connect', 2), 372 ('skip_connect', 3)], 373 reduce_concat=range(2, 6))374DARTS_CIFAR100_TS_50 = Genotype(375 normal=[('sep_conv_3x3', 0), 376 ('sep_conv_3x3', 1), 377 ('skip_connect', 0), 378 ('skip_connect', 1), 379 ('skip_connect', 0), 380 ('dil_conv_3x3', 1), 381 ('skip_connect', 0), 382 ('dil_conv_3x3', 1)], 383 normal_concat=range(2, 6), 384 reduce=[('max_pool_3x3', 1), 385 ('max_pool_3x3', 0), 386 ('max_pool_3x3', 0), 387 ('dil_conv_5x5', 2), 388 ('skip_connect', 2), 389 ('max_pool_3x3', 0), 390 ('skip_connect', 2), 391 ('max_pool_3x3', 0)], 392 reduce_concat=range(2, 6))393DARTS_CIFAR100_TS_34 = Genotype(394 normal=[('skip_connect', 0), 395 ('dil_conv_3x3', 1), 396 ('skip_connect', 0), 397 ('dil_conv_5x5', 1), 398 ('skip_connect', 0), 399 ('skip_connect', 1), 400 ('skip_connect', 0), 401 ('skip_connect', 1)], 402 normal_concat=range(2, 6), 403 reduce=[('avg_pool_3x3', 0), 404 ('avg_pool_3x3', 1), 405 ('skip_connect', 2), 406 ('avg_pool_3x3', 0), 407 ('skip_connect', 2), 408 ('avg_pool_3x3', 0), 409 ('skip_connect', 2), 410 ('avg_pool_3x3', 0)], 411 reduce_concat=range(2, 6))412DARTS_CIFAR10_TS_34 = Genotype(413 normal=[('sep_conv_3x3', 0), 414 ('sep_conv_3x3', 1), 415 ('skip_connect', 0), 416 ('dil_conv_3x3', 2), 417 ('sep_conv_3x3', 0), 418 ('dil_conv_5x5', 1), 419 ('skip_connect', 0), 420 ('sep_conv_3x3', 1)], 421 normal_concat=range(2, 6), 422 reduce=[('avg_pool_3x3', 1), 423 ('skip_connect', 0), 424 ('skip_connect', 2), 425 ('avg_pool_3x3', 1), 426 ('avg_pool_3x3', 1), 427 ('skip_connect', 2), 428 ('skip_connect', 2), 429 ('skip_connect', 3)], 430 reduce_concat=range(2, 6))431# DARTS_CIFAR100 = Genotype(432# normal=[('skip_connect', 0),433# ('skip_connect', 1),434# ('skip_connect', 0),435# ('skip_connect', 1),436# ('skip_connect', 0),437# ('skip_connect', 1),438# ('skip_connect', 0),439# ('skip_connect', 1)],440# normal_concat=range(2, 6),441# reduce=[('max_pool_3x3', 0),442# ('dil_conv_3x3', 1),443# ('skip_connect', 2),444# ('avg_pool_3x3', 0),445# ('skip_connect', 2),446# ('max_pool_3x3', 0),447# ('skip_connect', 2),448# ('avg_pool_3x3', 0)],449# reduce_concat=range(2, 6))450DARTS_CIFAR100_1ST = Genotype(451 normal=[('skip_connect', 0),452 ('skip_connect', 1),453 ('skip_connect', 0),454 ('sep_conv_3x3', 1),455 ('skip_connect', 0),456 ('skip_connect', 1),457 ('skip_connect', 0),458 ('skip_connect', 1)],459 normal_concat=range(2, 6),460 reduce=[('avg_pool_3x3', 0),461 ('avg_pool_3x3', 1),462 ('skip_connect', 2),463 ('avg_pool_3x3', 0),464 ('avg_pool_3x3', 0),465 ('skip_connect', 2),466 ('skip_connect', 2),467 ('avg_pool_3x3', 0)],468 reduce_concat=range(2, 6))469DARTS_CIFAR100 = Genotype(470 normal=[('skip_connect', 0),471 ('sep_conv_3x3', 1),472 ('skip_connect', 0),473 ('skip_connect', 1),474 ('skip_connect', 0),475 ('skip_connect', 1),476 ('skip_connect', 0),477 ('skip_connect', 1)],478 normal_concat=range(2, 6),479 reduce=[('avg_pool_3x3', 0),480 ('skip_connect', 1),481 ('skip_connect', 2),482 ('avg_pool_3x3', 0),483 ('avg_pool_3x3', 0),484 ('skip_connect', 2),485 ('skip_connect', 2),486 ('avg_pool_3x3', 0)],487 reduce_concat=range(2, 6))488DARTS_CIFAR100_TS_18 = Genotype(489 normal=[('sep_conv_3x3', 0),490 ('sep_conv_3x3', 1),491 ('skip_connect', 0),492 ('skip_connect', 1),493 ('skip_connect', 0),494 ('skip_connect', 1),495 ('skip_connect', 0),496 ('skip_connect', 1)],497 normal_concat=range(2, 6),498 reduce=[('avg_pool_3x3', 0),499 ('skip_connect', 1),500 ('avg_pool_3x3', 0),501 ('skip_connect', 2),502 ('avg_pool_3x3', 0),503 ('skip_connect', 2),504 ('skip_connect', 2),505 ('avg_pool_3x3', 0)],506 reduce_concat=range(2, 6))507DARTS_CIFAR100_ES = Genotype(508 normal=[('sep_conv_3x3', 1),509 ('skip_connect', 0),510 ('sep_conv_5x5', 1),511 ('skip_connect', 0),512 ('sep_conv_3x3', 0),513 ('sep_conv_3x3', 1),514 ('sep_conv_3x3', 1),515 ('sep_conv_3x3', 3)],516 normal_concat=range(2, 6),517 reduce=[('max_pool_3x3', 0),518 ('skip_connect', 1),519 ('max_pool_3x3', 0),520 ('dil_conv_3x3', 2),521 ('max_pool_3x3', 0),522 ('sep_conv_5x5', 2),523 ('max_pool_3x3', 0),524 ('skip_connect', 2)],525 reduce_concat=range(2, 6))526DARTS_CIFAR100_TS_18_ES = Genotype(527 normal=[('sep_conv_3x3', 0),528 ('sep_conv_3x3', 1),529 ('skip_connect', 0),530 ('sep_conv_5x5', 1),531 ('sep_conv_3x3', 0),532 ('sep_conv_3x3', 1),533 ('sep_conv_5x5', 2),534 ('skip_connect', 0)],535 normal_concat=range(2, 6),536 reduce=[('max_pool_3x3', 0),537 ('skip_connect', 1),538 ('max_pool_3x3', 0),539 ('skip_connect', 2),540 ('max_pool_3x3', 0),541 ('sep_conv_5x5', 3),542 ('max_pool_3x3', 0),543 ('skip_connect', 2)],544 reduce_concat=range(2, 6))545DARTS_CIFAR10_ES = Genotype(546 normal=[('sep_conv_3x3', 1),547 ('skip_connect', 0),548 ('dil_conv_5x5', 2),549 ('skip_connect', 0),550 ('sep_conv_5x5', 1),551 ('skip_connect', 0),552 ('sep_conv_3x3', 1),553 ('sep_conv_3x3', 2)],554 normal_concat=range(2, 6),555 reduce=[('max_pool_3x3', 0),556 ('sep_conv_3x3', 1),557 ('skip_connect', 2),558 ('max_pool_3x3', 0),559 ('skip_connect', 2),560 ('max_pool_3x3', 0),561 ('skip_connect', 2),562 ('max_pool_3x3', 0)],563 reduce_concat=range(2, 6))564DARTS_CIFAR10_TS_18_ES = Genotype(565 normal=[('sep_conv_3x3', 0),566 ('sep_conv_3x3', 1),567 ('skip_connect', 0),568 ('sep_conv_5x5', 1),569 ('sep_conv_3x3', 0),570 ('sep_conv_3x3', 1),571 ('skip_connect', 0),572 ('sep_conv_5x5', 1)],573 normal_concat=range(2, 6),574 reduce=[('avg_pool_3x3', 0),575 ('avg_pool_3x3', 1),576 ('avg_pool_3x3', 0),577 ('skip_connect', 2),578 ('skip_connect', 2),579 ('max_pool_3x3', 0),580 ('avg_pool_3x3', 0),581 ('skip_connect', 2)],582 reduce_concat=range(2, 6))583PDARTS_TS_CIFAR100_GAMMA_2 = Genotype(normal=[('sep_conv_3x3', 0), ('sep_conv_3x3', 1), ('skip_connect', 0), ('sep_conv_3x3', 2), ('skip_connect', 0), ('sep_conv_3x3', 2), ('sep_conv_3x3', 0), ('sep_conv_3x3', 2)], normal_concat=range(2, 6), reduce=[('avg_pool_3x3', 0), ('avg_pool_3x3', 1), ('avg_pool_3x3', 0), ('dil_conv_5x5', 2), ('avg_pool_3x3', 0), ('sep_conv_5x5', 2), ('avg_pool_3x3', 0), ('sep_conv_5x5', 4)], reduce_concat=range(2, 6))584PDARTS_TS_CIFAR100_GAMMA_3 = Genotype(normal=[('sep_conv_3x3', 0), ('dil_conv_3x3', 1), ('skip_connect', 0), ('sep_conv_3x3', 1), ('skip_connect', 0), ('dil_conv_5x5', 2), ('sep_conv_3x3', 0), ('dil_conv_5x5', 1)], normal_concat=range(2, 6), reduce=[('skip_connect', 0), ('max_pool_3x3', 1), ('skip_connect', 0), ('sep_conv_3x3', 1), ('avg_pool_3x3', 0), ('dil_conv_5x5', 3), ('dil_conv_3x3', 3), ('dil_conv_3x3', 4)], reduce_concat=range(2, 6))585PDARTS_TS_CIFAR100_GAMMA_0_1 = Genotype(normal=[('sep_conv_3x3', 0), ('sep_conv_3x3', 1), ('skip_connect', 0), ('dil_conv_3x3', 2), ('skip_connect', 0), ('sep_conv_3x3', 2), ('sep_conv_3x3', 0), ('dil_conv_3x3', 3)], normal_concat=range(2, 6), reduce=[('avg_pool_3x3', 0), ('dil_conv_5x5', 1), ('avg_pool_3x3', 0), ('sep_conv_3x3', 2), ('dil_conv_5x5', 2), ('dil_conv_3x3', 3), ('avg_pool_3x3', 0), ('sep_conv_5x5', 2)], reduce_concat=range(2, 6))586PDARTS_TS_CIFAR100_GAMMA_0_5 = Genotype(normal=[('sep_conv_3x3', 0), ('sep_conv_3x3', 1), ('skip_connect', 0), ('sep_conv_3x3', 1), ('sep_conv_3x3', 0), ('skip_connect', 2), ('sep_conv_3x3', 1), ('sep_conv_5x5', 4)], normal_concat=range(2, 6), reduce=[('sep_conv_3x3', 0), ('skip_connect', 1), ('avg_pool_3x3', 0), ('sep_conv_3x3', 2), ('avg_pool_3x3', 0), ('sep_conv_3x3', 1), ('avg_pool_3x3', 0), ('dil_conv_5x5', 2)], reduce_concat=range(2, 6))587DARTS_TS_18_CIFAR10_GAMMA_0_5 = Genotype(normal=[('skip_connect', 0), ('sep_conv_3x3', 1), ('sep_conv_3x3', 0), ('sep_conv_3x3', 1), ('skip_connect', 0), ('dil_conv_5x5', 1), ('skip_connect', 0), ('dil_conv_3x3', 1)], normal_concat=range(2, 6), reduce=[('max_pool_3x3', 0), ('skip_connect', 1), ('skip_connect', 2), ('max_pool_3x3', 0), ('max_pool_3x3', 0), ('skip_connect', 2), ('skip_connect', 2), ('max_pool_3x3', 0)], reduce_concat=range(2, 6))588DARTS_TS_18_CIFAR10_GAMMA_0_1 = Genotype(normal=[('skip_connect', 0), ('sep_conv_3x3', 1), ('skip_connect', 0), ('skip_connect', 1), ('skip_connect', 0), ('dil_conv_5x5', 1), ('skip_connect', 0), ('dil_conv_3x3', 1)], normal_concat=range(2, 6), reduce=[('max_pool_3x3', 0), ('avg_pool_3x3', 1), ('max_pool_3x3', 0), ('skip_connect', 2), ('max_pool_3x3', 0), ('skip_connect', 2), ('skip_connect', 2), ('avg_pool_3x3', 0)], reduce_concat=range(2, 6))589DARTS_TS_18_CIFAR10_GAMMA_2 = Genotype(normal=[('skip_connect', 0), ('sep_conv_5x5', 1), ('skip_connect', 0), ('skip_connect', 1), ('sep_conv_3x3', 0), ('dil_conv_3x3', 1), ('skip_connect', 0), ('dil_conv_3x3', 1)], normal_concat=range(2, 6), reduce=[('avg_pool_3x3', 0), ('avg_pool_3x3', 1), ('skip_connect', 2), ('avg_pool_3x3', 0), ('skip_connect', 2), ('avg_pool_3x3', 0), ('skip_connect', 2), ('skip_connect', 3)], reduce_concat=range(2, 6))590DARTS_TS_18_CIFAR10_GAMMA_3 = Genotype(normal=[('skip_connect', 0), ('sep_conv_3x3', 1), ('skip_connect', 0), ('skip_connect', 1), ('skip_connect', 0), ('dil_conv_5x5', 1), ('skip_connect', 0), ('dil_conv_5x5', 1)], normal_concat=range(2, 6), reduce=[('avg_pool_3x3', 1), ('avg_pool_3x3', 0), ('skip_connect', 2), ('avg_pool_3x3', 1), ('skip_connect', 2), ('avg_pool_3x3', 0), ('skip_connect', 2), ('avg_pool_3x3', 0)], reduce_concat=range(2, 6))591DARTS_TS_18_CIFAR10_LAMBDA_2 = Genotype(normal=[('sep_conv_3x3', 0), ('sep_conv_3x3', 1), ('sep_conv_3x3', 0), ('dil_conv_5x5', 1), ('skip_connect', 0), ('sep_conv_3x3', 1), ('skip_connect', 0), ('sep_conv_3x3', 1)], normal_concat=range(2, 6), reduce=[('skip_connect', 0), ('avg_pool_3x3', 1), ('skip_connect', 2), ('max_pool_3x3', 0), ('avg_pool_3x3', 1), ('skip_connect', 2), ('skip_connect', 2), ('skip_connect', 3)], reduce_concat=range(2, 6))592DARTS_TS_18_CIFAR10_LAMBDA_0_1 = Genotype(normal=[('sep_conv_3x3', 0), ('sep_conv_3x3', 1), ('skip_connect', 0), ('skip_connect', 2), ('sep_conv_3x3', 0), ('sep_conv_3x3', 1), ('skip_connect', 0), ('dil_conv_3x3', 1)], normal_concat=range(2, 6), reduce=[('avg_pool_3x3', 1), ('avg_pool_3x3', 0), ('skip_connect', 2), ('max_pool_3x3', 0), ('skip_connect', 2), ('skip_connect', 3), ('skip_connect', 2), ('avg_pool_3x3', 0)], reduce_concat=range(2, 6))593DARTS_TS_18_CIFAR10_LAMBDA_0_5 = Genotype(normal=[('sep_conv_3x3', 0), ('dil_conv_3x3', 1), ('sep_conv_3x3', 0), ('sep_conv_5x5', 1), ('skip_connect', 0), ('dil_conv_3x3', 1), ('skip_connect', 0), ('skip_connect', 1)], normal_concat=range(2, 6), reduce=[('max_pool_3x3', 0), ('dil_conv_3x3', 1), ('skip_connect', 2), ('max_pool_3x3', 0), ('skip_connect', 2), ('skip_connect', 3), ('skip_connect', 3), ('skip_connect', 2)], reduce_concat=range(2, 6))594DARTS_TS_18_CIFAR10_LAMBDA_3 = Genotype(normal=[('skip_connect', 0), ('sep_conv_3x3', 1), ('skip_connect', 0), ('sep_conv_3x3', 1), ('skip_connect', 0), ('sep_conv_3x3', 1), ('skip_connect', 0), ('sep_conv_3x3', 1)], normal_concat=range(2, 6), reduce=[('max_pool_3x3', 0), ('dil_conv_5x5', 1), ('skip_connect', 2), ('avg_pool_3x3', 0), ('avg_pool_3x3', 0), ('skip_connect', 2), ('skip_connect', 2), ('avg_pool_3x3', 0)], reduce_concat=range(2, 6))595PDARTS_TS_18_CIFAR100_LAMBDA_3 = Genotype(normal=[('skip_connect', 0), ('sep_conv_3x3', 1), ('skip_connect', 0), ('sep_conv_5x5', 1), ('dil_conv_3x3', 1), ('sep_conv_3x3', 2), ('sep_conv_3x3', 0), ('sep_conv_3x3', 1)], normal_concat=range(2, 6), reduce=[('max_pool_3x3', 0), ('sep_conv_3x3', 1), ('skip_connect', 1), ('sep_conv_3x3', 2), ('max_pool_3x3', 0), ('sep_conv_5x5', 1), ('dil_conv_3x3', 1), ('dil_conv_3x3', 2)], reduce_concat=range(2, 6))596PDARTS_TS_18_CIFAR100_LAMBDA_0_1 = Genotype(normal=[('sep_conv_3x3', 0), ('sep_conv_3x3', 1), ('skip_connect', 0), ('sep_conv_3x3', 1), ('skip_connect', 0), ('sep_conv_3x3', 1), ('sep_conv_3x3', 0), ('dil_conv_3x3', 1)], normal_concat=range(2, 6), reduce=[('max_pool_3x3', 0), ('sep_conv_5x5', 1), ('avg_pool_3x3', 0), ('sep_conv_3x3', 1), ('avg_pool_3x3', 0), ('dil_conv_5x5', 3), ('avg_pool_3x3', 0), ('dil_conv_3x3', 1)], reduce_concat=range(2, 6))597PDARTS_TS_18_CIFAR100_LAMBDA_0_5 = Genotype(normal=[('skip_connect', 0), ('sep_conv_5x5', 1), ('sep_conv_3x3', 0), ('skip_connect', 1), ('sep_conv_3x3', 0), ('sep_conv_3x3', 3), ('sep_conv_3x3', 0), ('dil_conv_3x3', 1)], normal_concat=range(2, 6), reduce=[('skip_connect', 0), ('skip_connect', 1), ('skip_connect', 0), ('skip_connect', 1), ('avg_pool_3x3', 0), ('avg_pool_3x3', 1), ('avg_pool_3x3', 0), ('dil_conv_5x5', 4)], reduce_concat=range(2, 6))598PDARTS_TS_18_CIFAR100_LAMBDA_2 = Genotype(normal=[('sep_conv_3x3', 0), ('sep_conv_3x3', 1), ('skip_connect', 0), ('dil_conv_3x3', 1), ('skip_connect', 0), ('sep_conv_5x5', 2), ('sep_conv_3x3', 0), ('dil_conv_5x5', 4)], normal_concat=range(2, 6), reduce=[('max_pool_3x3', 0), ('skip_connect', 1), ('avg_pool_3x3', 0), ('sep_conv_5x5', 1), ('avg_pool_3x3', 0), ('sep_conv_3x3', 2), ('max_pool_3x3', 0), ('dil_conv_3x3', 1)], reduce_concat=range(2, 6))599PDARTS_TS_18_CIFAR100_AB_1 = Genotype(normal=[('dil_conv_3x3', 0), ('sep_conv_3x3', 1), ('skip_connect', 0), ('sep_conv_3x3', 2), ('sep_conv_3x3', 0), ('dil_conv_3x3', 1), ('sep_conv_3x3', 0), ('sep_conv_3x3', 3)], normal_concat=range(2, 6), reduce=[('skip_connect', 0), ('dil_conv_5x5', 1), ('avg_pool_3x3', 0), ('skip_connect', 1), ('skip_connect', 0), ('sep_conv_5x5', 2), ('skip_connect', 0), ('sep_conv_3x3', 1)], reduce_concat=range(2, 6))600PDARTS_TS_18_CIFAR100_AB_4 = Genotype(normal=[('sep_conv_3x3', 0), ('dil_conv_3x3', 1), ('sep_conv_3x3', 0), ('skip_connect', 1), ('max_pool_3x3', 0), ('sep_conv_3x3', 1), ('skip_connect', 0), ('sep_conv_5x5', 1)], normal_concat=range(2, 6), reduce=[('avg_pool_3x3', 0), ('skip_connect', 1), ('avg_pool_3x3', 0), ('sep_conv_5x5', 2), ('avg_pool_3x3', 0), ('dil_conv_3x3', 1), ('avg_pool_3x3', 0), ('sep_conv_3x3', 4)], reduce_concat=range(2, 6))601DARTS_TS_18_CIFAR10_AB_1 = Genotype(normal=[('sep_conv_3x3', 0), ('sep_conv_3x3', 1), ('skip_connect', 0), ('dil_conv_5x5', 1), ('skip_connect', 0), ('sep_conv_3x3', 1), ('skip_connect', 0), ('sep_conv_3x3', 1)], normal_concat=range(2, 6), reduce=[('max_pool_3x3', 0), ('dil_conv_5x5', 1), ('skip_connect', 2), ('max_pool_3x3', 0), ('skip_connect', 2), ('avg_pool_3x3', 0), ('skip_connect', 2), ('skip_connect', 3)], reduce_concat=range(2, 6))602DARTS_TS_18_CIFAR10_AB_4 = Genotype(normal=[('sep_conv_3x3', 0), ('sep_conv_3x3', 1), ('skip_connect', 0), ('dil_conv_3x3', 1), ('sep_conv_3x3', 0), ('sep_conv_3x3', 1), ('skip_connect', 0), ('sep_conv_3x3', 1)], normal_concat=range(2, 6), reduce=[('dil_conv_3x3', 0), ('sep_conv_3x3', 1), ('skip_connect', 2), ('max_pool_3x3', 1), ('skip_connect', 2), ('dil_conv_3x3', 3), ('skip_connect', 2), ('skip_connect', 4)], reduce_concat=range(2, 6))603PDARTS_TUNED_CIFAR100 = Genotype(normal=[('sep_conv_3x3', 0), ('skip_connect', 1), ('sep_conv_3x3', 0), ('skip_connect', 1), ('sep_conv_3x3', 0), ('sep_conv_5x5', 3), ('sep_conv_5x5', 0), ('sep_conv_3x3', 1)], normal_concat=range(2, 6), reduce=[('avg_pool_3x3', 0), ('sep_conv_3x3', 1), ('avg_pool_3x3', 0), ('dil_conv_3x3', 2), ('max_pool_3x3', 1), ('sep_conv_3x3', 3), ('sep_conv_5x5', 1), ('dil_conv_5x5', 4)], reduce_concat=range(2, 6))...
routing.py
Source:routing.py
...12class Mapper(_Mapper):13 ''' This Mapper allows us to intercept the connect calls used by routes14 so that we can collect named routes and later use them to create links15 via some helper functions like build_nav(). '''16 def connect(self, *args, **kw):17 '''Connect a new route, storing any named routes for later.18 This custom connect() method wraps the standard connect() method,19 and additionally saves any named routes that are connected in a dict20 ckan.routing.named_routes, which ends up being accessible via the21 Pylons config as config['routes.named_routes'].22 Also takes some additional params:23 :param ckan_icon: name of the icon to be associated with this route,24 e.g. 'group', 'time'. Available icons are listed here:25 http://fortawesome.github.io/Font-Awesome/3.2.1/icons/26 :type ckan_icon: string27 :param highlight_actions: space-separated list of controller actions28 that should be treated as the same as this named route for menu29 highlighting purposes, e.g. 'index search'30 :type highlight_actions: string31 '''32 ckan_icon = kw.pop('ckan_icon', None)33 highlight_actions = kw.pop('highlight_actions', kw.get('action', ''))34 ckan_core = kw.pop('ckan_core', None)35 out = _Mapper.connect(self, *args, **kw)36 route = self.matchlist[-1]37 if ckan_core is not None:38 route._ckan_core = ckan_core39 if len(args) == 1 or args[0].startswith('_redirect_'):40 return out41 # we have a named route42 needed = []43 matches = re.findall('\{([^:}]*)(\}|:)', args[1])44 for match in matches:45 needed.append(match[0])46 route_data = {47 'icon': ckan_icon,48 # needed lists the names of the parameters that need defining49 # for the route to be generated50 'needed': needed,51 'controller': kw.get('controller'),52 'action': kw.get('action', ''),53 'highlight_actions': highlight_actions54 }55 named_routes[args[0]] = route_data56 return out57def make_map():58 """Create, configure and return the routes Mapper"""59 # import controllers here rather than at root level because60 # pylons config is initialised by this point.61 # Helpers to reduce code clutter62 GET = dict(method=['GET'])63 PUT = dict(method=['PUT'])64 POST = dict(method=['POST'])65 DELETE = dict(method=['DELETE'])66 GET_POST = dict(method=['GET', 'POST'])67 PUT_POST = dict(method=['PUT', 'POST'])68 PUT_POST_DELETE = dict(method=['PUT', 'POST', 'DELETE'])69 OPTIONS = dict(method=['OPTIONS'])70 import ckan.lib.plugins as lib_plugins71 lib_plugins.reset_package_plugins()72 map = Mapper(directory=config['pylons.paths']['controllers'],73 always_scan=config['debug'])74 map.minimization = False75 map.explicit = True76 # CUSTOM ROUTES HERE77 for plugin in p.PluginImplementations(p.IRoutes):78 map = plugin.before_map(map)79 # The ErrorController route (handles 404/500 error pages); it should80 # likely stay at the top, ensuring it can always be resolved.81 map.connect('/error/{action}', controller='error', ckan_core=True)82 map.connect('/error/{action}/{id}', controller='error', ckan_core=True)83 map.connect('*url', controller='home', action='cors_options',84 conditions=OPTIONS, ckan_core=True)85 # Mark all routes added from extensions on the `before_map` extension point86 # as non-core87 for route in map.matchlist:88 if not hasattr(route, '_ckan_core'):89 route._ckan_core = False90 # CKAN API versioned.91 register_list = [92 'package',93 'dataset',94 'resource',95 'tag',96 'group',97 'revision',98 'licenses',99 'rating',100 'user',101 'activity'102 ]103 register_list_str = '|'.join(register_list)104 # /api ver 1, 2, 3 or none105 with SubMapper(map, controller='api', path_prefix='/api{ver:/1|/2|/3|}',106 ver='/1') as m:107 m.connect('/search/{register}', action='search')108 # /api/util ver 1, 2 or none109 with SubMapper(map, controller='api', path_prefix='/api{ver:/1|/2|}',110 ver='/1') as m:111 m.connect('/util/dataset/munge_name', action='munge_package_name')112 m.connect('/util/dataset/munge_title_to_name',113 action='munge_title_to_package_name')114 m.connect('/util/tag/munge', action='munge_tag')115 ###########116 ## /END API117 ###########118 map.redirect('/packages', '/dataset')119 map.redirect('/packages/{url:.*}', '/dataset/{url}')120 map.redirect('/package', '/dataset')121 map.redirect('/package/{url:.*}', '/dataset/{url}')122 with SubMapper(map, controller='package') as m:123 m.connect('search', '/dataset', action='search',124 highlight_actions='index search')125 m.connect('dataset_new', '/dataset/new', action='new')126 m.connect('/dataset/{action}',127 requirements=dict(action='|'.join([128 'list',129 'autocomplete',130 'search'131 ])))132 m.connect('/dataset/{action}/{id}/{revision}', action='read_ajax',133 requirements=dict(action='|'.join([134 'read',135 'edit',136 'history',137 ])))138 m.connect('/dataset/{action}/{id}',139 requirements=dict(action='|'.join([140 'new_resource',141 'history',142 'read_ajax',143 'history_ajax',144 'follow',145 'activity',146 'groups',147 'unfollow',148 'delete',149 'api_data',150 ])))151 m.connect('dataset_edit', '/dataset/edit/{id}', action='edit',152 ckan_icon='pencil-square-o')153 m.connect('dataset_followers', '/dataset/followers/{id}',154 action='followers', ckan_icon='users')155 m.connect('dataset_activity', '/dataset/activity/{id}',156 action='activity', ckan_icon='clock-o')157 m.connect('/dataset/activity/{id}/{offset}', action='activity')158 m.connect('dataset_groups', '/dataset/groups/{id}',159 action='groups', ckan_icon='users')160 m.connect('dataset_resources', '/dataset/resources/{id}',161 action='resources', ckan_icon='bars')162 m.connect('dataset_read', '/dataset/{id}', action='read',163 ckan_icon='sitemap')164 m.connect('/dataset/{id}/resource/{resource_id}',165 action='resource_read')166 m.connect('/dataset/{id}/resource_delete/{resource_id}',167 action='resource_delete')168 m.connect('resource_edit', '/dataset/{id}/resource_edit/{resource_id}',169 action='resource_edit', ckan_icon='pencil-square-o')170 m.connect('/dataset/{id}/resource/{resource_id}/download',171 action='resource_download')172 m.connect('/dataset/{id}/resource/{resource_id}/download/{filename}',173 action='resource_download')174 m.connect('/dataset/{id}/resource/{resource_id}/embed',175 action='resource_embedded_dataviewer')176 m.connect('/dataset/{id}/resource/{resource_id}/viewer',177 action='resource_embedded_dataviewer', width="960",178 height="800")179 m.connect('/dataset/{id}/resource/{resource_id}/preview',180 action='resource_datapreview')181 m.connect('views', '/dataset/{id}/resource/{resource_id}/views',182 action='resource_views', ckan_icon='bars')183 m.connect('new_view', '/dataset/{id}/resource/{resource_id}/new_view',184 action='edit_view', ckan_icon='pencil-square-o')185 m.connect('edit_view',186 '/dataset/{id}/resource/{resource_id}/edit_view/{view_id}',187 action='edit_view', ckan_icon='pencil-square-o')188 m.connect('resource_view',189 '/dataset/{id}/resource/{resource_id}/view/{view_id}',190 action='resource_view')191 m.connect('/dataset/{id}/resource/{resource_id}/view/',192 action='resource_view')193 # group194 map.redirect('/groups', '/group')195 map.redirect('/groups/{url:.*}', '/group/{url}')196 # These named routes are used for custom group forms which will use the197 # names below based on the group.type ('group' is the default type)198 with SubMapper(map, controller='group') as m:199 m.connect('group_index', '/group', action='index',200 highlight_actions='index search')201 m.connect('group_list', '/group/list', action='list')202 m.connect('group_new', '/group/new', action='new')203 for action in [204 'edit',205 'delete',206 'member_new',207 'member_delete',208 'history',209 'followers',210 'follow',211 'unfollow',212 'admins',213 'activity',214 ]:215 m.connect('group_' + action,216 '/group/' + action + '/{id}',217 action=action)218 m.connect('group_about', '/group/about/{id}', action='about',219 ckan_icon='info-circle'),220 m.connect('group_edit', '/group/edit/{id}', action='edit',221 ckan_icon='pencil-square-o')222 m.connect('group_members', '/group/members/{id}', action='members',223 ckan_icon='users'),224 m.connect('group_activity', '/group/activity/{id}/{offset}',225 action='activity', ckan_icon='clock-o'),226 m.connect('group_read', '/group/{id}', action='read',227 ckan_icon='sitemap')228 # organizations these basically end up being the same as groups229 with SubMapper(map, controller='organization') as m:230 m.connect('organizations_index', '/organization', action='index')231 m.connect('organization_index', '/organization', action='index')232 m.connect('organization_new', '/organization/new', action='new')233 for action in [234 'delete',235 'admins',236 'member_new',237 'member_delete',238 'history']:239 m.connect('organization_' + action,240 '/organization/' + action + '/{id}',241 action=action)242 m.connect('organization_activity', '/organization/activity/{id}/{offset}',243 action='activity', ckan_icon='clock-o')244 m.connect('organization_read', '/organization/{id}', action='read')245 m.connect('organization_about', '/organization/about/{id}',246 action='about', ckan_icon='info-circle')247 m.connect('organization_read', '/organization/{id}', action='read',248 ckan_icon='sitemap')249 m.connect('organization_edit', '/organization/edit/{id}',250 action='edit', ckan_icon='pencil-square-o')251 m.connect('organization_members', '/organization/members/{id}',252 action='members', ckan_icon='users')253 m.connect('organization_bulk_process',254 '/organization/bulk_process/{id}',255 action='bulk_process', ckan_icon='sitemap')256 lib_plugins.register_package_plugins(map)257 lib_plugins.register_group_plugins(map)258 # tags259 map.redirect('/tags', '/tag')260 map.redirect('/tags/{url:.*}', '/tag/{url}')261 map.redirect('/tag/read/{url:.*}', '/tag/{url}',262 _redirect_code='301 Moved Permanently')263 map.connect('/tag', controller='tag', action='index')264 map.connect('/tag/{id}', controller='tag', action='read')265 # users266 map.redirect('/users/{url:.*}', '/user/{url}')267 with SubMapper(map, controller='revision') as m:268 m.connect('/revision', action='index')269 m.connect('/revision/edit/{id}', action='edit')270 m.connect('/revision/diff/{id}', action='diff')271 m.connect('/revision/list', action='list')272 m.connect('/revision/{id}', action='read')273 with SubMapper(map, controller='ckan.controllers.storage:StorageController') as m:274 m.connect('storage_file', '/storage/f/{label:.*}',275 action='file')276 with SubMapper(map, controller='util') as m:277 m.connect('/i18n/strings_{lang}.js', action='i18n_js_strings')278 m.connect('/util/redirect', action='redirect')279 m.connect('/testing/primer', action='primer')280 m.connect('/testing/markup', action='markup')281 # robots.txt282 map.connect('/(robots.txt)', controller='template', action='view')283 # Mark all unmarked routes added up until now as core routes284 for route in map.matchlist:285 if not hasattr(route, '_ckan_core'):286 route._ckan_core = True287 for plugin in p.PluginImplementations(p.IRoutes):288 map = plugin.after_map(map)289 # Mark all routes added from extensions on the `after_map` extension point290 # as non-core291 for route in map.matchlist:292 if not hasattr(route, '_ckan_core'):293 route._ckan_core = False294 # sometimes we get requests for favicon.ico we should redirect to295 # the real favicon location.296 map.redirect('/favicon.ico', config.get('ckan.favicon'))297 map.redirect('/*(url)/', '/{url}',298 _redirect_code='301 Moved Permanently')299 map.connect('/*url', controller='template', action='view', ckan_core=True)...
test_protocol.py
Source:test_protocol.py
...12from twisted.trial.unittest import TestCase13from twisted.test.proto_helpers import MemoryReactor, StringTransport14class MemoryConnector:15 _disconnected = False16 def disconnect(self):17 self._disconnected = True18class MemoryReactorWithConnectorsAndTime(MemoryReactor, Clock):19 """20 An extension of L{MemoryReactor} which returns L{IConnector}21 providers from its C{connectTCP} method.22 """23 def __init__(self):24 MemoryReactor.__init__(self)25 Clock.__init__(self)26 self.connectors = []27 def connectTCP(self, *a, **kw):28 MemoryReactor.connectTCP(self, *a, **kw)29 connector = MemoryConnector()30 self.connectors.append(connector)31 return connector32 def connectUNIX(self, *a, **kw):33 MemoryReactor.connectUNIX(self, *a, **kw)34 connector = MemoryConnector()35 self.connectors.append(connector)36 return connector37 def connectSSL(self, *a, **kw):38 MemoryReactor.connectSSL(self, *a, **kw)39 connector = MemoryConnector()40 self.connectors.append(connector)41 return connector42class ClientCreatorTests(TestCase):43 """44 Tests for L{twisted.internet.protocol.ClientCreator}.45 """46 def _basicConnectTest(self, check):47 """48 Helper for implementing a test to verify that one of the I{connect}49 methods of L{ClientCreator} passes the right arguments to the right50 reactor method.51 @param check: A function which will be invoked with a reactor and a52 L{ClientCreator} instance and which should call one of the53 L{ClientCreator}'s I{connect} methods and assert that all of its54 arguments except for the factory are passed on as expected to the55 reactor. The factory should be returned.56 """57 class SomeProtocol(Protocol):58 pass59 reactor = MemoryReactorWithConnectorsAndTime()60 cc = ClientCreator(reactor, SomeProtocol)61 factory = check(reactor, cc)62 protocol = factory.buildProtocol(None)63 self.assertIsInstance(protocol, SomeProtocol)64 def test_connectTCP(self):65 """66 L{ClientCreator.connectTCP} calls C{reactor.connectTCP} with the host67 and port information passed to it, and with a factory which will68 construct the protocol passed to L{ClientCreator.__init__}.69 """70 def check(reactor, cc):71 cc.connectTCP('example.com', 1234, 4321, ('1.2.3.4', 9876))72 host, port, factory, timeout, bindAddress = reactor.tcpClients.pop()73 self.assertEqual(host, 'example.com')74 self.assertEqual(port, 1234)75 self.assertEqual(timeout, 4321)76 self.assertEqual(bindAddress, ('1.2.3.4', 9876))77 return factory78 self._basicConnectTest(check)79 def test_connectUNIX(self):80 """81 L{ClientCreator.connectUNIX} calls C{reactor.connectUNIX} with the82 filename passed to it, and with a factory which will construct the83 protocol passed to L{ClientCreator.__init__}.84 """85 def check(reactor, cc):86 cc.connectUNIX('/foo/bar', 123, True)87 address, factory, timeout, checkPID = reactor.unixClients.pop()88 self.assertEqual(address, '/foo/bar')89 self.assertEqual(timeout, 123)90 self.assertEqual(checkPID, True)91 return factory92 self._basicConnectTest(check)93 def test_connectSSL(self):94 """95 L{ClientCreator.connectSSL} calls C{reactor.connectSSL} with the host,96 port, and context factory passed to it, and with a factory which will97 construct the protocol passed to L{ClientCreator.__init__}.98 """99 def check(reactor, cc):100 expectedContextFactory = object()101 cc.connectSSL('example.com', 1234, expectedContextFactory, 4321, ('4.3.2.1', 5678))102 host, port, factory, contextFactory, timeout, bindAddress = reactor.sslClients.pop()103 self.assertEqual(host, 'example.com')104 self.assertEqual(port, 1234)105 self.assertIdentical(contextFactory, expectedContextFactory)106 self.assertEqual(timeout, 4321)107 self.assertEqual(bindAddress, ('4.3.2.1', 5678))108 return factory109 self._basicConnectTest(check)110 def _cancelConnectTest(self, connect):111 """112 Helper for implementing a test to verify that cancellation of the113 L{Deferred} returned by one of L{ClientCreator}'s I{connect} methods is114 implemented to cancel the underlying connector.115 @param connect: A function which will be invoked with a L{ClientCreator}116 instance as an argument and which should call one its I{connect}117 methods and return the result.118 @return: A L{Deferred} which fires when the test is complete or fails if119 there is a problem.120 """121 reactor = MemoryReactorWithConnectorsAndTime()122 cc = ClientCreator(reactor, Protocol)123 d = connect(cc)124 connector = reactor.connectors.pop()125 self.assertFalse(connector._disconnected)126 d.cancel()127 self.assertTrue(connector._disconnected)128 return self.assertFailure(d, CancelledError)129 def test_cancelConnectTCP(self):130 """131 The L{Deferred} returned by L{ClientCreator.connectTCP} can be cancelled132 to abort the connection attempt before it completes.133 """134 def connect(cc):135 return cc.connectTCP('example.com', 1234)136 return self._cancelConnectTest(connect)137 def test_cancelConnectUNIX(self):138 """139 The L{Deferred} returned by L{ClientCreator.connectTCP} can be cancelled140 to abort the connection attempt before it completes.141 """142 def connect(cc):143 return cc.connectUNIX('/foo/bar')144 return self._cancelConnectTest(connect)145 def test_cancelConnectSSL(self):146 """147 The L{Deferred} returned by L{ClientCreator.connectTCP} can be cancelled148 to abort the connection attempt before it completes.149 """150 def connect(cc):151 return cc.connectSSL('example.com', 1234, object())152 return self._cancelConnectTest(connect)153 def _cancelConnectTimeoutTest(self, connect):154 """155 Like L{_cancelConnectTest}, but for the case where the L{Deferred} is156 cancelled after the connection is set up but before it is fired with the157 resulting protocol instance.158 """159 reactor = MemoryReactorWithConnectorsAndTime()160 cc = ClientCreator(reactor, Protocol)161 d = connect(reactor, cc)162 connector = reactor.connectors.pop()163 # Sanity check - there is an outstanding delayed call to fire the164 # Deferred.165 self.assertEqual(len(reactor.getDelayedCalls()), 1)166 # Cancel the Deferred, disconnecting the transport just set up and167 # cancelling the delayed call.168 d.cancel()169 self.assertEqual(reactor.getDelayedCalls(), [])170 # A real connector implementation is responsible for disconnecting the171 # transport as well. For our purposes, just check that someone told the172 # connector to disconnect.173 self.assertTrue(connector._disconnected)174 return self.assertFailure(d, CancelledError)175 def test_cancelConnectTCPTimeout(self):176 """177 L{ClientCreator.connectTCP} inserts a very short delayed call between178 the time the connection is established and the time the L{Deferred}179 returned from one of its connect methods actually fires. If the180 L{Deferred} is cancelled in this interval, the established connection is181 closed, the timeout is cancelled, and the L{Deferred} fails with182 L{CancelledError}.183 """184 def connect(reactor, cc):185 d = cc.connectTCP('example.com', 1234)186 host, port, factory, timeout, bindAddress = reactor.tcpClients.pop()187 protocol = factory.buildProtocol(None)188 transport = StringTransport()189 protocol.makeConnection(transport)190 return d191 return self._cancelConnectTimeoutTest(connect)192 def test_cancelConnectUNIXTimeout(self):193 """194 L{ClientCreator.connectUNIX} inserts a very short delayed call between195 the time the connection is established and the time the L{Deferred}196 returned from one of its connect methods actually fires. If the197 L{Deferred} is cancelled in this interval, the established connection is198 closed, the timeout is cancelled, and the L{Deferred} fails with199 L{CancelledError}.200 """201 def connect(reactor, cc):202 d = cc.connectUNIX('/foo/bar')203 address, factory, timeout, bindAddress = reactor.unixClients.pop()204 protocol = factory.buildProtocol(None)205 transport = StringTransport()206 protocol.makeConnection(transport)207 return d208 return self._cancelConnectTimeoutTest(connect)209 def test_cancelConnectSSLTimeout(self):210 """211 L{ClientCreator.connectSSL} inserts a very short delayed call between212 the time the connection is established and the time the L{Deferred}213 returned from one of its connect methods actually fires. If the214 L{Deferred} is cancelled in this interval, the established connection is215 closed, the timeout is cancelled, and the L{Deferred} fails with216 L{CancelledError}.217 """218 def connect(reactor, cc):219 d = cc.connectSSL('example.com', 1234, object())220 host, port, factory, contextFactory, timeout, bindADdress = reactor.sslClients.pop()221 protocol = factory.buildProtocol(None)222 transport = StringTransport()223 protocol.makeConnection(transport)224 return d225 return self._cancelConnectTimeoutTest(connect)226 def _cancelConnectFailedTimeoutTest(self, connect):227 """228 Like L{_cancelConnectTest}, but for the case where the L{Deferred} is229 cancelled after the connection attempt has failed but before it is fired230 with the resulting failure.231 """232 reactor = MemoryReactorWithConnectorsAndTime()233 cc = ClientCreator(reactor, Protocol)234 d, factory = connect(reactor, cc)235 connector = reactor.connectors.pop()236 factory.clientConnectionFailed(237 connector, Failure(Exception("Simulated failure")))238 # Sanity check - there is an outstanding delayed call to fire the239 # Deferred.240 self.assertEqual(len(reactor.getDelayedCalls()), 1)241 # Cancel the Deferred, cancelling the delayed call.242 d.cancel()243 self.assertEqual(reactor.getDelayedCalls(), [])244 return self.assertFailure(d, CancelledError)245 def test_cancelConnectTCPFailedTimeout(self):246 """247 Similar to L{test_cancelConnectTCPTimeout}, but for the case where the248 connection attempt fails.249 """250 def connect(reactor, cc):251 d = cc.connectTCP('example.com', 1234)252 host, port, factory, timeout, bindAddress = reactor.tcpClients.pop()253 return d, factory254 return self._cancelConnectFailedTimeoutTest(connect)255 def test_cancelConnectUNIXFailedTimeout(self):256 """257 Similar to L{test_cancelConnectUNIXTimeout}, but for the case where the258 connection attempt fails.259 """260 def connect(reactor, cc):261 d = cc.connectUNIX('/foo/bar')262 address, factory, timeout, bindAddress = reactor.unixClients.pop()263 return d, factory264 return self._cancelConnectFailedTimeoutTest(connect)265 def test_cancelConnectSSLFailedTimeout(self):266 """267 Similar to L{test_cancelConnectSSLTimeout}, but for the case where the268 connection attempt fails.269 """270 def connect(reactor, cc):271 d = cc.connectSSL('example.com', 1234, object())272 host, port, factory, contextFactory, timeout, bindADdress = reactor.sslClients.pop()273 return d, factory274 return self._cancelConnectFailedTimeoutTest(connect)275class ProtocolTests(TestCase):276 """277 Tests for L{twisted.internet.protocol.Protocol}.278 """279 def test_interfaces(self):280 """281 L{Protocol} instances provide L{IProtocol} and L{ILoggingContext}.282 """283 proto = Protocol()284 self.assertTrue(verifyObject(IProtocol, proto))...
test_sa_connection.py
Source:test_sa_connection.py
...10 Column('id', Integer, nullable=False,11 primary_key=True),12 Column('name', String(255)))13@pytest.fixture()14def sa_connect(connection_creator):15 async def connect(**kwargs):16 conn = await connection_creator(**kwargs)17 await conn.autocommit(True)18 cur = await conn.cursor()19 await cur.execute("DROP TABLE IF EXISTS sa_tbl")20 await cur.execute("CREATE TABLE sa_tbl "21 "(id serial, name varchar(255))")22 await cur.execute("INSERT INTO sa_tbl (name)"23 "VALUES ('first')")24 await cur._connection.commit()25 # yield from cur.close()26 engine = mock.Mock()27 engine.dialect = sa.engine._dialect28 return sa.SAConnection(conn, engine)29 return connect30@pytest.mark.run_loop31async def test_execute_text_select(sa_connect):32 conn = await sa_connect()33 res = await conn.execute("SELECT * FROM sa_tbl;")34 assert isinstance(res.cursor, Cursor)35 assert ('id', 'name') == res.keys()36 rows = await res.fetchall()37 assert res.closed38 assert res.cursor is None39 assert 1 == len(rows)40 row = rows[0]41 assert 1 == row[0]42 assert 1 == row['id']43 assert 1 == row.id44 assert 'first' == row[1]45 assert 'first' == row['name']46 assert 'first' == row.name47 # TODO: fix this48 await conn._connection.commit()49@pytest.mark.run_loop50async def test_execute_sa_select(sa_connect):51 conn = await sa_connect()52 res = await conn.execute(tbl.select())53 assert isinstance(res.cursor, Cursor)54 assert ('id', 'name') == res.keys()55 rows = await res.fetchall()56 assert res.closed57 assert res.cursor is None58 assert res.returns_rows59 assert 1 == len(rows)60 row = rows[0]61 assert 1 == row[0]62 assert 1 == row['id']63 assert 1 == row.id64 assert 'first' == row[1]65 assert 'first' == row['name']66 assert 'first' == row.name67 # TODO: fix this68 await conn._connection.commit()69@pytest.mark.run_loop70async def test_execute_sa_insert_with_dict(sa_connect):71 conn = await sa_connect()72 await conn.execute(tbl.insert(), {"id": 2, "name": "second"})73 res = await conn.execute(tbl.select())74 rows = await res.fetchall()75 assert 2 == len(rows)76 assert (1, 'first') == rows[0]77 assert (2, 'second') == rows[1]78@pytest.mark.run_loop79async def test_execute_sa_insert_with_tuple(sa_connect):80 conn = await sa_connect()81 await conn.execute(tbl.insert(), (2, "second"))82 res = await conn.execute(tbl.select())83 rows = await res.fetchall()84 assert 2 == len(rows)85 assert (1, 'first') == rows[0]86 assert (2, 'second') == rows[1]87@pytest.mark.run_loop88async def test_execute_sa_insert_named_params(sa_connect):89 conn = await sa_connect()90 await conn.execute(tbl.insert(), id=2, name="second")91 res = await conn.execute(tbl.select())92 rows = await res.fetchall()93 assert 2 == len(rows)94 assert (1, 'first') == rows[0]95 assert (2, 'second') == rows[1]96@pytest.mark.run_loop97async def test_execute_sa_insert_positional_params(sa_connect):98 conn = await sa_connect()99 await conn.execute(tbl.insert(), 2, "second")100 res = await conn.execute(tbl.select())101 rows = await res.fetchall()102 assert 2 == len(rows)103 assert (1, 'first') == rows[0]104 assert (2, 'second') == rows[1]105@pytest.mark.run_loop106async def test_scalar(sa_connect):107 conn = await sa_connect()108 res = await conn.scalar(tbl.count())109 assert 1 == res110@pytest.mark.run_loop111async def test_scalar_None(sa_connect):112 conn = await sa_connect()113 await conn.execute(tbl.delete())114 res = await conn.scalar(tbl.select())115 assert res is None116 # TODO: fix this117 await conn._connection.commit()118@pytest.mark.run_loop119async def test_row_proxy(sa_connect):120 conn = await sa_connect()121 res = await conn.execute(tbl.select())122 rows = await res.fetchall()123 row = rows[0]124 row2 = await (await conn.execute(tbl.select())).first()125 assert 2 == len(row)126 assert ['id', 'name'] == list(row)127 assert 'id' in row128 assert 'unknown' not in row129 assert 'first' == row.name130 assert 'first' == row[tbl.c.name]131 with pytest.raises(AttributeError):132 row.unknown133 assert "(1, 'first')" == repr(row)134 assert (1, 'first') == row.as_tuple()135 assert (555, 'other') != row.as_tuple()136 assert row2 == row137 assert 5 != row138 # TODO: fix this139 await conn._connection.commit()140@pytest.mark.run_loop141async def test_insert(sa_connect):142 conn = await sa_connect()143 res = await conn.execute(tbl.insert().values(name='second'))144 assert 1 == res.rowcount145 assert 2 == res.lastrowid146@pytest.mark.run_loop147async def test_raw_insert(sa_connect):148 conn = await sa_connect()149 await conn.execute(150 "INSERT INTO sa_tbl (name) VALUES ('third')")151 res = await conn.execute(tbl.select())152 assert 2 == res.rowcount153 assert ('id', 'name') == res.keys()154 assert res.returns_rows155 rows = await res.fetchall()156 assert 2 == len(rows)157 assert 2 == rows[1].id158@pytest.mark.run_loop159async def test_raw_insert_with_params(sa_connect):160 conn = await sa_connect()161 res = await conn.execute(162 "INSERT INTO sa_tbl (id, name) VALUES (%s, %s)",163 2, 'third')164 res = await conn.execute(tbl.select())165 assert 2 == res.rowcount166 assert ('id', 'name') == res.keys()167 assert res.returns_rows168 rows = await res.fetchall()169 assert 2 == len(rows)170 assert 2 == rows[1].id171@pytest.mark.run_loop172async def test_raw_insert_with_params_dict(sa_connect):173 conn = await sa_connect()174 res = await conn.execute(175 "INSERT INTO sa_tbl (id, name) VALUES (%(id)s, %(name)s)",176 {'id': 2, 'name': 'third'})177 res = await conn.execute(tbl.select())178 assert 2 == res.rowcount179 assert ('id', 'name') == res.keys()180 assert res.returns_rows181 rows = await res.fetchall()182 assert 2 == len(rows)183 assert 2 == rows[1].id184@pytest.mark.run_loop185async def test_raw_insert_with_named_params(sa_connect):186 conn = await sa_connect()187 res = await conn.execute(188 "INSERT INTO sa_tbl (id, name) VALUES (%(id)s, %(name)s)",189 id=2, name='third')190 res = await conn.execute(tbl.select())191 assert 2 == res.rowcount192 assert ('id', 'name') == res.keys()193 assert res.returns_rows194 rows = await res.fetchall()195 assert 2 == len(rows)196 assert 2 == rows[1].id197@pytest.mark.run_loop198async def test_raw_insert_with_executemany(sa_connect):199 conn = await sa_connect()200 # with pytest.raises(sa.ArgumentError):201 await conn.execute(202 "INSERT INTO sa_tbl (id, name) VALUES (%(id)s, %(name)s)",203 [{"id": 2, "name": 'third'}, {"id": 3, "name": 'forth'}])204 await conn.execute(205 tbl.update().where(206 tbl.c.id == bindparam("id")207 ).values(208 {"name": bindparam("name")}209 ),210 [211 {"id": 2, "name": "t2"},212 {"id": 3, "name": "t3"}213 ]214 )215 with pytest.raises(sa.ArgumentError):216 await conn.execute(217 DropTable(tbl),218 [{}, {}]219 )220 with pytest.raises(sa.ArgumentError):221 await conn.execute(222 {},223 [{}, {}]224 )225@pytest.mark.run_loop226async def test_raw_select_with_wildcard(sa_connect):227 conn = await sa_connect()228 await conn.execute(229 'SELECT * FROM sa_tbl WHERE name LIKE "%test%"')230@pytest.mark.run_loop231async def test_delete(sa_connect):232 conn = await sa_connect()233 res = await conn.execute(tbl.delete().where(tbl.c.id == 1))234 assert () == res.keys()235 assert 1 == res.rowcount236 assert not res.returns_rows237 assert res.closed238 assert res.cursor is None239@pytest.mark.run_loop240async def test_double_close(sa_connect):241 conn = await sa_connect()242 res = await conn.execute("SELECT 1")243 await res.close()244 assert res.closed245 assert res.cursor is None246 await res.close()247 assert res.closed248 assert res.cursor is None249@pytest.mark.run_loop250@pytest.mark.skip("Find out how to close cursor on __del__ method")251async def test_weakrefs(sa_connect):252 conn = await sa_connect()253 assert 0 == len(conn._weak_results)254 res = await conn.execute("SELECT 1")255 assert 1 == len(conn._weak_results)256 cur = res.cursor257 assert not cur.closed258 # TODO: fix this, how close cursor if result was deleted259 # yield from cur.close()260 del res261 assert cur.closed262 assert 0 == len(conn._weak_results)263@pytest.mark.run_loop264async def test_fetchall(sa_connect):265 conn = await sa_connect()266 await conn.execute(tbl.insert().values(name='second'))267 res = await conn.execute(tbl.select())268 rows = await res.fetchall()269 assert 2 == len(rows)270 assert res.closed271 assert res.returns_rows272 assert [(1, 'first'), (2, 'second')] == rows273@pytest.mark.run_loop274async def test_fetchall_closed(sa_connect):275 conn = await sa_connect()276 await conn.execute(tbl.insert().values(name='second'))277 res = await conn.execute(tbl.select())278 await res.close()279 with pytest.raises(sa.ResourceClosedError):280 await res.fetchall()281@pytest.mark.run_loop282async def test_fetchall_not_returns_rows(sa_connect):283 conn = await sa_connect()284 res = await conn.execute(tbl.delete())285 with pytest.raises(sa.ResourceClosedError):286 await res.fetchall()287@pytest.mark.run_loop288async def test_fetchone_closed(sa_connect):289 conn = await sa_connect()290 await conn.execute(tbl.insert().values(name='second'))291 res = await conn.execute(tbl.select())292 await res.close()293 with pytest.raises(sa.ResourceClosedError):294 await res.fetchone()295@pytest.mark.run_loop296async def test_first_not_returns_rows(sa_connect):297 conn = await sa_connect()298 res = await conn.execute(tbl.delete())299 with pytest.raises(sa.ResourceClosedError):300 await res.first()301@pytest.mark.run_loop302async def test_fetchmany(sa_connect):303 conn = await sa_connect()304 await conn.execute(tbl.insert().values(name='second'))305 res = await conn.execute(tbl.select())306 rows = await res.fetchmany()307 assert 1 == len(rows)308 assert not res.closed309 assert res.returns_rows310 assert [(1, 'first')] == rows311@pytest.mark.run_loop312async def test_fetchmany_with_size(sa_connect):313 conn = await sa_connect()314 await conn.execute(tbl.insert().values(name='second'))315 res = await conn.execute(tbl.select())316 rows = await res.fetchmany(100)317 assert 2 == len(rows)318 assert not res.closed319 assert res.returns_rows320 assert [(1, 'first'), (2, 'second')] == rows321@pytest.mark.run_loop322async def test_fetchmany_closed(sa_connect):323 conn = await sa_connect()324 await conn.execute(tbl.insert().values(name='second'))325 res = await conn.execute(tbl.select())326 await res.close()327 with pytest.raises(sa.ResourceClosedError):328 await res.fetchmany()329@pytest.mark.run_loop330async def test_fetchmany_with_size_closed(sa_connect):331 conn = await sa_connect()332 await conn.execute(tbl.insert().values(name='second'))333 res = await conn.execute(tbl.select())334 await res.close()335 with pytest.raises(sa.ResourceClosedError):336 await res.fetchmany(5555)337@pytest.mark.run_loop338async def test_fetchmany_not_returns_rows(sa_connect):339 conn = await sa_connect()340 res = await conn.execute(tbl.delete())341 with pytest.raises(sa.ResourceClosedError):342 await res.fetchmany()343@pytest.mark.run_loop344async def test_fetchmany_close_after_last_read(sa_connect):345 conn = await sa_connect()346 res = await conn.execute(tbl.select())347 rows = await res.fetchmany()348 assert 1 == len(rows)349 assert not res.closed350 assert res.returns_rows351 assert [(1, 'first')] == rows352 rows2 = await res.fetchmany()353 assert 0 == len(rows2)354 assert res.closed355@pytest.mark.run_loop356async def test_create_table(sa_connect):357 conn = await sa_connect()358 res = await conn.execute(DropTable(tbl))359 with pytest.raises(sa.ResourceClosedError):360 await res.fetchmany()361 with pytest.raises(aiomysql.ProgrammingError):362 await conn.execute("SELECT * FROM sa_tbl")363 res = await conn.execute(CreateTable(tbl))364 with pytest.raises(sa.ResourceClosedError):365 await res.fetchmany()366 res = await conn.execute("SELECT * FROM sa_tbl")...
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!!