Re: Strange tab completion oddity with enums?

2019-10-07 Thread Peter Otten
Chris Angelico wrote:

> 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?

With Python 3.7:

>>> from enum import Flag
>>> import inspect
>>> print(inspect.getsource(Flag.__dir__))
def __dir__(self):
added_behavior = [
m
for cls in self.__class__.mro()
for m in cls.__dict__
if m[0] != '_' and m not in self._member_map_
]
return (['__class__', '__doc__', '__module__'] + added_behavior)

Looks like everything starting with an underscore (except class, doc, and 
module) is suppressed, probably to suppress some noise...

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


Re: Get __name__ in C extension module

2019-10-07 Thread Barry Scott



> On 7 Oct 2019, at 04:34, MRAB  wrote:
> 
> 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.

You could have 2 modules that share code.
But you cannot init the same module twice.

Barry


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


Re: Get __name__ in C extension module

2019-10-07 Thread Barry Scott



> On 7 Oct 2019, at 00:44, Ian Pilcher  wrote:
> 
> 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.


I meant pass in the specific named logger that you C code will use.

> 
>> 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 ...

You are clearly aware of the risks.

Barry


> 
> -- 
> 
> Ian Pilcher arequip...@gmail.com
>  "I grew up before Mark Zuckerberg invented friendship" 
> 
> 

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


Re: Strange tab completion oddity with enums?

2019-10-07 Thread Chris Angelico
On Mon, Oct 7, 2019 at 6:00 PM Peter Otten <__pete...@web.de> wrote:
>
> Chris Angelico wrote:
>
> > 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?
>
> With Python 3.7:
>
> >>> from enum import Flag
> >>> import inspect
> >>> print(inspect.getsource(Flag.__dir__))
> def __dir__(self):
> added_behavior = [
> m
> for cls in self.__class__.mro()
> for m in cls.__dict__
> if m[0] != '_' and m not in self._member_map_
> ]
> return (['__class__', '__doc__', '__module__'] + added_behavior)
>
> Looks like everything starting with an underscore (except class, doc, and
> module) is suppressed, probably to suppress some noise...
>

That's why dir() shows what it does, but tab completion seems to have
some other source, as it's able to find a lot of other attributes but
not __annotations__. Very strange.

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


Re: Strange tab completion oddity with enums?

2019-10-07 Thread Peter Otten
Chris Angelico wrote:

>> Looks like everything starting with an underscore (except class, doc, and
>> module) is suppressed, probably to suppress some noise...
>>
> 
> That's why dir() shows what it does, but tab completion seems to have
> some other source, as it's able to find a lot of other attributes but
> not __annotations__. Very strange.

If I'm reading rlcompleter.py correctly 

words = set(dir(thisobject))
words.discard("__builtins__")

if hasattr(thisobject, '__class__'):
words.add('__class__')
words.update(get_class_members(thisobject.__class__))

def get_class_members(klass):
ret = dir(klass)
if hasattr(klass,'__bases__'):
for base in klass.__bases__:
ret = ret + get_class_members(base)
return ret

it implements its own way to walk the class tree, and type being the base 
class of EnumMeta those additional attributes should be the result of

dir(type)

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


python -m pip install and pip install

2019-10-07 Thread Hongyi Zhao
Hi,

What's the diff:

python -m pip install mod
and
pip install mod 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python -m pip install and pip install

2019-10-07 Thread Chris Angelico
On Mon, Oct 7, 2019 at 10:21 PM Hongyi Zhao  wrote:
>
> Hi,
>
> What's the diff:
>
> python -m pip install mod
> and
> pip install mod

I presume from your series of posts that you either do not have access
to a web browser, or do not trust search engines, because all of these
questions can be answered easily and simply by a quick search of
Google/DuckDuckGo/Bing/etc/etc/etc. Please, make some effort to
research things yourself before asking questions.

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


Re: Get __name__ in C extension module

2019-10-07 Thread Ian Pilcher

On 10/7/19 2:09 AM, Barry Scott wrote:

I meant pass in the specific named logger that you C code will use.


Right, but I'm assuming that your C/C++ code will receive the logger
object by calling PyArg_ParseTuple (or similar), and I further assume
that you want to validate that the object actually is a logger.  Doing
that validation (by using an "O!" unit in the PyArg_ParseTuple format
string) requires access to the logging.Logger type object, and I was
unable to find a way to access that object by name.

--

Ian Pilcher arequip...@gmail.com
 "I grew up before Mark Zuckerberg invented friendship" 

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


Re: Access violation in Python garbage collector (visit_decref) - how to debug?

2019-10-07 Thread Geoff Bache
I've so far only tried within my application, but I'm aware it would be
easier if I could reproduce it outside. Even simplifying the context within
the application has proved difficult though, so I suspect this will be
hard. But I can try a bit more...

The file isn't "large" by production standards, only by test data
standards. It's about 500kb and not at all deeply nested, basically a long
list of dictionaries. But I don't seem to be able to reduce it further
either.

/Geoff

On Fri, Oct 4, 2019 at 9:53 PM Chris Angelico  wrote:

> On Sat, Oct 5, 2019 at 5:38 AM Geoff Bache  wrote:
> >
> > Hi all,
> >
> > We are running Python embedded in our C++ product and are now
> experiencing
> > crashes (access violation reading 0xff on Windows) in the Python
> > garbage collector.
> >
> > We got this on Python 3.6.4 originally, but I can reproduce it with both
> > Python 3.6.8 and Python 3.7.4.
> >
> > The chances of producing a minimal example that reproduces it reliably
> are
> > currently small I would say. All attempts to simplify the set up seem to
> > cause the problem to go away.
> > Indeed I can only reproduce it by sending a fairly large amount of data 2
> > or 3 times to our server - sending either half of the data does not
> > reproduce it.
>
> Have you tried to reproduce the issue outside of your application?
> Even if it means creating a gigantic Python script with a whopping
> triple-quoted string for the input data, it'd be helpful to try this.
> If you CAN repro the problem, it'd be way easier to diagnose (since we
> don't need your code, just your test case); and if you CAN'T, it may
> mean an issue with the embedding aspects.
>
> What's your definition of "fairly large" here? How many
> kilobytes/megabytes/gigabytes, and how deeply nested is the JSON
> object?
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Access violation in Python garbage collector (visit_decref) - how to debug?

2019-10-07 Thread Geoff Bache
It's possible. Our embedding code is fairly simple and we've tried to
encapsulate all the DECREFing in resource objects, i.e. that do DECREF in
their destructor when they go out of scope. We hoped this would minimise
this sort of problem.
The C++ calling code essentially tries to call progwrapper.prog with a
dictionary as arguments, and looks like

GilStateHolder pyStateHolder;
PyRefHolder module(PyImport_ImportModule("progwrapper"));

  if (module._ref)
  {
PyXRefHolder func(PyObject_GetAttrString(module._ref, "prog"));
PyXRefHolder pyArgs(PyTuple_New(1));
PyXRefHolder pyDict(convertDictForPython(dictIn));
PyTuple_SetItem(pyArgs._ref, 0, pyDict._ref);
PyRefHolder pyRet(PyObject_CallObject(func._ref, pyArgs._ref));

if (pyRet._ref != NULL)
{
  addPythonValuesToDict(pyRet._ref, theDict);
  ...

where GilStateHolder, PyRefHolder etc are such resource objects as
described above, wrapping round the PyObject pointers etc.

/Geoff

On Fri, Oct 4, 2019 at 9:56 PM MRAB  wrote:

> On 2019-10-04 20:32, Geoff Bache wrote:
> > Hi all,
> >
> > We are running Python embedded in our C++ product and are now
> experiencing
> > crashes (access violation reading 0xff on Windows) in the Python
> > garbage collector.
> >
> > We got this on Python 3.6.4 originally, but I can reproduce it with both
> > Python 3.6.8 and Python 3.7.4.
> >
> > The chances of producing a minimal example that reproduces it reliably
> are
> > currently small I would say. All attempts to simplify the set up seem to
> > cause the problem to go away.
> > Indeed I can only reproduce it by sending a fairly large amount of data 2
> > or 3 times to our server - sending either half of the data does not
> > reproduce it.
> >
> [snip]
> In my experience, it's most likely caused by incorrect refcounting,
> DECREFing too many times.
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Access violation in Python garbage collector (visit_decref) - how to debug?

2019-10-07 Thread Geoff Bache
Yes, this is hard, that's why I'm here :)

I've enabled the equivalent tools to valgrind in Visual Studio, and tried
setting PYTHONMALLOC=debug, but neither of those seem to be showing
anything either. I don't really know what else to try in this direction.

/Geoff

On Sat, Oct 5, 2019 at 7:22 AM dieter  wrote:

> Geoff Bache  writes:
> > ...
> > We are running Python embedded in our C++ product and are now
> experiencing
> > crashes (access violation reading 0xff on Windows) in the Python
> > garbage collector.
>
> Errors like this are very difficult to analyse. The main reason:
> the memory corruption is likely far away from the point when
> it is finally detected (by an access violation in your case).
>
> Python can be built in a special way to add marks to
> its allocated memory blocks and verify their validity.
> This increases the chance to detect a memory corruption earlier
> and thereby facilitates the detection of the root cause.
>
> There are tools for the analysis of memory management problems
> (e.g. "valgrind", though this may be for Linux). In my
> experience, even with those tools, the analysis is very difficult.
>
> I have several times successfully analysed memory corruption
> problems. In those cases, I have been lucky that the corruption
> was reproducible and affected typically the same address.
> Thus, I could put a (hardware) memory breakpoint at this address
> stopping the program as soon as this address was written and
> then analyse the state in the debugger. This way, I could detect
> precisely which code caused the corruption. However,
> this was quite a long time ago; nowadays, modern operating systems
> employ address randomization thus reducing significantly that
> the corruption affects the same address (which may mean that
> you need to deactivate address randomization to get a better chance
> for this kind of analysis.
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Formatting floating point

2019-10-07 Thread boffi
DL Neil  writes:

> Agreed: there's ton(ne)s of information 'out there', much of it old,
> eg Python2, "formatter" (deprecated since v3.4)

?

are you referring to the `string.Formatter`[*] class?

$ python
Python 3.7.4 (default, Aug 13 2019, 20:35:49) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from string import Formatter
>>> 

if not, what else?

g

[*] https://docs.python.org/2/library/string.html#string.Formatter
https://docs.python.org/3.7/library/string.html#string.Formatter
-- 
https://mail.python.org/mailman/listinfo/python-list


sqlalchemy & #temp tables

2019-10-07 Thread Albert-Jan Roskam
Hi,

I am using sqlalchemy (SA) to access a MS SQL Server database (python 3.5, Win 
10). I would like to use a temporary table (preferably #local, but ##global 
would also be an option) to store results of a time-consuming query. In other 
queries I'd like to access the temporary table again in various places in my 
Flask app. How do I do that, given that SA closes the connection after each 
request?

I can do:
with engine.connect() as con:
con.execute('select * into #tmp from tbl')
con.execute('select  * from #tmp')

... but that's limited to the scope of the context manager.

Oh, I don't have rights to create a 'real' table. :-(

Thanks!

Albert-Jan

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


Re: Access violation in Python garbage collector (visit_decref) - how to debug?

2019-10-07 Thread MRAB

On 2019-10-07 08:04, Geoff Bache wrote:
It's possible. Our embedding code is fairly simple and we've tried to 
encapsulate all the DECREFing in resource objects, i.e. that do DECREF 
in their destructor when they go out of scope. We hoped this would 
minimise this sort of problem.
The C++ calling code essentially tries to call progwrapper.prog with a 
dictionary as arguments, and looks like


GilStateHolder pyStateHolder;
PyRefHolder module(PyImport_ImportModule("progwrapper"));

  if (module._ref)
  {
    PyXRefHolder func(PyObject_GetAttrString(module._ref, "prog"));
    PyXRefHolder pyArgs(PyTuple_New(1));
    PyXRefHolder pyDict(convertDictForPython(dictIn));
    PyTuple_SetItem(pyArgs._ref, 0, pyDict._ref);


|Do you DECREF pyDict._ref later on? I ask because PyTuple_SetItem 
steals a reference.|


|Many other functions that "store" an object, such as PyList_Append, 
INCREF to retain the stored object, but PyTuple_SetItem doesn't.|


||


    PyRefHolder pyRet(PyObject_CallObject(func._ref, pyArgs._ref));

    if (pyRet._ref != NULL)
    {
      addPythonValuesToDict(pyRet._ref, theDict);
  ...

where GilStateHolder, PyRefHolder etc are such resource objects as 
described above, wrapping round the PyObject pointers etc.


[snip]


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


Re: pre-edit stuff persists in a reloaded a module

2019-10-07 Thread Friedrich Rentsch




On 10/5/19 1:48 PM, Friedrich Rentsch wrote:

Hi all,

Python 2.7. I habitually work interactively in an Idle window. 
Occasionally I correct code, reload and find that edits fail to load. 
I understand that reloading is not guaranteed to reload everything, 
but I don't understand the exact mechanism and would appreciate some 
illumination. Right now I am totally bewildered, having deleted and 
garbage collected a module and an object, reloaded the module and 
remade the object and when I inspect the corrected source 
(inspect.getsource (Object.run)) I see the uncorrected source, which 
isn't even on the disk anymore. The command 'reload' correctly 
displays the name of the source, ending '.py', indicating that it 
recognizes the source being newer than the compile ending '.pyc'. 
After the reload, the pyc-file is newer, indicating that it has been 
recompiled. But the runtime error persist. So the recompile must have 
used the uncorrected old code. I could kill python with signal 15, but 
would prefer a targeted purge that doesn't wipe clean my Idle 
workbench. (I know I should upgrade to version 3. I will as soon as I 
get around to it. Hopefully that will fix the problem.)


Thanks for comments

Frederic

Closing the thread with thanks to all who responded, offering excellent 
advice.


Frederic

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


Re: Formatting floating point

2019-10-07 Thread DL Neil via Python-list

On 8/10/19 4:04 AM, boffi wrote:

DL Neil  writes:


Agreed: there's ton(ne)s of information 'out there', much of it old,
eg Python2, "formatter" (deprecated since v3.4)

?
are you referring to the `string.Formatter`[*] class?
 $ python
 Python 3.7.4 (default, Aug 13 2019, 20:35:49)
 [GCC 7.3.0] :: Anaconda, Inc. on linux
 Type "help", "copyright", "credits" or "license" for more information.
 >>> from string import Formatter
if not, what else?
g
[*] https://docs.python.org/2/library/string.html#string.Formatter
 https://docs.python.org/3.7/library/string.html#string.Formatter



As mentioned, there is a wealth of information, but I must admit it took 
a moment-or-two before I re-located the 'official' reference to f-strings:


2.4.3. Formatted string literals 
https://docs.python.org/3/reference/lexical_analysis.html#f-strings

(and please 'read-around' this section for more valuable data!)

FYI the original PEP is at https://www.python.org/dev/peps/pep-0498/


It is worth reading about string formatters (per (your) ref, above), 
because many elements of the 'Formatting mini-language' are also 
available within f-strings!



As you have probably observed, there are now (v3.6+) several methods 
which can be applied to the formatting of data. Each has its advantages, 
and whilst I have a preference for, and thus recommended, WRITING code 
to use f-strings, it is also (likely to be) important that we READ and 
comprehend the older/alternatives!



NB politesse suggests that I should apologise, but I no-longer wish to 
work with Python2; hence only mentioning Py3 'here'.

--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: Mouse control

2019-10-07 Thread David L Neil via Python-list

On 7/10/19 4:11 AM, Alexander Vergun wrote:
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,



Are you asking to be able to start a Python application in one window, 
which will control the mouse and keyboard in other concurrent 
applications' windows?


I don't use MS-Windows, but don't think this is possible (under any OpSys).

As you say "and probably keyboard": imagine typing and having the text 
appear in every open window/application? If you don't want that, how do 
you indicate which one should accept the input? Answer: "focus" = the 
'target' for any typing is the application/window which currently has focus.


Extending those thoughts to the mouse doesn't quite ring-true, because 
I'm currently in 'this' window writing an email message, yet the mouse 
will cheerfully scroll the window 'below'. Is that the sort of action 
you wish?


In my experience, the Python application, including "mouse", only 
operates in "user space" and within the one window. If we can extend 
Python's power and dominion, I'll be interested to learn...


Meantime, there are demo snippets which follow the mouse, printing its 
coordinates. Using one of those, what happens to the coordinates when 
the mouse is moved outside of the Python application's window?



BTW you may like to take a look at PyAutoGUI, if only because it has 
been more recently updated. It may also help by bringing 'everything' 
under one import rather than the multiple packages (listed above):

https://pypi.org/project/PyAutoGUI/
https://pyautogui.readthedocs.io/en/latest/mouse.html
https://automatetheboringstuff.com/chapter18/
--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: python -m pip install and pip install

2019-10-07 Thread Pankaj Jangid
Hongyi Zhao  writes:

> Hi,
>
> What's the diff:
>
> python -m pip install mod
> and
> pip install mod 

A very good use-case is when you have both, python2 and python3
installed.

python2 -m pip install mod

and

python3 -m pip install mod

will install the package in the corresponding PYTHONPATH.

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


Re: python -m pip install and pip install

2019-10-07 Thread Hongyi Zhao
On Tue, 08 Oct 2019 06:28:05 +0530, Pankaj Jangid wrote:

> A very good use-case is when you have both, python2 and python3
> installed.
> 
> python2 -m pip install mod

If so, why not just:

pip2 install mod

> 
> and
> 
> python3 -m pip install mod

and using:

pip3 install mod

> 
> will install the package in the corresponding PYTHONPATH.

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


How to handle '-' in the 'from' part in a "from import" statement?

2019-10-07 Thread jfong
For example:
from my-dir import test

I know it can be solved by renaming, but any alternative?

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


Re: How to handle '-' in the 'from' part in a "from import" statement?

2019-10-07 Thread DL Neil via Python-list

On 8/10/19 3:45 PM, jf...@ms4.hinet.net wrote:

For example:
from my-dir import test

I know it can be solved by renaming, but any alternative?



The manual is your friend:
- import
- importlib

(the latter allows modules to be identified by strings)

However, Pythons has naming rules. If you try to get around them, sooner 
or later you'll 'forget' and trip yourself up. Recommend your first idea!

--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to handle '-' in the 'from' part in a "from import" statement?

2019-10-07 Thread jfong
DL Neil於 2019年10月8日星期二 UTC+8上午11時02分20秒寫道:
> On 8/10/19 3:45 PM, jf...@ms4.hinet.net wrote:
> > For example:
> > from my-dir import test
> > 
> > I know it can be solved by renaming, but any alternative?
> 
> 
> The manual is your friend:
> - import
> - importlib
> 
> (the latter allows modules to be identified by strings)
> 
> However, Pythons has naming rules. If you try to get around them, sooner 
> or later you'll 'forget' and trip yourself up. Recommend your first idea!
> -- 
> Regards =dn

Yes, I had noticed the naming rules. Thank you.

But most of the download from Github has a directory named '-master' which 
causes a trouble sometimes.

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


Re: python -m pip install and pip install

2019-10-07 Thread Cameron Simpson

On 08Oct2019 02:49, Hongyi Zhao  wrote:

On Tue, 08 Oct 2019 06:28:05 +0530, Pankaj Jangid wrote:

A very good use-case is when you have both, python2 and python3
installed.
python2 -m pip install mod
python3 -m pip install mod
will install the package in the corresponding PYTHONPATH.



If so, why not just:
pip2 install mod
and using:
pip3 install mod


Because of the slight disconnect between "pip2" and "python2", etc. Do 
you _know_ they both use the same Python install? With "pythonX -m pip" 
you're using the same python executable which will be accessing what you 
just installed.


It isn't a deal breaker, but preferred: one less moving part.

Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: Access violation in Python garbage collector (visit_decref) - how to debug?

2019-10-07 Thread dieter
Geoff Bache  writes:
> Yes, this is hard, that's why I'm here :)
>
> I've enabled the equivalent tools to valgrind in Visual Studio, and tried
> setting PYTHONMALLOC=debug, but neither of those seem to be showing
> anything either. I don't really know what else to try in this direction.

It likely is too hard to be solved remotely (in this list).

When I had analysed a similar problem (a long time ago),
I had the chance that the problem was quite easily reproducible --
is this the case for you?

This allowed me to run the program under debugger ("gdb") control
with the debugger getting control as soon as the access violation
occured. The "gdb" allows debugging both at C as well as at
machine code level. Using machine code level debugging allowed
me to determine the address where the wrong pointer came from.
Setting a (hardware) watchpoint for this address stopped the
program when the wrong pointer was written to this address.
This gave me two important pieces of information: which code
writes the wrong pointer and what was in the memory region before the memory
corruption (which type of Python object was involved).

In my case, reproducibility, machine level debugging, hardware write
watchpoints and a detailed knowledge of Python's runtime data structures have
been necessary to resolve the problem.



> On Sat, Oct 5, 2019 at 7:22 AM dieter  wrote:
>
>> Geoff Bache  writes:
>> > ...
>> > We are running Python embedded in our C++ product and are now
>> experiencing
>> > crashes (access violation reading 0xff on Windows) in the Python
>> > garbage collector.
>>
>> Errors like this are very difficult to analyse. The main reason:
>> the memory corruption is likely far away from the point when
>> it is finally detected (by an access violation in your case).
>>
>> Python can be built in a special way to add marks to
>> its allocated memory blocks and verify their validity.
>> This increases the chance to detect a memory corruption earlier
>> and thereby facilitates the detection of the root cause.
>>
>> There are tools for the analysis of memory management problems
>> (e.g. "valgrind", though this may be for Linux). In my
>> experience, even with those tools, the analysis is very difficult.
>>
>> I have several times successfully analysed memory corruption
>> problems. In those cases, I have been lucky that the corruption
>> was reproducible and affected typically the same address.
>> Thus, I could put a (hardware) memory breakpoint at this address
>> stopping the program as soon as this address was written and
>> then analyse the state in the debugger. This way, I could detect
>> precisely which code caused the corruption. However,
>> this was quite a long time ago; nowadays, modern operating systems
>> employ address randomization thus reducing significantly that
>> the corruption affects the same address (which may mean that
>> you need to deactivate address randomization to get a better chance
>> for this kind of analysis.
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>

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


Re: How to handle '-' in the 'from' part in a "from import" statement?

2019-10-07 Thread dieter
jf...@ms4.hinet.net writes:
> ...
> But most of the download from Github has a directory named '-master' 
> which causes a trouble sometimes.

Those are likely not meant to be imported directly.

Typically, you have a "setup" step which installs (in some way)
a "distribution". This step usually ensures that you can use
"normal" Python import syntax to access all associated packages.

The "setup" step is typically performed with
"python setup.py develop" or "python setup.py install" --
with the "distribution" providing the "setup.py".

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


Re: sqlalchemy & #temp tables

2019-10-07 Thread Frank Millman

On 2019-10-07 5:30 PM, Albert-Jan Roskam wrote:

Hi,

I am using sqlalchemy (SA) to access a MS SQL Server database (python 3.5, Win 
10). I would like to use a temporary table (preferably #local, but ##global 
would also be an option) to store results of a time-consuming query. In other 
queries I'd like to access the temporary table again in various places in my 
Flask app. How do I do that, given that SA closes the connection after each 
request?

I can do:
with engine.connect() as con:
 con.execute('select * into #tmp from tbl')
 con.execute('select  * from #tmp')

... but that's limited to the scope of the context manager.

Oh, I don't have rights to create a 'real' table. :-(

Thanks!

Albert-Jan



This does not answer your question directly, but FWIW this is what I do.

I do not use SA, but I have written my app to support Sql Server, 
PostgreSQL and sqlite3 as backend databases. However, no matter which 
one is in use, I also use sqlite3 as an in-memory database to store 
temporary information. It took me a little while to get it all working 
smoothly, but now it works well.


Of course this may not work for you if you have a large volume of temp 
data, but it may be worth trying.


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


Re: Strange tab completion oddity with enums?

2019-10-07 Thread Piet van Oostrum
Chris Angelico  writes:

> 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'}

Also strange:

It shows Foo.__abstractmethods__ but there is no such attribute.
What's going on?

>>> Foo.__abstractmethods__
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: __abstractmethods__

-- 
Piet van Oostrum 
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list