<commit_before># -*- coding: utf-8 -*-

import functools
import re
import urllib2

import scio

# NOTE(jkoelker) I hate soap so much

WSDL = 'http://ws.eoddata.com/data.asmx?wsdl'
FIRST_CAP_RE = re.compile('(.)([A-Z][a-z]+)')
ALL_CAP_RE = re.compile('([a-z0-9])([A-Z])')


def convert_date(date):
    if not date:
        return date

    if not isinstance(date, basestring):
        try:
            return date.strftime('%Y%m%d')
        except:
            pass

    return date


def require_login(f):

    @functools.wraps(f)
    def wrapper(self, *args, **kwargs):
        if not self.token and not self.login():
            return

        return f(self, *args, **kwargs)

    return wrapper


def success(obj, method):
    result = method + 'Result'
    obj = getattr(obj, result)

    if not hasattr(obj, 'Message'):
        return False

    if 'success' in obj.Message.lower():
        return obj

    return False


def convert(value):
    if isinstance(value, basestring):
        v = value.lower()

        if v == 'true':
            return True

        elif v == 'false':
            return False

    return value


# NOTE(jkoelker) http://stackoverflow.com/a/1176023/101801
def decamelize(name):
    return ALL_CAP_RE.sub(r'\1_\2', FIRST_CAP_RE.sub(r'\1_\2', name)).lower()


def dictify(obj):
    return dict([(decamelize(k.strip('_')), convert(v))
                 for k, v in obj.__dict__.iteritems()
                 if k.startswith('_') and k.endswith('_')])


def list_to_dictify(objs, key):
    res = {}

    for obj in objs:
        obj = dictify(obj)
        res[obj[key]] = obj

    return res


class Client(object):
    def __init__(self, username, password):
        self.client = scio.Client(urllib2.urlopen(WSDL))
        self.username = username
        self.password = password
        self.token = None
        self.last_response = None

    def _get(self, method, **kwargs):
        func = getattr(self.client.service, method)
        self.last_response = func(**kwargs)
        return self.last_response

    def _result(self, method, processor=None, **kwargs):
        obj = self._get(method, Token=self.token, **kwargs)

        result = success(obj, method)
        if not result:
            return

        if processor:
            result = processor(result)

        return result

    def login(self):
        method = 'Login'
        obj = self._get(method, Username=self.username, Password=<PASSWORD>)

        result = success(obj, method)

        if not result:
            return

        self.token = result.Token
        return self.token

    @require_login
    def country_list(self):
        method = 'CountryList'
        processor = lambda obj: dict([(c.Code, c.Name)
                                      for c in obj.COUNTRIES.CountryBase])<commit_msg>raise insead of returning None<commit_after># -*- coding: utf-8 -*-

import functools
import re
import urllib2

import scio

# NOTE(jkoelker) I hate soap so much

WSDL = 'http://ws.eoddata.com/data.asmx?wsdl'
FIRST_CAP_RE = re.compile('(.)([A-Z][a-z]+)')
ALL_CAP_RE = re.compile('([a-z0-9])([A-Z])')


class Error(Exception):
    pass


class LoginError(Error):
    pass


def convert_date(date):
    if not date:
        return date

    if not isinstance(date, basestring):
        try:
            return date.strftime('%Y%m%d')
        except:
            pass

    return date


def require_login(f):

    @functools.wraps(f)
    def wrapper(self, *args, **kwargs):
        if not self.token:
            self.login()

        return f(self, *args, **kwargs)

    return wrapper


def success(obj, method):
    result = method + 'Result'
    obj = getattr(obj, result)

    if not hasattr(obj, 'Message'):
        return False

    if 'success' in obj.Message.lower():
        return obj

    return False


def convert(value):
    if isinstance(value, basestring):
        v = value.lower()

        if v == 'true':
            return True

        elif v == 'false':
            return False

    return value


# NOTE(jkoelker) http://stackoverflow.com/a/1176023/101801
def decamelize(name):
    return ALL_CAP_RE.sub(r'\1_\2', FIRST_CAP_RE.sub(r'\1_\2', name)).lower()


def dictify(obj):
    return dict([(decamelize(k.strip('_')), convert(v))
                 for k, v in obj.__dict__.iteritems()
                 if k.startswith('_') and k.endswith('_')])


def list_to_dictify(objs, key):
    res = {}

    for obj in objs:
        obj = dictify(obj)
        res[obj[key]] = obj

    return res


class Client(object):
    def __init__(self, username, password):
        self.client = scio.Client(urllib2.urlopen(WSDL))
        self.username = username
        self.password = password
        self.token = None
        self.last_response = None

    def _get(self, method, **kwargs):
        func = getattr(self.client.service, method)
        self.last_response = func(**kwargs)
        return self.last_response

    def _result(self, method, processor=None, **kwargs):
        obj = self._get(method, Token=self.token, **kwargs)

        result = success(obj, method)
        if not result:
            raise Error(str(self.last_response))

        if processor:
            result = processor(result)

        return result

    def login(self):
        method = 'Login'
        obj = self._get(method, Username=self.username, Password=self.password)

        result = success(obj, method)

        if not result:
            raise LoginError(str(self.last_response))

        self.token = result.Token
        return self.token

    @require_login
    def country_list(self):
        method = 'CountryList'
        processor = lambda obj: dict([(c.Code, c.Name)
                                      for c in obj.COUNTRIES.CountryBase])
{'fraction_non_alphanumeric': 0.12463165193274398, 'fraction_numerical': 0.007626971745536488, 'mean_word_length': 2.1966759002770084, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5952538', 'n_tokens_mistral': 1899, 'n_tokens_neox': 1796, 'n_words': 450}