MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
PyConverter.cs
1using System;
2using System.Collections.Generic;
3using System.Reflection;
4using System.Linq;
5
7{
15 public class PyConverter
16 {
20 public PyConverter()
21 {
22 this.Converters = new List<PyClrTypeBase>();
23 this.PythonConverters = new Dictionary<IntPtr, Dictionary<Type, PyClrTypeBase>>();
24 this.ClrConverters = new Dictionary<Type, Dictionary<IntPtr, PyClrTypeBase>>();
25 }
26
27 private List<PyClrTypeBase> Converters;
28
29 private Dictionary<IntPtr, Dictionary<Type, PyClrTypeBase>> PythonConverters;
30
31 private Dictionary<Type, Dictionary<IntPtr, PyClrTypeBase>> ClrConverters;
32
37 public void Add(PyClrTypeBase converter)
38 {
39 this.Converters.Add(converter);
40
41 Dictionary<Type, PyClrTypeBase> py_converters;
42 var state = this.PythonConverters.TryGetValue(converter.PythonType.Handle, out py_converters);
43 if (!state)
44 {
45 py_converters = new Dictionary<Type, PyClrTypeBase>();
46 this.PythonConverters.Add(converter.PythonType.Handle, py_converters);
47 }
48 py_converters.Add(converter.ClrType, converter);
49
50 Dictionary<IntPtr, PyClrTypeBase> clr_converters;
51 state = this.ClrConverters.TryGetValue(converter.ClrType, out clr_converters);
52 if (!this.ClrConverters.ContainsKey(converter.ClrType))
53 {
54 clr_converters = new Dictionary<IntPtr, PyClrTypeBase>();
55 this.ClrConverters.Add(converter.ClrType, clr_converters);
56 }
57 clr_converters.Add(converter.PythonType.Handle, converter);
58 }
59
66 public void AddObjectType<T>(PyObject pyType, PyConverter converter = null)
67 {
68 if (converter == null)
69 {
70 converter = this;
71 }
72 this.Add(new ObjectType<T>(pyType, converter));
73 }
74
79 public void AddListType(PyConverter converter = null)
80 {
81 this.AddListType<object>(converter);
82 }
83
89 public void AddListType<T>(PyConverter converter = null)
90 {
91 if (converter == null)
92 {
93 converter = this;
94 }
95 this.Add(new PyListType<T>(converter));
96 }
97
104 public void AddDictType<K, V>(PyConverter converter = null)
105 {
106 if (converter == null)
107 {
108 converter = this;
109 }
110 this.Add(new PyDictType<K, V>(converter));
111 }
112
119 public T ToClr<T>(PyObject obj)
120 {
121 return (T)ToClr(obj, typeof(T));
122 }
123
130 public object ToClr(PyObject obj, Type t = null)
131 {
132 if (obj == null)
133 {
134 return null;
135 }
136 PyObject type = obj.GetPythonType();
137 Dictionary<Type, PyClrTypeBase> converters;
138 var state = PythonConverters.TryGetValue(type.Handle, out converters);
139 if (!state)
140 {
141 return obj;
142 }
143 if (t == null || !converters.ContainsKey(t))
144 {
145 return converters.Values.First().ToClr(obj);
146 }
147 else
148 {
149 return converters[t].ToClr(obj);
150 }
151 }
152
160 public PyObject ToPython(object clrObj, IntPtr? t = null)
161 {
162 if (clrObj == null)
163 {
164 return null;
165 }
166 Type type = clrObj.GetType();
167 Dictionary<IntPtr, PyClrTypeBase> converters;
168 var state = ClrConverters.TryGetValue(type, out converters);
169 if (!state)
170 {
171 throw new Exception($"Type {type.ToString()} not recognized");
172 }
173 if (t == null || !converters.ContainsKey(t.Value))
174 {
175 return converters.Values.First().ToPython(clrObj);
176 }
177 else
178 {
179 return converters[t.Value].ToPython(clrObj);
180 }
181 }
182 }
183
191 public abstract class PyClrTypeBase
192 {
198 protected PyClrTypeBase(string pyType, Type clrType)
199 {
200 this.PythonType = PythonEngine.Eval(pyType);
201 this.ClrType = clrType;
202 }
203
209 protected PyClrTypeBase(PyObject pyType, Type clrType)
210 {
211 this.PythonType = pyType;
212 this.ClrType = clrType;
213 }
214
218 public PyObject PythonType
219 {
220 get;
221 private set;
222 }
223
227 public Type ClrType
228 {
229 get;
230 private set;
231 }
232
238 public abstract object ToClr(PyObject pyObj);
239
245 public abstract PyObject ToPython(object clrObj);
246 }
247
256 {
264 public PyClrType(PyObject pyType, Type clrType,
265 Func<PyObject, object> py2clr, Func<object, PyObject> clr2py)
266 : base(pyType, clrType)
267 {
268 this.Py2Clr = py2clr;
269 this.Clr2Py = clr2py;
270 }
271
275 private Func<PyObject, object> Py2Clr;
276
280 private Func<object, PyObject> Clr2Py;
281
287 public override object ToClr(PyObject pyObj)
288 {
289 return this.Py2Clr(pyObj);
290 }
291
297 public override PyObject ToPython(object clrObj)
298 {
299 return this.Clr2Py(clrObj);
300 }
301 }
302
311 {
315 public StringType()
316 : base("str", typeof(string))
317 {
318 }
319
325 public override object ToClr(PyObject pyObj)
326 {
327 return pyObj.As<string>();
328 }
329
335 public override PyObject ToPython(object clrObj)
336 {
337 return new PyString(Convert.ToString(clrObj));
338 }
339 }
340
349 {
353 public BooleanType()
354 : base("bool", typeof(bool))
355 {
356 }
357
363 public override object ToClr(PyObject pyObj)
364 {
365 return pyObj.As<bool>();
366 }
367
373 public override PyObject ToPython(object clrObj)
374 {
375 //return new PyBoolean(Convert.ToString(clrObj));
376 throw new NotImplementedException();
377 }
378 }
379
388 {
392 public Int32Type()
393 : base("int", typeof(int))
394 {
395 }
396
402 public override object ToClr(PyObject pyObj)
403 {
404 return pyObj.As<int>();
405 }
406
412 public override PyObject ToPython(object clrObj)
413 {
414 return new PyInt(Convert.ToInt32(clrObj));
415 }
416 }
417
426 {
430 public Int64Type()
431 : base("int", typeof(long))
432 {
433 }
434
440 public override object ToClr(PyObject pyObj)
441 {
442 return pyObj.As<long>();
443 }
444
450 public override PyObject ToPython(object clrObj)
451 {
452 return new PyInt(Convert.ToInt64(clrObj));
453 }
454 }
455
464 {
468 public FloatType()
469 : base("float", typeof(float))
470 {
471 }
472
478 public override object ToClr(PyObject pyObj)
479 {
480 return pyObj.As<float>();
481 }
482
488 public override PyObject ToPython(object clrObj)
489 {
490 return new PyFloat(Convert.ToSingle(clrObj));
491 }
492 }
493
502 {
506 public DoubleType()
507 : base("float", typeof(double))
508 {
509 }
510
516 public override object ToClr(PyObject pyObj)
517 {
518 return pyObj.As<double>();
519 }
520
526 public override PyObject ToPython(object clrObj)
527 {
528 return new PyFloat(Convert.ToDouble(clrObj));
529 }
530 }
531
539 public class PyPropetryAttribute : Attribute
540 {
545 {
546 this.Name = null;
547 }
548
554 public PyPropetryAttribute(string name, string py_type = null)
555 {
556 this.Name = name;
557 this.PythonTypeName = py_type;
558 }
559
563 public string Name
564 {
565 get;
566 private set;
567 }
568
572 public string PythonTypeName
573 {
574 get;
575 private set;
576 }
577
581 public IntPtr? PythonType
582 {
583 get;
584 set;
585 }
586 }
587
595 abstract class ClrMemberInfo
596 {
600 public string PyPropertyName;
601
605 public IntPtr? PythonType;
606
610 public string ClrPropertyName;
611
615 public Type ClrType;
616
621
627 public abstract void SetPyObjAttr(PyObject pyObj, object clrObj);
628
634 public abstract void SetClrObjAttr(object clrObj, PyObject pyObj);
635 }
636
645 {
653 {
654 this.PropertyInfo = info;
655 this.ClrPropertyName = info.Name;
656 this.ClrType = info.PropertyType;
657 this.PyPropertyName = py_info.Name;
658 if (string.IsNullOrEmpty(this.PyPropertyName))
659 {
660 this.PyPropertyName = info.Name;
661 }
662 //this.PythonType = converter.Get();
663 this.Converter = converter;
664 }
665
670 {
671 get;
672 private set;
673 }
674
680 public override void SetPyObjAttr(PyObject pyObj, object clrObj)
681 {
682 var clr_value = this.PropertyInfo.GetValue(clrObj, null);
683 var py_value = this.Converter.ToPython(clr_value);
684 pyObj.SetAttr(this.PyPropertyName, py_value);
685 }
686
692 public override void SetClrObjAttr(object clrObj, PyObject pyObj)
693 {
694 var py_value = pyObj.GetAttr(this.PyPropertyName);
695 var clr_value = this.Converter.ToClr(py_value);
696 this.PropertyInfo.SetValue(clrObj, clr_value, null);
697 }
698 }
699
708 {
715 public ClrFieldInfo(FieldInfo info, PyPropetryAttribute py_info, PyConverter converter)
716 {
717 this.FieldInfo = info;
718 this.ClrPropertyName = info.Name;
719 this.ClrType = info.FieldType;
720 this.PyPropertyName = py_info.Name;
721 if (string.IsNullOrEmpty(this.PyPropertyName))
722 {
723 this.PyPropertyName = info.Name;
724 }
725 //this.PythonType = converter.Get();
726 this.Converter = converter;
727 }
728
733
739 public override void SetPyObjAttr(PyObject pyObj, object clrObj)
740 {
741 var clr_value = this.FieldInfo.GetValue(clrObj);
742 var py_value = this.Converter.ToPython(clr_value);
743 pyObj.SetAttr(this.PyPropertyName, py_value);
744 }
745
751 public override void SetClrObjAttr(object clrObj, PyObject pyObj)
752 {
753 var py_value = pyObj.GetAttr(this.PyPropertyName);
754 var clr_value = this.Converter.ToClr(py_value);
755 this.FieldInfo.SetValue(clrObj, clr_value);
756 }
757 }
758
766 public class ObjectType<T> : PyClrTypeBase
767 {
773 public ObjectType(PyObject pyType, PyConverter converter)
774 : base(pyType, typeof(T))
775 {
776 this.Converter = converter;
777 this.Properties = new List<ClrMemberInfo>();
778
779 // Get all attributes
780 foreach (var property in this.ClrType.GetProperties())
781 {
782 var attr = property.GetCustomAttributes(typeof(PyPropetryAttribute), true);
783 if (attr.Length == 0)
784 {
785 continue;
786 }
787 var py_info = attr[0] as PyPropetryAttribute;
788 this.Properties.Add(new ClrPropertyInfo(property, py_info, this.Converter));
789 }
790
791 foreach (var field in this.ClrType.GetFields())
792 {
793 var attr = field.GetCustomAttributes(typeof(PyPropetryAttribute), true);
794 if (attr.Length == 0)
795 {
796 continue;
797 }
798 var py_info = attr[0] as PyPropetryAttribute;
799 this.Properties.Add(new ClrFieldInfo(field, py_info, this.Converter));
800 }
801 }
802
806 private PyConverter Converter;
807
811 private List<ClrMemberInfo> Properties;
812
818 public override object ToClr(PyObject pyObj)
819 {
820 var clrObj = Activator.CreateInstance(this.ClrType);
821 foreach (var item in this.Properties)
822 {
823 item.SetClrObjAttr(clrObj, pyObj);
824 }
825 return clrObj;
826 }
827
833 public override PyObject ToPython(object clrObj)
834 {
835 var pyObj = this.PythonType.Invoke();
836 foreach (var item in this.Properties)
837 {
838 item.SetPyObjAttr(pyObj, clrObj);
839 }
840 return pyObj;
841 }
842 }
843
852 public class PyListType<T> : PyClrTypeBase
853 {
858 public PyListType(PyConverter converter)
859 : base("list", typeof(List<T>))
860 {
861 this.Converter = converter;
862 }
863
867 private PyConverter Converter;
868
874 public override object ToClr(PyObject pyObj)
875 {
876 var dict = this._ToClr(new PyList(pyObj));
877 return dict;
878 }
879
885 private object _ToClr(PyList pyList)
886 {
887 var list = new List<T>();
888 foreach (PyObject item in pyList)
889 {
890 var _item = this.Converter.ToClr<T>(item);
891 list.Add(_item);
892 }
893 return list;
894 }
895
901 public override PyObject ToPython(object clrObj)
902 {
903 return this._ToPython(clrObj as List<T>);
904 }
905
911 public PyObject _ToPython(List<T> clrObj)
912 {
913 var pyList = new PyList();
914 foreach (var item in clrObj)
915 {
916 PyObject _item = this.Converter.ToPython(item);
917 pyList.Append(_item);
918 }
919 return pyList;
920 }
921 }
922
932 public class PyDictType<K, V> : PyClrTypeBase
933 {
938 public PyDictType(PyConverter converter)
939 : base("dict", typeof(Dictionary<K, V>))
940 {
941 this.Converter = converter;
942 }
943
947 private PyConverter Converter;
948
954 public override object ToClr(PyObject pyObj)
955 {
956 var dict = this._ToClr(new PyDict(pyObj));
957 return dict;
958 }
959
965 private object _ToClr(PyDict pyDict)
966 {
967 var dict = new Dictionary<K, V>();
968 foreach (PyObject key in pyDict.Keys())
969 {
970 var _key = this.Converter.ToClr<K>(key);
971 PyObject objVal = pyDict[key];
972
973 var _value = this.Converter.ToClr<V>(objVal);
974 dict.Add(_key, _value);
975 }
976 return dict;
977 }
978
984 public override PyObject ToPython(object clrObj)
985 {
986 return this._ToPython(clrObj as Dictionary<K, V>);
987 }
988
994 public PyObject _ToPython(Dictionary<K, V> clrObj)
995 {
996 var pyDict = new PyDict();
997 foreach (var item in clrObj)
998 {
999 PyObject _key = this.Converter.ToPython(item.Key);
1000 PyObject _value = this.Converter.ToPython(item.Value);
1001 pyDict[_key] = _value;
1002 }
1003 return pyDict;
1004 }
1005 }
1006}
The BooleanType represents a clr bool type.
Definition: PyConverter.cs:349
BooleanType()
The constructor.
Definition: PyConverter.cs:353
override PyObject ToPython(object clrObj)
Converts a bool to a PyObject.
Definition: PyConverter.cs:373
override object ToClr(PyObject pyObj)
Converts a PyObject to a bool.
Definition: PyConverter.cs:363
The ClrFieldInfo defines the clr field information.
Definition: PyConverter.cs:708
override void SetPyObjAttr(PyObject pyObj, object clrObj)
Sets the Python object attribute.
Definition: PyConverter.cs:739
ClrFieldInfo(FieldInfo info, PyPropetryAttribute py_info, PyConverter converter)
The constructor.
Definition: PyConverter.cs:715
FieldInfo FieldInfo
Returns the field information.
Definition: PyConverter.cs:732
override void SetClrObjAttr(object clrObj, PyObject pyObj)
Sets the clr object attribute.
Definition: PyConverter.cs:751
The ClrMemberInfo represents clr information.
Definition: PyConverter.cs:596
abstract void SetPyObjAttr(PyObject pyObj, object clrObj)
Set the Python object attribute.
Type ClrType
Returns the clr type.
Definition: PyConverter.cs:615
string ClrPropertyName
Returns the clr property name.
Definition: PyConverter.cs:610
PyConverter Converter
Returns the converter used.
Definition: PyConverter.cs:620
abstract void SetClrObjAttr(object clrObj, PyObject pyObj)
Set the clr object attribute.
IntPtr? PythonType
Returns the Python type.
Definition: PyConverter.cs:605
string PyPropertyName
Returns the Python property name.
Definition: PyConverter.cs:600
The ClrPropertyInfo specifies the clr property information.
Definition: PyConverter.cs:645
override void SetClrObjAttr(object clrObj, PyObject pyObj)
Sets the clr object attribute.
Definition: PyConverter.cs:692
override void SetPyObjAttr(PyObject pyObj, object clrObj)
Sets the Python object attribute.
Definition: PyConverter.cs:680
ClrPropertyInfo(PropertyInfo info, PyPropetryAttribute py_info, PyConverter converter)
The constructor.
Definition: PyConverter.cs:652
PropertyInfo PropertyInfo
Return the clr Property information.
Definition: PyConverter.cs:670
The DoubleType represents a clr double type.
Definition: PyConverter.cs:502
DoubleType()
The constructor.
Definition: PyConverter.cs:506
override PyObject ToPython(object clrObj)
Converts a double to a PyObject.
Definition: PyConverter.cs:526
override object ToClr(PyObject pyObj)
Converts a PyObject to a float.
Definition: PyConverter.cs:516
The FloatType represents a clr float type.
Definition: PyConverter.cs:464
FloatType()
The constructor.
Definition: PyConverter.cs:468
override object ToClr(PyObject pyObj)
Converts a PyObject to a float.
Definition: PyConverter.cs:478
override PyObject ToPython(object clrObj)
Converts a float to a PyObject.
Definition: PyConverter.cs:488
The Int32Type represents a clr int type.
Definition: PyConverter.cs:388
Int32Type()
The constructor.
Definition: PyConverter.cs:392
override PyObject ToPython(object clrObj)
Converts a int to a PyObject.
Definition: PyConverter.cs:412
override object ToClr(PyObject pyObj)
Converts a PyObject to a int.
Definition: PyConverter.cs:402
The Int64Type represents a clr long type.
Definition: PyConverter.cs:426
override PyObject ToPython(object clrObj)
Converts a long to a PyObject.
Definition: PyConverter.cs:450
Int64Type()
The constructor.
Definition: PyConverter.cs:430
override object ToClr(PyObject pyObj)
Converts a PyObject to a long.
Definition: PyConverter.cs:440
Convert between Python object and clr object
Definition: PyConverter.cs:767
override object ToClr(PyObject pyObj)
Converts the PyObject to a clr object.
Definition: PyConverter.cs:818
override PyObject ToPython(object clrObj)
Converts a clr object to a PyObject.
Definition: PyConverter.cs:833
ObjectType(PyObject pyType, PyConverter converter)
The constructor.
Definition: PyConverter.cs:773
The PyClrTypeBase is the base class for other types.
Definition: PyConverter.cs:192
PyClrTypeBase(PyObject pyType, Type clrType)
The constructor.
Definition: PyConverter.cs:209
PyClrTypeBase(string pyType, Type clrType)
The constructor.
Definition: PyConverter.cs:198
abstract PyObject ToPython(object clrObj)
Converts the clr object to a PyObject type.
abstract object ToClr(PyObject pyObj)
Converts the PyObject type to the clr object.
Type ClrType
Returns the clr type.
Definition: PyConverter.cs:228
PyObject PythonType
Returns the Python type.
Definition: PyConverter.cs:219
The PyClrType class defines a Python clr type.
Definition: PyConverter.cs:256
override PyObject ToPython(object clrObj)
Converts a clr object to a PyObject.
Definition: PyConverter.cs:297
PyClrType(PyObject pyType, Type clrType, Func< PyObject, object > py2clr, Func< object, PyObject > clr2py)
The constructor.
Definition: PyConverter.cs:264
override object ToClr(PyObject pyObj)
Converts a PyObject to a clr object.
Definition: PyConverter.cs:287
use PyConverter to convert between python object and clr object.
Definition: PyConverter.cs:16
PyObject ToPython(object clrObj, IntPtr? t=null)
Convert a clr object to a PyObject.
Definition: PyConverter.cs:160
void AddListType(PyConverter converter=null)
Add a new list type to the converter.
Definition: PyConverter.cs:79
void AddObjectType< T >(PyObject pyType, PyConverter converter=null)
Add a new PyObjec type to the converter.
Definition: PyConverter.cs:66
void AddDictType< K, V >(PyConverter converter=null)
Add a new dictionary type of K key types and V value types.
Definition: PyConverter.cs:104
void Add(PyClrTypeBase converter)
Add a new type for conversion.
Definition: PyConverter.cs:37
object ToClr(PyObject obj, Type t=null)
Convert a PyObject to a clr type of 't'.
Definition: PyConverter.cs:130
T ToClr< T >(PyObject obj)
Convert the PyObject to a specifict type T
Definition: PyConverter.cs:119
void AddListType< T >(PyConverter converter=null)
Add a new list type to the converter.
Definition: PyConverter.cs:89
Specifies a PyDictionType dictionary.
Definition: PyConverter.cs:933
override object ToClr(PyObject pyObj)
Convert a PyObject to a clr object.
Definition: PyConverter.cs:954
override PyObject ToPython(object clrObj)
Convert a clr object to a PyObjec.t
Definition: PyConverter.cs:984
PyObject _ToPython(Dictionary< K, V > clrObj)
Convert a clr Dictionary to a PyDict and return it as a PyObject.
Definition: PyConverter.cs:994
PyDictType(PyConverter converter)
The constructor.
Definition: PyConverter.cs:938
Defines a PyListType of type 'T'
Definition: PyConverter.cs:853
PyListType(PyConverter converter)
The constructor.
Definition: PyConverter.cs:858
override PyObject ToPython(object clrObj)
Converts a clr object to a PyObject.
Definition: PyConverter.cs:901
override object ToClr(PyObject pyObj)
Converts the PyObject to a clr object.
Definition: PyConverter.cs:874
PyObject _ToPython(List< T > clrObj)
Converts a clr List to a PyList.
Definition: PyConverter.cs:911
The PyPropertyAttribute represents a Python attribute.
Definition: PyConverter.cs:540
string Name
Returns the attribute name.
Definition: PyConverter.cs:564
PyPropetryAttribute(string name, string py_type=null)
The constructor.
Definition: PyConverter.cs:554
PyPropetryAttribute()
The constructor.
Definition: PyConverter.cs:544
string PythonTypeName
Returns the atttribute type name.
Definition: PyConverter.cs:573
IntPtr? PythonType
Returns the Python type.
Definition: PyConverter.cs:582
The StringType represents a clr string type.
Definition: PyConverter.cs:311
override object ToClr(PyObject pyObj)
Converts a PyObject to a string.
Definition: PyConverter.cs:325
override PyObject ToPython(object clrObj)
Converts a string to a PyObject.
Definition: PyConverter.cs:335
StringType()
The constructor.
Definition: PyConverter.cs:315