[go: up one dir, main page]

Menu

[dfe9bc]: / globals / strings.py  Maximize  Restore  History

Download this file

183 lines (135 with data), 4.1 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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# ******************************************************
# * Copyright © 2017-2023 - Jordan Irwin (AntumDeluge) *
# ******************************************************
# * This software is licensed under the MIT license. *
# * See: LICENSE.txt for details. *
# ******************************************************
## @module globals.strings
#
# Module for manipulating strings & string lists
import sys
import libdbr.strings
from libdbr.logger import Logger
__logger = Logger(__name__)
## Checks if a text string is empty.
#
# @param text
# The string to be checked.
# @deprecated
# Use `libdbr.strings.isEmpty`.
def TextIsEmpty(text):
__logger.deprecated(TextIsEmpty, alt=libdbr.strings.isEmpty)
return not text.strip(" \t\n\r")
## Removes empty lines from a string or string list.
#
# @param text
# String or \b \e list to be checked.
# @return
# String or \b \e tuple with empty lines removed.
def RemoveEmptyLines(text):
fmt_string = False
if IsString(text):
fmt_string = True
text = text.split("\n")
elif isinstance(text, tuple):
text = list(text)
# Iterate in reverse to avoid skipping indexes
for INDEX in reversed(range(len(text))):
if libdbr.strings.isEmpty(text[INDEX]):
text.pop(INDEX)
if fmt_string:
return "\n".join(text)
return tuple(text)
## Checks if object is a string instance.
#
# Compatibility function for legacy Python versions.
#
# @param text
# String to check.
def IsString(text):
return isinstance(text, str)
## Converts an object to a string instance.
#
# Compatibility function for legacy Python versions.
#
# @param item
# Instance to be converted to string.
# @return
# Compatible string.
# @deprecated
# Use `libdbr.strings.toString`.
def ToString(item):
__logger.deprecated(ToString, alt=libdbr.strings.toString)
return libdbr.strings.toString(item)
## @deprecated
# Use `libdbr.strings.toString`.
def GS(st):
__logger.deprecated(GS, alt=libdbr.strings.toString)
return str(st)
## Tests if a string can be converted to int or float.
#
# @param string
# String to be tested.
# @deprecated
# Use `libdbr.strings.isNumeric`.
def StringIsNumeric(string):
__logger.deprecated(StringIsNumeric, alt=libdbr.strings.isNumeric)
try:
float(string)
return True
except ValueError:
return False
## Tests if a string is formatted for versioning.
#
# @param string
# String to be checked.
# @return
# `True` if string is formatted to represent versioning information.
def StringIsVersioned(string):
for P in string.split("."):
if not P.isnumeric():
return False
return True
## Retrieves a class instance's module name string.
#
# @param item
# Object instance.
# @param className
# If `True`, returns class object's name instead of module name.
# @param full
# If `True`, return entire module/class string without parsing.
# @return
# String representation.
def GetModuleString(item, className=False, full=False):
module = ToString(item.__class__)
# Strip extra characters
if "'" in module:
module = module[module.index("'")+1:].split("'")[0]
if full:
return module
module = module.split(".")
if className:
return module[-1]
return ".".join(module[:-1])
## Retrieves an instance's method name in the format of "Class.Method".
#
# @param function
# Function object.
# @param includeModule
# Prepend module name to string for class methods.
# @return
# String representation.
def GetFunctionString(function, includeModule=False):
function = ToString(function).strip("<>")
if function.startswith("bound method "):
function = function.split("<")
module = function[1].split(";")[0]
function = function[0].lstrip("bound method ").split(" ")[0]
if includeModule:
class_name = function.split(".")[0]
if ".{}".format(class_name) in module:
module = module.replace(".{}".format(class_name), "")
function = "{}.{}".format(module, function)
else:
function = function.split(" ")[1]
return function