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

@@ -26,7 +26,6 @@ class Bot_info(commands.Cog, name='Bot Info'):
title=f"General information", title=f"General information",
description=f"General information on about bot", description=f"General information on about bot",
) )
# emb.set_thumbnail(self.bot.user.avarat_url)
emb.add_field(name="System info:", value=f"Memory Usage: {round(_process.memory_info().rss / 2 ** 20, 2)} Mb\n" emb.add_field(name="System info:", value=f"Memory Usage: {round(_process.memory_info().rss / 2 ** 20, 2)} Mb\n"
f"CPU Usage: {_process.cpu_percent()}%\n" f"CPU Usage: {_process.cpu_percent()}%\n"
f'Bot ping: {round(self.bot.latency * 1000)}\n' f'Bot ping: {round(self.bot.latency * 1000)}\n'

View File

@@ -1,74 +1,84 @@
import sqlite3 import sqlite3
import disnake
class User: from lib import logger
def __init__(self, userid: str = None):
self.userid = userid
self.list: list = self._lister(self.userid) class DB_Reader:
def __init__(self, guildid: int = None):
self.guildid = guildid
self.list = self._read_db(self.guildid)
self._current_index = 0 self._current_index = 0
def __str__(self) -> str: def __str__(self) -> str:
return self.name return str(self.guildid)
def __repr__(self) -> str:
return (
f'<Audio file name={self.name} path={self.userid} type={self.type}>'
)
@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 @classmethod
def _lister(cls, path) -> list: def _read_db(cls, guildid: int) -> list:
_dict: list = [] try:
for _files in os.walk(path): sql_con = sqlite3.connect("user.db")
_dict.extend(_files) cursor = sql_con.cursor()
break cursor.execute(f"""SELECT * FROM "{guildid}" """)
return _dict[2] record = cursor.fetchall()
return record
except sqlite3.Error as _e:
logger.info(f'Error reading DB\n{_e}')
def __iter__(self): def __iter__(self):
return _ListGenerationIter(self) return _ListGenerationIter(self)
class _FileAttrs: class _DBAttrs:
def __init__(self, name, path, type): def __init__(self,
self.name = name userid,
self.path = path username,
self.type = type nick,
isbot,
defaulttracks,
usertracks):
self.userid = userid
self.username = username
self.nick = nick
self.isbot = isbot
self.defaulttracks = defaulttracks
self.usertracks = usertracks
def __str__(self): def __str__(self):
return 'Audio Dict Generator' return self.username
def __repr__(self): def __repr__(self):
return f'<File attrs name={self.name} path={self.path} type={self.path}>' return f'<File attrs userid={self.userid} username={self.username} nick={self.nick} ' \
f'isbot={self.isbot} defaulttracks={self.defaulttracks} usertracks={self.usertracks}>'
class _ListGenerationIter: class _ListGenerationIter:
def __init__(self, list_class): def __init__(self, user_class):
self._list = list_class.list self._current_index = 0
self._name = list_class.name self._list = user_class.list
self._type = list_class.type
self._path = list_class.path
self._size = len(self._list) self._size = len(self._list)
self._current_index = 0
def __iter__(self): def __iter__(self):
return self return self
def __next__(self): def __next__(self):
if self._current_index < self._size: if self._current_index < self._size:
_name = self._list[self._current_index] _userid = self._list[self._current_index][0]
_path = self._path _username = self._list[self._current_index][1]
_type = self._type _nick = self._list[self._current_index][2]
_isbot = self._list[self._current_index][3]
_defaulttracks = self._list[self._current_index][4]
_usertracks = self._list[self._current_index][5]
self._current_index += 1 self._current_index += 1
memb = _FileAttrs(_name, _path, _type) memb = _DBAttrs(_userid,
_username,
_nick,
_isbot,
_defaulttracks,
_usertracks)
return memb return memb
raise StopIteration raise StopIteration

View File

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