Able to import module but cannot use function inside it - python-3.x

I have a function called learn in a module called lrn_proto.
I use it at other script, I can import it, but I'm not able to use it's function, it works before. But right now its not working anymore, and I even didn't modify the source code.
Here is the source of lrn_proto:
import sys
import os
import re
import time
sys.path.append(os.path.abspath('..\\..\\..\\conf'))
sys.path.append(os.path.abspath('..\\..\\writer\\mem_writer_proto'))
from cfg_exe import exe_loc as loc
sys.path.append(os.path.abspath(loc.lib))
sys.path.append(os.path.abspath(loc.mem))
sys.path.append(os.path.abspath(loc.main))
import mem_writer as mw
import prototype_ui as ptui
def learn(src,keywords,type,identifier):
if src == r'\exit':
print('You have choose to exit this program, see you later.')
sys.exit()
elif src == r'\add':
ptui.menu()
temp = src.lower()
for x in keywords:
if x in temp:
temp_k1 = list(temp.partition(x))
try:
temp_k1_t = list(str(temp_k1.index(x)))
except ValueError:
pass
for b in temp_k1_t:
temp_k = list([temp_k1.pop(int(b))])
print('True\n')
continue
elif x not in temp:
temp_k = list('')
pass
for x in type:
if x in temp:
temp_t1 = list(temp.partition(x))
try:
temp_t1_t = list(str(temp_t1.index(x)))
except ValueError:
pass
for b in temp_t1_t:
temp_t = list([temp_t1.pop(int(b))])
print('True\n')
continue
elif x not in temp:
temp_t = list('')
pass
for x in identifier:
if x in temp:
temp_i1 = list(temp.partition(x))
try:
temp_i1_t = list(str(temp_i1.index(x)))
except ValueError:
pass
for b in temp_i1_t:
temp_i = list([temp_i1.pop(int(b))])
print('True\n')
continue
elif x not in temp:
temp_i = list('')
pass
temp_kti = list(temp_k+temp_t+temp_i)
act_patt = '|'.join(temp_kti)
act = (''.join((re.split(act_patt,temp))).strip(' .'))
print('Output:')
print('Keywords: ',temp_k,'\nType: ',temp_t,'\nIdentifier: ',temp_i,'\nContent: ',act)
mw.dict(title,type,keywords,identifier,actions)
I tried:
from lrn_proto import learn
It produce:
ImportError: cannot import name 'learn' from 'lrn_proto'
Then with
import lrn_proto,
It produce this while the function is being called:
AttributeError: module 'lrn_proto' has no attribute 'learn'
The weird things are Those produced error doesn't tell me whats wrong with my code.
I had already add all modules directory to sys.path
Did anyone can inform me what is the wrong of my code?
I had no idea about what's wrong with my code.
I'm using Python3 on Windows.
Kindly ask me if you need more info about this question.

Related

Load pure Python module from in-memory zipfile

From this, I was able to make this:
import os
import types
import zipfile
import sys
import io
class ZipImporter(object):
def __init__(self, zip_file):
self.zfile = zip_file
self._paths = [x.filename for x in self.zfile.filelist]
def _mod_to_paths(self, fullname):
# get the python module name
py_filename = fullname.replace(".", os.sep) + ".py"
# get the filename if it is a package/subpackage
py_package = fullname.replace(".", os.sep) + "/__init__.py"
print(py_package)
if py_filename in self._paths:
return py_filename
elif py_package in self._paths:
return py_package
else:
return None
def find_module(self, fullname, path):
if self._mod_to_paths(fullname) is not None:
return self
return None
def load_module(self, fullname):
filename = self._mod_to_paths(fullname)
if not filename in self._paths:
raise ImportError(fullname)
new_module = types.ModuleType(fullname)
new_module.__name__=fullname
print(fullname)
exec(self.zfile.open(filename, 'r').read(),new_module.__dict__)
new_module.__file__ = filename
new_module.__loader__ = self
if filename.endswith("__init__.py"):
new_module.__path__ = []
new_module.__package__ = fullname
else:
new_module.__package__ = fullname.rpartition('.')[0]
sys.modules[fullname]=new_module
return new_module
module_zip=zipfile.ZipFile(io.BytesIO(),"w")
for key in module_dict:
module_zip.writestr(key,module_dict[key])
sys.meta_path.append(ZipImporter(module_zip))
import pyparsing
Using the source code of pyparsing as a test. However, it fails with ImportError: attempted relative import with no known parent package. Even if I replace all the relative imports with absolute imports, it fails with RecursionError: maximum recursion depth exceeded while calling a Python object, as it tries to import pyparsing repeatedly. Is there something fundamental I'm not understanding about the way Python's import system works?
I found the answer --- PEP 302 says that:
Note that the module object must be in sys.modules before the loader executes the module code. This is crucial because the module code may (directly or indirectly) import itself; adding it to sys.modules beforehand prevents unbounded recursion in the worst case and multiple loading in the best.

Why wont this work? - def (function) not being called from main()

I need to be able to use classes, but trying to just get my simple code to work
import pandas as pd, numpy as np
class OutOfCountry7ViewModel():
def pandas_conversion(self):
#from csv import readers
deImport = pd.read_csv("ooc-exceptions.csv")
d1 = pd.read_csv("CS_Out_Of_Country.csv", encoding='windows-1252', parse_dates=True)
d2 = pd.read_csv("sccm-devices.csv", encoding='windows-1252')
d3 = pd.read_csv("CTLDAPRawData.csv", encoding='windows-1252')
#pandas join magic
lj_df1 = pd.merge(d1, d2, left_on="ComputerName", right_on="Name", how="left")
lj_df2 = pd.merge(d2, d3, left_on="PrimaryUser", right_on="Employee Number", how="left")
#lj_df = plj_df1d.join(lj_df2, lsuffix=)
df = (lj_df1)
#print(df)
df.to_csv('CS_Out_of_country_tabl.csv', index=False,header=df.columns, encoding='utf-8')
csv = 'CS_Out_of_country_tabl.csv'
return csv
def main():
pandas_conversion(self)
if __name__ == '__main__':
main()
i keep getting an error, NameError: name 'pandas_conversion' is not defined
Are you trying to do something like this? -
import pandas as pd, numpy as np
class OutOfCountry7ViewModel():
def pandas_conversion(self,csv):
...
def main(self):
self.pandas_conversion(csv)
if __name__ == '__main__':
some_object = OutOfCountry7ViewModel()
some_object.main()
This should work:
a = OutOfCountry7ViewModel()
a.pandas_conversion()
Hope this helped!
Try to remember the semantics and indentation of python.
Unused import numpy
Class/Parent Class has no (), Line 3
class OutOfCountry7ViewModel(): #wrong
class OutOfCountry7ViewModel: #right
There is no need of ()
df = (lj_df1)
#if you using some func then you miss that func name
If you're defining a method in the class you've to add instance self
def main(self):
pandas_conversion(self) #wrong calling func with parameter
I think your code is wrong because in PyCharm, it says def pandas_conversion(self): may be static.
So, your code is incomplete, there is something missing that we can't find.

od.download() == None but it's not empty | Exception handling Opendatasets

Update: turns out od.download() returns None by design.
What might be better than a None check for od.download() "failure"?
I am downloading a .zip file using opendatasets lib.
In iris_scans(); line print(download), without the if-statement prints None.
However, at invocation scans = iris_scans() data is returned and subsequent prints can display data successfully.
The purpose of the if-statement is for "Graceful error handling".
Note: I've used an if-statement instead of try-except as there are many possibilities why download == None (e.g. dead link, connection interrupt etc.)
pip3 install opendatasets
import opendatasets as od
import zipfile
import os
import shutil
from PIL import Image
import numpy as np
def iris_scans():
download = od.download('http://www.mae.cuhk.edu.hk/~cvl/iris_database/iris_database.zip')
"""
if download == None:
print('Iris Scans - Link could not be established')
return [[]*1778]
"""
print(download)
path_extract = 'iris_database/'
with zipfile.ZipFile('iris_database.zip', 'r') as zip_ref:
zip_ref.extractall(path_extract)
os.remove(path_extract + 'readme.txt')
filenames = os.listdir(path_extract)
scans = []
for f in filenames:
img = Image.open(path_extract + f)
#print("IMG", img)
matrix = np.array(img)
#print("MATRIX", matrix)
scans.append(matrix)
shutil.rmtree(path_extract)
os.remove(path_extract[:-1] + '.zip')
# Data Augmentation
scans_90 = [np.rot90(s) for s in scans]
scans_180 = [np.rot90(s) for s in scans_90]
scans_270 = [np.rot90(s) for s in scans_180]
scans_flip = [np.flip(s) for s in scans]
scans_flip_90 = [np.rot90(s) for s in scans_flip]
scans_flip_180 = [np.rot90(s) for s in scans_flip_90]
scans_flip_270 = [np.rot90(s) for s in scans_flip_180]
scans += scans_90
scans += scans_180
scans += scans_270
scans += scans_flip_90
scans += scans_flip_180
scans += scans_flip_270
return scans
scans = iris_scans()
print(scans[0])
print(len(scans))
The original question was a road-block on the path of implementing some form of Exception Handling for the download.
od.download() == None by design; so an alternative to if download == None needed to be made.
As pointed out and assisted by #Henry; the below Try-except incorporates all exceptions found in the Github source.
...
import urllib
def iris_scans():
try:
download = od.download('http://www.dgcdgyugcwyugyugcasc.com/wqduiuwqdwq') # BROKEN
...
return scans
except (urllib.error.URLError, IOError, RuntimeError) as e:
print('Iris Scans - failed')
return [[]*1778]
Iris Scans - failed
[]
1
Top answer to this post demos many exceptions on one line.

Python freezes when accessing string value in subprocess

I spent nearly the whole day with this and came to the end of my knowledge:
I want to change a shared multiprocessing.Value string in the subprocess, but python hangs as soon as the subprocess is trying to change the shared value.
Below an example code:
from multiprocessing import Process, Value, freeze_support
from ctypes import c_wchar_p
def test(x):
with x.get_lock():
x.value = 'THE TEST WORKED'
return
if __name__ == "__main__":
freeze_support()
value = Value(c_wchar_p, '')
p = Process(target=test, args = (value,))
p.start()
print(p.pid)
# this try block is to also allow p.run()
try:
p.join()
p.terminate()
except:
pass
print(value.value)
What I tried and does not work:
I tried ctypes c_wchar_p and c_char_p, but both result in the same freezing.
I tried also without x.get_lock()
I tried also without freeze_support()
What works (but does not help):
Using a float as the shared value (value = Value('d',0) and x.value = 1).
Running the Process without starting a subprocess (replace p.start() with p.run() )
I am using Windows 10 64 bit and Python 3.6.4 (Spyder, but also tried outside of Spyder).
Any help welcome!
A shared pointer won't work in another process because the pointer is only valid in the process in which it was created. Instead, use an array:
import multiprocessing as mp
def test(x):
x.value = b'Test worked!'
if __name__ == "__main__":
x = mp.Array('c',15)
p = mp.Process(target=test, args = (x,))
p.start()
p.join()
print(x.value)
Output:
b'Test worked!'
Note that array type 'c' is specialized and returns a SynchronizedString vs. other types that return SynchronizedArray. Here's how to use type 'u' for example:
import multiprocessing as mp
from ctypes import *
def test(x):
x.get_obj().value = 'Test worked!'
if __name__ == "__main__":
x = mp.Array('u',15)
p = mp.Process(target=test, args = (x,))
p.start()
p.join()
print(x.get_obj().value)
Output:
Test worked!
Note that operations on the wrapped value that are non-atomic such as += that do read/modify/write should be protected with a with x.get_lock(): context manager.

Been trying to overlap these two images but I keep getting a TypeError: 'NoneType' object is not callable

I have been trying to get this work for a very long time but I couldn't seem to get it to work. I tried to import Image as well but canopy couldn't name the module Image. I really need some help with this one. Someone help me please, I'm so stuck right now.
import PIL
import matplotlib.pyplot as plt
import os.path
import PIL.ImageDraw
def get_images(directory=None):
if directory == None:
directory = os.getcwd() # Use working directory if unspecified
image_list = [] # Initialize aggregaotrs
file_list = []
directory_list = os.listdir(directory) # Get list of files
for entry in directory_list:
absolute_filename = os.path.join(directory, entry)
try:
image = PIL.Image.open(absolute_filename)
file_list += [entry]
image_list += [image]
except IOError:
pass # do nothing with errors tying to open non-images
return image_list, file_list
def ben(x_range=None):
background = PIL.Image.open("funland.jpg")
overlay = PIL.Image.open("shivam2.jpg")
background = background.convert("RGBA")
overlay = overlay.convert("RGBA")
background_pixels = background.load()
overlay_pixels = overlay.load()
if x_range == None:
for y in x_range(overlay.size[1]):
for x in x_range(overlay.size[0]):
background_pixels[x,y] = (background_pixels[x,y][0], background_pixels[x,y][1], background_pixels[x,y][2], 255)
if x_range == None:
for y in x_range(overlay.size[1]):
for x in x_range(overlay.size[0]):
overlay_pixels[x,y] = (overlay_pixels[x,y][0], overlay_pixels[x,y][1], overlay_pixels[x,y][2], 128)
new_img = PIL.Image.blend(background, overlay, 0.5)
new_img.save("new.png","PNG")
The error is in these two lines:
if x_range == None:
for y in x_range(overlay.size[1]):
First, you check if the range is a None object, then you try to call it with (overlay.size[1]). I guess you meant x_range != None?

Resources