Skip to content

Instantly share code, notes, and snippets.

@eedeep
Created August 12, 2013 05:33
Show Gist options
  • Save eedeep/6208444 to your computer and use it in GitHub Desktop.
Save eedeep/6208444 to your computer and use it in GitHub Desktop.
def known_sortable(size):
try:
return int(size)
return float(size)
except ValueError:
if size.isalpha()
return size
return False
def compound_size(size):
separators = ['-', '/',]
for s in separators:
if s in size:
return s
return False
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:
# ok, we don't have a scale dict
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 known_sortable(size):
return known_sortable(size)
elif size.isalnum():
tokenize = re.compile(r'(\d+)|(\D+)').findall
return tuple(int(num) if num else alpha for num, alpha in tokenize(size))
elif compound_size(size):
<do the new stuff here>
else:
return size
except KeyError:
# we've got a scale dict but our size isn't in it
return size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment