[IronPython] Using SetPropertyValue and Put()

Curt Hagenlocher curt at hagenlocher.org
Fri Apr 9 04:22:57 CEST 2010

I don't really know anything about WMI -- what's the type of the object
returned from the CreateWithNodeType method? What happens when you call

As for the code, are you asking for a critique of the Python style? I hope
so, because I'm about to provide one! :) As a long-time Python user, here's
what I find unsettling:

1) You've defined a class, but aren't really making use of its "class"-ness.
As it currently works, you could simply replace the single method of the
class with a standalone function.
2) CreateWithNodeType is a class method. This means that its first argument
is a type object and not an instance object. Naming the parameter "self"
confuses me! It's more traditional to name the class parameter "cls". Like
"self", this is only a convention and not a rule, but it's one I think most
Python programmers would expect.
3) In CreateWithNodeType, you're setting a number of properties on the type
object. These will simply be overwritten on the next call to the method, and
they're not being used once the method exits. Slightly more worrying, it
makes the code non-thread-safe -- if two threads start executing this method
at the same time, they may interfere with each other. (This is simply a
value judgement on my part; in 2010, I don't believe in writing code that's
needlessly thread-unsafe even if I don't ever expect it to be used in a
multithreaded fashion.)
4) It's more traditional to return "None" instead of "False" to indicate "no
object" as a return value.
Having said that, there don't appear to be any problems that would prevent
the code from working correctly -- these are really just stylistic quibbles.
On Thu, Apr 8, 2010 at 2:56 PM, Michael <ironpython at klintoe.eu> wrote:

> Hi
> I'm trying to set some properties on an instance in WMI. I can set the
> property, but I can't figure out to call the Put() method, so that the
> properties are stored.
> As I have only looked at IronPython for a couple of day I would also like
> if someone can comment on potential issues with  the code.
> Regards
> /Michael
> # Code snippet Begin
> class OV_ManagedNode:
>    def __init__(self):
>        import clr
>        clr.AddReference('System.Management')
>        global System
>        global Management
>        import System.Management
>    @classmethod
>    def CreateWithNodeType(self,\
>                               PrimaryNodeName,\
>                               SystemTypeId=11,\
>                               OsTypeId=18,\
>                               OsVersionId=18052,\
>                               HeartBeatMode=0):
>        try:
>            self.PrimaryNodeName = PrimaryNodeName
>            self.SystemTypeId = SystemTypeId
>            self.OsTypeId = OsTypeId
>            self.OsVersionId = OsVersionId
>            self.HeartBeatMode = HeartBeatMode
>            self.OV_ManagedNode =
> System.Management.ManagementClass('root\HewlettPackard\OpenView\data:OV_ManagedNode')
>            self.inParams =
> self.OV_ManagedNode.GetMethodParameters('CreateWithNodeType')
>            self.inParams['PrimaryNodeName'] = self.PrimaryNodeName
>            self.inParams['SystemTypeId'] = self.SystemTypeId
>            self.inParams['OsTypeId'] = self.OsTypeId
>            self.inParams['OsVersionId'] = self.OsVersionId
>            self.inParams['HeartBeatMode'] = self.HeartBeatMode
>            self.outParams =
> self.OV_ManagedNode.InvokeMethod('CreateWithNodeType', self.inParams, None)
>            #Return Instance of the newly created OV_ManagedNode
>            return self.outParams['ReturnValue']
>        except:
>            return False
> a = OV_ManagedNode()
> b = a.CreateWithNodeType('test')
> b.SetPropertyValue('AllowCertAutoGranting', True)
> # Code snippet End
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20100408/a0cd3197/attachment.html>

More information about the Ironpython-users mailing list