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

"""Provides a ``RedisFactory`` to get a configured redis client from a
  settings dictionary, e.g.::

      >>> factory = RedisFactory()
      >>> client = factory({'redis.url': 'redis://localhost:6379'})

  And ``GetRedisClient`` which wraps the factory so it can be used as a
  Pyramid request method.
"""

__all__ = [
    'GetRedisClient',
    'RedisFactory',
]

import logging
logger = logging.getLogger(__name__)
import pyramid.exceptions
import redis

try:
    import urlparse
except ImportError:  # py3
    import urllib.parse as urlparse

from zope.component import getGlobalSiteManager
from zope.interface import Interface
from zope.interface import directlyProvides


class IRedisClientConfiguration(Interface):
    """Marker interface provided by RedisClientConfiguration"""


class RedisClientConfiguration(dict):
    """Parse the application settings into connection pool kwargs."""

    def __init__(self, **kwargs):
        self.parse_url = kwargs.get('parse_url', urlparse.urlparse)
        self.pool_cls = kwargs.get('pool_cls', redis.BlockingConnectionPool)

    def __call__(self, settings):
        """Unpack the settings. Parse the url into components and build
          a dict to return. As an alternative, you may also provide a
          unix_socket_path.
        """
        self.clear()  # make sure you can reconfigure the client
        db = settings.get('redis.db', 0)
        config = {'db': int(db)}

        if 'redis.url' in settings:
            # Unpack.
            url = settings['redis.url']

            # Parse into a config dict.
            o = self.parse_url(url)
            config.update({
                'host': o.hostname,
                'port': o.port,
            })
            if o.password:
                config['password'] = <PASSWORD>

            max_connections = settings.get('redis.max_connections', None)
            if max_connections is not None:
                config['max_connections'] = int(max_connections)
            config = {'connection_pool': self.pool_cls(**config)}
        elif 'redis.unix_socket_path' in settings:
            config['unix_socket_path'] = settings['redis.unix_socket_path']
        else:
            raise pyramid.exceptions.ConfigurationError(
                """To use redis with pyramid, redis.url or
                redis.unix_socket_path should be provided"""
            )
        self.update(config)


class RedisFactory(object):

    def __init__(self, **kwargs):
        self.get_registry = kwargs.get('get_registry', getGlobalSiteManager)
        self.config = kwargs.get('parse_config', RedisClientConfiguration())
        self.provides = kwargs.get('provides', directlyProvides)
        self.redis_cls = kwargs.get('redis_cls', redis.StrictRedis)
<commit_msg>Test for None not if key is set.<commit_after># -*- coding: utf-8 -*-

"""Provides a ``RedisFactory`` to get a configured redis client from a
  settings dictionary, e.g.::

      >>> factory = RedisFactory()
      >>> client = factory({'redis.url': 'redis://localhost:6379'})

  And ``GetRedisClient`` which wraps the factory so it can be used as a
  Pyramid request method.
"""

__all__ = [
    'GetRedisClient',
    'RedisFactory',
]

import logging
logger = logging.getLogger(__name__)
import pyramid.exceptions
import redis

try:
    import urlparse
except ImportError:  # py3
    import urllib.parse as urlparse

from zope.component import getGlobalSiteManager
from zope.interface import Interface
from zope.interface import directlyProvides


class IRedisClientConfiguration(Interface):
    """Marker interface provided by RedisClientConfiguration"""


class RedisClientConfiguration(dict):
    """Parse the application settings into connection pool kwargs."""

    def __init__(self, **kwargs):
        self.parse_url = kwargs.get('parse_url', urlparse.urlparse)
        self.pool_cls = kwargs.get('pool_cls', redis.BlockingConnectionPool)

    def __call__(self, settings):
        """Unpack the settings. Parse the url into components and build
          a dict to return. As an alternative, you may also provide a
          unix_socket_path.
        """
        self.clear()  # make sure you can reconfigure the client
        db = settings.get('redis.db', 0)
        config = {'db': int(db)}

        if settings['redis.url'] is not None:
            # Unpack.
            url = settings['redis.url']

            # Parse into a config dict.
            o = self.parse_url(url)
            config.update({
                'host': o.hostname,
                'port': o.port,
            })
            if o.password:
                config['password'] = <PASSWORD>

            max_connections = settings.get('redis.max_connections', None)
            if max_connections is not None:
                config['max_connections'] = int(max_connections)
            config = {'connection_pool': self.pool_cls(**config)}
        elif settings['redis.unix_socket_path'] is not None:
            config['unix_socket_path'] = settings['redis.unix_socket_path']
        else:
            raise pyramid.exceptions.ConfigurationError(
                """To use redis with pyramid, redis.url or
                redis.unix_socket_path should be provided"""
            )
        self.update(config)


class RedisFactory(object):

    def __init__(self, **kwargs):
        self.get_registry = kwargs.get('get_registry', getGlobalSiteManager)
        self.config = kwargs.get('parse_config', RedisClientConfiguration())
        self.provides = kwargs.get('provides', directlyProvides)
        self.redis_cls = kwargs.get('redis_cls', redis.StrictRedis)

{'fraction_non_alphanumeric': 0.10627763041556144, 'fraction_numerical': 0.002475685234305924, 'mean_word_length': 2.6513879922530665, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, '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': '6929655', 'n_tokens_mistral': 1620, 'n_tokens_neox': 1567, 'n_words': 431}