Initial commit
This commit is contained in:
commit
0af82cb48b
|
|
@ -0,0 +1,11 @@
|
|||
NON-AI ISC License
|
||||
|
||||
Copyright 2024 Jeff Lieb
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that:
|
||||
|
||||
1. The above copyright notice and this permission notice appear in all copies.
|
||||
|
||||
2. The software and any modifications made to it may not be used for the purpose of training or improving machine learning algorithms, including but not limited to artificial intelligence, natural language processing, or data mining. This condition applies to any derivatives, modifications, or updates based on the Software code. Any usage of the Software in an AI-training dataset is considered a breach of this License.
|
||||
|
||||
THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
[tool.poetry]
|
||||
name = "mock-the-python"
|
||||
version = "0.1.0"
|
||||
description = "Yet another mocking library"
|
||||
authors = ["Jeff <jeff.m.lieb@gmail.com>"]
|
||||
readme = "README.md"
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = "^3.8"
|
||||
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry-core"]
|
||||
build-backend = "poetry.core.masonry.api"
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
from inspect import getmembers
|
||||
|
||||
class MultipleMembersMatchedException(Exception)
|
||||
def __init__(self, module, matches, member):
|
||||
super().__init__(f"Multiple members found matching {member} in {module}. Matched members were {matches}")
|
||||
|
||||
|
||||
class NoMembersMatchedExcedption(Exception):
|
||||
def __init__(self, module, member, name):
|
||||
super().__init__(f"No members found matching member {member}{f' or name {name}' if name is not None else ''} in {module}.")
|
||||
|
||||
|
||||
def at(module):
|
||||
"""Returns an object with a single method named member.
|
||||
Calling member with anything, or name, will search for the
|
||||
member on the provided module by instance comparison with
|
||||
the results from inspect.getmembers.
|
||||
|
||||
If the member is not found, it will be looked up by name instead.
|
||||
|
||||
This is meant to replace use of bare strings in mock.patch calls,
|
||||
so that an IDE will have a much easier time finding references that
|
||||
need updated during refactorings.
|
||||
"""
|
||||
class MockAt:
|
||||
@staticmethod
|
||||
def member(member, *, name=None):
|
||||
matches = (
|
||||
[name for name, value in getmembers(module) if value is member]
|
||||
if name is None
|
||||
else [candidate for candidate, _ in getmembers(module) if candidate == name]
|
||||
)
|
||||
|
||||
if len(matches) == 1:
|
||||
return f"{module.__name__}.{matches[0]}"
|
||||
elif len(matches) > 1:
|
||||
raise MultipleMembersMatchedException(module, matches, member, name)
|
||||
else:
|
||||
raise NoMembersMatchedExcedption(module, member, name)
|
||||
|
||||
return MockAt()
|
||||
Loading…
Reference in New Issue