Next / Previous / Contents / NM Tech homepage

8.3. class MyCBC: Interface to the old database

In this database, all the exported attributes are the same as in Section 5, “Using the pycbc interface”, except for the table names. Also, because it is intended only for use in one single-threaded application, the Session() class constructor is not exported.

mycbc.py
# - - - - -   c l a s s   M y C B C

class MyCBC(object):
    '''Interface to the 1998 MySQL CBC database

      Exports:
        MyCBC(password):
          [ password is a string ->
              if password is the MySQL CBC database password ->
                return a new MyCBC instance giving read-write
                access to that database
              else ->
                return a new MyCBC instance giving read-only
                access to that database ]
        .engine:
          [ an sqlalchemy.engine.Engine instance connected to
            the database ]
        .meta:
          [ the metadata as sqlalchemy.schema.MetaData instance ]
        .s:
          [ a Session connected to self.engine ]
        .Cir:     [ class mapped to the cir table ]
        .Stnd:    [ class mapped to the stnd table ]
        .AsPub:   [ class mapped to the aspub table ]
        .Eff:     [ class mapped to the eff table ]
        .cir_table, .stnd_table, .aspub_table, .eff_table:
          [ the actual Table instances for these classes ]

Since the only purpose of this module is to drive the extraction of data from the old database described in Section 9, “transloader: Copy over the MySQL database”, rather than set up table relations in the orm, we'll just define a few methods that run simple queries that generate the circle records and then dig down to retrieve all the related rows from the other tables. Note that all these retrieval methods do no error checking, on the assumption that all the foreign key constraints on the MySQL database are true. This database was built when MySQL had no foreign key constraints, but the software that loaded it insured them.

mycbc.py
        .genCirs():
          [ generate a sequence of Cir instances representing
            to the rows of the cir table ]
        .genStnds(lat_lon):
          [ lat_lon is a lat_lon column value ->
              generate a sequence of Stnd instances that use
              that lat_lon ]
        .getEff(count_id):
          [ count_id is a count_id column value ->
              return the Eff instance for that count_id ]
        .getAsPub(count_id):
          [ count_id is a count_id column value ->
              return the AsPub instance for that count_id ]
        .genCens(count_id):
          [ (count_id is a count_id column value) ->
              generate the Cen instances for count_id ]
    '''

Here are the definitions of the tables and mapped classes, which are all inside the MyCBC class.

mycbc.py
#================================================================
# Tables and mapped classes
#----------------------------------------------------------------
    meta = schema.MetaData()

    class Cir(object):
        def __init__(self, lat_lon, physio, water, odd, regions, name ):
            self.lat_lon = lat_lon
            self.physio = physio
            self.water = water
            self.odd = odd
            self.regions = regions
            self.name = name
        def __repr__(self):
            return ( "<Cir(%s %s: %s)>" %
                     (self.lat_lon, self.regions, self.name) )

    class Stnd(object):
        def __init__(self, lat_lon, count_id):
            self.lat_lon = lat_lon
            self.count_id = count_id
        def __repr__(self):
            return ( "<Stnd(%s=%s)>" %
                     (self.lat_lon, self.count_id))

    class AsPub(object):
        def __init__(self, count_id, as_lat_lon, as_regions, as_name):
            self.count_id = count_id
            self.as_lat_lon = as_lat_lon
            self.as_regions = as_regions
            self.as_name = as_name
        def __repr__(self):
            return ( "<AsPub(%s %s %s: %s)>" %
                     (self.count_id, self.as_lat_lon,
                      self.as_regions, self.as_name) )

    class Eff(object):
        def __init__(self, count_id, yyyymmdd, n_obs,
                     ph_tot, ph_foot, ph_car, ph_o, h_fd, h_owl,
                     pm_tot, pm_f, pm_c, pm_o, m_owl):
            self.count_id = count_id
            self.yyyymmdd = yyyymmdd
            self.n_obs = n_obs
            self.ph_tot = ph_tot
            self.ph_foot = ph_foot
            self.ph_car = ph_car
            self.ph_o = ph_o
            self.h_fd = h_fd
            self.h_owl = h_owl
            self.pm_tot = pm_tot
            self.pm_f = pm_f
            self.pm_c = pm_c
            self.pm_o = pm_o
            self.m_owl = m_owl
        def __repr__(self):
            return ( "<Eff(%s %s %d)>" %
                     (self.count_id, self.yyyymmdd, self.n_obs) )

    class Cen(object):
        def __init__(self, count_id, seq_no, form, rel, alt_form,
                     age, sex, plus, q, census):
            self.count_id = count_id
            self.seq_no = seq_no
            self.form = form
            self.rel = rel
            self.alt_form = alt_form
            self.age = age
            self.sex = sex
            self.plus = plus
            self.q = q
            self.census = census