<commit_before>import math
import operator
import collections

from django.core import urlresolvers
import pyes

from billy.conf import settings
from billy.utils import parse_param_dt

from .base import (db, Document, RelatedDocument, RelatedDocuments,
                   ListManager, AttrManager, take)
from .metadata import Metadata
from .utils import CachedAttribute, mongoid_2_url

elasticsearch = pyes.ES(settings.ELASTICSEARCH_HOST,
                        settings.ELASTICSEARCH_TIMEOUT)


class Sponsor(dict):
    legislator = RelatedDocument('Legislator', instance_key='leg_id')


class SponsorsManager(AttrManager):

    def __iter__(self):
        '''Lazily fetch all legislator objects from the db.
        '''
        sponsors = self.bill['sponsors']
        try:
            legislators = self._legislators
        except AttributeError:
            ids = filter(None, map(operator.itemgetter('leg_id'), sponsors))
            legislators = db.legislators.find({'_id': {'$in': ids}})
            legislators = dict((obj['_id'], obj) for obj in legislators)
            self._legislators = legislators
        for sponsor in sponsors:
            leg_id = sponsor['leg_id']
            if leg_id is not None and leg_id in legislators:
                legislator = legislators[sponsor['leg_id']]
                legislator.update(sponsor)
                yield legislator
            else:
                yield sponsor

    def primary_sponsors(self):
        'Return the primary sponsors on the bill.'
        for sponsor in self:
            if sponsor['type'] == 'primary':
                yield sponsor

    def first_primary(self):
        try:
            return next(self.primary_sponsors())
        except StopIteration:
            return

    def excluding_first_primary(self):
        first = self.first_primary()
        sponsors = list(self)
        if '_id' in first:
            first_id = first['_id']
            for sp in sponsors:
                print sp['_id']
                if first_id == sp['_id']:
                    sponsors.remove(sp)
                    break
        else:
            return sponsors
        return sponsors

    def first_fifteen(self):
        'views.bill'
        return take(15, self)<commit_msg>add first method to sponsor manager<commit_after>import math
import operator
import collections

from django.core import urlresolvers
import pyes

from billy.conf import settings
from billy.utils import parse_param_dt

from .base import (db, Document, RelatedDocument, RelatedDocuments,
                   ListManager, AttrManager, take)
from .metadata import Metadata
from .utils import CachedAttribute, mongoid_2_url

elasticsearch = pyes.ES(settings.ELASTICSEARCH_HOST,
                        settings.ELASTICSEARCH_TIMEOUT)


class Sponsor(dict):
    legislator = RelatedDocument('Legislator', instance_key='leg_id')


class SponsorsManager(AttrManager):

    def __iter__(self):
        '''Lazily fetch all legislator objects from the db.
        '''
        sponsors = self.bill['sponsors']
        try:
            legislators = self._legislators
        except AttributeError:
            ids = filter(None, map(operator.itemgetter('leg_id'), sponsors))
            legislators = db.legislators.find({'_id': {'$in': ids}})
            legislators = dict((obj['_id'], obj) for obj in legislators)
            self._legislators = legislators
        for sponsor in sponsors:
            leg_id = sponsor['leg_id']
            if leg_id is not None and leg_id in legislators:
                legislator = legislators[sponsor['leg_id']]
                legislator.update(sponsor)
                yield legislator
            else:
                yield sponsor

    def primary_sponsors(self):
        'Return the primary sponsors on the bill.'
        for sponsor in self:
            if sponsor['type'] == 'primary':
                yield sponsor

    def first_primary(self):
        try:
            return next(self.primary_sponsors())
        except StopIteration:
            return

    def first(self):
        return next(self)

    def excluding_first_primary(self):
        first = self.first_primary()
        sponsors = list(self)
        if '_id' in first:
            first_id = first['_id']
            for sp in sponsors:
                print sp['_id']
                if first_id == sp['_id']:
                    sponsors.remove(sp)
                    break
        else:
            return sponsors
        return sponsors

    def first_fifteen(self):
        'views.bill'
        return take(15, self)
{'fraction_non_alphanumeric': 0.08253484320557491, 'fraction_numerical': 0.0013066202090592336, 'mean_word_length': 2.0599600266489007, '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': '2602504', 'n_tokens_mistral': 1274, 'n_tokens_neox': 1194, 'n_words': 340}