My Firebase structure is as below.
I'm using python to use firebase,
and also I use Pyrebase.
I want to get a key, which looks like '288xxxxxx'
from the value(busStopName).
Can I get the answer in Pyrebase?
Thank you in advance...
The following should work, based on the documentation
searched_bus = db.child("asanbus").order_by_child("busStopName").equal_to("XYZ").get()
for bus in searched_bus.each():
print(bus.key())
Assumption: there is only one record corresponding to busStopname = "XYZ". If not the case, you may do:
searched_bus = db.child("asanbus").order_by_child("busStopName").equal_to("XYZ").limit_to_first(1).get()
Related
I'm using Facebook's Python SDK to extract Ads Insights, but I can't find the right way to retrieve ALL fields without having to declare them, which is pretty cumbersome.
My current code looks like this:
ads = tempaccount.get_insights(
params={'date_preset': 'yesterday',
'level': 'ad'},
fields=[AdsInsights.Field.account_id,
AdsInsights.Field.account_name,
AdsInsights.Field.ad_id,
AdsInsights.Field.ad_name,
AdsInsights.Field.adset_id,
AdsInsights.Field.adset_name,
AdsInsights.Field.campaign_id,
AdsInsights.Field.campaign_name,
AdsInsights.Field.cost_per_outbound_click,
AdsInsights.Field.outbound_clicks,
AdsInsights.Field.spend])
Is there a way to force the "fields" attribute to bring all possible fields without declaring them?
Unfortunately, you will need to specify all the required fields. It's not possible to get all fields without explicitly specifying them.
My code also looks similar to yours when querying the insights endpoint.
This worked for me:
from facebookads.adobjects.adsinsights import AdsInsights
for property, value in vars(AdsInsights.Field).items():
print(property, ":", value)
Got from this post: How to enumerate an object's properties in Python?
I'm using python. I am trying to retrieve all payouts to a certain destination.
The code for retrieving everything is:
stripe.Payout.list(limit=3)
How do I add a parameter containing the destination ID? (Destination ID is in this form: ba_XXXXXXXXXXXXXXXXX)
I actually figured it out. This is not included in stripe API documentation. Just add a field like this:
stripe.Account.retrieve(destination = "ba_XXXXXXXXXXXXXXXX")
The only problem is this doesn't allow NOT filters. I can't do !=. It has to be = only. I haven't tried with > or <. Can anyone help me with this?
I'm following this tutorial to structure Firebase data. Near the end, it says the following:
With this kind of structure, you should keep in mind to update the data at 2 locations under the user and group too. Also, I would like to notify you that everywhere on the Internet, the object keys are written like "user1","group1","group2" etc. where as in practical scenarios it is better to use firebase generated keys which look like '-JglJnGDXcqLq6m844pZ'. We should use these as it will facilitate ordering and sorting.
So based on that, I'm assuming that the final result should be the following:
I'm using this python wrapper to post the data.
How can I achieve this?
When you write data to a Firebase array (for example in Javascript) using a line like this
var newPostKey = firebase.database().ref().child('users').push().key;
var updates = {item1: value1, item2: value2};
return firebase.database().ref().update(updates);
Like is described here, you will get a generated key for data "pushed". In the example above newPostKey will contain this generated key
UPDATE
To answer the updated question with with the Python wrapper:
Look for the section "Saving Data" in the page you linked to.
The code would look something like this;
data = {"Title": "The Animal Book"}
book = db.child("AllBooks").push(data)
data = {"Title": "Animals"}
category = db.child("Categories").push(data)
data = {category['name']: true }
db.child("AllBooks").child(book['name']).child("categories").push(data)
So I'm trying to learn Python here, and would appreciate any help you guys could give me. I've written a bit of code that asks one of my favorite websites for some information, and the api call returns an answer in a dictionary. In this dictionary is a list. In that list is a dictionary. This seems crazy to me, but hell, I'm a newbie.
I'm trying to assign the answers to variables, but always get various error messages depending on how I write my {},[], or (). Regardless, I can't get it to work. How do I read this return? Thanks in advance.
{
"answer":
[{"widgets":16,
"widgets_available":16,
"widgets_missing":7,
"widget_flatprice":"156",
"widget_averages":15,
"widget_cost":125,
"widget_profit":"31",
"widget":"90.59"}],
"result":true
}
Edited because I put in the wrong sample code.
You need to show your code, but the de-facto way of doing this is by using the requests module, like this:
import requests
url = 'http://www.example.com/api/v1/something'
r = requests.get(url)
data = r.json() # converts the returned json into a Python dictionary
for item in data['answer']:
print(item['widgets'])
Assuming that you are not using the requests library (see Burhan's answer), you would use the json module like so:
data = '{"answer":
[{"widgets":16,
"widgets_available":16,
"widgets_missing":7,
"widget_flatprice":"156",
"widget_averages":15,
"widget_cost":125,
"widget_profit":"31",
"widget":"90.59"}],
"result":true}'
import json
data = json.loads(data)
# Now you can use it as you wish
data['answer'] # and so on...
First I will mention that to access a dictionary value you need to use ["key"] and not {}. see here an Python dictionary syntax.
Here is a step by step walkthrough on how to build and access a similar data structure:
First create the main dictionary:
t1 = {"a":0, "b":1}
you can access each element by:
t1["a"] # it'll return a 0
Now lets add the internal list:
t1["a"] = ["x",7,3.14]
and access it using:
t1["a"][2] # it'll return 3.14
Now creating the internal dictionary:
t1["a"][2] = {'w1':7,'w2':8,'w3':9}
And access:
t1["a"][2]['w3'] # it'll return 9
Hope it helped you.
How would I get a list of the names of an index in Python? Here is what I have so far:
>>> es=e.es
>>> es
<Elasticsearch([{'host': '14555f777d8097.us-east-1.aws.found.io', 'port': 9200}])>
>>> es.indices
<elasticsearch.client.indices.IndicesClient object at 0x10de86790>
# how to get a list of all indexes in this cluster?
This question comes up when searching for information on retrieving aliases using the python-elasticsearch library. The accepted answer says to use get_aliases but that method has been removed (as of 2017). To get aliases, you can use the following:
es.indices.get_alias("*")
UPDATE
The latest usage should be with a keyword arg:
es.indices.get_alias(index="*")
how to get a list of all indexes in this cluster?
Use the wildcard. Works with elasticsearch-py.
for index in es.indices.get('*'):
print index
Here is one way to do it with the get_alias() method:
>>> indices=es.indices.get_alias().keys()
>>> sorted(indices)
[u'avails', u'hey', u'kibana-int']
If you are willing to use pyelasticsearch module they have support for the GET _mapping command, which produces the schema of the cluster. This will allow you to see the indices, and drill into each index to see doc_types, and their fields, etc. Here's an example:
import pyelasticsearch as pyes
es = pyes.ElasticSearch(["http://hostname0:9200", "http://hostname1:9200"]) ## don't accidentally type Elasticsearch, the class from the other two modules
schema = es.get_mapping() ## python dict with the map of the cluster
To get just the list of indices,
indices_full_list = schema.keys()
just_indices = [index for index in indices_full_list if not index.startswith(".")] ## remove the objects created by marvel, e.g. ".marvel-date"
This is related to this question
You can use the Cat API:es.cat.indices(h='index', s='index').split()
I use curl to call the stats API and get information about the indices. Then I parse the JSON object that is returned to find the index names.
curl localhost:9200/_stats
In Python you can call curl using the requests library. I don't know of a way to do this using the Elasticsearch or Elasticsearch-DSL Python library.
You can get _mapping to get list of all indexes by doing something like that.
requests.get(full_elastic_url + "/_mapping")
_cat API seems the right way to do this, since the _aliases way of doing will soon be removed by elasticsearch since it exposes the system indices.
indices = es.cat.indices(h='index', s='index').split()
It did the job for me.
If you want 'alias name' and not 'index name', here the perfect solution:
response = es.indices.get(indexname)
alias_names = list(response[indexname]['aliases'].keys())
In alias_names we get list of alias names on a particular index.