www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - PyD - accessing D class fields in Python

reply clothlen <clothlen0+dlang gmail.com> writes:
Howdy;

I'm trying to extend my Python program with D, but I'm having 
trouble accessing a D class's field/attribute/property/something.

My D file looks like this:

```
module simpletest;
import pyd.pyd;
import std.stdio;

class ExampleTest{
	int _a = 3;
	int a(){
		return _a;
	}
	void a(int a){
		this._a = a;
	}
	int b = 4;
}

extern(C) void PydMain() {
	module_init();
	wrap_class!(
		ExampleTest,
		Property!(ExampleTest.a),
		Property!(ExampleTest.b)
		// Member!("b")
	);
}

```

and my Python file looks like this:

```


"""Run with `pipenv run pytest -s tests/test_simple_test.py`."""
import simpletest


def test_class():
	test = simpletest.ExampleTest()
	assert None is print(test.a)
	assert test.a == 3
	test.a = 2
	assert test.a == 2
	assert None is print(test.b)
```

The first field, `a`, I can read, but `attribute 'b' of 
'simpletest.ExampleTest' objects is not readable`. `a` has a 
getter and setter function; do I have to make getters and setters 
for all fields I want to share with Python? That sounds like a 
lot of wasted space if so. The only examples at the Github 
(https://github.com/ariovistus/pyd/tree/master/examples) only 
seem to show it with getters and setters.
Mar 18 2019
parent reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Monday, 18 March 2019 at 22:25:10 UTC, clothlen wrote:
 Howdy;

 I'm trying to extend my Python program with D, but I'm having 
 trouble accessing a D class's 
 field/attribute/property/something.

 My D file looks like this:

 ```
 module simpletest;
 import pyd.pyd;
 import std.stdio;

 class ExampleTest{
 	int _a = 3;
 	int a(){
 		return _a;
 	}
 	void a(int a){
 		this._a = a;
 	}
 	int b = 4;
 }

 extern(C) void PydMain() {
 	module_init();
 	wrap_class!(
 		ExampleTest,
 		Property!(ExampleTest.a),
 		Property!(ExampleTest.b)
 		// Member!("b")
Maybe you need `Member!(“b”, “rw”)`?
 	);
 }
If that doesn’t work, maybe you find help by filing an issue on PyD.
Mar 19 2019
parent reply mw <mingwu gmail.com> writes:
On Tuesday, 19 March 2019 at 23:58:48 UTC, Bastiaan Veelo wrote:
 On Monday, 18 March 2019 at 22:25:10 UTC, clothlen wrote:
 Howdy;

 I'm trying to extend my Python program with D, but I'm having 
 trouble accessing a D class's 
 field/attribute/property/something.

 My D file looks like this:

 ```
 module simpletest;
 import pyd.pyd;
 import std.stdio;

 class ExampleTest{
 	int _a = 3;
 	int a(){
 		return _a;
 	}
 	void a(int a){
 		this._a = a;
 	}
 	int b = 4;
 }

 extern(C) void PydMain() {
 	module_init();
 	wrap_class!(
 		ExampleTest,
 		Property!(ExampleTest.a),
 		Property!(ExampleTest.b)
 		// Member!("b")
Maybe you need `Member!(“b”, “rw”)`?
 	);
 }
If that doesn’t work, maybe you find help by filing an issue on PyD.
I filed the issue here (still no response yet): https://github.com/ariovistus/pyd/issues/152 Just tried: ``` Member!("m_i", Mode!"rw"), ``` Still the same error: ``` /usr/local/lib/python3.6/dist-packages/pyd/infrastructure/pyd/ ake_wrapper.d(127): Error: no property shim for type pyd.struct_wrap.Member!("m_i", Mode!"rw") /usr/local/lib/python3.6/dist-packages/pyd/infrastructure/pyd/ ake_wrapper.d(137): Error: template instance pyd.make_wrapper.class_decls!(0u, Foo, Member!("m_i", Mode!"rw"), Init!int, Init!(int, int), Property!(i, Docstring!"A sample property of Foo."), BinaryOperatorX!("+", false, Guess), Def!(opSlice, PyName!"__iter__", Range function()), Def!(foo, Docstring!"A sample method of Foo."), Def!(a), Def!(b), Def!(c), Def!(d), Def!(e), Def!(f), Def!(g), Def!(h), Def!(j), Def!(k), Def!(l), Def!(m), Def!(n)) error instantiating ``` Anyone know how to expose D class data member to Python? Thanks!
May 17
next sibling parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Monday, 17 May 2021 at 16:54:07 UTC, mw wrote:
 On Tuesday, 19 March 2019 at 23:58:48 UTC, Bastiaan Veelo wrote:
 [...]
I filed the issue here (still no response yet): https://github.com/ariovistus/pyd/issues/152 [...]
https://github.com/symmetryinvestments/autowrap
May 17
parent mw <mingwu gmail.com> writes:
On Monday, 17 May 2021 at 17:16:27 UTC, Imperatorn wrote:
 On Monday, 17 May 2021 at 16:54:07 UTC, mw wrote:
 On Tuesday, 19 March 2019 at 23:58:48 UTC, Bastiaan Veelo 
 wrote:
 [...]
I filed the issue here (still no response yet): https://github.com/ariovistus/pyd/issues/152 [...]
https://github.com/symmetryinvestments/autowrap
Thanks, I just tried, but still cannot get access to class field: I build this example: https://github.com/symmetryinvestments/autowrap/tree/master/examples/pyd and test this: https://github.com/symmetryinvestments/autowrap/blob/master/examples/pyd/source/class_wrap.d class Bizzy { int _m; ... } ``` $ dub build --config=python36 $ ln -s lib/pyd/libpydtests.so pyd.so $ python3
 import pyd
 b = pyd.Bizzy(0)
 b
bye(0)
 b._m = 3
Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'pyd.Bizzy' object has no attribute '_m' ``` Add `public` does not work either: ``` class Bizzy { public int _m; ... } ``` Any hints?
May 17
prev sibling parent reply mw <mingwu gmail.com> writes:
On Monday, 17 May 2021 at 16:54:07 UTC, mw wrote:
 I filed the issue here (still no response yet):

 https://github.com/ariovistus/pyd/issues/152
It's fixed now: pyd version >= 0.14.1
May 18
parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Tuesday, 18 May 2021 at 23:27:40 UTC, mw wrote:
 On Monday, 17 May 2021 at 16:54:07 UTC, mw wrote:
 I filed the issue here (still no response yet):

 https://github.com/ariovistus/pyd/issues/152
It's fixed now: pyd version >= 0.14.1
👍
May 19