Skip to content

Commit

Permalink
feat: renamed resource() into grab(), a simpler and more obvious …
Browse files Browse the repository at this point in the history
…name for what it does.
  • Loading branch information
joshorr committed Nov 2, 2022
1 parent c2ebe5b commit aafcec8
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 158 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ from udepend import Dependency


# This is a example Dependency class, the intent with this class is to treat
# it as a semi-singleton shared resource.
# it as a semi-singleton shared dependency/resource.
class MyResource(Dependency):

# It's important to allow resources to be allocated with
Expand All @@ -129,29 +129,29 @@ class MyResource(Dependency):
#
# Next, we get value of `some_attribute` off of it.
# Prints 'my-default-value'
print(MyResource.resource().name)
print(MyResource.grab().name)

# Change the value of the name attribute on current resource
MyResource.resource().name = 'changing-the-value'
# Change the value of the name attribute on current dependency
MyResource.grab().name = 'changing-the-value'

# Now prints 'changing-the-value'
print(MyResource.resource().name)
print(MyResource.grab().name)

# You can temporarily override a resource via a python context manager:
# You can temporarily inject your own version of a dependency via a python context manager:
with MyResource(name='my-temporary-name'):
# When someone askes for the current resource of `MyResource`,
# When someone asks for the current dependency of `MyResource`,
# they will get back the one I created in `with` statement above.
#
# Now prints 'my-temporary-name'
print(MyResource.resource().name)
print(MyResource.grab().name)

# Object we created and temporary activated by above `with` statement
# has been deactivated (ie: thrown out).
# Old one that was the active one previously is the one that is now used when
# the current resource for `MyResource` is asked for.
# the current dependency for `MyResource` is asked for.
#
# prints 'changing-the-value'
print(MyResource.resource().name)
print(MyResource.grab().name)
```


Expand All @@ -176,9 +176,9 @@ class DataResource(Dependency):
another_optional_field: str = "hello!"


# Get current DataResource resource, print it's another_optional_field;
# Get current DataResource dependency, print it's another_optional_field;
# will print out `hello!`:
print(DataResource.resource().another_optional_field)
print(DataResource.grab().another_optional_field)
```

### Thread Safety
Expand Down Expand Up @@ -237,7 +237,7 @@ from xyn_config import Config
config = ActiveResourceProxy.wrap(Config)

# This is a simpler way to get the same proxy
# (no imports are needed, just call the class method on any resource class):
# (no imports are needed, just call the class method on any Dependency subclass):
config = Config.resource_proxy()
```

Expand All @@ -255,7 +255,7 @@ active resource for `Config`. So it's the equivalent of doing this:
```python
from xyn_config import Config

get_method = Config.resource().get
get_method = Config.grab().get
value = get_method('some_config_var')
```

Expand Down
4 changes: 2 additions & 2 deletions docs/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ from xyn_config import Config
config = ActiveResourceProxy.wrap(Config)

# This is a simpler way to get the same proxy
# (no imports are needed, just call the class method on any resource class):
# (no imports are needed, just call the class method on any Dependency class):
config = Config.resource_proxy()
```

Expand All @@ -74,7 +74,7 @@ active resource for `Config`. So it's the equivalent of doing this:
```python
from xyn_config import Config

get_method = Config.resource().get
get_method = Config.grab().get
value = get_method('some_config_var')
```

Expand Down
17 changes: 8 additions & 9 deletions docs/dataclasses.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,19 @@ from dataclasses import dataclass

@dataclass
class DataResource(Dependency):
# Making all fields optional, so DataResource can be created lazily:
my_optional_field: str = None
another_optional_field: str = "hello!"
# Making all fields optional, so DataResource can be created lazily:
my_optional_field: str = None
another_optional_field: str = "hello!"


# Get current DataResource resource, print it's another_optional_field;
# Get current DataResource dependency, print it's another_optional_field;
# will print out `hello!`:
print(DataResource.resource().another_optional_field)
print(DataResource.grab().another_optional_field)

DataResource.grab()

DataResource.depend()
DataResource.grab()

DataResource.dependency()

DataResource.resource()
DataResource.grab()

```
16 changes: 8 additions & 8 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Tge subclass will inherit some nice features that make it easier to use.
from udepend import Dependency

# This is an example Dependency class, the intent with this class
# is to treat it as a semi-singleton shared resource.
# is to treat it as a semi-singleton shared dependency.
class MyUniversalDependency(Dependency):

# It's important to allow resources to be allocated with
Expand All @@ -61,28 +61,28 @@ class MyUniversalDependency(Dependency):
#
# Next, we get value of it's `name` attribute:

assert MyUniversalDependency.resource().name == 'original-value'
assert MyUniversalDependency.grab().name == 'original-value'

# Change the value of the name attribute on current resource
MyUniversalDependency.resource().name = 'changed-value'
# Change the value of the name attribute on current dependency
MyUniversalDependency.grab().name = 'changed-value'

# We still have access to the same object, so it has the new value:
assert MyUniversalDependency.resource().name == 'changed-value'
assert MyUniversalDependency.grab().name == 'changed-value'

# Inherit from Dependency allows you to use them as a context manager.
# This allows you to easily/temporarily inject dependencies:

with MyUniversalDependency(name='injected-value'):
# When someone asks for the current resource of `MyResource`,
# When someone asks for the current dependency of `MyResource`,
# they will get the one I created in `with` statement above.

assert MyUniversalDependency.resource().name == 'injected-value'
assert MyUniversalDependency.grab().name == 'injected-value'

# Object we created and temporary activated/injected
# by above `with` statement has been deactivated/uninjected.
# So, the previous object is what is now used:

assert MyUniversalDependency.resource().name == 'changed-value'
assert MyUniversalDependency.grab().name == 'changed-value'
```

There is also a way to get a proxy-object that represents the
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.plugins]
# Pytest plugin to support automatic clearing of all current resource between each test.
# Pytest plugin to support automatic clearing of all current dependency resources between each test.
pytest11 = {glazy_pytest_plugin = "glazy.pytest_plugin"}

[tool.black]
Expand Down
30 changes: 15 additions & 15 deletions tests/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ def test_ensure_pytest_plugin_context_autouse_fixture_working():
# instead of indirectly.
@UContext(resources=[SomeDependency(my_name='first-name')])
def test_decorator_on_context_object():
first_resource = SomeDependency.resource()
first_resource = SomeDependency.grab()
assert first_resource.my_name == 'first-name'
decorated_context = UContext.current()

new_resource = SomeDependency(my_name='new-name')
with UContext(resources=[new_resource]) as with_context:
assert with_context is not decorated_context, "Should be new context"
# I created new UContext and manually added new instance of SomeDependency;
# check to see if it's the current resource and looks intact.
inside_resource = SomeDependency.resource()
# check to see if it's the current dependency and looks intact.
inside_resource = SomeDependency.grab()
assert inside_resource is not first_resource
assert inside_resource is new_resource
assert inside_resource.my_name == 'new-name'
Expand All @@ -62,7 +62,7 @@ def test_decorator_on_context_object():
assert with_context is not decorated_context, "Should be new context"
# Check to see if when adding new UContext, we still get the same SomeDependency
# instance, and that they still look intact with the correct values.
assert SomeDependency.resource() is first_resource
assert SomeDependency.grab() is first_resource
assert first_resource.my_name == 'first-name'
assert new_resource.my_name == 'new-name'

Expand Down Expand Up @@ -120,29 +120,29 @@ def test_module_level_context(glazy_test_context):

# Ensure that when we used the module-level-context, ensure it still uses the current
# context as it's first parent. the `create=False` will ensure it won't add this looked
# up resource to it's self.
# up dependency to it's self.
assert module_level_context.resource(for_type=float, create=False) == 1.2
assert module_level_context is not UContext.current()

# Ensure that we have not float resource in the outer-module version (since create=False).
# Ensure that we have not float dependency in the outer-module version (since create=False).
assert float not in module_level_context._resources

# See if the copied-context has the same resources still
assert SomeDependency.resource().my_name == "start-name"
assert SomeDependency.grab().my_name == "start-name"
assert UContext.current(for_type=int) == 20
assert UContext.current(for_type=str) == "hello-str"


def test_initial_context_resources_with_dict():
my_context = UContext(
resources={int: 'string-as-int-resource', float: False, SomeDependency: 'str-instead'}
resources={int: 'string-as-int-dependency', float: False, SomeDependency: 'str-instead'}
)

with my_context:
# SomeDependency was replaced by a direct-string 'str-instead', testing replacing
# resources with a completely different type (if you want to mock/test something specific).
assert SomeDependency.resource() == 'str-instead'
assert UContext.current(for_type=int) == 'string-as-int-resource'
assert SomeDependency.grab() == 'str-instead'
assert UContext.current(for_type=int) == 'string-as-int-dependency'
assert UContext.current(for_type=float) is False


Expand All @@ -157,7 +157,7 @@ class TestDependency(Dependency):
my_name: str = 'hello!'


module_level_test_resource = TestDependency.resource()
module_level_test_resource = TestDependency.grab()
print(module_level_test_resource)
module_level_test_resource.my_name = "module-level-change"

Expand All @@ -166,14 +166,14 @@ class TestDependency(Dependency):
@pytest.mark.parametrize("test_run", [1, 2])
def test_module_level_resource_in_unit_test(test_run):
# Make sure we have a different TestDependency instance.
assert module_level_test_resource is not TestDependency.resource()
assert module_level_test_resource is not TestDependency.grab()

# Check values, see if they are still at the module-level value
assert TestDependency.resource().my_name == "hello!"
assert TestDependency.grab().my_name == "hello!"

# Do a unit-test change, ensure we don't see it in another unit test.
TestDependency.resource().my_name = "unit-test-change"
assert TestDependency.resource().my_name == "unit-test-change"
TestDependency.grab().my_name = "unit-test-change"
assert TestDependency.grab().my_name == "unit-test-change"


def test_each_unit_test_starts_with_a_single_parentless_root_like_context():
Expand Down
20 changes: 10 additions & 10 deletions tests/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def assert_myclass(param):
assert my_class.my_method() == param
assert my_class_via_proxy_method.my_prop == param
assert my_class_via_proxy_method.my_method() == param
assert MyClass.resource().my_prop == param
assert MyClass.resource().my_method() == param
assert MyClass.grab().my_prop == param
assert MyClass.grab().my_method() == param


def test_shared_threaded_resource():
Expand All @@ -59,11 +59,11 @@ class ThreadSharableDependency(Dependency):
class NonThreadSharableResource(PerThreadDependency):
hello2 = "a"

ThreadSharableDependency.resource().hello = "3"
NonThreadSharableResource.resource().hello2 = "b"
ThreadSharableDependency.grab().hello = "3"
NonThreadSharableResource.grab().hello2 = "b"

assert ThreadSharableDependency.resource().hello == "3"
assert NonThreadSharableResource.resource().hello2 == "b"
assert ThreadSharableDependency.grab().hello == "3"
assert NonThreadSharableResource.grab().hello2 == "b"

thread_out_sharable = None
thread_out_nonsharable = None
Expand All @@ -74,12 +74,12 @@ def thread_func():
nonlocal thread_out_sharable
nonlocal thread_out_nonsharable

# This should produce an '3', since resource can be shared between threads.
thread_out_sharable = ThreadSharableDependency.resource().hello
# This should produce an '3', since dependency can be shared between threads.
thread_out_sharable = ThreadSharableDependency.grab().hello

# This should produce an 'a', since the resource is NOT shared between threads;
# This should produce an 'a', since the dependency is NOT shared between threads;
# the setting of it to 'b' above is in another thread and is NOT shared.
thread_out_nonsharable = NonThreadSharableResource.resource().hello2
thread_out_nonsharable = NonThreadSharableResource.grab().hello2

import threading

Expand Down
Loading

0 comments on commit aafcec8

Please sign in to comment.