Skip to content

Commit ae15bee

Browse files
author
saner99
committed
v0.1.7, release, stable
1 parent 2579d75 commit ae15bee

File tree

13 files changed

+601
-140
lines changed

13 files changed

+601
-140
lines changed

ctprint/ctprint/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from ctprint.ctprint import ctprint, ctp, ctdecode, cterr, ctlog
Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import sys
2-
import traceback # for tcprint.err()
3-
# colorama определяет ansi-коды, изменяет output, добавляет синтаксис. кроссплатформенная
1+
import sys # for ctprint.err()
2+
import traceback # for ctprint.err()
3+
# colorama - makes ANSI escape character sequences (for producing colored terminal text and cursor positioning) work under MS Windows.
44
from colorama import init, Fore, Back
5-
from help_message import help_message as h_m # класс со стрингами
6-
# colorama autoreset автоматически возвращает к дефолтным системным параметрам
5+
from .help_message import h_m # экземпляр класса со стрингами
6+
# colorama autoreset=True - automatically back to default color
77
init(autoreset=True)
88

9-
# from tcprint import tcp, tdecode, terr, tlog # tcprint imports
9+
# from ctprint import ctp, ctdecode, cterr, ctlog # ctprint imports
1010

1111

1212
tags: dict[str, dict[str, str]] = {
@@ -29,15 +29,15 @@
2929
}
3030

3131

32-
class tcprint():
32+
class ctprint():
3333

34-
def __init__(self, string: str = None) -> None:
34+
def __init__(self, *strings: str) -> None:
3535
self.tags = tags
3636

37-
if string:
38-
print(self.decode(string))
37+
if strings:
38+
print(self.decode(*strings))
3939

40-
def decode(self, *strings: str) -> str: # *args for tcprint.decode(string1, string2, string3)
40+
def decode(self, *strings: str) -> str: # *args for ctprint.decode(string1, string2, string3)
4141

4242
result_string = ''
4343
for string in strings:
@@ -49,14 +49,14 @@ def decode(self, *strings: str) -> str: # *args for tcprint.decode(string1, str
4949
result_string += string # натягивает стринги друг на друга
5050
return result_string
5151

52-
def err(self, exception=None, comment:any='') -> None:
52+
def err(self, exception=None, comment: any = '') -> None:
5353
if comment:
5454
comment = f" comment: {str(comment)} "
5555
exc_type, exc_value, exc_traceback = sys.exc_info()
5656
# берёт тб последней ошибки. хорошо бы добавить проверку по exception
5757
ex_tb = traceback.extract_tb(exc_traceback, limit=1)[0]
5858

59-
print(self.decode( # exception не проходит через decode(), чтобы в exception были видны теги
59+
print(self.decode( # the exception is not decoded so that the tags are visible in it
6060
f"\n<bw>>>>>>>[Error]>>>> in: {ex_tb.name} line: {ex_tb.lineno} -> /> <red>{ex_tb.line} <bw>>{comment}<plain><white>"), str(exception), self.decode(f"\n<bg_white><black>[file]>> <plain> {ex_tb.filename}"))
6161

6262
def log(self, **vars) -> None:
@@ -66,44 +66,40 @@ def log(self, **vars) -> None:
6666

6767
def help() -> None:
6868
help_msg = h_m.help_info + h_m.description + h_m.tags + h_m.methods
69-
print(tcprint().decode(help_msg).replace(
70-
'PRE', '')) # PRE - разделяет теги
69+
print(ctprint().decode(help_msg).replace(
70+
'PRE', '')) # PRE - separates the tags
7171

72-
if input(tdecode("""
72+
if input(ctdecode("""
7373
<magenta>Press <bw> Y /><magenta> if u wanna see short live example: />""")).lower() == 'y':
7474

75-
print(tcprint().decode(h_m.test_example).replace('PRE', ''))
76-
tcprint("<info> def test() output:/>\n")
75+
print(ctprint().decode(h_m.test_example).replace('PRE', ''))
76+
ctprint("<info> def test() output:/>\n")
7777
test()
7878

79-
tcprint(h_m.ending)
79+
ctprint(h_m.ending)
8080

8181

8282
# for imports
83-
tcp = tcprint
84-
terr = tcp().err
85-
tdecode = tcp().decode
86-
tlog = tcp().log
83+
ctp = ctprint
84+
cterr = ctp().err
85+
ctdecode = ctp().decode
86+
ctlog = ctp().log
8787

8888
# example:
8989
# ./test.py
90-
# from tcprint import tcp, terr, tdecode
90+
# from ctprint import ctp, cterr, ctdecode
9191

92-
# tcp.help() # print help message
92+
# ctp.help() # print help message
9393

9494

9595
def test(): # usage example
9696

9797
try:
9898

99-
tcp('<bw> b&w !!! /><magenta> magenta <plain> plain <green>green />etc')
99+
ctp('<bw> b&w !!! /><magenta> magenta <plain> plain <green>green />etc')
100100
1/0 # for error's view example
101101

102102
except Exception as _ex:
103103

104-
# terr(Exception, your_comment)
105-
terr(_ex, [{'excepted': 3.1415926535}])
106-
107-
108-
if __name__ == '__main__':
109-
tcp.help()
104+
# cterr(Exception, your_comment)
105+
cterr(_ex, [{'excepted': 3.1415926535}])
Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
# from tcprint import tcp, tdecode, terr, tlog
1+
# from ctprint import ctp, ctdecode, cterr, ctlog
22

33

44
class help_message():
55

66
help_info: str = f"""
7-
<bw>>>>>>>[ tcprint.help() ]>>>> <plain><magenta> Help info: <plain>
7+
<bw>>>>>>>[ ctprint.help() ]>>>> <plain><magenta> Help info: <plain>
88
<magenta>{'-'*40}/>"""
99

1010
description = f"""
11-
- All tcprint opening tags are always enclosed in angle brackets: '<magenta>< ><plain>'
12-
- tcprint have ONLY ONE CLOSING TAG - <magenta>/PRE>/> (reset all formating to sys default)
11+
- All ctprint opening tags are always enclosed in angle brackets: '<magenta>< ><plain>'
12+
- ctprint have ONLY ONE CLOSING TAG - <magenta>/PRE>/> (reset all formating to sys default)
1313
- for RESET FORMATING to default u can use tags <magenta><PREreset><plain> and <magenta><PREbg_reset><plain>.
1414
Also u can format text to white and background to black using <magenta><PREplain><plain>
1515
"""
@@ -68,23 +68,23 @@ class help_message():
6868
"""
6969
methods: str = f"""
7070
<magenta>{'-'*24}
71-
Logging tcprint methods:
71+
Logging ctprint methods:
7272
{'-'*24}/>
73-
<magenta>from<red> tcprint <magenta>import<red> tcp/>, <red>tdecode/>, <red>terr/>, <red>tlog/> # for example
73+
<magenta>from<red> ctprint <magenta>import<red> ctp/>, <red>ctdecode/>, <red>cterr/>, <red>ctlog/> # for example
7474
75-
<blue>tcp/>(<green>'<PREbw>text\t\PRE>'/>)/> output:
75+
<blue>ctp/>(<green>'<PREbw>text\t\PRE>'/>)/> output:
7676
<bw>text\t/>
7777
78-
<blue>tdecode/>(<green>'<PREbw>text\t\PRE>'/>)/> return-> <bw>text\t/>
78+
<blue>ctdecode/>(<green>'<PREbw>text\t\PRE>'/>)/> return-> <bw>text\t/>
7979
80-
<blue>terr/>(<yellow>Exception/>, <green>'your comment'/>)/> error message output:
80+
<blue>cterr/>(<yellow>Exception/>, <green>'your comment'/>)/> error message output:
8181
<bw>>>>>>>[Error]>>>> in: <name> line: <line index> ->/><red> <the line's code> <bw>> comment: your comment /> <excaption>
8282
<bw>[file]>> /> <path to file>
8383
8484
<red>var1/>=<yellow>0
8585
<red>var2/>=<green>'text'
8686
<red>var3/>="""+"""{<green>'dict_key','<PREbw>text/PRE>'/>}"""+"""
87-
<blue>tlog/>(<red>var1 = <red>var1/>, <red>var2/> = <red>var2/> , <red>var3/> = <red>var3/>)/> log message output:
87+
<blue>ctlog/>(<red>var1 = <red>var1/>, <red>var2/> = <red>var2/> , <red>var3/> = <red>var3/>)/> log message output:
8888
<bw>var1 :/> 0
8989
<bw>var2 :/> text
9090
<bw>var3_dict_key :/> {'dict_key','<PREbw>text/PRE>'}
@@ -94,21 +94,23 @@ class help_message():
9494

9595
test_example: str = """
9696
/>#./test.py
97-
<magenta>from<red> tcprint <magenta>import<red> tcp/>, <red>tdecode/>, <red>terr/>, <red>tlog/>
97+
<magenta>from<red> ctprint <magenta>import<red> ctp/>, <red>ctdecode/>, <red>cterr/>, <red>ctlog/>
9898
99-
# tcp.help() # print help message
99+
# ctp.help() # print help message
100100
101101
<magenta>def/> <blue>test/>():
102102
103103
<magenta>try/>:
104104
105-
<blue>tcp/>(<green>'<PREbw> b&w !!! /PRE><PREmagenta> magenta <PREplain> plain <PREgreen>green /PRE>etc'/>)
105+
<blue>ctp/>(<green>'<PREbw> b&w !!! /PRE><PREmagenta> magenta <PREplain> plain <PREgreen>green /PRE>etc'/>)
106106
<yellow>1<blue>/<yellow>0/> # for error's view example
107107
108108
<magenta>except<yellow> Exception<magenta> as <red>_ex/>:
109109
110-
<blue>terr/>(<red>_ex/>, [{<green>'excepted'/>: <yellow>3.1415926535/>}]) # terr(Exception, your_comment)
110+
<blue>cterr/>(<red>_ex/>, [{<green>'excepted'/>: <yellow>3.1415926535/>}]) # cterr(Exception, your_comment)
111111
112112
113113
<magenta>if <red>__name__ <blue>==<green> '__main__'/>:
114114
<blue>test/>()"""
115+
116+
h_m = help_message()

ctprint/tests/__init__.py

Whitespace-only changes.

ctprint/tests/test_ctprint.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import sys # for ctprint.err()
2+
import traceback # for ctprint.err()
3+
# colorama - makes ANSI escape character sequences (for producing colored terminal text and cursor positioning) work under MS Windows.
4+
from colorama import init, Fore, Back
5+
from . import test_help_message as h_m # класс со стрингами
6+
# colorama autoreset=True - automatically back to default color
7+
init(autoreset=True)
8+
9+
# from ctprint import ctp, ctdecode, cterr, ctlog # ctprint imports
10+
11+
12+
tags: dict[str, dict[str, str]] = {
13+
14+
'simple_styles': {'<plain>': Fore.WHITE + Back.BLACK, # white text on black background
15+
'<bw>': Fore.BLACK + Back.WHITE, # black text on white background
16+
'/>': Fore.RESET + Back.RESET,
17+
'<info>': f'\n{Fore.YELLOW}{Back.BLACK}>>>>>>[Info]>>>> ',
18+
'<err>': f'\n{Fore.RED}{Back.BLACK}>>>>>>[Error]>>>> ',
19+
'<bw_info>': f'\n{Fore.BLACK}{Back.WHITE}>>>>>>[Info]>>>> ',
20+
'<bw_err>': f'\n{Fore.BLACK}{Back.WHITE}>>>>>>[Error]>>>> '},
21+
22+
'fore': {'<magenta>': Fore.MAGENTA, '<red>': Fore.RED, '<green>': Fore.GREEN,
23+
'<yellow>': Fore.YELLOW, '<blue>': Fore.BLUE, '<cyan>': Fore.CYAN,
24+
'<white>': Fore.WHITE, '<black>': Fore.BLACK, '<reset>': Fore.RESET},
25+
26+
'back': {'<bg_magenta>': Back.MAGENTA, '<bg_red>': Back.RED, '<bg_green>': Back.GREEN,
27+
'<bg_yellow>': Back.YELLOW, '<bg_blue>': Back.BLUE, '<bg_cyan>': Back.CYAN,
28+
'<bg_white>': Back.WHITE, '<bg_black>': Back.BLACK, '<bg_reset>': Back.RESET}
29+
}
30+
31+
32+
class ctprint():
33+
34+
def __init__(self, *strings: str) -> None:
35+
self.tags = tags
36+
37+
if strings:
38+
print(self.decode(*strings))
39+
40+
def decode(self, *strings: str) -> str: # *args for ctprint.decode(string1, string2, string3)
41+
42+
result_string = ''
43+
for string in strings:
44+
for tags_dict in self.tags:
45+
for tag in self.tags[tags_dict]:
46+
string = string.replace(
47+
tag, self.tags[tags_dict][tag])
48+
49+
result_string += string # натягивает стринги друг на друга
50+
return result_string
51+
52+
def err(self, exception=None, comment: any = '') -> None:
53+
if comment:
54+
comment = f" comment: {str(comment)} "
55+
exc_type, exc_value, exc_traceback = sys.exc_info()
56+
# берёт тб последней ошибки. хорошо бы добавить проверку по exception
57+
ex_tb = traceback.extract_tb(exc_traceback, limit=1)[0]
58+
59+
print(self.decode( # the exception is not decoded so that the tags are visible in it
60+
f"\n<bw>>>>>>>[Error]>>>> in: {ex_tb.name} line: {ex_tb.lineno} -> /> <red>{ex_tb.line} <bw>>{comment}<plain><white>"), str(exception), self.decode(f"\n<bg_white><black>[file]>> <plain> {ex_tb.filename}"))
61+
62+
def log(self, **vars) -> None:
63+
64+
for varname, varvalue in vars.items():
65+
print(self.decode(f'<bw> {str(varname)} \t:'), str(varvalue))
66+
67+
def help() -> None:
68+
help_msg = h_m.help_info + h_m.description + h_m.tags + h_m.methods
69+
print(ctprint().decode(help_msg).replace(
70+
'PRE', '')) # PRE - separates the tags
71+
72+
if input(ctdecode("""
73+
<magenta>Press <bw> Y /><magenta> if u wanna see short live example: />""")).lower() == 'y':
74+
75+
print(ctprint().decode(h_m.test_example).replace('PRE', ''))
76+
ctprint("<info> def test() output:/>\n")
77+
test()
78+
79+
ctprint(h_m.ending)
80+
81+
82+
# for imports
83+
ctp = ctprint
84+
cterr = ctp().err
85+
ctdecode = ctp().decode
86+
ctlog = ctp().log
87+
88+
# example:
89+
# ./test.py
90+
# from ctprint import ctp, cterr, ctdecode
91+
92+
# ctp.help() # print help message
93+
94+
95+
def test(): # usage example
96+
97+
try:
98+
99+
ctp('<bw> b&w !!! /><magenta> magenta <plain> plain <green>green />etc')
100+
1/0 # for error's view example
101+
102+
except Exception as _ex:
103+
104+
# cterr(Exception, your_comment)
105+
cterr(_ex, [{'excepted': 3.1415926535}])

0 commit comments

Comments
 (0)