# Another version of reading point data from a file using re

import re

import datetime, time

 

data_file = r'H:\TEMP\temsys\sample_points3.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()

   

    in_pnt = False

    patt = re.compile(r'''\d+\.\d+E\+\d+|           # engineering notation ++

                          -\d+\.\d+E\+\d+|          # engineering notation -+

                          -\d+\.\d+E-\d+|           # engineering notation --

                          \d+\.\d+E-\d+|            # engineering notation +-

                          \d+\.\d+|                 # positive float format

                          -\d+\.\d+|                # negative float format

                          \d+\.|                    # positive float format

                          -\d+\.|                   # negative float format

                          \d+                       # positive integer

                          ''', re.X

                      )

   

    for line in fList:

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

 

        if in_pnt:

            pt_data += re.findall(patt, line)

            in_pnt = False

            ptDict[convert_data(pt_data[0])] = [convert_data(x) for x in pt_data[1:] if x != '0']

 

        elif 'pnt' in line.lower() and '*' in line:

            in_pnt = True

            pt_data = re.findall(patt, line)

       

        elif 'rect' in lineList or 'tria' in lineList:

            sLst = re.findall(r'\d{8}', line)

            wireDict[convert_data(sLst[0])] = [convert_data(x) for x in sLst[1:]]

           

        elif 'pnt' in lineList:

            sLst = re.findall(r'\d.{7}|(?<=\d.{7}).+', line)

            ptDict[convert_data(sLst[0])] = [convert_data(x.strip()) for x in sLst[1:]]

           

    return ptDict,wireDict

 

if __name__ == '__main__':

    ptDict,wireDict = read_file_data(data_file)

    print 'Point dictionary:'

    for key in ptDict:

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

    print '\nWire dictionary:'

    for key in wireDict:

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

 

print

print time.time()

print time.clock()

print time.ctime(time.time())

print time.strftime('%H:%M:%S', time.localtime())

 

'''

>>> Point dictionary:

30400000 = [10.0, 0.0, 0.0]

40000000 = [15.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]

1 = [0.0, 0.0, 0.0]

8 = [10.0, 5.0, 0.0]

9 = [15.0, 5.0, 0.0]

10270106 = [1097.15002, 250.67631499999999, 140.78933699999999]

10270107 = [1115.47864, 271.83373999999998, 144.698837]

10010012 = [3.0, 0.0, 0.0]

60000800 = [0.0, 5.0, 0.0]

20020003 = [5.0, 0.0, 0.0]

10260209 = [1156.2659900000001, 313.99282799999997, 155.018463]

80009000 = [10.0, 5.0, 0.0]

7 = [5.0, 5.0, 0.0]

3280311 = [1365.6743200000001, -371.22653200000002, 201.031464]

50050000 = [20.0, 0.0, 0.0]

90009000 = [15.0, 5.0, 0.0]

70000000 = [5.0, 5.0, 0.0]

3280502 = [1254.3385000000001, -142.613068, 180.20266699999999]

3280503 = [1270.5728799999999, -175.843582, 184.23608400000001]

3280504 = [1282.8614500000001, -201.004501, 187.21845999999999]

 

Wire dictionary:

10000000 = [10000000, 20000000, 70000000]

20000000 = [20000000, 30000000, 80000000]

30000000 = [30000000, 40000000, 90000000]

10000071 = [10000101, 20000022, 70000000, 60000055]

40000000 = [40000000, 50000000]

30000088 = [30000208, 40000002, 90005000, 80003000]

20000092 = [20000105, 30000004, 80004000, 71111167]

40000094 = [40000304, 50000071, 90000600]

'''

 

''' Data File:

Rect    1000007110000101200000227000000060000055

Rect    2000009220000105300000048000400071111167

Rect    3000008830000208400000029000500080003000

Tria     40000094400003045000007190000600

Pnt      100100123.      0.      0.

Pnt      200200035.      0.      0.

Pnt      3040000010.     0.      0.

Pnt      4000000015.     0.      0.

Pnt      5005000020.     0.      0.

Pnt      600008000.      5.      0.

Pnt      700000005.      5.      0.

Pnt      8000900010.     5.      0.

Pnt      9000900015.     5.      0.

Pnt      100100123.      0.      0.

Pnt      200200035.      0.      0.

Pnt      3040000010.     0.      0.

Pnt      4000000015.     0.      0.

Pnt      5005000020.     0.      0.

Pnt      600008000.      5.      0.

Pnt      700000005.      5.      0.

Pnt      8000900010.     5.      0.

Pnt      9000900015.     5.      0.

Rect    100000001000000020000000700000006

Rect    200000002000000030000000800000007

Rect    300000003000000040000000900000008

Tria    4000000040000000500000009

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.

 

Pnt    *         3280311       0          1.36567432E+03 -3.71226532E+02

*         2.01031464E+02       0

Pnt  *         3280502       0          1.25433850E+03 -1.42613068E+02

*         1.80202667E+02       0

Pnt  *         3280503       0          1.27057288E+03 -1.75843582E+02

*         1.84236084E+02       0

Pnt    *         3280504       0          1.28286145E+03 -2.01004501E+02

*         1.87218460E+02       0

 

Pnt*     10260209                       1156.26599      313.992828

*       155.018463

Pnt*     10270106                       1097.15002      250.676315

*       140.789337

Pnt*     10270107                       1115.47864      271.83374

*       144.698837

'''

 

'''

>>> 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}

>>>

'''

 

 

'''

>>> f = open(r'H:\TEMP\temsys\sample_points4.txt').readlines()

>>> in_pt = False

>>> ptDict = {}

>>> for line in f:

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

... if in_pnt:

...       pt_data += [s.strip('* ') for s in lineList[1].split() if (s != '*' and s != '0')]

...       in_pnt = False

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

... elif 'pnt' in lineList and '*' in lineList[1]:

...       in_pnt = True

...       pt_data = [lineList[0], ] + [s.strip('* ') for s in lineList[1].split() if (s != '*' and s != '0')]

...      

>>> ptDict

{3280504: [1282.8614500000001, -201.004501, 187.21845999999999], 3280503: [1270.5728799999999, -175.843582, 184.23608400000001], 3280502: [1254.3385000000001, -142.613068, 180.20266699999999], 3280311: [1365.6743200000001, -371.22653200000002, 201.031464]}

>>>

'''

'''

>>> s = 'Pnt    *         3280504       0          1.28286145E+03 -2.01004501E+02'

>>> re.findall('\d+\.\d+E\+\d+|\+\d+\.\d+E\+\d+|-\d+\.\d+E\+\d+|\d+\.\d+|\d+', s)

['3280504', '0', '1.28286145E+03', '-2.01004501E+02']

>>>

'''

"""

>>> patt = re.compile(r'''\d+\.\d+E\+\d+|

... \d+\.\d+E\+\d+|

... -\d+\.\d+E\+\d+|

... -\d+\.\d+E-\d+|

... \d+\.\d+E-\d+|

... \d+\.\d+|

... -\d+\.\d+|

... \d+''', re.X

... )

>>> patt

<_sre.SRE_Pattern object at 0x00DE68D0>

>>> s = 'Pnt    *         3280311       0          +1.36567432E+03 -3.71226532E+02'

>>> re.findall(patt,s)

['3280311', '0', '1.36567432E+03', '-3.71226532E+02']

>>> dd = {}

>>> lst = re.findall(patt,s)

>>> dd[int(lst[0])] = [float(i) for i in lst[1:] if i != '0']

>>> dd

{3280311: [1365.6743200000001, -371.22653200000002]}

>>> s1 = '*       155.018463'

>>> lst1 = re.findall(patt,s)

>>> dd[int(lst[0])] = dd[int(lst[0])]+[float(i) for i in lst1 if i != '0']

>>> dd

{3280311: [1365.6743200000001, -371.22653200000002, 155.018463]}

>>>

"""

"""

# ghostdog74

data = open('H:/TEMP/temsys/strdata.txt').read()

pat = re.compile("(\d+) THRU (\d+)",re.M|re.DOTALL)

for items in pat.findall(data):

    print items

 

'''

>>> ('880', '897')

('1001343', '1001349')

('921', '932')

('1001323', '1001331')

('1001343', '1001349')

('1001359', '1001365')

…………………………………..

"""