@@ -15,7 +15,6 @@ limitations under the License.
1515******************************************************************************/
1616
1717using NumSharp ;
18- using NumSharp . Backends ;
1918using System ;
2019using System . Collections ;
2120using System . Collections . Generic ;
@@ -26,18 +25,15 @@ limitations under the License.
2625namespace Tensorflow
2726{
2827 /// <summary>
29- /// Mapping C# functions to Python
28+ /// Binding utilities to mimic python functions.
3029 /// </summary>
31- public static class Python
30+ public static partial class Binding
3231 {
3332 public static void print ( object obj )
3433 {
3534 Console . WriteLine ( obj . ToString ( ) ) ;
3635 }
3736
38- //protected int len<T>(IEnumerable<T> a)
39- // => a.Count();
40-
4137 public static int len ( object a )
4238 {
4339 switch ( a )
@@ -66,15 +62,15 @@ public static IEnumerable<int> range(int start, int end)
6662 return Enumerable . Range ( start , end - start ) ;
6763 }
6864
69- public static T New < T > ( ) where T : IPyClass , new ( )
65+ public static T New < T > ( ) where T : IObjectLife , new ( )
7066 {
7167 var instance = new T ( ) ;
72- instance . __init__ ( instance ) ;
68+ instance . __init__ ( ) ;
7369 return instance ;
7470 }
7571
7672 [ DebuggerNonUserCode ( ) ] // with "Just My Code" enabled this lets the debugger break at the origin of the exception
77- public static void tf_with ( IPython py , Action < IPython > action )
73+ public static void tf_with ( IObjectLife py , Action < IObjectLife > action )
7874 {
7975 try
8076 {
@@ -94,7 +90,7 @@ public static void tf_with(IPython py, Action<IPython> action)
9490 }
9591
9692 [ DebuggerNonUserCode ( ) ] // with "Just My Code" enabled this lets the debugger break at the origin of the exception
97- public static void tf_with < T > ( T py , Action < T > action ) where T : IPython
93+ public static void tf_with < T > ( T py , Action < T > action ) where T : IObjectLife
9894 {
9995 try
10096 {
@@ -114,7 +110,7 @@ public static void tf_with<T>(T py, Action<T> action) where T : IPython
114110 }
115111
116112 [ DebuggerNonUserCode ( ) ] // with "Just My Code" enabled this lets the debugger break at the origin of the exception
117- public static TOut tf_with < TIn , TOut > ( TIn py , Func < TIn , TOut > action ) where TIn : IPython
113+ public static TOut tf_with < TIn , TOut > ( TIn py , Func < TIn , TOut > action ) where TIn : IObjectLife
118114 {
119115 try
120116 {
@@ -124,7 +120,6 @@ public static TOut tf_with<TIn, TOut>(TIn py, Func<TIn, TOut> action) where TIn
124120 catch ( Exception ex )
125121 {
126122 Console . WriteLine ( ex . ToString ( ) ) ;
127- throw ;
128123 return default ( TOut ) ;
129124 }
130125 finally
@@ -276,7 +271,9 @@ public static bool hasattr(object obj, string key)
276271 var __memberobject__ = __type__ . GetMember ( key ) ;
277272 return ( __memberobject__ . Length > 0 ) ? true : false ;
278273 }
274+
279275 public delegate object __object__ ( params object [ ] args ) ;
276+
280277 public static __object__ getattr ( object obj , string key , params Type [ ] ___parameter_type__ )
281278 {
282279 var __dyn_obj__ = obj . GetType ( ) . GetMember ( key ) ;
@@ -288,7 +285,7 @@ public static __object__ getattr(object obj, string key, params Type[] ___parame
288285 try
289286 {
290287 var __method__ = ( ___parameter_type__ . Length > 0 ) ? obj . GetType ( ) . GetMethod ( key , ___parameter_type__ ) : obj . GetType ( ) . GetMethod ( key ) ;
291- return ( __object__ ) ( ( object [ ] args ) => __method__ . Invoke ( obj , args ) ) ;
288+ return ( object [ ] args ) => __method__ . Invoke ( obj , args ) ;
292289 }
293290 catch ( System . Reflection . AmbiguousMatchException ex )
294291 {
@@ -297,16 +294,17 @@ public static __object__ getattr(object obj, string key, params Type[] ___parame
297294 }
298295 else if ( __type__ . MemberType == System . Reflection . MemberTypes . Field )
299296 {
300- var __field__ = ( object ) obj . GetType ( ) . GetField ( key ) . GetValue ( obj ) ;
301- return ( __object__ ) ( ( object [ ] args ) => { return __field__ ; } ) ;
297+ var __field__ = obj . GetType ( ) . GetField ( key ) . GetValue ( obj ) ;
298+ return ( object [ ] args ) => { return __field__ ; } ;
302299 }
303300 else if ( __type__ . MemberType == System . Reflection . MemberTypes . Property )
304301 {
305- var __property__ = ( object ) obj . GetType ( ) . GetProperty ( key ) . GetValue ( obj ) ;
306- return ( __object__ ) ( ( object [ ] args ) => { return __property__ ; } ) ;
302+ var __property__ = obj . GetType ( ) . GetProperty ( key ) . GetValue ( obj ) ;
303+ return ( object [ ] args ) => { return __property__ ; } ;
307304 }
308- return ( __object__ ) ( ( object [ ] args ) => { return "NaN" ; } ) ;
305+ return ( object [ ] args ) => { return "NaN" ; } ;
309306 }
307+
310308 public static IEnumerable TupleToEnumerable ( object tuple )
311309 {
312310 Type t = tuple . GetType ( ) ;
@@ -315,18 +313,20 @@ public static IEnumerable TupleToEnumerable(object tuple)
315313 var flds = t . GetFields ( ) ;
316314 for ( int i = 0 ; i < flds . Length ; i ++ )
317315 {
318- yield return ( ( object ) flds [ i ] . GetValue ( tuple ) ) ;
316+ yield return flds [ i ] . GetValue ( tuple ) ;
319317 }
320318 }
321319 else
322320 {
323321 throw new System . Exception ( "Expected Tuple." ) ;
324322 }
325323 }
324+
326325 public static bool isinstance ( object Item1 , Type Item2 )
327326 {
328- return ( Item1 . GetType ( ) == Item2 ) ;
327+ return Item1 . GetType ( ) == Item2 ;
329328 }
329+
330330 public static bool isinstance ( object Item1 , object tuple )
331331 {
332332 var tup = TupleToEnumerable ( tuple ) ;
@@ -338,11 +338,4 @@ public static bool isinstance(object Item1, object tuple)
338338 return false ;
339339 }
340340 }
341-
342- public interface IPython : IDisposable
343- {
344- void __enter__ ( ) ;
345-
346- void __exit__ ( ) ;
347- }
348341}
0 commit comments