Re: A problem with opening a file

2020-11-29 Thread dn via Python-list

On 29/11/2020 20:56, Gabor Urban wrote:

Hi,

I am facing an issue I was not able to solve yet. I have a class saving
messages to a file. The relevant code is:



import OS


if you're wanting the Python Standard Library, this should not be in 
upper-case




import sys


are these two imports used elsewhere?



class MxClass:

 def __init__(self, fName, fMode,):
 self.fileName = fName
 self.fileMode = fMode
 


names such as fName and writeMethod are valid Python, but won't pass 
PEP-8 conventions




 def writeMethod(self, message):
 data = open(self.fileName, self.fileMode)
 



Tests reveal: the class is instantiated correctly, other methods work
perfect. The writeMethod raises a NameError opening the file
"name 'open' is not defined"

I am stuck here.


Please review ALL of the code. Is an open() function defined somewhere? 
(which will "shadow" the built-in function)


That said the description doesn't quite match.

Please copy-paste the exact error messages because there's likely 
missing information...

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


Re: How to run Jupyter notebook in command line and get full error message?

2020-11-29 Thread Skip Montanaro
>
> My VPN keeps dropping and can not run Jupyter Notebook as it is.
>

You don't provide a lot of detail, but this seems similar to the kind of
flaky networking we used to deal with in the Before Times. Simply
connecting directly to a host over the Internet was often plagued by
disconnects. For that we used screen(1). You don't mention your operating
system. It's available for most/all Linux systems and Macs. If you're on
Windows I imagine you could get it using WSL.

Of course, I might be way off-base focusing on just the above statement
from your post. If so, you might want to provide some more details about
your problem.

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


Re: ssl connection has been closed unexpectedly

2020-11-29 Thread Dieter Maurer
Shaozhong SHI wrote at 2020-11-28 23:29 +:
>I keep getting the following error when I use engine =
>create_engine(logging in details to postgres)
>df.to_sql('table_name', and etc.)
>
>
>OperationalError: (psycopg2.OperationalError) SSL connection has been
>closed unexpectedly

SSL works as follows: a "normal" connection is opened to
the specified "communication port". After that, an "SSL handshake"
sets up encryption for the following communication over the channel.
In your case, the "SSL handshake" was aborted by the communication
partner closing the connection.

You might get errors like this if the connected "port" is
not prepared for SSL connections.

Check the Postgres server configuration and your connection
parameters. It the specified communication port SSL aware?
If so, look whether there is helpful information in the Postgres
log file.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A problem with opening a file

2020-11-29 Thread Dieter Maurer
Gabor Urban wrote at 2020-11-29 08:56 +0100:
>I am facing an issue I was not able to solve yet. I have a class saving
>messages to a file. The relevant code is:
>
>
>
>import OS
>import sys
>
>class MxClass:
>
>def __init__(self, fName, fMode,):
>self.fileName = fName
>self.fileMode = fMode
>
>
>def writeMethod(self, message):
>data = open(self.fileName, self.fileMode)
>
>
>
>
>Tests reveal: the class is instantiated correctly, other methods work
>perfect. The writeMethod raises a NameError opening the file
>"name 'open' is not defined "

"open" is a so called "built-in" function.
In "normal" (i.e. not restricted) mode, it should be available.
Are you sure, you operate in "normal" mode?

Under Python 3, you could try to import "open" from the module "io".
(Under Python 2, "file" is a synonym for "open").
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: numpy problem (follow up)

2020-11-29 Thread MRAB

On 2020-11-29 18:33, Dennis Lee Bieber wrote:

On Sat, 28 Nov 2020 17:28:50 -0600, Larry Burford 
declaimed the following:



when trying to run the tutorial program standardplot.py I get a msg that 
says my numpy won't pass a sanity check due to a problem in the Win runtime




Wait for M$ to fix their runtime... Or try to install a non-official
numpy release (I haven't figured out where to get 1.19.3; PIP can't locate
it)

https://github.com/int-brain-lab/iblenv/issues/111
https://developercommunity.visualstudio.com/content/problem/1207405/fmod-after-an-update-to-windows-2004-is-causing-a.html


Really?

I just did what Malcolm did (though with py):

py -m pip uninstall numpy
py -m pip install numpy==1.19.3

It worked for me!
--
https://mail.python.org/mailman/listinfo/python-list


Re: numpy problem (follow up)

2020-11-29 Thread Malcolm

Hi

https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy also has a numpy wheel

1.19.4+vanilla‑cp39‑cp39‑win_amd64.whl

"Vanilla is a minimal distribution, which does not include any optimized 
BLAS libray or C runtime DLLs."


Have not tried this.

cheers

Malcolm

On 30/11/2020 7:19 am, MRAB wrote:

On 2020-11-29 18:33, Dennis Lee Bieber wrote:

On Sat, 28 Nov 2020 17:28:50 -0600, Larry Burford 
declaimed the following:



when trying to run the tutorial program standardplot.py I get a msg 
that says my numpy won't pass a sanity check due to a problem in the 
Win runtime




Wait for M$ to fix their runtime... Or try to install a non-official
numpy release (I haven't figured out where to get 1.19.3; PIP can't 
locate

it)

https://github.com/int-brain-lab/iblenv/issues/111
https://developercommunity.visualstudio.com/content/problem/1207405/fmod-after-an-update-to-windows-2004-is-causing-a.html 




Really?

I just did what Malcolm did (though with py):

py -m pip uninstall numpy
py -m pip install numpy==1.19.3

It worked for me!

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


Re: ssl connection has been closed unexpectedly

2020-11-29 Thread Dan Stromberg
What happens if you execute this from a shell prompt on a system with
openssl installed?
openssl s_client -connect host.com:443

(replacing host.com with your server's hostname and 443 with your port of
interest)


On Sat, Nov 28, 2020 at 3:30 PM Shaozhong SHI 
wrote:

> Hi,
>
> I keep getting the following error when I use engine =
> create_engine(logging in details to postgres)
> df.to_sql('table_name', and etc.)
>
>
> OperationalError: (psycopg2.OperationalError) SSL connection has been
> closed unexpectedly
>
> (Background on this error at: http://sqlalche.me/e/13/e3q8)
> OperationalError: (psycopg2.OperationalError) SSL connection has been
> closed unexpectedly
>
> Can anyone shed any light on this?
>
> Regards,
>
> David
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: filtering out warnings

2020-11-29 Thread Jason Friedman
>
> The Box API is noisy ... very helpful for diagnosing, and yet for
> production code I'd like less noise.
>
> I tried this:
>
> warnings.filterwarnings(
> action='ignore',
> # category=Warning,
> # module=r'boxsdk.*'
> )
>
> but I still see this:
>
> WARNING:boxsdk.network.default_network:"POST
> https://api.box.com/oauth2/token"; 400 83
> {'Date': 'Sat, 28 Nov 2020 04:30:03 GMT', 'Content-Type':
> 'application/json', 'Transfer-Encoding': 'chunked', 'Connection':
> 'keep-alive', 'Strict-Transport-Security': 'max-age=31536000',
> 'Set-Cookie': 'box_visitor_id=5fc1d24b134ce6.76522820; expires=Sun,
> 28-Nov-2021 04:30:03 GMT;
>

Posted and answered on StackOverflow (
https://stackoverflow.com/questions/65061846/filtering-out-warnings-from-box-sdk-python/
).

Answer was:
import logging
logging.getLogger('boxsdk').setLevel(logging.CRITICAL)
-- 
https://mail.python.org/mailman/listinfo/python-list


A problem with opening a file -- again

2020-11-29 Thread Gabor Urban
Hi guys,

I tried to solve the problem once again. I have inserted some print
statements the check the variables.

The actual code (naplo.py) is copy-pasted here:



class Naplo:
def __init__(self, pNeve, pMeret, pMode = 'a'):
self.logNev = pNeve
self.meret = pMeret
self.startMode = pMode
self.mode = 'a'
self.sor = 0
self.puffer = []
self.start = True

def __del__(self):
print(' .. sor = %d'%self.sor)
print(' .. startMode = %s'%self.startMode)
print(' .. mode = %s'%self.mode)
print(' .. logName = %s'%self.logNev)
print(self.puffer)
if self.sor != 0:
if self.start:
trc = open(self.logNev,self.startMode)
else:
trc = open(self.logNev,self.mode)
for idx in xrange(self.meret):
trc.write(self.puffer[idx])
trc.close()


def Kiir(self, txt):
msg = '%s\n'%txt
self.puffer.append(msg)
self.sor += 1
print(' .. sor = %d'%self.sor)
print(' .. startMode = %s'%self.startMode)
print(' .. mode = %s'%self.mode)
print(' .. logName = %s'%self.logNev)
print(self.puffer)
print('.. ' + self.logNev)
if self.sor == self.meret:
if self.start:
trc = open(self.logNev,self.startMode)
else:
trc = open(self.logNev,self.mode)
for idx in xrange(self.meret):
trc.write(self.puffer[idx])
trc.close()
self.start = False
self.puffer = []
self.sor = 0

lf = Naplo('nap1.log', 6)
lf.Kiir('Ez az elso.')

 

I am using a Windows version:

python
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit
(AMD64)]
 on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> ^Z

 

python naplo.py
 .. sor = 1
 .. startMode = a
 .. mode = a
 .. logName = nap1.log
['Ez az elso.\n']
.. nap1.log
 .. sor = 1
 .. startMode = a
 .. mode = a
 .. logName = nap1.log
['Ez az elso.\n']
Exception ignored in: >
Traceback (most recent call last):
  File "naplo.py", line 20, in __del__
NameError: name 'open' is not defined

-- 
Urbán Gábor

Linux is like a wigwam: no Gates, no Windows and an Apache inside.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: A problem with opening a file -- again

2020-11-29 Thread Chris Angelico
On Mon, Nov 30, 2020 at 8:37 AM Gabor Urban  wrote:
> class Naplo:
> def __del__(self):
> if self.sor != 0:
> if self.start:
> trc = open(self.logNev,self.startMode)
> else:
> trc = open(self.logNev,self.mode)
> for idx in xrange(self.meret):
> trc.write(self.puffer[idx])
> trc.close()
>

This seems like a really REALLY bad idea. You're putting a lot of work
into your __del__ function, and that's not getting called until
everything's shutting down. (Also, xrange doesn't exist, hence the
"exception ignored" thing.)

Avoid putting this sort of work into __del__ by explicitly marking
that you're done with the object. A context manager is a good choice
here.

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


Re: A problem with opening a file -- again

2020-11-29 Thread dn via Python-list

On 30/11/2020 10:36, Gabor Urban wrote:

Hi guys,

I tried to solve the problem once again. I have inserted some print
statements the check the variables.

The actual code (naplo.py) is copy-pasted here:


Thanks = helpful

+1 @Chris' response!


Meantime, what happens if you start python, and enter into the REPL:

trc = open( 'nap1.log', 'a' )
trc

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


Re: A problem with opening a file -- again

2020-11-29 Thread Eryk Sun
On 11/29/20, Chris Angelico  wrote:
>
> This seems like a really REALLY bad idea. You're putting a lot of work
> into your __del__ function, and that's not getting called until
> everything's shutting down. (Also, xrange doesn't exist, hence the
> "exception ignored" thing.)
>
> Avoid putting this sort of work into __del__ by explicitly marking
> that you're done with the object. A context manager is a good choice
> here.

The documentation of the __del__() finalizer contains the following warning:

__del__() can be executed during interpreter shutdown. As a consequence,
the global variables it needs to access (including other modules) may
already have been deleted or set to None.

A finalizer can locally reference required globals and builtins as the
default value of keyword-only arguments, e.g. __del__(self, *,
open=open). But in this case I second the suggestion to make the
object a context manager with __enter__() and __exit__() methods.
-- 
https://mail.python.org/mailman/listinfo/python-list


Reading binary data with the CSV module

2020-11-29 Thread Jason Friedman
Using the Box API:


print(source_file.content())


returns


b'First Name,Last Name,Email Address,Company,Position,Connected
On\nPeter,van

(and more data, not pasted here)


Trying to read it via:

with io.TextIOWrapper(source_file.content(), encoding='utf-8') as text_file:

reader = csv.DictReader(text_file)

for row in reader:

print(row)


Getting this error:

Traceback (most recent call last):
  File "/home/jason/my_box.py", line 278, in 
with io.TextIOWrapper(source_file.content(), encoding='utf-8') as
text_file:
AttributeError: 'bytes' object has no attribute 'readable'
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Reading binary data with the CSV module

2020-11-29 Thread MRAB

On 2020-11-30 01:31, Jason Friedman wrote:

Using the Box API:


print(source_file.content())


returns


b'First Name,Last Name,Email Address,Company,Position,Connected
On\nPeter,van

(and more data, not pasted here)


Trying to read it via:

with io.TextIOWrapper(source_file.content(), encoding='utf-8') as text_file:

reader = csv.DictReader(text_file)

for row in reader:

print(row)


Getting this error:

Traceback (most recent call last):
   File "/home/jason/my_box.py", line 278, in 
 with io.TextIOWrapper(source_file.content(), encoding='utf-8') as
text_file:
AttributeError: 'bytes' object has no attribute 'readable'



csv.DictReader appears to be happy with a list of strings representing 
the lines.


Try this:

contents = source_file.content()

for row in csv.DictReader(contents.decode('utf-8').splitlines()):
print(row)
--
https://mail.python.org/mailman/listinfo/python-list


Re: Reading binary data with the CSV module

2020-11-29 Thread Jason Friedman
>
> csv.DictReader appears to be happy with a list of strings representing
> the lines.
>
> Try this:
>
> contents = source_file.content()
>
> for row in csv.DictReader(contents.decode('utf-8').splitlines()):
>  print(row)
>

Works great, thank you! Question ... will this form potentially use less
memory?

for row in
csv.DictReader(source_file.content().decode('utf-8').splitlines()):
print(row)
-- 
https://mail.python.org/mailman/listinfo/python-list