[IronPython] Using SetPropertyValue and Put()

Michael ironpython at klintoe.eu
Fri Apr 9 11:45:52 CEST 2010


The type returned from CreateWithNodeType is a 'ManagementBaseObject'
which again is a Instance of the newly created OV_ManagedNode.

Well nothing really happens as the Put method is not available, and that
sort of my problem.

Actually yes, critique is very welcome, as this is my only way to know
what I'm doing wrong, and where I can improve my noob python skills 

1) I'm trying to make the WMI Methods provided by OpenView available
through Python, so my code snippet is just one of the methods (the one I'm
having trouble with).

The documentation says the following on the OV_ManagedNode Class.

class OV_ManagedNode 
{ 
Properties: 
    Some Properties...

Class Methods: 
    OV_ManagedNode CreateWithNodeType( 
    [in] string PrimaryNodeName, 
    [in] uint16 SystemTypeId, 
    [in] uint16 OsTypeId, 
    [in] uint32 OsVersionId, 
    ...
    ...
    ...
    ); 

    Some other Methods...

Instance Methods: 
    sint32 GetParents( 
    [out] OV_NodeGroup NodeGroups[], 
    [in, optional] boolean IncludeAllHierarchicalParents 
    ); 

That's why I'm creating a Class.

2) Just to be sure, self.PrimaryNodeName should be changed to
cls.PrimaryNodeName to follow the convention?
3) As soon as the method is executed the only thing I need is the returned
OV_ManagedNode instance, so I think this is okay,
as the method can only be called once with the given parameters.
4) That's a simple on, I'll change this at once.

Regards

/Michael

On Thu, 8 Apr 2010 19:22:57 -0700, Curt Hagenlocher <curt at hagenlocher.org>
wrote:
> 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
> b.Put()?
> 
> 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
>>



More information about the Ironpython-users mailing list