<commit_before># Copyright (C) 2014 <NAME>
# https://github.com/yuichi1004/tiny-pyj
import re
import json
import random
import urllib2

class RpcMethod(object):
    def __init__(self, client, method, service):
        self.service = service 
        self.method = method
        self.client = client
    def __call__(self, *args, **kwargs):
        return self.client.request(self.method, self.service, args, kwargs)

class RpcError(Exception): 
    def __init__(self, msg):
        super(RpcError, self).__init__(msg)

class RpcValidationError(RpcError): 
    def __init__(self, msg):
        super(RpcValidationError, self).__init__(msg)

class Client(object):
    def __init__(self, smd, host, username = None, password = <PASSWORD>):
        self.smd = smd
        self.host = host
        self.username = username
        self.password = password
        self.services = smd['services']
        for s in self.services:
            method_name = re.sub('([A-Z])', r'_\1', s).lower()
            setattr(self, method_name, RpcMethod(self, s, self.services[s]))
        
        self.url_opener = urllib2.build_opener()
        if username is not None and password is not None:
            password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
            password_mgr.add_password(None, self.host, username, password)
            auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr)
            self.url_opener = urllib2.build_opener(auth_handler)
    
    def validate_param(self, name, value, type):
        if (type == 'integer'):
            if not isinstance(value, int):
                raise RpcValidationError('{} needs to be integer'.format(name))
        if (type == 'string'):
            if not isinstance(value, str):
                raise RpcValidationError('{} needs to be string'.format(name))
        if (type == 'boolean'):
            if not isinstance(value, bool):
                raise RpcValidationError('{} needs to be boolean'.format(name))

    def request(self, method, service, args, kwargs):
        unchecked_args = kwargs
        checked_args = {}
        parameters = service['parameters']
        for i in range(0, len(args)):
            name = parameters[i]['name']
            unchecked_args[name] = args[i]
        for p in parameters:
            name = p['name']
            if name in unchecked_args:
                self.validate_param(name, unchecked_args[name], p['type'])
                checked_args[name] = unchecked_args[name]
                del unchecked_args[name]
            else:
                if not p['optional']:
                    raise RpcError('arg {} is required'.format(name))
        if len(unchecked_args) > 0:
            RpcError('Unknown args: ' + str(unchecked_args))<commit_msg>Minor bug fix<commit_after># Copyright (C) 2014 <NAME>
# https://github.com/yuichi1004/tiny-pyj
import re
import json
import random
import urllib2

class RpcMethod(object):
    def __init__(self, client, method, service):
        self.service = service 
        self.method = method
        self.client = client
    def __call__(self, *args, **kwargs):
        return self.client.request(self.method, self.service, args, kwargs)

class RpcError(Exception): 
    def __init__(self, msg):
        super(RpcError, self).__init__(msg)

class RpcValidationError(RpcError): 
    def __init__(self, msg):
        super(RpcValidationError, self).__init__(msg)

class Client(object):
    def __init__(self, smd, host, username = None, password = <PASSWORD>):
        self.smd = smd
        self.host = host
        self.username = username
        self.password = password
        self.services = smd['services']
        for s in self.services:
            method_name = re.sub('([A-Z])', r'_\1', s).lower()
            setattr(self, method_name, RpcMethod(self, s, self.services[s]))
        
        self.url_opener = urllib2.build_opener()
        if username is not None and password is not None:
            password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
            password_mgr.add_password(None, self.host, username, password)
            auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr)
            self.url_opener = urllib2.build_opener(auth_handler)
    
    def validate_param(self, name, value, type):
        if (type == 'integer'):
            if not isinstance(value, int):
                raise RpcValidationError('{} needs to be integer'.format(name))
        if (type == 'string'):
            if not isinstance(value, str):
                raise RpcValidationError('{} needs to be string'.format(name))
        if (type == 'boolean'):
            if not isinstance(value, bool):
                raise RpcValidationError('{} needs to be boolean'.format(name))

    def request(self, method, service, args, kwargs):
        unchecked_args = kwargs
        checked_args = {}
        parameters = service['parameters']
        for i in range(0, len(args)):
            name = parameters[i]['name']
            unchecked_args[name] = args[i]
        for p in parameters:
            name = p['name']
            if name in unchecked_args:
                self.validate_param(name, unchecked_args[name], p['type'])
                checked_args[name] = unchecked_args[name]
                del unchecked_args[name]
            else:
                if not p['optional']:
                    raise RpcError('arg {} is required'.format(name))
        if len(unchecked_args) > 0:
            raise RpcError('Unknown args: ' + str(unchecked_args))
{'fraction_non_alphanumeric': 0.11292073832790445, 'fraction_numerical': 0.00579080709373869, 'mean_word_length': 2.3824969400244798, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2579706', 'n_tokens_mistral': 1609, 'n_tokens_neox': 1586, 'n_words': 402}