I need to port some python2 code to python3.
There I found that a dict of QTreeWidgetItem is created. In Python 2 this is working fine, as the object is hashable. But in python 3 you will get an error because __hash__ is not implemented:
$ python2
>>> from PyQt5 import QtWidgets
>>> x = QtWidgets.QTreeWidgetItem()
>>> foo = {x: 23}
>>> hash(x)
-9223363252877437056
$ python3
>>> from PyQt5 import QtWidgets
>>> x = QtWidgets.QTreeWidgetItem()
>>> hash(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'QTreeWidgetItem'
I consider this is a bug or is there any reason for this, that I do not see?
The PyQt5 documentation does not mention anything in this direction, and for QTreeWidgetItem, there is only the C++ doc available, which does not help in this python specific case.
Related
I would like to able to extract details about a molecule in ".mae" format. I imported the rdkit.Chem.rdmolfiles functions and it seems to work for MolFromSmiles, but not for MaeMolSupplier as suggested in the 2019 documentation. Instead I get a NameError. Any aid/help in calling this function would be greatly appreciated.
Works OK with MolFromSmiles
import rdkit
from rdkit.Chem.rdmolfiles import *
mol = MolFromSmiles('C1NCN1')
print(mol)
(my-rdkit-env) [Me]$ python3 testrdkit.py
<rdkit.Chem.rdchem.Mol object at 0x7f237f917030>
Now to show the error
import rdkit
from rdkit.Chem.rdmolfiles import *
suppl = MaeMolSupplier(file('five.mae'))
print(suppl)
my-rdkit-env) [Me]$ python3 testrdkit.py
Traceback (most recent call last):
File "testrdkit.py", line 8, in <module>
suppl = MaeMolSupplier(file('five.mae'))
NameError: name 'MaeMolSupplier' is not defined
import * doesn't work here either.
Just import rdmolfiles.
from rdkit.Chem import rdmolfiles
suppl = rdmolfiles.MaeMolSupplier('five.mae')
print(suppl)
<rdkit.Chem.rdmolfiles.MaeMolSupplier object at 0x000002792CEFC5B0>
Been trying to get this to work for a few hours now. Nothing I try is splitting this text up. I only want the Current CPU from this
>>> from __future__ import print_function
>>> from urllib.request import urlopen
>>> import json
>>> import subprocess
>>> import requests
>>> import random
>>> import sys
>>> import os
>>> import time
>>> import datetime
>>> import MySQLdb as my
>>> import psutil
>>> os.popen('vcgencmd measure_temp').readline()
"temp=52.0'C\n"
>>> cpu = psutil.cpu_freq()
>>> cpu = cpu.split('current=')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'scpufreq' object has no attribute 'split'
>>> psutil.cpu_freq()
scpufreq(current=600.0, min=600.0, max=1500.0)
>>> psutil.cpu_freq(percpu=True)
[scpufreq(current=600.0, min=600.0, max=1500.0)]
>>> cpu = psutil.cpu_freq(percpu=True)
>>> cpu.split('=')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'split'
>>> AttributeError: 'list' object has no attribute 'split'
File "<stdin>", line 1
AttributeError: 'list' object has no attribute 'split'
^
SyntaxError: invalid syntax
>>> AttributeError: 'list' object has no attribute 'split'
File "<stdin>", line 1
AttributeError: 'list' object has no attribute 'split'
^
SyntaxError: invalid syntax
>>> psutil.cpu_freq(percpu=True).readline()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'readline'
>>> cpu = psutil.cpu_freq()
Where am I going wrong with this?
OS: Rasbian Buster
Python: python3
PIP: pip3
It looks mostly like you're ignoring your error messages:
>>> cpu = psutil.cpu_freq()
>>> cpu = cpu.split('current=')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'scpufreq' object has no attribute 'split'
The return value from psutil.cpu_freq() isn't a string, so it doesn't have a split method. If you just print the value...
>>> cpu
scpufreq(current=700.0, min=700.0, max=800.0)
...you get some idea of what attributes it has, and indeed, we can access those values like this:
>>> cpu.current
700.0
>>> cpu.max
800.0
When you set percpu=True, you're getting back a list:
>>> psutil.cpu_freq(percpu=True)
[scpufreq(current=600.0, min=600.0, max=1500.0)]
And once again, a list isn't a string, so there's no split method. Since there's only a single CPU, you get back a 1-item list, so you can access values like this:
>>> cpu = psutil.cpu_freq(percpu=True)
>>> cpu[0].current
700.0
I use the code below to read a pickle file made in python2
import pickle
with open('data.pkl', 'rb') as fin:
data_df = pickle.load(fin, encoding='latin1')
Everything works well except the column including Japanese charactors.
For example, string supposed to be "東京都" may become something like "æ±äº¬é".
I think python3 reads the bytes format string as str. How can I convert it back?
Here is some test I did in python3
>>> a='\xe6\x9d\xb1\xe4\xba\xac\xe9\x83\xbd'
>>> b=b'\xe6\x9d\xb1\xe4\xba\xac\xe9\x83\xbd'
>>> a
'æ\x9d±äº¬é\x83½'
>>> b
b'\xe6\x9d\xb1\xe4\xba\xac\xe9\x83\xbd'
>>> print(a)
æ±äº¬é
>>> print(b)
b'\xe6\x9d\xb1\xe4\xba\xac\xe9\x83\xbd'
>>> a.decode('utf-8')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'decode'
>>> b.decode('utf-8')
'東京都'
I think pickle.load reads the utf-8 code as str (like the a case above).
[EDIT]
The reason why I set pickle.load encoding to latin1 was because there's column with datetime format. It causes error if I set encoding='utf-8
why should I use just import module to see the functions in it?
By help (module )
but it is not woking with from module import *
is there any way to see the functions of it by from module import *
When you do from module import * - only the exported symbols from module are added to your module. The name module itself is not imported. Since help merely looks at documentation of imported symbols, that's why your help(module) is not working.
>>> from os import *
>>> help(os)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>>
What you can do is this:
>>> import os
>>> help(os)
>>>
>>> from os import path
>>>
I read "How to think like a Computer Scientist. Learning with Python." book. So I usually have no difficulties to interpret examples from python2 to python3, but at chapter 11 Files & Exceptions I encountered this snippet
>>> import pickle
>>> f = open("test.pck", "w")
>>> pickle.dump(12.3, f)
>>> pickle.dump([1,2,3], f)
>>> f.close()
which when I evaluate it using Python 3.5.2 gives this error
Traceback (most recent call last): File "/(myDirs)/files.py", line 3, in <module>
pickle.dump(3.14, f)
TypeError: write() argument must be str, not bytes
I am not a good docs reader, so if you can help me to solve this riddle I would be grateful.
You need to open the file in binary mode.
In line 2:
f = open("test.pck", "wb")