Entries Tagged 'Code Fragment' ↓

Python: Find objects of a certain type in memory

This code snippet will allow you to look for objects of a certain type in memory. This is very useful for plugins:

"""
    This looks through all the objects in memory, to find ones
    of the designated type.
"""
 
import gc
 
class Animal(object):
    def make_noise(self):
        print "snort"
 
animal = Animal()
 
for obj in gc.get_objects():
    if type(obj) == Animal:
        obj.make_noise()

Code Fragment: How to programatically get MySQL table relationships

This magical chunk of goodness will allow you to query a mysql database to determine what table fields are foreign keys, and what they reference. Just replace ‘database_name’ and ‘table_name’ with the respective names you want:

1
2
3
4
5
6
7
SELECT column_name, referenced_table_name,
referenced_column_name
FROM information_schema.key_column_usage
WHERE table_schema='database_name'
AND table_name='table_name'
AND referenced_table_name IS NOT NULL
AND referenced_column_name IS NOT NULL;