Skip to content

Instantly share code, notes, and snippets.

@code-shoily
Last active July 22, 2016 16:46
Show Gist options
  • Save code-shoily/f0d9cba19024b01681a5f31743737b07 to your computer and use it in GitHub Desktop.
Save code-shoily/f0d9cba19024b01681a5f31743737b07 to your computer and use it in GitHub Desktop.
Separates fuzzily given size input (i.e. 1cm x 2", 10.4' x 2mm x 11 1/4", 10 meter) etc. Units are irrelevant.
# HOLY FUCK I OVERDOSED MY MEDS, I'M FUCKED
def sanitize(n):
return n.strip().replace(" ", "").replace("\\", "/")
to_decimal = lambda n: "/" not in n and float(n) or float(n.split("/")[0])/float(n.split("/")[1])
def separate_dims(n):
separated = filter(None, re.findall(r'\d*\.?/?\\?\d*', n))
return separated, len(separated)
def fractionize(s):
if "/" in s:
out = s[:-3], s[-3:]
else:
out = s, "0"
return float(out[0]) + to_decimal(out[1])
def main(n):
pass_1, dims = separate_dims(sanitize(n))
pass_2 = []
for i in pass_1:
pass_2.append(fractionize(i))
return pass_2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment