0

I have two scripts, qyeryfunction.py and generatecharts.py. I would like to get the results of the week_number variable from queryfunction and put them in weeks in generatecharts.py. How can I do this?

#queryfunction.py
import MySQLdb

try:
    db = MySQLdb.connect("localhost","myusr","mypass","mydb")
except MySQLdb.DatabaseError:
    print 'Cant connect to database, check if MySQL service is running'

class WeekQuery:
    def __init__(self, name):
        self.name = name

    def query(self):
        try:
            cursor = db.cursor()
            cursor.callproc(self)
            results = cursor.fetchall()
            week_number = [i[0] for i in cursor.description]
        except MySQLdb.ProgrammingError:
            print 'Data unavailable'
            cursor.close()
        return results

and

#generatecharts.py
from queryfunction import WeekQuery, query

#class GenerateCharts():

n = WeekQuery('my_first_query')
g = WeekQuery('my_second_query')

first_data = query(n.name)
second_data = query(g.name)
weeks = ???

1 Answer 1

3

You can just return the week_number variable from the query function in queryfunction.py as well:

return (results, week_number)

And you'd have to change generatecharts.py slightly to accept a tuple as the return value. This will assign results to first_data and week_number to weeks:

first_data, weeks = query(n.name)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your help, I didn't expect that it will be as easy.
No worries, glad it helped :^)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.