Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

In order to run this script, you'll need to have both py.test and six installed.

View test_fun_with_partials.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
# -*- coding: utf-8 -*-
# In order to run this script, you'll need to
# have both py.test and six installed.
# Assuming you have pip, in a virtualenv just
# type at the command-line:
#
# pip install pytest six
#
# To run it, just type:
#
# python test_fun_with_partials.py
 
def power(base, exponent):
return base ** exponent
 
 
def square(base):
return power(base, 2)
def cube(base):
return power(base, 3)
 
from functools import partial
 
square = partial(power, exponent=2)
cube = partial(power, exponent=3)
 
def test_partials():
assert square(2) == 4
assert cube(2) == 8
 
def test_partial_docs():
assert square.keywords == {"exponent": 2}
assert square.func == power
 
assert cube.keywords == {"exponent": 3}
assert cube.func == power
 
 
def test_power_partials():
 
# List to store the partials
power_partials = []
for x in range(1, 11):
 
# create the partial
f = partial(power, exponent=x)
# Add the partial to the list
power_partials.append(f)
 
# We could just use list comprehension instead of the loop
# [partial(power, exponent=x) for x in range(1, 11)]
# Test the first power
assert power_partials[0](2) == 2
# Test the fifth power
assert power_partials[4](2) == 32
# Test the tenth power
assert power_partials[9](2) == 1024
 
 
 
# Since I like my article code to work in both Python 2.7 and 3,
# I'll import the excellent six library to manage the
# differences between Python versions. Six is available on PyPI
# at https://pypi.python.org/pypi/six.
from six import add_metaclass
 
class PowerMeta(type):
def __init__(cls, name, bases, dct):
# generate 50 partial power functions:
for x in range(1, 51):
# Set the partials to the class
setattr(
# cls represents the class
cls,
# name the partial
"p{}".format(x),
# partials created here
partial(power, exponent=x)
)
super(PowerMeta, cls).__init__(name, bases, dct)
 
@add_metaclass(PowerMeta)
class PowerStructure(object):
pass
 
def test_power_structure_object():
p = PowerStructure()
 
# 10 squared
assert p.p2(10) == 100
# 2 to the 5th power
assert p.p5(2) == 32
 
# 2 to the 50th power
assert p.p50(2) == 1125899906842624
 
def test_power_structure_class():
# Thanks to the power of metaclasses, we don't need to instantiate!
# 10 squared
assert PowerStructure.p2(10) == 100
# 2 to the 5th power
assert PowerStructure.p5(2) == 32
 
# 2 to the 50th power
assert PowerStructure.p50(2) == 1125899906842624
if __name__ == "__main__":
import pytest
pytest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.