当前位置: 首页>>代码示例>>Python>>正文


Python Util.json_dump方法代码示例

本文整理汇总了Python中util.Util.json_dump方法的典型用法代码示例。如果您正苦于以下问题:Python Util.json_dump方法的具体用法?Python Util.json_dump怎么用?Python Util.json_dump使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在util.Util的用法示例。


在下文中一共展示了Util.json_dump方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: from util import Util [as 别名]
# 或者: from util.Util import json_dump [as 别名]
def main():
  data = DataAggregator.create()

  with open('../items.json', 'w') as items_output:
    items_output.write(Util.json_dump(data.get_items()))
  print 'Successfully wrote items.json'

  with open('../champions.json', 'w') as champions_output:
    champions_output.write(Util.json_dump(data.get_champions()))
  print 'Successfully wrote champions.json'
开发者ID:omgimanerd,项目名称:itemify,代码行数:12,代码来源:get_items_champions.py

示例2: main

# 需要导入模块: from util import Util [as 别名]
# 或者: from util.Util import json_dump [as 别名]
def main():
    analyzer = DataAnalyzer.create()

    with open("../stats.json") as stats_input:
        stats = map(json.loads, stats_input.readlines())
    print "Read stats.json: %s entries found" % len(stats)

    # by_champion is a object with 126 keys, one for each champion id.
    # The value assigned to each key is a list of their game data generated
    # from stats.json
    by_champion = {}
    for stat in stats:
        champion = stat["champion"]
        if by_champion.get(champion, None):
            by_champion[champion].append(stat)
        else:
            by_champion[champion] = [stat]

    # build_stats is an object with 126 keys, one for each champion id.
    # The value assigned to each key is a dict of the format:
    # { itemId: effectivenessScore, itemId: effectivenessScore ...... }
    # effectivenessScore is a score of the item's effectiveness based on
    # win and KDA, and increases linearly with number of times built.
    build_stats = {}
    for champion in by_champion:
        championName = analyzer.get_champion_name_by_id(champion)
        build_stats[championName] = {}
        for game in by_champion[champion]:
            kda = 0
            if game["kills"]:
                kda += game["kills"]
            if game["assists"]:
                kda += game["assists"] / 2
            if game["deaths"]:
                kda /= float(game["deaths"])
            effectivenessScore = kda
            if game.get("win", False):
                effectivenessScore += 2
            for i in range(1, 7):
                item = game.get("item%s" % i, None)
                if item:
                    if build_stats[championName].get(item, None):
                        build_stats[championName][item] += effectivenessScore
                    else:
                        build_stats[championName][item] = effectivenessScore

    # The build stats will be written into the /stats-by-champion directory
    # with one JSON file per champion. We split this build data into viable
    # end game builds and intermediate builds by sorting the build data by
    # effectiveness score and parsing it then.
    for champion in build_stats:
        # trinkets, boots, endgame, and consumables all store items that people
        # have built for this champion, sorted in order of effectiveness.
        # buildObj is a dict that when dumped to a JSON object, becomes a valid
        # build file that someone can put in the League of Legends directory and
        # use.
        build_output = {}
        for category in CATEGORIES:
            build_output[category] = []

        effectiveness_sorted_items = sorted(build_stats[champion], key=build_stats[champion].get)[::-1]
        for item in effectiveness_sorted_items:
            if analyzer.is_irrelevant(item) or not analyzer.get_item_name_by_id(item):
                continue
            elif analyzer.is_starter(item):
                build_output["Starting Items"].append(item)
            elif analyzer.is_boot(item) and analyzer.get_item_depth(item) >= 2:
                build_output["Boots"].append(item)
            elif analyzer.is_jungle(item) and analyzer.get_item_depth(item) >= 2:
                build_output["Jungle Items"].append(item)
            elif analyzer.is_elixir(item):
                build_output["Elixirs"].append(item)
            elif not analyzer.get_items_built_from(item) and (not analyzer.is_trinket(item)):
                build_output["Endgame Items"].append(item)

        # For each category of item, we will only show a certain number of items and
        # will will generate the item set for each category. We will also sort
        # the items on build tree, where lower tier items will come first.
        generator = ItemSetGenerator.create(champion)
        for category in CATEGORIES:
            build_output[category] = build_output[category][: ITEM_LIMIT[category]]
            if SORT_TIER[category]:
                build_output[category] = sorted(
                    build_output[category], key=lambda item: analyzer.get_item_data_by_id(item).get("depth", 0)
                )

            items = ItemSetBlockItems()
            for item in build_output[category]:
                items.add_item(item, 1)
            generator.add_block("Recommended %s" % category, False, items.get_items(), category == "Jungle Items")
            build_output[category] = map(analyzer.get_item_data_by_id, build_output[category])
        item_set = generator.get_item_set()

        # We will write the build JSON file to one file and the parsed data to
        # another.
        data_file = Util.normalize_champion_name(champion)
        with open("../builds/%s.json" % data_file, "w") as champion_output:
            champion_output.write(Util.json_dump(build_output))
            print "Wrote %s.json" % data_file

#.........这里部分代码省略.........
开发者ID:omgimanerd,项目名称:itemify,代码行数:103,代码来源:generate_champion_builds.py


注:本文中的util.Util.json_dump方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。