Skip to content

Commit

Permalink
Added new features (permutations, to_literal); replaced list, dict, i…
Browse files Browse the repository at this point in the history
…nt with to_literal; removed unnecessary file (BetterStringOnefile)
  • Loading branch information
DerSchinken committed May 24, 2021
1 parent 381185f commit b362cfc
Show file tree
Hide file tree
Showing 17 changed files with 92 additions and 988 deletions.
48 changes: 15 additions & 33 deletions BetterString/BetterString.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import re
import random
import hashlib
import itertools

# Important: Always put out new version on PyPI before pushing to github

Expand Down Expand Up @@ -142,43 +143,15 @@ def upper(self, end=FULL_SIZE, start=START) -> BetterString:

return BetterString(upper_string)

def list(self) -> list:
def to_literal(self):
"""
Converts your string into a list or a tuple
If the string is representing a list it will
convert it to the represented list
if not it will return every character in a list
Returns the string converted to its literal
"""
try:
list_ = eval(self.string)
converted = eval(self.string)
return converted
except SyntaxError:
list_ = list(self.string)
except NameError:
list_ = list(self.string)

return list_

def int(self) -> int:
"""
Converts your string into an integer
"""
try:
return int(self.string)
except ValueError:
raise CannotConvertToError("int")

def dict(self) -> dict:
"""
Converts your string into an dictionary
"""
try:
return eval(self.string)
except SyntaxError:
raise CannotConvertToError("dict") from None
except NameError:
raise CannotConvertToError("dict") from None
except TypeError:
raise CannotConvertToError("dict") from None
raise CannotConvertToError

def str(self) -> str:
"""
Expand Down Expand Up @@ -245,6 +218,15 @@ def bomb(self, ret) -> BetterString:
"""
return BetterString(ret[:random.randint(0, len(self.string) - 1)])

def permutations(self) -> itertools.permutations:
"""
returns all permutations of the string
"""
# We are returning the itertools.permutations object
# because if we convert it to a lst this would
# take an eternity
return itertools.permutations(self.string)

def rainbow(self) -> BetterString:
"""
Makes the string rainbow colored
Expand Down
Loading

0 comments on commit b362cfc

Please sign in to comment.