#๐ help with getters and setters
55 messages ยท Page 1 of 1 (latest)
@pale kite
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
class InitialScan:
def __init__(self):
total_size_h264Files = 0
total_size_hevcFiles = 0
total_size_otherFiles = 0
error_files = []
def get_total_size_h264Files(self):
return self.total_size_h264Files
@staticmethod
def perform_initial_scan(self, folder_path: Path, debug=False):
if not folder_path.is_dir():
print("This is not a directory path! \n Exiting.")
exit()
for file_path in folder_path.iterdir():
file_name = file_path.name
# Converts file size from bytes to Megabytes
file_size = int(file_path.stat().st_size / (1024 * 1024))
# Check if it's a regular file (not a directory)
if file_path.is_file():
try:
codec = codec_extractor(file_path=file_path)
if codec == "h264":
h264_files.append(file_name)
h264_file_paths.append(file_path)
self.total_size_h264 += file_size
elif codec == "hevc":
hevc_files.append(file_name)
hevc_file_paths.append(file_path)
self.total_size_hevc += file_size
else:
other_files.append(file_name)
other_file_paths.append(file_path)
self.total_size_other += file_size
if debug:
print(f"File: {file_name}, Path: {file_path}, Size: {file_size} MB")
print(codec)
except Exception as e:
print('something went wrong')
print(e)
self.error_files.append(file_name)
And where do you want to use getters and setters in this code?
By the way, you should set self.var, not just var
def __init__(self):
self.total_size_h264Files = 0
self.total_size_hevcFiles = 0
self.total_size_otherFiles = 0
self.error_files = []
i get error with my get_total_size_h264Files()
There are also more problems like
@staticmethod
def perform_initial_scan(self, folder_path: Path, debug=False):
```A `@staticmethod` doesn't have access to a `self`
```py
if codec == "h264":
h264_files.append(file_name)
h264_file_paths.append(file_path)
self.total_size_h264 += file_size
```more variables that might want to be `self.(...)` instead, your call on this one
where do u mean? i dont see var here in the snippet u sent
var as in any variable
Those variables you set in __init__ won't be in self.(...)
cause you need this instead of what you have
def __init__(self):
total_size_h264Files = 0
total_size_hevcFiles = 0
total_size_otherFiles = 0
error_files = []```
like this right?
no
this
alright i have fixed this , what next?
fix the staticmethod thing
done
And also, where would you want to use setters/getters?
removed the decorators
get_total_size_h264Files()
on this func
so i can access total_size_h264Files
from an object
wait
i think
these works now
The pythonic way is
def __init__(self, ...):
self._total_size_h264Files = 0 # leading _ to avoid name clash...
# ...here
@property
def total_size_h264Files(self):
return self._total_size_h264Files
because we fixed self in init() ?
yeah earlier it was not working
i think because of the init func being incorrectly done
Though you really don't need a getter/setter if you just want to access/change an attribute
They're useful in certain scenarios, but if you're just doing this
def __init__(self, ...):
self._total_size_h264Files = 0
@property
def total_size_h264Files(self):
return self._total_size_h264Files
@total_size_h264Files.setter
def total_size_h264Files(self, new_value):
self._total_size_h264Files = new_value
Then there is 0 reason to do that instead of simply
def __init__(self, ...):
self.total_size_h264Files = 0
noted!
makes sense
thank you for helping me
why do i get this
self.total_size_h264 += file_size
when i use this inside a method
@old forge any idea?
Did you save?
yes
You didn't define it in your init so it doesn't exist yet when you did this statement
๐ฎ oh its a global var
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.