0

I trying to generate charts for the last four weeks from the specified week number(week by week)

So far I have models and view working fine, I able to query data from raw database Also I know how to create four list from query:

results = [i for i in chain.from_iterable(cursor.fetchall())]

[u'AAA', 191L, u'BBB', 21L, u'CCC', 420L, u'DDD', 150L, u'EEE', 759L, u'FFF', 290L, u'GGG', 166L, u'HHH', 8L, u'III', 1147L, u'JJJ', 1427L, u'KKK', 34L, u'LLL', 8L]
[u'AAA', 191L, u'BBB', 21L, u'CCC', 420L, u'DDD', 150L, u'EEE', 759L, u'FFF', 290L, u'GGG', 166L, u'HHH', 8L, u'III', 1147L, u'JJJ', 1427L, u'KKK', 34L, u'LLL', 8L]
[u'AAA', 191L, u'BBB', 21L, u'CCC', 420L, u'DDD', 150L, u'EEE', 759L, u'FFF', 290L, u'GGG', 166L, u'HHH', 8L, u'III', 1147L, u'JJJ', 1427L, u'KKK', 34L, u'LLL', 8L]
[u'AAA', 191L, u'BBB', 21L, u'CCC', 420L, u'DDD', 150L, u'EEE', 759L, u'FFF', 290L, u'GGG', 166L, u'HHH', 8L, u'III', 1147L, u'JJJ', 1427L, u'KKK', 34L, u'LLL', 8L]

but I still unable to merge one list from this four lists, on the end the list should look like is below:

[["ZZZ", "AAA", "BBB", "CCC"], ["48", 21, 223, 232], ["47", 334, 343, 232], ["46", 345, 542, 245], ["45", 764, 463, 989]]

Where: ZZZ = Week number "48", "47", "46", "45" AAA, BBB, CCC.... = name from query

any suggestion?

my models.py

def four_weeks(year, week):
    end = datetime(year, 1, 1) + relativedelta(weeks=week-1, weekday=SU)
    start = end - relativedelta(weeks=4, weekday=MO)
    mint, maxt = datetime.min.time(), datetime.max.time()
    for dt in rrule(WEEKLY, start, count=4):
        yield dt.combine(dt, mint), dt.combine(dt + timedelta(days=6), maxt)

views.py

def WeekCombo(request):
    fweeks = []
    year = 2013 #this is only for test
    week = 48   #in future it will be integrated with form
    cursor = connections['mydba'].cursor()
    for start, end in four_weeks(year, week):
        cursor.execute("SELECT DISTINCT (p.name) AS platform, count(e.id ) AS count FROM event e, lu_platform p WHERE e.platform_id = p.id AND e.sourcetype_id = 1 AND e.event_datetime BETWEEN %s AND %s AND e.sender_id  NOT IN ( 759, 73 ) GROUP BY p.name ORDER BY p.name", [start, end] )
        results = [i for i in chain.from_iterable(cursor.fetchall())]
        fweeks.append(results)
    return render_to_response('form.html', {'fweeks': fweeks}, context_instance=RequestContext(request))
Robert
  • 651
  • 1
  • 6
  • 18
  • Sounds like you want to merge the resulting lists. http://stackoverflow.com/questions/1720421/merge-two-lists-in-python – Andreas Klebinger Dec 12 '13 at 13:04
  • 1
    It's not entirely clear what your question is. Do you want to merge the results as a dictionary? {'AAA' : (17L, 44L, 76L), 'BBB' : (21L, 96L, 225L, 304L).... ? – Mzzl Dec 12 '13 at 13:06
  • Why you are not using django models? – scriptmonster Dec 12 '13 at 14:22
  • I don’t have much experience with django/python and this is a external database I using connection "from django.db import connections" to make query. Data base is full of statistic (u'Name', valueL) and I will create charts for this. In this case I will combine four weeks back in one google Combochart https://google-developers.appspot.com/chart/interactive/docs/gallery/combochart – Robert Dec 12 '13 at 14:58

1 Answers1

0

Sounds like you want to merge the resulting lists.

How to append list to second list (concatenate lists)

You can create a empty list and extend it by the results of each query step by step.

If you want to run all queries first and merge the lists afterwards use itertools.chain

>>> listoflists = [['a'], ['b', 'c'], ['d', 'e', 'f']]
>>> [i for i in itertools.chain.from_iterable(listoflists)]
['a', 'b', 'c', 'd', 'e', 'f']
Community
  • 1
  • 1