How to code this differently


if hasattr (FDM, 'int0') == False:
    FDM.int0=1

So, how to code above so, it wont give deprecated message?

Thanks

if 'int0' not in FDM :
   FDM['int0']=1

couldn’t have said it better myself :wink:

I red somewhere about python that one of the concept was something like
‘it’s faster to do an error than trying to avoid it.’

so maybe :

try : test=FDM['int0']
except : FDM['int0']=1

would be more pythonic I don’t know.
anyway a quick benchmark I did said it’s 6 times slower than a if ‘string’ in list test.

I mean, you’ll gain time 0.17 secs on a 3Ghz monocore if you’ve an object with 10 000 properties :slight_smile:

EDIT : it’s 2 times faster max, not 6 !

What exactly did you use to benchmark these tests?

edit : there was an error, corrected now, and a better benchmark is available below this one
.

import GameLogic as g
import time

sc = g.getCurrentScene()
co = g.getCurrentController()
ow = co.owner
o2 = sc.objects['OBCube']

if ow['init'] :
	ow['init'] = False
	for p in range(0,10000) :
		ow[str(p)]=p

print
count=0
s=time.time()
for p in range(10000,20000) :
	try : test=ow[str(p)]
	except :
		o2[str(p)] = p
		count +=1
print time.time() - s, count
count=0
s=time.time()
for p in range(20000,30000) :
	if str(p) not in ow :
		o2[str(p)] = p
		count +=1
print time.time() - s, count

it’s a good thing you ask :
I did not save the bench, but while quickly answering you, I notice an error with the first test I made :no::o : I wrote ‘if p not in’ and test=own[p] rather than str§ so the test result is completely different now. ‘for string in list’ is still faster but only 2 times max on my machine.

quick explanation :
I use 2 objets. one has this script and an init boolean property set to True.
the other is the default cube ‘Cube’
it creates 10000 props at init,

then it test 10000 times wether a prop exists (and it does not exist). so it writes a new prop in the Cube object

same thing for the second test.

I suppose it’s ok… if not please let me know

This is an interesting test. I did a similar benchmark but also testing with has_key(key).
This are my results:

when a property exist:
try…except is the fastest,
in is 40-50% slower,
has_key() is 50-60% slower

when the property does not exist:
in is the fastest
has_key() is 150-200% slower on small lists 10%-50% slower on large lists (>2000 entries)
try…except is 500%-1200% slower on small lists 500-600% on large lists (>2000 entries)

My conclusion:
use try…except if the property ususally exists
use in if the property usually dos not exist
no need for has_key()

it’s a shame. there’s still an error on my benchmark.
and I’ve found a new way to crash blender.
and maybe something usefull I didn’t know…

I wrote :

if ow['init'] :
	ow['init'] = False
	for p in range(0,10000) :
		ow[p]=p

it creates props for sure, but the key is an integer and not a string. so ow[‘123’] does not exist, but ow[123] does. a getPropertyNames give me a big list of integer, not of strings. so one can override the gui ‘limitation’ and have key of different type…

…and game object works either as key or value :

it means you can store a game object in a property value, and even as a key itself, with another value attached.

you can retrieve any object from a script with a ob2=ob1[‘propname’] for example.


import GameLogic as g
import time

sc = g.getCurrentScene()
co = g.getCurrentController()
ow = co.owner

print 
if ow['init'] :
	o2 = sc.objects['OBCube']
<b>	ow['ob']=o2
	ow[o2]=True</b>
	ow['init'] = False
else :
	
	print <b>ow['ob'].worldPosition</b>
	
	for k in ow.getPropertyNames() :
		#print k,ow[k], type(ow[k])
		if type(k) &lt;&gt; str :
			print <b>k.worldOrientation</b>

also I correctd my benchmark.
note that if you itterate a huge number of properties blender takes a lot of time to restore the properties as they were before to run. (and I crash at 20000 props)
here’s a benchmark blend that won’t crash. (temp props are deleted in game, so blender is happy)
http://jerome.le.chat.free.fr/3d/files/benchmarkGameEngine.blend
SPACE to quit, NOT ESC !!!
iteration number is 100000 (nb of props created). modify the ‘it’ property. yes, it’s a string.

@monster :
in the python doc I read :
“has_key() is deprecated in favor of key in d.”
http://docs.python.org/library/stdtypes.html
but I don’t think it’s related. I suppose props look like dict but is not. for example, I don’t manage to make setdefault work :
http://docs.python.org/library/stdtypes.html#dict.setdefault

I added the has_key test in the benchmark. my results differ from yours, but maybe I’m wrong (considering this thread, there’s chances…) :
if string in is always faster for me.

littleneo,
I haven’t used your code, but the relative processing time should be similar. There are some tolerances when running the tests multiple times. The overall result is the same (in is better for missing properties;try…except is better for existing properties).
By the way, my bge was hanging when adding 10000 properties. So I added just 2000.
I haven’t known that has_key is deprecated. As it is slower than the other opportunities I will change anyway.

Thanks for the info

I haven’t known that has_key is deprecated. As it is slower than the other opportunities I will change anyway.

the info I ave about tjis is not revelant, the function is still described in the BGE API, I guess it’s not the same, not sure that properties are really python dict…

maybe you could try my file ? I checked it again, and in is always faster… also the test will work with > 10000 prop (100 000 in the blend)

Thank you for everybody’s input. Very interesting and educating.

You guys certainly seem to know what your on about :eyebrowlift2:

Ive got a few deprecated code warnings, I’m trying to look around for a site or something which will tell me how to correct these (as in the correct syntax for the new code)
Is there such a resource around? I dont know how you guys just come up with the answer so easily, maybe my knowledge of python just isnt good enough yet.

AD-Edge:
you could do a new post and ask for the deprecation info you need.

most of the time a quick

print dir(object)
can tell you which property you have to use instead of the deprecated method

i am not aware of any site that lists the deprecation info, but i could solve nearly all of the warnings i got using the python consoles info.

greetings
manarius

Thanks manarius
I just noticed then that the console gives the solution (although it can still be difficult to interpret how to change it) But Ive managed to fix all my deprecation warnings now. :slight_smile:

@AD-Edge :
you MUST visit the Clark site for the 2.49b api :slight_smile:
http://www.tutorialsforblender3d.com/GameDoc/index_GameDoc.html
(and also check the sticky posts)

thanks littleneo, a fresh bookmark was added to my browser :smiley:

but since the documentation inside blender is very good, i guess i will use it seldom :slight_smile:

print dir() rules :ba:

greetings
manarius