Mr O Callaghan ICT

AllPages
RecentChanges
Links to this page
Edit this page
Search
Entry portal
Advice For New Users

Tasks found here: https://www.dropbox.com/sh/m6euna3bdf85byh/u7DtgETnpy

Project 4, task 1:

This is not professional quality code, but it's a quick example of just getting it done.

It does not include proper testing, proper documentation, proper error-checking, or anything else a "proper" piece of code should do, but it does the job asked of it in a simple and straight-forward manner.


#!/usr/bin/python

import re
from sys import argv

alphanumeric = '[a-zA-Z0-9]'
field_regular_expression = 2* alphanumeric + '*'

email_regular_expression = '^'
email_regular_expression += field_regular_expression
email_regular_expression += '@'
email_regular_expression += field_regular_expression
email_regular_expression += '\\.'
email_regular_expression += field_regular_expression
email_regular_expression += '$'

#############################################
# Here's the code that actually does the test
#############################################

def validate_email(address_to_check):

    email_match_object = re.search(email_regular_expression,address_to_check)
    if email_match_object == None:
        return 'INVALID'
    return 'VALID'

#############################################
# Some some informal, ad hoc testing
#############################################

test_data = [
    ('a@b.c'      ,'VALID'),
    ('ab23@f45.d3','VALID'),
    ('@bc.d'      ,'INVALID'),
    ('123.c@cvb'  ,'INVALID'),
    ]

all_results_successful = True

for address_to_test,expected_result in test_data:

    returned_value = validate_email(address_to_test)
    if returned_value != expected_result:
        print 'TEST FAILURE'
        print 'Address tested : %s' % address_to_test
        print 'Expected result: %s' % expected_result
        print 'Result returned: %s' % returned_value
        all_results_successful = False

if all_results_successful:

    print 'All tests passed'

for a in argv[1:]:

    print a, validate_email(a)


Project 4, task 2

Designed to run from the command line.

The "Usage" message should be split into a separate file.


#!/usr/bin/python

from sys import argv

def find_and_validate_user(client_email, password, filename=None):

    if filename==None:
        filename = 'p4t2.csv'

    handle = file(filename)

    for data_line in handle.xreadlines():
        data_fields = data_line.strip().split(',')
        if data_fields[0] == client_email:
            if data_fields[1] == password:
                return data_fields

    return None

if len(argv)<=1:

    print 'Usage:'
    print
    print '    Given an email and password, find and verify the client'
    print '    exists. If not found, or password doesn\'t match, return'
    print '    "Client not found" message'
    print
    print '    Parameters come in pairs.  You can specify a different'
    print '    data file on the command line by preceding it with -f'
    print
    print '    [ -f filename ] : specify data file (defaults to p4t2.csv)'
    print
    print '    Otherwise provide an email and password'
    print
    print '    email password  : display user details'
    print
    print '    No parameters will provoke this message'
    print '    Any number of clients can be supplied in a single invocation.'
    print
    print 'Example:'
    print
    print '    python p4t2.py -f P4.csv email password other_email pass2'

argv.pop(0)

while len(argv) > 1:
    client_email = argv.pop(0)
    if client_email == '-f':
        filename = argv.pop(0)
        client_email = argv.pop(0)

    password = argv.pop(0)

    user_data = find_and_validate_user(client_email,password,filename)

    if user_data:

        print 'Client name: %s, %s' % (user_data[3],user_data[2])
        print 'Address: %s' % user_data[4]
        print 'Address: %s' % user_data[5]
        print 'Postcode: %s' % user_data[6]
        print 'Email: %s' % user_data[0]

    else:

        print 'Client "%s" not found' % client_email

    print

if len(argv)==1:

    print 'Extra parameter "%s" ignored' % argv[0]



Project 4, task 3


#!/usr/bin/python

from sys import argv
from re import search

default_data_file = 'Seating.txt'

def get_seating_row(handle):

    line = handle.readline().strip() + 12*'X'
    return line[0],line[2:12]

def read_seats(filename=None):

    if filename == None:
        filename = default_data_file

    handle = file(filename)

    seating_data = {}
    for i in range(5):
        row,seats = get_seating_row(handle)
        seating_data[row] = seats
    return seating_data


def allocate(row,num,seating_data):

    if row not in seating_data:
        return 'Invalid row: %s' % row

    seating_row = seating_data[row]
    try:
        int_num = int(num)
    except:
        return 'Unable to understand number of seats requested: %s' % num

    found = seating_row.find(int_num*'-')
    if found < 0:
        rtn = ''
        rtn += 'Row %s does not have %s contiguous seats available.\n' % (row,num)
        rtn += 'Maximum block in row %s is of size %d.' % (row,max([len(i) for i in seating_row.split('X')]))
        return rtn

    return 'Seats available for %s people: %s%d - %s%d' % (num,row,found+1,row,found+int_num)


seating_data = read_seats()

argv.pop(0)
if len(argv) != 2:
    print 'Need a row and required number of seats as parameters'
else:
    row,num = argv
    print allocate(row,num,seating_data)

This also needs a seating file showing which seats are taken. In this example X indicates taken.


E:----------
D:-X-X-XXX--
C:XXXX--X---
B:XXXXXXXXXX
A:----XXX---

Example output:



% python p4t3.py x
Need a row and required number of seats as parameters

% python p4t3.py x 5
Invalid row: x

% python p4t3.py A 5
Row A does not have 5 contiguous seats available.
Maximum block in row A is of size 4.

% python p4t3.py A 3
Seats available for 3 people: A1 - A3

% python p4t3.py D 3
Row D does not have 3 contiguous seats available.
Maximum block in row D is of size 2.

% python p4t3.py D 2
Seats available for 2 people: D9 - D10

% python p4t3.py D 1
Seats available for 1 people: D1 - D1

% python p4t3.py B 4
Row B does not have 4 contiguous seats available.
Maximum block in row B is of size 0.


Links to this page / Page history / Last change to this page
Recent changes / Edit this page (with sufficient authority)
All pages / Search / Change password / Logout