Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
First Python Project
#3
Thanks a lot for the response! I wasn't aware that many of the functions / methods you suggested even existed while making python-utils. I've made some edits to my code. Please let me know what you think:

# Adds commas to a number.
# Example: add_commas(1000) returns "1,000"
def add_commas(number: int):
    return '{:,}'.format(number)

# This function creates a string with a repeating argument.
# Example: repeat("#", 5") returns "#####"
def repeat(args: str, amount: int):
    return args * amount

# Removes all empty values from a list.
# Example: trim(['1', '2', '3', '']) returns ['1', '2', '3']
def trim(array: list):
    return [item for item in array if item]

# Lowercase all string values in a list
# Example: lowercase(['A', 'B', 'C']) returns ['a', 'b', 'c']
def lowercase(array: list, first: bool = False):
    if first:
        vs = []
        for i, item in enumerate(array):
            vs.append(array[i][0].lower() + array[i][1:])
        return vs
    else:
        return list(map(str.lower, array))

# Removes all numbers from the string.
def remove_numbers(text: str):
    for c in string.digits:
        text.replace(c, '')
    return text


# Removes all letters from the string.
def remove_letters(text: str):
    for c in string.ascii_letters[:26]:
        text.replace(c, '')
    return text

# Returns a new list containing values with the specified data type.
# Example: extract(["Hello, World!", 1, 2, 3], int) returns [1, 2, 3]
def extract(array: list, data: type):
    return [item for item in array if isinstance(item, data)]
Reply


Messages In This Thread
First Python Project - by Brennan - Jun-27-2018, 04:03 AM
RE: First Python Project - by buran - Jun-27-2018, 08:03 AM
RE: First Python Project - by Brennan - Jun-27-2018, 06:33 PM
RE: First Python Project - by buran - Jun-27-2018, 07:07 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020