Oct-14-2022, 07:07 PM
(This post was last modified: Oct-14-2022, 07:07 PM by deanhystad.)
Two years posting to this forum disqualifies you as a python beginner. That's about how long I've been using Python.
You don't seem to fully understand classes. It looks like you think they work just like functions.
Normally you don't work directly with classes, you work with objects. To get a HangerFinder object you need to create an instance of class HangerFinder. After creating the object you use the object to call the detect_picture_nr() method. The code should look something like this:
You don't seem to fully understand classes. It looks like you think they work just like functions.
Normally you don't work directly with classes, you work with objects. To get a HangerFinder object you need to create an instance of class HangerFinder. After creating the object you use the object to call the detect_picture_nr() method. The code should look something like this:
class HangerFinder: def __init__(self, hash_list_film): """This is called when you create an instance of the class. Initialize things here.""" # Would this be a good place to make the hash list? Pass in a film list and compute the hash list? self.hash_list = hash_list_film self.start = 0 def detect_picture_nr(self, start=None, end=None): """ This is a method that returns the start and end index of a matching sequence. It uses instance variables that were created by the __init__(). """ if end is not None: end = min(end, len(self.hash_list)) else: end = len(self.hash_list) if start is not None: self.start = max(0, min(end, start)) start = self.start if start >= end: return None for i in range(start+1, end): if self.hash_list[i] != self.hash_list[i-1]: self.start = i return start, i-1 self.start = end return start, end-1 # Create an instance finder = HangerFinder(["12", "11", "11", "11", "17", "22", "22", "22", "22", "23"]) # Use the instance while (range_ := finder.detect_picture_nr()): print(range_)
Output:(0, 0)
(1, 3)
(4, 4)
(5, 8)
(9, 9)
But you really don't need a class for something like this. I would write it as a generator.def hangers(frame_list, hash_func): """Return list of sequential frames that have the same hash value""" if frame_list: start = 0 prev = hash_func(frame_list[start]) for index, film in enumerate(frame_list[1:], start=1): next = hash_func(film) if next != prev: yield frame_list[start:index] start = index prev = next yield frame_list[start:] # Using your number list to represent a frame list. Using hash() in place of your hash function for hanger in hangers(["12", "11", "11", "11", "17", "22", "22", "22", "22", "23"], hash): print(hanger)
Output:['12']
['11', '11', '11']
['17']
['22', '22', '22', '22']
['23']
The same function without requiring frames to be indexable. This lets you use a lazy iterator instead of having to make a big list containing all the frames.def hangers(frames, hash_func): """Return lists of sequential frames with the same hash value frames: An iterable of things to hash. hash_func: Function that returns a hash value. """ frame_iter = iter(frames) seq = [next(frame_iter)] prev_hash = hash_func(seq[0]) for frame in frame_iter: next_hash = hash_func(frame) if prev_hash != next_hash: yield(seq) seq = [] prev_hash = next_hash seq.append(frame) yield seq # Using your number list to represent a frame list. Using hash() in place of your hash function for hanger in hangers(iter(["12", "11", "11", "11", "17", "22", "22", "22", "22", "23"]), hash): print(hanger)