-
Notifications
You must be signed in to change notification settings - Fork 140
/
number_multiplier.py
38 lines (27 loc) · 1.02 KB
/
number_multiplier.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
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
from unittest import TestCase
class NumberMultiplier(object):
# Example of Python-based num_types.
@classmethod
def create(cls, param):
# This example uses parameter ``n`` from config file.
# ``2`` is used when not specified.
n = int(param.get('n', 2))
return cls(n)
def __init__(self, n):
self._n = n
def extract(self, key, value):
"""
Returns the number multipled by the number specified in the
config (``n``).
The key is typed as ``str`` (unicode).
The value is typed as ``float``.
This method should return list of key-values (pair of feature key
as ``str`` and its value as number).
"""
return [(key, value * self._n)]
class NumberMultiplierTest(TestCase):
def test_extract(self):
ex = NumberMultiplier.create({'n': 3})
self.assertEqual([('v', 30)], ex.extract('v', 10))