473,748 Members | 11,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.__dat a)
def __cmp__(self, string): return cmp(self.__data , string)
def __contains__(se lf, 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 1422
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.__dat a)
def __cmp__(self, string): return cmp(self.__data , string)
def __contains__(se lf, 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.attrib ute = "test" # works fine
Application. AnObject.attrib ute = s
ERROR : Traceback (most recent call last):
File "<Script Block >", line 18, in <module>
XSI.Selection[0].name = s
File "C:\Python25\Li b\site-packages\win32c om\client\dynam ic.py",
line 544, in __setattr__
self._oleobj_.I nvoke(entry.dis pid, 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.__dat a)
def __cmp__(self, string): return cmp(self.__data , string)
def __contains__(se lf, 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.attrib ute = "test" # works fine
Application. AnObject.attrib ute = s
ERROR : Traceback (most recent call last):
File "<Script Block >", line 18, in <module>
XSI.Selection[0].name = s
File "C:\Python25\Li b\site-packages\win32c om\client\dynam ic.py",
line 544, in __setattr__
self._oleobj_.I nvoke(entry.dis pid, 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.__dat a)

then
>>Application.A nObject.attribu te = 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.__dat a)
def __cmp__(self, string): return cmp(self.__data , string)
def __contains__(se lf, 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.A nObject.attribu te = "test" # works fine
Application.A nObject.attribu te = s
ERROR : Traceback (most recent call last):
File "<Script Block >", line 18, in <module>
XSI.Selection[0].name = s
File "C:\Python25\Li b\site-packages\win32c om\client\dynam ic.py",
line 544, in __setattr__
self._oleobj_.I nvoke(entry.dis pid, 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.__dat a)

then
>>Application.A nObject.attribu te = 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")
>Applicatio n.AnObject.attr ibute = "test" # works fine
>Applicatio n.AnObject.attr ibute = s
ERROR : Traceback (most recent call last):
File "<Script Block >", line 18, in <module>
XSI.Selection[0].name = s
File "C:\Python25\Li b\site-packages\win32c om\client\dynam ic.py",
line 544, in __setattr__
self._oleobj_.I nvoke(entry.dis pid, 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.__dat a)

then
> >>Application.A nObject.attribu te = 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.attri bute = "test" # works fine
Application .AnObject.attri bute = s
ERROR : Traceback (most recent call last):
File "<Script Block >", line 18, in <module>
XSI.Selection[0].name = s
File "C:\Python25\Li b\site-packages\win32c om\client\dynam ic.py",
line 544, in __setattr__
self._oleobj_.I nvoke(entry.dis pid, 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.__dat a)
then
>>Application.A nObject.attribu te = 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
1877
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 the function in the onClick event of the link being written on screen fails indicating 'theForm' as being passed through is 'not defined'. The form is being passed as "document.resultAdd" and can be checked as arriving in my function. So what...
0
1406
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 that this params are variants. I'm traying to pass "object" variables to the params but the com object return an error or a null object, the piece of code that i'm using is: using AgenObj;
0
3290
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 = 0ms.....60ms. I'm already timing the callbacks using the Win32API QueryPerformanceCounter/Frequency. The code I have is actually performing the callback and it looks like it is close to 1ms but I now need to get my object passed to this static...
2
1152
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
1241
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
3891
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 property on it that is also another object. private void pIObj_EventProc(MObj.MainObj pObj) { MObj.Obj mainobj = pObj.Obj; // IF I DON'T DO THE NEXT LINE, THE APPLICATION HANGS AT SOME LATER TIME // THE UI DOESN'T EVEN UPDATE....
7
1622
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 have the same value as when called the first time. What would be the best approach (and simplest) way to deal with this? Cheers Andrew
7
3305
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 object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
4
1781
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. Works most of the time, but not always. At first, I had an object variable declared in a class, and everything worked fine. Then, I figured I should really have 2 classes- one wrapping the other. I need my object in both classes, so I passed it as...
1
1266
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: 'ui-state-highlight', drop: function(ev, ui) { alert("B "+ev);
0
8989
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9537
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8241
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2213
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.