using NumSharp.Core; using System; using System.Collections.Generic; using System.Text; namespace Tensorflow { /// /// Mapping C# functions to Python /// public class Python { protected void print(object obj) { Console.WriteLine(obj.ToString()); } public static void with(IPython py, Action action) { try { py.__enter__(); action(py); } catch (Exception ex) { Console.WriteLine(ex.ToString()); throw ex; } finally { py.__exit__(); py.Dispose(); } } public static void with(IPython py, Action action) where T : IPython { try { py.__enter__(); action((T)py); } catch (Exception ex) { Console.WriteLine(ex.ToString()); throw ex; } finally { py.__exit__(); py.Dispose(); } } public static TOut with(IPython py, Func action) where TIn : IPython { try { py.__enter__(); return action((TIn)py); } catch (Exception ex) { Console.WriteLine(ex.ToString()); throw ex; } finally { py.__exit__(); py.Dispose(); } } public static IEnumerable<(T, T)> zip(NDArray t1, NDArray t2) { int index = 0; yield return(t1.Data(index), t2.Data(index)); } public static IEnumerable<(T, T)> zip(IList t1, IList t2) { for (int i = 0; i < t1.Count; i++) yield return (t1[i], t2[i]); } public static IEnumerable<(int, T)> enumerate(IList values) { for (int i = 0; i < values.Count; i++) yield return (i, values[i]); } } public interface IPython : IDisposable { void __enter__(); void __exit__(); } }