Add a filter to view answer body with StackAPI for Python - python-3.x

I am using StackAPI (python wrapper) to extract data from Stack Oveflow. Everything works perfectly except when trying to add a filter.
SITE = StackAPI('stackoverflow', key=<my_key>)
answers = SITE.fetch('questions/{ids}/answers?filter=!9_bDE(fI5', ids=[int(q_id)])
I created the filter using the StackExchange API's filter edit function and copied it.
The fetch works perfectly if I remove the ?filter=!9_bDE(fI5' from end point. But that output is useless as I am interested in the answer body.
Could some one please help?

I was doing it all wrong. Instead of adding the filter as part of the end point, it should be a parameter.
answers = SITE.fetch('questions/{ids}/answers', ids=[int(q_id)], filter='!9_bDE(fI5')
This performed as expected.

Related

Questions regarding Python replace specific texts

I'm writing a script to scrape from another website with Python, and I am facing this question that I have yet to figure out a method to resolve it.
So say I have set to replace this particular string with something else.
word_replace_1 = 'dv'
namelist = soup.title.string.replace(word_replace_1,'11dv')
The script works fine, when the titles are dv234,dv123 etc.
The output will be 11dv234, 11dv123.
However if the titles are, dv234, mixed with dvab123, even though I did not set dvab to be replaced with anything, the script is going to replace it to 11dvab123. What should I do here?
Also, if the title is a combination of alphabits,numbers and Korean characters, say DAV123ㄱㄴㄷ,
how exactly should I make it to only spitting out DAV123, and adding - in between alphabits and numbers?
Python - making a function that would add "-" between letters
This gives me the idea to add - in between all characters, but is there a method to add - between character and number?
the only way atm I can think of is creating a table of replacing them, for example something like this
word_replace_3 = 'a1'
word_replace_4 = 'a2'
.......
and then print them out as
namelist3 = soup.title.string.replace(word_replace_3,'a-1').replace(word_replace_4,'a-2')
This is just slow and not efficient. What would be the best method to resolve this?
Thanks.

Where can I find an overview of how the ec2.instancesCollection is built

In boto3 there's a function:
ec2.instances.filter()
The documentation:
http://boto3.readthedocs.org/en/latest/reference/services/ec2.html#instance
Say it returns a list(ec2.Instance) I wish...
when I try printing the return I get this:
ec2.instancesCollection(ec2.ServiceResource(), ec2.Instance)
I've tried searching for any mention of an ec2.instanceCollection, but the only thing I found was something similar for ruby.
I'd like to iterate through this instanceCollection so I can see how big it is, what machines are present and things like that.
Problem is I have no idea how it works, and when it's empty iteration doesn't work at all(It throws an error)
The filter method does not return a list, it returns an iterable. This is basically a Python generator that will produce the desired results on demand in an efficient way.
You can use this iterator in a loop like this:
for instance in ec2.instances.filter():
# do something with instance
or if you really want a list you can turn the iterator into a list with:
instances = list(ec2.instances.filter())
I'm adding this answer because 5 years later I had the same question and went round in circles trying to find the answer.
First off, the return type in the documentation is wrong (still). As you say, it states that the return type is: list(ec2.Instance)
where it should be:ec2.instancesCollection.
At the time of writing there's an open issue in github covering this - https://github.com/boto/boto3/issues/2000.
When you call the filter method a ResourceCollection is created for the particular type of resource against which you called the method. In this case the resource type is instance which gives an instancesCollection. You can see the code for the ResourceCollection superclass of instancesCollection here:
https://github.com/boto/boto3/blob/develop/boto3/resources/collection.py
The documentation here gives an overview of the collections: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/collections.html
To get to how to use it and actually answer your question, what I did was to turn the iterator into a list and iterate over the list if the size is > 0.
testList = list(ec2.instances.filter(Filters=filters))
if len(testList) > 0;
for item in testList;
.
.
.
This may well not be the best way of doing it but it worked for me.

modx - getPage - [[+pageNav]] Placeholder always has a value

I try to hide the getPage [[+pageNav]] Placeholder if there is no pagination. But I can't do the following.
[[!+pageNav:notempty=`<ul class="overview__pagination">[[!+pageNav]]</ul>`]]
Does someone know how I can hide the element with an apropriate output filter? (without own extra snippet). I also tried the following and some other (not likely to work variations).
[[!+pageNav:isnot=``:then=`<ul class="overview__pagination">[[!+pageNav]]</ul>`]]`
Are you calling that code in a chunk that is cached?
Otherwise i've experienced this aswell and it seems custom placeholders sometimes behave that way, it's probably due to the fact that they actually have some unprocessed value during the IF computation but when it's actually output you see nothing. Or that the value is somehow "null" instead of "" while modx output filter might do a strict comparison.
If you're not calling it in a cached chunk or part of code, i suggest first trying with another getPage placeholder such as pageCount or total.
Like:
[[!+pageCount:gt=`1`:then=`<ul class="overview__pagination">[[!+page.nav]]</ul>`]]
If that still doesn't work, a last resort in the form of a simple snippet will always solve it, like:
[[!outputPagination? &total=`[[+total]]` &limit=`XX` &output=`<ul class="overview__pagination">[[!+page.nav]]</ul>`]]
In snippet:
if ($total > $limit) {
return $output;
}
Shouldn't it be...
[[!+page.nav:notempty=`<ul class="overview__pagination">[[!+page.nav]]</ul>`]]
Well, there is a much more easier way to do it than in the first answer. It's like TheMistaC says, even if my answer is a lot easier:
[[!+page.nav:notempty=`
[[!+page.nav]]
`]]
I use it to display a list of articles with getResources, so I know this works fine.

for loop in play framework 1.2.5 (groovy template)

I am using the following code to use the for loop with a list (I am familiar with the other syntax copied below as well). The first for loop is not working for me even though its the same list. Any ideas on how to go about troubleshooting this:
#{list items:0..items.size(), as:'item'}${item}#{/list} //Not Working - I have tried moving the items.size out of the loop as well but it has not worked for me
This is working for the same list:
#{list items, as:'item'}${item}#{/list}
Any thoughts/suggestions why? Thanks in advance.
Edit: The list is that of user defined instances (primarily holding Strings and Integers)
The syntax is incorrect, the items parameter need an iterable and you give it an integer, if you need an index you can use the item_index variable

How can I convert a string array to a variant array in VBscript?

I'm using a function in vbscript which returns a variant array of strings.
JobIDs = objDoc.ConnectedSubmit(objServer)
The problem is I can't get the Job ID values from that array, as vbscript doesn't handle typed variables. It just gives a type mismatch when I attempt to do ANYTHING with the JobIDs array. I found some promising information here, but when I used the conversion function:
Set objConverter = CreateObject("ADS.ArrayConvert")
ConvertedJobIDs = objConverter.CStrArray(JobIDs())
It is giving me the same type mismatch error. Am I missing something obvious here? This is, apparently, an official microsoft solution, so I'm not sure why it seems to have the same problem, namely, not being able to actually do anything with a string array in the first place. I've seen the first part of my question answered in many places, all pointing to the MS solution, but I have yet to see any follow up reports of someone successfully using that solution.
I'm not sure if I understand why it doesn't work, so this answer might not be very helpful. I would have thought that something like this might work (following on from your previous question I'm assuming you're trying to get the cancellation to work):
For Each id In JobIDs
WScript.Echo id
YourJob = YourOutgoingFaxQueue.GetJob(id)
YourJob.Cancel()
Next
This behavior is by design, VBScript can't do anything with a non-variant array, there was a KB article from Microsoft that explained this but it is not on-line anymore:
Q165967 - PRB: Script Error Occurs When Referencing Non-variant Array
An archived copy of the article can be found at:
https://ftp.zx.net.nz/pub/archive/ftp.microsoft.com/MISC/KB/en-us/165/967.HTM

Resources