Skip to main content

Add your description here

Project description

Handy

Installation

pip install handyobj

Why you might consider using this package ?

In javascript when you work with arrays, you can do some chaining operation in a functional programming style like :

jsList = [1, 2, 3, 4]
console.log(jsList.map(el => el * 2))
>>> [ 2, 4, 6, 8 ]
jsList = [1, 2, 3, 4]
console.log(jsList.filter(el => el % 2 == 0).map(el => el + 1))
[ 3, 5 ]

Python offers functools module with map and filter but they are not as handy as in javascript when we want to chain them in FP style.

This is why i'm proposing this package that offers 2 objects, SmartList that act as a simple wrapper on standard list and offers methods like (map,sort, filter_by_redicates, filter_by_val_attributes etc...) and ObjectDict that act as a wrapper on a normal dict, and allow access to it's values directly as if they were attributes of the object (works also with nested dict).

So SmartList used in combination with ObjectDict offers you the same experience as you would have in javascript. In some scenarios i found this pattern really handy to extract the data i want from 3rd party apis when they were not granular enough.

Usage

# Condider we have the following data list of dict
ldict = [{'name': 'Kenneth Thomas',
  'job': 'Broadcast presenter',
  'company': 'Williams-Miller'},
 {'name': 'Edward Hamilton',
  'job': 'Quarry manager',
  'company': 'Anderson-Kline'},
 {'name': 'Brandon Weeks',
  'job': 'Theme park manager',
  'company': 'Williams, Garcia and Allen'},
 {'name': 'Kim Little',
  'job': 'Insurance risk surveyor',
  'company': 'Frank-Hutchinson'},
 {'name': 'David Gutierrez',
  'job': 'Community pharmacist',
  'company': 'Allen, Herman and Ellis'},
 {'name': 'Matthew Cook',
  'job': 'Chief Operating Officer',
  'company': 'Hansen-Young'},
 {'name': 'Mark Watson',
  'job': 'Doctor, general practice',
  'company': 'Vargas PLC'},
 {'name': 'Jessica Ellis',
  'job': 'Tourist information centre manager',
  'company': 'Williams-Reeves'},
 {'name': 'Jessica Duncan',
  'job': 'Administrator, Civil Service',
  'company': 'Hobbs Group'},
 {'name': 'Diana Butler',
  'job': 'Scientist, research (maths)',
  'company': 'Olsen, Maddox and Smith'}]
# Now consider i want extract just people that work as managers, here is how i do 

from handyobj import SmartList, ObjectDict

managers = SmartList(ldict).\
    map(lambda el : ObjectDict(el)).\
    filter_by_predicates(lambda el : "manager" in  el.job.lower())


>>> [{'name': 'Edward Hamilton',
  'job': 'Quarry manager',
  'company': 'Anderson-Kline'},
 {'name': 'Brandon Weeks',
  'job': 'Theme park manager',
  'company': 'Williams, Garcia and Allen'},
 {'name': 'Jessica Ellis',
  'job': 'Tourist information centre manager',
  'company': 'Williams-Reeves'}]

# There are more things you can do with the methods of smart list, like aggregation by predicates, or check attribute equality or pattern matching : 


# len -> gives you lenght of your list 
# map -> map your list to a new list
# first -> give you first element of list (index error if doesnt exist)
# last -> give you last element of list (index error if doesnt exist)
# last_or_none  -> give you last element of list (None if doesn't exist)
# first_or_none  -> give you first element of list (None if doesn't exist)
# one_or_none -> one element or None otherwise
# sort -> sort your list to a new list 
# filter_by_predicates -> filter by predicates (functions that return true or false)
# filter_by_attribute_values -> filter by keyword attribute values()
# filter_by_matched_attributes -> filter by keyword patterns values()
# group_by_labeled_predicates -> group by [(label, predicate)], you obtain a SmartList of tuples [(label, filtered_smart_list)]

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

handyobj-0.1.3.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

handyobj-0.1.3-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file handyobj-0.1.3.tar.gz.

File metadata

  • Download URL: handyobj-0.1.3.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for handyobj-0.1.3.tar.gz
Algorithm Hash digest
SHA256 46c779264b5d7099142890c6fb5b67dcb82c00e80562c87259d3704bd025b5c8
MD5 07c5a1b838172c2c43f89ef23dc95de2
BLAKE2b-256 a517c5f775375fd3841da2a2903e68975f5bd13e81999e8f4a1e382861fad5e2

See more details on using hashes here.

File details

Details for the file handyobj-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: handyobj-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for handyobj-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8a38d052021bd140babd9b5fb5fb94a9a869d843d9c2175699beb92ba6b116ab
MD5 d5af49955ccd4026e807eacc645075ab
BLAKE2b-256 1ac24911986333782b9f669dc970ec1d4e430af253a3d432d1d87f6ed281cdaf

See more details on using hashes here.

Supported by

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