472,371 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

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

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
6 1322
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Mark Anderson | last post by:
I have code to develop result page links (like a search engine) for some results being passed from a database where I've no server-sdide acces - thus JS. The code is below and works fine except...
0
by: Mihaly | last post by:
Hello, i'm using a VC++ com+ object, it's reference in vs .net, this com object have a function that have to params, the interop com object recive Object Params and the original com object say...
0
by: Jeffrey B. Holtz | last post by:
Has anyone used the multimedia timere timeSetEvent in C#? I'm trying to use it to get a 1ms accurate timer. All the other implementations are far to inaccurate in their resolution. 1ms Timer =...
2
by: Oleg | last post by:
Can I pass an object from one web application to another on the same server? I want to kind of post it or something so that I don't have to pass info in the url string.
0
by: martin_saucier | last post by:
I have a problem where when passing object ByRef in a WebService, some object members are being return as Nothing when coming back from the call ? How do I fix this ? Thanks. Martin
4
by: Jayme Pechan | last post by:
I have a ATL COM object that is loaded through Interop in a C# application. The COM object fires an event and one of the parameters is another object created inside the object. This object has a...
7
by: J055 | last post by:
Hi I'm a little confused by this so would appreciate some advice on ways to deal with the following. When I call the CodeMaker.Update static method for the second time I want the dt parameter to...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
4
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I had a class that worked, but I knew wasn't designed correctly from an OO point of view, so I changed it so it was more like I thought it 'should' be, and now it doesn't always work....
1
by: koyanpaing | last post by:
Hi everyone, I would like to know how can i know the value from the passing object.Here is the code example. <script> $trash.droppable({ accept: '#gallery > li', activeClass:...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.