Skip to content

Commit c6d8770

Browse files
committed
TR updates, first round
1 parent fac2bd2 commit c6d8770

File tree

5 files changed

+50
-10
lines changed

5 files changed

+50
-10
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import asyncio
2+
import csv
3+
4+
import aiofiles
5+
6+
7+
class AsyncCSVIterator:
8+
def __init__(self, path):
9+
self.path = path
10+
self.file = None
11+
12+
def __aiter__(self):
13+
return self
14+
15+
async def __anext__(self):
16+
if self.file is None:
17+
self.file = await aiofiles.open(self.path, mode="r")
18+
lines = await self.file.readlines()
19+
self.reader = csv.reader(lines)
20+
try:
21+
return next(self.reader)
22+
except StopIteration:
23+
await self.file.close()
24+
raise StopAsyncIteration
25+
26+
27+
async def main():
28+
csv_iter = AsyncCSVIterator("data.csv")
29+
# Skip the headers
30+
await anext(csv_iter)
31+
# Process the rest of the rows
32+
async for row in csv_iter:
33+
print(row)
34+
35+
36+
asyncio.run(main())

python-async-iterators/compress.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ async def stream_generator(files):
1212

1313

1414
async def main(directory, zip_name="output.zip"):
15-
files = [{"file": file} for file in directory.iterdir()]
16-
17-
async with aiofiles.open(zip_name, mode="wb") as z:
15+
files = [{"file": path} for path in directory.iterdir() if path.is_file()]
16+
async with aiofiles.open(zip_name, mode="wb") as archive:
1817
async for chunk in stream_generator(files):
19-
await z.write(chunk)
18+
await archive.write(chunk)
2019

2120

2221
directory = Path()

python-async-iterators/data.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Name,Age,Job
2+
John Doe,30,Software Engineer
3+
Jane Smith,25,Data Scientist
4+
Jim Brown,45,Project Manager
5+
Jessica Jones,40,UX Designer

python-async-iterators/large_file_iterable.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55

66
class AsyncFileIterable:
7-
def __init__(self, filename, chunk_size=1024):
8-
self.filename = filename
7+
def __init__(self, path, chunk_size=1024):
8+
self.path = path
99
self.chunk_size = chunk_size
1010

1111
async def __aiter__(self):
12-
async with aiofiles.open(self.filename, mode="rb") as file:
12+
async with aiofiles.open(self.path, mode="rb") as file:
1313
while True:
1414
chunk = await file.read(self.chunk_size)
1515
if not chunk:

python-async-iterators/large_file_iterator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55

66
class AsyncFileIterator:
7-
def __init__(self, filename, chunk_size=1024):
8-
self.filename = filename
7+
def __init__(self, path, chunk_size=1024):
8+
self.path = path
99
self.chunk_size = chunk_size
1010
self.file = None
1111

@@ -14,7 +14,7 @@ def __aiter__(self):
1414

1515
async def __anext__(self):
1616
if self.file is None:
17-
self.file = await aiofiles.open(self.filename, mode="rb")
17+
self.file = await aiofiles.open(self.path, mode="rb")
1818
chunk = await self.file.read(self.chunk_size)
1919
if not chunk:
2020
await self.file.close()

0 commit comments

Comments
 (0)