AttributeError: 'float' object has no attribute 'co_names' - python-3.x

simple inspect script for pyc file
there is a problem with co_name function
the script is work well until the marshal module is load then fall.
magic: 160d0d0a
mod_time: 1493965574
source_size: 231
code:
Traceback (most recent call last):
File "/home/ubuntu/Downloads/book-resources-master/chapter4/code-exec-eg/python/inspect.py", line 24, in <module>
inspect_code(code)
File "/home/ubuntu/Downloads/book-resources-master/chapter4/code-exec-eg/python/inspect.py", line 8, in inspect_code
print('{}{}(line:{})'.format(indent, code.co_names, code.co_firstlineno))
AttributeError: 'float' object has no attribute 'co_names'
if anyone can help !
thanks
import marshal
import types
def to_long(s):
return s[0] + (s[1] << 8) + (s[2] << 16) + (s[3] << 24)
def inspect_code(code, indent=' '):
print('{}{}(line:{})'.format(indent, code.co_names, code.co_firstlineno))
for c in code.co_consts:
if isinstance(c, types.CodeType):
inspect_code(c, indent + ' ')
f = open('__pycache__/add.cpython-39.pyc', 'rb')
magic = f.read(4)
print('magic: {}'.format(magic.hex()))
mod_time = to_long(f.read(4))
print('mod_time: {}'.format(mod_time))
source_size = to_long(f.read(4))
print('source_size: {}'.format(source_size))
print('\ncode:')
code = marshal.load(f)
inspect_code(code)
f.close()
import dis
dis.disassemble(code)

I'm not familiar with marshal module nor pyc content, but when I try your code with Python 3.9,
I got error for reading code value. And the format of file seems different for my sample pyc built with Python 3.9.
magic: 610d0d0a
mod_time: 0 # <---- Unknown
source_size: 1621462747 # <---- Must be mode time
When I read 4 more bytes before reading value for code, I got this:
magic: 610d0d0a
mod_time: 0
source_size: 1621462747
4 more bytes: 14 # <----- Must be source size
Then I could read code value:
code:
('print',)(line:1)
1 0 LOAD_NAME 0 (print)
2 LOAD_CONST 0 ('Hello')
4 CALL_FUNCTION 1
6 POP_TOP
8 LOAD_CONST 1 (None)
10 RETURN_VALUE
I'm not sure why you're able to run marshal.load() without error but may you try to read 4 more or fewer bytes before calling marshal.load(f)?

Related

How to read number of bytes in the ibd file (subprocess.check_output return code)

I want to know why I received error running my command in order to read number of bytes in the ibd file. What might be wrong in my code? Thanks a lot in advance.
I want to read my dataset which is in the format of imzML including another complementary file of ibd. More info can be ontained from http://psi.hupo.org/ms/mzml .
python
import subprocess
nbytes_str = subprocess.check_output(['wc -c < \"' + fname + '.ibd\"'], shell=True)
nbytes = int(nbytes_str)
nbytes # number of bytes in the ibd file
my error is:
python
---------------------------------------------------------------------------
CalledProcessError Traceback (most recent call last)
<ipython-input-7-381047b77c3f> in <module>
----> 1 nbytes_str = subprocess.check_output(['wc -c < \"' + fname + '.ibd\"'], shell=True)
2 nbytes = int(nbytes_str)
3 nbytes # number of bytes in the ibd file
~\.conda\envs\MSI\lib\subprocess.py in check_output(timeout, *popenargs, **kwargs)
354
355 return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
--> 356 **kwargs).stdout
357
358
~\.conda\envs\MSI\lib\subprocess.py in run(input, timeout, check, *popenargs, **kwargs)
436 if check and retcode:
437 raise CalledProcessError(retcode, process.args,
--> 438 output=stdout, stderr=stderr)
439 return CompletedProcess(process.args, retcode, stdout, stderr)
440
CalledProcessError: Command '['wc -c < "P1 lipids pos mode Processed Norm.ibd"']' returned non-zero exit status 1.
First of all, as the exception says: Your command returned non-zero exit status 1. It means the called command is not correct (and failed). So you should fix your wc -c < "P1 lipids pos mode Processed Norm.ibd" command to make your code working.
On the other hand you can get the number of bytes:
my_str = "hello world" # In your case : my_str = subprocess.check_output([...
my_str_as_bytes = str.encode(my_str) # Convert string to byte type
type(my_str_as_bytes) # ensure it is byte representation
len(my_str_as_bytes) # Lenght (number) of bytes
BUT in Python3 the subprocess.check_output return bytes by default so the conversion is not needed only get the len of returned value.
For example:
import subprocess
nbytes_byte = subprocess.check_output(['wc -c < test.txt'], shell=True)
print(type(nbytes_byte))
print(len(nbytes_byte))
Content of test.txt:
Hello World
Output:
>>> python3 test.py
<class 'bytes'>
3
Furthermore here is a similar question: Python : Get size of string in bytes
EDIT:
I recommend to define the path of the IDB file based on your Python file path.
For example:
Your Python file path: /home/user/script/my_script.py
You IDB file path: /home/user/idb_files/P1 lipids pos mode Processed Norm.ibd
In the above case you should define the IDB file path:
import os
idb_file_path = os.path.join(os.path.dirname(__file__), "..", "idb_files", "P1 lipids pos mode Processed Norm.ibd")
Here is the complete example:
import os
import subprocess
# The "os.path.join" joins the path from the inputs
# The "os.path.dirname(__file__)" returns the path of the directory of the current script
idb_file_path = os.path.join(os.path.dirname(__file__), "..", "idb_files", "P1 lipids pos mode Processed Norm.ibd")
nbytes_byte = subprocess.check_output(['wc -c < "{}"'.format(idb_file_path)], shell=True)
print(type(nbytes_byte))
print(len(nbytes_byte))

Converting from string to integer to solve the ( ValueError: invalid literal for int() with base 10: '' ) error message in Python

I've a python code that has different functions, and the code has different functions and main function.
The main function is as follows:
def main():
while True:
ch = menu(['Soil Analysis and Yield Prediction','Supervised Learning','Unsupervised Learning','Exit'])
print('\n\n')
if ch==3:
break
elif ch==1:
Sup()
elif ch==2:
Usup()
else:
stdout.write('\nINVALID RESPONSE, TRY AGAIN .........\n\n')
#print('{:^204s}'.format('*'*204))
print('\n\n')
print('{:^204s}'.format('Authors:\tKshitij Jaiswal, Vibhav , Gaurav Khattar\n'))
print('{:^204s}'.format('THANKS YOU FOR USING OUR SOFTWARE'))
if __name__=='__main__':
main()
When the user enters 1, then certain functions will be called, and if the user enters 2, then other functions will be called.
When I'm running the program, I'm getting this error:
ValueError Traceback (most recent call last)
<ipython-input-11-6292cf16da20> in <module>
17
18 if __name__=='__main__':
---> 19 main()
20
<ipython-input-11-6292cf16da20> in main()
1 def main():
2 while True:
----> 3 ch = menu(['Soil Analysis and Yield Prediction','Supervised Learning','Unsupervised Learning','Exit'])
4 print('\n\n')
5 if ch==3:
<ipython-input-10-2a274c21fe19> in menu(x)
6 print(str(i)+'.',x[i])
7 stdout.write('\n\nEnter your Choice:\t')
----> 8 return int(stdin.readline())
ValueError: invalid literal for int() with base 10: ''
And the function menu() is the as follows:
def menu(x):
print('*'*204)
print('{:^204s}'.format(x[0]))
print('\n\n')
for i in range(1,len(x)):
print(str(i)+'.',x[i])
stdout.write('\n\nEnter your Choice:\t')
return int(stdin.readline())
I tried to return int(float(stdin.readline()) but it didn't work. Please any help would be appreciated.
You don't need to use stdout and stdin in order to achieve what you want, you can use input instead the both of them:
def menu(x):
print('*'*204)
print('{:^204s}'.format(x[0]))
print('\n\n')
for i in range(1,len(x)):
print(str(i)+'.',x[i])
return int(input('\n\nEnter your Choice:\t'))
It's possible that if you're running your code via some sort of IDE or even Jupyter, the program sends signals to your app, which affects your input.
This is what happened when I put the following loop in my Jupyter notebook (till I stopped it):
i = 0
while True:
i += 1
print(stdin.readline())
print(i)
Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
...
Also check this article to get a better understanding on the difference between input and stdin
https://www.geeksforgeeks.org/difference-between-input-and-sys-stdin-readline/

unable to read the .WAV file using python

I am trying to read a FM signal which is recorded as WAV file using GNU radio Companion, using python. I am attaching the .grc file used.
I can clearly hear the recorded signal but reading the data gives a null ([] ) value.
The python code
import soundfile as sf
data, fs = sf.read('/home/fm_record_RSM_10_01_2019_dat.wav')
for i in data:
print(i)
this gives
data
array([], dtype=float64)
fs
96000
When the following code is used,
import wave
input_wave_file= wave.open('/home/fm_record_RSM_10_01_2019_dat.wav', 'r')
nc,sw,fr,nf,ct,cn = input_wave_file.getparams()
another error is raised as given below
Error Traceback (most recent call last)
<ipython-input-3-5009fe3506e7> in <module>()
1 import wave
2
----> 3 input_wave_file= wave.open('/home/fm_record_RSM_10_01_2019_dat.wav', 'r')
4 nc,sw,fr,nf,ct,cn = input_wave_file.getparams()
5 frame_data = input_wave_file.readframes(5)
~/anaconda3/lib/python3.7/wave.py in open(f, mode)
508 mode = 'rb'
509 if mode in ('r', 'rb'):
--> 510 return Wave_read(f)
511 elif mode in ('w', 'wb'):
512 return Wave_write(f)
~/anaconda3/lib/python3.7/wave.py in __init__(self, f)
162 # else, assume it is an open file object already
163 try:
--> 164 self.initfp(f)
165 except:
166 if self._i_opened_the_file:
~/anaconda3/lib/python3.7/wave.py in initfp(self, file)
131 raise Error('file does not start with RIFF id')
132 if self._file.read(4) != b'WAVE':
--> 133 raise Error('not a WAVE file')
134 self._fmt_chunk_read = 0
135 self._data_chunk = None
Error: not a WAVE file
Could someone help me to find what the problem could be? Is it because of any mistake in the setting of record wav block in .grc file or any mistake in python file? Kindly help
Thanks a lot
Msr
#! /usr/bin/env python3
import soundfile as sf
import wave
import sys
if len(sys.argv) < 2:
print("Expected filename.wav on cmdline")
quit(1)
data, fs = sf.read(sys.argv[1])
for i in range(10):
print(i)
print('...')
input_wave_file= wave.open(sys.argv[1], 'r')
nc,sw,fr,nf,ct,cn = input_wave_file.getparams()
print('nc', nc)
print('sw', sw)
print('fr', fr)
print('nf', nf)
print('ct', ct)
print('cn', cn)
chunk = 1024
data = input_wave_file.readframes(chunk)
print('data[0:10] =', data[0:10])
print('data[0:10] =', end='')
for i in range(10):
print(data[i],' ',end='')
print('')
In a linux environment, I put the above into a file named playsound.py .
Then I executed (at the cmdline prompt)
$ chmod +x playsound.py
$ ./playsound.py file.wav
[ 0.06454468 0.05557251]
[ 0.06884766 0.05664062]
[ 0.0552063 0.06777954]
[ 0.04733276 0.0708313 ]
[ 0.05505371 0.065979 ]
[ 0.05358887 0.06677246]
[ 0.05621338 0.06045532]
[ 0.04891968 0.06298828]
[ 0.04986572 0.06817627]
[ 0.05410767 0.06661987]
...
nc 2
sw 2
fr 44100
nf 32768
ct NONE
cn not compressed
data[0:10] = b'C\x08\x1d\x07\xd0\x08#\x07\x11\x07'
data[0:10] =67 8 29 7 208 8 64 7 17 7
file.wav was some existing .wav file I had handy.
I previously tried
for i in data:
print(i)
as you had done, that worked also, but the output was too much.
I think you should check that the filename you are supplying points to a valid WAV file.
For instance, the path you list is "/home/filename.wav" .
Usually it will be at least "/home/username/filename.wav"

TypeError: unhashable type: 'Int64Index'

The section of my code that is causing me problems is
def Half_Increase(self):
self.keg_count=summer17.iloc[self.result_rows,2].values[0]
self.keg_count +=1
summer17[self.result_rows,2] = self.keg_count
print(keg_count)
So this function is to be executed when a button widget is pressed. It's supposed to get the value from a specific cell in a dataframe, add 1 to it, and then return the new value to the dataframe. (I'm not entirely sure if this is the proper way to do this.)
I get the following error
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python3.6\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "beerfest_program_v0.3.py", line 152, in Half_Increase
summer17[self.result_rows,2] = self.keg_count
File "C:\Python3.6\lib\site-packages\pandas\core\frame.py", line 2331, in __setitem__
self._set_item(key, value)
File "C:\Python3.6\lib\site-packages\pandas\core\frame.py", line 2397, in _set_item
value = self._sanitize_column(key, value)
File "C:\Python3.6\lib\site-packages\pandas\core\frame.py", line 2596, in _sanitize_column
if broadcast and key in self.columns and value.ndim == 1:
File "C:\Python3.6\lib\site-packages\pandas\core\indexes\base.py", line 1640, in __contains__
hash(key)
File "C:\Python3.6\lib\site-packages\pandas\core\indexes\base.py", line 1667, in __hash__
raise TypeError("unhashable type: %r" % type(self).__name__)
TypeError: unhashable type: 'Int64Index'
I'm guessing this has something to do with the variable types not matching but I've looked and cant find how to remedy this.
I think you need iloc:
summer17.iloc[result_rows,2] += 1
Sample:
summer17 = pd.DataFrame({'a':[1,2,3],
'b':[3,4,5],
'c':[5,9,7]})
#if reselt_rows is scalar
result_rows = 1
print(summer17)
a b c
0 1 3 5
1 2 4 9
2 3 5 7
summer17.iloc[result_rows,2] += 1
print(summer17)
a b c
0 1 3 5
1 2 4 10
2 3 5 7
It is same as:
#get value
keg_count=summer17.iloc[result_rows,2]
#increment
keg_count +=1
#set value
summer17.iloc[result_rows,2] = keg_count
print(summer17)
a b c
0 1 3 5
1 2 4 10
2 3 5 7
But if result_rows is list or 1d array:
result_rows = [1,2]
#get all values per positions defined in result_rows
#filter only first value by values[0]
keg_count=summer17.iloc[result_rows,2].values[0]
#increment
keg_count +=1
#set all values of result_rows by incremented value
summer17.iloc[result_rows,2] = keg_count
print(summer17)
a b c
0 1 3 5
1 2 4 10
2 3 5 10

"AssertionError: write() argument must be a bytes instance" when running WSGISOAPHandler

I have a SOAP server with pysimplesoap in Python 3.
Code
from wsgiref.simple_server import make_server
application = WSGISOAPHandler(dispatcher)
wsgid = make_server('', 8008, application)
wsgid.serve_forever()
I don't know why am I get the following error.
Error
Traceback (most recent call last):
File "/usr/lib/python3.4/wsgiref/handlers.py", line 138, in run
self.finish_response()
File "/usr/lib/python3.4/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/usr/lib/python3.4/wsgiref/handlers.py", line 266, in write
"write() argument must be a bytes instance"
AssertionError: write() argument must be a bytes instance
It's all because of WSGI is made for Python 2, so you can face some troubles using it in Python 3. If you dont want to change library's behavior like in first answer, workaround is to encode() all text data like:
def application(environ,start_response):
response_body = 'Hello World'
return [response_body.encode()]
Wsgi framework is built around Python 2. Therefore, if there is stuff in your program that does not include Python 3 dependencies, run the app with Python 2.
in "handlers.py" line 180
self.write(data.encode()) instead of self.write(data)
In my case the problem turned out to be that I was unintentionally outputting an object instead of a string. Fixed by encoding my result as a string via json.dumps(obj).
+++ pysimplesoap/server.py
e['name'] = k
if array:
e[:] = {'minOccurs': "0", 'maxOccurs': "unbounded"}
- if v in TYPE_MAP.keys():
- t = 'xsd:%s' % TYPE_MAP[v]
- elif v is None:
+
+ # check list and dict first to avoid
+ # TypeError: unhashable type: 'list' or
+ # TypeError: unhashable type: 'dict'
+ if v is None:
t = 'xsd:anyType'
elif isinstance(v, list):
n = "ArrayOf%s%s" % (name, k)
n = "%s%s" % (name, k)
parse_element(n, v.items(), complex=True)
t = "tns:%s" % n
+ elif v in TYPE_MAP.keys():
+ t = 'xsd:%s' % TYPE_MAP[v]
else:
raise TypeError("unknonw type v for marshalling" % str(v))
e.add_attribute('type', t)

Resources