<commit_before>import os
import re
import boto.ec2

__author__ = '<NAME> <<EMAIL>>'
__version__ = '0.0.4'
__license__ = 'BSD'
__all__ = ('credentials', 'instances')


class credentials(object):
    """
    Simple credentials singleton that holds our fun AWS info
    and masquerades as a dict
    """
    ACCESS_KEY_ID = None
    SECRET_ACCESS_KEY = None
    REGION_NAME = 'us-east-1'

    def keys(self):
        return ['aws_access_key_id', 'aws_secret_access_key', 'region_name']

    def __getitem__(self, item):
        item = item.upper()
        return os.environ.get(item) or getattr(self, item, None) or getattr(self, item[4:])

    @classmethod
    def from_file(cls, filename):
        """
        Load ACCESS_KEY_ID and SECRET_ACCESS_KEY from csv
        generated by Amazon's IAM.

        >>> ec2.credentials.from_file('credentials.csv')
        """
        import csv
        with open(filename, 'r') as f:
            reader = csv.DictReader(f)
            row = reader.next()  # Only one row in the file
        try:
            cls.ACCESS_KEY_ID = row['Access Key Id']
            cls.SECRET_ACCESS_KEY = row['Secret Access Key']
        except KeyError:
            raise IOError('Invalid credentials format')


class instances(object):
    """Singleten to stem off queries for instances"""

    @classmethod
    def all(cls):
        """
        Grab all AWS instances and cache them for future filters

        >>> ec2.instances.all()
        [ ... ]
        """
        if not hasattr(cls, '_instances'):
            conn = boto.ec2.connect_to_region(**credentials())
            # Ugh
            cls._instances = [i for r in conn.get_all_instances() for i in r.instances]
        return cls._instances

    @classmethod
    def filter(cls, **kwargs):
        """
        The meat. Filter instances using Django model style syntax.

        All kwargs are translated into attributes on instance objects.
        If the attribute is not found, it looks for a similar key
        in the tags.

        There are a couple comparisons to check against as well:
            exact: check strict equality
            iexact: case insensitive exact
            like: check against regular expression<commit_msg>Separate _connect() function for testing<commit_after>import os
import re
import boto.ec2

__author__ = '<NAME> <<EMAIL>>'
__version__ = '0.0.4'
__license__ = 'BSD'
__all__ = ('credentials', 'instances')


class credentials(object):
    """
    Simple credentials singleton that holds our fun AWS info
    and masquerades as a dict
    """
    ACCESS_KEY_ID = None
    SECRET_ACCESS_KEY = None
    REGION_NAME = 'us-east-1'

    def keys(self):
        return ['aws_access_key_id', 'aws_secret_access_key', 'region_name']

    def __getitem__(self, item):
        item = item.upper()
        return os.environ.get(item) or getattr(self, item, None) or getattr(self, item[4:])

    @classmethod
    def from_file(cls, filename):
        """
        Load ACCESS_KEY_ID and SECRET_ACCESS_KEY from csv
        generated by Amazon's IAM.

        >>> ec2.credentials.from_file('credentials.csv')
        """
        import csv
        with open(filename, 'r') as f:
            reader = csv.DictReader(f)
            row = reader.next()  # Only one row in the file
        try:
            cls.ACCESS_KEY_ID = row['Access Key Id']
            cls.SECRET_ACCESS_KEY = row['Secret Access Key']
        except KeyError:
            raise IOError('Invalid credentials format')


class instances(object):
    """Singleten to stem off queries for instances"""

    @classmethod
    def _connect(cls):
        return boto.ec2.connect_to_region(**credentials())

    @classmethod
    def all(cls):
        """
        Grab all AWS instances and cache them for future filters

        >>> ec2.instances.all()
        [ ... ]
        """
        if not hasattr(cls, '_instances'):
            conn = cls._connect()
            # Ugh
            cls._instances = [i for r in conn.get_all_instances() for i in r.instances]
        return cls._instances

    @classmethod
    def filter(cls, **kwargs):
        """
        The meat. Filter instances using Django model style syntax.

        All kwargs are translated into attributes on instance objects.
        If the attribute is not found, it looks for a similar key
        in the tags.

        There are a couple comparisons to check against as well:
            exact: check strict equality
            iexact: case insensitive exact
            like: check against regular expression
{'fraction_non_alphanumeric': 0.09472527472527473, 'fraction_numerical': 0.003956043956043956, 'mean_word_length': 2.2978260869565217, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, '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': '4160963', 'n_tokens_mistral': 1322, 'n_tokens_neox': 1284, 'n_words': 422}