Esempio n. 1
0
 def test_date_fields_return_dates_or_none(self):
     for i in range(1,46):
         p = President(i)
         self.assertIsInstance(p.birth_date, date, "birth_date is not datetime.date object")
         self.assertIsInstance(p.death_date, (date, type(None)), "death_date is not datetime.date object or None")
         self.assertIsInstance(p.term_start_date, date, "term_start is not datetime.date object")
         self.assertIsInstance(p.term_end_date, (date, type(None)), "term_end is not datetime.date object or None")
Esempio n. 2
0
def handle_client(client):

    while True:
        raw_request = client.recv(1024)

        if not raw_request:
            break

        logging.info("Client request: {0}".format(raw_request))

        (num, field) = raw_request.split()

        p = President(int(num))

        if (field == b'FNAME'): response = p.first_name.encode()
        if (field == b'LNAME'): response = p.last_name.encode()
        if (field == b'BDATE'): response = p.birth_date.encode()
        if (field == b'DDATE'): response = p.death_date.encode()
        if (field == b'TSDATE'): response = p.term_start.encode()
        if (field == b'TEDATE'): response = p.term_end.encode()
        if (field == b'PARTY'): response = p.party.encode()
        if (field == b'BSTATE'): response = p.birth_state.encode()
        if (field == b'BPLACE'): response = p.birth_place.encode()

        client.send(response + b'\n')

    client.close()

    return
def president_by_term(termnum):
    """Retrieve president information for a specified term number"""
    term = int(termnum)
    if 0 < term < 45:
        p = President(term)
        return render_template('president_detail.html', presidents=[p])
    else:
        return '<h2>Sorry,  {} is not a valid term number</h2>'.format(term)
def user_name(username):
    user_agent = request.headers.get('User-Agent')
    return render_template(
        'vars_and_filters.html',
        president=President(26),
        browser=user_agent,
        username=username.replace('+', ' '),
        fruits=FRUITS,
    )
Esempio n. 5
0
def president_by_term(termnum):

    """Retrieve president information for a specified term number"""
    term = int(termnum)
    if 0 < term < 46:
        presidents = [President(term)]
        return render_template('president_results.html', presidents=presidents)
    else:
        html_content = '<h2>Sorry,  {} is not a valid term number</h2>'.format(term)
        return html_content
Esempio n. 6
0
def main():
    s3 = boto3.resource('s3')  # <1>

    for i in range(1, 46):
        p = President(i)
        data = f"{i};{p.first_name};{p.last_name};{p.party}"
        bindata = data.encode()

        obj = s3.Object(WORD_BUCKET_NAME, f"president/{i}")
        obj.put(Body=bindata)
Esempio n. 7
0
def format_html_for_president(term_num):
    """Return HTML for one president"""
    p = President(term_num)
    return '''
    <h1>{}: {} {}</h1>
    <h2>Born in: {}</h2>
    <h2>Lived: {} to {}</h2>
    <h2>Party: {}</h2>
    '''.format(term_num, p.first_name, p.last_name, p.birth_state,
               p.birth_date, p.death_date, p.party)
def president_by_last_name(last_name):
    """Retrieve president information for a specified last name;
        May return info for more than one president
    """
    presidents = []
    for i in xrange(1, 45):
        p = President(i)
        if p.last_name.lower() == last_name.lower():
            presidents.append(p)

    if presidents:
        return render_template('president_results.html', presidents=presidents)
    else:
        return '<h2>Sorry,  {} not found</h2>'.format(last_name)
Esempio n. 9
0
def president_by_last_name(last_name):
    """Retrieve president information for a specified last name;
        May return info for more than one president
    """
    html_content = ''
    for i in range(1, 46):
        p = President(i)
        if p.last_name.lower() == last_name.lower():
            html_content += format_html_for_president(i)

    if html_content:
        return html_content
    else:
        return '<h2>Sorry,  {} not found</h2>'.format(last_name)
def index():
    """Main (and only) page; returns list of all presidents"""
    presidents = []
    for i in range(1, 46):
        presidents.append(President(i))
    accept_type = request.headers.get('ACCEPT')
    response = ''
    print("Accept type:", accept_type)
    if accept_type.startswith('text/html'):
        response = render_template('president_list_bs.html',
                                   presidents=presidents)
    elif accept_type.startswith('application/xml'):
        response = pres_list_to_xml(presidents)
    elif accept_type == 'application/json':
        presidents_as_dicts = [pres_to_dict(p) for p in presidents]
        response = jsonify(presidents=presidents_as_dicts)
    # handle error here if non-expected type

    return response
Esempio n. 11
0
#!/usr/bin/env python

from president import President

p = President(44)

print(f'President {p.first_name} {p.last_name}, the {p.term_number}th president, was born in {p.birth_city} {p.birth_state}')
Esempio n. 12
0
def test_president_one_is_gw():
    p = President(1)
    assert p.first_name == 'George'
    assert p.last_name == 'Washington'
Esempio n. 13
0
 def test_pres_1_last_name_is_washington(self):
     p = President(1)
     self.assertEqual('Washington', p.last_name)
Esempio n. 14
0
 def setUp(self):  # run before each test
     self.pres = President(1)
Esempio n. 15
0
 def test_invalid_term_raises_error(self):
     with self.assertRaises(ValueError):
         p = President(99)
Esempio n. 16
0
 def test_invalid_term_raises_error(self):
     for invalid_term in -1, 0, 46, 1000:
         with self.assertRaises(ValueError):
             p = President(invalid_term)
Esempio n. 17
0
# Exercise 3-1

from president import President

# Tests President object class.
for term in 1, 26, 39, 45:
    p = President(term)
    print("{} {} was born at {}, {} on {}".format(p.first_name, p.last_name,
                                                  p.birth_place, p.birth_state,
                                                  p.birth_date))
    print()
Esempio n. 18
0
 def test_1_through_45_are_valid_terms(self):
     for i in range(1, 46):
         p = President(i)
         self.assertIsInstance(p, President)
def test_president_one_last_name_is_george():
    p = President(1)
    assert p.first_name == "George"
Esempio n. 20
0
def list_presidents():
    """Return list of all presidents"""
    presidents = []
    for i in range(1, 45):
        presidents.append(President(i))
    return render_template('president_list_bs.html', presidents=presidents)
Esempio n. 21
0
#!/usr/bin/env python

from president import President
"""
Write a main script to exercise some or all of the properties from president.py. 
It could look something like
    from president import President
    
    p = President(1)   # George Washington
    print("George was born at {0}, {1} on {2}".format(
        p.birth_place, p.birth_state, p.birth_date
    )
"""
with open('data/presidents.txt') as pres_in:
    for line in pres_in:
        print(line)
        p = President(line[0])
Esempio n. 22
0
#!/usr/bin/env python

from president import President
"""
Monkey-patch the President class to add a method get_full_name which returns a single string consisting of the first name and the last name, separated by a space.
Try This_: Instead of a method, make full_name a property.
"""


@property
def full_name(self):
    return f"{self.first_name}-{self.last_name}"


setattr(President, "full_name", full_name)

p = President(1)
print(p.full_name)
Esempio n. 23
0
#!/usr/bin/python

from president import President


def fn(self):
    return '%s %s' % (self.first_name, self.last_name)


setattr(President, 'get_full_name', fn)
# or just
# President.getFullName = fn

abe = President(16)
print(abe.get_full_name())

millard = President(13)
print(millard.get_full_name())
Esempio n. 24
0
 def test_president_one_last_name_is_george(self):
     p = President(1)
     self.assertEqual(p.first_name, "George", "President 1's first name is not George")
#!/usr/bin/env python
from president import President

for term in 1, 26, 39, 45:
    p = President(term)  # George Washington

    print("{} {} was born at {}, {} on {}".format(p.first_name, p.last_name,
                                                  p.birth_place, p.birth_state,
                                                  p.birth_date))
    print()
def test_date_fields_return_dates_or_none(term_number):
    p = President(term_number)
    assert isinstance(p.birth_date, date) or p.birth_date is None
    assert isinstance(p.death_date, date) or p.death_date is None
    assert isinstance(p.term_start_date, date) or p.term_start_date is None
    assert isinstance(p.term_end_date, date) or p.term_end_date is None
Esempio n. 27
0
from president import President

p = President(26)

print(p.first_name, p.last_name)

attrs = [a for a in dir(p) if not a.startswith('_')]

print(attrs, '\n')

for attr in attrs:
    print(attr, getattr(p, attr))
print()

#  getattr() hasattr() setattr() delattr()


def get_full_name(self):
    return f"{self.first_name} {self.last_name}"


setattr(President, "get_full_name", get_full_name)

print(p.get_full_name())

setattr(p, 'assassinated', False)

print(p.assassinated)


def serialize(obj):
def test_invalid_term_raises_error(invalid_term):
    with pytest.raises(ValueError):
        p = President(invalid_term)
Esempio n. 29
0
#!/usr/bin/env python

from president import President

for pres_term in 1, 26, 37:
    p = President(pres_term)

    lastname = getattr(p, 'last_name')
    firstname = getattr(p, 'first_name')
    party = getattr(p, 'party')
    print(firstname, lastname, 'was a', party)
Esempio n. 30
0
 def test_date_fields_are_datetime(self):
     p = President(1)
     self.assertIsInstance(p.birth_date, date)