Advertisement
Guest User

Untitled

a guest
Apr 21st, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 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='хххххххххх' user='ххххххххххх'" \
  11.                             " host='хххххххх' password='ххххххххххх'")
  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. data_dict = []
  23. for row in data:
  24.     data_dict.append(dict(row))
  25.  
  26. class Account:
  27.    
  28.     id = 0
  29.     parent_id = 0
  30.     lvl = 0
  31.    
  32.     def __init__(self, i, p):
  33.         self.id = i
  34.         self.parent_id = p
  35.  
  36.        
  37. cLvlAccs = [0]
  38. cLvl = 1
  39. while len(cLvlAccs) > 0:
  40.     newLvlAccs = []
  41.     for acc in data_dict:
  42.         if (cLvlAccs.count(acc.parent_id) > 0):
  43.             acc.lvl = cLvl;
  44.             newLvlAccs.append(acc.id)
  45.     cLvlAccs = newLvlAccs
  46.     cLvl += 1
  47.  
  48. for acc in data_dict:
  49.     print "{0} - {1}".format(acc.id, acc.lvl)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement