Ultimate transformation library that supports validation, contexts and aiohttp.

Related tags

Networkingtrafaret
Overview

Trafaret

Build status @ Circle CI Gitter Chat Latest release BSD license

Ultimate transformation library that supports validation, contexts and aiohttp.

Trafaret is rigid and powerful lib to work with foreign data, configs etc. It provides simple way to check anything, and convert it accordingly to your needs.

It has shortcut syntax and ability to express anything that you can code:

", line 1, in File "/Users/mkrivushin/w/trafaret/trafaret/__init__.py", line 204, in __call__ return self.check(val) File "/Users/mkrivushin/w/trafaret/trafaret/__init__.py", line 144, in check return self._convert(self.check_and_return(value)) File "/Users/mkrivushin/w/trafaret/trafaret/__init__.py", line 1105, in check_and_return raise DataError(error=errors, trafaret=self) trafaret.DataError: {'b': DataError({1: DataError(value is not a string)})}">
>>> from trafaret.constructor import construct
>>> validator = construct({'a': int, 'b': [str]})
>>> validator({'a': 5, 'b': ['lorem', 'ipsum']})
{'a': 5, 'b': ['lorem', 'ipsum']}

>>> validator({'a': 5, 'b': ['gorky', 9]})
Traceback (most recent call last):
  File "
     
      "
     , line 1, in <module>
  File "/Users/mkrivushin/w/trafaret/trafaret/__init__.py", line 204, in __call__
    return self.check(val)
  File "/Users/mkrivushin/w/trafaret/trafaret/__init__.py", line 144, in check
    return self._convert(self.check_and_return(value))
  File "/Users/mkrivushin/w/trafaret/trafaret/__init__.py", line 1105, in check_and_return
    raise DataError(error=errors, trafaret=self)
trafaret.DataError: {'b': DataError({1: DataError(value is not a string)})}

Read The Docs hosted documentation http://trafaret.readthedocs.org/en/latest/ or look to the docs/intro.rst for start.

Trafaret can even generate Trafarets instances to build transformators from json, like in json schema implementation Trafaret Schema

New

2.0.2

  • construct for int and float will use ToInt and ToFloat

2.0.0

  • WithRepr – use it to return custom representation, like
  • Strip a lot from dict, like keys()
  • Trafarets are not mutable
  • DataError has new code attribute, self.failure has code argument
  • OnError has code argument too
  • New DataError.to_struct method that returns errors in more consistent way
  • String, AnyString, Bytes, FromBytes(encoding=utf-8)
  • Int, ToInt, Float, ToFloat
  • ToDecimal
  • Iterable that acts like a List, but works with any iterable
  • New Date, ToDate and DateTime, ToDateTime trafarets
  • StrBool trafaret renamed to ToBool
  • Visitor trafaret was deleted
  • Test coverage

1.x.x

  • converters and convert=False are deleted in favor of And and &
  • String parameter regex deleted in favor of Regexp and RegexpRaw usage
  • new OnError to customize error message
  • context=something argument for __call__ and check Trafaret methods. Supported by Or, And, Forward etc.
  • new customizable method transform like change_and_return but takes context= arg
  • new trafaret_instance.async_check method that works with await

Doc

For simple example what can be done:

import datetime
import trafaret as t

date = t.Dict(year=t.Int, month=t.Int, day=t.Int) >> (lambda d: datetime.datetime(**d))
assert date.check({'year': 2012, 'month': 1, 'day': 12}) == datetime.datetime(2012, 1, 12)

Work with regex:

>>> c = t.RegexpRaw(r'^name=(\w+)$') >> (lambda m: m.group(1))
>>> c.check('name=Jeff')
'Jeff'

Rename dict keys:

>>> c = t.Dict({(t.Key('uNJ') >> 'user_name'): t.String})
>>> c.check({'uNJ': 'Adam'})
{'user_name': 'Adam'}

Arrow date checking:

import arrow

def check_datetime(str):
    try:
        return arrow.get(str).naive
    except arrow.parser.ParserError:
        return t.DataError('value is not in proper date/time format')

Yes, you can write trafarets that simple.

Related projects

Trafaret Config

Trafaret Validator

Comments
  • SyntaxError on python 3.5

    SyntaxError on python 3.5

    Upgraded to 0.11 and seeing a SyntaxError:

    File "/home/foo/.virtualenvs/bar/lib/python3.5/site-packages/trafaret_config/__init__.py", line 3, in <module>
        from .simple import read_and_validate, parse_and_validate
      File "/home/foo/.virtualenvs/bar/lib/python3.5/site-packages/trafaret_config/simple.py", line 4, in <module>
        import trafaret as _trafaret
      File "/home/foo/.virtualenvs/bar/lib/python3.5/site-packages/trafaret/__init__.py", line 1, in <module>
        from .base import (
      File "/home/foo/.virtualenvs/bar/lib/python3.5/site-packages/trafaret/base.py", line 20, in <module>
        from .async import (
      File "/home/foo/.virtualenvs/bar/lib/python3.5/site-packages/trafaret/async.py", line 176
        yield (
        ^
    SyntaxError: 'yield' inside async function
    
    opened by thijstriemstra 8
  • Need to correct error message for Bytes checker

    Need to correct error message for Bytes checker

    In [19]: t.Bytes().check(b'test') 
    Out[19]: b'test'
    
    In [18]: t.Bytes().check('test')
    ... 
    DataError: value is not a string
    

    In my opinion we need to change string to bytes string.

    opened by Arfey 7
  • Trafaret object to pass value as is

    Trafaret object to pass value as is

    Hello and thank you for your work fiirst of all.

    I have a small suggestion to use trafaret in more uniform way. I have some data with assigned trafaret and some without constraints. So I have to do something like this: if data.trafaret: val = data.trafaret.check_and_return(rawval) else: val = rawval

    If you'll kindly add an object something like "NoCheck" which will pass arguments out without any check it will be possible to always do: val = data.trafaret.check_and_return(rawval)

    while data without constraints will be initialized as: data.trafaret = trafaret.NoCheck()

    opened by ant5 6
  • inner trafaret validation should be called via __call__ method

    inner trafaret validation should be called via __call__ method

    Basic trafarets should be called directly, instead of calling check method.

    Reason: method __call__ is an entrypoint to trafaret

    If we need to redefine method __call__ in custom trafaret, and use this trafaret in t.Or or any other containers, then its method __call__ will never be called.

    Example:

    import trafaret as t
    
    class CustomString(t.String):
        def __call__(self, val, context=None):
            print('hello')
            return super().__call__(val, context)
    
    
    Example = t.Or(CustomString(), t.Int())
    Example('foo')
    

    hello wil never be printed

    opened by litwisha 6
  • `extras.py` is still present on version 2.0.0

    `extras.py` is still present on version 2.0.0

    Steps to reproduce: install latest version from pypi

    image

    There is no extras.py in source code anymore but it is still present after installation. This creates certain confusion as two versions of KeysSubset are present (in keys.py and extras). Moreover, documentation is still pointing to the extras, which is even more confusing

    opened by azhpushkin 5
  • Too old to be a <1.0.0 release version

    Too old to be a <1.0.0 release version

    I'd like to rely on this software for important world changing projects, but it's still in 0.X version which means minor version bumps include breaking changes which is not good.

    Are there any plans to bump to 1.X so I can pin to trafaret>=1,<2? I'd like some sort of agreement of stability before I include start using this to change the world.

    opened by tim-win 5
  • `construct(dict)` doesn't convert types

    `construct(dict)` doesn't convert types

    I'm not sure if it's the intended behavior, but in 0.12 it worked without wrapping into t.Dict:

    >>> rule, raw = {"foo": int}, {"foo": "1"}
    >>> construct(rule)(raw)
    {'foo': '1'}
    >>> construct(t.Dict(rule))(raw)
    {'foo': 1}
    
    opened by imbolc 4
  • Add documentation about Iterable

    Add documentation about Iterable

    In version2 (branch) u need to add to introduction part documentation about Iterable.

    For that u can use tests which connected with current part. As example u can use this PR.

    Please don't forget:

    • your MR u need to do to the version2 (branch)
    • add screenshot with your part of documentation
    • write below that u want to take it to work
    help wanted good first issue 
    opened by Arfey 4
  • DeprecationWarning on import of abstract classes from collections

    DeprecationWarning on import of abstract classes from collections

    In python 3.7 I'm getting the following deprecation warning:

    DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
    

    collections.abc is available starting from python 3.3.

    Are there any plans on fixing it? (I guess you want to retain python2 support yet)

    Also I don't mind to create PR to fix this. If you agree, do you have any preferred way this to be done?

    opened by fly 4
  • Casting one element to a list

    Casting one element to a list

    Sorry for disturbing again.

    I wonder is it possible to convet individually supplied value to a list.

    I want to do something like: t = trafaret.List(trafaret.String()) t(['a','b'])

    ['a','b'] t('a') ['a']

    It will be amazingly cosily to pass FieldStorage.getvalue() directly to trafaret.

    opened by ant5 4
  • Add documentation about Enum

    Add documentation about Enum

    In version2 (branch) u need to add to introduction part documentation about Enum.

    For that u can use old documentation and also tests which connected with current part. As example u can use this PR.

    In this task i recommend to add some example of usage and some reach description.

    Please don't forget:

    • your MR u need to do to the version2 (branch)
    • add screenshot with your part of documentation
    • write below that u want to take it to work
    help wanted good first issue 
    opened by Arfey 3
  • Add type stubs

    Add type stubs

    When importing trafaret from a type-checked codebase, it often generates type check failures (mypy) due to missing explicit __rshift__() method declaration and -> NoReturn for the Trafaret._failure() method.

    I'm currently workarounding the issue by adding a custom stubs: https://github.com/lablup/backend.ai-common/tree/main/stubs/trafaret

    It would be nice if we could embed the type stubs or provide a separate type stubs package. You may start from my privately written type stubs.

    For details about writing/distributing type stubs, refer PEP-561.

    opened by achimnol 2
  • guard don't work correct with keyword-only arguments and default value

    guard don't work correct with keyword-only arguments and default value

    import trafaret as trf
    
    
    @trf.guard(a=trf.Int)
    def foo(*, a=1):
        pass
    
    
    foo()
    
    # GuardError: {'a': DataError('is required')}
    

    problem related to defaults property from FullArgSpec class

    def foo(*, a=1): pass
    def bar(a=1): pass
    
    getargspec(foo)
    # FullArgSpec(args=[], varargs=None, varkw=None, defaults=None, kwonlyargs=['a'], kwonlydefaults={'a': 1}, annotations={})
    
    getargspec(bar)
    # FullArgSpec(args=['a'], varargs=None, varkw=None, defaults=(1,), kwonlyargs=[], kwonlydefaults=None, annotations={})
    

    https://github.com/Deepwalker/trafaret/blob/master/trafaret/base.py#L1493-L1496

    here we need try to use the kwonlydefaults mapping

    help wanted 
    opened by Arfey 3
  • Support for Set in addition to List and Tuple?

    Support for Set in addition to List and Tuple?

    Hi, thanks for the useful package!

    Is there any technical reason behind Set support absence?

    Or I'm a very first person who actually needs it?

    If it's missed because no one needs it yet, and maintainers are not against it, I would like to provide pull request with such implementation. Documentation and tests included.

    Hopefully, new minor release would be issued after this contribution.

    Thoughts?

    Regards, Artem.

    opened by proofit404 3
  • Generate JSON schema from trafarets

    Generate JSON schema from trafarets

    Hi,

    Would it be possible to generate a JSON schema (https://json-schema.org/) from the trafaret definition? This would be really useful because the schema can be used in the IDE (vscode in my case) to autocomplete the yaml file as you are typing (https://github.com/redhat-developer/yaml-language-server). Any thoughts on this?

    opened by ErikOrjehag 4
  • Avoid checking default values

    Avoid checking default values

    When using a default value such as: t.Key("integration", default=None, trafaret=t.Regexp(r"\w+"))

    This will result in an error, as it checks the default argument against the trafaret object. It seems sensible to skip any checking of the default argument for efficiency and to allow code as shown above.

    opened by Dreamsorcerer 0
Releases(v2.0.2)
Enrich IP addresses with metadata and security IoC

Stratosphere IP enrich Get an IP address and enrich it with metadata and IoC You need API keys for VirusTotal and PassiveTotal (RiskIQ) How to use fro

Stratosphere IPS 10 Sep 25, 2022
Una simple herramienta para rastrear IP programada en Python

Spyrod-v2 Una simple herramienta para rastrear IP programada en Python Instalacion apt install git -y cd $HOME git clone https://github.com/Euronymou5

15 Dec 08, 2022
Search ports in multiples hosts

Search Port ✨ Multiples Searchs ✨ Create list hosts Create list targets Start Require Python 3.10.0+. python main.py Struture Function Directory load_

Tux#3634 7 Apr 29, 2022
Python Scrcpy Client - allows you to view and control android device in realtime

Python Scrcpy Client This package allows you to view and control android device in realtime. Note: This gif is compressed and experience lower quality

LengYue 126 Jan 02, 2023
Whoisss is a website information gatharing Tool.

Whoisss Whoisss is a website information gatharing Tool. You can cse it to collect information about website. Usage apt-get update apt-get upgrade pkg

Md. Nur habib 2 Jan 23, 2022
Interact remotely with the computer using Python and MQTT protocol 💻

Comandos_Remotos Interagir remotamento com o computador através do Python e protocolo MQTT. 💻 Status: em desenvolvimento 🚦 Objetivo: Interagir com o

Guilherme_Donizetti 6 May 10, 2022
A simple python application for generating a WiFi QR code for ease of connection

A simple python application for generating a WiFi QR code Initialize the class by providing QR code values WiFi_QR_Code(self, error_correction: int =

Ivan 2 Aug 01, 2022
Dos attack a Bluetooth connection!

Bluetooth Denial of service Script made for attacking Bluetooth Devices By Samrat Katwal. Warning This project was created only for fun purposes and p

Samrat 1 Oct 29, 2021
StarCraft II Client - protocol definitions used to communicate with StarCraft II.

Overview The StarCraft II API is an interface that provides full external control of StarCraft II. This API exposes functionality for developing softw

Blizzard Entertainment 3.6k Dec 30, 2022
A Simplest TCP client and echo server

Простейшие TCP-клиент и эхо-сервер Цель работы Познакомиться с приемами работы с сетевыми сокетами в языке программирования Python. Задания для самост

Юля Нагубнева 1 Oct 25, 2021
simple subdomain finder

Subdomain-finder Simple SubDomain finder using python which is easy to use just download and run it Wordlist you can use your own wordlist but here i

AsjadOwO 5 Sep 24, 2021
Multi-path load balancing is a method used by most of the real-time network to split the packets into different paths rather than transferring it through a single path

Multipath-Load-Balancing Method of managing incoming traffic by distributing and sharing load fairly among multiple routes from source to destination

Dharshan Kumar 6 Dec 10, 2022
This Tool can help enginners and biggener in network, the tool help you to find of any ip with subnet mask that can calucate them and show you ( Availble IP's , Subnet Mask, Network-ID, Broadcast-ID )

This Tool can help enginners and biggener in network, the tool help you to find of any ip with subnet mask that can calucate them and show you ( Availble IP's , Subnet Mask, Network-ID, Broadcast-ID

12 Dec 13, 2022
Makes dynamically updating your Cloudflare DNS records a bit easier ⏩👍😎

Easy Dynamic Cloudflare DNS Updater Makes dynamically updating your Cloudflare DNS records a bit easier ⏩ 👍 😎 If using it as a 'Dynamic DNS' client,

Zac Koch 3 Dec 19, 2021
The Delegate Network: An Interactive Voice Response Delegative Democracy Implementation of Liquid Democracy

The Delegate Network Overview The delegate network is a completely transparent, easy-to-use and understand version of what is sometimes called liquid

James Bowery 2 Feb 25, 2022
PyBERT is a serial communication link bit error rate tester simulator with a graphical user interface (GUI).

PyBERT PyBERT is a serial communication link bit error rate tester simulator with a graphical user interface (GUI). It uses the Traits/UI package of t

David Banas 59 Dec 23, 2022
Minimal, self-hosted, 0-config alternative to ngrok. Caddy+OpenSSH+50 lines of Python.

If you have a webserver running on one computer (say your development laptop), and you want to expose it securely (ie HTTPS) via a public URL, SirTunnel allows you to easily do that.

Anders Pitman 423 Jan 02, 2023
Dnspython is a DNS toolkit for Python.

dnspython is a DNS toolkit for Python. It supports almost all record types. It can be used for queries, zone transfers, and dynamic updates. It supports TSIG authenticated messages and EDNS0.

Bob Halley 2.1k Jan 06, 2023
BlueHawk is an HTTP/1.1 compliant web server developed in python

This project is done as a part of Computer Networks course. It aims at the implementation of the HTTP/1.1 Protocol based on RFC 2616 and includes the basic HTTP methods of GET, POST, PUT, DELETE and

2 Nov 11, 2022
Converts Cisco formatted MAC Addresses to PC formatted MAC Addresses

Cisco-MAC-to-PC-MAC Converts a file with a list of Cisco formatted MAC Addresses to PC formatted MAC Addresses... Ex: abcd.efgh.ijkl to AB:CD:EF:GH:I

Stew Alexander 0 Jan 04, 2022