Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Prereq.
- # sudo apt-get install libpq-dev
- # sudo pip3 install psycopg2
- # -*- coding: utf-8 -*-
- import psycopg2
- import psycopg2.extras
- sql = "select * from accounts"
- try:
- conn = psycopg2.connect("dbname='хххххххх' user='ххххххххххх'" \
- " host='хххххххххххх' password='ххххххххххххх'")
- except psycopg2.Error as err:
- print("Connection error: {}".format(err))
- try:
- cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
- cur.execute(sql)
- data = cur.fetchall()
- except psycopg2.Error as err:
- print("Query error: {}".format(err))
- data_dict = []
- for row in data:
- data_dict.append(dict(row))
- def get_ib_level(level, account_id, parent_id, count, list):
- if isinstance(list[count].get('parent_id'), int):
- parent_account_id = any((t_dict.get['account_id'] == parent_id and t_dict.get['parent_id'] == account_id) for t_dict in list)
- parent_parent_id = any((t_dict.get['parent_id'] == account_id and t_dict.get['account_id'] == parent_id) for t_dict in list)
- level += 1
- get_ib_level(level, parent_account_id, parent_parent_id, count, list)
- elif list[count].get('parent_id') is None:
- return level
- start_count = 0
- for data_dict[start_count] in data_dict:
- if data_dict[start_count].get('parent_id') is None:
- ib_level = 0
- data_dict[start_count].update({'IB_level': ib_level})
- print('IB level 0:\n')
- print(data_dict[start_count])
- elif isinstance(data_dict[start_count].get('parent_id'), int):
- ib_level = get_ib_level(
- 0,
- data_dict[start_count].get('account_id'),
- data_dict[start_count].get('parent_id'),
- start_count,
- data_dict
- )
- data_dict[start_count].update({'IB_level': ib_level})
- print('IB level 1:\n')
- print(data_dict[start_count])
- start_count = start_count + 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement