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)
A p2p chat app for zephyr

A p2p chat app for zephyr

L3gacy B3ta 4 Jun 02, 2021
A simple framwork to streamline the Domain Adaptation training process.

FastDA Introduction This is a simple framework for domain adaptation training. You can use it to build your own training process. It heavily relies on

Vincent Zhang 7 Nov 22, 2022
This Tool Help To Information gathering for domain name or ip address...

Owl-Eye This Tool Help To Information gathering for domain name or ip address... follow this command $apt update && upgrade $apt install python apt in

Black Owl 6 Nov 12, 2022
Way find out if DNS is down or your instance

DNS-PING Way to find out if DNS is down or your instance Problem: At times it happens that DNS provider services of a website URL is down and so to re

Giten Mitra 4 Nov 18, 2022
Equibles Stocks API for Python

Equibles Stocks API for Python Requirements. Python 2.7 and 3.4+ Installation & Usage pip install If the python package is hosted on Github, you can i

Equibles 3 Apr 15, 2022
A Python tool used to automate the execution of the following tools : Nmap , Nikto and Dirsearch but also to automate the report generation during a Web Penetration Testing

📡 WebMap A Python tool used to automate the execution of the following tools : Nmap , Nikto and Dirsearch but also to automate the report generation

Iliass Alami Qammouri 274 Jan 01, 2023
A Python framework for interacting with Solana's Pyth network.

Pyth Network A basic Python framework for reading and decoding data regarding the Pyth network

1 Nov 29, 2021
Tsunami-Fi is simple multi-tool bash application for Wi-Fi attacks

🪴 Tsunami-Fi 🪴 Русская версия README 🌿 Description 🌿 Tsunami-Fi is simple multi-tool bash application for Wi-Fi WPS PixieDust and NullPIN attack,

【Kiko】 35 Dec 09, 2022
Roadster - Distance to Closest Road Feature Server

Roadster: Distance to Closest Road Feature Server Milliarium Aerum, the zero of

Textualization Software Ltd. 4 May 23, 2022
Port Traffic/Bandwidth Monitor Script

python-switch-port-traffic-alarm Port Traffic/Bandwidth Monitor Script That's an Switch Port Traffic monitor program is checking the switch uplink por

goksinenki 4 Sep 02, 2021
EV: IDS Evasion via Packet Manipulation

EV: IDS Evasion via TCP/IP Packet Manipulation 中文文档 Introduction EV is a tool that allows you crafting TCP packets and leveraging some well-known TCP/

256 Dec 08, 2022
A pretty quick and simple interface to paramiko SFTP

A pretty quick and simple interface to paramiko SFTP. Provides multi-threaded routines with progress notifications for reliable, asynchronous transfers. This is a Python3 optimized fork of pysftp wit

14 Dec 21, 2022
Event-driven networking engine written in Python.

Twisted For information on changes in this release, see the NEWS file. What is this? Twisted is an event-based framework for internet applications, su

Twisted Matrix Labs 4.9k Jan 08, 2023
Apple Store Stock Notifier monitors the availability of selected Apple devices in selected Apple stores, and sends you a notification when devices are available!

Apple Store Stock Notifier This software will immediately send you a notification via Telegram when one of your coveted Apple Devices is available in

Floris-Jan Willemsen 25 Dec 05, 2022
Converts from PC formatted MAC addresses (hardware addresses) to Cisco format or vice-versa

MAC-Converter Converts from PC formatted MAC addresses (hardware addresses) to Cisco format or vice-versa Stores the results to a file in the same dir

Stew Alexander 0 Dec 24, 2022
BibleNotifyDesktop - Desktop version of Bible Notify

Bible Notify Desktop This is the repository for the Desktop version of the daily

Bible Notify 5 Nov 16, 2022
Tool to transfer credential files from Firefox to your local machine to decrypt offline.

Firefox-Dumper Firefox Dumper identifies the current user's Firefox profile directory and exfiltrates the credential files to the attacker's FTP serve

Joe Helle 22 Sep 10, 2022
A great python/java dynamic DNS service for NameSilo, with log, email reminder...

English NameSilo DDNS is a DDNS service for NameSilo domain names for home broadband , it can automatically detect IP changes in home broadband

云牧青 77 Dec 28, 2022
Base on browser-time to get har from network, and use python to analyze the data .

base on browser-time to get har from network, and use python to analyze the data

1 Dec 20, 2021
The can package provides controller area network support for Python developers

python-can The Controller Area Network is a bus standard designed to allow microcontrollers and devices to communicate with each other. It has priorit

Brian Thorne 904 Dec 29, 2022