Skip to content

Instantly share code, notes, and snippets.

View lachlan-eagling's full-sized avatar

Lachlan Eagling lachlan-eagling

  • Sunshine Coast, Australia
  • 04:45 (UTC +10:00)
View GitHub Profile
@lachlan-eagling
lachlan-eagling / iter_file_by_line.py
Created October 5, 2019 01:43
Blog - Generators (natural file line iteration)
for line in open(filename):
do_something(line)
@lachlan-eagling
lachlan-eagling / basic_gen.py
Created October 5, 2019 01:40
Blog - Generators (Basic Example)
def basic_generator():
for i in range(50_000_000):
yield i
gen = basic_generator()
for i in gen:
print(i)
@lachlan-eagling
lachlan-eagling / assignment_expr_perf.py
Last active September 19, 2019 02:10
Assignment Expression Performance (Blog)
def round_pows_slow():
return [pow(x, 2) for x in range(3_000_000) if pow(x, 2) % 2 == 0]
def round_pows_fast():
return [i for x in range(3_000_000) if (i := pow(x, 2) % 2 == 0)]
>>> round_pows_slow() runtime: 1105.8068 ms
>>> round_pows_fast() runtime: 899.7900 ms
>>> round_pows_fast() improvement: 206.0168 ms
@lachlan-eagling
lachlan-eagling / assignment_expr_perf.py
Created September 19, 2019 01:35
Assignment Expression Performance (Blog)
def round_squares_slow():
return [pow(x, 2) for x in range(3_000_000) if pow(x, 2) % 2 == 0]
def round_squares_fast():
return [i for x in range(3_000_000) if (i := pow(x, 2) % 2 == 0)]
>>> round_squares_slow() runtime: 1105.806827545166 ms
>>> round_squares_fast() runtime: 899.7900485992432 ms
>>> round_squares_fast() improvement: 206.01677894592285 ms
@lachlan-eagling
lachlan-eagling / assignment_expr_scopes.py
Created September 19, 2019 01:35
Assignment Expression Scope (Blog)
# Scope of variable without assignment expression.
def say_hello():
name = get_random_name()
if name:
print(f"Hello, {name}")
print(f"name {name} is still in scope")
# Scope of variable using assignment expression.
def say_hello():
if name := get_random_name():
@lachlan-eagling
lachlan-eagling / illegal_assignment_expressions.py
Created September 19, 2019 01:33
Illegal Assignment Expressions (Blog)
# Top level of an expression statement
y := f(x)
# At the top level of the right hand side of an expression statement.
y0 = y1 := f(x)
# Value of a keyword argument in a function call.
f(x = y := f(x))
# Default value in a function definition.
@lachlan-eagling
lachlan-eagling / basic_assignment_expr.py
Last active September 19, 2019 01:30
Basic Assignment Expression Usage (Blog)
my_dict = {"age": 28}
if val := my_dict.get("name"): # evaluates to False so `print(val)` is not executed.
print(val)
if val := my_dict.get("age"): # evaluates to True so `print(val)` is executed.
print(val)
@lachlan-eagling
lachlan-eagling / add.py
Last active April 28, 2019 05:14
Blog - Python Bytecode
import dis
def add(x, y):
return x + y
>>> dis.dis(add)
2 0 LOAD_FAST 0 (x)
2 LOAD_FAST 1 (y)
4 BINARY_ADD
@lachlan-eagling
lachlan-eagling / hello_world.pyc
Created April 16, 2019 07:07
Blog - Python Bytecode (hello_world bytecode)
2 0 LOAD_GLOBAL 0 (print)
2 LOAD_CONST 1 ('Hello, World!')
4 CALL_FUNCTION 1
6 POP_TOP
8 LOAD_CONST 0 (None)
10 RETURN_VALUE
@lachlan-eagling
lachlan-eagling / dis_hello_world.py
Last active April 28, 2019 05:33
Blog - Python Bytecode (dis example)
import dis
def hello_world():
print("Hello, World!")
>>> dis.dis(hello_world)
2 0 LOAD_GLOBAL 0 (print)
2 LOAD_CONST 1 ('Hello, World!')