def main(argv): if len(argv) != 2: return 1 input_file = argv[1] file_reader = FileReader(input_file) threads = [] while True: try: raw_world = file_reader.get_world() except StopIteration: break except FormatError: continue world = World(len(threads) + 1, raw_world) thread = world.run() if thread: threads.append(thread) thread.start() else: Country.clean_cash(len(threads) + 1) for thread in threads: thread.join() res = sorted(World.result) for key in res: print("Case Number ", key) for country in sorted(World.result[key]): print(country[1], country[0])
class GenderDetector: def __init__(self, country='us', unknown_value='unknown'): self.index = Index(country) self.country = Country(country) self.unknown_value = unknown_value def guess(self, name): name = self._format_name(name) initial_position = self.index(name[0]) with open(self.country.file()) as csvfile: csvfile.seek(initial_position) reader = csv.reader(csvfile) for row in reader: if row[0] == name: return self._guess(row) return self.unknown_value def _guess(self, row): gender = self.country.guess(row) if gender in ['male', 'female']: return gender else: return self.unknown_value def _format_name(self, name): name = name.strip() return name[0].upper() + name[1:].lower().strip()
class GenderDetector: def __init__(self, country='us', unknown_value='unknown'): self.index = Index(country) self.country = Country(country) self.unknown_value = unknown_value def guess(self, name): name = self._format_name(name) initial_position = self.index(name[0]) with open(self.country.file()) as csvfile: csvfile.seek(initial_position) reader = csv.reader(csvfile) for row in reader: if row[0] == name: return self._guess(row) return self.unknown_value def _guess(self, row): gender = self.country.guess(row) if gender in ['male', 'female']: return gender else: return self.unknown_value def _format_name(self, name): name = name.strip() return name[0].upper() + name[1:].lower().strip()
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_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_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 run(self): if self.is_init: if self.check_connections(): return Thread(target=self.world_handler) else: print("Not all country can be connect. World: {}".format( self.countries_names())) Country.clean_cash(self.__world_id) return None
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 __extract_country(self, source, source_type): if source.is_representing_country(): if source_type == 'way': return Country.extract_from_way(source=source, nodes=self.nodes) elif source_type == 'relation': return Country.extract_from_relation(source=source, nodes=self.nodes, ways=self.ways) raise ValueError( 'Source {0} is not representing country!'.format(source))
def __init__(self, con, DefaultCountry): # Set debugging for this module. self._db = Debug("customer", False) # create a country object and read the country code as passed. cntry = Country() (dbStatus, name) = cntry.readCountryByCode(con, DefaultCountry) if dbStatus == True: self._defCountryCode = DefaultCountry.upper() else: self._defCountryCode = None # Null the Customer on first initialising self.__nullCustomer()
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(): 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, 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, 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, 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 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 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
class Index: """ Generate index based on the position in bytes of every letter in the alphabet. The index is stored in an OrderedDict. It's a lazy index, the index is not generated till the first call. """ def __init__(self, country='us'): self.indices = [] self.country = Country(country) def __call__(self, letter): if len(self.indices) == 0: self._generate_index() return self.indices[letter.upper()] def _generate_index(self): self.indices = OrderedDict() with open(self.country.file()) as file: total = file.readline() # Omit headers line for line in file: if line[0] not in self.indices: self.indices[line[0]] = len(total) total = total + line
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 __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 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 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, 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 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
class Index: """ Generate index based on the position in bytes of every letter in the alphabet. The index is stored in an OrderedDict. It's a lazy index, the index is not generated till the first call. """ def __init__(self, country='us'): self.indices = [] self.country = Country(country) def __call__(self, letter): if len(self.indices) == 0: self._generate_index() return self.indices[letter.upper()] def _generate_index(self): self.indices = OrderedDict() with open(self.country.file()) as file: total = file.readline() # Omit headers line for line in file: if line[0] not in self.indices: self.indices[line[0]] = len(total) total = total + line
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 __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 test_get_case_fatality_rate(self): country = Country("TEST") with open("./tests/data/australiadata.json", "r") as filehandler: country.data = json.loads(filehandler.readline()) country.get_cumulative_cases() country.get_cumulative_deaths() country.get_case_fatality_rate() self.assertEqual(country.case_fatality_rate, [ 1 / 30 * 100, 1 / 39 * 100, 2 / 52 * 100, 2 / 55 * 100, 2 / 60 * 100 ])
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, 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
class TestPerson(unittest.TestCase): def setUp(self): self.Country = Country('United Kingdom') def tearDown(self): self.Country = None def test_country_initialisation(self): self.assertEqual(self.Country.Name, 'United Kingdom') def test_serialise(self): exp = '{"Name": "United Kingdom"}' s = self.Country.to_JSON() self.assertEqual(s,exp) def test_deserialise(self): s = self.Country.to_JSON() c = Country() c.LoadFromJSON(s) self.assertEqual(c.Name, self.Country.Name)
parent=Scorelist.single_key()) except Exception, e: logging.error( "Score.submit: Got exception when creating Score " \ + "model object. Type: %s, msg: %s", type( e ), e ) return Score.SUBMIT_FAIL try: new_score.put() except Exception, e: logging.error( "Score.submit: Got exception when putting score " \ + "to the datastore. Type: %s, msg: %s", type( e ), e ) return Score.SUBMIT_FAIL # Save the location of the submit. try: Country.save( location ) except Exception, msg: logging.warning( "Score.submit: Got exception when saving " \ + "location: '%s'", msg ) cls._delete_cached_list_if_invalid( control, location, points ) cls._delete_cached_list_if_invalid( control, config.LOCATION_WORLD, points ) cls._delete_cached_list_if_invalid( control, config.LOCATION_WEEK, points ) return Score.SUBMIT_SUCCESS @classmethod def _already_exists( cls, name, comment, points, control ): scores = Score.all().ancestor(Scorelist.single_key()) \
# coding=utf-8 import json import requests from ambit import Ambito from country import Country from utils import stringbetween, get_tv_channels_from_part, get_license_info page = requests.get('https://raw.githubusercontent.com/LaQuay/TDTChannels/master/TELEVISION.md', headers={'Cache-Control': 'no-cache'}) content = str(page.text) print("Updating TV files") spain = Country("Spain") andorra = Country("Andorra") international = Country("International") content_nacional = stringbetween(content, "## Nacionales", "## Locales") content_local = stringbetween(content, "## Locales", "## Internacionales") canales_nacionales = stringbetween(content_nacional, "", "## Informativos") spain.add_ambit(Ambito("Generalistas", get_tv_channels_from_part(canales_nacionales))) canales_informativos = stringbetween(content_nacional, "## Informativos", "## Deportivos") spain.add_ambit(Ambito("Informativos", get_tv_channels_from_part(canales_informativos))) canales_deportivos = stringbetween(content_nacional, "## Deportivos", "## Infantiles") spain.add_ambit(Ambito("Deportivos", get_tv_channels_from_part(canales_deportivos)))
def setUp(self): self.Country = Country('United Kingdom')
def __init__(self, address): self.__address = address self.__attempts = [] self.__country = Country.for_name(Address.GI.country_name_by_addr(address)) self.__country.add_address(self)
def __init__(self, country="us", unknown_value="unknown"): self.index = Index(country) self.country = Country(country) self.unknown_value = unknown_value
def get(self): self.response.out.write( "cronjob here!<br /><br />" ) clean_invisible = unicode( self.request.get( "clean_invisible" ) ) if clean_invisible == "yes": # Get a location. location = Country.get_random_location() # Get the locations lowest high score. for control in config.VALID_CONTROLS: lowest_score = Score.get_lowest_score( control, location ) if lowest_score is None: continue self.clean_country( control, location, lowest_score ) flush = unicode( self.request.get( "flush" ) ) if flush == "yes": memcache.flush_all() reflag_week_shallow = unicode( self.request.get( "reflag_week_shallow" ) ) if reflag_week_shallow == "yes": Score.reflag_new_week() Score._delete_cached_list( "tilt", config.LOCATION_WEEK ) Score._delete_cached_list( "touch", config.LOCATION_WEEK ) for control in ("tilt", "touch"): Score.get_top_list(config.TOP_LIST_LENGTH, control, config.LOCATION_WEEK) clear_world_week_duplicates = unicode( self.request.get( "clear_world_week_duplicates" ) ) if clear_world_week_duplicates == "yes": for location in ( config.LOCATION_WORLD, config.LOCATION_WEEK ): for control in config.VALID_CONTROLS: self.delete_duplicates( control, location ) clear_random_country_duplicates = unicode( self.request.get( "clear_random_country_duplicates" ) ) if clear_random_country_duplicates == "yes": location = Country.get_random_location() for control in config.VALID_CONTROLS: self.delete_duplicates( control, location ) clear_country_duplicates = unicode( self.request.get( "clear_country_duplicates" ) ) if clear_country_duplicates != "": for control in config.VALID_CONTROLS: self.delete_duplicates( control, clear_country_duplicates ) clear_all_country_duplicates = unicode( self.request.get( "clear_all_country_duplicates" ) ) if clear_all_country_duplicates == "yes": start_location = Country.next_country() location = start_location count = 0 try: while True: for control in config.VALID_CONTROLS: self.delete_duplicates( control, location ) location = Country.next_country() count += 1 except DeadlineExceededError, ex: logging.error( "CronJob.get: Got DeadlineExceededError. " \ + "Managed to clear %d countries from \"%s\" to \"%s\"", count, start_location, location ) return
def __init__(self, country='us'): self.indices = [] self.country = Country(country)
def __init__(self, country='us', unknown_value='unknown'): self.index = Index(country) self.country = Country(country) self.unknown_value = unknown_value