Skip to main content

Object used to maintain state.

Project description

# State pattern

**The state pattern tries to deal with common state issues by making it immutable and utilizing copy-on-write.**

This pattern kind of emerged for me out of necessity while working with databases. I had inherited quite a bit of code that was analyzing, creating, editing, and removing data, all in a couple of quite large, borderline godlike, functions.

I can't get into too much detail, but more often than not, the same bit of data was fetched over and over, making it slow. Sometimes bits of the code was working on data that had already been edited by other bits of the code, and whenever there was an error, it tended to leave the data in a very iffy state.

I did not like this one bit.

Further reading: http://segfaultsourcery.com/post/state-pattern

## Installation

### Requirements
* Python 3.5 and up

`$ pip install statepattern`

## Usage

This example shows how to assign and reassign values in a `State` object.

```python
from statepattern import State as S

state1 = S(
dog='water',
horses=5,
)

state2 = state1.assign(dog='not water', cinnamon_toast='man')
print(state1, state2)
```


This example shows how to use a `State` object to chain function calls. A function must accept a `State` as its only argument and return a `State`.

```python
from statepattern import State as S


def increment(state: S) -> S:
points = state.points or 0
return state.assign(points=points + 1)


def double(state: S) -> S:
points = state.points or 0
return state.assign(points=points * 2)


before = S(points=0)

after = (
before
.then(increment)
.then(double)
.then(increment)
)

print(before, after)
```

This example shows how to discard a value by key.

```python
from statepattern import State as S

state1 = S(
dog='water',
horses=5,
)

state2 = state1.discard('dog')
print(state1, state2)
```


## Development
There are no dependencies at all, by design. I still recommend making a virtualenv to work in, however.
```
$ virtualenv venv
$ source venv/bin/activate
$ pip install -e .
```

## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update (or write) tests as appropriate.

## License
[BSD 2-Clause License](https://tldrlegal.com/license/bsd-2-clause-license-(freebsd))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

statepattern-1.0.0.tar.gz (4.2 kB view details)

Uploaded Source

File details

Details for the file statepattern-1.0.0.tar.gz.

File metadata

  • Download URL: statepattern-1.0.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0

File hashes

Hashes for statepattern-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8a9ec6072f7ad9b93abb483f9b7ac32d90a1955b8d13e3cccf5a0ee99be45265
MD5 520e6f2fe46a703dbba1eb684694ce53
BLAKE2b-256 2d5782465d18372f7e0ded85f41ff9909ae7dd2bb771052e742151d66cac4db2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page