class Program:
def Main(self):
fname = QFileDialog.getOpenFileName()
filepath = str(fname[0])
fileObject = filepath.split('/')
file = fileObject[len(fileObject) - 1]
def image(self, Main):
img1 = cv2.imread(Main.file())
i want to put in img1 the file from function Main() but i get
Traceback (most recent call last):
File "test.py", line 7, in image
img1 = cv2.imread(Main.file())
AttributeError: 'Program' object has no attribute 'file'
There are some weird things in your code as far as I can see:
I don't know how you're calling the image method, but it currently receives a Main value. Correct me if I'm wrong since this is pure speculation, but I think you wanted to call the Main method of Program. If so, that's not how you do it. You can refactor it as:
class Program:
def Main(self):
fname = QFileDialog.getOpenFileName()
filepath = str(fname[0])
fileObject = filepath.split('/')
file = fileObject[len(fileObject) - 1]
# Remove Main from method signature, since it's a method from Program
def image(self):
# Call Main from the self object
img1 = cv2.imread(self.Main.file())
You're calling self.Main.file() inside the image method. Since Main is a method and it does not have another method called file, it'll throw a AttributeError. Another pure speculation, but I think you wanted to access the variable file inside the Main method. If so, that's not how you do it. You can't access a variable inside another method, but you can access a value returned by that method. You can refactor your Main to return the file that you want (and rename it to a proper name, such as get_file):
class Program:
# Rename Main to a more descriptive name
def get_file(self):
fname = QFileDialog.getOpenFileName()
filepath = str(fname[0])
fileObject = filepath.split('/')
# Return it so we can access this value outside of it
return fileObject[len(fileObject) - 1]
def image(self):
# Access the file returned by get_file
# Since this a method of Program, call it using the self object
file = self.get_file()
# Use the returned value
img1 = cv2.imread(file)
Some minor improvements:
class Program:
# Rename Main to a more descriptive name
def get_file(self):
fname = QFileDialog.getOpenFileName()
filepath = str(fname[0])
fileObject = filepath.split('/')
# You can remove use -1 as index to the last value
return fileObject[-1]
def image(self):
file = self.get_file()
# Probably you want to return the variable below
return cv2.imread(file)
Full code:
class Program:
def get_file(self):
fname = QFileDialog.getOpenFileName()
filepath = str(fname[0])
fileObject = filepath.split('/')
return fileObject[-1]
def image(self):
file = self.get_file()
return cv2.imread(file)
Related
How can I implement a static property or method-pair in Python using a sharable and accessible static attribute?
class StaticClass:
__static_attr: str
#classmethod
def set(cls, input__):
cls.__static_class = input__
#classmethod
def get(cls):
return cls.__static_attr
StaticClass.set("Hello!")
print(StaticClass.get())
Output:
C:\Users\pc\AppData\Local\Microsoft\WindowsApps\python3.7.exe C:/Users/pc/source/repos/main.py
Traceback (most recent call last):
File "C:/Users/pc/source/repos/main.py", line 15, in <module>
print(StaticClass.get())
File "C:/Users/pc/source/repos/main.py", line 11, in get
return cls.__static_attr
AttributeError: type object 'StaticClass' has no attribute '_StaticClass__static_attr'
Process finished with exit code 1
Edit: The above source code has a typo in set(). If that typo is fixed, the code runs perfectly.
However, in the case of my original source code, that attribute was a List[str]. In that case, the program only runs only if the attribute is initialized through a pair of square brackets [].
import os
from typing import List
class SearchFilesInTheDirectory:
__file_list: List[str] = []
#classmethod
def do_indexing_of_files(cls, path, file_extension):
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith(file_extension):
cls.__file_list.append(os.path.join(root, file))
#classmethod
def get_files_list(cls):
return cls.__file_list
#classmethod
def printf(cls):
if cls.__file_list is not None:
for file in cls.__file_list:
print(file)
else:
print("directory is empty")
#classmethod
def write_to_file(cls, file_name):
if cls.__file_list is not None:
with open(file_name, 'w') as f:
for f_name in cls.__file_list:
f.write("%s\t%s\n" % f_name)
else:
print("directory is empty")
It would be a better idea is to initialize the class static variable, not just declare its type. This way you would be able to call get even before set, and have it return a default value:
class StaticClass:
__static_attr = ""
#classmethod
def set(cls, input__):
cls.__static_attr = input__
#classmethod
def get(cls):
return cls.__static_attr
StaticClass.set("Hello!")
print(StaticClass.get())
Also, it seems like your original code had a typo, as a result of which set method was assigning to a variable other than the one you were declaring (__static_class instead of __static_attr). Because of that, and because the static variable was not initialized, your get method failed.
I am new to python and am trying to run unit tests injected with data from an external file:
class TestIsEquivalent(unittest.TestCase):
filename = os.getcwd() + '\\data\\test-data.txt'
with open(filename) as f:
testdata = f.readlines()
for value in testdata:
print(value)
def test_0(self):
result = isEquivalent(value, value, 0)
self.assertEqual(result, True)
I am getting the following error on line isEquivalent() is called with in the test function:
NameError: name 'value' is not defined
This would seem to suggest a scoping issue but I cannot figure out how to pass the value in the for loop into the test method. Any suggestions would be much appreciated.
Your complete test case should be wrapped inside a function. I'm not completely sure what you're trying to do with a function definition inside a for loop, but moving def test_0(self) to the top of your classing does the trick.
class TestIsEquivalent(unittest.TestCase):
def test_0(self):
filename = os.getcwd() + '\\data\\test-data.txt'
with open(filename) as f:
testdata = f.readlines()
for value in testdata:
print(value)
result = isEquivalent(value, value, 0)
self.assertEqual(result, True)
But if you want to create multiple test cases that all you the values inside of testdata, and you dont want to read the file multiple times, you can use the setUpClass function that is executed once at the start of all unittests inside a class. The result would look like this.
class TestIsEquivalent(unittest.TestCase):
#classmethod
def setUpClass(cls):
filename = os.getcwd() + '\\data\\test-data.txt'
with open(filename) as f:
cls.testdata = f.readlines()
def test_0(self):
for value in self.testdata:
print(value)
result = isEquivalent(value, value, 0)
self.assertEqual(result, True)
This is an exercise that I wanted to try because I thought it was interesting. The exercise is unnecessarily complex for what it is doing, but it is trying to act as practice for understanding class, function and variable behavior in more complex python programs.
import os
class grabFile:
fileObject = None
def __init__(self, filename):
self.fileObject = open(filename, "r")
def getFile():
return self.fileObject
class counter:
fileC = None
lineCount = 0
def __init__(self, fileObject):
self.fileC = fileObject
def lineCounter(self):
while True:
self.fileC.readline()
print(x)
return lineCount
def Main():
fileGrabber = grabFile("test.txt")
fileObj = fileGrabber.getFile
countObj = counter(fileObj)
lineCount = countObj.lineCounter()
print(lineCount)
Main()
However, when I run this, I get the following error:
Traceback (most recent call last):
File "/home/may/Desktop/Tree/Programming/MiscProjects/TextAnalyzer.py", line 32, in <module>
Main()
File "/home/may/Desktop/Tree/Programming/MiscProjects/TextAnalyzer.py", line 29, in Main
lineCount = countObj.lineCounter()
File "/home/may/Desktop/Tree/Programming/MiscProjects/TextAnalyzer.py", line 19, in lineCounter
self.fileC.readline()
AttributeError: 'function' object has no attribute 'readline'
[Finished in 0.2s with exit code 1]
Can anyone help me understand this program fully? And also, although this is not the correct place to ask, offer any critique on styling or formatting of the program? Especially one the use of "self".
Thank you!
I think you meant to call the method:
fileObj = fileGrabber.getFile()
And you need to change to instance method:
def getFile(self):
return self.fileObject
And your line counter method needs some work:
def lineCounter(self):
self.lineCount = len(self.fileC.readlines())
return self.lineCount
I'm writing a test runner. I have an object that can catch and store exceptions, which will be formatted as a string later as part of the test failure report. I'm trying to unit-test the procedure that formats the exception.
In my test setup, I don't want to actually throw an exception for my object to catch, mainly because it means that the traceback won't be predictable. (If the file changes length, the line numbers in the traceback will change.)
How can I attach a fake traceback to an exception, so that I can make assertions about the way it's formatted? Is this even possible? I'm using Python 3.3.
Simplified example:
class ExceptionCatcher(object):
def __init__(self, function_to_try):
self.f = function_to_try
self.exception = None
def try_run(self):
try:
self.f()
except Exception as e:
self.exception = e
def format_exception_catcher(catcher):
pass
# No implementation yet - I'm doing TDD.
# This'll probably use the 'traceback' module to stringify catcher.exception
class TestFormattingExceptions(unittest.TestCase):
def test_formatting(self):
catcher = ExceptionCatcher(None)
catcher.exception = ValueError("Oh no")
# do something to catcher.exception so that it has a traceback?
output_str = format_exception_catcher(catcher)
self.assertEquals(output_str,
"""Traceback (most recent call last):
File "nonexistent_file.py", line 100, in nonexistent_function
raise ValueError("Oh no")
ValueError: Oh no
""")
Reading the source of traceback.py pointed me in the right direction. Here's my hacky solution, which involves faking the frame and code objects which the traceback would normally hold references to.
import traceback
class FakeCode(object):
def __init__(self, co_filename, co_name):
self.co_filename = co_filename
self.co_name = co_name
class FakeFrame(object):
def __init__(self, f_code, f_globals):
self.f_code = f_code
self.f_globals = f_globals
class FakeTraceback(object):
def __init__(self, frames, line_nums):
if len(frames) != len(line_nums):
raise ValueError("Ya messed up!")
self._frames = frames
self._line_nums = line_nums
self.tb_frame = frames[0]
self.tb_lineno = line_nums[0]
#property
def tb_next(self):
if len(self._frames) > 1:
return FakeTraceback(self._frames[1:], self._line_nums[1:])
class FakeException(Exception):
def __init__(self, *args, **kwargs):
self._tb = None
super().__init__(*args, **kwargs)
#property
def __traceback__(self):
return self._tb
#__traceback__.setter
def __traceback__(self, value):
self._tb = value
def with_traceback(self, value):
self._tb = value
return self
code1 = FakeCode("made_up_filename.py", "non_existent_function")
code2 = FakeCode("another_non_existent_file.py", "another_non_existent_method")
frame1 = FakeFrame(code1, {})
frame2 = FakeFrame(code2, {})
tb = FakeTraceback([frame1, frame2], [1,3])
exc = FakeException("yo").with_traceback(tb)
print(''.join(traceback.format_exception(FakeException, exc, tb)))
# Traceback (most recent call last):
# File "made_up_filename.py", line 1, in non_existent_function
# File "another_non_existent_file.py", line 3, in another_non_existent_method
# FakeException: yo
Thanks to #User for providing FakeException, which is necessary because real exceptions type-check the argument to with_traceback().
This version does have a few limitations:
It doesn't print the lines of code for each stack frame, as a real
traceback would, because format_exception goes off to look for the
real file that the code came from (which doesn't exist in our case).
If you want to make this work, you need to insert fake data into
linecache's
cache (because traceback uses linecache to get hold of the source
code), per #User's answer
below.
You also can't actually raise exc and expect the fake traceback
to survive.
More generally, if you have client code that traverses tracebacks in
a different manner than traceback does (such as much of the inspect
module), these fakes probably won't work. You'd need to add whatever
extra attributes the client code expects.
These limitations are fine for my purposes - I'm just using it as a test double for code that calls traceback - but if you want to do more involved traceback manipulation, it looks like you might have to go down to the C level.
EDIT2:
That is the code of linecache.. I will comment on it.
def updatecache(filename, module_globals=None): # module_globals is a dict
# ...
if module_globals and '__loader__' in module_globals:
name = module_globals.get('__name__')
loader = module_globals['__loader__']
# module_globals = dict(__name__ = 'somename', __loader__ = loader)
get_source = getattr(loader, 'get_source', None)
# loader must have a 'get_source' function that returns the source
if name and get_source:
try:
data = get_source(name)
except (ImportError, IOError):
pass
else:
if data is None:
# No luck, the PEP302 loader cannot find the source
# for this module.
return []
cache[filename] = (
len(data), None,
[line+'\n' for line in data.splitlines()], fullname
)
return cache[filename][2]
That means before you testrun just do:
class Loader:
def get_source(self):
return 'source of the module'
import linecache
linecache.updatecache(filename, dict(__name__ = 'modulename without <> around',
__loader__ = Loader()))
and 'source of the module' is the source of the module you test.
EDIT1:
My solution so far:
class MyExeption(Exception):
_traceback = None
#property
def __traceback__(self):
return self._traceback
#__traceback__.setter
def __traceback__(self, value):
self._traceback = value
def with_traceback(self, tb_or_none):
self.__traceback__ = tb_or_none
return self
Now you can set the custom tracebacks of the exception:
e = MyExeption().with_traceback(1)
What you usually do if you reraise an exception:
raise e.with_traceback(fake_tb)
All exception prints walk through this function:
import traceback
traceback.print_exception(_type, _error, _traceback)
Hope it helps somehow.
You should be able to simply raise whatever fake exception you want where you want it in your test runs. The python exception docs suggest you create a class and raise that as your exception. It's section 8.5 of the docs.
http://docs.python.org/2/tutorial/errors.html
Should be pretty straightforward once you've got the class created.
I wrote a simple program to read through a log and to parse through and obtain the lowest beginning number (the head) and to print it. I am now editing that program and combining it with a class I wrote to parse an actual logfile. Essentially, as opposed to sorting based off of the simple number from the log from my previous program, I now need to reference the parsed information from one class into another class. I was wondering what the most convenient way to do this. I am a beginner programmer in python and don't know if I can explicitly reference the class.
Here are the classes.
Parser
class LogLine:
SEVERITIES = ['EMERG','ALERT','CRIT','ERR','WARNING','NOTICE','INFO','DEBUG']
severity = 1
def __init__(self, line):
try:
m = re.match(r"^(\d{4}-\d{2}-\d{2}\s*\d{2}:\d{2}:\d{2}),?(\d{3}),?(\s+\[(?:[^\]]+)\])+\s+[A-Z]+\s+(\s?[a-zA-Z0-9\.])+\s?(\((?:\s?\w)+\))\s?(\s?.)+", line)
timestr, msstr, sevstr, self.filename, linestr, self.message = m.groups()
self.line = int(linestr)
self.sev = self.SEVERITIES.index(sevstr)
self.time = float(calendar.timegm(time.strptime(timestr, "%Y-%m-%d %H:%M:%S,%f"))) + float(msstr)/1000.0
dt = datetime.strptime(t, "%Y-%m-%d %H:%M:%S,%f")
except Exception:
print 'error',self.filename
def get_time(self):
return self.time
def get_severity(self):
return self.sev
def get_message(self):
return self.message
def get_filename(self):
return self.filename
def get_line(self):
return self.line
Sorter
class LogFile:
def __init__(self,filepath):
self.logfile = open(filepath, "r")
self.head = None
def __str__(self):
return "x=" + str(self.x) + "y="+str(self.y)
def readline(self):
if self.head != None:
h = self.head
self.head = None
return h
else:
return self.logfile.readline().rstrip(' ')
def get_line(self):
if self.head == None:
self.head = self.readline().rstrip(' ')
return self.head.get.line()
else:
return self.head.get.line()
def close (self):
self.logfile.close()
I have begun to edit my second class by adding the get_line function. Don't know if I'm on the right track.
In simpler terms, I need the head to become "LogLine"
It is okay to use one class from another class. You have one class that parses a single line from a log file and builds an object that represents the line; and you have another class that reads lines from a log file. It would be very natural for the second class to call the first class.
Here is a very simple class that reads all lines from a log file and builds a list:
class LogFile(object):
def __init__(self,filepath):
with open(filepath, "r") as f:
self.lst = [LogLine(line) for line in f]
You can see that self.lst is being set to a list of lines from the input log file, but not just the text of the line; the code is calling LogLine(line) to store instances of LogLine. If you want, you can sort the list after you build it:
self.lst.sort(key=LogLine.get_line)
If the log files are very large, it might not be practical to build the list. You have a .get_line() method function, and we can use that:
class LogFile(object):
def __init__(self,filepath):
self.logfile = open(filepath, "r")
def get_line(self):
try:
line = next(self.logfile) # get next line from open file object
return LogLine(line)
except StopIteration: # next() raises this when you reach the end of the file
return None # return
def close(self):
self.logfile.close()
An open file object (returned by the open() function) can be iterated. We can call next() on this object and it will give us the next input line. When the end of file is reached, Python will raise StopIteration to signal the end of the file.
Here the code will catch the StopIteration exception and return None when the end of the log file is reached. But I think this isn't the best way to handle this problem. Let's make the LogFile class work in for loops and such:
class LogFile(object):
def __init__(self,filepath):
self.f = open(filepath)
def __next__(self): # Python 3.x needs this to be named "__next__"
try:
line = next(self.f)
return LogLine(line)
except StopIteration:
# when we reach the end of input, close the file object
self.f.close()
# re-raise the exception
raise
next = __next__ # Python 2.x needs this to be named "next"
A for loop in Python will repeatedly call the .__next__() method function (Python 3.x) or else the .next() method function (Python 2.x) until the StopIteration exception is raised. Here we have defined both method function names so this code should work in Python 2.x or in Python 3.x.
Now you can do this:
for ll in LogFile("some_log_file"):
... # do something with ll, which will always be a LogLine instance