0

リファクタリングしたい非常に厄介なコードがありますが、完全に読めなくなっているためです。

 for region in feed['config']['regions']:
            if region['region'] == region_name:
                for instance_type in region['instanceTypes']:
                    if instance_type['type'] == instance_type_name:
                        for instance_size in instance_type['sizes']:
                            if instance_size['size'] == instance_size_name:
                                for platform in instance_size['valueColumns']:
                                    if platform['name'] == platform_name:
                                        prices = platform['prices']
                                        assert prices.keys() == ['USD']
                                        return decimal.Decimal(prices['USD'])
        assert False, "Failed to determine price for instance with region=%r, type=%r, size=%r, platform=%r" % \
                (region_name, instance_type_name, instance_size_name, platform_name)

各ループまたは if ステートメントで関数を使用することを考えましたが、それでは関数の負荷がかかります。より良い解決策はありますか?

4

2 に答える 2

6
try:
    region = [r for r in feed['config']['regions'] if region['region'] == region_name][0]
    instance_type = [t for t in region['instanceTypes'] if i['type'] == instance_type_name][0]
    # ...
    return decimal.Decimal(prices['USD'])
except IndexError:
    raise Exception("Failed to determine price for instance with region=%r, type=%r, size=%r, platform=%r" % 
                (region_name, instance_type_name, instance_size_name, platform_name))

またはさらに一歩:

def filter_by_key(key, value, objects):
    return [o for o in objects if region[key] == value][0]

try:
    region = filter_by_key('region', region_name, feed['config']['regions'])
    instance_type = filter_by_key('type', instace_type_name, region['instanceTypes'])
    # ...
    return decimal.Decimal(prices['USD'])
except IndexError:
    raise Exception("Failed to determine price for instance with region=%r, type=%r, size=%r, platform=%r" % 
                (region_name, instance_type_name, instance_size_name, platform_name))
于 2012-04-10T07:46:03.587 に答える
2

ループに入れておきたい場合は、continueインデントを保存するために使用できます。

for region in feed['config']['regions']:
    if region['region'] != region_name: continue
    for instance_type in region['instanceTypes']:
        if instance_type['type'] != instance_type_name: continue
        for instance_size in instance_type['sizes']:
            if instance_size['size'] != instance_size_name: continue
            for platform in instance_size['valueColumns']:
                if platform['name'] != platform_name: continue
                prices = platform['prices']
                assert prices.keys() == ['USD']
                return decimal.Decimal(prices['USD'])
assert False, "Failed to determine price for instance with region=%r, type=%r, size=%r, platform=%r" % \
                (region_name, instance_type_name, instance_size_name, platform_name)

またはリスト内包表記を使用する場合:

regions = (r for r in feed['config']['regions'] if r['region'] == region_name)
instance_types = (i for i in r['instanceTypes'] for r in regions if i['type'] == instance_type_name)
instance_sizes = (i for i in t['sizes'] for t in instance_types if i['size'] == instance_size_name)
platforms = (p for p in s['valueColumns'] for s in instance_sizes if p['name'] == platform_name)
for platform in platforms:
    prices = platform['prices']
    assert prices.keys() == ['USD']
    return decimal.Decimal(prices['USD'])
assert False, "Failed to determine price for instance with region=%r, type=%r, size=%r, platform=%r" % \
                (region_name, instance_type_name, instance_size_name, platform_name)      
于 2012-04-10T07:44:32.697 に答える