Related
I have the following info stored in a dict called "info1":
[{'symbol': 'ETHUSDT', 'orderId': 480977, 'orderListId': -1, 'clientOrderId': 'S3', 'price': '0.00000000', 'origQty': '0.02000000', 'executedQty': '0.02000000', 'cummulativeQuoteQty': '25.07260000', 'status': 'FILLED', 'timeInForce': 'GTC', 'type': 'MARKET', 'side': 'BUY', 'stopPrice': '0.00000000', 'icebergQty': '0.00000000', 'time': 1672948804160, 'updateTime': 1672948804160, 'isWorking': True, 'workingTime': 1672948804160, 'origQuoteOrderQty': '0.00000000', 'selfTradePreventionMode': 'NONE'}, {'symbol': 'ETHUSDT', 'orderId': 566634, 'orderListId': -1, 'clientOrderId': 'S3', 'price': '0.00000000', 'origQty': '0.02000000', 'executedQty': '0.02000000', 'cummulativeQuoteQty': '24.98800000', 'status': 'FILLED', 'timeInForce': 'GTC', 'type': 'MARKET', 'side': 'SELL', 'stopPrice': '0.00000000', 'icebergQty': '0.00000000', 'time': 1672971274007, 'updateTime': 1672971274007, 'isWorking': True, 'workingTime': 1672971274007, 'origQuoteOrderQty': '0.00000000', 'selfTradePreventionMode': 'NONE'}]
How would I go about updating the values for both 'time' keys in this dict?
Have tried to identify the index of each, without luck.
First off, you are not dealing with a dictionary, but a list of dictionaries.
isinstance(info1, list)
#True
That is why you can simply try to fetch a key and make an update here. First, you have to iterate through the list of dictionaries and then use dict.update to update specific key values. Here is how you can do this with a simple loop -
new_times = [1672948804190, 1672971274120] #one for each dict in list
for d,t in zip(info1, new_times):
d.update({'time':t})
print(info1)
[{'symbol': 'ETHUSDT',
'orderId': 480977,
'orderListId': -1,
'clientOrderId': 'S3',
'price': '0.00000000',
'origQty': '0.02000000',
'executedQty': '0.02000000',
'cummulativeQuoteQty': '25.07260000',
'status': 'FILLED',
'timeInForce': 'GTC',
'type': 'MARKET',
'side': 'BUY',
'stopPrice': '0.00000000',
'icebergQty': '0.00000000',
'time': 1672948804190, #<---------
'updateTime': 1672948804160,
'isWorking': True,
'workingTime': 1672948804160,
'origQuoteOrderQty': '0.00000000',
'selfTradePreventionMode': 'NONE'},
{'symbol': 'ETHUSDT',
'orderId': 566634,
'orderListId': -1,
'clientOrderId': 'S3',
'price': '0.00000000',
'origQty': '0.02000000',
'executedQty': '0.02000000',
'cummulativeQuoteQty': '24.98800000',
'status': 'FILLED',
'timeInForce': 'GTC',
'type': 'MARKET',
'side': 'SELL',
'stopPrice': '0.00000000',
'icebergQty': '0.00000000',
'time': 1672971274120, #<---------
'updateTime': 1672971274007,
'isWorking': True,
'workingTime': 1672971274007,
'origQuoteOrderQty': '0.00000000',
'selfTradePreventionMode': 'NONE'}]
I use the binance.client package to interact with the Binance API. There is a need to get active futures positions. That is, positions that are open, but there were no deals to close them. Everything I could find in the documentation returns the history of transactions, that is, it is impossible to tell from them whether all positions were closed or only part of them.
client.get_all_orders(symbol)
client.get_all_margin_orders(symbol)
Return this:
[]
[]
client.futures_get_all_orders(symbol)
Returns a list containing both purchase and sale positions. Therefore, it is impossible to say with certainty for what amount there is an open position now. An example of such data:
[{'orderId': 4438, 'symbol': 'BTCUSDT', 'status': 'FILLED', 'clientOrderId': 'XY7J7nhdGPWi6mu', 'price': '0', 'avgPrice': '37179.70000', 'origQty': '0.001', 'executedQty': '0.001', 'cumQuote': '37.17870', 'timeInForce': 'GTC', 'type': 'MARKET', 'reduceOnly': False, 'closePosition': False, 'side': 'BUY', 'positionSide': 'LONG', 'stopPrice': '0', 'workingType': 'CONTRACT_PRICE', 'priceProtect': False, 'origType': 'MARKET', 'time': 164549023, 'updateTime': 164549023},
{'orderId': 3458, 'symbol': 'BTCUSDT', 'status': 'FILLED', 'clientOrderId': '1zpqZVxYgYyfD', 'price': '0', 'avgPrice': '37219.90000', 'origQty': '0.001', 'executedQty': '0.001', 'cumQuote': '37.21890', 'timeInForce': 'GTC', 'type': 'MARKET', 'reduceOnly': True, 'closePosition': False, 'side': 'SELL', 'positionSide': 'LONG', 'stopPrice': '0', 'workingType': 'CONTRACT_PRICE', 'priceProtect': False, 'origType': 'MARKET', 'time': 16454903, 'updateTime': 16454903},
{'orderId': 4396, 'symbol': 'BTCUSDT', 'status': 'FILLED', 'clientOrderId': 'iKlNyjk7yaKtBo', 'price': '0', 'avgPrice': '36854.30000', 'origQty': '0.001', 'executedQty': '0.001', 'cumQuote': '36.74630', 'timeInForce': 'GTC', 'type': 'MARKET', 'reduceOnly': False, 'closePosition': False, 'side': 'BUY', 'positionSide': 'LONG', 'stopPrice': '0', 'workingType': 'CONTRACT_PRICE', 'priceProtect': False, 'origType': 'MARKET', 'time': 164551553, 'updateTime': 164551553}]
.get_position
https://github.com/Binance-
docs/Binance_Futures_python/blob/master/example/trade/get_position.py
how can I create a list of dictionaries with those lists
temp = [['header1', '4', '8', '16', '32', '64', '128', '256', '512', '243,6'], ['media_range', '1,200', '2,400', '4,800', '4,800', '6,200', '38,400', '76,800', '153,600', '160,000'], ['speed', '300', '600', '1,200', '2,000', '2,000', '2,000', '2,000', '2,000', '2,000']]
the headers of the dictionary is the first element of the lists
the expected Output is:
output= [{'header1': '4', 'media_range': '1,200', 'speed': '300'}, {'header1': '8', 'media_range': '2,400', 'speed': '600'}, ...]
Ideally the code should handle any amount of lists (in this case 3)
IIUC
>>> temp = [['header1', '4', '8', '16', '32', '64', '128', '256', '512', '243,6'], ['media_range', '1,200', '2,400', '4,800', '4
...: ,800', '6,200', '38,400', '76,800', '153,600', '160,000'], ['speed', '300', '600', '1,200', '2,000', '2,000', '2,000', '2,0
...: 00', '2,000', '2,000']]
>>>
>>> keys = [l[0] for l in temp]
>>> values = [l[1:] for l in temp]
>>> dicts = [dict(zip(keys, sub)) for sub in zip(*values)]
>>>
>>> dicts
[{'header1': '4', 'media_range': '1,200', 'speed': '300'},
{'header1': '8', 'media_range': '2,400', 'speed': '600'},
{'header1': '16', 'media_range': '4,800', 'speed': '1,200'},
{'header1': '32', 'media_range': '4,800', 'speed': '2,000'},
{'header1': '64', 'media_range': '6,200', 'speed': '2,000'},
{'header1': '128', 'media_range': '38,400', 'speed': '2,000'},
{'header1': '256', 'media_range': '76,800', 'speed': '2,000'},
{'header1': '512', 'media_range': '153,600', 'speed': '2,000'},
{'header1': '243,6', 'media_range': '160,000', 'speed': '2,000'}]
Slightly shorter solution with zip and unpacking:
temp = [['header1', '4', '8', '16', '32', '64', '128', '256', '512', '243,6'], ['media_range', '1,200', '2,400', '4,800', '4,800', '6,200', '38,400', '76,800', '153,600', '160,000'], ['speed', '300', '600', '1,200', '2,000', '2,000', '2,000', '2,000', '2,000', '2,000']]
header, *data = zip(*temp)
result = [dict(zip(header, i)) for i in data]
Output:
[{'header1': '4', 'media_range': '1,200', 'speed': '300'}, {'header1': '8', 'media_range': '2,400', 'speed': '600'}, {'header1': '16', 'media_range': '4,800', 'speed': '1,200'}, {'header1': '32', 'media_range': '4,800', 'speed': '2,000'}, {'header1': '64', 'media_range': '6,200', 'speed': '2,000'}, {'header1': '128', 'media_range': '38,400', 'speed': '2,000'}, {'header1': '256', 'media_range': '76,800', 'speed': '2,000'}, {'header1': '512', 'media_range': '153,600', 'speed': '2,000'}, {'header1': '243,6', 'media_range': '160,000', 'speed': '2,000'}]
You could use zip(). This requires you to know how many lists but does the expected output.
for header1,media_range,speed in zip(temp[0], temp[1], temp[2]):
if header1 != "header1":
output.append({temp[0][0]: header1, temp[1][0]: media_range, temp[2][0]: speed})
Now I can use softlayer Python API to create the block storage, command like this:
client['SoftLayer_Product_Order'].placeOrder(orderData)
This command can be executed and response looks like this:
{'orderId': 11999815, 'placedOrder': {'status': 'PENDING_AUTO_APPROVAL', 'account': {'companyName': 'xxxxxx', 'id': 323716, 'brandId': 11246}, 'orderQuoteId': '', 'userRecordId': 426607, 'orderTypeId': 4, 'items': [{'itemId': 5936, 'setupFee': '0', 'promoCodeId': '', 'recurringFee': '0', 'description': 'Endurance Storage', 'laborFee': '0', 'oneTimeFee': '0', 'itemPriceId': '45064', 'children': [{'itemId': 5944, 'setupFee': '0', 'promoCodeId': '', 'recurringFee': '0', 'description': 'Block Storage', 'laborFee': '0', 'oneTimeFee': '0', 'itemPriceId': '45104', 'categoryCode': 'storage_block', 'parentId': 171180595, 'id': 171180597}, {'itemId': 5940, 'setupFee': '0', 'promoCodeId': '', 'recurringFee': '0', 'description': '2 IOPS per GB', 'laborFee': '0', 'oneTimeFee': '0', 'itemPriceId': '45084', 'categoryCode': 'storage_tier_level', 'parentId': 171180595, 'id': 171180599}, {'itemId': 5138, 'setupFee': '0', 'promoCodeId': '', 'recurringFee': '36.25', 'description': '250 GB Storage Space', 'laborFee': '0', 'oneTimeFee': '0', 'itemPriceId': '45254', 'categoryCode': 'performance_storage_space', 'parentId': 171180595, 'id': 171180601}, {'itemId': 6028, 'setupFee': '0', 'promoCodeId': '', 'recurringFee': '.36', 'description': '5 GB Storage Space (Snapshot Space)', 'laborFee': '0', 'oneTimeFee': '0', 'itemPriceId': '46136', 'categoryCode': 'storage_snapshot_space', 'parentId': 171180595, 'id': 171180603}], 'categoryCode': 'storage_service_enterprise', 'parentId': '', 'id': 171180595}, {'itemId': 5944, 'setupFee': '0', 'promoCodeId': '', 'recurringFee': '0', 'description': 'Block Storage', 'laborFee': '0', 'oneTimeFee': '0', 'itemPriceId': '45104', 'categoryCode': 'storage_block', 'parentId': 171180595, 'id': 171180597}, {'itemId': 5940, 'setupFee': '0', 'promoCodeId': '', 'recurringFee': '0', 'description': '2 IOPS per GB', 'laborFee': '0', 'oneTimeFee': '0', 'itemPriceId': '45084', 'categoryCode': 'storage_tier_level', 'parentId': 171180595, 'id': 171180599}, {'itemId': 5138, 'setupFee': '0', 'promoCodeId': '', 'recurringFee': '36.25', 'description': '250 GB Storage Space', 'laborFee': '0', 'oneTimeFee': '0', 'itemPriceId': '45254', 'categoryCode': 'performance_storage_space', 'parentId': 171180595, 'id': 171180601}, {'itemId': 6028, 'setupFee': '0', 'promoCodeId': '', 'recurringFee': '.36', 'description': '5 GB Storage Space (Snapshot Space)', 'laborFee': '0', 'oneTimeFee': '0', 'itemPriceId': '46136', 'categoryCode': 'storage_snapshot_space', 'parentId': 171180595, 'id': 171180603}], 'accountId': 323716, 'userRecord': {'username': 'xxxx', 'lastName': 'xxxx', 'id': 426607, 'firstName': 'xxxx', 'accountId': xxxx}, 'id': 11999815, 'presaleEventId': ''}, 'orderDate': '2017-01-04T00:42:21-08:00', 'orderDetails': {'preTaxSetup': '0', 'storageGroups': [], 'postTaxRecurring': '36.61', 'billingOrderItemId': '', 'presetId': '', 'prices': [{'itemId': 5936, 'setupFee': '0', 'recurringFee': '0', 'laborFee': '0', 'oneTimeFee': '0', 'item': {'thirdPartyPolicyAssignments': [], 'capacity': '0', 'description': 'Endurance Storage', 'bundle': [], 'keyName': 'CODENAME_PRIME_STORAGE_SERVICE', 'units': 'N/A', 'id': 5936}, 'id': 45064, 'categories': [{'categoryCode': 'storage_service_enterprise', 'id': 394, 'name': 'Endurance'}]}, {'itemId': 5944, 'setupFee': '0', 'recurringFee': '0', 'laborFee': '0', 'oneTimeFee': '0', 'item': {'thirdPartyPolicyAssignments': [], 'capacity': '0', 'description': 'Block Storage', 'bundle': [], 'keyName': 'BLOCK_STORAGE_2', 'units': 'N/A', 'id': 5944}, 'id': 45104, 'categories': [{'categoryCode': 'storage_block', 'id': 398, 'name': 'Block Storage'}]}, {'itemId': 5940, 'setupFee': '0', 'recurringFee': '0', 'laborFee': '0', 'oneTimeFee': '0', 'item': {'thirdPartyPolicyAssignments': [], 'capacity': '0', 'description': '2 IOPS per GB', 'bundle': [], 'keyName': 'READHEAVY_TIER', 'units': 'N/A', 'id': 5940}, 'id': 45084, 'categories': [{'categoryCode': 'storage_tier_level', 'id': 396, 'name': 'Storage Tier Level'}]}, {'itemId': 5138, 'setupFee': '0', 'recurringFee': '36.25', 'laborFee': '0', 'oneTimeFee': '0', 'item': {'thirdPartyPolicyAssignments': [], 'capacity': '250', 'description': '250 GB Storage Space', 'bundle': [], 'keyName': '250_GB_PERFORMANCE_STORAGE_SPACE', 'units': 'GB', 'id': 5138}, 'id': 45254, 'categories': [{'categoryCode': 'performance_storage_space', 'id': 382, 'name': 'Storage Space'}]}, {'itemId': 6028, 'setupFee': '0', 'recurringFee': '.36', 'laborFee': '0', 'oneTimeFee': '0', 'item': {'thirdPartyPolicyAssignments': [], 'capacity': '5', 'description': '5 GB Storage Space', 'bundle': [], 'keyName': '5_GB_STORAGE_SPACE', 'units': 'GB', 'id': 6028}, 'id': 46136, 'categories': [{'categoryCode': 'storage_snapshot_space', 'id': 402, 'name': 'Storage Snapshot Space'}]}], 'sendQuoteEmailFlag': '', 'packageId': 240, 'useHourlyPricing': False, 'preTaxRecurringMonthly': '36.61', 'message': '', 'preTaxRecurring': '36.61', 'billingInformation': {'billingNameFirst': 'xxxxx', 'billingPostalCode': 'xxxxxx', 'billingPhoneVoice': 'xxxxx', 'billingNameCompany': 'xxxxxx', 'billingAddressLine1': 'xxxxx', 'cardExpirationMonth': '', 'billingNameLast': 'Urbisci', 'cardExpirationYear': '', 'billingState': 'xxxx', 'billingCountryCode': 'xxxx', 'billingEmail': 'xxxxxx', 'taxExempt': 0, 'billingCity': 'San Jose'}, 'primaryDiskPartitionId': '', 'locationObject': {'id': 1004995, 'name': 'sjc03', 'longName': 'San Jose 3'}, 'taxCompletedFlag': True, 'isManagedOrder': '', 'originVolumeScheduleId': '', 'imageTemplateId': '', 'postTaxRecurringMonthly': '36.61', 'resourceGroupTemplateId': '', 'postTaxSetup': '0', 'sshKeys': [], 'location': '1004995', 'stepId': '', 'proratedInitialCharge': '33.07', 'totalRecurringTax': '0', 'originVolumeId': '', 'osFormatType': {'createDate': '', 'keyName': 'LINUX', 'id': 12}, 'paymentType': 'ADD_TO_BALANCE', 'resourceGroupId': '', 'sourceVirtualGuestId': '', 'bigDataOrderFlag': False, 'properties': [], 'extendedHardwareTesting': '', 'preTaxRecurringHourly': '0', 'postTaxRecurringHourly': '0', 'taxCacheHash': '97601ef55aa1f213ad046476769b64f57ce4d1ab', 'currencyShortName': 'USD', 'itemCategoryQuestionAnswers': [], 'containerSplHash': '000000001fe660de00007f62a57eefc9', 'proratedOrderTotal': '33.07', 'serverCoreCount': '', 'privateCloudOrderFlag': False, 'totalSetupTax': '0', 'quantity': 1}}
The problem is the response does not provide any useful info to identify the storage, such as storage id or storage name.
I know the API to get all storage info:
client['SoftLayer_Account'].getIscsiNetworkStorage()
But I can't identify which one I just created(storage name is automated, created by API), check the created time is not a good idea.
The response from SoftLayer_Product_Order::placeOrder method contains an orderId property
'orderId': 11999815
We can find the storage through this, try this script for it:
"""
Get storage through orderId
Important manual pages:
http://sldn.softlayer.com/reference/services/SoftLayer_Account/getNetworkStorage
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Network_Storage
http://sldn.softlayer.com/article/object-filters
License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <sldn#softlayer.com>
"""
import SoftLayer
"""
Your SoftLayer API username and key.
"""
USERNAME = 'set me'
API_KEY = 'set me'
# Define the orderId from block storage
orderId = 11999815
# Declare a new API service object
client = SoftLayer.Client(username=USERNAME, api_key=API_KEY)
accountService = client['Account']
# Define an object filter to get storage through orderId
objectFilter = {"iscsiNetworkStorage": {"billingItem": {"orderItem": {"order": {"id": {"operation": orderId}}}}}}
try:
result = accountService.getIscsiNetworkStorage(filter=objectFilter)
print(result)
except SoftLayer.SoftLayerAPIError as e:
print("Error. "
% (e.faultCode, e.faultString))
I hope it helps
Okay so I've been working on processing some annotated text output. What I have so far is a dictionary with annotation as key and relations an array of elements:
'Adenotonsillectomy': ['0', '18', '1869', '1716'],
'OSAS': ['57', '61'],
'apnea': ['41', '46'],
'can': ['94', '97', '1796', '1746'],
'deleterious': ['103', '114'],
'effects': ['122', '129', '1806', '1752'],
'for': ['19', '22'],
'gain': ['82', '86', '1776', '1734'],
'have': ['98', '102', ['1776 1786 1796 1806 1816'], '1702'],
'health': ['115', '121'],
'lead': ['67', '71', ['1869 1879 1889'], '1695'],
'leading': ['135', '142', ['1842 1852'], '1709'],
'may': ['63', '66', '1879', '1722'],
'obesity': ['146', '153'],
'obstructive': ['23', '34'],
'sleep': ['35', '40'],
'syndrome': ['47', '55'],
'to': ['143', '145', '1852', '1770'],
'weight': ['75', '81'],
'when': ['130', '134', '1842', '1758'],
'which': ['88', '93', '1786', '1740']}
What I want to do is sort this by the first element in the array and reorder the dict as:
'Adenotonsillectomy': ['0', '18', '1869', '1716']
'for': ['19', '22'],
'obstructive': ['23', '34'],
'sleep': ['35', '40'],
'apnea': ['41', '46'],
etc...
right now I've tried to use operator to sort by value:
sorted(dependency_dict.items(), key=lambda x: x[1][0])
However the output I'm getting is still incorrect:
[('Adenotonsillectomy', ['0', '18', '1869', '1716']),
('deleterious', ['103', '114']),
('health', ['115', '121']),
('effects', ['122', '129', '1806', '1752']),
('when', ['130', '134', '1842', '1758']),
('leading', ['135', '142', ['1842 1852'], '1709']),
('to', ['143', '145', '1852', '1770']),
('obesity', ['146', '153']),
('for', ['19', '22']),
('obstructive', ['23', '34']),
('sleep', ['35', '40']),
('apnea', ['41', '46']),
('syndrome', ['47', '55']),
('OSAS', ['57', '61']),
('may', ['63', '66', '1879', '1722']),
('lead', ['67', '71', ['1869 1879 1889'], '1695']),
('weight', ['75', '81']),
('gain', ['82', '86', '1776', '1734']),
('which', ['88', '93', '1786', '1740']),
('can', ['94', '97', '1796', '1746']),
('have', ['98', '102', ['1776 1786 1796 1806 1816'], '1702'])]
I'm not sure whats going wrong. Any help is appreciated.
The entries are sorted in alphabetical order. If you want to sort them on integer value, convert the value to int first:
sorted(dependency_dict.items(), key=lambda x: int(x[1][0]))