Woobinda

Delete object by ID from node

Jan 26th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.14 KB | None | 0 0
  1. exists_strategy = {'type': 'CHAIN', 'id': '00000000-0000-0000-0000-000000000001', 'payload': {'actions': [{'type': 'TRAILING_SELL', 'id': '00000000-0000-0000-0000-000000000002', 'payload': {'quantityUnits': 'percent', 'distance': 3, 'exchange': 'binance', 'market': 'USDT-BTC', 'distanceUnits': 'percent', 'quantity': 100}}, {'type': 'TRAILING_BUY', 'id': '00000000-0000-0000-0000-000000000003', 'payload': {'quantityUnits': 'basePercent', 'distance': 3, 'exchange': 'binance', 'market': 'USDT-BTC', 'distanceUnits': 'percent', 'quantity': 100}}, {'type': 'OR', 'id': '00000000-0000-0000-0000-000000000004', 'payload': {'actions': [{'type': 'CHAIN', 'id': '00000000-0000-0000-0000-000000000005', 'payload': {'actions': [{'type': 'STOP_LOSS', 'id': '00000000-0000-0000-0000-000000000006', 'payload': {'quantityUnits': 'percent', 'quantity': 100, 'stopRate': 3500, 'exchange': 'binance', 'market': 'USDT-BTC'}}]}}, {'type': 'CHAIN', 'id': '00000000-0000-0000-0000-000000000007', 'payload': {'actions': [{'type': 'WAIT_FOR_RATE', 'id': '00000000-0000-0000-0000-000000000008', 'payload': {'rate': 4500, 'exchange': 'binance', 'market': 'USDT-BTC', 'operator': '>='}}, {'type': 'TRAILING_SELL', 'id': '00000000-0000-0000-0000-000000000009', 'payload': {'quantityUnits': 'percent', 'distance': 5, 'exchange': 'binance', 'market': 'USDT-BTC', 'distanceUnits': 'percent', 'quantity': 100}}, {'type': 'AND', 'id': '00000000-0000-0000-0000-000000000010', 'payload': {'actions': [{'type': 'CHAIN', 'id': '00000000-0000-0000-0000-000000000011', 'payload': {'actions': [{'type': 'MARKET_BUY', 'id': '00000000-0000-0000-0000-000000000012', 'payload': {'quantityUnits': 'baseCoin', 'quantity': 100, 'exchange': 'binance', 'market': 'USDT-BTC'}}, {'type': 'MARKET_SELL', 'id': '00000000-0000-0000-0000-000000000013', 'payload': {'quantityUnits': 'percent', 'quantity': 100, 'exchange': 'binance', 'market': 'USDT-BTC'}}]}}, {'type': 'CHAIN', 'id': '00000000-0000-0000-0000-000000000014', 'payload': {'actions': [{'type': 'LIMIT_BUY', 'id': '00000000-0000-0000-0000-000000000015', 'payload': {'rate': 4500, 'quantity': 100, 'exchange': 'binance', 'market': 'USDT-BTC', 'quantityUnits': 'baseCoin'}}, {'type': 'LIMIT_SELL', 'id': '00000000-0000-0000-0000-000000000016', 'payload': {'rate': 4500, 'quantity': 100, 'exchange': 'binance', 'market': 'USDT-BTC', 'quantityUnits': 'percent'}}]}}]}}]}}]}}]}}
  2.  
  3. import copy
  4.  
  5.  
  6. def delete_chunk_by_id(exists_strategy, chunk_id):
  7.     strategy = copy.deepcopy(exists_strategy)
  8.  
  9.     if chunk_id == strategy.get('id'):
  10.         return "Can't delete main node"
  11.  
  12.     def check_and_delete_chunk(chunk):
  13.  
  14.         for i, payload in enumerate(chunk):
  15.  
  16.             if payload.get('id') == chunk_id:
  17.                 return chunk.pop(i)
  18.  
  19.             if payload.get('payload').get('actions'):
  20.                 deleted_chunk = check_and_delete_chunk(payload.get('payload').get('actions'))
  21.                 if deleted_chunk:
  22.                     return deleted_chunk
  23.  
  24.     chunk = strategy.get('payload').get('actions')
  25.     result = check_and_delete_chunk(chunk)
  26.     return strategy if result else "ID not found"
  27.  
  28.  
  29. print(delete_chunk_by_id(strategy, chunk_id='00000000-0000-0000-0000-000000000002'))
Add Comment
Please, Sign In to add comment