On 12/04/2014 05:55 PM, Jon 1234 wrote: >> The get_memb script is specific to our database, but it could basically >> be anything, even just a straight MySQL query, that produces the desired >> list (In our case, the logic of determining whether a record in the >> table is that of a current member is complex and in some cases involves >> looking at dates in other records, thus the python script). > > That sounds so similar to my situation that I'm going to push my luck, and > ask if I could see an anonymised version of your get_memb script - would that > be all right? Even the "who is a member?" part would be useful as, while mine > would be different, it would help me with learning the necessary Python.
OK. It's attached. It is the script I run except for database names and MySQL user and password. Caveats: It's not documented and is not straightforward for even me to follow, and I know what it's doing. The tPeople table contains various entries for people who may or may not be club members. For members, there are individual and family members. For family members there is a primary for one person and a duplicate for the second person (all families have 2 members). RelatedTo is the ID of the other record of a family. Duplicate records expiration doesn't count - look up the expiration of the RelatedTo record. Some of the things I do are because I don't trust the integrity of the data in the database. -- Mark Sapiro <m...@msapiro.net> The highway is for gamblers, San Francisco Bay Area, California better use your sense - B. Dylan
#!/usr/bin/python2.7 # # Makes a list of "name" <address> for all current members email addresses. # import sys import time import MySQLdb import datetime # Is this production? PROD = True TYPES = ['i', 'f', 'd', 'j', 'g', ] FAM_TYPES = ['f', 'g', ] DUP_TYPES = ['d', ] def compdate(d1, d2): """Compares two datetime.date instances and returns an integer equal to the number of months between them ignoring day of month. =0 if the two dates are in the same year&month, >0 if d2's year&month is > d1's year&month and <0 otherwise.""" return d2.month - d1.month + 12 * (d2.year - d1.year) def main(): if PROD: base = 'xxxxxx' else: base = 'xxxxxx_sand' db=MySQLdb.connect(user='xxxxxx', passwd='xxxxxx', db=base) c = db.cursor() c.execute("""SELECT ID, RelatedTo, First, Last, eMail, Expiration, memberno, Type FROM tPeople""") dic = {} while True: rec = c.fetchone() if not rec: break dic[rec[0]] = rec c.close() db.close() dv = dic.values() dv.sort(lambda x, y: cmp(x[3] + x[2], y[3] + y[2])) emails = {} for rec in dv: id, related, first, last, e_mail, exdate, memberno, type = rec if not e_mail: continue if not type or not type.lower() in TYPES: continue if type.lower() in DUP_TYPES: try: if not dic[rec[1]][7] or dic[rec[1]][7].lower() not in FAM_TYPES: continue exdate = dic[rec[1]][5] except KeyError: #print >> sys.stderr, rec continue if not exdate: print >> sys.stderr, 'No exdate for %s %s' % (first, last) continue if compdate(datetime.date.today(), exdate) < 0: continue if e_mail in emails: if type.lower() in DUP_TYPES: continue elif emails[e_mail][2].lower() in DUP_TYPES: emails[e_mail] = (first, last, type) continue else: print >> sys.stderr, 'Relateds, neither is dup.\n ', print >> sys.stderr, rec continue else: emails[e_mail] = (first, last, type) for e_mail, name_type in emails.items(): print '"%s %s" <%s>' % (name_type[0], name_type[1], e_mail) if __name__ == '__main__': main()
------------------------------------------------------ Mailman-Users mailing list Mailman-Users@python.org https://mail.python.org/mailman/listinfo/mailman-users Mailman FAQ: http://wiki.list.org/x/AgA3 Security Policy: http://wiki.list.org/x/QIA9 Searchable Archives: http://www.mail-archive.com/mailman-users%40python.org/ Unsubscribe: https://mail.python.org/mailman/options/mailman-users/archive%40jab.org