so such a problem, that i can’t figure it out. Maybe i have too little Python knowledge.
The problem is, that after this function runs once smoothly, another time i get error in another function.
Function, after which things break:
def setFixedPriority( self, priority, lister ):
step = priority / lister . __len__ ( )
for j in range( 0, lister . __len__( ) ):
for i in range( 0, self . listOfJobs . __len__ ( ) ) :
if self . listOfJobs[ i ] . category == lister[ j ]:
self . listOfJobs[ i ] . priority += priority
elif self . listOfJobs[ i ] . jobType == lister[ j ]:
self . listOfJobs[ i ] . priority += priority
elif self . listOfJobs[ i ] . timeToDo == lister[ j ]:
self . listOfJobs[ i ] . priority += priority
priority -= step
self . sortByPriority( )
Function in which appears the problem:
def sortByPriority( self ) :
tmp = range ( 1, self . listOfJobs . __len__ ( ) + 1 )
for i in reversed ( tmp ) :
for j in range ( 1, i ) :
if self . listOfJobs [ j - 1 ] . priority < self . listOfJobs [ j ] . priority :
self . listOfJobs [ j - 1 ],
self . listOfJobs [ j ] = self . listOfJobst [ j ],
self . listOfJobs [ j - 1 ]
Calling the function (From different python script/file/class):
self . jobs . setFixedPriority( int( self . settings[ 'Spinbox1' ] ), self . settings[ 'type' ] . split( ":" ) )
And the error i get:
File "data/ToDoListClass.py", line 82, in sortByPriority
self . listOfJobs [ j ] = self . listOfJobst [ j ],
AttributeError: jobList instance has no attribute 'listOfJobst'
I know that the sortByPriority works OK, because i am calling this once before setFixedPriority and it does not give me errors.
What could cause this to happen?
If you look closely, you have a typo there. The attribute is called
listOfJobswithout a trailingt.Note that while this will make go of the error, this will probably not fix the function:
That construct is probably supposed to swap
listOfJobs[j]andlistOfJobs[j - 1]. Due to the line breaks, this will do the following though:listOfJobs[j - 1](nothing else happens with that)listOfJobs[j]tolistOfJobs[j].listOfJobs[j - 1](again, nothing else happens).What you want to do is to write it either in one line:
Or if you want to keep line breaks, use Python’s
\syntax to make the line continue:Although I would argue if that makes it really clear what happens.
Finally, you can clean your code up a lot. You can directly iterate over lists, and you can also check multiple conditions in a single if. And lastly, Python’s sort function allows you to specify a custom comparison function so that you don’t need to implement an own sorting algorithm but can use Python’s implementation. All in all, you could for example end up with just something like this: