Skip to main content

Helps in finding types/classes associated with object

Project description

headtype

Headtype is a python library for finding types/classes associated with object. Sometimes it may be worth it to know which object would work with class or vice-verse. You wouldnt want to pass non url object into class that expects url in string form.

Managing objects and classes could be a lot work especially having to do repeately. This library takes that away and let you focus on how classes/types would be matched against objects.

Install

Enter this into your command-line application.

pip install headtype

Usage

The first thing is to import headtype library.

import headtype

Now headtype is imported and ready to use, its time to specify types/classes to be matched by objects. This will create headtype.Type instances that contain class/type along with how it will match objects.

But first we need to setup functions for matching objects before creating those headtype.Type instances.

def names_func(object_):
    return object_ in {"marry", "john", "hope"}

def fruits_func(object_):
    return object_ in {"apple", "orange", "grapes"}

def numbers_func(object_):
    return object_ in {100, 500, 1000, 5000, 10000}

Since functions for matching are now setup, let setup types to be matched with objects. We will also create fake classes for use with examples to make things clear.

class names(): pass

class fruits(): pass

class numbers(): pass

Now everything is ready to actually create headtype.Type instances from those classes and functions.

# Here headtype.Type instances get created and associated with functions.
names_type = headtype.create_type(names, names_func)
fruits_type = headtype.create_type(fruits, fruits_func)
numbers_type = headtype.create_type(numbers, numbers_func)

Type instances need to be grouped into collection of types either in form of iterable or headtype.Types.

# Creates list with headtype.Type instances
type_objects = [names_type, fruits_type, numbers_type]
# Creates headtype.Types instance from headtype.Type instances.
types_object = headtype.create_types(type_objects)

Let now check if object and type really match with each other based on that function set type.

# 1000 is number and is not in names.
headtype.object_matches_type(1000, names_type) # False
# 'John' is in names and get matched.
headtype.object_matches_type("john", names_type) # True
# 1000 is number and satisfies numbers_type
headtype.object_matches_type(1000, numbers_type) # True

What about finding types/classes that matches certain object?, thats also possible. This is when order of items may become important especially if aim is to get single type.

# This gets first type matched(types order matters)
# Just as expected, nothing was matched.
headtype.find_matching_type(1000, type_objects) # None
# 'orange' is fruit according to fruits_type.
headtype.find_matching_type("orange", type_objects) 
# <class '__main__.fruits'>

# This gets multiple types but results is filter object.
matched_types = headtype.find_matching_types("orange", type_objects) 
list(matched_types) # [<class '__main__.fruits'>]
# Limit can be provided to limit items involved.
matched_types = headtype.find_matching_types(500, type_objects) 
list(matched_types) # [<class '__main__.numbers'>]

There is something very obvious about the headtype.Type instances in that they expect certain types of objects. Both names_type and fruits_type expects objects of str and numbers_type expects int object types.

It may be better to remove headtype.Type instances that does not correspond with object type. That is done by supplying object types supported by headtype.Type instance.

Here are new headtype.Type instances with objects types/classes they expected their objects to be. This may improve performance especially if headtype.Type instances take long to match objects.

# Much faster since objects types are provided.
names_type = headtype.create_type(names, names_func, object_types=[str])
fruits_type = headtype.create_type(names, fruits_func, object_types=[str])
numbers_type = headtype.create_type(names, names_func, object_types=[int])

License

MIT license

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

headtype-0.1.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

headtype-0.1.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file headtype-0.1.0.tar.gz.

File metadata

  • Download URL: headtype-0.1.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.0

File hashes

Hashes for headtype-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e9b9af9b703325543917e4f3e576b5d3f4d3ce3e6ba9eeabc7e228a3ead49764
MD5 fade030b822ba16d2d72645b60af2a1f
BLAKE2b-256 d3175265c0ecec677414f98a23aef85b512c0c0eba212853fa0f3f9f1f39391e

See more details on using hashes here.

File details

Details for the file headtype-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: headtype-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.0

File hashes

Hashes for headtype-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c901ce172b7ebd59b695439b2be495820bf97081fda00b6802ae90f1d9dead91
MD5 7675faafe8e84db4607e09ffebf34455
BLAKE2b-256 8ac22ae7b5ac815485084442a1c30b6efdf5daa7c19eae88feb6c788dc5c02b3

See more details on using hashes here.

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