Skip to content

Commit

Permalink
correct handling of defaults FunctionBuilder, along with tests to the…
Browse files Browse the repository at this point in the history
… same effect.
  • Loading branch information
mahmoud committed Feb 3, 2016
1 parent 0143804 commit 4cfa95e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
10 changes: 10 additions & 0 deletions TODO.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ TODO
* autoincremented IDs for loggers and records?
* First-class datetime Formatter/FormatField support (type_func)

Formatting
----------

* Shorteners
* Bytes shortening (strutils.bytes2human)
* Numeric shortening (K, M, etc.)
* Time shortening (h, m, s, ms, us)
* Shortened string
* reprlib

Sinks/emitters
--------------

Expand Down
7 changes: 6 additions & 1 deletion lithoxyl/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ def wrap(self, level, name=None, inject_as=None, **kw):
def record_wrapper(func_to_log, _name=name):
if _name is None: # wooo nonlocal
_name = func_to_log.__name__

@wraps(func_to_log, injected=inject_as)
def logged_func(*a, **kw):
rec = self.record(level, _name, **kw)
Expand Down Expand Up @@ -269,18 +270,21 @@ def __init__(self, name, **kw):

def get_sig_str(self):
return inspect.formatargspec(self.args, self.varargs,
self.keywords, self.defaults)[1:-1]
self.keywords, [])[1:-1]

@classmethod
def from_func(cls, func):
# TODO: copy_body? gonna need a good signature regex.
argspec = inspect.getargspec(func)
kwargs = {'name': func.__name__,
'doc': func.__doc__,
'defaults': func.__defaults__,
'module': func.__module__,
'dict': getattr(func, '__dict__', {})}

for a in ('args', 'varargs', 'keywords', 'defaults'):
kwargs[a] = getattr(argspec, a)

return cls(**kwargs)

def get_func(self, execdict=None, add_source=True, with_dict=True):
Expand All @@ -303,6 +307,7 @@ def get_func(self, execdict=None, add_source=True, with_dict=True):

func.__name__ = self.name
func.__doc__ = self.doc
func.__defaults__ = self.defaults
if with_dict:
func.__dict__.update(self.dict)
func.__module__ = self.module
Expand Down
31 changes: 21 additions & 10 deletions lithoxyl/tests/test_fb.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@

import inspect
from lithoxyl.logger import FunctionBuilder


def example_func(a, b, c=0, d=1):
return 'what a great example'
def example_func(a, b, c=1, d=1):
return (a + b) / d


def test_remove_arg():
fb = FunctionBuilder.from_func(example_func)

fb.remove_arg('d')
fb.body = '\n'.join(inspect.getsource(example_func).splitlines()[1:])

new_func = fb.get_func()
src = new_func.__source__

assert 'example_func(a, b, c=0):' in src

fb = FunctionBuilder.from_func(example_func)
assert new_func(1, 2) == 3
assert new_func(2, 4, d=2) == 3

fb.remove_arg('c')
no_c_func = fb.get_func()

assert no_c_func(2, 4, 2) == 3


MISSING = object()


def obj_default_func(arg=MISSING):
return arg


def test_hmm():
fb = FunctionBuilder.from_func(obj_default_func)
fb.body = '\n'.join(inspect.getsource(obj_default_func).splitlines()[1:])
new_func = fb.get_func()
src = new_func.__source__

assert 'example_func(a, b, d=1):' in src
assert new_func() is MISSING

0 comments on commit 4cfa95e

Please sign in to comment.