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
Metadata
Release files for headtype 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| headtype-0.1.0.tar.gz | 5.9 kB | Without provenance | Details for headtype-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| headtype-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for headtype-0.1.0-py3-none-any.whl |
Total release size: 12.3 kB
Release files / headtype-0.1.0.tar.gz
| Download URL | headtype-0.1.0.tar.gz |
|---|---|
| Size | 5.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e9b9af9b703325543917e4f3e576b5d3f4d3ce3e6ba9eeabc7e228a3ead49764
|
|
BLAKE2b-256 checksum How to use checksums |
d3175265c0ecec677414f98a23aef85b512c0c0eba212853fa0f3f9f1f39391e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.0
|
Release files / headtype-0.1.0-py3-none-any.whl
| Download URL | headtype-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c901ce172b7ebd59b695439b2be495820bf97081fda00b6802ae90f1d9dead91
|
|
BLAKE2b-256 checksum How to use checksums |
8ac22ae7b5ac815485084442a1c30b6efdf5daa7c19eae88feb6c788dc5c02b3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.0
|
Security for headtype 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.