Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ To qualify for inclusion, a snippet must not have any dependencies outside the p

# Summary

The snippets may have more functionality that this -- taking a look at the test suite is the best way to get an exhaustive sense of their functionality -- but these examples will give you the gist of things.
Note that each snippet may have more functionality than shown in this readme -- taking a look at the source code and test suite is the best way to get an exhaustive sense of their capability -- but these examples will give you a sense of when each snippet is useful.

## Colors

Just a shortcut to the `seaborn.color_palette()` of colors in hex:

```python
>>> from pyiron_snippets.colors import SeabornColors
>>> SeabornColors.white
>>> from pyiron_snippets import colors
>>> colors.SeabornColors.white
'#ffffff'

```
Expand All @@ -38,9 +38,9 @@ Just a shortcut to the `seaborn.color_palette()` of colors in hex:
Easily indicate that some functionality is being deprecated

```python
>>> from pyiron_snippets.deprecate import deprecate
>>> from pyiron_snippets import deprecate
>>>
>>> @deprecate(message="Use `bar(a, b)` instead", version="0.5.0")
>>> @deprecate.deprecate(message="Use `bar(a, b)` instead", version="0.5.0")
... def foo(a, b):
... pass
>>>
Expand All @@ -56,9 +56,9 @@ Raises a warning like `DeprecationWarning: __main__.foo is deprecated: Use bar(a
A dictionary that allows dot-access. Has `.items()` etc.

```python
>>> from pyiron_snippets.dotdict import DotDict
>>> from pyiron_snippets import dotdict
>>>
>>> d = DotDict({"a": 1})
>>> d = dotdict.DotDict({"a": 1})
>>> d.b = 2
>>> print(d.a, d.b)
1 2
Expand All @@ -73,7 +73,7 @@ Make dynamic classes that are still pickle-able
>>> from abc import ABC
>>> import pickle
>>>
>>> from pyiron_snippets.factory import classfactory
>>> from pyiron_snippets import factory
>>>
>>> class HasN(ABC):
... '''Some class I want to make dynamically subclass.'''
Expand All @@ -86,7 +86,7 @@ Make dynamic classes that are still pickle-able
... self.x = x
... self.y = y
>>>
>>> @classfactory
>>> @factory.classfactory
... def has_n_factory(n, s="wrapped_function", /):
... return (
... f"{HasN.__name__}{n}{s}", # New class name
Expand Down Expand Up @@ -115,9 +115,9 @@ Make dynamic classes that are still pickle-able
Shortcuts for filesystem manipulation

```python
>>> from pyiron_snippets.files import DirectoryObject
>>> from pyiron_snippets import files
>>>
>>> d = DirectoryObject("some_dir")
>>> d = files.DirectoryObject("some_dir")
>>> d.write(file_name="my_filename.txt", content="Some content")
>>> d.file_exists("my_filename.txt")
True
Expand All @@ -131,9 +131,9 @@ True
A meta-class introducing a `__post__` dunder which runs after the `__init__` of _everything_ in the MRO.

```python
>>> from pyiron_snippets.has_post import HasPost
>>> from pyiron_snippets import has_post
>>>
>>> class Foo(metaclass=HasPost):
>>> class Foo(metaclass=has_post.HasPost):
... def __init__(self, x=0):
... self.x = x
... print(f"Foo.__init__: x = {self.x}")
Expand Down Expand Up @@ -163,14 +163,14 @@ Honestly, try thinking if there's another way to solve your problem; this is a d
Fail gracefully when optional dependencies are missing for (optional) functionality.

```python
>>> from pyiron_snippets.import_alarm import ImportAlarm
>>> from pyiron_snippets import import_alarm
>>>
>>> with ImportAlarm(
>>> with import_alarm.ImportAlarm(
... "Some functionality unavailable: `magic` dependency missing"
... ) as my_magic_alarm:
... import magic
>>>
>>> with ImportAlarm("This warning won't show up") as datetime_alarm:
>>> with import_alarm.ImportAlarm("This warning won't show up") as datetime_alarm:
... import datetime
>>>
>>> class Foo:
Expand Down Expand Up @@ -230,15 +230,15 @@ If at first you don't succeed
```python
>>> from time import time
>>>
>>> from pyiron_snippets.retry import retry
>>> from pyiron_snippets import retry
>>>
>>> def at_most_three_seconds():
... t = int(time())
... if t % 3 != 0:
... raise ValueError("Not yet!")
... return t
>>>
>>> retry(at_most_three_seconds, msg="Tried and failed...", error=ValueError) % 3
>>> retry.retry(at_most_three_seconds, msg="Tried and failed...", error=ValueError) % 3
0

```
Expand All @@ -251,9 +251,9 @@ Depending on the system clock at invokation, this simple example may give warnin
A metaclass for the [singleton pattern](https://en.wikipedia.org/wiki/Singleton_pattern).

```python
>>> from pyiron_snippets.singleton import Singleton
>>> from pyiron_snippets import singleton
>>>
>>> class Foo(metaclass=Singleton):
>>> class Foo(metaclass=singleton.Singleton):
... pass
>>>
>>> foo1 = Foo()
Expand Down
8 changes: 5 additions & 3 deletions notebooks/example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"id": "c44a93e4-1ce4-4a26-a82a-3d2bff41988b",
"metadata": {},
"source": [
"# Demos"
"# Demos\n",
"\n",
"Import and explore various snippets tools. `DotDict` is included to get you rolling."
]
},
{
Expand All @@ -31,9 +33,9 @@
}
],
"source": [
"from pyiron_snippets.dotdict import DotDict\n",
"from pyiron_snippets import dotdict\n",
"\n",
"dd = DotDict({\"foo\": \"this is dot accessible\"})\n",
"dd = dotdict.DotDict({\"foo\": \"this is dot accessible\"})\n",
"print(dd.foo)"
]
},
Expand Down
Loading