By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,131 Members | 1,728 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,131 IT Pros & Developers. It's quick & easy.

Passing an object through COM which acts like str but isn't

P: n/a
Forgive me if I mangle any terminology here, but please correct me if
I do...

I have an object which acts exactly like a string as long as I stay in
Python land. However, I am using the object in Softimage|XSI a 3D
application on Windows. It was created while (briefly) owned by
Microsoft, so knowledge of COM with excel or anything else should be
applicable I should think. I should also say I am a COM novice and
still learning Python (there are few that aren't learning though I
suppose).

Here is an example:

class Name(object):
def __init__(self, s): self.__data = s
def __repr__(self): return repr(self.__data)
def __cmp__(self, string): return cmp(self.__data, string)
def __contains__(self, char): return char in self.__data

__data = "Test"
__doc = "Test"
def __Set(self, value):
self.__data = value

def __Get(self):
return self.__data

data = property(fget = __Get,
fset = __Set,
fdel = None,
doc = "string-like example")
It also uses some new-style class Property

Aug 15 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
The previous message was posted prematurely. Please ignore it. (I hit
enter witht he wrong focus I guess...no confirmation or edit
available? This was my first post.)

- Rafe

Aug 15 '08 #2

P: n/a
Forgive me if I mangle any terminology here, but please correct me if
I do...

I have an object which acts exactly like a string as long as I stay in
Python land. However, I am using the object in Softimage|XSI, a 3D
application on Windows. I'm getting variant erros when trying to use
my instances as I would a string.

XSI was created while (briefly) owned by Microsoft, so knowledge of
COM with excel, or anything else, should be applicable I should think.
I should also say I am a COM novice and still learning the depths of
Python.

Here is an example...

class StrLike(object):
def __init__(self, s): self.__data = s
def __repr__(self): return repr(self.__data)
def __cmp__(self, string): return cmp(self.__data, string)
def __contains__(self, char): return char in self.__data

__data = ""
def __Set(self, value): self.__data = value
def __Get(self): return self.__data
data = property(fget = __Get,
fset = __Set,
fdel = None,
doc = "string-like example")

>>>s = StrLike("test")
s
'test'
>>>if s == "test": print "cmp works"
cmp works
Now if I try to pass this as I would a string, roughly like so...
>>>s = StrLike("test")
Application.AnObject.attribute = "test" # works fine
Application.AnObject.attribute = s
ERROR : Traceback (most recent call last):
File "<Script Block >", line 18, in <module>
XSI.Selection[0].name = s
File "C:\Python25\Lib\site-packages\win32com\client\dynamic.py",
line 544, in __setattr__
self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
TypeError: Objects of type 'StrLike' can not be converted to a COM
VARIANT

Inheriting the str type doesn't raise any errors, but it's immutible
so it won't work. The attribute I am trying to set in XSI only takes a
string. So is it possible to make a string like object work like a
string in this scenario? Is there some built-in method I am missing or
some win32com.client trick? Help?
Thanks for reading,

- Rafe
Aug 15 '08 #3

P: n/a
Rafe schrieb:
Forgive me if I mangle any terminology here, but please correct me if
I do...

I have an object which acts exactly like a string as long as I stay in
Python land. However, I am using the object in Softimage|XSI, a 3D
application on Windows. I'm getting variant erros when trying to use
my instances as I would a string.

XSI was created while (briefly) owned by Microsoft, so knowledge of
COM with excel, or anything else, should be applicable I should think.
I should also say I am a COM novice and still learning the depths of
Python.

Here is an example...

class StrLike(object):
def __init__(self, s): self.__data = s
def __repr__(self): return repr(self.__data)
def __cmp__(self, string): return cmp(self.__data, string)
def __contains__(self, char): return char in self.__data

__data = ""
def __Set(self, value): self.__data = value
def __Get(self): return self.__data
data = property(fget = __Get,
fset = __Set,
fdel = None,
doc = "string-like example")

>>>s = StrLike("test")
s
'test'
>>>if s == "test": print "cmp works"
cmp works
Now if I try to pass this as I would a string, roughly like so...
>>>s = StrLike("test")
Application.AnObject.attribute = "test" # works fine
Application.AnObject.attribute = s
ERROR : Traceback (most recent call last):
File "<Script Block >", line 18, in <module>
XSI.Selection[0].name = s
File "C:\Python25\Lib\site-packages\win32com\client\dynamic.py",
line 544, in __setattr__
self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
TypeError: Objects of type 'StrLike' can not be converted to a COM
VARIANT

Inheriting the str type doesn't raise any errors, but it's immutible
so it won't work. The attribute I am trying to set in XSI only takes a
string. So is it possible to make a string like object work like a
string in this scenario? Is there some built-in method I am missing or
some win32com.client trick? Help?
Thanks for reading,

- Rafe
Add
def __str__(self): return repr(self.__data)

then
>>Application.AnObject.attribute = str(s)
should work

untested

Best Regards

Wolfgang
Aug 15 '08 #4

P: n/a
On Aug 15, 10:27 pm, Wolfgang Grafen <wolfgang.gra...@ericsson.com>
wrote:
Rafe schrieb:
Forgive me if I mangle any terminology here, but please correct me if
I do...
I have an object which acts exactly like a string as long as I stay in
Python land. However, I am using the object in Softimage|XSI, a 3D
application on Windows. I'm getting variant erros when trying to use
my instances as I would a string.
XSI was created while (briefly) owned by Microsoft, so knowledge of
COM with excel, or anything else, should be applicable I should think.
I should also say I am a COM novice and still learning the depths of
Python.
Here is an example...
class StrLike(object):
def __init__(self, s): self.__data = s
def __repr__(self): return repr(self.__data)
def __cmp__(self, string): return cmp(self.__data, string)
def __contains__(self, char): return char in self.__data
__data = ""
def __Set(self, value): self.__data = value
def __Get(self): return self.__data
data = property(fget = __Get,
fset = __Set,
fdel = None,
doc = "string-like example")
>>s = StrLike("test")
s
'test'
>>if s == "test": print "cmp works"
cmp works
Now if I try to pass this as I would a string, roughly like so...
>>s = StrLike("test")
Application.AnObject.attribute = "test" # works fine
Application.AnObject.attribute = s
ERROR : Traceback (most recent call last):
File "<Script Block >", line 18, in <module>
XSI.Selection[0].name = s
File "C:\Python25\Lib\site-packages\win32com\client\dynamic.py",
line 544, in __setattr__
self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
TypeError: Objects of type 'StrLike' can not be converted to a COM
VARIANT
Inheriting the str type doesn't raise any errors, but it's immutible
so it won't work. The attribute I am trying to set in XSI only takes a
string. So is it possible to make a string like object work like a
string in this scenario? Is there some built-in method I am missing or
some win32com.client trick? Help?
Thanks for reading,
- Rafe

Add
def __str__(self): return repr(self.__data)

then
>>Application.AnObject.attribute = str(s)
should work

untested

Best Regards

Wolfgang
Thanks for the reply.

I don't need __str__ because Python will automatically use __repr__
when __str__ isn't defined anyway. I also don't want people to have to
wrap this in str().

While str() does work, the real problem is usability. The user will
have to try it, decrypt the "can not be converted" error message and
then figure out on their own that they have to use str(). It will be
intuitive/expected to work especially when used within the context of
the XSI(application) SDK.

Surely there is a way to get my object to be == a string in the eyes
of COM.

- Rafe
Aug 15 '08 #5

P: n/a
Rafe schrieb:
On Aug 15, 10:27 pm, Wolfgang Grafen <wolfgang.gra...@ericsson.com>
wrote:
>Rafe schrieb:
>>Now if I try to pass this as I would a string, roughly like so...
>s = StrLike("test")
>Application.AnObject.attribute = "test" # works fine
>Application.AnObject.attribute = s
ERROR : Traceback (most recent call last):
File "<Script Block >", line 18, in <module>
XSI.Selection[0].name = s
File "C:\Python25\Lib\site-packages\win32com\client\dynamic.py",
line 544, in __setattr__
self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
TypeError: Objects of type 'StrLike' can not be converted to a COM
VARIANT
Inheriting the str type doesn't raise any errors, but it's immutible
so it won't work. The attribute I am trying to set in XSI only takes a
string. So is it possible to make a string like object work like a
string in this scenario? Is there some built-in method I am missing or
some win32com.client trick? Help?
Thanks for reading,
- Rafe
Add
def __str__(self): return repr(self.__data)

then
> >>Application.AnObject.attribute = str(s)
should work

untested

Best Regards

Wolfgang

Thanks for the reply.

I don't need __str__ because Python will automatically use __repr__
when __str__ isn't defined anyway. I also don't want people to have to
wrap this in str().

While str() does work, the real problem is usability. The user will
have to try it, decrypt the "can not be converted" error message and
then figure out on their own that they have to use str(). It will be
intuitive/expected to work especially when used within the context of
the XSI(application) SDK.

Surely there is a way to get my object to be == a string in the eyes
of COM.
Nope. There is no automatic type casting. You have to write a function
for this
for the '.attribute' method, or you have to use str() which is quite
useable and comprehensive IMO.

Wolfgang
Aug 15 '08 #6

P: n/a
On Aug 16, 1:25 am, Wolfgang Grafen <wolfgang.gra...@ericsson.com>
wrote:
Rafeschrieb:
On Aug 15, 10:27 pm, Wolfgang Grafen <wolfgang.gra...@ericsson.com>
wrote:
>Rafeschrieb:
>Now if I try to pass this as I would a string, roughly like so...
s = StrLike("test")
Application.AnObject.attribute = "test" # works fine
Application.AnObject.attribute = s
ERROR : Traceback (most recent call last):
File "<Script Block >", line 18, in <module>
XSI.Selection[0].name = s
File "C:\Python25\Lib\site-packages\win32com\client\dynamic.py",
line 544, in __setattr__
self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
TypeError: Objects of type 'StrLike' can not be converted to a COM
VARIANT
Inheriting the str type doesn't raise any errors, but it's immutible
so it won't work. The attribute I am trying to set in XSI only takes a
string. So is it possible to make a string like object work like a
string in this scenario? Is there some built-in method I am missing or
some win32com.client trick? Help?
Thanks for reading,
-Rafe
Add
def __str__(self): return repr(self.__data)
then
>>Application.AnObject.attribute = str(s)
should work
untested
Best Regards
Wolfgang
Thanks for the reply.
I don't need __str__ because Python will automatically use __repr__
when __str__ isn't defined anyway. I also don't want people to have to
wrap this in str().
While str() does work, the real problem is usability. The user will
have to try it, decrypt the "can not be converted" error message and
then figure out on their own that they have to use str(). It will be
intuitive/expected to work especially when used within the context of
the XSI(application) SDK.
Surely there is a way to get my object to be == a string in the eyes
of COM.

Nope. There is no automatic type casting. You have to write a function
for this
for the '.attribute' method, or you have to use str() which is quite
useable and comprehensive IMO.

Wolfgang
Thanks for confirming.

It seems like an mutable string class to inherit would solve this, but
that is a can-o-worms I've noticed.

I've been able to work around this by internalizing the object which I
want to act like a string and then using a Property to control the
access.

- Rafe
Aug 19 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.