Skip to content

Instantly share code, notes, and snippets.

View 2tony2's full-sized avatar
🏠
Working from home

Tony Zeljkovic 2tony2

🏠
Working from home
View GitHub Profile
1
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:
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):
cursor = connection.cursor()
results = cursor.fetch_pandas_batches()
assert isinstance(results, Generator)
for result in results:
assert isinstance(result, PandasDataFrame)
break
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'
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):
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
def my_generator():
yield 1
yield 2
yield 3
gen = my_generator()
for item in gen:
print(item)
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):
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