SQLAlchemy and scalar values [duplicate] - python

This question already has answers here:
Query for list of attribute instead of tuples in SQLAlchemy
(4 answers)
Closed 5 years ago.
I have simple question regarding SQLAlchemy, is it possible to get the rows from the result as scalars instead of tuples? In other words I want an equivalent to:
[i[0] for i in self.archive.query(IRTerm.term).distinct()]
Thank you

No built in way in SQLAlchemy, but with python it isn't too hard. The example you gave works fine. You can also do map(itemgetter(0), query) or for value, in query:.

Since 0.6.5 you can use query.as_scalar (ref).

Related

Avoid 'SettingWithCopyWarning:' when setting values with loc and conditions (inside a function) [duplicate]

This question already has answers here:
How to deal with SettingWithCopyWarning in Pandas
(20 answers)
Closed 2 years ago.
I'm trying to set some values in one column (col_2) coming from another column (col_3) when values in col_1 fulfill a condition.
I'm doing that with the .loc operator as after all my research looking for alternatives it looks as the best practice.
data.loc[data.col_1.isin(['A','B']),col_2]=data[col_3]
Nevertheless this stills trigger the 'SettingWithCopyWarning:'.
Is there any other way to do this operation without raising that warning?
Following the advice of Cameron,the line of code was part of a function, where data was passed as argument.
Starting the function with data = data.copy() solves the issue

Fetch all records but some condition on filed in SQLAlchemy? [duplicate]

This question already has answers here:
SQLAlchemy get every row the matches query and loop through them
(2 answers)
Closed 4 years ago.
I need select * from new_entry where batch=2018.
I tried query.filter(NewEntry.batch==batch).first() i got first record in database but i need all columns which is matched to batch.What can I do?
Since you are using first() you will only get the first result, to get all the results as a list you have to use all()
query.filter(NewEntry.batch==batch).all()

How to filter for my requirement? [duplicate]

This question already has answers here:
How do I do an OR filter in a Django query?
(8 answers)
Closed 4 years ago.
I have a requirement:
in my filter, I want to filter like bellow:
messages = Message.objects.filter(to_user=user or from_user=user).all()
I mean if the to_user==user or from_user==user all be queried out.
I found the bellow related post:
How to use OR filter condition in queryset?
But this is one params, you see this is different between my requirement scenario.
Use Q objects, that allows complex lookups.
from django.db.models import Q
messages = Message.objects.filter(Q(to_user=user) | Q(from_user=user))

SQLalchemy return tuples when querying all() [duplicate]

This question already has answers here:
Query for list of attribute instead of tuples in SQLAlchemy
(4 answers)
SQL Alchemy ORM returning a single column, how to avoid common post processing
(7 answers)
Closed 5 years ago.
I'm trying to get a list of integers, for this query:
session.query(C.ex_id).filter(c.foo==foo).all()
I get a list of tuples instead of list of integers. I don't want to iterate over the list I get, I want to get it form the query itself.
What can I do?
This may not be exactly what you want, but one work around is:
some_list = map(lambda (x,):x, session.query(C.ex_id).filter(c.foo==foo).all())

pymongo find on list instead of db [duplicate]

This question already has answers here:
Testing a filter on a document on client-side
(2 answers)
Closed 6 years ago.
Is there a way to use the pymongo find method on a list (or iterable) of dictionaries instead on performing the search on the database?
I have a list of dictionaries and I'd like to filter them using queries like in pymongo.
Is there any library that can do that?
you don't need an external library to accomplish that simple task. If i understand correctly you just need to filter a list of dictionaries based on some value.
def find(dict_list, key, value_list):
return [dict for dict in dict_list if dict[key] in value_list]
You can then pass a list of dictionaries a key to match and values of that key that you want to search.

Categories