Skip to content

Instantly share code, notes, and snippets.

@eedeep
Created August 12, 2013 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eedeep/6208341 to your computer and use it in GitHub Desktop.
Save eedeep/6208341 to your computer and use it in GitHub Desktop.
def scale_size(size, scale=None):
'''
This function receives size form choices. i.e. 'XXS' or 10 or A or whatever,
and an optional scale. If not scale is passed, returns the value of size in any of the default scales.
If scale is passed, then it returns the value of size in such scale.
'''
#default scales for certain type of products.
default_sizes = {'XXXXXS':-6, 'XXXXS':-5, 'XXXS':-4, 'XXS':-3, 'XS':-2, 'S':-1, 'M':0, 'L':1, 'XL':2, 'XXL':3, 'XXXL':4, 'XXXXL':5, 'XXXXXS':6}
baby_sizes = {"NEWBORN":0, "0-3M":1, "3-6M":2, "6-12M":3, "6-18M":4, "6-12M":5, "6-18M":6, "12-18M":7, "18-24M":8, "18-36M":9 }
kid_sizes = {"1-2Y":1, "2-3Y":2, "3-4Y":3, "4-5Y":4, "6-7Y":5, "8-9Y":6, "9-10Y":7}
#If scale then we will return the value for size in the scale if exists, otherwise the size.
#If not scale we will return the value for size in some of the defined scales if exists, otherwise size.
try:
return scale[size]
except TypeError:
if size in default_sizes:
return default_sizes[size]
elif size in baby_sizes:
return baby_sizes[size.upper()]
elif size in kid_sizes:
return kid_sizes[size.upper()]
elif size.isdigit():
return int(size)
elif size.isalnum() and not size.isalpha():
tokenize = re.compile(r'(\d+)|(\D+)').findall
return tuple(int(num) if num else alpha for num, alpha in tokenize(size))
else:
return size
except KeyError:
return size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment