Skip to content

Commit 55b771c

Browse files
committed
Merge remote-tracking branch 'origin/master'
# Conflicts: # string_py/string_py.py
2 parents 764b6b1 + 0d492fa commit 55b771c

File tree

8 files changed

+207
-20
lines changed

8 files changed

+207
-20
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ print(Str("Hello World!").first(5))
3737
```
3838
Hello
3939
```
40+
## Information Example
41+
```python
42+
from string_py import Str
43+
44+
print(Str("Hello World!").get_upper(index=True))
45+
```
46+
```
47+
{0: 'H', 6: 'W'}
48+
```
4049
## Printer Example
4150
```python
4251
from string_py import Printer
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from string_py import Str, Color
2+
import string
3+
4+
5+
class PasswordManager:
6+
def __init__(self, password: Str):
7+
self.password = password
8+
9+
@staticmethod
10+
def generate_password() -> str:
11+
return Str(string.ascii_letters + string.digits + ".,?!$#").generate(min_=20, max_=30)
12+
13+
def calc_strength(self, points: int):
14+
print(points)
15+
if points <= 15:
16+
print("You password is okay. Maybe use a new password. Example: " + self.generate_password())
17+
elif points <= 10:
18+
print("Bad! You should use a new password. Example: " + Color.basic,
19+
self.generate_password())
20+
if points < 5:
21+
print("Very Bad! You should use a new password. Example: ", Color.basic,
22+
self.generate_password())
23+
else:
24+
print("Your password is fine!")
25+
26+
def check_password(self):
27+
points = 0
28+
if len(self.password) < 8:
29+
return self.calc_strength(points)
30+
else:
31+
points += 0.5 * len(self.password)
32+
if self.password.get_numeric(chars=False) == 0:
33+
points -= 1
34+
if self.password.get_upper(chars=False) == 0:
35+
points -= 1
36+
if self.password.get_lower(chars=False) == 0:
37+
points -= 1
38+
if self.password.get_punctuation(chars=False) == 0:
39+
points -= 1
40+
else:
41+
points += 10
42+
return self.calc_strength(round(points))
43+
44+
45+
if __name__ == "__main__":
46+
PasswordManager(Str(input("Your password: "))).check_password()

examples/string_formatting.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
print(Format.align(values={"Username:": "John", "Register Date:": "01.01.2001"}))
44
# Output:
5-
5+
#
66
# Username: John
77
# Register Date: 01.01.2001
8+
9+
print(Format.surround("Hello World", char="*"))
10+
# Output:
11+
#
12+
# ***************
13+
# * Hello World *
14+
# ***************
15+
16+
print(Format.table([["Product", "Value", "Sold"], ["Carrot", "3.99$", "34"], ["Milk", "5$", "103"]]))
17+
# Output:
18+
#
19+
# ┌────────────────────────┐
20+
# │ Product │ Value │ Sold │
21+
# ├────────────────────────┤
22+
# │ Carrot │ 3.99$ │ 34 │
23+
# │────────────────────────│
24+
# │ Milk │ 5$ │ 103 │
25+
# └────────────────────────┘

examples/string_information.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from string_py import Str
2+
3+
random_string = Str("qOUG5p5!6wq?.s,")
4+
5+
print(random_string.get_upper())
6+
# Output: ['O', 'U', 'G']
7+
8+
print(random_string.get_numeric(chars=False))
9+
# Output: 3
10+
11+
print(random_string.get_punctuation(index=True))
12+
# Output: {7: '!', 11: '?', 12: '.', 14: ','}

pyproject.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[build-system]
2+
requires = ["setuptools", "setuptools-scm"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "string_py"
7+
authors = [
8+
{name = "BabyEntchen", email = "baby_entchen@web.de"},
9+
]
10+
description = "Utils for strings in python"
11+
readme = "README.md"
12+
requires-python = ">=3.7"
13+
keywords=['python', 'string', 'strings', 'utils', 'string utils', 'random', 'random strings', 'formatting', 'formatter']
14+
license = {text = "MIT"}
15+
classifiers = [
16+
"Programming Language :: Python :: 3",
17+
]
18+
dependencies = []
19+
dynamic = ["version"]

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44
from pathlib import Path
55

66

7-
VERSION = '0.0.2'
7+
VERSION = '0.1.0'
88
DESCRIPTION = 'Utils for strings in python'
99
this_directory = Path(__file__).parent
1010
LONG_DESCRIPTION = (this_directory / "README.md").read_text()
11+
URL = "https://github.com/BabyEntchen/string_py"
1112

1213
# Setting up
1314
setup(
1415
name="string_py",
1516
version=VERSION,
1617
author="BabyEntchen",
1718
author_email="<baby_entchen@web.de>",
19+
url=URL,
1820
description=DESCRIPTION,
1921
long_description_content_type="text/markdown",
2022
long_description=LONG_DESCRIPTION,

string_py/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
__title__ = "string_py"
2-
__version__ = "0.0.2"
2+
__version__ = "0.1.0"
33
__license__ = "MIT"
44
__author__ = "BabyEntchen"
55
__copyright__ = "Copyright 2023 BabyEntchen"

string_py/string_py.py

Lines changed: 98 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from time import sleep
33
from asyncio import sleep as aio_sleep
44
from random import choices, randint
5+
import string
56

67

78
class 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

188273
class 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

Comments
 (0)