-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextEdit.py
More file actions
88 lines (78 loc) · 2.92 KB
/
TextEdit.py
File metadata and controls
88 lines (78 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from typing import List, Tuple
from random import randint
class TextEdit:
@staticmethod
def spoiler(text: str) -> str:
"""
This function puts every char into double vertical bars.
:param text: text to add vertical bars to
:return: result
"""
b = ""
for i in text:
b += f"||{i}||"
return b
@staticmethod
def emojify(text: str, char_space: int = 1, space_size: int = 3, diakritika: bool = True) -> Tuple[str, List[str]]:
"""
Converts text to emoji form, can also remove basic diacritics.
:param text: text to convert
:param char_space: space size between emoji characters
:param space_size: space size between words
:param diakritika: determines if diacritics should be removed
:return: returns tuple of converted text and characters that weren't converted
"""
text = text.lower()
if diakritika:
# this dictionary removes diacritics
preklad = str.maketrans("áéíóúýčďěňřšťžů",
"aeiouycdenrstzu")
text = text.translate(preklad)
b = ""
znaky = []
for i in text:
if i == " ": # space is replaced for better readability
b += space_size * " "
elif i.isnumeric(): # jde o číslo
b += i + chr(0x20e3) + char_space * " "
elif i.isalpha() and i.isascii(): # means alphabetic char
b += chr(ord(i) + 127365) + char_space * " " # adding 127365 outputs emoji version
else: # chars that aren't numbers nor letters are saved here
b += i
znaky.append(i)
return b, znaky
@staticmethod
def varied(text: str, min_num=2, max_num=3) -> str:
"""
Function that creates varied case for the text in random intervals. Converts text to lowercase before varying.
:param text: text to be converted
:param min_num: minimal character space between lower and upper case
:param max_num: maximum character space between lower and upper case
:return: text with varied text
:raises ValueError: if min_num is larger than max_num
"""
b = ""
rand = 0
# script cycles through text and saves char space in rand variable, resets after every uppercase letter
for i in text:
if not i.isalpha():
b += i
continue
elif rand:
b += i.lower()
else:
b += i.upper()
rand = randint(min_num, max_num)
rand -= 1
return b
@staticmethod
def reverse(text: str) -> str:
"""
Reverses char order.
:param text: input text
:return: reversed string
"""
a = ""
for i in reversed(text):
a += i
return a