Skip to content

Commit 77fb9a5

Browse files
committedDec 29, 2013
[processing] refactored some helpre functions (contributed by Nathan Woodrow)
1 parent 6b4641d commit 77fb9a5

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed
 

‎python/plugins/processing/tools/vector.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -135,34 +135,27 @@ def spatialindex(layer):
135135

136136

137137
def createUniqueFieldName(fieldName, fieldList):
138-
shortName = fieldName[:10]
138+
def nextname(name):
139+
num = 1
140+
while True:
141+
returnname ='{name}_{num}'.format(name=name[:8], num=num)
142+
yield returnname
143+
num += 1
139144

140-
if len(fieldList) == 0:
141-
return shortName
145+
def found(name):
146+
return any(f.name() == name for f in fieldList)
142147

143-
fieldNames = [f.name() for f in fieldList]
148+
shortName = fieldName[:10]
144149

145-
if shortName not in fieldNames:
150+
if not fieldList:
146151
return shortName
147152

148-
shortName = fieldName[:8] + '_1'
149-
changed = True
150-
while changed:
151-
changed = False
152-
for n in fieldList:
153-
if n == shortName:
154-
155-
# Create unique field name
156-
num = int(shortName[-1:])
157-
if num < 9:
158-
shortName = shortName[:8] + '_' + str(num + 1)
159-
else:
160-
shortName = shortName[:7] + '_' + str(num + 1)
161-
162-
changed = True
163-
164-
return shortName
153+
if not found(shortName):
154+
return shortName
165155

156+
for newname in nextname(shortName):
157+
if not found(newname):
158+
return newname
166159

167160
def findOrCreateField(layer, fieldList, fieldName, fieldLen=24, fieldPrec=15):
168161
idx = layer.fieldNameIndex(fieldName)

0 commit comments

Comments
 (0)
Please sign in to comment.