Giter Site home page Giter Site logo

spidertype's Introduction

SpiderType

SpiderType is a Python library designed for handling and managing data types with static typing, inspired by TypeScript. This library provides a foundation for creating and registering custom types, validating values against these types, and applying static type checks to class methods.

Table of Contents

Installation

Clone the GitHub repository and install the requirements:

git clone https://github.com/simondev413/spidertype.git
cd spidertype
pip install -r requirements.txt

Usage

Registering Types

To register a new custom type, use the register_type method of the SpiderTypeMap class:

from spidertype.core import SpiderTypeMap
from spidertype.types import Type

class MyCustomType(Type):
    def validate(self, value):
        # Custom validation logic
        pass

type_map = SpiderTypeMap()
my_type = MyCustomType('CustomType')
type_map.register_type('CustomType', my_type)

Retrieving Types

To retrieve a registered type, use the get_type method of the SpiderTypeMap class:

retrieved_type = type_map.get_type('CustomType')

Validating Types

To validate a value against an expected type, use the validate method of the SpiderTypeMap class:

try:
    type_map.validate(value_to_validate, 'CustomType')
except TypeValidationError as e:
    print(e)

Static Type Checking

To apply static type checking to class methods, use the static_type_check decorator:

from spidertype.validation import static_type_check

class MyClass:
    @static_type_check
    def my_method(self, param: CustomType):
        pass

Exceptions

The SpiderType library defines several exceptions for different errors:

  • SpiderTypeError: Base exception for all errors in the SpiderType library.
  • TypeValidationError: Exception raised when type validation fails.
  • RegistrationError: Exception raised when an error occurs during type registration.
  • TypeNotFoundError: Exception raised when a specified type is not found.
  • InvalidTypeError: Exception raised when an invalid type is used.

Classes and Methods

SpiderTypeMap

  • register_type(name, type_def): Registers a new custom type.
  • get_type(name): Retrieves a registered custom type.
  • validate(value, type_name): Validates a value against an expected type.

SpiderTypeError

  • Base exception for all errors in the SpiderType library.

TypeValidationError

  • Exception raised when type validation fails.

RegistrationError

  • Exception raised when an error occurs during type registration.

TypeNotFoundError

  • Exception raised when a specified type is not found.

InvalidTypeError

  • Exception raised when an invalid type is used.

StaticTyped

  • Base class for custom static typed classes.

Type

  • validate(value): Validates a value against the custom type.

static_type_check

  • Decorator to apply static type checking to class methods.

validate_type

  • Function to validate a value against an expected type.

For more details, refer to the full library documentation.

spidertype's People

Contributors

simondev413 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.