#๐Ÿ”’ help with getters and setters

55 messages ยท Page 1 of 1 (latest)

pale kite
#

how do i use getters and setter?
do i even need those?

split meteorBOT
#

@pale kite

Python help channel opened

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.

pale kite
#
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)

old forge
pale kite
#

pithink i get error with my get_total_size_h264Files()

old forge
#

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
pale kite
old forge
#

Those variables you set in __init__ won't be in self.(...)

pale kite
#

how come?'

#

ah!!

#

i see what u mean

#

makes sense , thanks!

old forge
pale kite
#
    def __init__(self):
        total_size_h264Files = 0
        total_size_hevcFiles = 0
        total_size_otherFiles = 0
        error_files = []```
like this right?
old forge
#

no

pale kite
#

๐Ÿค”

#

ah right , sorry

#

thats why my IDE said that

pale kite
old forge
pale kite
#

done

old forge
#

And also, where would you want to use setters/getters?

pale kite
#

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

old forge
#

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
pale kite
#

because we fixed self in init() ?

#

yeah earlier it was not working

#

i think because of the init func being incorrectly done

old forge
#

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
pale kite
#

makes sense

#

thank you for helping me

pale kite
#

why do i get this

#

self.total_size_h264 += file_size

#

when i use this inside a method

#

@old forge any idea?

old forge
pale kite
#

yes

old forge
pale kite
#

here is the entire code btw

pale kite
split meteorBOT
#
Python help channel closed

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.