Why cant my reader import the basereader from the file? - python

i am trying to run a file "reader_microphone.py" with some of its line below
import pyaudio
import numpy
import wave
from reader import basereader
class MicrophoneReader(BaseReader):
default_chunksize = 8192
default_format = pyaudio.paInt16
default_channels = 2
default_rate = 44100
default_seconds = 0
i am getting this error-
from reader import basereader
ImportError: cannot import name 'basereader' from 'reader' (C:\ProgramData\Anaconda3\lib\reader.py)

and welcome to StackOverflow.
There is no basereader reference in the reader package. You can take a look at the package's documentation here, to try and see what's the object you are looking for.

Related

Extract data from*.vtu file

I am trying to extract some data arrays from a *.vtu file
import numpy
import vtk.vtk
from vtk import vtkUnstructuredGridReader
from vtk.util import numpy_support as VN
reader = vtkUnstructuredGridReader()
reader.SetFileName("heterogeneousbox-00041.vtu")
reader.ReadAllVectorsOn()
reader.ReadAllScalarsOn()
reader.Update()
data = reader.GetOutput()
GasSaturation = data.GetPointData().GetScalars("Sn")
print(type(GasSaturation))
but I get this error: ModuleNotFoundError: No module named 'vtk'
PS: I am using pycharm and python3.8 as interpreter!
Thanks in advance

how to read pickle file

I ran the following code as well as different version of same code but I keep running into this specific error: **ModuleNotFoundError: No module named 'pandas.core.internals.managers'; 'pandas.core.internals' is not a package
**
Please see code below:
import pickle
pickle_off = open(r"C:\Users\database.pkl","rb")
df = pd.read_pickle(pickle_off)
Let me know if this works:
import pickle
file_name = 'data.p'
with open(file_name,mode='rb') as f:
data = pickle.load(f)

Mutagen: Importing Artwork From URL

I just started coding Python a few days ago. I'm still a noob but I'm familiar with other languages so I'm learning quick. I need help with this script I'm writing. I'm using Mutagen to tag m4a files but I'm having issues with saving artwork from a url.
(Python Version 3.7.4)
Mutagen Api: https://mutagen.readthedocs.io/en/latest/api/mp4.html
Below is code that works but it only works for local images. I need to be able to do the same thing but with an image from a url: https://is1-ssl.mzstatic.com/image/thumb/Music123/v4/e3/4a/e6/e34ae621-5922-140d-7db0-f6ce0b44d626/19UMGIM78396.rgb.jpg/1400x1400bb.jpg.
from mutagen.mp4 import MP4, MP4Cover
from time import sleep
from os import listdir, getcwd
import datetime
import requests
import urllib.request
.....MORECODE HERE......
with open('artwork.jpg', "rb") as f:
currentfile["covr"] = [
MP4Cover(f.read(), imageformat=MP4Cover.FORMAT_JPEG)
]
Now below is the code I have right now but Python keeps crashing every time I run it. I tried a couple different methods but I can't seem to figure it out. It has to be fairly simple for someone experienced with Python. I tried this already (didn't work) Python Mutagen: add cover photo/album art by url?. I think it might be for an older version of Python. Any ideas?
from mutagen.mp4 import MP4, MP4Cover
from time import sleep
from os import listdir, getcwd
import datetime
import requests
import urllib.request
.....MORE CODE HERE.....
fd = urllib.request.urlopen("https://is1-ssl.mzstatic.com/image/thumb/Music123/v4/e3/4a/e6/e34ae621-5922-140d-7db0-f6ce0b44d626/19UMGIM78396.rgb.jpg/1400x1400bb.jpg")
with open(fd, "rb") as f:
currentfile["covr"] = [
MP4Cover(f.read(), imageformat=MP4Cover.FORMAT_JPEG)
]
File "mutagen.py", line 11, in with open(fd, "rb") as f:
TypeError: expected str, bytes or os.PathLike object, not HTTPResponse
You might consider something as follows:
url_artwork="https://is1-ssl.mzstatic.com/image/thumb/Music123/v4/e3/4a/e6/e34ae621-5922-140d-7db0-f6ce0b44d626/19UMGIM78396.rgb.jpg/1400x1400bb.jpg"
urllib.request.urlretrieve(url_artwork,"local.jpg")
with open("local.jpg", "rb") as f:
currentfile["covr"] = [
MP4Cover(f.read(), imageformat=MP4Cover.FORMAT_JPEG)
]

Import binary file in my library through a name

I want to import a binary file within a module through a name.
For example:
mylibrary
|
+---test (name of submodule)
|
+---TestData (name representing the binary file)
|
+---testdata.bin (the actual binary file)
I want:
import numpy as np
import mylibrary.test.TestData as TestData
with open(TestData, 'rb') as f:
np_array = np.load(f)
Is that a good idea? How to make this work?
You cannot do it like that, Python will not recognize non Python files when importing. But you can add __init__.py file to the test folder and place in it:
import os
TestData = os.path.join(os.path.dirname(os.path.realpath(__file__)), "testdata.bin")
Then you can easily load it in your other projects as:
import numpy as np
from mylibrary.test import TestData
with open(TestData, 'rb') as f:
np_array = np.load(f)

Can not save file using the below python code. Error: numpy.ndarray object has no attribute 'save'

import os
import sys
import numpy as np
import scipy
import pylab
import pymorph
import mahotas
import matplotlib.pyplot as plt
import Image
from scipy import ndimage
from pymorph import regmax
from PIL import Image
path='all_images'
for file in os.listdir(path):
current = os.path.join(path, file)
extension = os.path.splitext(current)[-1]
fileType = extension.upper()
print(current)
if os.path.isfile(current):
img = mahotas.imread(current)
imgf = ndimage.gaussian_filter(img, 8)
pylab.gray()
imgf.save('dnaa.gif')
Can not save file using the below python code. Error: numpy.ndarray object has no attribute 'save'. Can anyone help how to save file using pylab. I guss the last line of the code has some issue.
Use mahotas.imsave('dnaa.gif', imgf) instead. The NumPy array you get from gaussian_filter doesn't have save functionality built in.

Categories