whoosh_search() returns empty - sqlalchemy

Tutorial at http://pythonhosted.org/Flask-WhooshAlchemy/ works for me.
Here's my setup
class Post(db.Model):
__tablename__ = 'post'
__searchable__ = ['body']
id = db.Column(db.Integer, primary_key=True)
body = db.Column(db.String)
Results:
If I do not put whoosh_index() before whoosh_search(), I get the error:
AttributeError: 'BaseQuery' object has no attribute 'whoosh_search'
db.session.query(Post).filter(Post.body=='hi') returns the correct output.
Post.query.whooshee_search('hi') returns empty
I did leave some code out, like additional columns and backrefs, but I don't think that changes anything
There are a few things to note:
Tutorial worked fine without whoosh_index(), but my code threw error
when I did not include it.
db.session.query(Post).filter() code worked fine, indicating my model is at least somewhat correct, and there is something wrong with whoosh/my whoosh setup
Please help, thanks

I too was facing the same issue as Post.query.whoosh_search('post').all() returned empty list all the time.
I found that it can be solved with using the specific versions specified here
https://github.com/gyllstromk/Flask-WhooshAlchemy/blob/master/requirements.txt
I was using following versions:
Flask==0.10.1
Flask-SQLAlchemy-2.1
Whoosh-2.7.2
blinker-1.4
I replaced them with:
**Flask==0.10.1**
**Flask-SQLAlchemy==1.0**
**Whoosh==2.6.0**
**blinker==1.3**
I will update here once I find exactly which upgrade causes this problem and why.

Related

How to parse Table from Wikipedia using htmltab package?

All,
I am trying to parse 1 table located here https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population#Sovereign_states_and_dependencies_by_population. And I would like to use htmltab package to achieve this task. Currently my code looks like following. However I am getting below Error. I tried passing "Rank", "% of world population " in which function, but still received an error. I am not sure, what could be wrong ?
Please Note: I am new to R and Webscraping, if you could provide explanation of the code, that will be great help.
url3 <- "https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population#Sovereign_states_and_dependencies_by_population"
list_of_countries<- htmltab(doc = url3, which = "//th[text() = 'Country(or dependent territory)']/ancestor::table")
Error: Couldn't find the table. Try passing (a different) information to the which argument.
This is an XPath problem not an R problem. If you inspect the HTML of that table the relevant header is
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">
Country<br><small>(or dependent territory)</small>
</th>
So text() on this is just "Country".
For example this could work (this is not the only option, you will just have to try out various xpath selectors to see).
htmltab(doc = url3, which = "//th[text() = 'Country']/ancestor::table")
Alternatively it's the first table on the page, so you could try which=1 instead.
(NB in Chrome you can do $x("//th[text() = 'Country']") and so on in the developer console to try these things out, and no doubt in other browsers also)

PowerShell HTML method getElementsByClassName returns null

I'm trying to write a PowerShell script which fetches an HTML page from a website and extracts some information from it.
My code looks like this:
$html = (invoke-webrequest -uri $address).parsedHTML;
$bodyHTML = $html.body.getElementsByClassName("news-item")[0].innerText;
The script fetches the website fine. The important part of the website looks like this:
...
<DIV class=news-item>
Important Information
...
The Problem:
I always get an error message: "cannot index into a null array".
The getElementsByClassName()-Function does not return anything.
If I list all div's and show the class names:
$html.body.getElementsByTagName("div") | select className
it lists all the class names including "news-item", which I am looking for.
Does anybody have an idea what the problem might be?
The problem seems to be the PowerShell version used. The PowerShell version running on the machine was 4.11.
With PowerShell 5.1 on a different machine the code worked fine.
As a workaround, because PowerShell coult not be updated and I was only looking for div-elements, I used the following code:
$bodyHTML = ($html.body.getElementsByTagName("div") | where { $_.className -eq "news-item" })[0].innerText;

Django save/update not change data in database

Django 1.11.7
MySQL
I was trying to change the value of an object like this:
# change the value of the filed and save
def patch(...):
instance.field_name = new_name
instance.save()
print(instance.filed_name)
When I run the code I got the print result as new_name. But when I check the database manually I got the result as old_name.
Then I tried ways like:
instance.save(update_fields=['field'])
and
ModelName.objects.filter(id=instance.id).update(field_name=new_name)
but get the above problem as well. And meanwhile, the project runs perfectly functional except for this segment of code.
Any idea what caused this problem or suggestion on how to solve it?
Is that piece of code inside a transaction? Maybe the transaction gets rolledback somewhere later.
When you read from the DB are you inside a transaction? Some transaction modes may not show you this change.
Are you sure that field_name is the correct field name? Maybe you have a typo and you just set a property of the object without changing model field. From what I see you sometimes type "field_name" and sometimes "filed_name"

Flask SQLAlchemy query (order_by)

I can't figure this out, I've done little work before with SQLAlchemy, but Flask SQLAlchemy seems to work very differently, and I can't find much about it online or on the documentation.
Basically, I want to pull one item from the database, and I have done this:
current_word = WordOfDay.query.order_by("WordOfDay.created_date").limit(1)
However, I keep getting the following error:
AttributeError: 'BaseQuery' object has no attribute 'word_of_the_day'
Why does it keep referring to 'BaseQuery' and how can I get around this? Other queries seem to work such as .query.get(id)...

Can't access rails post data

I've spent the last couple of hours slowly losing my mind while trying to figure something out. I'm submitting a form in rails, everything works fine until I try to access the params. In the console I can see the following:
Parameters: {"authenticity_token"=>"3mdEW2lHhkzpZbDsJCu8ZEV/wbq2YB/ztNR0RLTMZDs=", "utf8"=>"✓", "project"=>{"name"=>"woeij", "client"=>"iwej", "description"=>"oiejdoiew woeij"}, "id"=>"13"}
As you can see I'm sending name, client, description and id. I can access ID fine with something like:
#id = params[:id]
However, when I try to access name, client, or description in the same way they're all empty.
If I do:
#project = params[:project]
I get:
namewoeijclientiwejdescriptionoiejdoiew woeij
Would someone mind explaining what I'm doing wrong? And why I can't just get "woeij" when I do:
#name = params[:name]
Sorry for the stupid question, big thanks as always.
Attributes are nested, do
params[:project][:name]
to retrieve name.
A really cool tool in the rails console is the y: if you type y params they'll be presented really nicely.
You have a hash inside a hash. After you do:
#project = params[:project]
You have all your project parameters inside that hash. You can select them like this:
#project[:name] #=> "woeij"
#project[:client] #=> "iwej"
You can also select them in one go like this:
params[:project][:description] #=> "oiejdoiew woeij"