<commit_before>from plex_metadata.core.helpers import try_convert

import logging
import re
import urlparse

DEFAULT_MEDIA = ['movie', 'show', 'season', 'episode']

log = logging.getLogger(__name__)


class Agent(object):
    def __init__(self, media, service, regex=None, children=None):
        self.media = media
        self.service = service

        self.regex = regex
        self.children = children

    #
    # Compile
    #

    @classmethod
    def compile(cls, entry, media=None):
        # Construct `Agent`
        return cls(
            media=cls.get_media(entry, media),
            service=entry.get('service'),

            # Compile regular expression
            regex=cls.compile_pattern(entry.get('pattern')),

            # Compile children
            children=[
                cls.compile(child, media)
                for child in (entry.get('children') or [])
            ]
        )

    @staticmethod
    def compile_pattern(pattern):
        if pattern is None:
            return None

        try:
            return re.compile(pattern, re.IGNORECASE)
        except Exception, ex:
            log.warn('Unable to compile regular expression: %r - %s', pattern, ex, exc_info=True)

        return None<commit_msg>Fixed Python 3.x compatibility issue in `Agent.compile_pattern()`<commit_after>from plex_metadata.core.helpers import try_convert

import logging
import re
import urlparse

DEFAULT_MEDIA = ['movie', 'show', 'season', 'episode']

log = logging.getLogger(__name__)


class Agent(object):
    def __init__(self, media, service, regex=None, children=None):
        self.media = media
        self.service = service

        self.regex = regex
        self.children = children

    #
    # Compile
    #

    @classmethod
    def compile(cls, entry, media=None):
        # Construct `Agent`
        return cls(
            media=cls.get_media(entry, media),
            service=entry.get('service'),

            # Compile regular expression
            regex=cls.compile_pattern(entry.get('pattern')),

            # Compile children
            children=[
                cls.compile(child, media)
                for child in (entry.get('children') or [])
            ]
        )

    @staticmethod
    def compile_pattern(pattern):
        if pattern is None:
            return None

        try:
            return re.compile(pattern, re.IGNORECASE)
        except Exception as ex:
            log.warn('Unable to compile regular expression: %r - %s', pattern, ex, exc_info=True)

        return None
{'fraction_non_alphanumeric': 0.1000784929356358, 'fraction_numerical': 0.0003924646781789639, 'mean_word_length': 2.1085365853658535, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7222758', 'n_tokens_mistral': 707, 'n_tokens_neox': 680, 'n_words': 182}