def get_country_code(country_name): """根据指定的国家,返回Pygal使用的两个字母的国别码""" for code, name in COUNTRIES.items(): if name == country_name: return code #如果没有找到指定的国家,就返回None elif country_name not in COUNTRIES.values(): lost_country.add(country_name) print(len(lost_country))
def get_country_code(country_name): """根据指定的国家, 返回国家代码""" for code, name in COUNTRIES.items(): if name == country_name: return code # 如果没有找到指定的国家, 就返回 None return None
def get_country_code(country_name): """根据指定的国家,返回pygal使用两个字母的国别码""" for code, name in COUNTRIES.items(): if name == country_name or name == country_name.title(): return code #如果没有找到指定的国家,返回NONE return None
def get_country_code(country_name): """Return the Pygal 2-digit country code for the given country.""" for code, name in COUNTRIES.items(): if name == country_name: return code # If the country wasn't found, return None. return None
def adapt(chart, data): if isinstance(chart, pygal.DateY): # Convert to a credible datetime return list(map( lambda t: (datetime.fromtimestamp(1360000000 + t[0] * 987654) if t[0] is not None else None, t[1]), data)) if isinstance(chart, pygal.XY): return data data = cut(data) if isinstance(chart, pygal.Worldmap): return list( map(lambda x: list( COUNTRIES.keys())[ int(x) % len(COUNTRIES)] if x is not None else None, data)) elif isinstance(chart, pygal.FrenchMap_Regions): return list( map(lambda x: list( REGIONS.keys())[ int(x) % len(REGIONS)] if x is not None else None, data)) elif isinstance(chart, pygal.FrenchMap_Departments): return list( map(lambda x: list( DEPARTMENTS.keys())[ int(x) % len(DEPARTMENTS)] if x is not None else None, data)) return data
def adapt(chart, data): if isinstance(chart, pygal.DateY): # Convert to a credible datetime return list( map( lambda t: (datetime.fromtimestamp(1360000000 + t[0] * 987654) if t[0] is not None else None, t[1]), data)) if isinstance(chart, pygal.XY): return data data = cut(data) if isinstance(chart, pygal.Worldmap): return list( map( lambda x: list(COUNTRIES.keys())[x % len(COUNTRIES)] if x is not None else None, data)) elif isinstance(chart, pygal.FrenchMap_Regions): return list( map( lambda x: list(REGIONS.keys())[x % len(REGIONS)] if x is not None else None, data)) elif isinstance(chart, pygal.FrenchMap_Departments): return list( map( lambda x: list(DEPARTMENTS.keys())[x % len(DEPARTMENTS)] if x is not None else None, data)) return data
def get_country_code(country_name): """根据指定的国家,返回Pygal使用的两个字母的国别码""" for code, name in COUNTRIES.items(): if name == country_name: return code #如果没有找到指定的国家,就返回None return None
def get_country_code(country_name): """根据指定的国家名,返回Pygal使用的两个字母的国别码""" for code, name in COUNTRIES.items(): if name == country_name: # 如果字典中存在指定的国家名,则返回其国别码,程序结束 return code # 如果没有找到指定的国家,就返回None,程序结束 return None
def get_country_code(country_name): """根据指定的国家,返回对应的国别码""" for code, name in COUNTRIES.items(): if name == country_name: return code # 如果没有找到对应国家,则返回None: return None
def get_country_code(country_name): """根据指定的国家返回pygal使用的两个字母的国别号""" for code,name in COUNTRIES.items(): if name == country_name: return code #如果没有找到指定的国家就返回none return None ## print(country_code,COUNTRIES[country_code])
def get_country_code(country_name): """根据国家返回国别码""" for code, name in COUNTRIES.items(): if name == country_name: return code return None # print(get_country_code("Andorra"))
def int_to_country(x): # This is used for test compatibility if isinstance(x, Number): try: x = int(x) except: return x if x >= 0 and x < len(COUNTRIES): return list(COUNTRIES.keys())[x] return x
from pygal.i18n import COUNTRIES for country_code in sorted(COUNTRIES.keys()): print(country_code, COUNTRIES(country_code))
from pygal.i18n import COUNTRIES for country_code in sorted(COUNTRIES.keys()): print(country_code, COUNTRIES[country_code])
def get_country_code(country_name): for code, name in COUNTRIES.items(): if name == country_name: return code return None
def treat_unnamed_code(codes): x = {} for code, name in COUNTRIES.items(): if code not in codes: x[code] = name return x
class Worldmap(Graph): """Worldmap graph""" _dual = True x_labels = list(COUNTRIES.keys()) country_names = COUNTRIES _adapters = [int_to_country] @cached_property def countries(self): return [val[0] for serie in self.all_series for val in serie.values if val[0] is not None] @cached_property def _values(self): """Getter for series values (flattened)""" return [val[1] for serie in self.series for val in serie.values if val[1] is not None] def _plot(self): map = etree.fromstring(MAP) map.set('width', str(self.view.width)) map.set('height', str(self.view.height)) for i, serie in enumerate(self.series): safe_vals = list(filter( lambda x: x is not None, cut(serie.values, 1))) if not safe_vals: continue min_ = min(safe_vals) max_ = max(safe_vals) for j, (country_code, value) in enumerate(serie.values): if value is None: continue if max_ == min_: ratio = 1 else: ratio = .3 + .7 * (value - min_) / (max_ - min_) country = map.find('.//*[@id="%s"]' % country_code) if country is None: continue cls = country.get('class', '').split(' ') cls.append('color-%d' % i) country.set('class', ' '.join(cls)) country.set( 'style', 'fill-opacity: %f' % ( ratio)) metadata = serie.metadata.get(j) if metadata: parent = country.getparent() node = decorate(self.svg, country, metadata) if node != country: country.remove(node) index = parent.index(country) parent.remove(country) node.append(country) parent.insert(index, node) last_node = len(country) > 0 and country[-1] if last_node is not None and last_node.tag == 'title': title_node = last_node text = title_node.text + '\n' else: title_node = self.svg.node(country, 'title') text = '' title_node.text = text + '[%s] %s: %d' % ( serie.title, self.country_names[country_code], value) self.nodes['plot'].append(map)
# Software Foundation, either version 3 of the License, or (at your option) any # later version. # # This library is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # details. # # You should have received a copy of the GNU Lesser General Public License # along with pygal. If not, see <http://www.gnu.org/licenses/>. import pygal from pygal.util import cut from datetime import datetime from pygal.i18n import COUNTRIES COUNTRY_KEYS = list(COUNTRIES.keys()) def get_data(i): return [ [(-1, 1), (2, 0), (0, 4)], [(0, 1), (None, 2), (3, 2)], [(-3, 3), (1, 3), (1, 1)], [(1, 1), (1, 1), (1, 1)], [(3, 2), (2, 1), (1, 1)]][i] def pytest_generate_tests(metafunc): if "Chart" in metafunc.funcargnames: metafunc.parametrize("Chart", pygal.CHARTS) if "datas" in metafunc.funcargnames: