Last active
December 31, 2023 14:17
-
-
Save sovietscout/ff1d2bf4a3ecb941eef26d1208fc7429 to your computer and use it in GitHub Desktop.
Revisions
-
sovietscout revised this gist
Dec 31, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -49,7 +49,7 @@ def __init__(self, text: str, case: Cases, delimiter: str = ' ', preserve_punctu super().__init__(wordList) class Convert(str): def __new__(cls, text: Union[str, List[str], FromCase], case: Cases = Cases.Camel, delimiter: Optional[str] = ' ') -> str: if isinstance(text, str): text = FromCase(text, Cases.Plain) -
sovietscout revised this gist
Apr 19, 2022 . 2 changed files with 19 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,8 +11,8 @@ class Cases(Enum): # Reference: https://stackoverflow.com/a/54330161 Macro = 5 Cobol = 6 Pascal = 7 Plain = 8 Custom = 9 # Aliases Caterpillar = 3 @@ -21,15 +21,17 @@ class Cases(Enum): # Reference: https://stackoverflow.com/a/54330161 Dash = 3 Lisp = 3 Css = 3 C = 2 Lower = 2 Upper = 5 Train = 6 CapitalCamel = 7 Sentence = 8 class FromCase(list): def __init__(self, text: str, case: Cases, delimiter: str = ' ', preserve_punctuation: bool = False) -> List[str]: removables = string.punctuation if not preserve_punctuation else '' normalizedText = text.translate(str.maketrans('', '', removables.replace(delimiter, ''))) if case in ([Cases.Camel, Cases.Pascal]): wordList = re.findall('^[a-z]+|[A-Z][^A-Z]*', normalizedText) @@ -44,13 +46,14 @@ def __init__(self, text: str, case: Cases, delimiter: str = ' ') -> List[str]: else: raise Exception("Case not supported") super().__init__(wordList) class Convert(str): def __new__(cls, text: Union[str, List[str], FromCase], case: Cases = Cases.Camel, delimiter: str = ' ') -> str: if isinstance(text, str): text = FromCase(text, Cases.Plain) # Lowercases string, replaces spaces with nothing, removes element if it is empty words = [word for word in [word.lower().replace(' ', '') for word in text] if word] if case is Cases.Camel: @@ -66,9 +69,9 @@ def __new__(cls, text: Union[str, List[str], FromCase], case: Cases = Cases.Came elif case is Cases.Cobol: convertedStr = '-'.join(words).upper() elif case is Cases.Pascal: convertedStr = ''.join(words).title() elif case is Cases.Plain: convertedStr = words[0].capitalize() + ' ' + ' '.join(words[1:]) elif case is Cases.Custom: convertedStr = delimiter.join(words) else: This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,19 @@ from CaseConverter import Convert, Cases, FromCase # Convert a sentence to snake case Convert("This is a cry for help", Cases.Snake) # this_is_a_cry_for_help # Supports other languages too! Convert("Конец близок", Cases.Kebab) # конец-близок # Convert a list of words to camel case Convert(["The", "fog", "is", "coming"], Cases.Camel) # theFogIsComing # Convert to a custom case (delimiter required) Convert("What do the numbers mean, Mason?", Cases.Custom, delimiter="|") # what|do|the|numbers|mean|mason # Convert from one case to another Convert(FromCase("ItWasAllAMistake", Cases.Pascal), Cases.Cobol) # IT-WAS-ALL-A-MISTAKE # Convert from a custom case (delimiter necessary) to another case Convert(FromCase("ratio+L+cope", Cases.Custom, '+'), Cases.Custom, '~') # ratio~l~cope -
sovietscout revised this gist
Apr 8, 2022 . 2 changed files with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -51,7 +51,7 @@ def __new__(cls, text: Union[str, List[str], FromCase], case: Cases = Cases.Came if isinstance(text, str): text = FromCase(text, Cases.Plain) words = [word for word in [word.lower().replace(' ', '') for word in text] if word] if case is Cases.Camel: convertedStr = words[0] + ''.join([word.capitalize() for word in words[1:]]) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ # Convert to snake case Convert("This is a cry for help", Cases.Snake) # this_is_a_cry_for_help # Convert a list of words to camel case Convert(["It", "was", "all", "a", "mistake"], Cases.Camel) # itWasAllAMistake # Convert to a custom case (delimiter required) -
sovietscout revised this gist
Apr 7, 2022 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -47,11 +47,11 @@ def __init__(self, text: str, case: Cases, delimiter: str = ' ') -> List[str]: return super().__init__(wordList) class Convert(str): def __new__(cls, text: Union[str, List[str], FromCase], case: Cases = Cases.Camel, delimiter: str = ' ') -> str: if isinstance(text, str): text = FromCase(text, Cases.Plain) words = [word for word in [word.replace(' ', '') for word in text] if word] if case is Cases.Camel: convertedStr = words[0] + ''.join([word.capitalize() for word in words[1:]]) -
sovietscout revised this gist
Apr 7, 2022 . 1 changed file with 11 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -28,30 +28,30 @@ class Cases(Enum): # Reference: https://stackoverflow.com/a/54330161 Train = 6 class FromCase(list): def __init__(self, text: str, case: Cases, delimiter: str = ' ') -> List[str]: normalizedText = text.translate(str.maketrans('', '', string.punctuation.replace(delimiter, ''))) if case in ([Cases.Camel, Cases.Pascal]): wordList = re.findall('^[a-z]+|[A-Z][^A-Z]*', normalizedText) elif case in ([Cases.Snake, Cases.Macro]): wordList = normalizedText.split('_') elif case in ([Cases.Kebab, Cases.Cobol]): wordList = normalizedText.split('-') elif case is Cases.Plain: wordList = normalizedText.split() elif case is Cases.Custom: wordList = normalizedText.split(delimiter) else: raise Exception("Case not supported") return super().__init__(wordList) class Convert(str): def __new__(cls, text: Union[str, Union[List[str], FromCase]], case: Cases = Cases.Camel, delimiter: str = ' ') -> str: if isinstance(text, str): text = FromCase(text, Cases.Plain) words = [word.lower().replace(' ', '') for word in text] if case is Cases.Camel: convertedStr = words[0] + ''.join([word.capitalize() for word in words[1:]]) -
sovietscout revised this gist
Apr 7, 2022 . 2 changed files with 2 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,6 @@ from typing import List, Union class Cases(Enum): # Reference: https://stackoverflow.com/a/54330161 Camel = 1 Snake = 2 Kebab = 3 @@ -13,6 +12,7 @@ class Cases(Enum): # Reference: https://stackoverflow.com/a/54330161 Cobol = 6 Pascal = 7 Custom = 8 Plain = 9 # Aliases Caterpillar = 3 @@ -31,8 +31,6 @@ class FromCase(list): def __init__(self, query: str, case: Cases = Cases.Plain, delimiter: str = ' ') -> List[str]: normalizedQuery = query.translate(str.maketrans('', '', string.punctuation.replace(delimiter, ''))) if case in ([Cases.Camel, Cases.Pascal]): wordList = re.findall('^[a-z]+|[A-Z][^A-Z]*', normalizedQuery) elif case in ([Cases.Snake, Cases.Macro]): @@ -55,8 +53,6 @@ def __new__(cls, query: Union[str, Union[List[str], FromCase]], case: Cases = Ca words = [word.lower() for word in query] if case is Cases.Camel: convertedStr = words[0] + ''.join([word.capitalize() for word in words[1:]]) elif case is Cases.Snake: This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ from CaseConverter import Convert, Cases, FromCase # Convert to snake case Convert("This is a cry for help", Cases.Snake) # this_is_a_cry_for_help -
sovietscout revised this gist
Apr 7, 2022 . 2 changed files with 6 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ import re import string from enum import Enum from typing import List, Union class Cases(Enum): # Reference: https://stackoverflow.com/a/54330161 Plain = 10 @@ -48,14 +48,14 @@ def __init__(self, query: str, case: Cases = Cases.Plain, delimiter: str = ' ') return super().__init__(wordList) class Convert(str): def __new__(cls, query: Union[str, Union[List[str], FromCase]], case: Cases = Cases.Camel, delimiter: str = ' ') -> str: if isinstance(query, str): query = FromCase(query, Cases.Plain) words = [word.lower() for word in query] convertedStr: str = '' if case is Cases.Camel: convertedStr = words[0] + ''.join([word.capitalize() for word in words[1:]]) @@ -78,4 +78,4 @@ def __new__(cls, query: Union[str, Union[List[str], FromCase]], case: Cases = Ca else: raise Exception("Case not recognised") return super().__new__(cls, convertedStr) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,4 @@ from CaseConverter import Convert, Cases, FromCase # Convert to snake case Convert("This is a cry for help", Cases.Snake) # this_is_a_cry_for_help @@ -13,4 +12,4 @@ Convert(FromCase("ThisIsDefinitelyNotFun", Cases.Pascal), Cases.Cobol) # THIS-IS-DEFINITELY-NOT-FUN # Custom cases (delimiters necessary) Convert(FromCase("the,fog,is,coming", Cases.Custom, ','), Cases.Custom, '^') # the^fog^is^coming -
sovietscout revised this gist
Apr 7, 2022 . 2 changed files with 44 additions and 41 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,10 @@ import re import string from enum import Enum from typing import List, Optional, Union class Cases(Enum): # Reference: https://stackoverflow.com/a/54330161 Plain = 10 Camel = 1 Snake = 2 Kebab = 3 @@ -26,56 +27,55 @@ class Cases(Enum): # Reference: https://stackoverflow.com/a/54330161 Upper = 5 Train = 6 class FromCase(list): def __init__(self, query: str, case: Cases = Cases.Plain, delimiter: str = ' ') -> List[str]: normalizedQuery = query.translate(str.maketrans('', '', string.punctuation.replace(delimiter, ''))) wordList = [] if case in ([Cases.Camel, Cases.Pascal]): wordList = re.findall('^[a-z]+|[A-Z][^A-Z]*', normalizedQuery) elif case in ([Cases.Snake, Cases.Macro]): wordList = normalizedQuery.split('_') elif case in ([Cases.Kebab, Cases.Cobol]): wordList = normalizedQuery.split('-') elif case is Cases.Plain: wordList = normalizedQuery.split() elif case is Cases.Custom: wordList = normalizedQuery.split(delimiter) else: raise Exception("Case not supported") return super().__init__(wordList) class Convert: def __new__(cls, query: Union[str, Union[List[str], FromCase]], case: Cases = Cases.Camel, delimiter: Optional[str] = None) -> str: if isinstance(query, str): query = FromCase(query, Cases.Plain) words = [word.lower() for word in query] convertedStr = "" if case is Cases.Camel: convertedStr = words[0] + ''.join([word.capitalize() for word in words[1:]]) elif case is Cases.Snake: convertedStr = '_'.join(words) elif case is Cases.Kebab: convertedStr = '-'.join(words) elif case is Cases.Flat: convertedStr = ''.join(words) elif case is Cases.Macro: convertedStr = '_'.join(words).upper() elif case is Cases.Cobol: convertedStr = '-'.join(words).upper() elif case is Cases.Pascal: convertedStr = ''.join([word.capitalize() for word in words]) elif case is Cases.Plain: convertedStr = ' '.join(words) elif case is Cases.Custom: convertedStr = delimiter.join(words) else: raise Exception("Case not recognised") return convertedStr This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ from CaseConverter import Convert, Cases, FromCase # Convert to snake case Convert("This is a cry for help", Cases.Snake) # this_is_a_cry_for_help @@ -9,5 +9,8 @@ # Convert to a custom case (delimiter required) Convert("What do the numbers mean, Mason?", Cases.Custom, delimiter="|") # what|do|the|numbers|mean|mason # Convert from one case to another Convert(FromCase("ThisIsDefinitelyNotFun", Cases.Pascal), Cases.Cobol) # THIS-IS-DEFINITELY-NOT-FUN # Custom cases (delimiters necessary) Convert(FromCase("the,fog,is,coming", Cases.Custom, ','), Cases.Custom, '^') # the^fog^is^coming -
sovietscout revised this gist
Apr 7, 2022 . 2 changed files with 20 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,7 @@ import re import string from enum import Enum from typing import List, Union class Cases(Enum): # Reference: https://stackoverflow.com/a/54330161 Camel = 1 @@ -26,12 +27,16 @@ class Cases(Enum): # Reference: https://stackoverflow.com/a/54330161 Train = 6 class Convert: def __new__(cls, query: Union[str, List[str]], case: Cases = Cases.Camel, delimiter: str = '') -> str: if isinstance(query, str): words = cls.__normalizeString(query).lower().split() elif isinstance(query, list): words = query convertedStr = "" if case == Cases.Camel: convertedStr = words[0].lower() + ''.join([word.capitalize() for word in words[1:]]) elif case == Cases.Snake: convertedStr = '_'.join(words) elif case == Cases.Kebab: @@ -45,15 +50,15 @@ def __new__(cls, query: str, case: Cases = Cases.Camel, delimeter: str = '') -> elif case == Cases.Pascal: convertedStr = ''.join([word.capitalize() for word in words]) elif case == Cases.Custom: convertedStr = delimiter.join(words) else: raise Exception("Case not recognised") return convertedStr @classmethod def fromCase(cls, query: str, fromCase: Cases, toCase: Cases, delimiter: str = ''): normalizedQuery = cls.__normalizeString(query) normalizedList = [] @@ -64,11 +69,13 @@ def fromCase(cls, query: str, fromCase: Cases, toCase: Cases): normalizedList = normalizedQuery.split('_') elif fromCase == Cases.Kebab or Cases.Cobol: normalizedList = normalizedQuery.split('-') elif fromCase == Cases.Custom: raise Exception("Custom case not supported") else: normalizedList = normalizedQuery.split() return cls(normalizedList, toCase, delimiter) @staticmethod def __normalizeString(query: str) -> str: return query.translate(str.maketrans('', '', string.punctuation)) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,8 +3,11 @@ # Convert to snake case Convert("This is a cry for help", Cases.Snake) # this_is_a_cry_for_help # Conve a list of words to camel case Convert(["It", "was", "all", "a", "mistake"], Cases.Camel) # itWasAllAMistake # Convert to a custom case (delimiter required) Convert("What do the numbers mean, Mason?", Cases.Custom, delimiter="|") # what|do|the|numbers|mean|mason # Convert from one case to another (from custom case type not supported) Convert.fromCase("thisIsDefinitelyNotFun", Cases.Camel, Cases.Cobol) # THIS-IS-DEFINITELY-NOT-FUN -
sovietscout revised this gist
Apr 7, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,4 +7,4 @@ Convert("What do the numbers mean, Mason?", Cases.Custom, delimeter="|") # what|do|the|numbers|mean|mason # Convert from one case to another (from custom case type not supported) Convert.fromCase("thisIsDefinitelyNotFun", Cases.Camel, Cases.Cobol) # THIS-IS-DEFINITELY-NOT-FUN -
sovietscout revised this gist
Apr 6, 2022 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,9 @@ from CaseConverter import Convert, Cases # Convert to snake case Convert("This is a cry for help", Cases.Snake) # this_is_a_cry_for_help # Convert to a custom case (delimeter required) Convert("What do the numbers mean, Mason?", Cases.Custom, delimeter="|") # what|do|the|numbers|mean|mason # Convert from one case to another (from custom case type not supported) -
sovietscout created this gist
Apr 6, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,74 @@ import re import string from enum import Enum class Cases(Enum): # Reference: https://stackoverflow.com/a/54330161 Camel = 1 Snake = 2 Kebab = 3 Flat = 4 Macro = 5 Cobol = 6 Pascal = 7 Custom = 8 # Aliases Caterpillar = 3 Hyphen = 3 Spinal = 3 Dash = 3 Lisp = 3 Css = 3 CapitalCamel = 7 C = 2 Lower = 2 Upper = 5 Train = 6 class Convert: def __new__(cls, query: str, case: Cases = Cases.Camel, delimeter: str = '') -> str: words = cls.__normalizeString(query).lower().split() convertedStr = "" if case == Cases.Camel: convertedStr = words[0] + ''.join([word.capitalize() for word in words[1:]]) elif case == Cases.Snake: convertedStr = '_'.join(words) elif case == Cases.Kebab: convertedStr = '-'.join(words) elif case == Cases.Flat: convertedStr = ''.join(words) elif case == Cases.Macro: convertedStr = '_'.join(words).upper() elif case == Cases.Cobol: convertedStr = '-'.join(words).upper() elif case == Cases.Pascal: convertedStr = ''.join([word.capitalize() for word in words]) elif case == Cases.Custom: convertedStr = delimeter.join(words) else: raise Exception("Case not recognised") return convertedStr @classmethod def fromCase(cls, query: str, fromCase: Cases, toCase: Cases): normalizedQuery = cls.__normalizeString(query) normalizedList = [] if fromCase == Cases.Camel or Cases.Pascal: normalizedList = re.findall('^[a-z]+|[A-Z][^A-Z]*', normalizedQuery) elif fromCase == Cases.Snake or Cases.Macro: normalizedList = normalizedQuery.split('_') elif fromCase == Cases.Kebab or Cases.Cobol: normalizedList = normalizedQuery.split('-') else: normalizedList = normalizedQuery.split() actualQuery = ' '.join(normalizedList) return cls(actualQuery, toCase) def __normalizeString(query: str) -> str: return query.translate(str.maketrans('', '', string.punctuation)) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,10 @@ from CaseConverter import Convert, Cases # Convert to snake Convert("This is a cry for help", Cases.Snake) # this_is_a_cry_for_help # Convert to custom type (delimeter required) Convert("What do the numbers mean, Mason?", Cases.Custom, delimeter="|") # what|do|the|numbers|mean|mason # Convert from one case to another (from custom case type not supported) Convert.fromCase("thisIsDefinitelyNotFun", Cases.Camel, Cases.Cobol) # IT-IS-DEFINITELY-NOT-FUN