This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with connection: | |
if file_path is not None: | |
counter = 0 | |
cursor_execution_config.command = f"PUT file://{snowflake_file_name}_{counter} @{full_qualified_stage_name} OVERWRITE = {overwrite}" | |
with open(file_path, "rb") as f: | |
if chunk_size == 0: | |
raise ValueError( | |
"Chunk size of 0 is not allowed for filestream pushing." | |
) | |
elif chunk_size > 0: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pythonCopy code | |
import paramiko | |
def stream_data_from_sftp(hostname, port, username, password, remote_path, chunk_size=1024): | |
transport = paramiko.Transport((hostname, port)) | |
transport.connect(username=username, password=password) | |
sftp = paramiko.SFTPClient.from_transport(transport) | |
with sftp.open(remote_path, 'rb') as file: | |
while chunk := file.read(chunk_size): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cursor = connection.cursor() | |
results = cursor.fetch_pandas_batches() | |
assert isinstance(results, Generator) | |
for result in results: | |
assert isinstance(result, PandasDataFrame) | |
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import s3fs | |
def stream_data_from_s3(bucket_name, file_path, chunk_size=1024): | |
fs = s3fs.S3FileSystem() | |
with fs.open(f'{bucket_name}/{file_path}', 'rb') as file: | |
while chunk := file.read(chunk_size): | |
yield chunk | |
# Example usage | |
bucket_name = 'my-bucket' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import httpx | |
def stream_data_from_api(url, chunk_size=1024): | |
with httpx.stream('GET', url) as response: | |
for chunk in response.iter_bytes(chunk_size): | |
yield chunk | |
# Example usage | |
url = '<https://example.com/large_file.zip>' | |
for chunk in stream_data_from_api(url): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def count_up_to(max): | |
count = 1 | |
while count <= max: | |
yield count | |
count += 1 | |
counter = count_up_to(3) | |
print(next(counter)) # Output: 1 | |
print(next(counter)) # Output: 2 | |
print(next(counter)) # Output: 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def my_generator(): | |
yield 1 | |
yield 2 | |
yield 3 | |
gen = my_generator() | |
for item in gen: | |
print(item) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyIterator: | |
def __init__(self, data): | |
self.data = data | |
self.index = 0 | |
def __iter__(self): | |
return self | |
def __next__(self): | |
if self.index < len(self.data): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my_list = [1, 2, 3] | |
iterator = iter(my_list) | |
print(next(iterator)) # Output: 1 | |
print(next(iterator)) # Output: 2 | |
print(next(iterator)) # Output: 3 | |
# Calling next again will raise a StopIteration Exception |
NewerOlder