Python hex to binary fail - python

I'm trying to convert a hex string 'aa' to binary as following:
a = bin(int('aa',16))
But it gives me the error of:
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
a = bin(int('aa',16))
TypeError: bin(QTextStream): argument 1 has unexpected type 'int'
Can anyone explain what is the problem with the conversion?

You did some sort of import *, probably
from PyQt4.QtCore import *
causing the built-in bin to be shadowed by a different function. Stop using import *, and the problem will go away.

Related

Pytorch Cityscapes Dataset, train_distribute problem - "Typeerror: path should be string, bytes, pathlike or integer, not NoneType"

I'm very unfamiliar with Machine Learning, python, and such, so forgive my oblivious errors. I'm trying to use machine learning systems on a dataset of streetscapes I have. I found a lot or resources, and I'm working off of this package which has a lot of examples and seems straightforward.
When I attempted to run the train_distribute.py file, I received this error:
(base) corey#corona:~/Desktop/pycity/GALD-Net-master$ python train_distribute.py
Traceback (most recent call last):
File "train_distribute.py", line 261, in <module>
main()
File "train_distribute.py", line 136, in main
if not os.path.exists(args.save_dir):
File "/home/corey/anaconda3/lib/python3.7/genericpath.py", line 19, in exists
os.stat(path)
TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType
Looking in the code, it's coming from these lines:
def main():
# make save dir
if args.local_rank == 0:
if not os.path.exists(args.save_dir):
os.makedirs(args.save_dir)
# launch the logger
Log.init(
log_level=args.log_level,
I'm guessing this means I need a more exact file structure, and to point the code at the right location. I am in no way a computer scientist and have close to zero understanding of what does what and how things like this work. Any advice for what I'm doing wrong and how I can approach fixing things?
From the error message, my guess would be that args.save_dir is None. os.path.exists cannot deal with None as a path:
>>> import os
>>> os.path.exists(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.8/genericpath.py", line 19, in exists
os.stat(path)
TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType
Looking at the script you cited, the save_dir argument has a default value of None. It might be useful to make this a required argument and remove the default value, since the main function depends on it.
You have to specify the save_dir as an argument.
Run the code like this
python train_distribute.py --save_dir=SAVE_PATH
Here SAVE_PATH will take the path where you want to get the outputs saved.
Also, note that if the folder specified by the given path does not exist then that folder will be created.

strftime returns ValueError

strftime returns Value error. Why?
This is the code
datetime.datetime.now().strftime('%-m')
My output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Invalid format string
Based on strftime.org format can depend on system.
On Unix it may needs - like "%-m" but on Windows it may need # like "%#m".
I could test it only on Linux and both works for me: "%-m %#m"
BTW: but if you want to get negative value then you need - before % like "-%m"

Python-Weka-Wrapper3 removing attributes from arff file error

I have an arff file and I need to remove the first 5 attributes from it (without manually deleting them). I tried to use the Python-Weka-Wrapper3 as it is explained here which enables the filtering options of Weka, however I get an error while using the following code:
import weka.filters as Filter
remove = Filter(classname="weka.filters.unsupervised.attribute.Remove", options=["-R", "1,2,3,4,5"])
The error that I receive is the following:
Traceback (most recent call last):
File "/home/user/Desktop/file_loading.py", line 16, in <module>
removing = Filter(classname="weka.filters.unsupervised.attribute.Remove", options=["-R", "last"])
TypeError: 'module' object is not callable
What could be the reason for this error? Also I would appreciate if anyone knows an alternative way to remove attributes from an arff file using Python.
You are attempting to call the module object instead of the class object.
Try using:
from weka.filters import Filter
remove = Filter(classname="weka.filters.unsupervised.attribute.Remove", options=["-R", "1,2,3,4,5"])

Cannot perform operations using rpy2 in Python: "TypeError: argument 1 must be a str, not int"

So I'm trying to get to grips with using the rpy2 module (I am familiar with R but new to Python). Following this tutorial, I first load the library and assign it to the variable 'r' using:
import rpy2
import rpy2.robjects as robjects
r = robjects.r
then I try to perform a simple operation to confirm everything is working:
print(r[2+2])
but I get this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python34\lib\site-packages\rpy2\robjects\__init__.py", line 248, in _
_getitem__
res = _globalenv.get(item)
TypeError: argument 1 must be str, not int
I'm sure it's just something stupid I'm doing wrong, but any advice would be much appreciated. I'm using python3.4.2 (64bit), rpy2-2.5.6 (64bit) on a Windows 7 machine (64bit).
You should use print(r(2+2)) instead of print(r[2+2]).
When you use r[2+2] you are trying to recover an element corresponding to the index 4 (the result of 2+2) of the r iterable. And your r object doesn't seem to respond to this kind of message.
Ok I think I have figured it out. For R to evaluate the function inside the parenthesis, the function must be in quotes e.g.
r("2+2")
This is what was confusing me because this looks like I'm providing a string.
Oddly I don't print the result (4) by using:
print(r("2+2"))
as this prints:
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
print(r("2+2"))
File "C:\Python34\lib\site-packages\rpy2\robjects\robject.py", line 49, in __str__
s = str.join(os.linesep, s)
TypeError: sequence item 0: expected str instance, bytes found
Instead I just print the result using:
answer = r("2+2")
answer[0]
(Because R is vector based, the initial value of the vector is the answer so you have to index it at the first position, otherwise you get:
answer = r("2+2")
answer
<FloatVector - Python:0x0000000005836EC8 / R:0x00000000047A51A0>
[4.000000]
Thanks for you help
Hefin

Problems using PIL 1.1.7

The trivial
import Image
im = Image.OPEN('C:\abc.bmp')
results in the following exception
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
im = Image.OPEN('C:\Documents and Settings\umair.ahmed\My Documents\My Pictures\avanza.bmp')
TypeError: 'dict' object is not callable
not sure if i am missing something, kindly help.
Use:
Image.open()
It's case-sensitive.
I don't think the error message came from your input, because the file names are different, but you should not use 'C:\abc.bmp', in your open() call, but use either C:/abc.bmp, or r'C:\abc.bmp'. Backslash is an escape character in Python.

Categories