Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trafaret object to pass value as is #29

Closed
ant5 opened this issue Feb 16, 2018 · 6 comments
Closed

Trafaret object to pass value as is #29

ant5 opened this issue Feb 16, 2018 · 6 comments

Comments

@ant5
Copy link

ant5 commented Feb 16, 2018

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()

@hellysmile
Copy link
Contributor

can You try trafaret.Type(object) ?

@Deepwalker
Copy link
Owner

try trafaret.Any

@Deepwalker
Copy link
Owner

We probably have not the best docs, so will not close this now and will treat it as doc issue.

@ant5
Copy link
Author

ant5 commented Feb 16, 2018

I've tried trafaret.Any(). But it seems no luck:

import trafaret
tft = trafaret.Any()
tft.check_and_return('aaa')
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'Any' object has no attribute 'check_and_return'

@Deepwalker
Copy link
Owner

check_and_return, check_value, transform is internal methods for trafarets. Use just direct call (value) or check(value). Trafarets are callables.

@ant5
Copy link
Author

ant5 commented Feb 16, 2018

Oh, I see. Thanks all. Also thank you for point me that trafaret is callable.
Both solution works:
import trafaret
tft = trafaret.Any()
tft('aaa')
'aaa'
tft2 = trafaret.Type(object)

tft2('aaa')
'aaa'

I prefer trafaret.Any() for readability.

Thank you again. I enjoy to produce really beautifull code with this library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants