Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import psycopg2
  4. import psycopg2.extras
  5.  
  6. sql = "select * from accounts"
  7.  
  8.  
  9. try:
  10.     conn = psycopg2.connect("dbname='rebate' user='postgresadmin'" \
  11.                             " host='pg-l.cuboidlogic.com' password='P3sdfi334JKd3JxmopfjLIVE1'")
  12. except psycopg2.Error as err:
  13.     print("Connection error: {}".format(err))
  14.  
  15. try:
  16.     cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
  17.     cur.execute(sql)
  18.     data = cur.fetchall()
  19. except psycopg2.Error as err:
  20.     print("Query error: {}".format(err))
  21.  
  22.    
  23. data_dict = []
  24. for row in data:
  25.     data_dict.append(dict(row))
  26.    
  27.  
  28. cLvlAccs = [0]
  29. cLvl = 1
  30. while len(cLvlAccs) > 0:
  31.     newLvlAccs = []
  32.     for acc in data_dict:
  33.         if (cLvlAccs.count(acc.parent_id) > 0):
  34.             acc.lvl = cLvl;
  35.             newLvlAccs.append(acc.id)
  36.     cLvlAccs = newLvlAccs
  37.     cLvl += 1
  38.  
  39. for acc in data_dict:
  40.     print "{0} - {1}".format(acc.id, acc.lvl)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement