Re: Converting program from shell to tkinter

2020-03-22 Thread Christian Gollwitzer

Hi Randall,

I assume you are asking about the Python programming language 
(tkinter??). This group discusses the Tcl programming language, from 
which Tk originates. See 2 answers below.

Xpost and Fup2 c.l.p

Christian

Am 22.03.20 um 00:59 schrieb Randall Wert:

I am trying to enhance a shell program -- you know, the kind that communicates with the 
user through simple lines of text, using print() and input() statements -- by adding a 
tkinter interface. I have successfully completed three projects using tkinter in the 
past, but this one is proving more difficult. The essence of the problem is, I want the 
program to still have the same control flow as in the "shell" program. It 
should issue messages to the user and receive their input. But tkinter seems to be purely 
designed for event-driven programs (responding to button clicks, for example).

To be more specific: It is a hangman game. The user enters different types of 
input as the program runs (choice of difficulty level, letters (guesses), Y/N 
for playing another game, etc.). I would like to be able to use a single user 
entry field for all of those purposes, and accept input from it when the user 
presses Enter, instead of having a different button for each type of user input.

I hope I am explaining it clearly. I would appreciate any tips on converting to 
a tkinter interface while maintaining the same program flow.



You must convert the program into a state machine. Luckily, both Tcl and 
Python can help you with this, in the form of a coroutine. For Tcl, take 
a look to this page


https://wiki.tcl-lang.org/page/Coroutines+for+event-based+programming

For Python, look up generators and google "asyncio tkinter".

Christian

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


Re: Intermittent bug with asyncio and MS Edge

2020-03-22 Thread Frank Millman

On 2020-03-21 8:04 PM, Barry Scott wrote:




On 21 Mar 2020, at 13:43, Frank Millman  wrote:

Hi all

I have a strange intermittent bug.

The role-players -
asyncio on Python 3.8 running on Windows 10
Microsoft Edge running as a browser on the same machine

The bug does not occur with Python 3.7.
It does not occur with Chrome or Firefox.
It does not occur when MS Edge connects to another host on the network, running 
the same Python program (Python 3.8 on Fedora 31).

The symptoms -
On receiving a connection, I send an HTML page to the browser,
which has 20 lines like this -



...

Intermittently, one or other of the script files is not received by MS Edge.


[...]
>> I don't know whether the problem lies with Python or MS Edge, but as 
it does not happen with Python 3.7, I am suspecting that something 
changed in 3.8 which does not match MS Edge's expectations.


I'd look at the network traffic with wireshark to see if there is anything 
different between edge and the other browsers.



You are leading me into deep waters here :-)  I have never used 
Wireshark before. I have now downloaded it and am running it - it 
generates a *lot* of data, most of which I do not understand yet!


One thing immediately stands out. When I run it with MS Edge and 
Python3.8, it shows a lot of lines highlighted in red, with the symbols 
[RST,ACK]. They do not appear when running Chrome, and they do not 
appear when running Python3.7.


I have another data point. I tried putting an asyncio.sleep() after 
sending each file. A value of 0.01 made no difference, but a value of 
0.1 makes the problem go away.


I will keep digging, but I thought I would post this information now in 
case it helps with diagnosis.


Frank

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


Re: Intermittent bug with asyncio and MS Edge

2020-03-22 Thread Chris Angelico
On Sun, Mar 22, 2020 at 6:58 PM Frank Millman  wrote:
> > I'd look at the network traffic with wireshark to see if there is anything 
> > different between edge and the other browsers.
> >
>
> You are leading me into deep waters here :-)  I have never used
> Wireshark before. I have now downloaded it and am running it - it
> generates a *lot* of data, most of which I do not understand yet!
>
> One thing immediately stands out. When I run it with MS Edge and
> Python3.8, it shows a lot of lines highlighted in red, with the symbols
> [RST,ACK]. They do not appear when running Chrome, and they do not
> appear when running Python3.7.

Interesting. RST means "Reset" and is sent when the connection is
closed. Which direction were these packets sent (Edge to Python or
Python to Edge)? You can tell by the source and destination ports -
one of them is going to be the port Python is listening on (eg 80 or
443), so if the destination port is 80, it's being sent *to* Python,
and if the source port is 80, it's being sent *from* Python.

> I have another data point. I tried putting an asyncio.sleep() after
> sending each file. A value of 0.01 made no difference, but a value of
> 0.1 makes the problem go away.

Interesting also.

Can you recreate the problem without Edge? It sounds like something's
going on with concurrent transfers, so it'd be awesome if you can
replace Edge with another Python program, and then post both programs.

Also of interest: Does the problem go away if you change "Connection:
Keep-Alive" to "Connection: Close" in your headers?

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


Re: Python 3.8.2 on MacOSX Sierra?

2020-03-22 Thread Barry Scott



> On 22 Mar 2020, at 04:47, Greg Ewing  wrote:
> 
> I'm trying to compile a framework build of Python 3.8.2 on
> MacOSX 10.12.6 (Sierra).

I guess you are using an old Mac that will not update to something newer?

> I'm getting:
> 
> ./Modules/posixmodule.c:4696:12: warning: 'utimensat' is only available on 
> macOS
>  10.13 or newer [-Wunguarded-availability-new]
>return utimensat(dir_fd, path, time, flags);
> 
> ./Modules/posixmodule.c:4721:12: warning: 'futimens' is only available on 
> macOS
>  10.13 or newer [-Wunguarded-availability-new]
>return futimens(fd, time);
> 
> And later when linking:
> 
> dyld: lazy symbol binding failed: Symbol not found: _utimensat
>  Referenced from: 
> /Local/Build/Python/Python-3.8.2/Python.framework/Versions/3.8/Python
>  Expected in: /usr/lib/libSystem.B.dylib
> 
> dyld: Symbol not found: _utimensat
>  Referenced from: 
> /Local/Build/Python/Python-3.8.2/Python.framework/Versions/3.8/Python
>  Expected in: /usr/lib/libSystem.B.dylib
> 
> make: *** [sharedinstall] Abort trap: 6
> 
> Am I out of luck? Is Python 3.8 only intended work on very recent
> versions of MacOSX?

if your XCode is new enough you might be able to build it with 

export MACOSX_DEPLOYMENT_TARGET=10.13

But I guess the result will not run.

Barry

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

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


Re: Intermittent bug with asyncio and MS Edge

2020-03-22 Thread Barry Scott



> On 22 Mar 2020, at 07:56, Frank Millman  wrote:
> 
> On 2020-03-21 8:04 PM, Barry Scott wrote:
>>> On 21 Mar 2020, at 13:43, Frank Millman  wrote:
>>> 
>>> Hi all
>>> 
>>> I have a strange intermittent bug.
>>> 
>>> The role-players -
>>>asyncio on Python 3.8 running on Windows 10
>>>Microsoft Edge running as a browser on the same machine
>>> 
>>> The bug does not occur with Python 3.7.
>>> It does not occur with Chrome or Firefox.
>>> It does not occur when MS Edge connects to another host on the network, 
>>> running the same Python program (Python 3.8 on Fedora 31).
>>> 
>>> The symptoms -
>>>On receiving a connection, I send an HTML page to the browser,
>>>which has 20 lines like this -
>>> 
>>>
>>>
>>>...
>>> 
>>> Intermittently, one or other of the script files is not received by MS Edge.
>>> 
> [...]
>>> >> I don't know whether the problem lies with Python or MS Edge, but as 
> it does not happen with Python 3.7, I am suspecting that something changed in 
> 3.8 which does not match MS Edge's expectations.
>> I'd look at the network traffic with wireshark to see if there is anything 
>> different between edge and the other browsers.
> 
> You are leading me into deep waters here :-)  I have never used Wireshark 
> before. I have now downloaded it and am running it - it generates a *lot* of 
> data, most of which I do not understand yet!

You can tell wireshark to only capture on one interface and to only capture 
packets for port 80.
(Captureing HTTPS means you cannot decode the packets without going deeper I 
recall)

Then you can tell wireshark to decode the captured data for http to drop a lot 
of the lower level details.


> 
> One thing immediately stands out. When I run it with MS Edge and Python3.8, 
> it shows a lot of lines highlighted in red, with the symbols [RST,ACK]. They 
> do not appear when running Chrome, and they do not appear when running 
> Python3.7.

As Chris said that should not happen.

> 
> I have another data point. I tried putting an asyncio.sleep() after sending 
> each file. A value of 0.01 made no difference, but a value of 0.1 makes the 
> problem go away.

What is the async wait to wait for the transmit buffers to drain?

> 
> I will keep digging, but I thought I would post this information now in case 
> it helps with diagnosis.
> 
> Frank
> -- 
> https://mail.python.org/mailman/listinfo/python-list 
> 
Barry
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3.8.2 on MacOSX Sierra?

2020-03-22 Thread Greg Ewing

On 22/03/20 9:55 pm, Barry Scott wrote:

I guess you are using an old Mac that will not update to something newer?


It's more that I don't *want* to update to something newer if I
don't have to. Also it seems very un-python-like to depend on a
specific OS version like this, which leads me to believe that
something is wrong somewhere.


if your XCode is new enough you might be able to build it with

export MACOSX_DEPLOYMENT_TARGET=10.13


Actually it seems to be more a matter of my XCode being *too*
new. I was using XCode 9, which turns out to contain an SDK
for 10.13. Which shouldn't matter, except that there seems to
be a bug in autoconf that makes it think some things are there
even though they're not:

https://bugs.python.org/issue31359

That's talking about a slightly different scenario (building
with one version and running on an older version) but I think
I'm being bitten by the same underlying issue.

I'm going to try again with XCode 8, which purportedly comes
with a 10.12 SDK, and see if that fixes it.

--
Greg

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


Re: Intermittent bug with asyncio and MS Edge

2020-03-22 Thread Frank Millman

On 2020-03-22 10:45 AM, Chris Angelico wrote:

On Sun, Mar 22, 2020 at 6:58 PM Frank Millman  wrote:

I'd look at the network traffic with wireshark to see if there is anything 
different between edge and the other browsers.



You are leading me into deep waters here :-)  I have never used
Wireshark before. I have now downloaded it and am running it - it
generates a *lot* of data, most of which I do not understand yet!

One thing immediately stands out. When I run it with MS Edge and
Python3.8, it shows a lot of lines highlighted in red, with the symbols
[RST,ACK]. They do not appear when running Chrome, and they do not
appear when running Python3.7.


Interesting. RST means "Reset" and is sent when the connection is
closed. Which direction were these packets sent (Edge to Python or
Python to Edge)? You can tell by the source and destination ports -
one of them is going to be the port Python is listening on (eg 80 or
443), so if the destination port is 80, it's being sent *to* Python,
and if the source port is 80, it's being sent *from* Python.



They are all being sent *from* Python *to* Edge.


I have another data point. I tried putting an asyncio.sleep() after
sending each file. A value of 0.01 made no difference, but a value of
0.1 makes the problem go away.


Interesting also.

Can you recreate the problem without Edge? It sounds like something's
going on with concurrent transfers, so it'd be awesome if you can
replace Edge with another Python program, and then post both programs.



Do you mean write a program that emulates a browser - make a connection, 
receive the HTML page, send a GET request for each file, and receive the 
results?


I will give it a go!


Also of interest: Does the problem go away if you change "Connection:
Keep-Alive" to "Connection: Close" in your headers?



Yes, the problem does go away.

Frank

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


Re: Intermittent bug with asyncio and MS Edge

2020-03-22 Thread Frank Millman

On 2020-03-22 11:00 AM, Barry Scott wrote:




On 22 Mar 2020, at 07:56, Frank Millman  wrote:

On 2020-03-21 8:04 PM, Barry Scott wrote:

I'd look at the network traffic with wireshark to see if there is anything 
different between edge and the other browsers.


You are leading me into deep waters here :-)  I have never used Wireshark 
before. I have now downloaded it and am running it - it generates a *lot* of 
data, most of which I do not understand yet!


You can tell wireshark to only capture on one interface and to only capture 
packets for port 80.
(Captureing HTTPS means you cannot decode the packets without going deeper I 
recall)

Then you can tell wireshark to decode the captured data for http to drop a lot 
of the lower level details.



Thanks. I am more or less doing that. Interestingly the [RST,ACK] 
messages appear on the tcp packets, so if I filter on http I do not see 
them.






One thing immediately stands out. When I run it with MS Edge and Python3.8, it 
shows a lot of lines highlighted in red, with the symbols [RST,ACK]. They do 
not appear when running Chrome, and they do not appear when running Python3.7.


As Chris said that should not happen.



As I replied to Chris, they appear in packets sent *from* Python *to* Edge.



I have another data point. I tried putting an asyncio.sleep() after sending 
each file. A value of 0.01 made no difference, but a value of 0.1 makes the 
problem go away.


What is the async wait to wait for the transmit buffers to drain?



Not sure what you are asking. I am just doing what it says in the docs -

=

write(data)
The method attempts to write the data to the underlying socket 
immediately. If that fails, the data is queued in an internal write 
buffer until it can be sent.


The method should be used along with the drain() method:

stream.write(data)
await stream.drain()

=

coroutine drain()
Wait until it is appropriate to resume writing to the stream. Example:

writer.write(data)
await writer.drain()
This is a flow control method that interacts with the underlying IO 
write buffer. When the size of the buffer reaches the high watermark, 
drain() blocks until the size of the buffer is drained down to the low 
watermark and writing can be resumed. When there is nothing to wait for, 
the drain() returns immediately.


=

Frank

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


Re: Intermittent bug with asyncio and MS Edge

2020-03-22 Thread Chris Angelico
On Sun, Mar 22, 2020 at 8:30 PM Frank Millman  wrote:
>
> On 2020-03-22 10:45 AM, Chris Angelico wrote:
> > On Sun, Mar 22, 2020 at 6:58 PM Frank Millman  wrote:
> >>> I'd look at the network traffic with wireshark to see if there is 
> >>> anything different between edge and the other browsers.
> >>>
> >>
> >> You are leading me into deep waters here :-)  I have never used
> >> Wireshark before. I have now downloaded it and am running it - it
> >> generates a *lot* of data, most of which I do not understand yet!
> >>
> >> One thing immediately stands out. When I run it with MS Edge and
> >> Python3.8, it shows a lot of lines highlighted in red, with the symbols
> >> [RST,ACK]. They do not appear when running Chrome, and they do not
> >> appear when running Python3.7.
> >
> > Interesting. RST means "Reset" and is sent when the connection is
> > closed. Which direction were these packets sent (Edge to Python or
> > Python to Edge)? You can tell by the source and destination ports -
> > one of them is going to be the port Python is listening on (eg 80 or
> > 443), so if the destination port is 80, it's being sent *to* Python,
> > and if the source port is 80, it's being sent *from* Python.
> >
>
> They are all being sent *from* Python *to* Edge.

Very interesting indeed. What that *might* mean is that Python is
misinterpreting something and then believing that the connection has
been closed, so it responds "Okay, I'm closing the connection". Or
possibly it sees some sort of error condition.

> >> I have another data point. I tried putting an asyncio.sleep() after
> >> sending each file. A value of 0.01 made no difference, but a value of
> >> 0.1 makes the problem go away.
> >
> > Interesting also.
> >
> > Can you recreate the problem without Edge? It sounds like something's
> > going on with concurrent transfers, so it'd be awesome if you can
> > replace Edge with another Python program, and then post both programs.
> >
>
> Do you mean write a program that emulates a browser - make a connection,
> receive the HTML page, send a GET request for each file, and receive the
> results?
>
> I will give it a go!

Yes - although the HTML page is most likely irrelevant. You could just
make a connection and then spam requests. Or make multiple
connections.

Actually, that's another thing worth checking. Is Edge using a single
connection for all requests, or separate connections for each request,
or something in between (eg a pool of four connections and spreading
requests between them)? You'll be able to recognize different
connections by the port numbers Edge uses, which are guaranteed unique
among concurrent connections, and are most likely to not be reused for
a while even if the other is closed.

> > Also of interest: Does the problem go away if you change "Connection:
> > Keep-Alive" to "Connection: Close" in your headers?
> >
>
> Yes, the problem does go away.
>

This makes me think that the answer to the previous question is going
to involve some connection reuse.

If you can recreate the problem with a single socket and multiple
requests, that would be extremely helpful. I also think it's highly
likely that this is the case.

My theory: Your program is sending a large amount of data to the lower
level API functions, which attempt to send a large amount of data to
the socket. At some point, something gets told "sorry, can't handle
all that data, hold some of it back" at a time when it's not prepared
to do so, and it misinterprets it as an error. This error results in
the connection being closed.

But that's just a theory.

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


Re: Intermittent bug with asyncio and MS Edge

2020-03-22 Thread Chris Angelico
On Sun, Mar 22, 2020 at 8:42 PM Frank Millman  wrote:
>
> On 2020-03-22 11:00 AM, Barry Scott wrote:
> >
> >
> >> On 22 Mar 2020, at 07:56, Frank Millman  wrote:
> >>
> >> On 2020-03-21 8:04 PM, Barry Scott wrote:
> >>> I'd look at the network traffic with wireshark to see if there is 
> >>> anything different between edge and the other browsers.
> >>
> >> You are leading me into deep waters here :-)  I have never used Wireshark 
> >> before. I have now downloaded it and am running it - it generates a *lot* 
> >> of data, most of which I do not understand yet!
> >
> > You can tell wireshark to only capture on one interface and to only capture 
> > packets for port 80.
> > (Captureing HTTPS means you cannot decode the packets without going deeper 
> > I recall)
> >
> > Then you can tell wireshark to decode the captured data for http to drop a 
> > lot of the lower level details.
> >
>
> Thanks. I am more or less doing that. Interestingly the [RST,ACK]
> messages appear on the tcp packets, so if I filter on http I do not see
> them.
>

I'm not 100% sure what "filter on HTTP" actually means, and it might
show only the data packets. Instead, filter on "from port 80 or to
port 80", which should show you the entire connection including the
SYN - SYN/ACK - ACK handshake, every data and acknowledgement packet,
and then whichever closing sequence gets used.

> >> I have another data point. I tried putting an asyncio.sleep() after 
> >> sending each file. A value of 0.01 made no difference, but a value of 0.1 
> >> makes the problem go away.
> >
> > What is the async wait to wait for the transmit buffers to drain?
> >
>
> Not sure what you are asking. I am just doing what it says in the docs -
>

I think his question is "what would asyncio be sleeping for, since you
already made it wait till it was drained". And my guess is that
there's a bug somewhere; the question is where.

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


Re: Intermittent bug with asyncio and MS Edge

2020-03-22 Thread Chris Angelico
On Sun, Mar 22, 2020 at 12:45 AM Frank Millman  wrote:
>
> Hi all
>
> I have a strange intermittent bug.
>
> The role-players -
>  asyncio on Python 3.8 running on Windows 10
>  Microsoft Edge running as a browser on the same machine
>
> The bug does not occur with Python 3.7.
> It does not occur with Chrome or Firefox.
> It does not occur when MS Edge connects to another host on the network,
> running the same Python program (Python 3.8 on Fedora 31).

What exact version of Python 3.7 did you test? I'm looking through the
changes to asyncio and came across this one, which may have some
impact.

https://bugs.python.org/issue36801

Also this one made a change that introduced a regression that was
subsequently fixed. Could be interesting.

https://bugs.python.org/issue36802

What happens if you try awaiting your writes? I think it probably
won't make any difference, though - from my reading of the source, I
believe that "await writer.write(...)" is the same as
"writer.write(...); await writer.drain()", so it's going to be exactly
the same as you're already doing.

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


Re: Python 3.8.2 on MacOSX Sierra?

2020-03-22 Thread Greg Ewing

On 22/03/20 10:20 pm, Greg Ewing wrote:

I'm going to try again with XCode 8, which purportedly comes
with a 10.12 SDK, and see if that fixes it.


It does.

--
Greg

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


Re: Intermittent bug with asyncio and MS Edge

2020-03-22 Thread Kouli
The RST from Python is probably caused here by HTTP 1.1 server closing TCP
connection without signalling "Connection: Close" in response headers: a
fast HTTP client will send another HTTP request before its TCP stack
detects the connection is being closed - and packets containing this new
requests will be replied with RST.

When you delay your response (as you mentioned), the Edge browser probably
opens more HTTP connections and will not send more HTTP requests in a
single connection as described above.

You should search for the cause Python closes the TCP connection (instead
of waiting for another HTTP request).

Kouli

On Sun, Mar 22, 2020 at 12:04 PM Chris Angelico  wrote:

> On Sun, Mar 22, 2020 at 12:45 AM Frank Millman  wrote:
> >
> > Hi all
> >
> > I have a strange intermittent bug.
> >
> > The role-players -
> >  asyncio on Python 3.8 running on Windows 10
> >  Microsoft Edge running as a browser on the same machine
> >
> > The bug does not occur with Python 3.7.
> > It does not occur with Chrome or Firefox.
> > It does not occur when MS Edge connects to another host on the network,
> > running the same Python program (Python 3.8 on Fedora 31).
>
> What exact version of Python 3.7 did you test? I'm looking through the
> changes to asyncio and came across this one, which may have some
> impact.
>
> https://bugs.python.org/issue36801
>
> Also this one made a change that introduced a regression that was
> subsequently fixed. Could be interesting.
>
> https://bugs.python.org/issue36802
>
> What happens if you try awaiting your writes? I think it probably
> won't make any difference, though - from my reading of the source, I
> believe that "await writer.write(...)" is the same as
> "writer.write(...); await writer.drain()", so it's going to be exactly
> the same as you're already doing.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Intermittent bug with asyncio and MS Edge

2020-03-22 Thread Frank Millman

On 2020-03-22 1:01 PM, Chris Angelico wrote:

On Sun, Mar 22, 2020 at 12:45 AM Frank Millman  wrote:


Hi all

I have a strange intermittent bug.

The role-players -
  asyncio on Python 3.8 running on Windows 10
  Microsoft Edge running as a browser on the same machine

The bug does not occur with Python 3.7.
It does not occur with Chrome or Firefox.
It does not occur when MS Edge connects to another host on the network,
running the same Python program (Python 3.8 on Fedora 31).


What exact version of Python 3.7 did you test? I'm looking through the
changes to asyncio and came across this one, which may have some
impact.


Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 
64 bit (AMD64)] on win32


Frank

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


How to copy paragraphs (with number formatting) and images from Words (.docx) and paste into Excel (.xlsx) using Python

2020-03-22 Thread A S
I have contract clauses in Words (.docx) format that needs to be frequently 
copy and pasted into Excel (.xlsx) to be sent to the third party. The clauses 
are often updated hence there's always a need to copy and paste these clauses 
over. I only need to copy and paste all the paragraphs and images after the 
contents page. Here is a sample of the Clause document 
(https://drive.google.com/open?id=1ZzV29R6y2q0oU3HAVrqsFa158OhvpxEK).

I have tried doing up a code using Python to achieve this outcome. Here is the 
code that I have done so far:

!pip install python-docx
import docx
import xlsxwriter

document = docx.Document("Clauses Sample.docx")
wb = xlsxwriter.Workbook('C://xx//clauses sample.xlsx')

docText = []
index_row = 0
Sheet1 = wb.add_worksheet("Shee")

for paragraph in document.paragraphs:
if paragraph.text:
docText.append(paragraph.text)
xx = '\n'.join(docText)

Sheet1.write(index_row,0, xx)

index_row = index_row+1

wb.close()
#print(xx) 
However, my Excel file output looks like this:

I can't seem to paste pictures into this discussion so please see both my 
current and desired Excel output here:

https://stackoverflow.com/questions/60800494/how-to-copy-paragraphs-with-number-formatting-and-images-from-words-docx-an
-- 
https://mail.python.org/mailman/listinfo/python-list


How to incorporate environment from with python.

2020-03-22 Thread Antoon Pardon
I think I can best explain what I want by showing two bash sessions:

Session 1)
--

$ /opt/csw/bin/python
Python 2.6.4 (r264:75706, Sep  9 2015, 15:05:38) [C] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named cx_Oracle
>>> ^D

===

Session 2)
--
$ ORACLE_OWNER=...
$ LD_LIBRARY_PATH=...
$ ORACLE_SID=...
$ TNS_ADMIN=...
$ LD_RUN_PATH=...
$ ORA_NLS33=...

$ export export LDFLAGS ORACLE_OWNER LD_LIBRARY_PATH ORACLE_SID TNS_ADMIN 
LD_RUN_PATH ORA_NLS33

$ /opt/csw/bin/python
Python 2.6.7 (r267:88850, Feb 10 2012, 01:39:24) [C] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>>



As you can see the import works in the second session because I
had done the needed assignments and exports in bash.

Now I was wondering, if I could do those kind of preparations in python.
I would like to start python from an unprepared bash, and do the necessary
stuff to make the import work.

I already tried changing os.environ and using os.putenv, but that didn't
seem to work.

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


Re: How to incorporate environment from with python.

2020-03-22 Thread Peter Otten
Antoon Pardon wrote:

> I think I can best explain what I want by showing two bash sessions:

> Python 2.6.4 (r264:75706, Sep  9 2015, 15:05:38) [C] on sunos5
> ImportError: No module named cx_Oracle

> Python 2.6.7 (r267:88850, Feb 10 2012, 01:39:24) [C] on sunos5
 import cx_Oracle


> As you can see the import works in the second session because I
> had done the needed assignments and exports in bash.

These seem to be different python interpreters. Maybe you just need to 
install cx_Oracle with the first one, or append the path containing 
cx_Oracle.so or whatever it's called to sys.path.


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


Re: How to incorporate environment from with python.

2020-03-22 Thread DL Neil via Python-list



On 23/03/20 4:04 AM, Antoon Pardon wrote:

I think I can best explain what I want by showing two bash sessions:

Session 1)
--

$ /opt/csw/bin/python
Python 2.6.4 (r264:75706, Sep  9 2015, 15:05:38) [C] on sunos5
Type "help", "copyright", "credits" or "license" for more information.

import cx_Oracle

Traceback (most recent call last):
   File "", line 1, in 
ImportError: No module named cx_Oracle

^D


===

Session 2)
--
$ ORACLE_OWNER=...
$ LD_LIBRARY_PATH=...
$ ORACLE_SID=...
$ TNS_ADMIN=...
$ LD_RUN_PATH=...
$ ORA_NLS33=...

$ export export LDFLAGS ORACLE_OWNER LD_LIBRARY_PATH ORACLE_SID TNS_ADMIN 
LD_RUN_PATH ORA_NLS33

$ /opt/csw/bin/python
Python 2.6.7 (r267:88850, Feb 10 2012, 01:39:24) [C] on sunos5
Type "help", "copyright", "credits" or "license" for more information.

import cx_Oracle





As you can see the import works in the second session because I
had done the needed assignments and exports in bash.

Now I was wondering, if I could do those kind of preparations in python.
I would like to start python from an unprepared bash, and do the necessary
stuff to make the import work.

I already tried changing os.environ and using os.putenv, but that didn't
seem to work.



It is possible to trap the import error using try...except.

The 'traditional' way to interrogate/modify the OS environment used 
https://docs.python.org/3/library/os.html#os.system


However, these days most prefer 
https://docs.python.org/3/library/subprocess.html


Please read the pros-and-cons carefully!
--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to incorporate environment from with python.

2020-03-22 Thread Peter J. Holzer
On 2020-03-22 16:04:59 +0100, Antoon Pardon wrote:
> $ /opt/csw/bin/python
> Python 2.6.4 (r264:75706, Sep  9 2015, 15:05:38) [C] on sunos5
> Type "help", "copyright", "credits" or "license" for more information.

Hmm. Solaris

> As you can see the import works in the second session because I
> had done the needed assignments and exports in bash.
> 
> Now I was wondering, if I could do those kind of preparations in python.
> I would like to start python from an unprepared bash, and do the necessary
> stuff to make the import work.
> 
> I already tried changing os.environ and using os.putenv, but that didn't
> seem to work.

I remember that once upon a time setting LD_LIBRARY_PATH had no effect
on the process itself (only on child processes) on 64 bit Linux, because
the runtime saved that value before loading the first shared library.
Maybe Solaris does the same?

In this case it might help to reexec the program. Something like

if "ORACLE_HOME" not in os.environ:
# Need to set env and reexec
os.environ["ORACLE_HOME"] = ...
os.environ["LD_LIBRARY_PATH"] = ...
...

os.execv(sys.argv[0], sys.argv)

# real content starts here

hp

-- 
   _  | Peter J. Holzer| Story must make more sense than reality.
|_|_) ||
| |   | [email protected] |-- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |   challenge!"


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Intermittent bug with asyncio and MS Edge

2020-03-22 Thread Barry Scott



> On 22 Mar 2020, at 11:59, Frank Millman  wrote:
> 
> On 2020-03-22 1:01 PM, Chris Angelico wrote:
>> On Sun, Mar 22, 2020 at 12:45 AM Frank Millman  wrote:
>>> 
>>> Hi all
>>> 
>>> I have a strange intermittent bug.
>>> 
>>> The role-players -
>>>  asyncio on Python 3.8 running on Windows 10
>>>  Microsoft Edge running as a browser on the same machine
>>> 
>>> The bug does not occur with Python 3.7.
>>> It does not occur with Chrome or Firefox.
>>> It does not occur when MS Edge connects to another host on the network,
>>> running the same Python program (Python 3.8 on Fedora 31).
>> What exact version of Python 3.7 did you test? I'm looking through the
>> changes to asyncio and came across this one, which may have some
>> impact.
> 
> Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 
> bit (AMD64)] on win32

Can you confirm that you have implemented Connection: keep-alive?
This means that the browser can send a 2nd GET on the same connection.

Barry


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

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


Re: Intermittent bug with asyncio and MS Edge

2020-03-22 Thread Barry Scott



> On 22 Mar 2020, at 09:41, Frank Millman  wrote:
> 
> On 2020-03-22 11:00 AM, Barry Scott wrote:
>>> On 22 Mar 2020, at 07:56, Frank Millman  wrote:
>>> 
>>> On 2020-03-21 8:04 PM, Barry Scott wrote:
 I'd look at the network traffic with wireshark to see if there is anything 
 different between edge and the other browsers.
>>> 
>>> You are leading me into deep waters here :-)  I have never used Wireshark 
>>> before. I have now downloaded it and am running it - it generates a *lot* 
>>> of data, most of which I do not understand yet!
>> You can tell wireshark to only capture on one interface and to only capture 
>> packets for port 80.
>> (Captureing HTTPS means you cannot decode the packets without going deeper I 
>> recall)
>> Then you can tell wireshark to decode the captured data for http to drop a 
>> lot of the lower level details.
> 
> Thanks. I am more or less doing that. Interestingly the [RST,ACK] messages 
> appear on the tcp packets, so if I filter on http I do not see them.
> 
>>> 
>>> One thing immediately stands out. When I run it with MS Edge and Python3.8, 
>>> it shows a lot of lines highlighted in red, with the symbols [RST,ACK]. 
>>> They do not appear when running Chrome, and they do not appear when running 
>>> Python3.7.
>> As Chris said that should not happen.
> 
> As I replied to Chris, they appear in packets sent *from* Python *to* Edge.
> 
>>> 
>>> I have another data point. I tried putting an asyncio.sleep() after sending 
>>> each file. A value of 0.01 made no difference, but a value of 0.1 makes the 
>>> problem go away.
>> What is the async wait to wait for the transmit buffers to drain?
> 
> Not sure what you are asking. I am just doing what it says in the docs -
> 
> =
> 
> write(data)
> The method attempts to write the data to the underlying socket immediately. 
> If that fails, the data is queued in an internal write buffer until it can be 
> sent.
> 
> The method should be used along with the drain() method:
> 
> stream.write(data)
> await stream.drain()
> 
> =
> 
> coroutine drain()
> Wait until it is appropriate to resume writing to the stream. Example:
> 
> writer.write(data)
> await writer.drain()
> This is a flow control method that interacts with the underlying IO write 
> buffer. When the size of the buffer reaches the high watermark, drain() 
> blocks until the size of the buffer is drained down to the low watermark and 
> writing can be resumed. When there is nothing to wait for, the drain() 
> returns immediately.
> 
> =

That you called drain() is what I was asking. I'm a Twisted user and have not 
used asyncio yet.

Barry

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

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


Re: How to incorporate environment from with python.

2020-03-22 Thread Chris Angelico
On Mon, Mar 23, 2020 at 3:24 AM Peter Otten <[email protected]> wrote:
>
> Antoon Pardon wrote:
>
> > I think I can best explain what I want by showing two bash sessions:
>
> > Python 2.6.4 (r264:75706, Sep  9 2015, 15:05:38) [C] on sunos5
> > ImportError: No module named cx_Oracle
>
> > Python 2.6.7 (r267:88850, Feb 10 2012, 01:39:24) [C] on sunos5
>  import cx_Oracle
> 
>
> > As you can see the import works in the second session because I
> > had done the needed assignments and exports in bash.
>
> These seem to be different python interpreters. Maybe you just need to
> install cx_Oracle with the first one, or append the path containing
> cx_Oracle.so or whatever it's called to sys.path.

I think everything else is irrelevant, and this is the only part that
matters. Why are there two distinct builds of Python 2.6, accessed via
the exact same path? What's going on here?

(Also, Python 2.6 is quite quite old by now. One of them was built in
2012 and hasn't been updated since. Any chance of installing something
newer?)

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


Re: How to copy paragraphs (with number formatting) and images from Words (.docx) and paste into Excel (.xlsx) using Python

2020-03-22 Thread Beverly Pope



> On Mar 22, 2020, at 9:47 AM, A S  wrote:
> 
> I can't seem to paste pictures into this discussion so please see both my 
> current and desired Excel output here:
> 
> https://stackoverflow.com/questions/60800494/how-to-copy-paragraphs-with-number-formatting-and-images-from-words-docx-an
>  
> 
Did you try using the 2 part answer on the stackoverflow webpage?

Bev in TX
-- 
https://mail.python.org/mailman/listinfo/python-list


Confusing textwrap parameters, and request for RE help

2020-03-22 Thread Chris Angelico
When using textwrap.fill() or friends, setting break_long_words=False
without also setting break_on_hyphens=False has the very strange
behaviour that a long hyphenated word will still be wrapped. I
discovered this as a very surprising result when trying to wrap a
paragraph that contained a URL, and wanting the URL to be kept
unchanged:

import shutil, re, textwrap
w = textwrap.TextWrapper(width=shutil.get_terminal_size().columns,
initial_indent=" ", subsequent_indent=" ",
break_long_words=False)
text = ("Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
"Praesent facilisis risus sed quam ultricies tempus. Vestibulum "
"quis tincidunt turpis, id euismod turpis. Etiam pellentesque sem "
"a magna laoreet sagittis. Proin id convallis odio, vel ornare "
"ipsum. Vivamus tincidunt sodales orci. Proin egestas sollicitudin "
"viverra. Etiam egestas, erat ac elementum tincidunt, risus nisl "
"fermentum ex, ut iaculis lorem quam vel erat. Pellentesque "
"condimentum ipsum varius ligula volutpat, sit amet pulvinar ipsum "
"condimentum. Nulla ut orci et mi sollicitudin vehicula. In "
"consectetur aliquet tortor, sed commodo orci porta malesuada. "
"Nulla urna sapien, sollicitudin et nunc et, ultrices euismod "
"lorem. Curabitur tempor est mauris, a ultricies urna mattis id. "
"Nam efficitur turpis a sem tristique sagittis. "

"https://example.com/long-url-goes-here/with-multiple-parts/wider-than-your-terminal/should-not-be-wrapped
"
"more words go here")
print(w.fill(text))

Unless you also add break_on_hyphens, this will appear to ignore the
break_long_words setting. Logically, it would seem that
break_long_words=False should take precedence over break_on_hyphens -
if you're not allowed to break a long word, why break it at a hyphen?

Second point, and related to the above. The regex that defines break
points, as found in the source code, is:

wordsep_re = re.compile(r'''
( # any whitespace
  %(ws)s+
| # em-dash between words
  (?<=%(wp)s) -{2,} (?=\w)
| # word, possibly hyphenated
  %(nws)s+? (?:
# hyphenated word
  -(?: (?<=%(lt)s{2}-) | (?<=%(lt)s-%(lt)s-))
  (?= %(lt)s -? %(lt)s)
| # end of word
  (?=%(ws)s|\Z)
| # em-dash
  (?<=%(wp)s) (?=-{2,}\w)
)
)''' % {'wp': word_punct, 'lt': letter,
'ws': whitespace, 'nws': nowhitespace},

It's built primarily out of small matches with long assertions, eg
"match a hyphen, as long as it's preceded by two letters or a letter
and a hyphen". What I want to do is create a *negative* assertion:
specifically, to disallow any breaking between "\b[a-z]+://" and "\b",
which will mean that a URL will never be broken ("https://..";
until the next whitespace boundary). Regex assertions of this form
have to be fixed lengths, though, so as described, this isn't
possible. Regexperts, any ideas? How can I modify this to never break
inside a URL?

Ultimately it would be great to have a break_urls parameter
(defaulting to True, and if False, will change the RE as described),
but since I can just patch in a different RE, that would work for
current Pythons.

I'd prefer to still be able to break words on hyphens while keeping
URLs intact, but if it's not possible, I'll just deny all hyphen
breaking. Seems unnecessary though.

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


Re: php to python code converter

2020-03-22 Thread bobimapexa
On Friday, May 8, 2009 at 1:38:25 PM UTC+3, bvidinli wrote:
> if anybody needs:
> http://code.google.com/p/phppython/

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


Like c enumeration in python3

2020-03-22 Thread Paulo da Silva
Hi!

Suppose a class C.
I want something like this:

class C:
KA=0
KB=1
KC=1
...
Kn=n

def __init__ ...
...


These constants come from an enum in a .h (header of C file).
They are many and may change from time to time.
Is there a way to somehow define them from inside __init__ giving for
example a list of names as strings?
There is an additional problem: C is not recognized inside __init__!

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


Re: Like c enumeration in python3 [ERRATA]

2020-03-22 Thread Paulo da Silva
Às 02:18 de 23/03/20, Paulo da Silva escreveu:
> Hi!
> 
> Suppose a class C.
> I want something like this:
> 
> class C:
>   KA=0
>   KB=1
KC=2
>   ...
>   Kn=n
> 
>   def __init__ ...
>   ...
> 
> 
> These constants come from an enum in a .h (header of C file).
> They are many and may change from time to time.
> Is there a way to somehow define them from inside __init__ giving for
> example a list of names as strings?
> There is an additional problem: C is not recognized inside __init__!
Of course I'm talking about C class name, not C language.
> 
> Thanks.
> 

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


Re: Like c enumeration in python3 [ERRATA]

2020-03-22 Thread DL Neil via Python-list

On 23/03/20 3:32 PM, Paulo da Silva wrote:

Às 02:18 de 23/03/20, Paulo da Silva escreveu:

Hi!


Olá,



Suppose a class C.
I want something like this:

class C:
KA=0
KB=1

KC=2

...
Kn=n

def __init__ ...
...


These constants come from an enum in a .h (header of C file).
They are many and may change from time to time.
Is there a way to somehow define them from inside __init__ giving for
example a list of names as strings?
There is an additional problem: C is not recognized inside __init__!



Please read the PSL docs carefully, because Python's enums seem to 
differ from those in other languages - sometimes in significant ways, 
and sometimes in a subtle manner!



I have been asking similar questions recently - particularly wanting to 
(also) use the "strings", because of the effort of upgrading many 
modules of code at the same time (the manual, or was it the PEP(?) 
refers to the need to upgrade the PSL to use enums, and to the effort 
that might cost - I suggest there has been little/v.slow take-up, but 
then keeping the PSL updated is a subject of a great many conversations, 
elsewhere).


Python's enums cannot be changed. Once set, C.KA cannot be changed to 1 
(for example). We may not add element Km (where m>n) at some later 
occasion after class definition-time (in fact the __new__() constructor 
is 're-wired' in order to prohibit its use post-definition). It is not 
permitted to sub-class an existing enum class, eg C(), perhaps to 
enlarge it with more members, if the 'super class' contains "members".


There is usually no need for an __init__. Plus, I've had a distinct lack 
of satisfaction playing with that - but YMMV!


Sub-classing, and even attempting to use the meta-class seems to be a 
major challenge (see StackOverflow).


It is possible to list the elements using the built-in iterator:

for c in C: print( c )

or iterator-conversion:

list( C )

and/or accessing the class's 'data-dict' directly:

for name, value in C.__members__.items():
--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to copy paragraphs (with number formatting) and images from Words (.docx) and paste into Excel (.xlsx) using Python

2020-03-22 Thread A S
On Monday, 23 March 2020 01:58:38 UTC+8, Beverly Pope  wrote:
> > On Mar 22, 2020, at 9:47 AM, A S  wrote:
> > 
> > I can't seem to paste pictures into this discussion so please see both my 
> > current and desired Excel output here:
> > 
> > https://stackoverflow.com/questions/60800494/how-to-copy-paragraphs-with-number-formatting-and-images-from-words-docx-an
> >  
> > 
> Did you try using the 2 part answer on the stackoverflow webpage?
> 
> Bev in TX

I'm able to get the paragraphs copied correctly now! But i'm trying to figure 
out if there's a way to copy and paste the images into the Excel, along with 
the paragraphs as well. Do you have an idea? :)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How does the super type present itself and do lookups?

2020-03-22 Thread Adam Preble
On Thursday, March 19, 2020 at 5:02:46 PM UTC-5, Greg Ewing wrote:
> On 11/03/20 7:02 am, Adam Preble wrote:
> > Is this foo attribute being looked up in an override of __getattr__, 
> > __getattribute__, or is it a reserved slot that's internally doing this? 
> > That's what I'm trying to figure out.
> 
> Looking at the source in Objects/typeobject.c, it uses the
> tp_getattro type slot, which corresponds to __getattribute__.

Thanks for taking the time to look this up for me. I saw the message soon after 
you originally posted it, but it took me this long to sit down and poke at 
everything some more.

I don't doubt what you got from the source, but I am trying to figure out how I 
could have inferred that from the code I was trying. It looks like 
child_instance.__getattribute__ == child_instance.super().__getattribute__. 
They print out with the same address and pass an equality comparison. That 
implies that they are the same, and that the super type is NOT doing something 
special with that slot.

Given that super().__getattribute__ internally ultimately should be something 
else, I am guessing there is something else at play causing an indirection.

I have two reasons to be interested in this:
1. There may be obscure behavior I should worry about in general if I'm trying 
to default to mimicking Python and the data model for my own stuff.
2. I need to improve my kung fu when I'm inspecting these objects so I don't 
get hung up on stuff like this in the future.

The bright side is having a custom get attribute implementation is pretty much 
correct, although mine would have c.__getattribute != 
c.super().__getattribute__.
-- 
https://mail.python.org/mailman/listinfo/python-list