##  stud_info.py Version 1.01

##  Copyright (c) 2007 Bruce Vaughan, BV Detailing & Design, Inc.

##  All rights reserved.

##  NOT FOR SALE. The software is provided "as is" without any warranty.

############################################################################

def gcd(m, n):

    if n == 0: return m

    return gcd(n,m%n)

   

def floatToFraction(n, d=16):

    n = round(n*d)/d   

    f = gcd(int(n*d), d)

    return '%d/%d' % (int(n*d)/f, d/f)   

   

def stud_dim(dia):

    '''returns default head thickness, head diameter, and burn off

    dictionary data - key (diameter): (head thickness, head diameter, burn off)'''

    dd = dict(zip(['1/4', '3/8', '1/2', '5/8', '3/4', '7/8', '1'], \

                  [(0.1875, 0.5, 0.125), (0.281, 0.75, 0.125), (0.3125, 1.0, 0.125), \

                   (0.3125, 1.25, 0.1875), (0.375, 1.25, 0.1875), (0.375, 1.375, 0.1875), \

                   (0.5, 1.625, 0.1875)]))

    if isinstance(dia, str):

        return dd.get(dia, None)

 

    elif isinstance(dia, float):

        return dd.get(floatToFraction(dia), None)

 

if __name__ == '__main__':

    print stud_dim('1/2')

    print stud_dim(0.5)

    print stud_dim(0.24)

    print floatToFraction(0.24)

    print floatToFraction(0.2)

    print stud_dim(0.2)

    print stud_dim('15/16')

 

'''

>>> (0.3125, 1.0, 0.125)

(0.3125, 1.0, 0.125)

(0.1875, 0.5, 0.125)

1/4

3/16

None

None

>>>

'''