mirror of
https://github.com/jbranchaud/til
synced 2026-03-04 06:58:45 +00:00
Compare commits
2 Commits
ec12f7ea80
...
be18f387ed
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
be18f387ed | ||
|
|
efb83050ab |
@@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/).
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter).
|
For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter).
|
||||||
|
|
||||||
_1737 TILs and counting..._
|
_1739 TILs and counting..._
|
||||||
|
|
||||||
See some of the other learning resources I work on:
|
See some of the other learning resources I work on:
|
||||||
|
|
||||||
@@ -1034,6 +1034,8 @@ If you've learned something here, support my efforts writing daily TILs by
|
|||||||
- [Break Debugger On First Line Of Program](python/break-debugger-on-first-line-of-program.md)
|
- [Break Debugger On First Line Of Program](python/break-debugger-on-first-line-of-program.md)
|
||||||
- [Create A Dummy DataFrame In Pandas](python/create-a-dummy-dataframe-in-pandas.md)
|
- [Create A Dummy DataFrame In Pandas](python/create-a-dummy-dataframe-in-pandas.md)
|
||||||
- [Dunder Methods](python/dunder-methods.md)
|
- [Dunder Methods](python/dunder-methods.md)
|
||||||
|
- [Install With PIP For Specific Interpreter](python/install-with-pip-for-specific-interpreter.md)
|
||||||
|
- [Iterate First N Items From Enumerable](python/iterate-first-n-items-from-enumerable.md)
|
||||||
- [Override The Boolean Context Of A Class](python/override-the-boolean-context-of-a-class.md)
|
- [Override The Boolean Context Of A Class](python/override-the-boolean-context-of-a-class.md)
|
||||||
- [Store And Access Immutable Data In A Tuple](python/store-and-access-immutable-data-in-a-tuple.md)
|
- [Store And Access Immutable Data In A Tuple](python/store-and-access-immutable-data-in-a-tuple.md)
|
||||||
- [Test A Function With Pytest](python/test-a-function-with-pytest.md)
|
- [Test A Function With Pytest](python/test-a-function-with-pytest.md)
|
||||||
|
|||||||
17
python/install-with-pip-for-specific-interpreter.md
Normal file
17
python/install-with-pip-for-specific-interpreter.md
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# Install With PIP For Specific Interpreter
|
||||||
|
|
||||||
|
The `pip` module can be invoked for any of its commands, such as install, using
|
||||||
|
a specific Python interpreter like so:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ python3 -m pip install black
|
||||||
|
```
|
||||||
|
|
||||||
|
This avoid ambiguity between the version of Python I am using and version of the
|
||||||
|
package manager I'm using.
|
||||||
|
|
||||||
|
Similarly if I need to upgrade `pip`, I can do the following:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ python3 -m pip install --upgrade pip
|
||||||
|
```
|
||||||
27
python/iterate-first-n-items-from-enumerable.md
Normal file
27
python/iterate-first-n-items-from-enumerable.md
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
# Iterate First N Items From Enumerable
|
||||||
|
|
||||||
|
As I'm working through the 2nd chapter of [Build a Large Language Model (from
|
||||||
|
scratch)](https://still.visualmode.dev/blogmarks/227), I came across a code
|
||||||
|
example processing a dictionary of words. This example used a for loop to print
|
||||||
|
out each dictionary entry until an index of 50 was reached on then it did a
|
||||||
|
`break`.
|
||||||
|
|
||||||
|
This struck me as an odd way to grab and process N items from a list. I did some
|
||||||
|
searching and found `itertools` which provides
|
||||||
|
[`islice`](https://docs.python.org/3/library/itertools.html#itertools.islice).
|
||||||
|
|
||||||
|
```python
|
||||||
|
from itertools import islice
|
||||||
|
|
||||||
|
# preprocess words from a file into a word list
|
||||||
|
all_words = ... # not shown here
|
||||||
|
|
||||||
|
vocab = {token: integer for integer, token in enumerate(all_words)}
|
||||||
|
for item in islice(enumerate(vocab.items()), 50):
|
||||||
|
print(item)
|
||||||
|
```
|
||||||
|
|
||||||
|
The `islice` function is a better approach because the intention (to grab the
|
||||||
|
first 50 things) is encoded in the function call rather than buried in a loop
|
||||||
|
body. It also has equivalent memory efficiency to the original example because
|
||||||
|
it lazily processes the list of `vocab` items.
|
||||||
Reference in New Issue
Block a user