Re: [IronPython] Printing an object out as a string
If you're doing the logging from within C#, then there's no way to recover the originally-passed Python object if the binder has had to perform a conversion -- as it will have in this case. On Tue, Jan 27, 2009 at 8:41 PM, Jeff Slutter wrote: > Curt Hagenlocher wrote: > > If you're willing to live on the bleeding edge and build from the latest > > sources, it turns out that Tomas just added this functionality to the > > hosting interfaces yesterday. ObjectOperations.Format(obj) should return > > a string that matches what the REPL would have printed. > > I have no problem with living on the bleeding edge. But I realized why > repr wasn't working so well for me: > > In a nutshell, I need to get the Python function call string that was > made (including arguments), for logging purposes. The functions are in > my C# code, the calls are made from Python. > > I can get the name of the function through some means, but the arguments > are the part I've having a hard time with. I can make the assumption > that the arguments being passed into the function have a textual > representation. > > So, if I have a function in C# like: > > void MyFunc( IEnumerable someList ) > { >string someListAsStr = SomeMagicFunction(someList); >Log( someListAsStr ); > } > > > and in Python I call it like: > > a = [0,1,2] > MyFunc( a ) > > > I want to log the string "[0,1,2]" > > repr wasn't working for me, I believe, because once I get inside MyFunc, > IronPython has wrapped and converted the PythonList into this > IEnumerable I have. So, repr was giving me something not so pretty. > > Will the new Format function give me what I want? Or should I continue > down a different path? > > Thanks, > Jeff > ___ > Users mailing list > Users@lists.ironpython.com > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com > ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] Printing an object out as a string
Curt Hagenlocher wrote: > If you're willing to live on the bleeding edge and build from the latest > sources, it turns out that Tomas just added this functionality to the > hosting interfaces yesterday. ObjectOperations.Format(obj) should return > a string that matches what the REPL would have printed. I have no problem with living on the bleeding edge. But I realized why repr wasn't working so well for me: In a nutshell, I need to get the Python function call string that was made (including arguments), for logging purposes. The functions are in my C# code, the calls are made from Python. I can get the name of the function through some means, but the arguments are the part I've having a hard time with. I can make the assumption that the arguments being passed into the function have a textual representation. So, if I have a function in C# like: void MyFunc( IEnumerable someList ) { string someListAsStr = SomeMagicFunction(someList); Log( someListAsStr ); } and in Python I call it like: a = [0,1,2] MyFunc( a ) I want to log the string "[0,1,2]" repr wasn't working for me, I believe, because once I get inside MyFunc, IronPython has wrapped and converted the PythonList into this IEnumerable I have. So, repr was giving me something not so pretty. Will the new Format function give me what I want? Or should I continue down a different path? Thanks, Jeff ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] Printing an object out as a string
If you need to call it from C# app you can use engine.ObjectOperations.Format(obj) where "engine" could be created like this: ScriptEngine engine = IronPython.Hosting.Python.CreateEngine(); ObjectOperations::Format calls "repr". Tomas -Original Message- From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of Jimmy Schementi Sent: Tuesday, January 27, 2009 7:56 PM To: Discussion of IronPython Subject: Re: [IronPython] Printing an object out as a string repr([0,1,2]) > -Original Message- > From: users-boun...@lists.ironpython.com [mailto:users- > boun...@lists.ironpython.com] On Behalf Of Jeff Slutter > Sent: Tuesday, January 27, 2009 7:53 PM > To: Discussion of IronPython > Subject: [IronPython] Printing an object out as a string > > Given an object, how can I go about printing out that object as a > string > representing the object in Python syntax? > > I need something a little more powerful than just object.ToString() > > I need something like how the interactive console reports the result of > the statement. > > For instance: > > a = [0,1,2] > b = [a,8,9] > > If I pass 'b' to a function in C#, like: > > void SomeFunction( object obj ) > { >string objStr = ConvertToString( obj ); >Console.Write( objStr ); > } > > it would print out: > [[0,1,2],8,9] > > Obviously ToString() will work for simple things, but once I try to use > lists or dictionaries it no longer works. > > Thank you, > Jeff > ___ > Users mailing list > Users@lists.ironpython.com > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] Printing an object out as a string
If you're willing to live on the bleeding edge and build from the latest sources, it turns out that Tomas just added this functionality to the hosting interfaces yesterday. ObjectOperations.Format(obj) should return a string that matches what the REPL would have printed. On Tue, Jan 27, 2009 at 7:52 PM, Jeff Slutter wrote: > Given an object, how can I go about printing out that object as a string > representing the object in Python syntax? > > I need something a little more powerful than just object.ToString() > > I need something like how the interactive console reports the result of > the statement. > > For instance: > > a = [0,1,2] > b = [a,8,9] > > If I pass 'b' to a function in C#, like: > > void SomeFunction( object obj ) > { > string objStr = ConvertToString( obj ); > Console.Write( objStr ); > } > > it would print out: > [[0,1,2],8,9] > > Obviously ToString() will work for simple things, but once I try to use > lists or dictionaries it no longer works. > > Thank you, > Jeff > ___ > Users mailing list > Users@lists.ironpython.com > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com > ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] Printing an object out as a string
repr([0,1,2]) > -Original Message- > From: users-boun...@lists.ironpython.com [mailto:users- > boun...@lists.ironpython.com] On Behalf Of Jeff Slutter > Sent: Tuesday, January 27, 2009 7:53 PM > To: Discussion of IronPython > Subject: [IronPython] Printing an object out as a string > > Given an object, how can I go about printing out that object as a > string > representing the object in Python syntax? > > I need something a little more powerful than just object.ToString() > > I need something like how the interactive console reports the result of > the statement. > > For instance: > > a = [0,1,2] > b = [a,8,9] > > If I pass 'b' to a function in C#, like: > > void SomeFunction( object obj ) > { >string objStr = ConvertToString( obj ); >Console.Write( objStr ); > } > > it would print out: > [[0,1,2],8,9] > > Obviously ToString() will work for simple things, but once I try to use > lists or dictionaries it no longer works. > > Thank you, > Jeff > ___ > Users mailing list > Users@lists.ironpython.com > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
[IronPython] Printing an object out as a string
Given an object, how can I go about printing out that object as a string representing the object in Python syntax? I need something a little more powerful than just object.ToString() I need something like how the interactive console reports the result of the statement. For instance: a = [0,1,2] b = [a,8,9] If I pass 'b' to a function in C#, like: void SomeFunction( object obj ) { string objStr = ConvertToString( obj ); Console.Write( objStr ); } it would print out: [[0,1,2],8,9] Obviously ToString() will work for simple things, but once I try to use lists or dictionaries it no longer works. Thank you, Jeff ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com