I am trying to implement fuzzy search from SQLite database in my flask app but I am having some difficulties. I am using sqlalchemy for the database and the fuzzywuzzy package. The function I am specifically using is fuzzywuzzy.fuzz.token_set_ratio(). Is it possible to make a query that filters the records so it returns only those which when the above function is called with the user given string (from the search) and the name row from the table as arguments it returns values greater than 70 for example? I hope that made sense.
I tried this and I knew it would not work but decided to try it anyways:
song.query.filter(fuzz.token_set_ratio(q, song.name) > 70)
If such query is not possible to do, then how should I implement fuzzy searching in my web app?
Related
I'm new on cloud firestore and I'm trying to make queries as efficient as possible but I kind of desperate with an specific one. I would greatly appreciate your help.
This is the situation:
I want to show a project list which that I'm getting from an user field and 2 queries in project entity. The user field let’s called "favorite projects" and it has the projects id that reference those projects on their entity. The other query retrieve me the public projects (==) and the last the private projects where the user is a contributor (array_contains).
I want to sort and filtering the result of the two queries. Is there an option to merge both queries and use sort and filter as a we do with a collection reference?
Thank you for your time, have a nice day!
Based on this and this documentation, I do not believe there is an out of the box solution for joining the results of queries such as the ones described.
You'll need to achieve that within the your code.
For example you can run the first query and store all the data of the document in a map or array. Then use the reference of the other document within the document_reference to make the second query and the third.
Once you have all of them you can do as you please using Python. But getting them ready using a single query or auto-joining the queries seems to not be supported yet.
I connect to sqlite database in Blaze using
df = bz.Data("sqlite:///<mydatabase>)
everything works fine but I do not know how to provide user-defined functions in my interaction with df.
I have a column called IP in df which is text containing IP addresses. I also have a function toSubnet (x, y) which takes in an IP address (x) in text format and return its /y subnet. For example:
out = toSubnet('1.1.1.1',24)
out
1.1.1.0/24
Now if I want to map all IPs to their /14 subnets, I use:
df.IP.map(lambda x:toSubnet(x,14),'string')
Which works when the backend is CSV. But with sqlite backend I get NotImplementedError.
What's wrong here?
NB: This doesn't tell you how to do exactly what you want, but it provides an explanation of why it doesn't work and a possible next step to get this to work with SQLite.
The problem you're running into is that it is very difficult to efficiently execute arbitrary Python code against an arbitrary SQL database.
Blaze takes user code and translates it to SQL as best it can, using SQLAlchemy, which I don't think has a way to do this.
Since nearly every database has a different way of dealing with user-defined functions (UDFs), it's quite a lot of work to build an API that allows the following:
A user to define a function in Python
Turn that pure Python function into a UDF native to the database.
That said, the Python interface to SQLite has a way to register Python functions that can be executed in a SQL statement:
https://docs.python.org/2/library/sqlite3.html#sqlite3.Connection.create_function
There currently isn't a way to express a UDF with Blaze using the SQL backend, though this could be implemented as new expression type that allows a user to register a function via the underlying database's db API.
We have built a series a forms using PHP that populate a MySQL Database and after learning more Python want to begin transitioning our whole web app over to a python back end instead of PHP. Can anyone offer a quick intro into searching a MySQL DB using Python?
The easiest way is to abstract the database using an ORM. I found that the python package SQLAlchemy works fantastically.
A ORM let's you treat the database objects as normal python objects. Most queries can be abstracted and for 99% of cases you won't need to write SQL queries. Also this makes transition between database technologies very simple.
Go check it out on:
http://www.sqlalchemy.org/
A search query would be something like:
session.query(User).filter_by(first_name='bob')
Now you will have all users with the first name 'bob'
You search it the same way you would in PHP; because the queries you are using will not change.
The only difference is the driver you have to use.
We're trying to set up a Pyramid project that will use MySQL instead of SQLAlchemy.
My experience with Pyramid/Python is limited, so I was hoping to find a guide online. Unfortunately, I haven't been able to find anything to push us in the right direction. Most search results were for people trying to use raw SQL/MySQL commands with SQLAlchemy (many were re-posted links).
Anyone have a useful tutorial on this?
Pyramid at its base does not assume that you will use any one specific library to help you with your persistence. In order to make things easier, then, for people who DO wish to use libraries such as SQLALchemy, the Pyramid library contains Scaffolding, which is essentially some auto-generated code for a basic site, with some additions to set up items like SQLAlchemy or a specific routing strategy. The pyramid documentation should be able to lead you through creating a new project using the "pyramid_starter" scaffolding, which sets up the basic site without SQLAlchemy.
This will give you the basics you need to set up your views, but next you will need to add code to allow you to connect to a database. Luckily, since your site is just python code, learning how to use MySQL in Pyramid is simply learning how to use MySQL in Python, and then doing the exact same steps within your Pyramid project.
Keep in mind that even if you'd rather use raw SQL queries, you might still find some usefulness in SQLAlchemy. At it's base level, SQLAlchemy simply wraps around the DBAPI calls and adds in useful features like connection pooling. The ORM functionality is actually a large addition to the tight lower-level SQLAlchemy toolset.
sqlalchemy does not make any assumption that you will be using it's orm. If you wish to use plain sql, you can do so, with nothing more than what sqlalchemy already provides. For instance, if you followed the recipe in the cookbook, you would have access to the sqlalchemy session object as request.db, your handler would look something like this:
def someHandler(request):
rows = request.db.execute("SELECT * FROM foo").fetchall()
The Quick Tutorial shows a Pyramid application that uses SQL but not SQLAlchemy. It uses SQLite, but should be reasonably easy to adapt for MySQL.
I am working on my first pylons + SQLAlchemy app (I'm new to both).
As I change my mind on the table structure, I wish there was a similar function to metadata.create_all(), that checks if there are new columns definitions and create them in the database.
Does such a function exist ?
I'm not (yet) a SQLAlchemy user, but I've heard good things about sqlalchemy-migrate. The general term of the problem you have is "schema migration", I'm sure a google search containing these terms will help you further.