Re: Get __name__ in C extension module

2019-10-06 Thread Ian Pilcher

On 10/5/19 12:55 PM, Ian Pilcher wrote:

This is straightforward, except that I cannot figure out how to retrieve
the __name__.


Making progress.  I can get a __name__ value with:

  PyDict_GetItemString(PyEval_GetGlobals(), "__name__")

I say "a __name__ value" because the returned value is actually that of
the calling module, not the name of my extension.

Is this normal?

Thanks!

--

Ian Pilcher [email protected]
 "I grew up before Mark Zuckerberg invented friendship" 


--
https://mail.python.org/mailman/listinfo/python-list


Re: Get __name__ in C extension module

2019-10-06 Thread MRAB

On 2019-10-06 15:48, Ian Pilcher wrote:

On 10/5/19 12:55 PM, Ian Pilcher wrote:

This is straightforward, except that I cannot figure out how to retrieve
the __name__.


Making progress.  I can get a __name__ value with:

PyDict_GetItemString(PyEval_GetGlobals(), "__name__")

I say "a __name__ value" because the returned value is actually that of
the calling module, not the name of my extension.

Is this normal?

Don't you already have the module's name? You have to specify it in the 
PyModuleDef struct that you pass to PyModule_Create.
Calling PyModule_Create returns a reference to the module, and you can 
get its namespace dict with PyModule_GetDict(...).

--
https://mail.python.org/mailman/listinfo/python-list


Anyone has good understanding of how model base agents work in 2D array? --AIMA repository

2019-10-06 Thread krishna singh
Hi,
I have started learning artificial intelligence and currently going through 
 AIMA repository https://github.com/aimacode/aima-python but I am stucked at 
model base reflex agent implementation in the 2D environment. Does anyone have 
a good understanding of how model base agents work in 2D array? 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Get __name__ in C extension module

2019-10-06 Thread Barry Scott



> On 5 Oct 2019, at 18:55, Ian Pilcher  wrote:
> 
> On 10/4/19 4:30 PM, Ian Pilcher wrote:
>> Ideally, I would pass my existing Logging.logger object into my C
>> function and use PyObject_CallMethod to call the appropriate method on
>> it (info, debug, etc.).
> 
> As I've researched this further, I've realized that this isn't the
> correct approach.
> 
> My extension should be doing the C equivalent of:
> 
>logger = logging.getLogger(__name__)

The way I build extension modules is to split the work between python code and
C++ code. You only need the C++ for the difficult stuff.

For the "foo" module I would create a foo.py that imports a _foo extension.

Then the answer to your question is simple. Do it in python and passt logger 
into the
C++ module.

Next I would never code directly against the C API. Its a pain to use and get 
right, get
the ref counts wrong and you get memory leaks of worse crash python.

There are lots of good alternative. Personal I use PyCXX, but then I'm the 
maintainer :-)
https://sourceforge.net/projects/cxx/ 

Barry


> 
> This is straightforward, except that I cannot figure out how to retrieve
> the __name__.  I can get it from the module object with
> PyModule_GetName, but that requires that I have a reference to the
> module object in order to do so.
> 
> I would have thought that this would be easy for a module function to
> access, but I haven't been able to find any API which does this.
> (Module functions get NULL as their 'self' argument.)
> 
> Any pointers appreciated.  Thanks!
> 
> -- 
> 
> Ian Pilcher [email protected]
>  "I grew up before Mark Zuckerberg invented friendship" 
> 
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 

-- 
https://mail.python.org/mailman/listinfo/python-list


Strange tab completion oddity with enums?

2019-10-06 Thread Chris Angelico
I'm not sure what's going on here, and it's probably not actually
enum-specific, but that's where I saw it.

If you create a plain class and have an attribute with an annotation,
you can see that:

>>> class Foo:
... spam: "ham" = 1
...
>>> Foo.__a
Foo.__abstractmethods__  Foo.__annotations__
>>> Foo.__annotations__
{'spam': 'ham'}

Note that __annotations__ shows up when tab-completing "__a".

Now consider an enumeration:

>>> from enum import Flag, auto
>>> class Bar(Flag):
... quux: "asdf" = auto()
...
>>> Bar.__
Bar.__abstractmethods__  Bar.__getattr__( Bar.__ne__(
Bar.__base__(Bar.__getattribute__(Bar.__new__(
Bar.__bases__Bar.__getitem__( Bar.__prepare__(
Bar.__basicsize__Bar.__gt__(  Bar.__qualname__
Bar.__bool__(Bar.__hash__(Bar.__reduce__(
Bar.__call__(Bar.__init__(Bar.__reduce_ex__(
Bar.__class__(   Bar.__init_subclass__(   Bar.__repr__(
Bar.__contains__(Bar.__instancecheck__(   Bar.__reversed__(
Bar.__delattr__( Bar.__itemsize__ Bar.__setattr__(
Bar.__dict__ Bar.__iter__(Bar.__sizeof__(
Bar.__dictoffset__   Bar.__le__(  Bar.__str__(
Bar.__dir__( Bar.__len__( Bar.__subclasscheck__(
Bar.__doc__  Bar.__lt__(  Bar.__subclasses__(
Bar.__eq__(  Bar.__members__  Bar.__subclasshook__(
Bar.__flags__Bar.__module__   Bar.__text_signature__
Bar.__format__(  Bar.__mro__  Bar.__weakrefoffset__
Bar.__ge__(  Bar.__name__
>>> Bar.__annotations__
{'quux': 'asdf'}

Double-tabbing "__" shows everything but, and double-tabbing "__ann"
has nothing... but the attribute is most definitely there.

Perhaps notable is dir():

>>> dir(Foo)
['__annotations__', '__class__', '__delattr__', '__dict__', '__dir__',
'__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__gt__', '__hash__', '__init__', '__init_subclass__', '__le__',
'__lt__', '__module__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', '__weakref__', 'spam']
>>> dir(Bar)
['__class__', '__doc__', '__members__', '__module__', 'quux']

But that's not the whole story, since tab completing "Bar.__" will
still show "__class__" and "__init__" that aren't in dir().

Tested with the default REPL CPython 3.6, 3.7, 3.8, and 3.9. Tested
also in IDLE on 3.9 but tab completion of dunders behaves differently
there (it ONLY seems to want to tab complete __class__, for some
reason) so it's not comparable.

What's actually going on here?

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Mouse control

2019-10-06 Thread Alexander Vergun

Hello all,

I am coding a voice assistant under Python 3.7, Windows 7. I am using 
PYcharm and libraries such as PYSimpleGUI, mouse, keyboard etc.  
Everything works except for the mouse control and probably keyboard, the 
problem is following, when I run the script under PYcharm, the script 
can control mouse only within PYcharm editor, but when the other window 
is on the top, the script cannot control the mouse, and the same happens 
when the script runs under the Python console. There is only one way to 
make script control the mouse - I have to manually click on the window 
which was opened by the script only then the script controls the mouse. 
I tried to call win32gui.SetCapture without any effect.


Does anyone have an idea how to handle this? Thank you very much,

Alexander

--
Ing. Alexander Vergun
tel.: +421905167381

--
https://mail.python.org/mailman/listinfo/python-list


Re: Get __name__ in C extension module

2019-10-06 Thread Ian Pilcher

On 10/6/19 11:55 AM, MRAB wrote:
Don't you already have the module's name? You have to specify it in the 
PyModuleDef struct that you pass to PyModule_Create.


I do.  Perhaps I'm trying to be too Pythonic, but there's so much advice
out there about using getLogger(__name__) in Python code, rather than
hardcoding the name.  I've been trying to follow that pattern in my
extension module.

Calling PyModule_Create returns a reference to the module, and you can 
get its namespace dict with PyModule_GetDict(...).


Right.  I have that in my module init function, but how do I access that
later in one of my extension functions?  The only thing I can think of
would be to save it in a static variable, but static variables seem to
be a no-no in extensions.

--

Ian Pilcher [email protected]
 "I grew up before Mark Zuckerberg invented friendship" 

--
https://mail.python.org/mailman/listinfo/python-list


Re: Get __name__ in C extension module

2019-10-06 Thread Ian Pilcher

On 10/6/19 12:55 PM, Barry Scott wrote:

Then the answer to your question is simple. Do it in python and passt
logger into the C++ module.


Funny thing, that's exactly where I started this journey.  I couldn't
figure out how to get the logging.Logger type object, so that I could
use a "O!" format string unit.  This led me to read a bit more about
the logging framework, which led me to the advice to get loggers by
name, rather than passing them around, etc., etc.


Next I would never code directly against the C API. Its a pain to use
and get right, get the ref counts wrong and you get memory leaks of
worse crash python.


Well, I like driving cars with manual transmissions, so ...

--

Ian Pilcher [email protected]
 "I grew up before Mark Zuckerberg invented friendship" 

--
https://mail.python.org/mailman/listinfo/python-list


Re: Get __name__ in C extension module

2019-10-06 Thread MRAB

On 2019-10-07 00:38, Ian Pilcher wrote:

On 10/6/19 11:55 AM, MRAB wrote:
> Don't you already have the module's name? You have to specify it in the 
> PyModuleDef struct that you pass to PyModule_Create.


I do.  Perhaps I'm trying to be too Pythonic, but there's so much advice
out there about using getLogger(__name__) in Python code, rather than
hardcoding the name.  I've been trying to follow that pattern in my
extension module.

> Calling PyModule_Create returns a reference to the module, and you can 
> get its namespace dict with PyModule_GetDict(...).


Right.  I have that in my module init function, but how do I access that
later in one of my extension functions?  The only thing I can think of
would be to save it in a static variable, but static variables seem to
be a no-no in extensions.

Can an extension module be instantiated twice? If so, would they share 
static variables? If they don't, then it's not a problem.


--
https://mail.python.org/mailman/listinfo/python-list