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
type User struct { | |
firstName string | |
lastName string | |
username string | |
age int | |
} |
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
from typing import List, Dict | |
from models import Employee | |
def process_employees(employees): | |
employee_dict = {} | |
for employee in employees: | |
employee_dict[employee.id] = employee | |
return employee_dict | |
def process_employees(employees: List[Employee]) -> Dict[int, Employee]: |
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 cProfile | |
print("Generator Runtime Stats:") | |
cProfile.run('sum((i for i in range(100000)))') | |
print("List Runtime Stats:") | |
cProfile.run('sum([i for i in range(100000)])') | |
Generator Runtime Stats: | |
100005 function calls in 0.123 seconds |
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
from sys import getsizeof | |
size = 50_000 | |
generator = (i for i in range(size)) | |
lst = [i for i in range(size)] | |
print(f"Generator Size: {getsizeof(generator)} bytes") | |
print(f"List Size: {getsizeof(lst)} bytes") | |
>>> Generator Size: 112 bytes |
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
generator = sequence_generator(3) | |
try: | |
print(next(generator)) | |
print(next(generator)) | |
print(next(generator)) | |
print(next(generator)) # This call to next will raise an exception. | |
except StopIteration: | |
print("Generator exhausted") |
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 sequence_generator(n): | |
for i in range(n): | |
yield i | |
generator = sequence_generator(3) | |
print(next(generator)) | |
>>> 0 | |
for i in generator: | |
print(i) |
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
for i in (x for x in range(n)): | |
do_something(i) |
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 fib_generator(n): | |
i, j = 0, 1 | |
for _ in range(n): | |
yield i | |
i, j = j, i + j |
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 ininite_number_generator(increment=1): | |
i := 0 | |
while True: | |
yield i | |
i += increment |
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 read_chunks(filename, mode="r", chunk_size=32): | |
with open(filename, mode) as f: | |
while True: | |
data = f.read(chunk_size) | |
if not data: | |
break | |
yield data | |
def main(): |