def test_kwargs_extra(self): trafaret = t.Dict(t.Key('foo', trafaret=t.Int()), allow_extra=['eggs']) trafaret.check({"foo": 1, "eggs": None}) trafaret.check({"foo": 1}) with self.assertRaises(t.DataError): trafaret.check({"foo": 2, "marmalade": 5})
import pathlib from typing import Any, Optional, List import trafaret from aiohttp import web from trafaret_config import commandline PATH = pathlib.Path(__file__).parent.parent settings_file = os.environ.get('SETTINGS_FILE', 'dev.yml') DEFAULT_CONFIG_PATH = PATH / 'config' / settings_file CONFIG_TRAFARET = trafaret.Dict({ trafaret.Key('app'): trafaret.Dict({ 'host': trafaret.String(), 'port': trafaret.Int(), }), trafaret.Key('redis'): trafaret.Dict({ 'host': trafaret.String(), 'port': trafaret.Int(), 'db': trafaret.Int(), }), trafaret.Key('rabbitmq'): trafaret.Dict({ 'host': trafaret.String(), 'port': trafaret.Int(), 'queue': trafaret.String(), 'exchange_name': trafaret.String(), 'exchange_type': trafaret.String(), 'routing_key': trafaret.String(),
class ImageFilter: ''' Methods __init__(self, image = [], upper_HSV = [], lower_HSV = []) Initialize the filter. Required inputs are a color image and an initial upper and lower range for the filter. find_shapes(self) Method to adjust the filter criteria until a masked image can be retuned ''' @t.guard(image=t.Any(), upper_HSV=t.List(t.Int(gte=0)), lower_HSV=t.List(t.Int(gte=0))) def __init__(self, image=[], upper_HSV=[], lower_HSV=[]): ''' Initializer. Parameters: ---------- image : list A list representing a color image of shape (image_size, image_size, 3) upper_HSV : list A list of the upper limits of the initial HSV filter lower_HSV: list A list of the lower limits of the initial HSV filter Returns: ---------- None. Initializes and saves the filter object attributes. ''' # Build Filter self.image = image self.upper_HSV = np.array(upper_HSV) self.lower_HSV = np.array(lower_HSV) self.contour_threshold_upper = 10 # Maximum number of contours self.contour_threshold_lower = 1 # Minimum number of contours self.min_shape_size = 150 # Minimum contour size to be counted self.image_size = 299 # Image size self.shape_stepsize = 200 # Step size to reduce minimum contour # size, if the filter can't find minimum # Option to blur and sharpen the image before masking self.sharpen = True # cv2 blurring methods, as a string. Options are: # blur, GaussianBlur, medianBlur, bilateralFilter or none self.filter_type = 'bilateralFilter' ### ERROR CHECKING ### # Verify upper bounds are greater than lower bounds if (upper_HSV[0] <= lower_HSV[0] or upper_HSV[1] <= lower_HSV[1] or upper_HSV[2] <= lower_HSV[2]): raise ValueError('Each value in HSV Upper range muste be ' 'greater than values in Lower HSV') #OpenCV uses H: 0 - 180, S: 0 - 255, V: 0 - 255 if (upper_HSV[0] > 180 or lower_HSV[0] > 180): raise ValueError('Hue must be less than 180') if (upper_HSV[1] > 255 or lower_HSV[1] > 255): raise ValueError('Saturation must be less than 255') if (upper_HSV[2] > 255 or lower_HSV[2] > 255): raise ValueError('Value must be less than 255') def find_shapes(self): ''' Sharpens the image (if self.sharpen is true), resizes the image to specified size and applies the tune_sat function to see if enough contours of a minimum size can be found by adjusting the filter boundaries (see filter_functions). If not, the minimum contour size is stepped down, and the tune_sat function tries again. Once enough contours are found, the masked image is converted back to color. Method returns the maked image in color, the list of contours found by cv2.findContours, and the upper and lower filter boundaries found. ''' contour_counter = 0 image = self.image upper_HSV = self.upper_HSV lower_HSV = self.lower_HSV contour_threshold_upper = self.contour_threshold_upper contour_threshold_lower = self.contour_threshold_lower min_shape_size = self.min_shape_size image_size = self.image_size sharpen = self.sharpen shape_stepsize = self.shape_stepsize image = cv2.resize(image, (image_size, image_size)) image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) if sharpen == True: image = sharpen_image(image, method=self.filter_type) init_sat_upper = upper_HSV[1] init_sat_lower = lower_HSV[1] while contour_counter <= contour_threshold_lower or contour_counter >= contour_threshold_upper: processed_image, contours, upper_HSV, lower_HSV = tune_sat( image, upper_HSV, lower_HSV, min_shape_size, contour_threshold_upper, contour_threshold_lower) contour_counter = count_contours(contours, min_shape_size) min_shape_size = min_shape_size - shape_stepsize if contour_counter >= contour_threshold_lower and contour_counter <= contour_threshold_upper: break upper_HSV[1] = init_sat_upper lower_HSV[1] = init_sat_lower if min_shape_size < 50: print('Contours Found are not in range') break image = cv2.cvtColor(processed_image, cv2.COLOR_HSV2BGR) upper_HSV = np.ndarray.tolist(upper_HSV) lower_HSV = np.ndarray.tolist(lower_HSV) return (image, contours, upper_HSV, lower_HSV)
'forecast_timescale': trafaret.String(), 'forecast_absolute_rate': trafaret.String(), 'forecast_collapse_expand': trafaret.String(), 'forecast_active_tab': trafaret.String(), 'forecast_tab': trafaret.String(), 'decomp_value_volume_price': trafaret.String(), 'd_summary_table_collapsed_expanded': trafaret.String(), 'd_details_table_collapsed_expanded': trafaret.String(), 'd_details_selected_factor': trafaret.Null() }) }), trafaret.Key('scenarios'): trafaret.Dict({ trafaret.Key('scenarios'): trafaret.Dict({ 'work_list_show_limit': trafaret.Int(), 'min_search_input_length': trafaret.Int(), 'sorting_field': trafaret.String(), 'sorting_order': trafaret.Bool(), 'dateformat': trafaret.String() }) }) }) TRAFARET_LAN = trafaret.Dict({ trafaret.Key('landing'): trafaret.Dict({ trafaret.Key('top_menu'): trafaret.Dict({ 'landing': trafaret.String(), 'dashboard': trafaret.String(),
def __init__(self, *, gte=None, lte=None, gt=None, lt=None, **kwargs): super(Integer, self).__init__(**kwargs) self._trafaret = t.Int(gte=gte, lte=lte, gt=gt, lt=lt) if self.allow_none: self._trafaret |= t.Null()
"""Module with config schema to validate it""" import trafaret as tr CONFIG_SCHEMA = tr.Dict({ tr.Key('general'): tr.Dict({ 'project_name': tr.String(), 'config': tr.String(), 'processes': tr.Int(), }), tr.Key('paths'): tr.Dict({ 'project_folder': tr.String(), 'fasta_input_folder': tr.String(), 'extract_output': tr.String(), 'prodigal_output': tr.String(), 'prodigal_nuc': tr.String(), 'prodigal_pro': tr.String(), 'hmmsearch_output': tr.String(), 'bcg_dir': tr.String(), 'align_mafft_output': tr.String(), 'align_output': tr.String(), 'align_input_merge': tr.String(), 'align_input_parse': tr.String(), 'align_filtering_output': tr.String(), 'align_concatenating_output': tr.String(), 'align_align_output': tr.String(), 'align_trees_output': tr.String(), }), tr.Key('prefixes'):
import trafaret as T from simcore_sdk.config import db, s3 from . import rest_config app_schema = T.Dict({ T.Key("host", default="0.0.0.0"): T.IP, "port": T.Int(), "log_level": T.Enum("DEBUG", "WARNING", "INFO", "ERROR", "CRITICAL", "FATAL", "NOTSET"), "testing": T.Bool(), "python2": T.String(), T.Key("max_workers", default=8, optional=True): T.Int(), T.Key("test_datcore", optional=True): T.Dict({ "token_key": T.String(), "token_secret": T.String() }), T.Key("disable_services", default=[], optional=True): T.List(T.String()) }) schema = T.Dict({ "version": T.String(), T.Key("main"): app_schema,
import trafaret as t from datetime import datetime, date DATE_FORMAT = '%Y-%m-%d' class Date(t.Trafaret): def check_and_return(self, value): if not isinstance(value, date): self._failure("value is not a data", value=value) return value.strftime(DATE_FORMAT) BOOK_TRAFARET = t.Dict({ t.Key('id', optional=True): t.Int(), 'name': t.String(), 'author': t.String(), 'rating': t.Int(), 'date_reading': t.Or(t.String() >> (lambda value: datetime.strptime(value, DATE_FORMAT)), Date()), }) BOOKS_TRAFARET = t.List(BOOK_TRAFARET)
# utils.py import trafaret as T TRAFARET = T.Dict({ T.Key('host'): T.IP, T.Key('port'): T.Int(), }) import boto3 # https://alexwlchan.net/2019/07/listing-s3-keys/ # Copyright (c) 2012-2019 Alex Chan # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), # to deal in the Software without restriction, including without limitation # the rights to use, copy, modify, merge, publish, distribute, sublicense, # and/or sell copies of the Software, and to permit persons to whom the Software # is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE.
# from sqlalchemy.orm import sessionmaker # DBSession = sessionmaker() # class ProxyPair(namedtuple('ProxyPair', models.ProxyTbl.__table__.columns.keys())): # __slots__ = () # # def __str__(self): # return str(self._asdict()) TRAFARET = trafaret.Dict({ trafaret.Key('mysql'): trafaret.Dict({ 'database': trafaret.String(), 'user': trafaret.String(), 'password': trafaret.String(), 'host': trafaret.String(), 'port': trafaret.Int(), 'minsize': trafaret.Int(), 'maxsize': trafaret.Int(), }), trafaret.Key('host'): trafaret.IP, trafaret.Key('port'): trafaret.Int(), trafaret.Key('debug'): trafaret.Bool(), trafaret.Key('period'): trafaret.Int() })
CONFIG_TRAFARET = trafaret.Dict({ "token": trafaret.String(), trafaret.Key('proxy', optional=True): trafaret.Dict({ 'url': trafaret.String(), 'username': trafaret.String(), 'password': trafaret.String() }), trafaret.Key('webhook'): trafaret.Dict({ 'host': trafaret.String(), 'path': trafaret.String(), 'app_host': trafaret.String(), 'app_port': trafaret.Int(), }), }) def get_config() -> Any: argv = ['-c', DEFAULT_CONFIG_PATH.as_posix()] ap = argparse.ArgumentParser() commandline.standard_argparse_options( ap, default_config=DEFAULT_CONFIG_PATH, ) config = commandline.config_from_options(ap.parse_args(argv), CONFIG_TRAFARET)
def test_binary_template(self): # Simple values template_1 = AsyncTemplate(t.Int()) template_2 = AsyncTemplate(t.String()) contract = AsyncContract(template_1 | template_2) result = self.loop.run_until_complete(contract(42)) self.assertEqual(result, 42) result = self.loop.run_until_complete(contract('test')) self.assertEqual(result, 'test') with self.assertRaises(ValueError): result = self.loop.run_until_complete(contract(['list'])) # Simple list values template_1 = AsyncTemplate(t.Int()) template_2 = AsyncTemplate(t.String()) contract = AsyncContract([template_1 | template_2]) result = self.loop.run_until_complete(contract([1, 2, 3])) self.assertEqual(result, [1, 2, 3]) result = self.loop.run_until_complete(contract(['test_1', 'test_2'])) self.assertEqual(result, ['test_1', 'test_2']) result = self.loop.run_until_complete(contract([1, 'test', 3])) self.assertEqual(result, [1, 'test', 3]) # Combinated list values template_1 = AsyncTemplate(t.Int()) template_2 = AsyncTemplate(t.String()) contract = AsyncContract( [template_1 | template_2, template_1, template_2]) result = self.loop.run_until_complete(contract([1, 1, 'test'])) self.assertEqual(result, [1, 1, 'test']) result = self.loop.run_until_complete(contract(['test', 1, 'test'])) self.assertEqual(result, ['test', 1, 'test']) # Dict values template = { 'key': AsyncTemplate(t.String()) | AsyncTemplate(t.Int()), 'value': AsyncTemplate(t.String()), } contract = AsyncContract(template) result = self.loop.run_until_complete( contract({ 'key': 'key', 'value': 'test' })) self.assertEqual(result, {'key': 'key', 'value': 'test'}) result = self.loop.run_until_complete( contract({ 'key': 12, 'value': 'test' })) self.assertEqual(result, {'key': 12, 'value': 'test'}) template_1 = { 'key': AsyncTemplate(t.String()), 'value': AsyncTemplate(t.String()), } template_2 = { 'id': AsyncTemplate(t.Int()), 'value': AsyncTemplate(t.String()), } contract_1 = AsyncContract(template_1) contract_2 = AsyncContract(template_2) contract = contract_1 | contract_2 result = self.loop.run_until_complete( contract({ 'key': 'key', 'value': 'test' })) self.assertEqual(result, {'key': 'key', 'value': 'test'}) result = self.loop.run_until_complete( contract({ 'id': 42, 'value': 'test' })) self.assertEqual(result, {'id': 42, 'value': 'test'})
def test_dataerror_and_catch(self): trafaret = t.Int() val = t.catch(trafaret, 'a') or 100 self.assertEqual(val, 100)
# !/usr/bin/env python # -*- coding: utf-8 -*- """Module with config schema to validate it""" import trafaret as tr CONFIG_SCHEMA = tr.Dict({ tr.Key('postgres'): tr.Dict({ 'database': tr.String(), 'user': tr.String(), 'password': tr.String(), 'host': tr.String(), 'port': tr.Int(), 'poolsize_min': tr.Int(), 'poolsize_max': tr.Int(), }), tr.Key('urls'): tr.Dict({ 'meta_url': tr.String(), 'meta_woeid_url': tr.String(), }), tr.Key('date_const'): tr.Dict({ 'str_date_meta': tr.String(), 'str_date_db': tr.String }) })
from .decimal import Decimal from .format import format_trafaret __VERSION__ = (0, 2, 1) check_number = t.OnError(t.Float() | Decimal(), 'Not a number') json_schema_type = ( t.Atom('null') & just(t.Null()) | t.Atom('boolean') & just(t.Bool()) | t.Atom('object') & just(t.Type(dict)) | t.Atom('array') & just(t.Type(list)) | t.Atom('number') & just(check_number) | t.Atom('integer') & just(t.Int()) | t.Atom('string') & just(t.String()) ) def multipleOf(multiplier): def check(value): if value % multiplier != 0: return t.DataError('%s is not devisible by %s' % (value, multiplier)) return value return check def uniq(lst): if len(set(lst)) < len(lst): return t.DataError('Array elements are not uniq')
import trafaret as t from trafaret.contrib.object_id import MongoId car = t.Dict({ t.Key('_id'): MongoId(), t.Key('manufacturer'): t.String(), t.Key('model'): t.String(), t.Key('year'): t.Int(), t.Key('color'): t.String(), t.Key('vin'): t.String(), })
from datetime import datetime import trafaret as t from tests import db ORDER_SCHEMA = t.Dict({ 'id': t.Int(), 'account_id': t.Int(), 'date_created': t.String() >> (lambda d: datetime.strptime(d, '%Y-%m-%d %H:%M:%S.%f')), 'date_edited': t.String() >> (lambda d: datetime.strptime(d, '%Y-%m-%d %H:%M:%S.%f')), 'products': t.List(t.Dict({ 'id': t.Int(), 'name': t.String(), 'price': t.Int(), })), }) async def test_empty_orders_for_new_user(client, create): user = await create(db.user()) response = await client.get(f'/users/{user.id}/orders') assert response.status == 200 body = await response.json() assert body['success'] is True assert body['data'] == []
import trafaret as T CONFIG_TRAFARET = T.Dict({ T.Key('mysql'): T.Dict({ 'db': T.String(), 'user': T.String(), 'password': T.String(), 'host': T.String(), 'port': T.Int(), 'minsize': T.Int(), 'maxsize': T.Int(), }), T.Key('host'): T.IP, T.Key('port'): T.Int(), })
import os import pathlib import trafaret as t import yaml logger = logging.getLogger(__name__) BASE_DIR = pathlib.Path(__file__).parent.parent DEFAULT_CONFIG = 'config.yml' CITIZENS_COLLECTION = 'requests_dump' config_template = t.Dict({ t.Key('mongo'): t.Dict({ t.Key('host'): t.Regexp(regexp=r'^\d+.\d+.\d+.\d+$'), t.Key('port'): t.Int(gte=0), t.Key('database'): t.String(), t.Key('max_pool_size'): t.Int(gte=0), }), t.Key('host'): t.Regexp(regexp=r'^\d+.\d+.\d+.\d+$'), t.Key('port'): t.Int(gte=0), t.Key('proxy-port'): t.Int(gte=0), }) def get_config(mode=DEFAULT_CONFIG): config_path = os.path.join(BASE_DIR, 'config', mode) with open(config_path, 'rt') as f: config = yaml.load(f, Loader=yaml.FullLoader) try:
from pathlib import Path from typing import Any import trafaret as T from aiohttp import web from trafaret_config import commandline PROJECT_PATH = Path(__file__).parent.parent DEFAULT_CONFIG_FILE = os.environ.get('CONFIG_FILE', 'dev.yml') DEFAULT_CONFIG_PATH = PROJECT_PATH / 'config' / DEFAULT_CONFIG_FILE TRAFARET = T.Dict({ T.Key('app'): T.Dict({ 'host': T.String(), 'port': T.Int(), 'workers': T.Int(), }), T.Key('postgres'): T.Dict({ 'user': T.String(), 'password': T.String(), 'database': T.String(), 'port': T.Int(), 'host': T.String(), }), T.Key('redis'): T.Dict({ 'port': T.Int(), 'host': T.String(), }),