Best Python code snippet using hypothesis
legislator_spider.py
Source:legislator_spider.py  
1import scrapy2import logging, sys3from cand.items import CandItem4# This script contains spiders to crawl different page from ballotpedia.org5# Spider1: parse out state links6# Spider2: parse the majority of candidate (except: FL, NH for Senate)7# Spider3: parse only general election candidates (inconsistent table number)8# Spider4: (Senate Only) parse FL, NH as they have different html layout9# Note: NY senators have multiple party10# https://ballotpedia.org/United_States_Senate_election_in_New_York,_201611# This spider needs more work to actually output the JSON file - only prints to console now12class SenateSpider1(scrapy.Spider):13    name = "senate-StateURLs"14    # use the output for the start_urls for other Senate spiders15    allowed_domains = ["ballotpedia.org"]16    start_urls = ["https://ballotpedia.org/United_States_Congress_elections,_2016"]17    def parse(self, response):18	# grep 50 state's info19        for i, href in enumerate(response.xpath('//table[@class="infobox"]/descendant::node()/a[contains(@href, "United_States_Senate_election_in")]/@href').extract()):20            url = response.urljoin(href)21            print i+1, url 22class SenateSpider2(scrapy.Spider):23    name = "senate-AllCandidatesInfo"24    allowed_domains = ["ballotpedia.org"]25    #start_urls = ["https://ballotpedia.org/United_States_Senate_election_in_California,_2016"]26    start_urls = ["https://ballotpedia.org/United_States_Senate_election_in_Alabama,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Alaska,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Arizona,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Arkansas,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_California,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Colorado,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Connecticut,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Georgia,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Hawaii,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Idaho,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Illinois,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Indiana,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Iowa,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Kansas,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Kentucky,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Louisiana,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Maryland,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Missouri,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Nevada,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_New_York,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_North_Carolina,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_North_Dakota,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Ohio,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Oklahoma,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Oregon,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Pennsylvania,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_South_Carolina,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_South_Dakota,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Utah,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Vermont,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Washington,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Wisconsin,_2016"]27    def parse(self, response):28        i=129        for href in response.xpath('//a[contains(@href, "Independent") or contains(@href, "Libertarian") or contains(@href, "Republican_Party") or contains(@href, "Democratic_Party") or contains(@href, "Green_Party") or contains(@href, "Peace_and_Freedom_Party") or contains(@href, "Equality_Party") or contains(@href, "Working_Families_Party") or contains(@href, "Independence_Party_of_America") or contains(@href, "Conservative_Party") or contains(@href, "Reform_Party") or contains(@href, "Constitution_Party")]'):30            party = href.xpath('./@title').extract()[0]31            logging.info('Party: %s', party)32            ccurl = href.xpath('following-sibling::a/@href').extract()33            if len(ccurl) >= 1:34                logging.debug('candidates url found: %s', ccurl)35                curl = ccurl[0]36            else:37                logging.debug('no candidate found')38                continue39            logging.info("Processing person #%d", i)40            i+=141            name = curl.replace("_", " ")[1:] 42            cand_urls = response.urljoin(curl)43            logging.info('Candidate url: %s', cand_urls)44            item = CandItem()45            item['name'] = name46            item['url'] = cand_urls47            item['party'] = party #TODO: remove party as parse_cand now handle it48            item['chamber'] = 'S'49            yield scrapy.Request(cand_urls, callback=self.parse_cand, meta={'item':item}) 50    def parse_cand(self, response):51        item = response.meta['item']52        logging.debug('grab picure...')53        for p in response.xpath('//table[@class="infobox"]/descendant::node()/@src'):54            item['pic'] = response.urljoin(p.extract())55            logging.info('pic: %s', p.extract())56            break 57        # District number 0 for senators to match Sunlight candidate data58        if item['chamber'] == 'S':59            logging.debug('setting senate district 0')60            item['dist'] = '0'61        else:62            logging.info('grab house district...')63            # if there this person is running for64            pro = response.xpath('//*[@id="mw-content-text"]/table/tr[5]/td/text()').extract()65            if pro[0].split(',')[0] == 'Running for U.S. House':66                d = pro[0].split(',')[2]67            else:68                ln = response.xpath('//*[@id="mw-content-text"]/table/tr[4]/td/text()').extract()69                d = ln[0].split(',')[2]70            item['dist'] = d71            logging.info('dist: %s', d)72        logging.debug('grab party...')73        for l in response.xpath('//table[@class="infobox"]//a[contains(@href, "Independent") or contains(@href, "Libertarian") or contains(@href, "Republican") or contains(@href, "Democratic") or contains(@href, "Green") or contains(@href, "Peace_and_Freedom") or contains(@href, "Equality") or contains(@href, "Working_Families") or contains(@href, "Independence_of_America") or contains(@href, "Conservative") or contains(@href, "Reform") or contains(@href, "Constitution")]'):74            party = l.xpath('./@href').extract()[0]75            item['party'] = party[1:]76            logging.info('party: %s', party)77            break78        logging.debug('grab incumbent...')79        item['incumbent'] = 080        for l in response.xpath('//table[@class="infobox"]//td[contains(text(), "Incumbent")]'):81            item['incumbent'] = 182            logging.info('is Incumbent!')83        # inconsistent format too84        #for l in response.xpath('//*[@id="mw-content-text"]/table/tr[4]/td/text()').extract():85        #    h = l.split(',')[1][1:]86        #    item['state'] = h87        logging.debug('grab campaign website...')88        for l in response.xpath('//a[@class="external text" and contains(text() ,"Campaign")]/@href'):89            logging.info('camp: %s', l.extract())90            item['camp'] = l.extract()91            break92        logging.debug('grab twitter...')93        for l in response.xpath('//a[@class="external text" and text()="Twitter"]/@href'):94            logging.info('Twitter Feed: %s', l.extract())95            item['twtr'] = l.extract()96            break97        logging.debug('grab facebook...')98        for l in response.xpath('//a[@class="external text" and text()="Facebook"]/@href'):99            logging.info('Facebook Page: %s', l.extract())100            item['fb'] = l.extract()101            break102        logging.debug('grab youtube...')103        for y in response.xpath('//a[@class="external text" and text()="Youtube"]/@href'):104            logging.info('Youtube: %s', y.extract())105            item['youtube'] = y.extract()106            break107        yield item108class SenateSpider3(scrapy.Spider):109    name = "senate-GeneralElectionCandidatesInfo"110    allowed_domains = ["ballotpedia.org"]111    #start_urls = ["https://ballotpedia.org/United_States_Senate_election_in_Florida,_2016"]112    start_urls = ["https://ballotpedia.org/United_States_Senate_election_in_Florida,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Alabama,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Alaska,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Arizona,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Arkansas,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_California,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Colorado,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Connecticut,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Georgia,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Hawaii,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Idaho,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Illinois,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Indiana,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Iowa,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Kansas,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Kentucky,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Louisiana,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Maryland,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Missouri,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Nevada,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_New_York,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_North_Carolina,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_North_Dakota,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Ohio,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Oklahoma,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Oregon,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Pennsylvania,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_South_Carolina,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_South_Dakota,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Utah,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Vermont,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Washington,_2016" ,"https://ballotpedia.org/United_States_Senate_election_in_Wisconsin,_2016"]113    def __init__(self):114        self.S2 = SenateSpider2()115    def parse(self, response):116        i=1117        logging.info('url: %s', response.url)118        state = response.url.split('in_')[1].split(',')[0].replace('_', ' ')119        logging.info('state: %s', state)120        for h in response.xpath("//table[contains(tr/td[2]/p[1]/b/big, 'General election candidates')]//a[not(contains(@href, 'Party') or contains(@href, '#cite_note') or contains(@href, 'Independent'))]/@href").extract():121            url = response.urljoin(h)122            logging.info('candidate url: %s', url)123            name = h.replace("_", " ")[1:] 124            item = CandItem()125            item['name'] = name126            item['url'] = url127            item['chamber'] = 'S'128            item['state'] = state129            item['gen_election_candidate'] = 1 130            yield scrapy.Request(url, callback=self.S2.parse_cand, meta={'item':item}) 131# sameple urls132#start_urls=["https://ballotpedia.org/Patty_Murray" ,"https://ballotpedia.org/Chris_Vance" ,"https://ballotpedia.org/Phil_Cornell" ,"https://ballotpedia.org/Mohammad_Said" ,"https://ballotpedia.org/Thor_Amundson" ,"https://ballotpedia.org/Uncle_Mover" ,"https://ballotpedia.org/Eric_John_Makus" ,"https://ballotpedia.org/Scott_Nazarino" ,"https://ballotpedia.org/Mike_Luke" ,"https://ballotpedia.org/Sam_Wright_(Washington)" ,"https://ballotpedia.org/Zach_Haller" ,"https://ballotpedia.org/Donna_Rae_Lands" ,"https://ballotpedia.org/Alex_Tsimerman" ,"https://ballotpedia.org/Pano_Churchill" ,"https://ballotpedia.org/Ted_Cummings" ,"https://ballotpedia.org/Chuck_Jackson" ,"https://ballotpedia.org/Jeremy_Teuton"] 133# spider3 applies to FL & NH for Senate134# Different layout for FL, and NH. Had to hardcode the xpath here135class SenateSpider4(scrapy.Spider):136    name = "senate-Special_FL_NH"137    allowed_domains = ["ballotpedia.org"]138    start_urls = ["https://ballotpedia.org/United_States_Senate_election_in_New_Hampshire,_2016"]139    def __init__(self):140        self.S2 = SenateSpider2()141    def parse(self, response):142        logging.debug('grab Democratic_Party candidates...') 143        for h in response.xpath('//div[@id="bodyContent"]/table[3]/tr/td[1]/a/@href').extract():144            url = response.urljoin(h)145            logging.info('candidate url: %s', url)146        147            name = h.replace("_", " ")[1:] 148            item = CandItem()149            item['name'] = name150            item['url'] = url151            item['chamber'] = 'S'152            yield scrapy.Request(url, callback=self.S2.parse_cand, meta={'item':item}) 153        logging.debug('grab Republican_Party candidates...')154        for h in response.xpath('//*[@id="bodyContent"]/table[3]/tr/td[3]/a/@href').extract():155            url = response.urljoin(h)156            logging.info('candidate url: %s', url)157            name = h.replace("_", " ")[1:] 158            item = CandItem()159            item['name'] = name160            item['url'] = url161            item['chamber'] = 'S'162            yield scrapy.Request(url, callback=self.S2.parse_cand, meta={'item':item}) 163class HouseSpider1(scrapy.Spider):164    name = "house-StateURLs"165    allowed_domains = ["ballotpedia.org"]166    start_urls = ["https://ballotpedia.org/United_States_Congress_elections,_2016"]167    def parse(self, response):168        for i, href in enumerate(response.xpath('//table[@class="infobox"]/descendant::node()/a[contains(@href, "United_States_House_of_Representatives_elections_in")]/@href').extract()):169            url = response.urljoin(href)170            print i+1, url 171class HouseSpider3(scrapy.Spider):172    name = "house-GeneralElectionCandidatesInfo"173    allowed_domains = ["ballotpedia.org"]174    #start_urls = ["https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Washington,_2016"]175    start_urls = ["https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Alabama,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Alaska,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Arizona,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Arkansas,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_California,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Colorado,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Connecticut,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Delaware,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Florida,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Georgia,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Hawaii,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Idaho,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Illinois,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Indiana,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Iowa,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Kansas,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Kentucky,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Louisiana,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Maine,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Maryland,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Massachusetts,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Michigan,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Minnesota,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Mississippi,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Missouri,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Montana,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Nebraska,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Nevada,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_New_Hampshire,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_New_Jersey,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_New_Mexico,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_New_York,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_North_Carolina,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_North_Dakota,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Ohio,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Oklahoma,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Oregon,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Pennsylvania,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Rhode_Island,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_South_Carolina,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_South_Dakota,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Tennessee,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Texas,_2014", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Utah,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Vermont,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Virginia,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Washington,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_West_Virginia,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Wisconsin,_2016", "https://ballotpedia.org/United_States_House_of_Representatives_elections_in_Wyoming,_2016"]176    def __init__(self):177        self.S2 = SenateSpider2()178    def parse(self, response):179        i=1180        logging.info('url: %s', response.url)181        state = response.url.split('in_')[1].split(',')[0].replace('_', ' ')182        logging.info('state: %s', state)183        for h in response.xpath("//table[contains(tr/td[2]/p[1]/b/big, 'General election candidates')]//a[not(contains(@href, 'Party') or contains(@href, '#cite_note') or contains(@href, 'Independent'))]/@href").extract():184            url = response.urljoin(h)185            logging.info('candidate url: %s', url)186            name = h.replace("_", " ")[1:] 187            item = CandItem()188            item['name'] = name189            item['url'] = url190            item['chamber'] = 'H'191            item['state'] = state192            item['gen_election_candidate'] = 1 ...coop_evol.py
Source:coop_evol.py  
1#    This file is part of DEAP.2#3#    DEAP is free software: you can redistribute it and/or modify4#    it under the terms of the GNU Lesser General Public License as5#    published by the Free Software Foundation, either version 3 of6#    the License, or (at your option) any later version.7#8#    DEAP is distributed in the hope that it will be useful,9#    but WITHOUT ANY WARRANTY; without even the implied warranty of10#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the11#    GNU Lesser General Public License for more details.12#13#    You should have received a copy of the GNU Lesser General Public14#    License along with DEAP. If not, see <http://www.gnu.org/licenses/>.15"""This example contains the evolving test from *Potter, M. and De Jong, K.,162001, Cooperative Coevolution: An Architecture for Evolving Co-adapted17Subcomponents.* section 4.2.4. The number of species is evolved by adding and18removing species as stagnation occurs.19"""20import random21try:22    import matplotlib.pyplot as plt23    plt.figure()24except:25    plt = False26import numpy27from deap import algorithms28from deap import tools29import coop_base30IND_SIZE = coop_base.IND_SIZE31SPECIES_SIZE = coop_base.SPECIES_SIZE32NUM_SPECIES = 133TARGET_SIZE = 3034IMPROVMENT_TRESHOLD = 0.535IMPROVMENT_LENGTH = 536EXTINCTION_TRESHOLD = 5.037noise =      "*##*###*###*****##*##****#*##*###*#****##******##*#**#*#**######"38schematas = ("1##1###1###11111##1##1111#1##1###1#1111##111111##1#11#1#11######",39             "1##1###1###11111##1##1000#0##0###0#0000##000000##0#00#0#00######",40             "0##0###0###00000##0##0000#0##0###0#0000##001111##1#11#1#11######")41toolbox = coop_base.toolbox42toolbox.register("evaluateContribution", coop_base.matchSetContribution)43def main(extended=True, verbose=True):44    target_set = []45    species = []46    47    stats = tools.Statistics(lambda ind: ind.fitness.values)48    stats.register("avg", numpy.mean)49    stats.register("std", numpy.std)50    stats.register("min", numpy.min)51    stats.register("max", numpy.max)52    53    logbook = tools.Logbook()54    logbook.header = "gen", "species", "evals", "std", "min", "avg", "max"55    56    ngen = 30057    g = 058    59    for i in range(len(schematas)):60        size = int(TARGET_SIZE/len(schematas))61        target_set.extend(toolbox.target_set(schematas[i], size))62    63    species = [toolbox.species() for _ in range(NUM_SPECIES)]64    species_index = list(range(NUM_SPECIES))65    last_index_added = species_index[-1]66    67    # Init with random a representative for each species68    representatives = [random.choice(species[i]) for i in range(NUM_SPECIES)]69    best_fitness_history = [None] * IMPROVMENT_LENGTH70    71    if plt and extended:72        contribs = [[]]73        stag_gen = []74        collab = []75    76    while g < ngen:77        # Initialize a container for the next generation representatives78        next_repr = [None] * len(species)79        for (i, s), j in zip(enumerate(species), species_index):80            # Vary the species individuals81            s = algorithms.varAnd(s, toolbox, 0.6, 1.0)82            83            # Get the representatives excluding the current species84            r = representatives[:i] + representatives[i+1:]85            for ind in s:86                # Evaluate and set the individual fitness87                ind.fitness.values = toolbox.evaluate([ind] + r, target_set)88            89            record = stats.compile(s)90            logbook.record(gen=g, species=j, evals=len(s), **record)91            92            if verbose: 93                print(logbook.stream)94            95            # Select the individuals96            species[i] = toolbox.select(s, len(s))  # Tournament selection97            next_repr[i] = toolbox.get_best(s)[0]   # Best selection98            99            if plt and extended:100                # Book keeping of the collaborative fitness101                collab.append(next_repr[i].fitness.values[0])102            103            g += 1104        105        representatives = next_repr106        107        # Keep representatives fitness for stagnation detection108        best_fitness_history.pop(0)109        best_fitness_history.append(representatives[0].fitness.values[0])110        111        try:112            diff = best_fitness_history[-1] - best_fitness_history[0]113        except TypeError:114            diff = float("inf")115        116        if plt and extended:117            for (i, rep), j in zip(enumerate(representatives), species_index):118                contribs[j].append((toolbox.evaluateContribution(representatives,119                    target_set, i)[0], g-1))120        121        if diff < IMPROVMENT_TRESHOLD:122            if len(species) > 1:123                contributions = []124                for i in range(len(species)):125                    contributions.append(toolbox.evaluateContribution(representatives, target_set, i)[0])126                127                for i in reversed(range(len(species))):128                    if contributions[i] < EXTINCTION_TRESHOLD:129                        species.pop(i)130                        species_index.pop(i)131                        representatives.pop(i)132            133            last_index_added += 1134            best_fitness_history = [None] * IMPROVMENT_LENGTH135            species.append(toolbox.species())136            species_index.append(last_index_added)137            representatives.append(random.choice(species[-1]))138            if extended and plt:139                stag_gen.append(g-1)140                contribs.append([])141    if extended:142        for r in representatives:143            # print final representatives without noise144            print("".join(str(x) for x, y in zip(r, noise) if y == "*"))145    146    if extended and plt:      # Ploting of the evolution147        line1, = plt.plot(collab, "--", color="k")148        149        for con in contribs:150            try:151                con, g = zip(*con)152                line2, = plt.plot(g, con, "-", color="k")153            except ValueError:154                pass155        156        axis = plt.axis("tight")157        158        for s in stag_gen:159            plt.plot([s, s], [0, axis[-1]], "--", color="k")160        161        plt.legend((line1, line2), ("Collaboration", "Contribution"), loc="center right")162        plt.xlabel("Generations")163        plt.ylabel("Fitness")164        plt.show()165    166if __name__ == "__main__":...solver15.py
Source:solver15.py  
...24        words = [re.sub("[^а-ÑÑ]+", "0", word.strip(punctuation)) for word in text.split() if ")" in word25                 and any([n in word for n in placeholders])]26        n_number = text.split(".")[0].split()[-1].lower()27        return text, words, n_number28    def get_representatives(self, word, representatives, threshold=70):29        representatives = [rep for rep in representatives if fuzz.ratio(word, rep) >= threshold]30        return representatives31    def get_similarity(self, x, representatives):32        y = [self.representatives["n"][rep] if rep in self.representatives["n"]33             else self.representatives["nn"][rep] for rep in representatives]34        similarity = max([cosine_similarity(x, y_.reshape(1, -1))[0][0] for y_ in y])35        return similarity36    37    def parse_representatives(self, task):38        text, words, n_number = self.process_task(task)39        solution = self.get_target(task)40        if len(n_number) == 1:41            n_words = [re.sub("[^а-ÑÑ]+", "н", word.strip(punctuation)) for word in text.split()42                       if any([d in word for d in solution]) and ")" in word]43            for word in n_words:44                if word not in self.representatives["n"]:45                    self.representatives["n"][word] = self.token_embedding([word])46            for word in words:47                n_replacement = word.replace("0", "н")48                nn_replacement = word.replace("0", "нн")49                if n_replacement not in n_words and nn_replacement not in self.representatives["nn"]:50                    self.representatives["nn"][nn_replacement] = self.token_embedding([nn_replacement])51        elif len(n_number) == 2:52            nn_words = [re.sub("[^а-ÑÑ]+", "нн", word.strip(punctuation)) for word in text.split()53                        if any([d in word for d in solution]) and ")" in word]54            for word in nn_words:55                if word not in self.representatives["nn"]:56                    self.representatives["nn"][word] = self.token_embedding([word]) 57            for word in words:58                n_replacement = word.replace("0", "н")59                nn_replacement = word.replace("0", "нн")60                if nn_replacement not in nn_words and n_replacement not in self.representatives["n"]:61                    self.representatives["n"][n_replacement] = self.token_embedding([n_replacement])62    def fit(self, tasks):63        for task in tasks:64            self.parse_representatives(task)65            66    def save(self, path="data/models/solver15.pkl"):67        with open(path, "wb") as f:68            pickle.dump(self.representatives, f)69    70    def load(self, path="data/models/solver15.pkl"):71        with open(path, "rb") as f:72            self.representatives = pickle.load(f)73    def predict_from_model(self, task):74        prediction = []75        text, words, n_number = self.process_task(task)76        for i, word in enumerate([word.replace("0", "н") for word in words]):77            representatives = {}78            x = self.token_embedding([word]).reshape(1, -1)79            c1 = self.get_representatives(word, self.representatives["n"])80            c2 = self.get_representatives(word, self.representatives["nn"])81            if c1:82                representatives["н"] = self.get_similarity(x, c1)83            if c2:84                representatives["нн"] = self.get_similarity(x, c2)85            if representatives:86                answer = max(representatives.items(), key=itemgetter(1))[0]87                if answer == n_number:88                    prediction.append(str(i + 1))...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!!
