22from time import sleep
33from asyncio import sleep as aio_sleep
44from random import choices , randint
5+ import string
56
67
78class Color :
@@ -22,6 +23,7 @@ class Printer:
2223 :param active:`True`
2324 If set to False, no more prints are executed. Exception: force Parameter is set to True.
2425 """
26+
2527 def __init__ (self , active : bool = True ):
2628 self .active = active
2729
@@ -87,17 +89,24 @@ async def aio_slow(self, args: str, speed: float = 0.1, force: bool = False, err
8789 await aio_sleep (speed )
8890
8991
90- class Str :
92+ class Str ( str ) :
9193 """Functions to work with strings
9294
9395 Parameters
9496 ----------
9597 :param values: `str`
9698 The value you want to work with
9799 """
100+
98101 def __init__ (self , values : str ):
99102 self .values = values
100103 self .chars = [* values ]
104+ self .ascii = {
105+ "ascii_lowercase" : string .ascii_lowercase ,
106+ "ascii_uppercase" : string .ascii_uppercase ,
107+ "digits" : string .digits ,
108+ "punctuation" : string .punctuation
109+ }
101110
102111 def generate (self , length : int = None , min_ : int = None , max_ : int = None ) -> str :
103112 """Generate a random string out of :class:`values`
@@ -121,6 +130,16 @@ def generate(self, length: int = None, min_: int = None, max_: int = None) -> st
121130 else :
122131 raise AttributeError ("min_ and max_ or length must have a value" )
123132
133+ def remove (self , chars : str ) -> str :
134+ """Remove chars from string
135+
136+ Parameters:
137+ -----------
138+ :param chars:
139+ The chars you want to remove
140+ """
141+ return self .values .replace (chars , "" )
142+
124143 def split (self , each : int = None , chars : str = None ) -> list [str ]:
125144 """An extension of the built-in .split method. Also split on certain index
126145
@@ -136,7 +155,7 @@ def split(self, each: int = None, chars: str = None) -> list[str]:
136155 if not each and not chars :
137156 raise AttributeError ("each or chars must have a value" )
138157 if each :
139- return [self .values [i :i + each ] for i in range (0 , len (self .values ), each )]
158+ return [self .values [i :i + each ] for i in range (0 , len (self .values ), each )]
140159 else :
141160 return self .values .split (chars )
142161
@@ -174,15 +193,81 @@ def last(self, length: int = 1, remove=False) -> str:
174193 else :
175194 return self .values [- length :]
176195
177- def remove (self , chars : str ) -> str :
178- """Remove chars from string
196+ def __get (self , type_ : str , index : bool ) -> list [str ] | dict [int , str ]:
179197
180- Parameters:
181- -----------
182- :param chars:
183- The chars you want to remove
198+ gets = [] if index is False else {}
199+ for num , char in enumerate (self .values ):
200+ if char in self .ascii [type_ ]:
201+ if index is False :
202+ gets .append (char )
203+ else :
204+ gets [num ] = char
205+ return gets
206+
207+ def get_upper (self , chars : bool = True , index : bool = False ) -> int | list [str ] | dict [int , str ]:
208+ """Get how many upper chars are in :class:`values`
209+
210+ Parameters
211+ ----------
212+ :param chars:`True`
213+ If set to True, returns a list of all uppercase chars
214+ If set to False, returns the number of uppercase chars
215+ :param index:`False`
216+ If set to True, also returns the Indexes of lower chars (`chars` MUST be `True`)
184217 """
185- return self .values .replace (chars , "" )
218+ upper = self .__get ("ascii_uppercase" , index )
219+ return upper if chars else len (upper )
220+
221+ def get_lower (self , chars : bool = True , index : bool = False ) -> int | list [str ] | dict [int , str ]:
222+ """Get how many lower chars are in :class:`values`
223+
224+ Parameters
225+ ----------
226+ :param chars:`True`
227+ If set to True, returns a list of all lower chars
228+ If set to False, returns the number of lower chars
229+ :param index:`False`
230+ If set to True, also returns the Indexes of lower chars (`chars` MUST be `True`)
231+ """
232+ if not chars and index :
233+ raise AttributeError ("If index is set to True, chars can't be False" )
234+
235+ lower = self .__get ("ascii_lowercase" , index )
236+ return lower if chars else len (lower )
237+
238+ def get_numeric (self , chars : bool = True , index : bool = False ) -> int | list [str ] | dict [int , str ]:
239+ """Get how many numeric chars are in :class:`values`
240+
241+ Parameters
242+ ----------
243+ :param chars:`True`
244+ If set to True, returns a list of all numeric chars
245+ If set to False, returns the number of numeric chars
246+ :param index:`False`
247+ If set to True, also returns the Indexes of numeric chars (`chars` MUST be `True`)
248+ """
249+ if not chars and index :
250+ raise AttributeError ("If index is set to True, chars can't be False" )
251+
252+ numeric = self .__get ("digits" , index )
253+ return numeric if chars else len (numeric )
254+
255+ def get_punctuation (self , chars : bool = True , index : bool = False ) -> int | list [str ] | dict [int , str ]:
256+ """Get how many punctuation chars are in :class:`values`
257+
258+ Parameters
259+ ----------
260+ :param chars:`True`
261+ If set to True, returns a list of all punctuation chars
262+ If set to False, returns the number of punctuation chars
263+ :param index:`False`
264+ If set to True, also returns the Indexes of punctuation chars (`chars` MUST be `True`)
265+ """
266+ if not chars and index :
267+ raise AttributeError ("If index is set to True, chars can't be False" )
268+
269+ punctuation = self .__get ("punctuation" , index )
270+ return punctuation if chars else len (punctuation )
186271
187272
188273class Format :
@@ -199,18 +284,18 @@ def surround(values: str, char: str = "*") -> str:
199284 :param char:`*`
200285 Char to surround with
201286 :return:
202- Returns a string with the text surrounded with chars
287+ Returns a string with the text surrounded with certain chars
203288 """
204289 return f"{ char * (len (values ) + 4 )} \n { char } { values } { char } \n { char * (len (values ) + 4 )} "
205290
206291 @staticmethod
207292 def align (values : dict [str , str ]):
208- """Algin a text
293+ """Align a text
209294
210295 Parameters
211296 ----------
212297 :param values:`dict[str, str]`
213- Texts to algin {"Left side": "Right side"}
298+ Texts to align {"Left side": "Right side"}
214299 :return:
215300 Returns a string with the key aligned left and the value right dependent from the keys
216301
@@ -226,16 +311,14 @@ def align(values: dict[str, str]):
226311 aligned_text += key + " " * ((length + 3 ) - len (key )) + values [key ] + "\n "
227312 return aligned_text
228313
229-
230-
231314 @staticmethod
232315 def table (values : list [list [str ]], border : bool = True ) -> str :
233316 """Create a table
234317
235318 Parameters
236319 ----------
237320 :param values:`list[list[str]]`
238- The values you want to print
321+ The values to create the table with
239322 :param border:`True`
240323 Set to `False` to remove the border
241324 :return:
@@ -264,5 +347,3 @@ def table(values: list[list[str]], border: bool = True) -> str:
264347 if index != len (values ) - 1 :
265348 table += "\n "
266349 return table
267-
268-
0 commit comments