def test_dict_hook(): function_mock = Mock(return_value=None) hook = Hook("b", handler=function_mock) assert Schema({ hook: str, Optional("b"): object }).validate({"b": "bye"}) == { "b": "bye" } function_mock.assert_called_once() assert Schema({ hook: int, Optional("b"): object }).validate({"b": "bye"}) == { "b": "bye" } function_mock.assert_called_once() assert Schema({ hook: str, "b": object }).validate({"b": "bye"}) == { "b": "bye" } assert function_mock.call_count == 2
def test_dict_hook(): function_mock = Mock(return_value=None) hook = Hook('b', handler=function_mock) assert Schema({ hook: str, Optional('b'): object }).validate({'b': 'bye'}) == { 'b': 'bye' } function_mock.assert_called_once() assert Schema({ hook: int, Optional('b'): object }).validate({'b': 'bye'}) == { 'b': 'bye' } function_mock.assert_called_once() assert Schema({ hook: str, 'b': object }).validate({'b': 'bye'}) == { 'b': 'bye' } assert function_mock.call_count == 2
# def haskey(key): # return lambda d: isinstance(d, dict) and key in d def haskey(key): return Schema(lambda d: isinstance(d, dict) and key in d, name="has key {key}".format(key=key)) b = Schema(And( { "test": int, Optional(1): int, Optional(2): int, Optional(3): int, }, Or( haskey(1), And( haskey(2), haskey(3) ) ) )) b.validate({"test": 54, 1: 4, 2: 5, 3: 6}) from schema import Hook def _my_function(key, scope, error): print(key, scope, error) Hook("key", handler=_my_function)
if isinstance(data, str) and re.match(r'-?\d+(\.\d+)?%', data): data = float(data.strip('%')) / 100 else: assert isinstance( data, float), 'This field should be float or percent string' return data policy_schema = Schema({ Optional('weights', default=None): list, Optional('method', default=None): And(str, lambda s: s in ["per_channel", "per_tensor"]), Optional('init_sparsity', default=0): And(float, lambda s: s < 1.0 and s >= 0.0), Hook('target_sparsity', handler=_valid_prune_sparsity): object, Optional('target_sparsity', default=0.5): float, Optional("start_epoch", default=0): int, Hook('end_epoch', handler=_valid_prune_epoch): object, Optional('end_epoch', default=4): int }) ops_schema = Schema({ Optional('weight', default=None): { Optional('granularity', default=None): And(list,
Optional('model_conversion'): model_conversion_schema, Optional('tuning', default={ 'strategy': {'name': 'basic'}, 'accuracy_criterion': {'relative': 0.01, 'higher_is_better': True}, 'objective': 'performance', 'exit_policy': {'timeout': 0, 'max_trials': 100, 'performance_only': False}, 'random_seed': 1978, 'tensorboard': False, 'workspace': {'path': default_workspace}}): { Optional('strategy', default={'name': 'basic'}): { 'name': And(str, lambda s: s in STRATEGIES), Optional('accuracy_weight', default=1.0): float, Optional('latency_weight', default=1.0): float } , Hook('accuracy_criterion', handler=_valid_accuracy_field): object, Optional('accuracy_criterion', default={'relative': 0.01}): { Optional('relative'): And(Or(str, float), Use(percent_to_float)), Optional('absolute'): And(Or(str, int, float), Use(percent_to_float)), Optional('higher_is_better', default=True): bool, }, Optional('objective', default='performance'): And(str, lambda s: s in OBJECTIVES), Optional('exit_policy', default={'timeout': 0, 'max_trials': 100, 'performance_only': False}): { Optional('timeout', default=0): int, Optional('max_trials', default=100): int, Optional('performance_only', default=False): bool, }, Optional('random_seed', default=1978): int, Optional('tensorboard', default=False): And(bool, lambda s: s in [True, False]),
Optional('Resize'): { 'size': And(int, lambda s: s > 0) }, Optional('CenterCrop'): { 'size': And(int, lambda s: s > 0) }, Optional('Reshape'): { 'shape': And(list, lambda s: all(isinstance(i, int) for i in s)), } }) dataloader_schema = Schema({ Optional('batch_size', default=1): And(int, lambda s: s > 0), Optional('dataset', default=None): { Hook('type', handler=_valid_type_field): object, Optional('type'): str, Optional('root'): str, Optional('content_folder'): str, Optional('style_folder'): str, }, Optional('transform', default=None): transform_schema }) schema = Schema({ 'framework': { Hook('name', handler=_valid_framework_field): object, 'name': And(str, lambda s: s in FRAMEWORKS), Optional('inputs', default=None): And(Or(str, list), Use(input_to_list)),
def main(): options = docopt(__doc__, version=VERSION) earlyoptionSchema = Schema({ Optional('--log'): Or(None, And( str, Use(str.upper), validateLogLevel, error="Log level not recognized") ), Optional('--quiet', default=False): bool, Optional('--debug'): bool, Optional('--url-only', default=False): bool, Optional('--batch', default=False): bool, Optional('--withNFO', default=False): bool, Optional('--withThumbnail', default=False): bool, Optional('--withName', default=False): bool, Optional('--withDescription', default=False): bool, Optional('--withTags', default=False): bool, Optional('--withPlaylist', default=False): bool, Optional('--withPublishAt', default=False): bool, Optional('--withPlatform', default=False): bool, Optional('--withCategory', default=False): bool, Optional('--withLanguage', default=False): bool, Optional('--withChannel', default=False): bool, # This allow to return all other options for further use: https://github.com/keleshev/schema#extra-keys object: object }) schema = Schema({ '--file': And(str, os.path.exists, validateVideo, error='file is not supported, please use mp4'), # Strict option checks - at the moment Schema needs to check Hook and Optional separately # Hook('--name', handler=_optionnalOrStrict): object, Hook('--description', handler=_optionnalOrStrict): object, Hook('--tags', handler=_optionnalOrStrict): object, Hook('--category', handler=_optionnalOrStrict): object, Hook('--language', handler=_optionnalOrStrict): object, Hook('--platform', handler=_optionnalOrStrict): object, Hook('--publishAt', handler=_optionnalOrStrict): object, Hook('--thumbnail', handler=_optionnalOrStrict): object, Hook('--channel', handler=_optionnalOrStrict): object, Hook('--playlist', handler=_optionnalOrStrict): object, # Validate checks # Optional('--name'): Or(None, And( str, lambda x: not x.isdigit(), error="The video name should be a string") ), Optional('--description'): Or(None, And( str, lambda x: not x.isdigit(), error="The video description should be a string") ), Optional('--tags'): Or(None, And( str, lambda x: not x.isdigit(), error="Tags should be a string") ), Optional('--category'): Or(None, And( str, validateCategory, error="Category not recognized, please see --help") ), Optional('--language'): Or(None, And( str, validateLanguage, error="Language not recognized, please see --help") ), Optional('--privacy'): Or(None, And( str, validatePrivacy, error="Please use recognized privacy between public, unlisted or private") ), Optional('--nfo'): Or(None, str), Optional('--platform'): Or(None, And(str, validatePlatform, error="Sorry, upload platform not supported")), Optional('--publishAt'): Or(None, And( str, validatePublish, error="DATE should be the form YYYY-MM-DDThh:mm:ss and has to be in the future") ), Optional('--peertubeAt'): Or(None, And( str, validatePublish, error="DATE should be the form YYYY-MM-DDThh:mm:ss and has to be in the future") ), Optional('--youtubeAt'): Or(None, And( str, validatePublish, error="DATE should be the form YYYY-MM-DDThh:mm:ss and has to be in the future") ), Optional('--cca'): bool, Optional('--disable-comments'): bool, Optional('--nsfw'): bool, Optional('--thumbnail'): Or(None, And( str, validateThumbnail, error='thumbnail is not supported, please use jpg/jpeg'), ), Optional('--channel'): Or(None, str), Optional('--channelCreate'): bool, Optional('--playlist'): Or(None, str), Optional('--playlistCreate'): bool, '--help': bool, '--version': bool, # This allow to return all other options for further use: https://github.com/keleshev/schema#extra-keys object: object }) # We need to validate early options first as withNFO and logs options should be prioritized try: options = earlyoptionSchema.validate(options) configureLogs(options) except SchemaError as e: logger.critical(e) exit(1) if options.get('--url-only') or options.get('--batch'): configureStdoutLogs() options = utils.parseNFO(options) # Once NFO are loaded, we need to revalidate strict options in case some were in NFO try: options = earlyoptionSchema.validate(options) except SchemaError as e: logger.critical(e) exit(1) if not options.get('--thumbnail'): options = utils.searchThumbnail(options) try: options = schema.validate(options) except SchemaError as e: logger.critical(e) exit(1) logger.debug("Python " + sys.version) logger.debug(options) if options.get('--platform') is None or "peertube" in options.get('--platform'): pt_upload.run(options) if options.get('--platform') is None or "youtube" in options.get('--platform'): yt_upload.run(options)