Dict, list and string helper package
Project description
FTHelper V0.1
FTHelper is a package developed for dictionary and list operations.
from fthelper import *
# or
from fthelper import Arr, Str
Dictionary and list methods (Arr)
Array Column
Returns the values from a single column of the dictionary, identified by the column name
myList = [
{
'name': 'fatih',
'surname': 'irday'
},
{
'name': 'tugba',
'surname': 'irday'
},
{
'name': 'tunahan',
'surname': 'irday'
}
]
result = Arr.column(arr=myList, key='name')
# ['fatih', 'tugba', 'tunahan']
developers = [
{'developer': {'id': 1, 'name': 'Rasmus', 'surname': 'LERDORF'}},
{'developer': {'id': 2, 'name': 'Guido van', 'surname': 'ROSSUM'}},
{'developer': {'id': 3, 'name': 'Fatih', 'surname': 'IRDAY'}},
]
result = Arr.column(arr=developers, key='developer.surname')
# ['LERDORF', 'ROSSUM', 'IRDAY']
Selectors
Shifts the selected value of the list off and returns it.
first
myList = [2, 4, 6, 8, 10]
result = Arr.first(arr=myList)
# 2
last
result = Arr.last(arr=myList)
# 10
Single get
single = {'local': 100, 'poc': 200, 'product': 300}
result = Arr.get(single, 'poc')
# 200
tree = {'products': {'desk': {'price': 100}, 'ask': {'price': 120}}}
result = Arr.get(tree, 'products.desk.price')
# 100
developers = [
{'developer': {'id': 1, 'name': 'Rasmus', 'surname': 'LERDORF'}},
{'developer': {'id': 2, 'name': 'Guido van', 'surname': 'ROSSUM'}},
{'developer': {'id': 3, 'name': 'Fatih', 'surname': 'IRDAY'}},
]
result = Arr.get(arr=developers, key_path='developer.name')
# ['Rasmus', 'Guido van', 'Fatih']
Combine
Creates a dictionary by using the values from the keys list as keys, and the values from the values listed as the corresponding values.
keys = ['a', 'b', 'c']
vals = (1, 2, 3)
result = Arr.combine(keys, vals)
# {'a': 1, 'b': 2, 'c': 3}
Divide
Divides dictionary into keys and values
keyList, ValueList = Arr.divide({
'name': 'tunahan',
'surname': 'irday'
})
# ['name', 'surname']
# ['tunahan', 'irday']
Have an item (Has)
Have an item in the dictionary
single = {'local': 100, 'poc': 200, 'product': 300}
result = Arr.has(single, 'poc')
# True
result = Arr.has(single, 'demo')
# False
tree = {'products': {'desk': {'price': 100}, 'ask': {'price': 120}}}
result = Arr.has(tree, 'products.desk.price')
# True
result = Arr.has(tree, 'products.desk.prices')
# False
developers = [
{'developer': {'id': 1, 'name': 'Rasmus', 'surname': 'LERDORF'}},
{'developer': {'id': 2, 'name': 'Guido van', 'surname': 'ROSSUM'}},
{'developer': {'id': 3, 'name': 'Fatih', 'surname': 'IRDAY'}},
]
result = Arr.has(arr=developers, key_path='developer.name')
# True
result = Arr.has(arr=developers, key_path='developer.language')
# False
Only
Getting dictionary list from dictionaries
single = {'name': 'Desk', 'price': 100, 'orders': 10}
result = Arr.only(single, ['name', 'price'])
# {'name': 'Desk', 'price': 100}
Pluck
The pluck
method retrieves all of the values for a given key:
myList = [
{
'name': 'fatih',
'surname': 'irday'
},
{
'name': 'tugba',
'surname': 'irday'
},
{
'name': 'tunahan',
'surname': 'irday'
}
]
r = Arr.pluck(myList, keys='name')
# ['fatih', 'tugba', 'tunahan']
r = Arr.pluck(myList, keys='name', vals='surname')
# {'fatih': 'irday', 'tugba': 'irday', 'tunahan': 'irday'}
multiple = [
{'developer': {'id': 1, 'name': 'Rasmus', 'surname': 'LERDORF'}},
{'developer': {'id': 2, 'name': 'Guido van', 'surname': 'ROSSUM'}},
{'developer': {'id': 3, 'name': 'Fatih', 'surname': 'IRDAY'}},
]
r = Arr.pluck(multiple, keys='developer.id', vals='developer.name')
# {1: 'Rasmus', 2: 'Guido van', 3: 'Fatih'}
r = Arr.pluck(multiple, keys='developer.id')
# {1: None, 2: None, 3: None}
r = Arr.pluck(multiple, vals='developer.name')
# ['Rasmus', 'Guido van', 'Fatih']
Prepend
Add items to the top of the list
r = Arr.prepend(['a', 'b', 'c'], 'd')
['d', 'a', 'b', 'c']
Exists
Used to search dictionaries or list
r = Arr.exists(['a', 'b', 'c'], search='c')
# True
myDict = {'id': 3, 'name': 'Fatih', 'surname': 'IRDAY'}
r = Arr.exists(myDict, search='Fatih')
# True
developers = [
{'developer': {'id': 1, 'name': 'Rasmus', 'surname': 'LERDORF'}},
{'developer': {'id': 2, 'name': 'Guido van', 'surname': 'ROSSUM'}},
{'developer': {'id': 3, 'name': 'Fatih', 'surname': 'IRDAY'}},
]
r = Arr.exists(developers, search='Guido van', key='developer.name')
# True
myDict = {'developer': {'id': 3, 'name': 'Fatih', 'surname': 'IRDAY'}}
r = Arr.keyExists(myDict, 'developer')
# True
r = Arr.keyExists(myDict, 'developer.id')
# True
Random
Fetches a random item from the list
r = Arr.random(['a', 'b', 'c'])
# b
Wrap
The wrap
method wraps the given value in an list or dictionary.
r = Arr.wrap(None)
# []
r = Arr.wrap()
# []
r = Arr.wrap('demo')
# ['demo']
r = Arr.wrap('developer.name.first', 'fatih')
# {'developer': {'name': {'first': 'fatih'}}}
Array Filter
filters empty and none values in the list.
ff = ['', None, 14, 'qwe']
r = Arr.array_filter(ff)
# [14, 'qwe']
String methods (Str)
Random
Creates a string of the specified length. Runs as 16 characters if length is not specified
result = Str.random()
# lQCcgS8V3fRfpjS4
result = Str.random(20)
# KLzXIkwNstlEs97oZuLd
Limit
The limit
method truncates the given string to the specified length. An additional string may be passed to this method via its third argument to specify which string should be appended to the end of the truncated string
result = Str.limit('Perfectly balanced, as all things should be.', 9)
# Perfectly
result = Str.limit('Perfectly balanced, as all things should be.', 9, '!..')
# Perfectly!..
Words
The words
method limits the number of words in a string. An additional string may be passed to this method via its third argument to specify which string should be appended to the end of the truncated string
result = Str.limit('Perfectly balanced, as all things should be.', 9)
# Perfectly
result = Str.limit('Perfectly balanced, as all things should be.', 9, '!..')
# Perfectly!..
String Cases
StringCase methods convert the given string to the desired StringCase
- kebab case
- camel case
- pascal case
- snake case
result = Str.kebabcase('fatih irday')
# fatih-irday
result = Str.camelcase('fatih irday')
# fatihIrday
result = Str.pascalcase('fatih irday')
# FatihIrday
result = Str.snakecase('fatih irday')
# fatih_irday
isStr
The isStr
method determines if a given string matches a given pattern
result = Str.isStr('13')
# True
result = Str.isStr(13)
# False
isAscii
The isAscii
method determines if a given ASCII matches a given pattern
result = Str.isAscii('ascii code'.encode())
# True
result = Str.isAscii('ascii code')
# False
isEmpty
The isEmpty
method determines if the given string is empty
result = Str.isEmpty('string')
# False
result = Str.isEmpty(13)
# False
result = Str.isEmpty(None)
# True
result = Str.isEmpty('')
# True
Start
The start
method adds a single instance of the given value to a string if it does not already start with that value
result = Str.start('this/string', '/')
# /this/string
result = Str.start('/this/string', '/')
# /this/string
Fluent strings (Str.of)
Fluent strings provide a more fluent, object-oriented interface for working with string values, allowing you to chain multiple string operations together using a more readable syntax compared to traditional string operations.
Append
The append
method appends the given values to the string
of = Str.of('Fatih').append(' Irday')
of.getValue()
# Fatih Irday
Prepend
The prepend
method prepends the given values onto the string
of = Str.of('Fatih Irday').prepend('Person : ')
of.getValue()
# Person : Fatih Irday
Replace
The replace
method replaces a given string within the string
of = Str.of('Python 2.x, Python version 2').replace('2', '3')
of.getValue()
# Person : Python 3.x, Python version 3
Replace First
The replaceFirst
method replaces the first occurrence of a given value in a string
of = Str.of('Python 2.x, Python version 2').replaceFirst('2', '3')
of.getValue()
# Person : Python 3.x, Python version 2
Replace Last
The replaceLast
method replaces the last occurrence of a given value in a string
of = Str.of('Python 2.x, Python version 2').replaceLast('2', '3')
of.getValue()
# Person : Python 2.x, Python version 3
Replace List
The replaceList
method replaces a given value in the string sequentially using a list
of = Str.of('Python ?.x and ?.x').replaceList('?', ['3.6', '3.9'])
of.getValue()
# Python 3.6.x and 3.9.x
Replace Matches
The replaceMatches
method replaces all portions of a string matching a pattern with the given replacement string
of = Str.of('(+1) 501-555-1000').replaceMatches("[^A-Za-z0-9]", '')
of.getValue()
# 15015551000
After
The after
method returns everything after the given value in a string. The entire string will be returned if the value does not exist within the string
of = Str.of('This is my name').after('is')
of.getValue()
# ' is my name'
After Last
The afterLast
method returns everything after the last occurrence of the given value in a string. The entire string will be returned if the value does not exist within the string
of = Str.of('This is my name').afterLast('is')
of.getValue()
# ' my name'
Before
The before
method returns everything before the given value in a string
of = Str.of('This is my name').before('is')
of.getValue()
# ' Th'
Before Last
The beforeLast
method returns everything before the last occurrence of the given value in a string
of = Str.of('This is my name').beforeLast('is')
of.getValue()
# ' This'
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.