ah, ok, I can see your code.
Unfortunately it is pretty complex and seem to serve a lot of different purposes.
Some remarks:
def main():
main() is unnecessary and adds no value (not even a meaningful name). Even worse it forces the reader to scroll to the end of the file just to see that you have to go to top again. This is no crime thriller where I want to see the murderer beforehand. -> remove it
if 'CompList' in own:
Why do you check this? Isn’t your code expecting it to be there? -> unnecessary check = remove it
if type(own['CompList']) is not str:
Why do you expect this property to be a string? Either you want it to be a string or something else. Does the content change often? Why you would change the type? If you want a string value use a different property name.
A frequent check for it’s type should only be necessary when the content changed. But there is no reason to write unexpected values into this property (non-string). If something is doing that - that change is your problem not the validation -> remove the check.
for component in own['CompList']:
lopping over a list … no big deal. Due to the immense size of the following code block I suggest to place it into one or more functions that deals with a single component at the time.
if 'health' in component:
Again, why do you even have a component that does not has this property? Your code does not do anything with it = wasted processing time -> remove this check.
if component.parent:
Why do you expect a component without parent? Your code does not do anything with it = wasted processing time -> remove this check.
for slots in component.parent['Jacks2']:
if slots==component:
This looks like a linear search to me. Better use the Python function list.index(x). It is much more efficient than your code.
-> it might be worth to simply remove the object from the list, rather than to rely on the index. But I do not know if this is information that you need at another place.
if type(component['child']) is not str:
Why do you expect this property to be a string? Either you want it to be a string or something else. Does the content change often? Why you would change the type? If you want a string value use a different property name.
component['child']
Dictionary search is pretty fast. But you have the result already. Why do you do a dictionary search 6 times in a row. If you found a result in the first place, you will find the same result at the sixth time too -> write the first result into a variable and use the variable. Variable access is much faster than dictionary search.
component['child']['Owner']="Empty"
component['child']="NotSet"
There is a nifty little value called None. Python very good in identifying this value. It is much faster than comparing strings. --> replace “Empty” and “NotSet” with constant None as that is what this constant expresses (other languages call that nil, null etc.).
40. if 'CompList' in own:
Beside the statement above … why do you check it again? unnecessary -> remove the check
45. if component['Type']=="Antigrav":
...
52. if component['Type']=='Lifter':
You already know it is an “Antigrav”. No need to check for “Lifter” again -> replace “if” with “elif” at the subsequent checks.
This is enough for now. Even with this few changes your code should become smaller and more efficient.
In general I suggest to split the code. As it is right now - nobody can understand what it is doing without scrolling around and around and around -> who has time to do that. Finally there is not really much that tells what it is supposed to do (I can’t read your mind ;)).
see this:
if component['Type']=="Antigrav":
processAntigrav(component)
elif component['Type']=="Lifter":
processLifter(component)
if component['Type']=='Holder':
processHolder(component)
if if component['Type']=='Head':
processHead(component)
Then you can focus on the single component.
This could be even more abstracted by using of classes. But this is a different topic.