本文整理汇总了Python中pupa.scrape.VoteEvent类的典型用法代码示例。如果您正苦于以下问题:Python VoteEvent类的具体用法?Python VoteEvent怎么用?Python VoteEvent使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了VoteEvent类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_fix_bill_id
def test_fix_bill_id():
j = create_jurisdiction()
j.legislative_sessions.create(name='1900', identifier='1900')
org1 = ScrapeOrganization(name='House', classification='lower')
bill = ScrapeBill('HB 1', '1900', 'Test Bill ID',
classification='bill', chamber='lower')
oi = OrganizationImporter('jid')
oi.import_data([org1.as_dict()])
from pupa.settings import IMPORT_TRANSFORMERS
IMPORT_TRANSFORMERS['bill'] = {
'identifier': lambda x: re.sub(r'([A-Z]*)\s*0*([-\d]+)', r'\1 \2', x, 1)
}
bi = BillImporter('jid', oi, DumbMockImporter())
bi.import_data([bill.as_dict()])
ve = ScrapeVoteEvent(legislative_session='1900', motion_text='passage',
start_date='1900-04-02', classification='passage:bill',
result='fail', bill_chamber='lower', bill='HB1',
identifier='4',
bill_action='passage',
organization=org1._id)
VoteEventImporter('jid', DumbMockImporter(), oi, bi).import_data([
ve.as_dict(),
])
IMPORT_TRANSFORMERS['bill'] = {}
ve = VoteEvent.objects.get()
ve.bill.identifier == 'HB 1'
示例2: add_vote
def add_vote(self, bill, chamber, date, text, url):
votes = re.findall(r'Ayes,?[\s]?(\d+)[,;]\s+N(?:oes|ays),?[\s]?(\d+)', text)
yes, no = int(votes[0][0]), int(votes[0][1])
vtype = 'other'
for regex, type in motion_classifiers.items():
if re.match(regex, text):
vtype = type
break
v = VoteEvent(
chamber=chamber,
start_date=TIMEZONE.localize(date),
motion_text=text,
result='pass' if yes > no else 'fail',
classification=vtype,
bill=bill,
)
v.pupa_id = url.split('/')[-1]
v.set_count('yes', yes)
v.set_count('no', no)
# fetch the vote itself
if url:
v.add_source(url)
if 'av' in url:
self.add_house_votes(v, url)
elif 'sv' in url:
self.add_senate_votes(v, url)
return v
示例3: _get_votes
def _get_votes(self, date, actor, action, bill, url):
vre = r'(?P<leader>.*)(AYES|YEAS):\s+(?P<yeas>\d+)\s+(NOES|NAYS):\s+(?P<nays>\d+).*'
if 'YEAS' in action.upper() or 'AYES' in action.upper():
match = re.match(vre, action)
if match:
v = match.groupdict()
yes, no = int(v['yeas']), int(v['nays'])
vote = VoteEvent(
chamber=actor,
motion_text=v['leader'],
result='pass' if yes > no else 'fail',
classification='passage',
start_date=TIMEZONE.localize(date),
bill=bill,
)
vote.add_source(url)
yield vote
示例4: get_vote_event
def get_vote_event(self, bill, act, votes, result):
'''Make VoteEvent object from given Bill, action, votes and result.'''
organization = json.loads(act['organization_id'].lstrip('~'))
vote_event = VoteEvent(legislative_session=bill.legislative_session,
motion_text=act['description'],
organization=organization,
classification=None,
start_date=act['date'],
result=result,
bill=bill)
legistar_web, legistar_api = [src['url'] for src in bill.sources]
vote_event.add_source(legistar_web)
vote_event.add_source(legistar_api + '/histories')
for vote in votes:
raw_option = vote['VoteValueName'].lower()
if raw_option == 'suspended':
continue
clean_option = self.VOTE_OPTIONS.get(raw_option, raw_option)
vote_event.vote(clean_option, vote['VotePersonName'].strip())
return vote_event
示例5: test_vote_event_identifier_dedupe
def test_vote_event_identifier_dedupe():
j = Jurisdiction.objects.create(id='jid', division_id='did')
j.legislative_sessions.create(name='1900', identifier='1900')
vote_event = ScrapeVoteEvent(legislative_session='1900', start_date='2013',
classification='anything', result='passed',
motion_text='a vote on something',
identifier='Roll Call No. 1')
dmi = DumbMockImporter()
bi = BillImporter('jid', dmi, dmi)
_, what = VoteEventImporter('jid', dmi, dmi, bi).import_item(vote_event.as_dict())
assert what == 'insert'
assert VoteEvent.objects.count() == 1
# same exact vote event, no changes
_, what = VoteEventImporter('jid', dmi, dmi, bi).import_item(vote_event.as_dict())
assert what == 'noop'
assert VoteEvent.objects.count() == 1
# new info, update
vote_event.result = 'failed'
_, what = VoteEventImporter('jid', dmi, dmi, bi).import_item(vote_event.as_dict())
assert what == 'update'
assert VoteEvent.objects.count() == 1
# new bill, insert
vote_event.identifier = 'Roll Call 2'
_, what = VoteEventImporter('jid', dmi, dmi, bi).import_item(vote_event.as_dict())
assert what == 'insert'
assert VoteEvent.objects.count() == 2
示例6: test_vote_event_pupa_identifier_dedupe
def test_vote_event_pupa_identifier_dedupe():
j = create_jurisdiction()
j.legislative_sessions.create(name='1900', identifier='1900')
Organization.objects.create(id='org-id', name='Legislature',
classification='legislature',
jurisdiction=j)
vote_event = ScrapeVoteEvent(legislative_session='1900', start_date='2013',
classification='anything', result='passed',
motion_text='a vote on something',
identifier='Roll Call No. 1')
vote_event.pupa_id = 'foo'
dmi = DumbMockImporter()
oi = OrganizationImporter('jid')
bi = BillImporter('jid', dmi, oi)
_, what = VoteEventImporter('jid', dmi, oi, bi).import_item(vote_event.as_dict())
assert what == 'insert'
assert VoteEvent.objects.count() == 1
# same exact vote event, no changes
_, what = VoteEventImporter('jid', dmi, oi, bi).import_item(vote_event.as_dict())
assert what == 'noop'
assert VoteEvent.objects.count() == 1
# new info, update
vote_event.result = 'failed'
_, what = VoteEventImporter('jid', dmi, oi, bi).import_item(vote_event.as_dict())
assert what == 'update'
assert VoteEvent.objects.count() == 1
# new bill identifier, update
vote_event.identifier = 'First Roll Call'
_, what = VoteEventImporter('jid', dmi, oi, bi).import_item(vote_event.as_dict())
assert what == 'update'
assert VoteEvent.objects.count() == 1
# new pupa identifier, insert
vote_event.pupa_id = 'bar'
_, what = VoteEventImporter('jid', dmi, oi, bi).import_item(vote_event.as_dict())
assert what == 'insert'
assert VoteEvent.objects.count() == 2
示例7: test_full_vote_event
def test_full_vote_event():
j = Jurisdiction.objects.create(id='jid', division_id='did')
j.legislative_sessions.create(name='1900', identifier='1900')
sp1 = ScrapePerson('John Smith', primary_org='lower')
sp2 = ScrapePerson('Adam Smith', primary_org='lower')
org = ScrapeOrganization(name='House', classification='lower')
bill = ScrapeBill('HB 1', '1900', 'Axe & Tack Tax Act', from_organization=org._id)
vote_event = ScrapeVoteEvent(legislative_session='1900', motion_text='passage',
start_date='1900-04-01', classification='passage:bill',
result='pass', bill_chamber='lower', bill='HB 1',
organization=org._id)
vote_event.set_count('yes', 20)
vote_event.yes('John Smith')
vote_event.no('Adam Smith')
oi = OrganizationImporter('jid')
oi.import_data([org.as_dict()])
pi = PersonImporter('jid')
pi.import_data([sp1.as_dict(), sp2.as_dict()])
mi = MembershipImporter('jid', pi, oi, DumbMockImporter())
mi.import_data([sp1._related[0].as_dict(), sp2._related[0].as_dict()])
bi = BillImporter('jid', oi, pi)
bi.import_data([bill.as_dict()])
VoteEventImporter('jid', pi, oi, bi).import_data([vote_event.as_dict()])
assert VoteEvent.objects.count() == 1
ve = VoteEvent.objects.get()
assert ve.legislative_session == LegislativeSession.objects.get()
assert ve.motion_classification == ['passage:bill']
assert ve.bill == Bill.objects.get()
count = ve.counts.get()
assert count.option == 'yes'
assert count.value == 20
votes = list(ve.votes.all())
assert len(votes) == 2
for v in ve.votes.all():
if v.voter_name == 'John Smith':
assert v.option == 'yes'
assert v.voter == Person.objects.get(name='John Smith')
else:
assert v.option == 'no'
assert v.voter == Person.objects.get(name='Adam Smith')
示例8: test_vote_event_bill_actions_two_stage
def test_vote_event_bill_actions_two_stage():
# this test is very similar to what we're testing in test_vote_event_bill_actions w/
# ve3 and ve4, that two bills that reference the same action won't conflict w/ the
# OneToOneField, but in this case we do it in two stages so that the conflict is found
# even if the votes weren't in the same scrape
j = create_jurisdiction()
j.legislative_sessions.create(name='1900', identifier='1900')
org1 = ScrapeOrganization(name='House', classification='lower')
bill = ScrapeBill('HB 1', '1900', 'Axe & Tack Tax Act', from_organization=org1._id)
bill.add_action(description='passage', date='1900-04-02', chamber='lower')
ve1 = ScrapeVoteEvent(legislative_session='1900', motion_text='passage',
start_date='1900-04-02', classification='passage:bill',
result='pass', bill_chamber='lower', bill='HB 1',
bill_action='passage',
organization=org1._id)
ve2 = ScrapeVoteEvent(legislative_session='1900', motion_text='passage',
start_date='1900-04-02', classification='passage:bill',
result='pass', bill_chamber='lower', bill='HB 1',
bill_action='passage',
organization=org1._id)
# disambiguate them
ve1.pupa_id = 'one'
ve2.pupa_id = 'two'
oi = OrganizationImporter('jid')
oi.import_data([org1.as_dict()])
bi = BillImporter('jid', oi, DumbMockImporter())
bi.import_data([bill.as_dict()])
# first imports just fine
VoteEventImporter('jid', DumbMockImporter(), oi, bi).import_data([
ve1.as_dict(),
])
votes = list(VoteEvent.objects.all())
assert len(votes) == 1
assert votes[0].bill_action is not None
# when second is imported, ensure that action stays pinned to first just as it would
# have if they were both in same import
VoteEventImporter('jid', DumbMockImporter(), oi, bi).import_data([
ve1.as_dict(),
ve2.as_dict(),
])
votes = list(VoteEvent.objects.all())
assert len(votes) == 2
assert votes[0].bill_action is not None
assert votes[1].bill_action is None
示例9: viva_voce_votes
def viva_voce_votes(root, session, chamber):
for el in root.xpath(u'//div[starts-with(., "All Members are deemed")]'):
mv = MaybeViva(el)
if not mv.is_valid:
continue
v = VoteEvent(
chamber=chamber,
start_date=None,
motion_text='passage' if mv.passed else 'other',
result='pass' if mv.passed else 'fail',
classification='passage' if mv.passed else 'other',
legislative_session=session[0:2],
bill=mv.bill_id,
bill_chamber=mv.chamber
)
v.set_count('yes', 0)
v.set_count('no', 0)
v.set_count('absent', 0)
v.set_count('not voting', 0)
yield v
示例10: test_vote_event_bill_id_dedupe
def test_vote_event_bill_id_dedupe():
j = create_jurisdiction()
session = j.legislative_sessions.create(name='1900', identifier='1900')
org = Organization.objects.create(id='org-id', name='House', classification='lower',
jurisdiction=j)
bill = Bill.objects.create(id='bill-1', identifier='HB 1', legislative_session=session,
from_organization=org)
bill2 = Bill.objects.create(id='bill-2', identifier='HB 2', legislative_session=session,
from_organization=org)
vote_event = ScrapeVoteEvent(legislative_session='1900', start_date='2013',
classification='anything', result='passed',
motion_text='a vote on something',
bill=bill.identifier, bill_chamber='lower',
chamber='lower')
dmi = DumbMockImporter()
oi = OrganizationImporter('jid')
bi = BillImporter('jid', dmi, oi)
_, what = VoteEventImporter('jid', dmi, oi, bi).import_item(vote_event.as_dict())
assert what == 'insert'
assert VoteEvent.objects.count() == 1
# same exact vote event, no changes
_, what = VoteEventImporter('jid', dmi, oi, bi).import_item(vote_event.as_dict())
assert what == 'noop'
assert VoteEvent.objects.count() == 1
# new info, update
vote_event.result = 'failed'
_, what = VoteEventImporter('jid', dmi, oi, bi).import_item(vote_event.as_dict())
assert what == 'update'
assert VoteEvent.objects.count() == 1
# new vote event, insert
vote_event = ScrapeVoteEvent(legislative_session='1900', start_date='2013',
classification='anything', result='passed',
motion_text='a vote on something',
bill=bill2.identifier, bill_chamber='lower',
chamber='lower')
_, what = VoteEventImporter('jid', dmi, oi, bi).import_item(vote_event.as_dict())
assert what == 'insert'
assert VoteEvent.objects.count() == 2
示例11: test_vote_event_bill_clearing
def test_vote_event_bill_clearing():
# ensure that we don't wind up with vote events sitting around forever on bills as
# changes make it look like there are multiple vote events
j = create_jurisdiction()
session = j.legislative_sessions.create(name='1900', identifier='1900')
org = Organization.objects.create(id='org-id', name='House', classification='lower',
jurisdiction=j)
bill = Bill.objects.create(id='bill-1', identifier='HB 1', legislative_session=session,
from_organization=org)
Bill.objects.create(id='bill-2', identifier='HB 2', legislative_session=session,
from_organization=org)
oi = OrganizationImporter('jid')
dmi = DumbMockImporter()
bi = BillImporter('jid', dmi, oi)
vote_event1 = ScrapeVoteEvent(legislative_session='1900', start_date='2013',
classification='anything', result='passed',
motion_text='a vote on somthing', # typo intentional
bill=bill.identifier, bill_chamber='lower',
chamber='lower'
)
vote_event2 = ScrapeVoteEvent(legislative_session='1900', start_date='2013',
classification='anything', result='passed',
motion_text='a vote on something else',
bill=bill.identifier, bill_chamber='lower',
chamber='lower'
)
# have to use import_data so postimport is called
VoteEventImporter('jid', dmi, oi, bi).import_data([
vote_event1.as_dict(),
vote_event2.as_dict()
])
assert VoteEvent.objects.count() == 2
# a typo is fixed, we don't want 3 vote events now
vote_event1.motion_text = 'a vote on something'
VoteEventImporter('jid', dmi, oi, bi).import_data([
vote_event1.as_dict(),
vote_event2.as_dict()
])
assert VoteEvent.objects.count() == 2
示例12: createVoteEvent
def createVoteEvent(self, motion, agenda_item_version):
version = agenda_item_version
date = self.toDate(version['date'])
v = VoteEvent(
motion_text=motion['title_text'],
result=RESULT_MAP[motion['result']],
classification=motion['action'],
start_date=date,
legislative_session=version['session'],
)
if motion['mover']:
v.extras['mover'] = motion['mover']
if motion['body_text']:
v.extras['body'] = motion['body_text']
v.set_bill(version['bill_identifier'])
v.add_source(version['url'])
return v
示例13: process_committee_vote
def process_committee_vote(self, committee_action, bill):
try:
date = committee_action["ActionDate"]
vote_info = committee_action["Vote"]
except KeyError:
self.logger.warning("Committee vote has no data. Skipping.")
return
date = self.date_format(date)
other_count = 0
for v in vote_info:
vote_count = 0 if v["VoteCount"] == "" else int(v["VoteCount"])
if v["VoteType"] == "Yes":
yes_count = vote_count
elif v["VoteType"] == "No":
no_count = vote_count
else:
other_count += vote_count
result = 'fail'
if yes_count > no_count:
result = 'pass'
v = VoteEvent(chamber='legislature',
start_date=date,
motion_text='Committee Vote',
result=result,
classification='committee',
bill=bill
)
v.set_count('yes', yes_count)
v.set_count('no', no_count)
v.set_count('other', other_count)
return v
示例14: scrape_vote
def scrape_vote(self, bill, vote_json, session):
if vote_json['amendmentNumber']:
motion = '{}: {}'.format(
vote_json['amendmentNumber'], vote_json['action'])
else:
motion = vote_json['action']
result = 'pass' if vote_json['yesVotesCount'] > vote_json['noVotesCount'] else 'fail'
v = VoteEvent(
chamber=self.chamber_abbrev_map[vote_json['chamber']],
start_date=self.parse_local_date(vote_json['voteDate']),
motion_text=motion,
result=result,
legislative_session=session,
bill=bill,
classification='other',
)
v.set_count(option='yes', value=vote_json['yesVotesCount'])
v.set_count('no', vote_json['noVotesCount'])
v.set_count('absent', vote_json['absentVotesCount'])
v.set_count('excused', vote_json['excusedVotesCount'])
v.set_count('other', vote_json['conflictVotesCount'])
for name in vote_json['yesVotes'].split(','):
if name.strip():
v.yes(name.strip())
for name in vote_json['noVotes'].split(','):
if name.strip():
v.no(name.strip())
# add votes with other classifications
# option can be 'yes', 'no', 'absent',
# 'abstain', 'not voting', 'paired', 'excused'
for name in vote_json['absentVotes'].split(','):
if name.strip():
v.vote(option="absent",
voter=name)
for name in vote_json['excusedVotes'].split(','):
if name.strip():
v.vote(option="excused",
voter=name)
for name in vote_json['conflictVotes'].split(','):
if name.strip():
v.vote(option="other",
voter=name)
source_url = 'http://lso.wyoleg.gov/Legislation/{}/{}'.format(
session, vote_json['billNumber'])
v.add_source(source_url)
yield v
示例15: scrape_action_page
def scrape_action_page(self, bill, page):
action_rows = page.xpath('//tbody/tr')
for row in action_rows:
action_date = row.xpath('td[1]/text()')[0]
action_date = datetime.strptime(action_date, '%m/%d/%Y')
action_year = action_date.year
action_date = action_date.strftime('%Y-%m-%d')
if row.xpath('td[2]/text()'):
action_actor = row.xpath('td[2]/text()')[0]
action_actor = self.chamber_map_reverse[action_actor.strip()]
action_name = row.xpath('string(td[3])')
# House votes
if "Supplement" in action_name:
actor = "lower"
vote_action = action_name.split(' -')[0]
y = int(action_name.strip().split('-')[1].split('YEAS')[0])
n = int(action_name.strip().split('YEAS to')[1].split('NAYS')[0])
# get supplement number
n_supplement = int(action_name.strip().split('No. ')[1].split(r')')[0])
cached_vote = VoteEvent(
chamber=actor,
start_date=action_date,
motion_text=vote_action,
result='pass' if y > n else 'fail',
classification='passage',
bill=bill,
)
cached_vote.set_count('yes', y)
cached_vote.set_count('no', n)
housevote_pdf = 'http://www.mass.gov/legis/journal/combined{}RCs.pdf'.format(
action_year
)
# note: 2014-2015 different format and no data on website for years prior to 2014
self.scrape_house_vote(cached_vote, housevote_pdf, n_supplement)
cached_vote.add_source(housevote_pdf)
cached_vote.pupa_id = '{}#{}'.format(housevote_pdf, n_supplement)
yield cached_vote
# Senate votes
if "Roll Call" in action_name:
actor = "upper"
# placeholder
vote_action = action_name.split(' -')[0]
try:
y, n = re.search('(\d+) yeas .*? (\d+) nays', action_name.lower()).groups()
y = int(y)
n = int(n)
except AttributeError:
y = int(re.search(r"yeas\s*(\d*)", action_name.lower()).group(1))
n = int(re.search(r"nays\s*(\d*)", action_name.lower()).group(1))
# TODO: other count isn't included, set later
cached_vote = VoteEvent(
chamber=actor,
start_date=action_date,
motion_text=vote_action,
result='pass' if y > n else 'fail',
classification='passage',
bill=bill,
)
cached_vote.set_count('yes', y)
cached_vote.set_count('no', n)
rollcall_pdf = 'http://malegislature.gov' + row.xpath('string(td[3]/a/@href)')
self.scrape_senate_vote(cached_vote, rollcall_pdf)
cached_vote.add_source(rollcall_pdf)
yield cached_vote
attrs = self.categorizer.categorize(action_name)
action = bill.add_action(
action_name.strip(),
action_date,
chamber=action_actor,
classification=attrs['classification'],
)
for com in attrs.get('committees', []):
action.add_related_entity(com, entity_type='organization')