Retrieve IP from json with fabric - python

Hi I have a json something like below i want to retrieve ip_address & store it in any variable in fabric.And if my ip_address (v4) field is array how can i use different ip set automatically on my [env.host] variable.
{
"droplet":{
"id":16549167,
"name":"test",
"memory":512,
"vcpus":1,
"disk":20,
"locked":false,
"status":"active",
"kernel":null,
"created_at":"2016-06-03T07:01:59Z",
"features":[
"ipv6",
"virtio"
],
"next_backup_window":null,
"snapshot_ids":[
],
"image":{
"id":17154032,
"name":"14.04.4 x64",
"distribution":"Ubuntu",
"slug":"ubuntu-14-04-x64",
"public":true,
"regions":[
"nyc1",
"tor1",
"sfo2",
"blr1"
],
"created_at":"2016-05-04T22:25:47Z",
"min_disk_size":20,
"type":"snapshot",
"size_gigabytes":19.68
},
"size":{
"slug":"512mb",
"memory":512,
"available":true
},
"networks":{
"v4":[
{
"ip_address":"104.236.223.78",
"netmask":"255.255.192.0",
"gateway":"104.236.192.1",
"type":"public"
}
],
"v6":[
{
"ip_address":"260:A880:08:001",
"netmask":64,
"gateway":"2604:800:0010:0000:0000:0000:0001",
"type":"public"
}
]
}

Related

How to use conditionals on Uniswap GraphQL API pool method?

So I'm new to graphQL and I've been figuring out the Uniswap API, through the sandbox browser, but I'm running this program which just gets metadata on the top 100 tokens and their relative pools, but the pool one isn't working at all. I'm trying to put two conditions of if token0's hash is this and token1's hash is this, it should output the pool of those two, however if only outputs pools with the token0 hash, and just ignores the second one. I've tried using and, _and, or two where's seperated by {} or , so on so forth. This is an example I have (python btw):
class ExchangePools:
def QueryPoolDB(self, hash1, hash2):
query = """
{
pools(where: {token0: "%s"}, where: {token1:"%s"}, first: 1, orderBy:volumeUSD, orderDirection:desc) {
id
token0 {
id
symbol
}
token1 {
id
symbol
}
token1Price
}
}""" % (hash1, hash2)
return query
or in the sandbox explorer this:
{
pools(where: {token0: "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599"} and: {token1:"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"}, first: 1, orderBy:volumeUSD, orderDirection:desc) {
id
token0 {
id
symbol
}
token1 {
id
symbol
}
token1Price
}
}
with this output:
{
"data": {
"pools": [
{
"id": "0x4585fe77225b41b697c938b018e2ac67ac5a20c0",
"token0": {
"id": "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599",
"symbol": "WBTC"
},
"token1": {
"id": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"symbol": "WETH"
},
"token1Price": "14.8094450357546760737720184457113"
}
]
}
}
How can I get the API to register both statements?

How to query and update nested JSON data in Django

I have below class defined to do statistics for voting system.
class FormPage(AbstractForm):
submission_stats = models.JSONField(null=True, editable=False)
Now, I have submission_stats in below format:
[
{
"id":4,
"label":"checkboxes",
"choices":[
{
"content":"option-A",
"num_vote":0,
"user_list":[
]
},
{
"content":"option-B",
"num_vote":0,
"user_list":[
]
},
{
"content":"option-C",
"num_vote":0,
"user_list":[
]
}
]
},
{
"id":7,
"label":"Radio Button",
"choices":[
{
"content":"option-1",
"num_vote":0,
"user_list":[
]
},
{
"content":"option-2",
"num_vote":0,
"user_list":[
]
},
{
"content":"option-3",
"num_vote":0,
"user_list":[
]
}
]
}
]
When I receive 2 vote submissions for example, I want to update num_vote (from 0 to 2) and user_list (from [] to [user_a, user_b]) field in this JSONField accordingly.
How to query and update elements in nested JSONField data please ?
install json library
pip install json
import json
data = json.load(submission_stats)
now this data is python dictionary. update it however you want
to save updated data back to json field, convert dictionary back to json like this
json.dumps(data, indent = 4)
and save
plus if you want to use sql with your this project i highly recomment you use
from django_extensions.db.fields.json import JSONField
instead of
models.JSONField
because models.JSONField doesn't go well with SQL

Returning parent json key after filtering for a specific key:value pair

I am call some API that returns a large API block. I need some help figuring out the most efficient way of filtering json. I've only ever interacted with specific sets or fields but not identifying a single key:value pair and then finding the parent. Once I find the parent, i want to only return the parent block. How would I go about doing it? I'm also trying to understand what kind of logic to use here.
Sample Block below. I would want to find 'itemtype:19' and then return '86' and then once I have '86' i can grab the values i need from 86. Example [86][itemDefinition][name].
},
"86":{
"saleStatus":8,
"failureIndexes":[
3
],
"augments":0,
"vendorItemIndex":86,
"itemHash":1476367343,
"quantity":1,
"costs":[
{
"itemHash":4046539562,
"quantity":10
}
],
"itemDefinition":{
"displayProperties":{
"description":"",
"name":"Sword Ammo Finder",
"icon":"/common/destiny2_content/icons/793bc0219a88f4454e413217628c40f0.png",
"iconSequences":[
{
"frames":[
"/common/destiny2_content/icons/793bc0219a88f4454e413217628c40f0.png"
]
},
{
"frames":[
"/common/destiny2_content/icons/e0d0166080ae8c0630338738ac6cff04.png"
]
}
],
"hasIcon":True
},
"tooltipNotifications":[
],
"collectibleHash":1263137395,
"backgroundColor":{
"colorHash":0,
"red":0,
"green":0,
"blue":0,
"alpha":0
},
"itemTypeDisplayName":"Helmet Armor Mod",
"uiItemDisplayStyle":"ui_display_style_energy_mod",
"itemTypeAndTierDisplayName":"Common Helmet Armor Mod",
"displaySource":"",
"tooltipStyle":"build",
"action":{
"verbName":"Dismantle",
"verbDescription":"",
"isPositive":False,
"requiredCooldownSeconds":0,
"requiredItems":[
],
"progressionRewards":[
],
"actionTypeLabel":"shard",
"rewardSheetHash":0,
"rewardItemHash":0,
"rewardSiteHash":0,
"requiredCooldownHash":0,
"deleteOnAction":True,
"consumeEntireStack":False,
"useOnAcquire":False
},
"inventory":{
"maxStackSize":1,
"bucketTypeHash":2422292810,
"recoveryBucketTypeHash":0,
"tierTypeHash":3340296461,
"isInstanceItem":False,
"nonTransferrableOriginal":False,
"tierTypeName":"Common",
"tierType":2,
"expirationTooltip":"",
"expiredInActivityMessage":"",
"expiredInOrbitMessage":"",
"suppressExpirationWhenObjectivesComplete":True
},
"plug":{
"insertionRules":[
],
"plugCategoryIdentifier":"enhancements.v2_head",
"plugCategoryHash":2912171003,
"onActionRecreateSelf":False,
"actionRewardSiteHash":0,
"actionRewardItemOverrideHash":0,
"insertionMaterialRequirementHash":1564811296,
"previewItemOverrideHash":0,
"enabledMaterialRequirementHash":0,
"enabledRules":[
],
"uiPlugLabel":"",
"plugStyle":0,
"plugAvailability":0,
"alternateUiPlugLabel":"",
"alternatePlugStyle":0,
"isDummyPlug":False,
"energyCost":{
"energyCost":2,
"energyTypeHash":728351493,
"energyType":1
}
},
"acquireRewardSiteHash":0,
"acquireUnlockHash":0,
"investmentStats":[
{
"statTypeHash":3779394102,
"value":2,
"isConditionallyActive":False
}
],
"perks":[
{
"requirementDisplayString":"",
"perkHash":3553498179,
"perkVisibility":0
}
],
"allowActions":True,
"doesPostmasterPullHaveSideEffects":False,
"nonTransferrable":True,
"itemCategoryHashes":[
59,
4104513227,
1362265421
],
"specialItemType":0,
"itemType":19,
"itemSubType":0,
"classType":3,
"breakerType":0,
"equippable":False,
"defaultDamageType":0,
"isWrapper":False,
"hash":1476367343,
"index":8169,
"redacted":False,
"blacklisted":False
}
},
"170":{
"saleStatus":1,
"failureIndexes":[
],
"augments":0,
"vendorItemIndex":170,
"itemHash":2979281381,
"quantity":1,
"costs":[
{
"itemHash":3853748946,
"quantity":1
},
{
"itemHash":1022552290,
"quantity":10
},
{
"itemHash":2014411539,
"quantity":25
},
{
"itemHash":3159615086,
"quantity":5000
}
],
"overrideNextRefreshDate":"2020-09-28T17:00:00Z",
"itemDefinition":{
"displayProperties":{
"description":"A collection of universal components that can be used to infuse power between gear items. Can be purchased from the Gunsmith or acquired from special reward sources.",
"name":"Upgrade Module",
"icon":"/common/destiny2_content/icons/62b016b25d8589f2b15c38890d2b24c3.jpg",
"hasIcon":True
},
"tooltipNotifications":[
],
"backgroundColor":{
"colorHash":0,
"red":94,
"green":12,
"blue":142,
"alpha":255
},
"itemTypeDisplayName":"Material",
"uiItemDisplayStyle":"",
"itemTypeAndTierDisplayName":"Legendary Material",
"displaySource":"",
"action":{
"verbName":"Discard",
"verbDescription":"",
"isPositive":False,
"requiredCooldownSeconds":0,
"requiredItems":[
],
"progressionRewards":[
],
"rewardSheetHash":0,
"rewardItemHash":0,
"rewardSiteHash":0,
"requiredCooldownHash":0,
"deleteOnAction":True,
"consumeEntireStack":False,
"useOnAcquire":False
},
"inventory":{
"stackUniqueLabel":"v460.materials.currencies.infusion_token",
"maxStackSize":25,
"bucketTypeHash":1469714392,
"recoveryBucketTypeHash":215593132,
"tierTypeHash":4008398120,
"isInstanceItem":False,
"nonTransferrableOriginal":False,
"tierTypeName":"Legendary",
"tierType":5,
"expirationTooltip":"",
"expiredInActivityMessage":"",
"expiredInOrbitMessage":"",
"suppressExpirationWhenObjectivesComplete":True
},
"acquireRewardSiteHash":0,
"acquireUnlockHash":0,
"investmentStats":[
],
"perks":[
],
"allowActions":True,
"doesPostmasterPullHaveSideEffects":False,
"nonTransferrable":True,
"itemCategoryHashes":[
40,
52
],
"specialItemType":0,
"itemType":0,
"itemSubType":0,
"classType":3,
"breakerType":0,
"equippable":False,
"defaultDamageType":0,
"isWrapper":False,
"hash":2979281381,
"index":10741,
"redacted":False,
"blacklisted":False
}
},

How to trim JSON array to get specific value

So I am using the Echo Arena API Which gives me some of the following in JSON Format. I am trying to get all of the NAMES of users in the match at the time, as seen here there is player name: rnedds and further down DarkCobra866. How can I get just the names and none of the other information? Using Python 3.
{
"teams":[
{
"players":[
{
"name":"rnedds",
"rhand":[
-3.3230002,
-1.2370001,
-18.701
],
"playerid":0,
"position":[
-2.7520001,
-0.96800005,
-18.622002
],
"lhand":[
-2.414,
-1.5630001,
-18.487001
],
"userid":1663152230440088,
"stats":{ }
},
{
"name":"DarkCobra866",
"rhand":[
-5.3710003,
-1.978,
-7.5110002
],
"playerid":4,
"position":[
-5.5280004,
-1.3520001,
-7.4040003
],
"lhand":[
-5.6520004,
-1.7540001,
-7.4020004
],
"userid":2649496045086049,
"stats":{ }
}
]
}
]
}
Currently, my code looks like this for other information in the API
matchdetails = {
'echosessionid' : data['sessionid'],
'echoclientname' : data['client_name'],
'echogameclockdisplay' : data['game_clock_display'],
'echogamestatus' : data['game_status']
}
currentMatchDetails = json.dumps(matchdetails)
Load your JSON string into a dictionary like this:
import json
json_text = '''
{
"teams":[
{
"players":[
{
"name":"rnedds",
"rhand":[
-3.3230002,
-1.2370001,
-18.701
],
"playerid":0,
"position":[
-2.7520001,
-0.96800005,
-18.622002
],
"lhand":[
-2.414,
-1.5630001,
-18.487001
],
"userid":1663152230440088,
"stats":{ }
},
{
"name":"DarkCobra866",
"rhand":[
-5.3710003,
-1.978,
-7.5110002
],
"playerid":4,
"position":[
-5.5280004,
-1.3520001,
-7.4040003
],
"lhand":[
-5.6520004,
-1.7540001,
-7.4020004
],
"userid":2649496045086049,
"stats":{ }
}
]
}
]
}
'''
data = json.loads(json_text)
players = [player['name'] for team in data['teams'] for player in team['players']]
print(players)
The above code will result in:
['rnedds', 'DarkCobra866']

Why my index fields are still shown as "analyzed" even after i index em as "not_analyzed"

I have a lot of data (json format) in Amazon SQS. I basically have a simple python script which pulls data from the SQS queue & then indexes it in ES. My problem is even though i have specified in my script to index as "not_analyzed", i still see my index filed as "analyzed" in index setting of kibana4 dashboard
Here is my python code :
doc = {
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"type_name": {
"dynamic_templates": [
{
"strings": {
"match_mapping_type": "string",
"mapping": {
"type": "string",
"index": "not_analyzed"
}
}
}
]
}
}
}
es = Elasticsearch()
h = { "Content-type":"application/json" }
res = requests.request("POST","http://localhost:9200/"+index_name+"/",headers=h,data=json.dumps(doc))
post = es.index(index=index_name , doc_type='server' , id =1 , body=json.dumps(new_list))
print "------------------------------"
print "Data Pushed Successfully to ES"
I am not sure what's wrong here?
The doc_type you're using when indexing (= server) doesn't match the one you have in your index mappings (= type_name).
So if you index your documents like this instead, it will work
post = es.index(index=index_name , doc_type='type_name' , id =1 , body=json.dumps(new_list))
^
|
change this

Categories

Resources