How to compose two functions whose outer function supplies arguments to the inner function : <p>I have two similar codes that need to be parsed and I'm not sure of the most pythonic way to accomplish this.</p>

<p>Suppose I have two similar "codes"</p>

<pre><code>secret_code_1 = 'asdf|qwer-sdfg-wert$$otherthing'
secret_code_2 = 'qwersdfg-qw|er$$otherthing'
</code></pre>

<p>both codes end with <code>$$otherthing</code> and contain a number of values separated by <code>-</code></p>

<p>At first I thought of using <code>functools.wrap</code> to separate some of the common logic from the logic specific to each type of code, something like this:</p>

<pre><code>from functools import wraps

def parse_secret(f):
  @wraps(f)
  def wrapper(code, *args):
    _code = code.split('$$')[0]
    return f(code, *_code.split('-'))
  return wrapper

@parse_secret
def parse_code_1b(code, a, b, c):
  a = a.split('|')[0]
  return (a,b,c)

@parse_secret
def parse_code_2b(code, a, b):
  b = b.split('|')[1]
  return (a,b)
</code></pre>

<p>However doing it this way makes it kind of confusing what parameters you should actually pass to the <code>parse_code_*</code> functions i.e.</p>

<pre><code>parse_code_1b(secret_code_1)
parse_code_2b(secret_code_2)
</code></pre>

<p>So to keep the formal parameters of the function easier to reason about I changed the logic to something like this:</p>

<pre><code>def _parse_secret(parse_func, code):
  _code = code.split('$$')[0]
  return parse_func(code, *_code.split('-'))

def _parse_code_1(code, a, b, c):
  """
  a, b, and c are descriptive parameters that explain
  the different components in the secret code

  returns a tuple of the decoded parts
  """
  a = a.split('|')[0]
  return (a,b,c)

def _parse_code_2(code, a, b):
  """
  a and b are descriptive parameters that explain
  the different components in the secret code

  returns a tuple of the decoded parts
  """
  b = b.split('|')[1]
  return (a,b)

def parse_code_1(code):
  return _parse_secret(_parse_code_1, code)

def parse_code_2(code):
  return _parse_secret(_parse_code_2, code)
</code></pre>

<p>Now it's easier to reason about what you pass to the functions:</p>

<pre><code>parse_code_1(secret_code_1)
parse_code_2(secret_code_2)
</code></pre>

<p>However this code is significantly more verbose.</p>

<p>Is there a better way to do this? Would an object-oriented approach with classes make more sense here?</p>

<p><a href="https://repl.it/Exqh/7" rel="noreferrer">repl.it example</a></p>
