same_count += count_matching_rejected_issuers_for_company(name)

        similar_count += count_similar_rejected_issuers_for_company(name)
        similar_count += count_similar_rejected_issuers_for_company(name)

print "%s same names" % same_count
print "%s similar but different names" % similar_count
print "Companies to issuer names:"
with open("companies_to_issuers.csv", "w") as csv_output:
    writer = csv.DictWriter(csv_output, ['COMPANY', 'ISSUER'])
    writer.writeheader()
    company_names = companies_to_issuers.keys()
    company_names.sort()
    for name in company_names:
        issuers = list(set(companies_to_issuers[name]))
        issuers.sort()
        has_different_names = ([issuer != name
                                for issuer in issuers]).count(True) > 0
        if not args.exclude or has_different_names:
            print name
            for issuer in issuers:
                print "\t%s (Parent Co? %s)" % (
                    issuer, "Y" if is_parent_company(name, issuer) else "N")
                if not args.parents or is_parent_company(name, issuer):
                    row = {}
                    row['COMPANY'] = name
                    row['ISSUER'] = issuer
                    writer.writerow(row)

print "Wrote to companies_to_issuers.csv."
        same_count += count_matching_valid_issuers_for_company(name)
        same_count += count_matching_rejected_issuers_for_company(name)

        similar_count += count_similar_rejected_issuers_for_company(name)
        similar_count += count_similar_rejected_issuers_for_company(name)

print "%s same names" % same_count
print "%s similar but different names" % similar_count
print "Companies to issuer names:"
with open("companies_to_issuers.csv", "w") as csv_output:
    writer = csv.DictWriter(csv_output, ['COMPANY', 'ISSUER'])
    writer.writeheader()
    company_names = companies_to_issuers.keys()
    company_names.sort()
    for name in company_names:
        issuers = list(set(companies_to_issuers[name]))
        issuers.sort()
        has_different_names = ([issuer != name for issuer in issuers]).count(True) > 0
        if not args.exclude or has_different_names:
            print name
            for issuer in issuers:
                print "\t%s (Parent Co? %s)" % (issuer, "Y" if is_parent_company(name, issuer) else "N")
                if not args.parents or is_parent_company(name, issuer):
                    row = {}
                    row['COMPANY'] = name
                    row['ISSUER'] = issuer
                    writer.writerow(row)

print "Wrote to companies_to_issuers.csv."
 def test_is_parent_company(self):
     self.assertTrue(is_parent_company("Exxon Corp", "Exxon Corp DE"))
 def test_is_not_parent_company(self):
     self.assertFalse(is_parent_company("Coca Cola", "Coca Cola Bottling Company Inc"))
 def test_is_parent_company_exact_match(self):
     self.assertTrue(is_parent_company("ARCHER DANIELS MIDLAND", "ARCHER DANIELS MIDLAND"))
 def test_adm_co(self):
     self.assertTrue(is_parent_company("ARCHER DANIELS MIDLAND", "ARCHER DANIELS MIDLAND CO"))
 def test_wells_fargo(self):
     self.assertTrue(is_parent_company("WELLS FARGO COMPANY", "WELLS FARGO COMPANY MN"))
 def test_apc(self):
     self.assertTrue(is_parent_company("AIR PRODUCTS AND CHEMICALS INC", "AIR PRODUCTS CHEMICALS INC DE"))
 def test_mcdonalds(self):
     self.assertTrue(is_parent_company("MCDONALD'S CORP", "MCDONALDS CORP"))
print query_template
con = sqlite3.connect("ingest.db")
cur = con.cursor()

valid_common_stocks_count = 0
for name in company_names:
    #print name
    query = query_template % 'valid_items'
    cur.execute(query, ['%' + db_escape_name(escape_name(name)) + '%'])
    #print "Looking for valid issue name: " + db_escape_name(escape_name(name))
    items = cur.fetchall()

    has_common_stock = False

    for item in items:
        if is_parent_company(name, item[0]):
            has_common_stock = True
            break
        else:
            print "Comparing %s with %s" % (name, item[0])
            print "NOT MATCH!!!"
    if not has_common_stock:
        no_valid_common_stock_companies.append(name)
    else:
        companies_with_valid_or_rejected_issues.append(name)
        valid_common_stocks_count += 1

    if len(items) == 0:
        print "NO VALID ITEMS FOR %s" % name

print "# of companies with no valid common stock filings: %d" % len(
print query_template
con = sqlite3.connect("ingest.db")
cur = con.cursor()

valid_common_stocks_count = 0
for name in company_names:
    #print name
    query = query_template % 'valid_items'
    cur.execute(query, ['%' + db_escape_name(escape_name(name)) + '%'])
    #print "Looking for valid issue name: " + db_escape_name(escape_name(name))
    items = cur.fetchall()

    has_common_stock = False

    for item in items:
        if is_parent_company(name, item[0]):
            has_common_stock = True
            break
        else:
            print "Comparing %s with %s" % (name, item[0])
            print "NOT MATCH!!!"
    if not has_common_stock:
        no_valid_common_stock_companies.append(name)
    else:
        companies_with_valid_or_rejected_issues.append(name)
        valid_common_stocks_count += 1

    if len(items) == 0:
        print "NO VALID ITEMS FOR %s" % name

print "# of companies with no valid common stock filings: %d" % len(no_valid_common_stock_companies)