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?
Related
I'm trying to read a specific data field in Google Cloud Firestore using python.
I have a collection called Products in my Firestore DB, and have manually added several documents with various fields.
So far I am able to pull a document using:
docs = db.collection(u'Products').where(u'checked', u'==', False).stream()
for doc in docs:
print(u'{} => {}'.format(doc.id, doc.to_dict()))
This works, and I receive the following output:
Test2 => {'link': 'https://stackoverflow.com', 'price': 14, 'checked': False}
However, I am unable to pull out the individual 'link' string from this dict. I have tried:
print(doc.to_dict('link'))
and several iterations of this, and get the following output:
to_dict() takes 1 positional argument but 2 were given
I have been following the firebase documentation here, but have found no examples of printing fields specifically.
Any advice on how to print the 'link' string from the query I have used?
I'm running Python 3.7.
Thanks in advance for your help.
to_dict returns a dict, so your code should probably look like:
print(doc.to_dict()['link'])
instead of passing the parameter directly.
Alternatively, since you only need the one field you can try:
print(doc.get('link'))
As it avoids creating a copy of the entire snapshot.
When using the jira python library and creating issues, non mandatory fields are being enforced on create_issue call.
Response on create issue attempt:
text: No issue link type with name 'Automated' found.
Response on create meta call to check mandatory fields:
'hasDefaultValue': False,
u'key': u'issuelinks',
u'name': u'Linked Issues',
u'operations': [u'add'],
u'required': False,
I had a similar issue and after a bit of digging around, this is what I did.
Open a jira and using developer tools (F12), find out the id of the mandatory custom fields. They should be named somewhat like "customfield_10304"
Once you have these field ids, just use them the way you set other fields while creating an issue. For eg.
new_issue = jira.create_issue(project={'key': project},
summary='{}'.format(summary),
description='{}'.format(description),
issuetype={'name': 'Bug'},
labels=labels,
versions=[{"name": affect_version[0]}],
customfield_10304=[{"value": env}],
customfield_10306=[{"value": customer}],
priority={'name': priority})
Jira behaves strange many times.
createmeta call returns you all the possible issuetypes, and their all fields, and which field is mandatory or not.
But even after this, there are certain fields which are mandatory but createmeta wont tell you this. You need to rely on the exception message that you got after filing create_issue().
In the exception message, exception_obj.response.text gives you the json having key/value of exact field required.
Then, you can search in response of createmeta about its schema type, and may be the allowedValues set.
And, then try again.
Basically, you need to do retry of above mechanism.
I'm currently using the facebookads api 2.5.1 with python and I'm struggling with Insights fields for video ads.
I'm requesting Insights in async mode like this :
i_async_job = campaign.get_insights(
fields=fields,
params={'time_range':{'since': from_date,'until': to_date},'level':'ad'},
async=True)
and I'm passing fields parameters like :
Insights.Field.spend,
Insights.Field.video_avg_sec_watched_actions,
Insights.Field.video_avg_pct_watched_actions,
Insights.Field.video_p25_watched_actions,
Insights.Field.video_p50_watched_actions,
Insights.Field.video_p75_watched_actions,
Insights.Field.video_p95_watched_actions,
...
it works but when I try to get these following fields :
Insights.Field.website_clicks,
Insights.Field.video_10_sec_watched_actions,
Insights.Field.video_30_sec_watched_actions,
it does not work... :-(
I've looked at the module sources and indeed, these fields are not present.
The only way to get these fields is to fire again a new Insight query but with an empty field array. You have to parse then the result json to extract these missing metrics....
mmmmh.. am I doing something wrong or could I get these stats in one query ? Thanks.
Regards
Nicolas
I have got data from POST like
first_name=jon&nick_name=harry
How can I change this to a python dictionary, like :
{
"first_name":"jon",
"nick_name":"harry"
}
>>> urlparse.parse_qs("first_name=jon&nick_name=harry")
{'nick_name': ['harry'], 'first_name': ['jon']}
If you trust that the URL arguments will always be properly formatted, this would be a minimal solution:
dict(pair.split("=") for pair in urlargs.split("&"))
In code that's going to be publicly accessible though, you'll probably want to use a library that does error checking. If you're using Django, you'll probably have access to them already in the dictionary-like object HttpRequest.POST.
how do i validate a field conditionally based on the presence of another field. for example, only make "state" required only if "country" is "US".
thanks,
steve
EDIT:
so i figured to do this:
chained_validators = [validators.RequireIfPresent('state', present="country")]
but the error message is associated with "_the_form" instead of "state". is there a way to link it to the field instead?
Had the same problem during a project in my company. We wrote our own Formencode validator for this. We currently try to merge it with the main project. In the meantime you can download it here: https://github.com/GevatterGaul/formencode
There is also a Howto in, well, german: http://techblog.auf-nach-mallorca.info/2014/08/19/dynamische_formulare_validieren_mit_formencode/
But let me give you a quick rundown in the context of your example:
from formencode import validators
v = validators.RequireIfMatching('country', expected_value='US', required_fields=['state'])
v.to_python(dict(country='US', state='Virginia'))
The main benefit is, that in comparison to validators.RequireIfPresent, validators.RequireIfMatching only requires a field when a given field matches a given value. In your example, only if 'country' is 'US', then it requires the 'state' field.
Hope I could help.