Skip to main content

Additional methods for Python's immutable data types.

Project description

Data Type Tools

Includes useful helper methods for Python's immutable data types using the forbiddenfruit library.

Dict Tools

  • sort_by_val
    • Sorts dictionary by values, expects dictionary with a depth of 1
      >>> obj = {'a': 3, 'b': 1, 'c': 2}
      >>> obj.sort_by_val(sort_type='int')
      {'b': 1, 'c': 2, 'a': 3}
      
    • Arguments
      • self: dictionary
      • asc: boolean (True)
        • Ascending order when True, Descending when False
      • sort_type: string ('string')
        • Data Type for sort, includes 'string', 'int', 'float', and 'date' types
        • Date Type is a string representing a date
        • Data does not need to match type i.e. a string with an int value '1'
        • Returns original data type
      • date_input: string ('mdy')
        • Date input format
        • i.e. 'ymd', 'mdy', etc.
    • Returns sorted dict
  • sort_by_key
    • Sorts dictionary by keys, expects dictionary with a depth of 1
       >>> obj = {'c': 1, 'a': 3, 'b': 2}
       >>> obj.sort_by_key(sort_type='string')
       {'a': 3, 'b': 2, 'c': 1}
      
    • Arguments
      • self: dictionary
      • asc: boolean (True)
        • Ascending order when True, Descending when False
      • sort_type: string ('string')
        • Data Type for sort, includes 'string', 'int', 'float', and 'date' types
        • Date Type is a string representing a date
        • Data does not need to match type i.e. a string with an int value '1'
        • Returns original data type
      • date_input: string ('mdy')
        • Date input format
        • i.e. 'ymd', 'mdy', etc.
    • Returns sorted dict
  • nested_sort
    • Sorts dictionary by nested key value
       >>> obj = {'a': {'b': {'c': 2}}, 'b': {'b': {'c': 1}}}
       >>> obj.nested_sort(nested_args=[{'keys': ['b', 'c'], 'sort_type': 'int'}])
       {'b': {'b': {'c': 1}}, 'a': {'b': {'c': 2}}}
      
    • Arguments
      • self: dictionary
      • asc: boolean (True)
        • Ascending order when True, Descending when False
      • nested_args: list
        • List of sort argument dicts, can include multiple to sort by multiple nested keys respectively
        • Args
          • keys: list
            • list of nested keys respectively i.e. ['b', 'c', 'd'] will sort by key ['b']['c']['d']
          • sort_type: string ('string')
            • Data Type for sort, includes 'string', 'int', 'float', and 'date' types
            • Date Type is a string representing a date
            • Data does not need to match type i.e. a string with an int value '1'
            • Returns original data type
          • date_input: string ('mdy')
            • Date input format
            • i.e. 'ymd', 'mdy', etc.
    • Returns sorted dict

List Tools

  • sort_by_val
    • Sorts list of dictionaries by values, expects dictionaries with a depth of 1
      >>> li = [{'a': 3}, {'b': 1}, {'c': 2}]
      >>> li.sort_by_val(sort_type='int')
      [{'b': 1}, {'c': 2}, {'a': 3}]
      
    • Arguments
      • self: list
      • asc: boolean (True)
        • Ascending order when True, Descending when False
      • sort_type: string ('string')
        • Data Type for sort, includes 'string', 'int', 'float', and 'date' types
        • Date Type is a string representing a date
        • Data does not need to match type i.e. a string with an int value '1'
        • Returns original data type
      • date_input: string ('mdy')
        • Date input format
        • i.e. 'ymd', 'mdy', etc.
    • Returns sorted list
  • sort_by_key
    • Sorts list of dictionaries by keys, expects dictionaries with a depth of 1
       >>> li = [{'c': 1}, {'a': 3}, {'b': 2}]
       >>> li.sort_by_key(sort_type='string')
       [{'a': 3}, {'b': 2}, {'c': 1}]
      
    • Arguments
      • self: list
      • asc: boolean (True)
        • Ascending order when True, Descending when False
      • sort_type: string ('string')
        • Data Type for sort, includes 'string', 'int', 'float', and 'date' types
        • Date Type is a string representing a date
        • Data does not need to match type i.e. a string with an int value '1'
        • Returns original data type
      • date_input: string ('mdy')
        • Date input format
        • i.e. 'ymd', 'mdy', etc.
    • Returns sorted list
  • sort_by_key_val
    • Sorts list of dictionaries by specific key value, expects dictionaries with a depth of 1
       >>> li = [{'a': 1, 'b': 2}, {'a': 2, 'b': 1}]
       >>> li.sort_by_key_val(key='b', sort_type='string')
       [{'a': 2, 'b': 1}, {'a': 1, 'b': 2}]
      
    • Arguments
      • self: list
      • asc: boolean (True)
        • Ascending order when True, Descending when False
      • sort_type: string ('string')
        • Data Type for sort, includes 'string', 'int', 'float', and 'date' types
        • Date Type is a string representing a date
        • Data does not need to match type i.e. a string with an int value '1'
        • Returns original data type
      • date_input: string ('mdy')
        • Date input format
        • i.e. 'ymd', 'mdy', etc.
    • Returns sorted list
  • nested_sort
    • Sorts list of dictionaries by nested key value
       >>> li = [{'a': {'b': {'c': 2}}}, {'b': {'b': {'c': 1}}}]
       >>> li.nested_sort(nested_args=[{'keys': ['b', 'c'], 'sort_type': 'int'}])
       [{'b': {'b': {'c': 1}}}, {'a': {'b': {'c': 2}}}]
      
    • Arguments
      • self: list
      • asc: boolean (True)
        • Ascending order when True, Descending when False
      • nested_args: list
        • List of sort argument dicts, can include multiple to sort by multiple nested keys respectively
        • Args
          • keys: list
            • list of nested keys respectively i.e. ['b', 'c', 'd'] will sort by key ['b']['c']['d']
          • sort_type: string ('string')
            • Data Type for sort, includes 'string', 'int', 'float', and 'date' types
            • Date Type is a string representing a date
            • Data does not need to match type i.e. a string with an int value '1'
            • Returns original data type
          • date_input: string ('mdy')
            • Date input format
            • i.e. 'ymd', 'mdy', etc.
    • Returns sorted list

String Tools

  • replace_multiple
    • Replace multiple substrings or characters in a string with their respective replacements
       >>> 'The Apple is Red'.replace_multiple({'Apple': 'Grass', 'Red': 'Green'})
       'The Grass is Green'
      
    • Arguments
      • self: string
      • dictionary: dict
        • dict keys are the values to replace, and key values will be their respective replacements in the string
    • Returns string with replacements
  • format_date
    • Formats string representing date in multiple formats
       >>> '1/01/75'.format_date(date_input='mdy', date_format='mmddyyyy', delimiter='-')
       '01-01-1975'
      
    • Arguments
      • self: string
      • date_input: string ('mdy')
        • date input order, i.e. 'mdy' for month, day, year, and 'ymd' for year, month, day
      • date_format string ('mmddyy')
        • date output format, the number of each letter correspond to date digits, i.e. a date with 'mmddyyyy' will have 4 digits for year while a date with 'mmddyy' will only have 2 digits for year
      • delimiter: string ('')
        • delimiter for date output, i.e. '/' yields 01/01/2019
    • Returns formatted date string
  • find_nth
    • Find nth occurence of substring in string
       >>> 'apple picking'.find_nth('p', 3)
       11
      
    • Arguments
      • self: string
      • string: string
        • substring to search in string
      • n: int
        • 1-based occurence number of substring to find in string.
    • Returns int index

Float Tools

  • round
    • Round value to n decimal places (scientific rounding, fixes python rounding errors)
       >>> 4.055.round(2)
       4.06
       >>> 4.054(2)
       4.05
      
    • Arguments
      • self: float
      • places: int (2)
        • Number of places to round to
    • Returns rounded float

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

datatype-tools-0.0.1.tar.gz (6.4 kB view hashes)

Uploaded Source

Built Distribution

datatype_tools-0.0.1-py3-none-any.whl (9.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page