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='xxxxx' user='xxxxx'" \
- " host='ps.xxxxx.com' password='xxxxxxxx'")
- 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_parent_id(a_id):
- for dct in data_dict:
- if dct['account_id'] == a_id:
- return dct['parent_id']
- def get_ib_level(level, account_id, parent_id):
- if isinstance(parent_id, int):
- next_account_id = parent_id
- next_parent_id = get_parent_id(next_account_id)
- print('%s;%s;%s' % (level, next_account_id, next_parent_id))
- level += 1
- level = get_ib_level(level, account_id, next_parent_id)
- return level
- elif parent_id == 'None':
- return 0
- 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 - %s \n' % data_dict[start_count].get('ib_number'))
- 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')
- )
- data_dict[start_count].update({'IB_level': ib_level})
- print('IB level : %s1 \n' % ib_level)
- start_count = start_count + 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement