Start a new topic

Python script question

This forum may not be appropriate - sorry.  I have some python code that works external to Creator, running in CodeSkulptor3.


I am processing a comment string, and during the integration of my code into Creator script I get an error that a) I can't see why, and b) since a, don't know how to address it.  What's my problem?


 The error is:

Run Script started <test_comment1B.py>...

('Code supplied is:', 'B')

('Value supplied is:', '1')

E: Traceback (most recent call last):

E: File "test_comment1B.py", line 129

E: TypeError: 'str' object is not callable


Line 129:

      boo = new_c_string(src_road, src_lane, dest_road, dest_lane, what_signal, hold_string, c_tail)


Function new_c_string definition starts on line 46.



# 2021.04.08 revised from comment edit script
#
# Reformat selection comment to match template isxn
# include halt line information
# use src, dest roads to determine signal name
#

db = mgGetCurrentDb()
selectList = mgGetSelectList (db)
num = mgGetRecListCount (selectList)


if (num == 0):
    mgSendMessage (MMSG_ERROR, "Nothing selected")
else:
	mgDeselectAll(db)
	for i in range (0, num):
		rec,m = mgGetNextRecInList (selectList)
		mgSelectOne(rec)
		tag = mgGetComment (rec)
		name = mgGetName (rec)
		  
        if tag:
				comment = tag.split('\n')
            # tmp_new = comment.split(' ')

            # print ('New comment? ', tmp_new)
            # new_comment = ' '.join(str(x) for x in tmp_new)

				s, inp_code = mgPromptDialogString(None, 50, "Enter ISXN code (B-G):","")
				if inp_code:
					inp_signal_code = inp_code
					print ('Code supplied is:', what_code)

					s, inp_signal_val = mgPromptDialogString(None, 50, "Enter input (1-6):","")
				if inp_signal_val:
					what_val = inp_signal_val
					print ('Value supplied is:', what_val)

				if not inp_signal_code or inp_signal_val:
					mgSendMessage (MMSG_ERROR, "Error: Missing Code or Value to process!")
					
##############################################
##############################################

				def new_c_string (src_road, src_lane, dest_road, dest_lane, what_signal, hold_string, c_tail):
    
					new_c_line = '@Corridor ' + src_road + ' ' + src_lane + ' ' + dest_road + ' ' + dest_lane + ' CrdrHold 0.00 0.00 TLIGHT CrdrHoldLine 2.00 0.00 CrdrHoldSign ' + what_signal + ' ' + " ".join([str(i) for i in c_tail])
    
					return new_c_line
			
###############################################
###############################################

				words = comment[0].split(' ')
				new_tag = []

				road1, road2, road3, road4, old_c_string, c_tail, new_c_string = '','','','','','',''

				signal_name = ''

				# hold offset stuff
				hold_string = 'CrdrHold 0.00 0.00 TLIGHT CrdrHoldLine 2.00 0.00 CrdrHoldSign '

				road4 = words[-1]
				road3 = words[-2]
				road2 = words[-3]
				road1 = words[-4]

				signal_r1 = 's_signal_south' + '_' + inp_signal_code + '_' + str(inp_signal_val)
				signal_r1_left = 's_signal_south_left' + '_' + inp_signal_code + '_' + str(inp_signal_val)

				signal_r2 = 's_signal_east' + '_' + inp_signal_code + '_' + str(inp_signal_val)
				signal_r2_left = 's_signal_east_left' + '_' + inp_signal_code + '_' + str(inp_signal_val)

				signal_r3 = 's_signal_north' + '_' + inp_signal_code + '_' + str(inp_signal_val)
				signal_r3_left = 's_signal_north_left' + '_' + inp_signal_code + '_' + str(inp_signal_val)

				signal_r4 = 's_signal_west' + '_' + inp_signal_code + '_' + str(inp_signal_val)
				signal_r4_left = 's_signal_west_left' + '_' + inp_signal_code + '_' + str(inp_signal_val)
				
				boo = ''

				# reconstruct tag

				# first line will be the same between old and new tag
				new_tag.append(comment[0])

				for x in range (1, len(comment)):
					if 'IntBorder' in comment[x]:
						new_tag.append(comment[x])
					elif 'Corridor' in comment[x]:
						#print (comment[x], type(comment[x]))
						src_road, src_lane, dest_road, dest_lane = '','','',''
        
					old_c_string = comment[x]
					c_words = comment[x].split(' ')
        
					src_road = c_words[1]
					src_lane = c_words[2]
        
					dest_road = c_words[3]
					dest_lane = c_words[4]
        
					c_tail = c_words[-3:]
        
					if (src_road == road1) and (dest_road == road4):
						what_signal = signal_r1_left
						boo = new_c_string(src_road, src_lane, dest_road, dest_lane, what_signal, hold_string, c_tail)
						new_tag.append(boo)
					elif (src_road == road1) and (dest_road == road3):
						what_signal = signal_r1
						boo = new_c_string(src_road, src_lane, dest_road, dest_lane, what_signal, hold_string, c_tail)
						new_tag.append(boo)
					elif (src_road == road1) and (dest_road == road2):
						new_tag.append(comment[x])
					elif (src_road == road2) and (dest_road == road1):
						what_signal = signal_r2_left
						boo = new_c_string(src_road, src_lane, dest_road, dest_lane, what_signal, hold_string, c_tail)
						new_tag.append(boo)
					elif (src_road == road2) and (dest_road == road4):
						what_signal = signal_r2
						boo = new_c_string(src_road, src_lane, dest_road, dest_lane, what_signal, hold_string, c_tail)
						new_tag.append(boo)
					elif (src_road == road2) and (dest_road == road3):
						new_tag.append(comment[x])
					elif (src_road == road3) and (dest_road == road2):
						what_signal = signal_r3_left
						boo = new_c_string(src_road, src_lane, dest_road, dest_lane, what_signal, hold_string, c_tail)
						new_tag.append(boo)
					elif (src_road == road3) and (dest_road == road1):
						what_signal = signal_r1
						boo = new_c_string(src_road, src_lane, dest_road, dest_lane, what_signal, hold_string, c_tail)
						new_tag.append(boo)
					elif (src_road == road3) and (dest_road == road4):
						new_tag.append(comment[x])
					elif (src_road == road1) and (dest_road == road4):
						what_signal = signal_r1_left
						boo = new_c_string(src_road, src_lane, dest_road, dest_lane, what_signal, hold_string, c_tail)
						new_tag.append(boo)
					elif (src_road == road1) and (dest_road == road3):
						what_signal = signal_r1
						boo = new_c_string(src_road, src_lane, dest_road, dest_lane, what_signal, hold_string, c_tail)
						new_tag.append(boo)
					elif (src_road == road1) and (dest_road == road2):
						new_tag.append(comment[x])
					elif (src_road == road4) and (dest_road == road3):
						what_signal = signal_r4_left
						boo = new_c_string(src_road, src_lane, dest_road, dest_lane, what_signal, hold_string, c_tail)
						new_tag.append(boo)
					elif (src_road == road4) and (dest_road == road2):
						what_signal = signal_r2
						boo = new_c_string(src_road, src_lane, dest_road, dest_lane, what_signal, hold_string, c_tail)
						new_tag.append(boo)
					elif (src_road == road4) and (dest_road == road1):
						new_tag.append(comment[x])
        

#       print (new_comment)

				ok = mgSetComment (rec, new_tag)            
 

  

Here is the CS3 code.  Note some things are hard-coded and the code is not identical to the above.


 

tag = '''@IntDef IntName intB2 IntHeight 0.00 IntRoadName CBr1 Br2 Ar1 Br1
@IntBorder 213.720002 378.959999 213.720002 281.040001 0
@IntBorder 213.720002 281.040001 268.800001 225.960002 0
@IntBorder 268.800001 225.960002 391.199999 225.960002 0
@IntBorder 391.199999 225.960002 446.279998 281.040001 0
@IntBorder 446.279998 281.040001 446.279998 378.959999 0
@IntBorder 446.279998 378.959999 391.199999 434.039998 0
@IntBorder 391.199999 434.039998 268.800001 434.039998 0
@IntBorder 268.800001 434.039998 213.720002 378.959999 0
@Corridor Ar1 5 Br2 2 CrdrCurves intB2_Ar1L5_Br2L2.corr 12.0
@Corridor Ar1 6 CBr1 6 CrdrCurves intB2_Ar1L6_CBr1L6.corr 12.0
@Corridor Ar1 7 CBr1 7 CrdrCurves intB2_Ar1L7_CBr1L7.corr 12.0
@Corridor Ar1 8 CBr1 8 CrdrCurves intB2_Ar1L8_CBr1L8.corr 12.0
@Corridor Ar1 9 Br1 7 CrdrCurves intB2_Ar1L9_Br1L7.corr 12.0
@Corridor Br1 0 CBr1 9 CrdrCurves intB2_Br1L0_CBr1L9.corr 12.0
@Corridor Br1 1 Br2 1 CrdrCurves intB2_Br1L1_Br2L1.corr 12.0
@Corridor Br1 2 Br2 2 CrdrCurves intB2_Br1L2_Br2L2.corr 12.0
@Corridor Br1 3 Ar1 3 CrdrCurves intB2_Br1L3_Ar1L3.corr 12.0
@Corridor Br2 4 CBr1 6 CrdrCurves intB2_Br2L4_CBr1L6.corr 12.0
@Corridor Br2 5 Br1 5 CrdrCurves intB2_Br2L5_Br1L5.corr 12.0
@Corridor Br2 6 Br1 6 CrdrCurves intB2_Br2L6_Br1L6.corr 12.0
@Corridor Br2 7 Ar1 0 CrdrCurves intB2_Br2L7_Ar1L0.corr 12.0
@Corridor CBr1 0 Br2 0 CrdrCurves intB2_CBr1L0_Br2L0.corr 12.0
@Corridor CBr1 1 Ar1 1 CrdrCurves intB2_CBr1L1_Ar1L1.corr 12.0
@Corridor CBr1 2 Ar1 2 CrdrCurves intB2_CBr1L2_Ar1L2.corr 12.0
@Corridor CBr1 3 Ar1 3 CrdrCurves intB2_CBr1L3_Ar1L3.corr 12.0
@Corridor CBr1 4 Br1 5 CrdrCurves intB2_CBr1L4_Br1L5.corr 12.0
'''

print (type(tag))
content = tag.split('\n')

print (type(content), len(content))

print (content[0])

# @IntDef IntName intB2 IntHeight 0.00 IntRoadName CBr1 Br2 Ar1 Br1

words = content[0].split(' ')
print (words[-4:])

#['CBr1', 'Br2', 'Ar1', 'Br1']

new_tag = []
road1, road2, road3, road4, old_c_string, c_tail, new_c_string = '','','','','','',''

signal_name = ''

# hold offset stuff
hold_string = 'CrdrHold 0.00 0.00 TLIGHT CrdrHoldLine 2.00 0.00 CrdrHoldSign '

road4 = words[-1]
road3 = words[-2]
road2 = words[-3]
road1 = words[-4]

print ('Roads:', road1, road2, road3, road4)
print ('####')

# signals

signal_count = 1
signal_ID = 'B'


signal_r1 = 's_signal_south' + '_' + signal_ID + '_' + str(signal_count)
signal_r1_left = 's_signal_south_left' + '_' + signal_ID + '_' + str(signal_count)

signal_r2 = 's_signal_east' + '_' + signal_ID + '_' + str(signal_count)
signal_r2_left = 's_signal_east_left' + '_' + signal_ID + '_' + str(signal_count)

signal_r3 = 's_signal_north' + '_' + signal_ID + '_' + str(signal_count)
signal_r3_left = 's_signal_north_left' + '_' + signal_ID + '_' + str(signal_count)

signal_r4 = 's_signal_west' + '_' + signal_ID + '_' + str(signal_count)
signal_r4_left = 's_signal_west_left' + '_' + signal_ID + '_' + str(signal_count)

def new_c_string (src_road, src_lane, dest_road, dest_lane, what_signal, hold_string, c_tail):
    
    new_c_line = '@Corridor ' + src_road + ' ' + src_lane + ' ' + dest_road + ' ' + dest_lane + ' CrdrHold 0.00 0.00 TLIGHT CrdrHoldLine 2.00 0.00 CrdrHoldSign ' + what_signal + ' ' + " ".join([str(i) for i in c_tail])
    
    return new_c_line


# reconstruct tag

# first line will be the same between old and new tag
new_tag.append(content[0])

for x in range (1, len(content)):
    if 'IntBorder' in content[x]:
        new_tag.append(content[x])
    elif 'Corridor' in content[x]:
        #print (content[x], type(content[x]))
        src_road, src_lane, dest_road, dest_lane = '','','',''
        
        old_c_string = content[x]
        c_words = content[x].split(' ')
        
        src_road = c_words[1]
        src_lane = c_words[2]
        
        dest_road = c_words[3]
        dest_lane = c_words[4]
        
        c_tail = c_words[-3:]
        
        if (src_road == road1) and (dest_road == road4):
            what_signal = signal_r1_left
            boo = new_c_string(src_road, src_lane, dest_road, dest_lane, what_signal, hold_string, c_tail)
            new_tag.append(boo)
        elif (src_road == road1) and (dest_road == road3):
            what_signal = signal_r1
            boo = new_c_string(src_road, src_lane, dest_road, dest_lane, what_signal, hold_string, c_tail)
            new_tag.append(boo)
        elif (src_road == road1) and (dest_road == road2):
            new_tag.append(content[x])
        elif (src_road == road2) and (dest_road == road1):
            what_signal = signal_r2_left
            boo = new_c_string(src_road, src_lane, dest_road, dest_lane, what_signal, hold_string, c_tail)
            new_tag.append(boo)
        elif (src_road == road2) and (dest_road == road4):
            what_signal = signal_r2
            boo = new_c_string(src_road, src_lane, dest_road, dest_lane, what_signal, hold_string, c_tail)
            new_tag.append(boo)
        elif (src_road == road2) and (dest_road == road3):
            new_tag.append(content[x])
        elif (src_road == road3) and (dest_road == road2):
            what_signal = signal_r3_left
            boo = new_c_string(src_road, src_lane, dest_road, dest_lane, what_signal, hold_string, c_tail)
            new_tag.append(boo)
        elif (src_road == road3) and (dest_road == road1):
            what_signal = signal_r1
            boo = new_c_string(src_road, src_lane, dest_road, dest_lane, what_signal, hold_string, c_tail)
            new_tag.append(boo)
        elif (src_road == road3) and (dest_road == road4):
            new_tag.append(content[x])
        elif (src_road == road1) and (dest_road == road4):
            what_signal = signal_r1_left
            boo = new_c_string(src_road, src_lane, dest_road, dest_lane, what_signal, hold_string, c_tail)
            new_tag.append(boo)
        elif (src_road == road1) and (dest_road == road3):
            what_signal = signal_r1
            boo = new_c_string(src_road, src_lane, dest_road, dest_lane, what_signal, hold_string, c_tail)
            new_tag.append(boo)
        elif (src_road == road1) and (dest_road == road2):
            new_tag.append(content[x])
        elif (src_road == road4) and (dest_road == road3):
            what_signal = signal_r4_left
            boo = new_c_string(src_road, src_lane, dest_road, dest_lane, what_signal, hold_string, c_tail)
            new_tag.append(boo)
        elif (src_road == road4) and (dest_road == road2):
            what_signal = signal_r2
            boo = new_c_string(src_road, src_lane, dest_road, dest_lane, what_signal, hold_string, c_tail)
            new_tag.append(boo)
        elif (src_road == road4) and (dest_road == road1):
            new_tag.append(content[x])
        
        
        # c_tail = c_words[-3:]
        
        # print ('what is all this?',*c_tail, sep = " ")
        #what is all this? CrdrCurves intB2_CBr1L4_Br1L5.corr 12.0
        
        

print ('** New tag:')        
for x in new_tag:
    print (x)

 


Here is the result from CS3, which I think is what I am looking for (debugging lines @top):


<class 'str'>
<class 'list'> 28
@IntDef IntName intB2 IntHeight 0.00 IntRoadName CBr1 Br2 Ar1 Br1
['CBr1', 'Br2', 'Ar1', 'Br1']
Roads: CBr1 Br2 Ar1 Br1
####
** New tag:
@IntDef IntName intB2 IntHeight 0.00 IntRoadName CBr1 Br2 Ar1 Br1
@IntBorder 213.720002 378.959999 213.720002 281.040001 0
@IntBorder 213.720002 281.040001 268.800001 225.960002 0
@IntBorder 268.800001 225.960002 391.199999 225.960002 0
@IntBorder 391.199999 225.960002 446.279998 281.040001 0
@IntBorder 446.279998 281.040001 446.279998 378.959999 0
@IntBorder 446.279998 378.959999 391.199999 434.039998 0
@IntBorder 391.199999 434.039998 268.800001 434.039998 0
@IntBorder 268.800001 434.039998 213.720002 378.959999 0
@Corridor Ar1 5 Br2 2 CrdrHold 0.00 0.00 TLIGHT CrdrHoldLine 2.00 0.00 CrdrHoldSign s_signal_north_left_B_1 CrdrCurves intB2_Ar1L5_Br2L2.corr 12.0
@Corridor Ar1 6 CBr1 6 CrdrHold 0.00 0.00 TLIGHT CrdrHoldLine 2.00 0.00 CrdrHoldSign s_signal_south_B_1 CrdrCurves intB2_Ar1L6_CBr1L6.corr 12.0
@Corridor Ar1 7 CBr1 7 CrdrHold 0.00 0.00 TLIGHT CrdrHoldLine 2.00 0.00 CrdrHoldSign s_signal_south_B_1 CrdrCurves intB2_Ar1L7_CBr1L7.corr 12.0
@Corridor Ar1 8 CBr1 8 CrdrHold 0.00 0.00 TLIGHT CrdrHoldLine 2.00 0.00 CrdrHoldSign s_signal_south_B_1 CrdrCurves intB2_Ar1L8_CBr1L8.corr 12.0
@Corridor Ar1 9 Br1 7 CrdrCurves intB2_Ar1L9_Br1L7.corr 12.0
@Corridor Br1 0 CBr1 9 CrdrCurves intB2_Br1L0_CBr1L9.corr 12.0
@Corridor Br1 1 Br2 1 CrdrHold 0.00 0.00 TLIGHT CrdrHoldLine 2.00 0.00 CrdrHoldSign s_signal_east_B_1 CrdrCurves intB2_Br1L1_Br2L1.corr 12.0
@Corridor Br1 2 Br2 2 CrdrHold 0.00 0.00 TLIGHT CrdrHoldLine 2.00 0.00 CrdrHoldSign s_signal_east_B_1 CrdrCurves intB2_Br1L2_Br2L2.corr 12.0
@Corridor Br1 3 Ar1 3 CrdrHold 0.00 0.00 TLIGHT CrdrHoldLine 2.00 0.00 CrdrHoldSign s_signal_west_left_B_1 CrdrCurves intB2_Br1L3_Ar1L3.corr 12.0
@Corridor Br2 4 CBr1 6 CrdrHold 0.00 0.00 TLIGHT CrdrHoldLine 2.00 0.00 CrdrHoldSign s_signal_east_left_B_1 CrdrCurves intB2_Br2L4_CBr1L6.corr 12.0
@Corridor Br2 5 Br1 5 CrdrHold 0.00 0.00 TLIGHT CrdrHoldLine 2.00 0.00 CrdrHoldSign s_signal_east_B_1 CrdrCurves intB2_Br2L5_Br1L5.corr 12.0
@Corridor Br2 6 Br1 6 CrdrHold 0.00 0.00 TLIGHT CrdrHoldLine 2.00 0.00 CrdrHoldSign s_signal_east_B_1 CrdrCurves intB2_Br2L6_Br1L6.corr 12.0
@Corridor Br2 7 Ar1 0 CrdrCurves intB2_Br2L7_Ar1L0.corr 12.0
@Corridor CBr1 0 Br2 0 CrdrCurves intB2_CBr1L0_Br2L0.corr 12.0
@Corridor CBr1 1 Ar1 1 CrdrHold 0.00 0.00 TLIGHT CrdrHoldLine 2.00 0.00 CrdrHoldSign s_signal_south_B_1 CrdrCurves intB2_CBr1L1_Ar1L1.corr 12.0
@Corridor CBr1 2 Ar1 2 CrdrHold 0.00 0.00 TLIGHT CrdrHoldLine 2.00 0.00 CrdrHoldSign s_signal_south_B_1 CrdrCurves intB2_CBr1L2_Ar1L2.corr 12.0
@Corridor CBr1 3 Ar1 3 CrdrHold 0.00 0.00 TLIGHT CrdrHoldLine 2.00 0.00 CrdrHoldSign s_signal_south_B_1 CrdrCurves intB2_CBr1L3_Ar1L3.corr 12.0
@Corridor CBr1 4 Br1 5 CrdrHold 0.00 0.00 TLIGHT CrdrHoldLine 2.00 0.00 CrdrHoldSign s_signal_south_left_B_1 CrdrCurves intB2_CBr1L4_Br1L5.corr 12.0

Thanks for checking it over.


1 Comment

Yeah, found a couple bugs in the 'working ' code.  Just wanted to follow up and say I have a solution that works outside Creator, so I have a way forward.


Cheers!

Login to post a comment