def scrape_votes(self, contest, table_data_attrib=None): votes_dict = defaultdict(lambda: defaultdict(int)) # Create the voting table for the contest voting_grid = self.soup.find('div', {'id': 'voting_grid'}) if not voting_grid.contents: return votes_dict # Switch to other table for tele/jury voting if table_data_attrib: btn = self.driver.find_element_by_xpath( '//button[@data-button="{}"]'.format(table_data_attrib)) btn.send_keys(keys.Keys.SPACE) soup = BeautifulSoup(self.driver.page_source, features='html.parser') voting_grid = soup.find('div', {'id': 'voting_grid'}) if len(voting_grid.text) > 0: # Create country id/value pairs, since voting countries can be different than participating # countries (e.g. San Marino in 2007) countries = {} for row in voting_grid.find('thead').findAll('tr'): cols = row.find_all('td') for c in cols: img = c.find('img') if img: countries[c['tdid']] = img['alt'] if 'data-from' in c.attrs: countries[c['data-from']] = c['data-from'] for row in voting_grid.find('tbody').findAll('tr'): cols = row.find_all('td') country_name = cols[2].text country_id = cols[2]['data-to'] # total_points = cols[3].text points_cols = cols[4:] for p in points_cols: from_country_id = p['data-from'] to_country_id = p['data-to'] if not p.text: votes_dict[from_country_id][to_country_id] = 0 else: votes_dict[from_country_id][to_country_id] = int( p.text) from_country_name = countries[from_country_id] to_country_name = countries[to_country_id] contest.countries[from_country_id] = Country( from_country_name, from_country_id) contest.countries[to_country_id] = Country( to_country_name, to_country_id) return votes_dict
def addCountry(self, name, pop, area, continent): if name not in self._countryCat: self._countryCat[name] = (Country(name, pop, area, continent)) self._cDictionary[name] = continent return True else: return False
def main(): if "--defaults" not in sys.argv: name, country_name, population, area, popularity, gdp = cli.initial_inputs( ) player = Player(name=name) country = Country(name=country_name, population=population, area=area, popularity=popularity, gdp=gdp) else: player = Player() country = Country(ai=False) game = Game(player, country) loop(game)
def __init__(self, continentFile, countryDataFile): self._countryCat = {} self._cDictionary = {} # Reading the file containing continents and slotting them into their own dictionary continent = open(continentFile, "r") line = continent.readline() line = continent.readline() while line != "": line = line.strip("\n").split(",") self._cDictionary[line[0]] = line[1] line = continent.readline() continent.close() # Reading the country information and creating Country objects to be stored in a dictionary from country import Country country = open(countryDataFile, "r") line = country.readline() line = country.readline() while line != "": line = line.replace(",", "").strip("\n").split("|") self._countryCat[line[0]] = Country(line[0], line[1], line[2], self._cDictionary[line[0]]) line = country.readline() country.close()
def add_countries(self): """ add countries to the continent""" if not hasattr(self, 'countries'): self.countries = [] while len(self.countries) < self.countrycount: self.countries.append(Country(self.redis, {'continent': self}))
def __init__(self, continent_file, data_file): data_file = open(data_file, 'r') continent_file = open(continent_file, 'r') # cDictionary is a dictionary where the key is the country name and value is the continent. self._cDictionary = {} # CountryCat is a collection of countries from the class Country. self._countryCat = {} # Ignores header. next(continent_file) # Fills the cDictionary. for line in continent_file: line_parts = line.strip().split(",") self._cDictionary[line_parts[0]] = line_parts[1] #print(self._cDictionary["China"]) # Ignores header. next(data_file) # Fills the catalogue: # By separating the lines based on "|", and then further by "," # Then by converting components into integer and float correspondingly. for line in data_file: line_parts = line.strip().split("|") line_parts[1] = int((line_parts[1].replace(",", ""))) line_parts[2] = float((line_parts[2].replace(",", ""))) # Makes it an object of the Class Country self._countryCat[line_parts[0]] = (Country( line_parts[0], line_parts[1], line_parts[2], self._cDictionary[line_parts[0]]))
def __init__(self, countryFile): self.countryCat = dict() f = open(countryFile, 'r', encoding='utf-8', errors='ignore') rawData = f.readlines() for n in range(1, len(rawData)): cName, cContinent, cPop, cArea = rawData[n].split('|') self.countryCat[cName] = Country(cName, cPop, cArea, cContinent)
def __init__(self, continent, data): # pass two txt files as parameters in constructor. continentfile = open(continent, "r") datafile = open(data, "r") self._countryCat = [] self._cDictionary = {} continentfile.readline( ) # read first line of the countryfile, exclude that from self._cDictionary. for line in continentfile: line = line.strip() parts = line.split(",") # split by comma. self._countryName = parts[0] self._countryContinent = parts[1] self._cDictionary[parts[0]] = parts[ 1] # append country name as key and corresponding continent as value. datafile.readline( ) # read first line of the countryfile, exclude that from self._countryCat. for line in datafile: line = line.strip() line = line.replace(",", "") # eliminate commas between numbers. parts = line.split("|") # split by "|". self._countryData = Country(parts[0], parts[1], parts[2], self._cDictionary[parts[0]]) self._countryCat.append( self._countryData) # add country object to the list.
def __init__(self, firstFile, secondFile): self.countryCat = [] self.cDictionary = {} # This is where I open the files based on the file names given and turn them each into a list. countryInfo = open(firstFile, "r") CountryContinent = open(secondFile, "r") countryInfo = countryInfo.readlines() CountryContinent = CountryContinent.readlines() # Here I'm formatting the list of continents and countries (Country,Continent), to remove the comas and the "\n". # I then add each line of the file into the dictionary "cDictionary". # I use the "[1:]" in my for loop to skip the first line of the file (the header). for line in CountryContinent[1:]: line = line.split(",") line[1] = line[1].strip("\n") self.cDictionary[line[0]] = line[1] # Here I format the list of countries (Country|Population|Area) by removing any commas from numbers (so python will recognize them) and splitting them into lists separated by "|". # I then append it into the cDictionary (Country,Continent) # I use the "[1:]" in my for loop to skip the first line of the file (the header). for line in countryInfo[1:]: line = line.replace(",", "") line = line.split("|") line[2] = line[2].strip("\n") for i in self.cDictionary: if i == line[0]: line.append(self.cDictionary[i]) countryItem = Country(line[0], int(line[1]), float(line[2]), line[3]) self.countryCat.append(countryItem)
def createCountries(self): count = Constant.COUNTRIES_COUNT for i in range(count): color = Constant.COUNTRY_COLORS[np.random.randint(0, 3, 1)[0]] self.countries.append(Country(color)) return self.countries
def addCountry(self, countryName, pop, area, cont): if countryName not in self._countryCat: self._countryCat[countryName] = Country(countryName, pop, area, cont) return True else: return False
def scrape(self): page = requests.get(Scraper.URL) tree = html.fromstring(page.content) for elem in tree.xpath(Scraper.TABLE_ROW): c = Country(elem.xpath(Scraper.COUNTRY_NAME)[0], elem.xpath(Scraper.UNIT_NAME)[0], elem.xpath(Scraper.LINK)[0]) self.populateRatingsForCountry(c) self.writeToFile(c)
def __init__(self, file1, file2): self._cDictionary = {} infile = open(file1, "r") #loop through continent for line in infile: with open('continent.txt') as x: next(x) for line in x: line = line.strip() #separate splitline = line.split(",") #splitline by , self._cDictionary[splitline[0]] = splitline[ 1] #appending to cDictionary infile.close() self._countryCat = {} infile2 = open(file2, "r") for line in infile2: with open("data.txt") as t: next(t) for line in t: area = float(line.split("|")[2].replace( ",", "").strip()) #splitline by | then replace with , pop = int(line.split("|")[1].replace(",", "")) name = line.split("|")[0] continent = self._cDictionary[name] self._countryCat[name] = Country(name, pop, area, continent) infile2.close()
def addCountry(self, name, pop, area, continent): if name in self._countryCat: return False else: self._cDictionary[name] = continent self._countryCat[name] = Country(name, pop, area, continent) return True
def __init__(self, firstFile, secondFile): self._cDictionary = {} continentFile = open(firstFile, "r") for line in continentFile: line = line.strip("\n") continentList = line.split(",") aKeywordName = continentList[0] aValueContinent = continentList[1] self._cDictionary[aKeywordName] = aValueContinent self._cDictionary.pop("Country") self._countryCat = {} self._nameList = [] self._populationList = [] self._areaList = [] dataFile = open(secondFile, "r") for line in dataFile: line = line.strip("\n") line = line.replace(",", "") line = line.split("|") self._nameList.append(line[0]) self._populationList.append(line[1]) self._areaList.append(line[2]) self._nameList.pop(0) self._populationList.pop(0) self._areaList.pop(0) for name in self._nameList: # creating a dict with country names and country objects. countryObject = Country( name, self._populationList[self._nameList.index(name)], self._areaList[self._nameList.index(name)], self._cDictionary[name]) self._countryCat[name] = countryObject
def __init__(self, countryFile = "", continentFile = ""): #Creating variables; a list and a dictionary self._countryCat = [] self._cDictionary = {} #Opening readers to read the text files readCountryFile = open(countryFile, 'r') readContinentFile = open(continentFile, 'r') #Constructing a dictionary from the continent file for line in readContinentFile: newline = line.split(",") self._cDictionary[newline[0].title()] = newline[1].strip() if "Country" in self._cDictionary: self._cDictionary.pop("Country") #to disregard the first line #Constructing a catalog of country objects from the data file firstline = True for line in readCountryFile: newline = line.split("|") if not firstline: #to disregard the first line newcountry = Country(newline[0], int(removePunctuation(newline[1])), float(removePunctuation(newline[2])), self._cDictionary[newline[0].title()]) self._countryCat.append(newcountry) firstline = False #Closing the file readers readCountryFile.close() readContinentFile.close()
def main(): num_of_samp = 10000 for name in sys.argv[1:]: if not country_name_valid(name): print 'Invalid country name: ' + name return False if len(sys.argv[1:]) < 2: print 'Input country names must be more than one!' return False country_list = [] for data in input_data.data_2018: ctry = Country(data[0], data[1], data[2]) country_list.append(ctry) cup_2018 = Cup('World Cup 2018') valid = 0 for i in range(0, num_of_samp): cup_2018.set_group_members(country_list, goupping_function_2018) if cup_2018.teams_in_same_group(sys.argv[1:]): valid += 1 result = round(float(valid) / float(num_of_samp) * 100, 2) msg = [] msg.append('The possibility of') msg.append(', '.join(sys.argv[1:])) msg.append('in the same group is') msg.append(str(result) + '%') print ' '.join(msg)
def createCountries(self, **args): count = args['count'] for i in range(count): dimensions = generator(Constant.REPRESENTATION[0], Constant.REPRESENTATION[1]) self.countries.append(Country(dimensions)) return self.countries
def test_get_cummulative_cases(self): country = Country("TEST") with open("./tests/data/australiadata.json", "r") as filehandler: country.data = json.loads(filehandler.readline()) country.get_cumulative_cases() self.assertEqual(country.cumulative_cases, [30, 39, 52, 55, 60])
def test_get_cummulative_deaths(self): country = Country("TEST") with open("./tests/data/australiadata.json", "r") as filehandler: country.data = json.loads(filehandler.readline()) country.get_cumulative_deaths() self.assertEqual(country.cumulative_deaths, [1, 1, 2, 2, 2])
def test_get_daily_deaths(self): country = Country("TEST") with open("./tests/data/australiadata.json", "r") as filehandler: country.data = json.loads(filehandler.readline()) country.get_daily_deaths() self.assertEqual(country.daily_deaths, [0, 1, 0, 0])
def readCountryTable(): #create country object and read the Countries ctry = Country() (dbStatus, countries) = ctry.readCountries(con) if (dbStatus == False): return render_template('error.html', error=ctry.error) else: return (countries)
def addCountry(self, name, pop, area, continent): for countryObject in self._countryCat: if countryObject.getName() == name: return False newCountry = Country(name, pop, area, continent) self._countryCat.append(newCountry) self._cDictionary[name] = continent return True
def worldcup_2018(): country_list = [] for data in input_data.data_2018: ctry = Country(data[0], data[1], data[2]) country_list.append(ctry) cup_2018 = Cup('World Cup 2018') cup_2018.set_group_members(country_list, goupping_function_2018) print cup_2018.all
def addCountry(self, c, p, a, ct): from country import Country if c not in self._countryCat: self._countryCat[c] = Country(c, p, a, ct) self._cDictionary[c] = ct return True else: return False
def addCountry(self, name, pop, area, continent): if not self.findCountry(name): newCountry = Country(name, pop, area, continent) self.countryCat.append(newCountry) self.cDictionary[name] = continent return True else: return False
def addCountry(self, newName = "", newPop = 0, newArea = 0.0, newContinent = ""): if self.findCountry(newName) is None: newName = newName.title() newContinent = newContinent.title() newCountry = Country(newName, newPop, newArea, newContinent) self._countryCat.append(newCountry) self._cDictionary[newName] = newContinent return True else: return False
def addCountry(self, newName, newPop, newArea, newContinent): # checks if the country already exists in countryCat if newName.title() not in countryCat: countryObject = Country(newName, newPop, newArea, newContinent) countryCat[newName.title()] = countryObject # returns True when the operation is successful return True else: # returns False when the country already exists return False
def addCountry(self, name, pop, area, continent): if name not in self._cDictionary: self._newCountry = Country(name, pop, area, continent) self._countryCat.append( self._newCountry ) # add new country to the self._countryCat list. self._cDictionary[ name] = continent # add new country to the self._cDictionary. return True else: return False
def read(path): countries = dict() with open(path, 'r') as file: reader = csv.reader(file) next(reader) for row in reader: countries[int(row[0])] = Country(row[1], row[2], row[3], row[4], row[5]) return countries