Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions itertools-in-python3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Alternatively, you can download the entire repository as a `.zip` file from the

## Running the Code Examples

To run the code examples, first make sure you have Python 3 installed on your machine. **The code examples WILL NOT work with Python 2.** If you need help installing Python 3, check out Real Python's [Python 3 Installation & Setup Guide](https://realpython.com/installing-python/).
To run the code examples, first make sure you have Python 3.12 or later installed on your machine, since `swimmers.py` uses `itertools.batched()`. If you need help installing Python 3, check out Real Python's [Python 3 Installation & Setup Guide](https://realpython.com/installing-python/).

To run the `sp500.py` file, type `python3 sp500.py` into your terminal. Here's how that looks, with output:

Expand All @@ -39,7 +39,7 @@ Max loss: -20.47% on 1987-10-19
Longest growth streak: 14 days (1971-03-26 to 1971-04-15)
```

> **Note:** Depending on your installation, you may need to type `python3.6` or `python36` to run the examples. If you follow the [Python 3 Installation & Setup Guide](https://realpython.com/installing-python/), you should have no problem.
> **Note:** Depending on your installation, you may need to type `python3.15` or `python315` to run the examples. If you follow the [Python 3 Installation & Setup Guide](https://realpython.com/installing-python/), you should have no problem.

Similarly, for `swimmers.py`:

Expand Down
4 changes: 2 additions & 2 deletions itertools-in-python3/sp500.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ def read_prices(csvfile, _strptime=datetime.strptime):
prices = tuple(read_prices("SP500.csv"))
gains = tuple(
DataPoint(day.date, 100 * (day.value / prev_day.value - 1.0))
for day, prev_day in zip(prices[1:], prices, strict=False)
for prev_day, day in it.pairwise(prices)
)

# Find maximum daily gain/loss.
zdp = DataPoint(None, 0) # zero DataPoint
max_gain = ft.reduce(max, it.filterfalse(lambda p: p <= zdp, gains))
max_gain = ft.reduce(max, it.filterfalse(lambda p: p <= zdp, gains), zdp)
max_loss = ft.reduce(min, it.filterfalse(lambda p: p > zdp, gains), zdp)


Expand Down
11 changes: 2 additions & 9 deletions itertools-in-python3/swimmers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,10 @@ def sort_and_group(iterable, key=None):
return it.groupby(sorted(iterable, key=key), key=key)


def grouper(iterable, n, fillvalue=None):
iters = [iter(iterable)] * n
return it.zip_longest(*iters, fillvalue=fillvalue)


def read_events(csvfile, _strptime=datetime.datetime.strptime):
def _median(times):
return statistics.median(
(_strptime(time, "%M:%S:%f").time() for time in row["Times"])
(_strptime(time, "%M:%S:%f").time() for time in times)
)

fieldnames = ["Event", "Name", "Stroke"]
Expand All @@ -41,9 +36,7 @@ def _median(times):
events_by_name = sort_and_group(evts, key=lambda evt: evt.name)
best_times = (min(evt) for _, evt in events_by_name)
sorted_by_time = sorted(best_times, key=lambda evt: evt.time)
teams = zip(
("A", "B"), it.islice(grouper(sorted_by_time, 4), 2), strict=False
)
teams = zip(("A", "B"), it.batched(sorted_by_time, 4), strict=False)
for team, swimmers in teams:
print(
"{stroke} {team}: {names}".format(
Expand Down
Loading