using NumSharp;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace Tensorflow
{
///
/// Mapping C# functions to Python
///
public class Python
{
protected void print(object obj)
{
Console.WriteLine(obj.ToString());
}
protected int len(IEnumerable a)
=> a.Count();
protected IEnumerable range(int end)
{
return Enumerable.Range(0, end);
}
protected IEnumerable range(int start, int end)
{
return Enumerable.Range(start, end);
}
public static T New(object args) where T : IPyClass
{
var instance = Activator.CreateInstance();
instance.__init__(instance, args);
return instance;
}
[DebuggerNonUserCode()] // with "Just My Code" enabled this lets the debugger break at the origin of the exception
public static void with(IPython py, Action action)
{
try
{
py.__enter__();
action(py);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
throw;
}
finally
{
py.__exit__();
py.Dispose();
}
}
[DebuggerNonUserCode()] // with "Just My Code" enabled this lets the debugger break at the origin of the exception
public static void with(T py, Action action) where T : IPython
{
try
{
py.__enter__();
action(py);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
throw;
}
finally
{
py.__exit__();
py.Dispose();
}
}
[DebuggerNonUserCode()] // with "Just My Code" enabled this lets the debugger break at the origin of the exception
public static TOut with(TIn py, Func action) where TIn : IPython
{
try
{
py.__enter__();
return action(py);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
throw;
return default(TOut);
}
finally
{
py.__exit__();
py.Dispose();
}
}
public static float time()
{
return (float)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
}
public static IEnumerable<(T, T)> zip(NDArray t1, NDArray t2)
{
for (int i = 0; i < t1.size; i++)
yield return (t1.Data(i), t2.Data(i));
}
public static IEnumerable<(T1, T2)> zip(IList t1, IList t2)
{
for (int i = 0; i < t1.Count; i++)
yield return (t1[i], t2[i]);
}
public static IEnumerable<(T1, T2)> zip(NDArray t1, NDArray t2)
{
for (int i = 0; i < t1.size; i++)
yield return (t1.Data(i), t2.Data(i));
}
public static IEnumerable<(T1, T2)> zip(IEnumerable e1, IEnumerable e2)
{
var iter2 = e2.GetEnumerator();
foreach (var v1 in e1)
{
iter2.MoveNext();
var v2 = iter2.Current;
yield return (v1, v2);
}
}
public static IEnumerable<(int, T)> enumerate(IList values)
{
for (int i = 0; i < values.Count; i++)
yield return (i, values[i]);
}
public static Dictionary ConvertToDict(object dyn)
{
var dictionary = new Dictionary();
foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(dyn))
{
object obj = propertyDescriptor.GetValue(dyn);
string name = propertyDescriptor.Name;
dictionary.Add(name, obj);
}
return dictionary;
}
}
public interface IPython : IDisposable
{
void __enter__();
void __exit__();
}
public class PyObject where T : IPyClass
{
public T Instance { get; set; }
}
}