EC2: Sort pricing by (non-)numeric part contrib/scrape-ec2-prices.py changes the ordering of data/pricing.json, as it sorts the dictionary keys by string, which puts '10' before '2'.
Add a function to split the dictionary keys into the numeric, alphabetic and other part and compare the first as integer, the middle by meaning, and the later as string. Signed-off-by: Anthony Shaw <anthony.p.s...@gmail.com> Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/78931d10 Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/78931d10 Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/78931d10 Branch: refs/heads/trunk Commit: 78931d10e0d6394d28f71b5923719046df71fc02 Parents: c43fcc8 Author: Philipp Hahn <h...@univention.de> Authored: Wed Dec 2 09:14:55 2015 +0100 Committer: Anthony Shaw <anthony.p.s...@gmail.com> Committed: Sat Dec 5 07:14:59 2015 +1100 ---------------------------------------------------------------------- contrib/scrape-ec2-prices.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/78931d10/contrib/scrape-ec2-prices.py ---------------------------------------------------------------------- diff --git a/contrib/scrape-ec2-prices.py b/contrib/scrape-ec2-prices.py index 4c7b653..b222705 100755 --- a/contrib/scrape-ec2-prices.py +++ b/contrib/scrape-ec2-prices.py @@ -114,6 +114,18 @@ REGION_NAME_MAP = { 'us-gov-west-1': 'ec2_us_govwest' } +INSTANCE_SIZES = [ + 'micro', + 'small', + 'medium', + 'large', + 'xlarge', + 'x-large', + 'extra-large' +] + +RE_NUMERIC_OTHER = re.compile(r'(?:([0-9]+)|([-A-Z_a-z]+)|([^-0-9A-Z_a-z]+))') + BASE_PATH = os.path.dirname(os.path.abspath(__file__)) PRICING_FILE_PATH = os.path.join(BASE_PATH, '../libcloud/data/pricing.json') PRICING_FILE_PATH = os.path.abspath(PRICING_FILE_PATH) @@ -178,7 +190,7 @@ def sort_nested_dict(value): """ result = OrderedDict() - for key, value in sorted(value.items()): + for key, value in sorted(value.items(), key=sort_key_by_numeric_other): if isinstance(value, (dict, OrderedDict)): result[key] = sort_nested_dict(value) else: @@ -187,6 +199,17 @@ def sort_nested_dict(value): return result +def sort_key_by_numeric_other(key_value): + """ + Split key into numeric, alpha and other part and sort accordingly. + """ + return tuple(( + int(numeric) if numeric else None, + INSTANCE_SIZES.index(alpha) if alpha in INSTANCE_SIZES else alpha, + other + ) for (numeric, alpha, other) in RE_NUMERIC_OTHER.findall(key_value[0])) + + def main(): print('Scraping EC2 pricing data')