Python Package FTHelper

Selam dostlar, benim gibi PHP’den başlayıp, Python’da da geliştirmeye devam edenlerin en büyük sıkıntısı array, dict, list ve tuple kavramları olsa gerek.

Php de alışkın olduğumuz array fonksiyonları maalesef Python’da yok. Bende bu durumdan rahatsız olan birisi olarak. PHP ve Laravel’de sıklıkla kullandığım array fonksiyonlarını Python pip paketi olarak yazdım 🙂 Umarım sizinde işinize yarar.

Kurulum

pip install fthelper 

Kullanım

from fthelper import *
# or
from fthelper import Arr, Str 


Dictionary and list methods (Arr)


Array Column

Sütun adıyla tanımlanan, dictionary tek bir sütunundaki değerleri verir

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

Listenin seçilen değerini döndürür.

myList = [2, 4, 6, 8, 10]
result = Arr.first(arr=myList)
# 2
myList = [2, 4, 6, 8, 10]
result = Arr.last(arr=myList)
# 10
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

keys dizisinden anahtarları, values dizisinden değerleri alıp oluşturduğu ilişkisel diziyi döndürür.

keys = ['a', 'b', 'c']
vals = (1, 2, 3)

result = Arr.combine(keys, vals)
# {'a': 1, 'b': 2, 'c': 3}

Divide

dictionary’i key ve value‘lara böler

keyList, ValueList = Arr.divide({
    'name': 'tunahan',
    'surname': 'irday'
})
# ['name', 'surname']
# ['tunahan', 'irday']

Have an item (Has)

dictionary’deki öğenin varlığını kontrol eder

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

dictionary’den dictionary listesi alma

single = {'name': 'Desk', 'price': 100, 'orders': 10}
result = Arr.only(single, ['name', 'price'])
# {'name': 'Desk', 'price': 100}

Pluck

pluck yöntemi, belirli bir anahtar için tüm değerleri alır

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

Listenin en üstüne öğe ekleyin

r = Arr.prepend(['a', 'b', 'c'], 'd')
# ['d', 'a', 'b', 'c']

Exists

Dictionary veya liste içinde aramak için kullanılır

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

liste içinden rastgele öğe döndürür

r = Arr.random(['a', 'b', 'c'])
# b

Wrap

wrap yöntemi, verilen değeri bir list veya dictionary çevirir

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

filter yöntemi, listedeki boş ve None değerleri temizler

ff = ['', None, 14, 'qwe']
r = Arr.array_filter(ff)
# [14, 'qwe']


String methods (Str)


Random

Belirtilen uzunlukta bir string oluşturur. Uzunluk belirtilmezse 16 karakter olarak çalışır

result = Str.random()
# lQCcgS8V3fRfpjS4

result = Str.random(20)
# KLzXIkwNstlEs97oZuLd

Limit

limit yöntemi, verilen string’i belirtilen uzunlukta keser. Kesilmiş string’in sonuna, string eklemek için üçüncü parametre kullanılır

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

words yöntemi bir string’i kelimelerinden böler, istenilen kelime sayısında keser. Kesilmiş string’in sonuna, string eklemek için üçüncü parametre kullanılır

result = Str.words('Perfectly balanced, as all things should be', 3)
# Perfectly balanced, as

result = Str.words('Perfectly balanced, as all things should be', 3, '!..')
# Perfectly balanced, as!..

String Cases

StringCase yöntemleri, verilen string’i istenen StringCase’e dönüştürür

  • 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

isStr yöntemi, belirli bir dizginin string olup olmadığına karar verir

result = Str.isStr('13')
# True

result = Str.isStr(13)
# False

isAscii

isAscii yöntemi, belirli bir dizginin ascii olup olmadığına karar verir

result = Str.isAscii('ascii code'.encode())
# True

result = Str.isAscii('ascii code')
# False

isEmpty

isEmpty yöntemi, belirli bir dizginin empty olup olmadığına karar verir

result = Str.isEmpty('string')
# False

result = Str.isEmpty(13)
# False

result = Str.isEmpty(None)
# True

result = Str.isEmpty('')
# True

Start

start yöntemi, istenilen değerle başlamıyorsa, string’e verilen değerin tek bir örneğini ekler

result = Str.start('this/string', '/')
# /this/string

result = Str.start('/this/string', '/')
# /this/string


Fluent strings (Str.of)


Fluid strings, dize değerleriyle çalışmak için daha akıcı, nesneye yönelik bir arabirim sağlar ve geleneksel dizgi işlemlerine kıyasla daha okunabilir bir sözdizimi kullanarak birden çok dize işlemini birbirine bağlamanıza olanak tanır.

Append

append yöntemi verilen değerleri dizeye ekler

of = Str.of('Fatih').append(' Irday')
of.getValue()
# Fatih Irday

Prepend

prepend yöntemi verilen değerleri dizin başına ekler

of = Str.of('Fatih Irday').prepend('Person : ')
of.getValue()
# Person : Fatih Irday

Replace

replace yöntemi, string içindeki değerleri değiştirir

of = Str.of('Python 2.x, Python version 2').replace('2', '3')
of.getValue()
# Person : Python 3.x, Python version 3

ReplaceFirst

replaceFirst yöntemi, string içinde istenilen ilk değeri değiştirir

of = Str.of('Python 2.x, Python version 2').replaceFirst('2', '3')
of.getValue()
# Person : Python 3.x, Python version 2

ReplaceLast

replaceLast yöntemi, string içinde istenilen son değeri değiştirir

of = Str.of('Python 2.x, Python version 2').replaceLast('2', '3')
of.getValue()
# Person : Python 2.x, Python version 3

ReplaceList

replaceList yöntemi, string’deki belirli bir değeri liste kullanarak sırayla değiştirir

of = Str.of('Python ?.x and ?.x').replaceList('?', ['3.6', '3.9'])
of.getValue()
# Python 3.6.x and 3.9.x

ReplaceMatches

replaceMatches yöntemi, bir desenle eşleşen string’lerin tüm bölümlerini verilen değiştirme string’i ile değiştirir

of = Str.of('(+1) 501-555-1000').replaceMatches("[^A-Za-z0-9]", '')
of.getValue()
# 15015551000

After

after yöntemi, string’te aranan ilk değerden sonraki her şeyi döndürür. Değer aranan öğe yoksa string’in tamamı döndürülür

of = Str.of('This is my name').after('is')
of.getValue()
# ' is my name'

AfterLast

afterLast yöntemi, string’te aranan son değerden sonraki her şeyi döndürür. Değer aranan öğe yoksa string’in tamamı döndürülür

of = Str.of('This is my name').afterLast('is')
of.getValue()
# ' my name'

Before

before yöntemi, string’te aranan ilk değerden önceki her şeyi döndürür. Değer aranan öğe yoksa string’in tamamı döndürülür

of = Str.of('This is my name').before('is')
of.getValue()
# ' Th'

BeforeLast

beforeLast yöntemi, string’te aranan son değerden önceki her şeyi döndürür. Değer aranan öğe yoksa string’in tamamı döndürülür

of = Str.of('This is my name').beforeLast('is')
of.getValue()
# ' This'



Not : Repoya direk erişim için bu bağlantıyı kullanabilirsiniz. Geliştirme konusunda destek olmak için ise bu bağlantıyı 🙂

Yorum Ekle

E-posta hesabınız yayımlanmayacak.

Gerçek kişi doğrulaması
5 + 3 =