python mongodb find method not working - python-3.x

Here is my method:
def mongo_find(collection_name, find_value):
list(MongoClient("localhost:27017").db[collection_name].find(find_value))
find_value = {'milestones.content': {'$regex': 'The idea'}}, {'milestones.content'}
print(list(mongo_find(collection_name, find_value)))
I'm getting this error:
TypeError: filter must be an instance of dict, bson.son.SON, or other type that inherits from collections.Mapping
but following works okay:
list(MongoClient("localhost:27017").db[collection_name].find({'milestones.content': {'$regex': 'The idea'}}, {'milestones.content'}))
So when running mongo_find method I tried printing:
print(find_value)
({'milestones.content': {'$regex': 'The idea'}}, {'milestones.content'})
Probably because round brackets are added on both ends. Is there a solution?

In your working example
list(MongoClient("localhost:27017").db[collection_name].find({'milestones.content': {'$regex': 'The idea'}}, {'milestones.content'}))
you are actually passing two parameters to the find function. The first one is of type dictionary and specifiies a filter
{'milestones.content': {'$regex': 'The idea'}}
while the second one is a python set which will be used for the projection
{'milestones.content'}
You non-working version passes but one parameter to the find() method which is a python tuple (that is where the round brackets in your output come from) and looks like that:
({'milestones.content': {'$regex': 'The idea'}}, {'milestones.content'})
So to fix that you would want to pass two parameters like in your working example:
def mongo_find(collection_name, filter. projection):
list(MongoClient("localhost:27017").db[collection_name].find(filter, projection))
filter = {'milestones.content': {'$regex': 'The idea'}}
projection = {'milestones.content'}
print(list(mongo_find(collection_name, filter, projection)))

Related

Explicit wait utility

I am using one generic explicit wait utility which can be used in different places in my test case. Following is the code I have written for the same under Base Test. Here I am looking for a text to present in the screen.For that I have given the parameter "text" in the function VerifyTextPresence.
But after running the script, I am having the below error. How to make it generic so that for any text, I can use this utility. For example, here I am checking for the text "Get" to be present in the screen
Utility Code:
def VerifyTextPresence(self,text):
wait = WebDriverWait(self.driver, 15)
element = wait.until(EC.presence_of_element_located((AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text(text)'))).text
Test Script:
def test_testcasename(self):
self.VerifyTextPresence("Get")
Error:
io.appium.uiautomator2.common.exceptions.UiSelectorSyntaxException: Could not parse expression `new UiSelector().textGet`: No opening parenthesis after method name at position
Based on Appium docs
https://appium.io/docs/en/writing-running-appium/android/uiautomator-uiselector/
UiSelector for text should look like:
new UiSelector().text("some text value")
and in your example:
new UiSelector().text(text)
I see 2 issues here:
no quotes for text
no reference to python method text arg
also
element = (...).text will put the text value to element, and looks not helpful.
Try this:
def VerifyTextPresence(self, text):
WebDriverWait(self.driver, 15).until(EC.presence_of_element_located((AppiumBy.ANDROID_UIAUTOMATOR, f"new UiSelector().text(\"{text}\")")))

What's the difference between the method .get() and the method .get in python? Both are appliable to dictionaries

Imagine I have a dict.
d = ['a': 1 , 'b':3]
I'm having a hard time to understand the difference between d.get and d.get().
I know that d.get() get the value from the key, like this:
print(d.get('a') )
output: 1
But when I write d.get, it shows this:
print(d.get)
output: <built-in method get of dict object at .........>
What is 'd.get' doing in my code?
I'm using python 3X
A method is literally just an attribute of an object that happens to be of type <class function>. The output you see is essentially what happens when you try to call print() on any function object, and is essentially a concise string representation that python creates for the function.
Actually calling a function is done with parentheses: d.get('a'), which means to execute the behavior the function refers to. It doesn't especially matter where the function is, though: I could do the following, and it would still work:
d = {'a': 1 , 'b':3}
freefunc = d.get
freefunc('a')
This is what the term "first class functions" refers to, when people compare python to something like Java. An entire function can be encapsulated in a variable and treated no differently than any other variable or attribute.
The short answer? There is no difference between the two methods. They are the same exact method.
The difference in your code is at when you write .get() you call the method, but when you write .get you just get a pointer (or location in the memory, to be exact) for that method, to call it later on if needed.
In the first scenario, you are calling print on the result of executing get('a'), which in this case is 1.
In your second scenario, you are calling print on the get function itself, instead of on an execution of it, which evaluates to its documentation, i.e. <built-in method get of dict object at... etc.

Why is function changing list values in python

i tried calling .copy on it and then passing it in the function. that didn't work.
when i tried coppying in the function itself it still changed the original list.
the function is in another file
main.py
win_condition.check_r_win(board)
win_condition.py
def check_r_win(board):
board = _board.copy()
for col in board:
while(len(col) <= ROWS):
col.append("-")
I don't really get what you are trying here. Python's list is a mutable object type. If you pass a object reference of a list to a function and change the list within this function, it also gets changed outside of the function scope.

Not able to pass object in graphql mutation

Can anyone tell me how to pass array of object in graphql mutation from react js side.I tried passing the object in react but getting error of not same type.
[input type][1]
[from api side][2]
[declaring the object][3]
[passing object in mutation][4]
[error i'm facing][5]
[1]: https://i.stack.imgur.com/ufVtA.png
[2]: https://i.stack.imgur.com/kQt5m.png
[3]: https://i.stack.imgur.com/hnxLM.png
[4]: https://i.stack.imgur.com/5JCHf.png
[5]: https://i.stack.imgur.com/BonPE.png
What i understood from your problem is that, You want to pass array of objects as argument to the mutation. It can be done easily.
First define a input in the schema,as below. It has the value name as string. You can add whatever you want. This structure will be used for passing value as argument.
input Product {
name: String
}
The above created input will be passed as a array as given below.
type RootMutation{
createProduct(product: [Product]): String
}
You will get the data in the resolver
createProduct: (args)=>{
// You will get the value in this 'args'
// Do Whatever you want
}
The input is given as follow
createProduct(product:[{name:"has"}])

error while calling function inside another function

I have function for newspaper3k which extract summary for given url. Given as :-
def article_summary(row):
url = row
article = Article(url)
article.download()
article.parse()
article.nlp()
text = article.summary
return text
I have pandas dataframe with column named as url
url
https://www.xyssss.com/dddd
https://www.sbkaksbk.com/shshshs
https://www.ascbackkkc.com/asbbs
............
............
There is another function main_code() which runs perfectly fine and inside which Im using article_summary.I want to add both functions article_summary and main_code() into one function final_code.
Here is my code : 1st function as:-
def article_summary(row):
url = row
article = Article(url)
article.download()
article.parse()
article.nlp()
text = article.summary
return text
Here is 2nd Function
def main_code():
article_data['article']=article_data['url'].apply(article_summary)
return article_data['articles']
When I have done:
def final_code():
article_summary()
main_code()
But final_code() not giving any output it shows as TypeError: article_summary() missing 1 required positional argument: 'row'
Are those the actual URLs you're using? If so, they seem to be causing an ArticleException, I tested your code with some wikipedia pages and it works.
On that note, are you working with just one df? If not, it's probably a good idea to pass it as a variable to the function.
-----------------------------------Edit after comments----------------------------------------------------------------------
I think a tutorial on Python functions will be beneficial. That said, in regards to your specific question, calling a function the way you described it will make it run twice, which is not needed in this case. As I said earlier, you should pass the df as an argument to the function, here is a tutorial on global vs local variables and how to use them.
The error you're getting is because you should pass an argument 'row' to the function article_summary (please see functions tutorial).

Resources