Hola-

I also recently was tasked by my wife to help her personalize letters
to all the Relief Society.  Enter python and OOo.  The attached python
script will extract all Relief Society members from the csv dump
(based on gender and date script is run).  I assume everyone else
does/will do this in their favorite language, but in case any python
people are interested here it is.

enjoy,
matt

ps - mail merging and label generated is a pain.  (I haven't done it
since the days of DOS, but I wanted to throw something through my 24
inch monitor a couple times.  My wife thought it was funny.....)
"""
Convert MLS Membership.csv dump to Relief Society only.  I also add 3 extra
name columns ("First", "Last", "First and Last") useful for doing mail merges.

Here's the usage:

usage: Utility to extract current RS members based on todays date

options:
  -h, --help            show this help message and exit
  -i FIN, --in-file=FIN
                        specify MLS export to read from
  -o FOUT, --out-file=FOUT
                        name of Relief Society file


Licensed under PSF license.

Copyright 2007 - matt harrison

"""

import datetime
import time
import csv
import optparse

HEADERS=["Indiv ID","Full Name","Preferred Name","MRN","HofH ID","HH Position","HH Order","Phone 1","Phone 2","E-mail Address","Street 1","Street 2","D/P","City","Postal","State/Prov","Country","2-Street 1","2-Street 2","2-D/P","2-City","2-Zip","2-State/Prov","2-Country","Ward GEO Code","Stake GEO Code","Sex","Birth","Baptized","Confirmed","Endowed","Rec Exp","Priesthood","Mission","Married","Spouse Member","Sealed to Spouse","Sealed to Prior"]

def get_data(filename):
    """return an iteration of dicts"""
    fin = open(filename)
    reader = csv.DictReader(fin)
    for line in reader:
        #extra comma in header puts empty field remove it
        del line[""]
        yield line

def rs_iter(data_iter):
    for data in data_iter:
        if is_rs(data):
            yield data

def add_names_iter(data_iter):
    """Take Preferred Name column and make 3 new columns
    First, Last and First and Last
    """
    #update HEADERS
    for name in ["First", "Last", "First and Last"]:
        if name not in HEADERS:
            global HEADERS
            HEADERS.append(name)
            
    for data in data_iter:
        preferred = data["Preferred Name"]
        split_index = preferred.index(", ")
        last = preferred[:split_index]
        first = preferred[split_index+2:]
        first_last = " ".join([first, last])

        data["First"] = first
        data["Last"] = last
        data["First and Last"] = first_last
        yield data
        
def to_file(data_iter, filename):
    fout = open(filename, 'w')
    #get data first since it updates HEADERS possibly
    datas = [x for x in data_iter]
    fout.write(",".join(HEADERS)+"\n")
    writer = csv.DictWriter(fout, fieldnames=HEADERS)
    for data in datas:
        writer.writerow(data)
    fout.close()

def is_rs(record):
    return is_18(record) and is_female(record)

def is_female(record):
    return record["Sex"] == "Female"

def is_18(record):
    """
    >>> is_18(dict(Birth="09/17/75"))
    True
    >>> is_18(dict(Birth="08/14/89"))
    True
    >>> is_18(dict(Birth="08/16/90"))
    False
    """
    today = datetime.date.today()
    #if empty assume they are too old to ask hence, hence True
    if not record["Birth"]:
        return True
    birth = datetime.date(*time.strptime(record["Birth"], "%d %b %Y")[:3])
    return today.year - birth.year > 18 or \
           (today.year - birth.year == 18 and \
            today.month >= birth.month and \
            today.day >= birth.day)

def test():
    import doctest
    doctest.testmod()


def main():
    p = optparse.OptionParser(usage="Utility to extract current RS members based on todays date")

    p.add_option("-i", "--in-file", action="store", dest="fin",
                 default="Membership.csv",
                 help="specify MLS export to read from")
    p.add_option("-o", "--out-file", action="store", dest="fout",
                 default="RS.csv",
                 help="name of Relief Society file")
    p.add_option("-t", action="store_true", dest="test",
                 help="run file tests")

    opt, args = p.parse_args()

    if opt.test:
        test()
    else:
        to_file(add_names_iter(rs_iter(get_data(opt.fin))), opt.fout)
    
if __name__ == "__main__":
    main()
_______________________________________________
Ldsoss mailing list
[email protected]
http://lists.ldsoss.org/mailman/listinfo/ldsoss

Reply via email to