[python-win32] translating VB into Python...

Michael March mmarch at gmail.com
Tue May 15 20:46:14 CEST 2007


Ok..  here is the VB example .. (this is for / provided by HP's
Quality Center help file..)

Sub LinkDefects()

'This example creates two defects and links them

    Dim BugF As BugFactory

    Dim Bug1 As Bug

' tdc is a TDConnection. The user is authenticated and

' connected to the project before this routine runs.

    Set BugF = tdc.BugFactory


'Create new defects

    Set Bug1 = BugF.AddItem(Null)

    Bug1.Summary = "Lydia Bennet is 15 years old."

    Bug1.Status = "New"

    Bug1.Priority = "3-High"

    Bug1.Field("BG_SEVERITY") = "3-High"
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    Bug1.DetectedBy = c_qcUser

    Bug1.Field("BG_DETECTION_DATE") = Date
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    Bug1.Post

I'm able to translate all that code, no prob, except for the lines highlighted.

If you do:

print Bug1.Field

..you get the current value of the field.

On 5/15/07, Tim Golden <mail at timgolden.me.uk> wrote:
> Michael March wrote:
> > When you do:
> >
> >     object.Field("UserDefined_01")
> >
> > ... the output is a unicode object..  So putting:
> >
> >    object.Field("UserDefined_01").Value
> >
> > .. barfs..
> >
> > I'm obviously missing something here..
>
> Not seeing the exact code you're using, so I
> could be wrong, but I would expect the result
> of the expression: object.Field ("blah") to
> be an instance of some class x.y.Field
> (or whatever). Since you wouldn't be able to
> use such a term on the LHS of a Python name-binding
> (object.Field("UserDefined_01") = "Open") then
> the first error you noted is unsurprising.
>
> I would then not be surprised if the pywin32
> proxy for that field handled things like
> __unicode__, __str__, __repr__ in such a way
> as to return the ms-defined default property,
> typically the one called .Value which could
> well be a Unicode string.
>
> But obviously, if what you say above is strictly
> true - that object.Field ("blah").Value "barfs"
> (and I'm going to guess that this means: "raises
> an AttributeError because the builtin Unicode
> object has no .Value attribute) then what I'm
> describing above isn't happening.
>
> But am I understanding correctly? Could you provide a
> small code fragment which might help clarify things?
>
> Here's a toy example using Excel:
>
> <code>
> from win32com.client.gencache import EnsureDispatch
> xl = EnsureDispatch ("Excel.Application")
> ws = xl.Workbooks.Add ().ActiveSheet
>
> cell_11 = ws.Cells (1, 1)
> print cell_11.__class__
> # win32com...Range
>
> cell_11 = "blah"
> # has now bound cell_11 to the string "blah".
> # It works, but isn't what we want. Rebind.
>
> cell_11 = ws.Cells (1, 1)
> cell_11.Value = "blah"
> # OK
>
> print cell_11
> # "blah" because of the default property
> print cell_11.Value
> # "blah"
>
> x = cell_11
> print x.__class__
> # win32com...Range
>
> y = cell_11.Value
> print y.__class__
> # type "str"
>
> </code>
>
> TJG
>


More information about the Python-win32 mailing list