Back to SDS/2 Parametric Scripts

 

data_file = r'H:\TEMP\temsys\sample_points.txt'

 

def convert_data(s):

    for func in (int, float):

        try:

            n = func(s)

            return n

        except:

            pass

    return s

 

def read_file_data(f):

    ptDict = {}

    wireDict = {}

    fList = open(f).readlines()

    for line in fList:

        lineList = [x.lower() for x in line.strip().split(' ') if x != '']

        if 'rect' in lineList or 'tria' in lineList:

            wireDict[convert_data(lineList[1])] = [convert_data(x) for x in lineList[2:]]

        elif 'pnt' in lineList:

            ptDict[convert_data(lineList[1])] = [convert_data(x) for x in lineList[2:]]

    return ptDict,wireDict

 

if __name__ == '__main__':

    ptDict,wireDict = read_file_data(data_file)

    print 'Point dictionary:'

    for key in ptDict:

        print '%d = %s' % (key, ptDict[key])

    print '\nWire dictionary:'

    for key in wireDict:

        print '%d = %s' % (key, wireDict[key])

 

'''

>>> Point dictionary:

1 = [0.0, 0.0, 0.0]

2 = [5.0, 0.0, 0.0]

3 = [10.0, 0.0, 0.0]

4 = [15.0, 0.0, 0.0]

5 = [20.0, 0.0, 0.0]

6 = [0.0, 5.0, 0.0]

7 = [5.0, 5.0, 0.0]

8 = [10.0, 5.0, 0.0]

9 = [15.0, 5.0, 0.0]

 

Wire dictionary:

1 = [1, 2, 7, 6]

2 = [2, 3, 8, 7]

3 = [3, 4, 9, 8]

4 = [4, 5, 9]

>>>

'''

 

''' Data File:

Rect    1       1       2       7       6

Rect    2       2       3       8       7

Rect    3       3       4       9       8

Tria    4       4       5       9

Pnt     1       0.      0.      0.

Pnt     2       5.      0.      0.

Pnt     3       10.     0.      0.

Pnt     4       15.     0.      0.

Pnt     5       20.     0.      0.

Pnt     6       0.      5.      0.

Pnt     7       5.      5.      0.

Pnt     8       10.     5.      0.

Pnt     9       15.     5.      0.

'''

'''

>>> d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}

>>> dict(zip([str(x) for x in d.values()], d.keys()))

{'[0, 1, 1]': 202, '[1, 0, 0]': 100, '[0, 1, 0]': 102}

>>>

'''

'''

>>> d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}

>>> dict(zip([tuple(x) for x in d.values()], d.keys()))

{(0, 1, 0): 102, (0, 1, 1): 202, (1, 0, 0): 100}

>>>

'''

'''

>>> d = {100: [1,0,0],102: [0,1,0],202: [0,1,1]}

>>> di = dict((tuple(v), k) for k, v in d.items())

>>> di

{(1, 0, 0): 100, (0, 1, 1): 202, (0, 1, 0): 102}

>>>

'''

'''

>>> line = 'Rect    1000000010000000200000007000000060000000'

>>> import re

>>> lineList = [x.lower() for x in re.split('[ 0]', s.strip()) if x != '']

>>> lineList

['rect', '1', '1', '2', '7', '6']

>>>

'''