Skip to content

Commit

Permalink
TR updates, first round
Browse files Browse the repository at this point in the history
  • Loading branch information
lpozo committed Jul 18, 2024
1 parent fac2bd2 commit c6d8770
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
36 changes: 36 additions & 0 deletions python-async-iterators/async_csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import asyncio
import csv

import aiofiles


class AsyncCSVIterator:
def __init__(self, path):
self.path = path
self.file = None

def __aiter__(self):
return self

async def __anext__(self):
if self.file is None:
self.file = await aiofiles.open(self.path, mode="r")
lines = await self.file.readlines()
self.reader = csv.reader(lines)
try:
return next(self.reader)
except StopIteration:
await self.file.close()
raise StopAsyncIteration


async def main():
csv_iter = AsyncCSVIterator("data.csv")
# Skip the headers
await anext(csv_iter)
# Process the rest of the rows
async for row in csv_iter:
print(row)


asyncio.run(main())
7 changes: 3 additions & 4 deletions python-async-iterators/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ async def stream_generator(files):


async def main(directory, zip_name="output.zip"):
files = [{"file": file} for file in directory.iterdir()]

async with aiofiles.open(zip_name, mode="wb") as z:
files = [{"file": path} for path in directory.iterdir() if path.is_file()]
async with aiofiles.open(zip_name, mode="wb") as archive:
async for chunk in stream_generator(files):
await z.write(chunk)
await archive.write(chunk)


directory = Path()
Expand Down
5 changes: 5 additions & 0 deletions python-async-iterators/data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Name,Age,Job
John Doe,30,Software Engineer
Jane Smith,25,Data Scientist
Jim Brown,45,Project Manager
Jessica Jones,40,UX Designer
6 changes: 3 additions & 3 deletions python-async-iterators/large_file_iterable.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@


class AsyncFileIterable:
def __init__(self, filename, chunk_size=1024):
self.filename = filename
def __init__(self, path, chunk_size=1024):
self.path = path
self.chunk_size = chunk_size

async def __aiter__(self):
async with aiofiles.open(self.filename, mode="rb") as file:
async with aiofiles.open(self.path, mode="rb") as file:
while True:
chunk = await file.read(self.chunk_size)
if not chunk:
Expand Down
6 changes: 3 additions & 3 deletions python-async-iterators/large_file_iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@


class AsyncFileIterator:
def __init__(self, filename, chunk_size=1024):
self.filename = filename
def __init__(self, path, chunk_size=1024):
self.path = path
self.chunk_size = chunk_size
self.file = None

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

async def __anext__(self):
if self.file is None:
self.file = await aiofiles.open(self.filename, mode="rb")
self.file = await aiofiles.open(self.path, mode="rb")
chunk = await self.file.read(self.chunk_size)
if not chunk:
await self.file.close()
Expand Down

0 comments on commit c6d8770

Please sign in to comment.