Here is what I'm trying to do. The database is Postgres.
numbers is a Python set() or some other iterable.
session.execute(table.update().\
where(func.substring(table.c.number,1 ,5) in numbers).\
values(provider="testprovider")
The problem with this is that in numbers doesn't seem to work. It will work for a single number if I do something like where(func.substring(table.c.number,1, 5)== '12345')
I've googled a lot on how to do WHERE IN with sqlalchemy, but all of them are with a SELECT query or do use the .in_() function on the column itself. For example this answer: link
The documentation also says the .in_() only works on the columns.
So, I don't really know how to proceed here. How can I write this expression?
Ah nevermind, although the documentation doesn't say it, I can use the .in_() function on the result of func.substring.
So
where(func.substring(table.c.number, 1, 5).in_(numbers))
worked.
Related
I'm using the search_repositories() function from the Pygithub package and am looking to build a query something like this in pseudo code:
('keyword1' OR 'keyword2) AND ('keyword3' OR 'keyword4') AND last_push_date > '2020-01-01'
So far I've not worked out the correct way to break up the clauses according to the logic of the parentheses and also have found that if I have any boolean logic, the 'qualifier' (i.e. the pushdate:>'2020-01-01') seems to break the query string and return no results.
I'm aware from the docs that you cannot have more than 5 AND/OR operators but in my testing that wasn't the case.
It seems that for instance I can do something like:
'"firstword secondword" OR "thirdword fourthword"' and it treats each as a phrase correctly, but adding a qualifier afterwards then returns no results - even if I put the pushed date to way back.
Also this seems to work:
'oneword anotherword+pushed:>2020-01-01'
But combining such logic just returns 0 results.
Any thoughts?
I am trying to sort a spreadsheet using openpyxl and Python. I have read the documents and I don't quite understand this page. I am expecting it to either add the auto filter dropdown arrows or sort my spreadsheet and it is returning errors. Here's my code
wb = openpyxl.load_workbook('report.xlsx')
ws = wb.active
ws['A2'] = "Store"
ws['B2'] = "Manager"
ws['C2'] = "Zone"
ws.column_dimensions.group('F','DU',hidden=True)
#ws.AutoFilter.add_sort_condition('C:C')
wb.save("report.xlsx")
According to the documents it looks like the line "ws.AutoFilter.add_sort_condition('C:C')" should give me the result I want. (Yes I understand it is currently a comment line. The rest of my code runs fine without that line so I commented it.)
When I have that line in the code I get the error - 'Worksheet' object has no attribute 'AutoFilter' but according to the documents it looks like it does. http://openpyxl.readthedocs.org/en/latest/_modules/openpyxl/worksheet/filters.html#AutoFilter.
If anyone can help explain to me why it is failing or what the documents mean that would be great.
This statement in the documents is particularly confusing to me:
"Don't create auto filters by yourself. It is created by :class:~openpyxl.worksheet.Worksheet.
You can use via :attr:~~openpyxl.worksheet.Worksheet.auto_filter attribute."
because I tried that too and it also failed.
Update: #crussell's reply worked in that it added the auto filter to my spreadsheet. However, it is still not adding the sort condition to the appropriate column.
See here: http://openpyxl.readthedocs.org/en/latest/api/openpyxl.worksheet.html?highlight=auto_filter#openpyxl.worksheet.worksheet.Worksheet.auto_filter
The auto_filter command returns the AutoFilter object, so in a sense they are the same thing.
What you need is ws.auto_filter.ref = 'C1:C20'
with the range of cells those of which you want to filter.
According to the documentation openpyxl can define the filter and/or sort but does not apply them!
They can only be applied from within Excel
I don't have a full answer for this, but I did find something interesting when using filters.
I added a filter column,
eg:
ws.auto_filter.add_filter_column(1,{},True)
then I opened the resulting spreadsheet. The column showed the filter! But the data was not actually filtered in the spreadsheet. I had to click on the "Data" tab and click "Reapply" the filter.
So it looks like the adding of a sort or filter column works, except it never actually applies the sort or filter.
I have been hunting down a function that will apply the filter, but have not yet had any luck. If anyone has thoughts, I'd love to hear them!
Thanks!
After looking at the documentation and the source code for the AutoFilter.add_sort_condition() function it looks like the ref you're providing may need to be changed to include row indices, like "C1:C120", for example. Have you tried it with specific row numbers? Also, be sure to take a look at the comment regarding the ref variable right below the function declaration in:
http://openpyxl.readthedocs.org/en/latest/_modules/openpyxl/worksheet/filters.html#AutoFilter
if you're not following where I'm coming from. Cheers and good luck!
I've researched a lot in terms of doing a query to do an order by before a group by and have found most answers in terms of raw SQL. However, I'd like to see a solution in SQLAlchemy Code.
My original naive solution looked as follows:
session.query(MyTable).order_by(timestamp).group_by(begin_date)
unfortunately, this causes the table to first be grouped then ordered, which will not return what I am expecting.
Second,I tried:
stmt = session.query(MyTable).order_by(timestamp)
session.query(stmt).group_by(begin_date)
This returns the correct results, however, the results are of KeyedTuples whereas I want to actually have MyTable objects for backwards compatibility reasons.
How can I achieve this?
The code in latest comment of original question seems still have some problem. Here is a working version:
stmt = session.query(MyTable).order_by(timestamp).subquery()
session.query().add_entity(MyTable, alias=stmt).group_by(begin_date);
I'm pretty new to SQLite and Python and have run into a bit of confusion. I'm trying to return all elements in a column that contain a substring which is passed to a function as a variable in Python. My code is running, but it's returning an empty result instead of the correct result.
Here's the code with the names generalized:
def myFunc(cursor,myString):
return cursor.execute("""select myID from Column where name like '%'+?'%' """,(myString,))
Like I said, the code does run without error but returns an empty result instead of the result that I know it should be. I'm assuming it has something to do with my use of the wildcard and/or question mark, but I can't be sure. Anyone have any ideas? Thanks in advance for your time/help! Also, this is my first post, so I apologize in advance if I missed any of the recommended protocols for asking questions.
Well, '%'+?'%' definitely isn't going to work—you're trying to concatenate with + on the left, but with no operator…
You can compute LIKE-search fields if you do it right—'%'+?+'%', in this case. That will cause problems with some databases (from not working, to doing a less efficient search), but, at least according to CL.'s comment, sqlite3 will be fine.
But the easy thing to do is to just substitute a complete parameter, rather than part of one. You can put % into the parameters, and it'll be interpreted just fine. So:
return cursor.execute("""select myID from Column where name like ?""",
('%'+myString+'%',))
And this also has the advantage that if you want to do a search for initial substrings ('foo%'), it'll be the same SQL statement but with a different parameter.
Try this:
def myFunc(cursor,myString):
return cursor.execute('select myID from Column where name like "{0}"'.format(myString))
i find this code :
def _oauth_parse_response(body):
p = cgi.parse_qs(body, keep_blank_values=False)
but i don't know what is mean
thanks
It means "look on the cgi object for an attribute called parse_qs, and call it as a function with body as a positional argument and keep_blank_values as a keyword argument with the value of False".
For the definition of cgi look further up, but it probably is the stdlib module of the same name.
docs.python.org has an excellent search engine, which will show you this:
This function is deprecated in this
module. Use urllib.parse.parse_qs()
instead. It is maintained here only
for backward compatibility.
and once you follow the link, you see:
Parse a query string given as a string
argument (data of type
application/x-www-form-urlencoded).
Data are returned as a dictionary. The
dictionary keys are the unique query
variable names and the values are
lists of values for each name.
and so on.
Much as I may like getting easy rep for answering absolutely trivial questions that anybody with a pulse should have zero trouble answering for themselves, maybe with some help from today's reasonably powerful search engines, some questions are really too easy to answer -- the stackoverflow equivalent of shooting sitting birds. You're not a newbie here -- why not, and I'm going to suggest an absolutely revolutionary strategy!, make the microscopic effort of doing your own searches and asking questions when there is something worth asking?
Parses a query string into a dictionary.
Deprecated in python >= 2.6.