fixed some issues with iter

This commit is contained in:
2022-09-01 19:43:59 +03:00
parent 3c0851102d
commit 646008e4d8
3 changed files with 68 additions and 63 deletions

View File

@@ -1,11 +1,13 @@
import mimetypes
import os
from typing import Tuple, Optional
class ListGenerator:
def __init__(self, path: str = None):
self.path = path
self.list: list = self._lister(self.path)
self._size = len(self.list)
self._current_index = 0
def __str__(self) -> str:
@@ -14,25 +16,10 @@ class ListGenerator:
else:
return 'Audio iter generator'
def __repr__(self) -> str:
if self.path:
return (
f'<Audio file name={self.name} path={self.path} type={self.type}>'
)
else:
return (
f'<Audio file name={None} path={None} type={None}>'
)
@property
def name(self) -> str:
return self.list[self._current_index]
@property
def type(self) -> str:
guess = mimetypes.guess_type(f'{self.path}/{self.name}')[0].split('/')[0]
return guess
@classmethod
def _lister(cls, path) -> list:
_dict: list = []
@@ -47,6 +34,10 @@ class ListGenerator:
def __iter__(self):
return _ListGenerationIter(self)
def __next__(self):
if self._current_index < self._size:
self._current_index += 1
class _FileAttrs:
def __init__(self, name, path, type):
@@ -58,27 +49,32 @@ class _FileAttrs:
return self.name
def __repr__(self):
return f'<File attrs name={self.name} path={self.path} type={self.path}>'
return f'<File attrs name={self.name} path={self.path} type=<{self.type}> >'
class _ListGenerationIter:
def __init__(self, list_class):
self._current_index = 0
self._list = list_class.list
self._name = list_class.name
self._type = list_class.type
self._name = self._list[self._current_index]
self._path = list_class.path
self._size = len(self._list)
self._current_index = 0
def __iter__(self):
return self
@property
def type(self) -> tuple[Optional[str], Optional[str]]:
guess = mimetypes.guess_type(f'{self._path}/{self._list[self._current_index]}')[0]
return guess
def __next__(self):
if self._current_index < self._size:
_name = self._list[self._current_index]
_path = self._path
_type = self._type
_type = self.type
self._current_index += 1
memb = _FileAttrs(_name, _path, _type)
return memb