Concatenating the querysets into a list is the simplest approach. If the database will be hit for all querysets anyway (e.g. because the result needs to be sorted), this won't add further cost.
from itertools import chain
result_list = list(chain(page_list, article_list, post_list))
Using itertools.chain
is faster than looping each list and appending elements one by one, since itertools
is implemented in C. It also consumes less memory than converting each queryset into a list before concatenating.
Now it's possible to sort the resulting list e.g. by date (as requested in hasen j's comment to another answer). The sorted()
function conveniently accepts a generator and returns a list:
result_list = sorted(
chain(page_list, article_list, post_list),
key=lambda instance: instance.date_created)
If you're using Python 2.4 or later, you can use attrgetter
instead of a lambda. I remember reading about it being faster, but I didn't see a noticeable speed difference for a million item list.
from operator import attrgetter
result_list = sorted(
chain(page_list, article_list, post_list),
key=attrgetter('date_created'))
If you want to find all commits where the commit message contains a given word, use
$ git log --grep=word
If you want to find all commits where "word" was added or removed in the file contents (to be more exact: where the number of occurrences of "word" changed), i.e., search the commit contents, use a so-called 'pickaxe' search with
$ git log -Sword
In modern Git there is also
$ git log -Gword
to look for differences whose added or removed line matches "word" (also commit contents).
Note that -G
by default accepts a regex, while -S
accepts a string, but it can be modified to accept regexes using the --pickaxe-regex
.
To illustrate the difference between -S<regex> --pickaxe-regex
and -G<regex>
, consider a commit with the following diff in the same file:
+ return !regexec(regexp, two->ptr, 1, ®match, 0);
...
- hit = !regexec(regexp, mf2.ptr, 1, ®match, 0);
While git log -G"regexec\(regexp"
will show this commit, git log -S"regexec\(regexp" --pickaxe-regex
will not (because the number of occurrences of that string did not change).
With Git 2.25.1 (Feb. 2020), the documentation is clarified around those regexes.
See commit 9299f84 (06 Feb 2020) by Martin Ågren (``).
(Merged by Junio C Hamano -- gitster
-- in commit 0d11410, 12 Feb 2020)
diff-options.txt
: avoid "regex" overload in the example \
Reported-by: Adam Dinwoodie
Signed-off-by: Martin Ågren
Reviewed-by: Taylor Blau
When we exemplify the difference between -G
and -S
(using --pickaxe-regex
), we do so using an example diff and git diff
invocation involving "regexec", "regexp", "regmatch", etc.
The example is correct, but we can make it easier to untangle by avoiding writing "regex.*" unless it's really needed to make our point.
Use some made-up, non-regexy words instead.
The git diff
documentation now includes:
To illustrate the difference between -S<regex> --pickaxe-regex
and -G<regex>
, consider a commit with the following diff in the same file:
+ return frotz(nitfol, two->ptr, 1, 0);
...
- hit = frotz(nitfol, mf2.ptr, 1, 0);
While git log -G"frotz\(nitfol"
will show this commit, git log -S"frotz\(nitfol" --pickaxe-regex
will not (because the number of occurrences of that string did not change).
Best Answer
You can't. The official GitHub searching rules:
Clone and use
git-grep
:git support searching in sources with git-grep command. Just clone a repository and use the command in the folder:
Alternatives:
I recommend you to try ripgrep tool, it's fast and simple. Works like
git-grep
but looks nicer:And you can use the standard
grep
to search any text in files: