Skip to content

Commit

Permalink
Merge pull request projectmesa#522 from djmitche/use-click
Browse files Browse the repository at this point in the history
Use click and add `mesa run`
  • Loading branch information
jackiekazil authored May 14, 2018
2 parents 644a992 + a0be39c commit 409b479
Show file tree
Hide file tree
Showing 19 changed files with 89 additions and 30 deletions.
4 changes: 2 additions & 2 deletions docs/best-practices.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ underscores, such as ``thunder_cats``. Within that directory:
* ``server.py`` should contain the visualization support, including the server
class.

* ``run.py`` is a Python script that will run the model when invoked as
``python run.py``.
* ``run.py`` is a Python script that will run the model when invoked via
``mesa run``.

After the number of files grows beyond a half-dozen, try to use sub-folders to
organize them. For example, if the visualization uses image files, put those in
Expand Down
4 changes: 2 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ Getting started quickly:
$ pip install mesa
To launch an example model, open any of the directories in the `examples <https://github.com/projectmesa/mesa/tree/master/examples>`_ folder and launch the ``run.py`` file there, e.g.:
To launch an example model, clone the `repository <https://github.com/projectmesa/mesa>`_ folder and invoke ``mesa run`` for one of the ``examples/`` subdirectories:

.. code-block:: bash
schelling $ python run.py
schelling $ mesa run examples/wolf_sheep
For more help on using Mesa, check out the following resources:

Expand Down
4 changes: 2 additions & 2 deletions examples/Schelling/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ To install the dependencies use pip and the requirements.txt in this directory.

## How to Run

To run the model interactively, run ``run.py`` in this directory. e.g.
To run the model interactively, run ``mesa run`` in this directory. e.g.

```
$ python run.py
$ mesa run
```

Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and press Reset, then Run.
Expand Down
4 changes: 2 additions & 2 deletions examples/bank_reserves/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ To install the dependencies use pip and the requirements.txt in this directory.

## Interactive Model Run

To run the model interactively, run ``run.py`` in this directory. e.g.
To run the model interactively, use `mesa run` in this directory:

```
$ python run.py
$ mesa run
```

Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/), select the model parameters, press Reset, then Start.
Expand Down
2 changes: 1 addition & 1 deletion examples/boid-flockers/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and p
=======
* Launch the visualization
```
$ python run.py
$ mesa run
```
* Visit your browser: http://127.0.0.1:8521/
* In your browser hit *run*
6 changes: 3 additions & 3 deletions examples/boltzmann_wealth_model_network/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ To install the dependencies use pip and the requirements.txt in this directory.

## How to Run

To run the model interactively, run ``run.py`` in this directory. e.g.
To run the model interactively, run ``mesa run`` in this directory. e.g.

```
$ python run.py
$ mesa run
```

Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and press Reset, then Run.
Expand All @@ -45,4 +45,4 @@ This model is drawn from econophysics and presents a statistical mechanics appro

[Milakovic, M. A Statistical Equilibrium Model of Wealth Distribution. February, 2001.](https://editorialexpress.com/cgi-bin/conference/download.cgi?db_name=SCE2001&paper_id=214)

[Dragulescu, A and Yakovenko, V. Statistical Mechanics of Money, Income, and Wealth: A Short Survey. November, 2002](http://arxiv.org/pdf/cond-mat/0211175v1.pdf)
[Dragulescu, A and Yakovenko, V. Statistical Mechanics of Money, Income, and Wealth: A Short Survey. November, 2002](http://arxiv.org/pdf/cond-mat/0211175v1.pdf)
4 changes: 2 additions & 2 deletions examples/color_patches/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ An agent's state represents its "opinion" and is shown by the color of the cell

## How to Run

To run the model interactively, run ``run.py`` in this directory. e.g.
To run the model interactively, run ``mesa run`` in this directory. e.g.

```
$ python run.py
$ mesa run
```

Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and press Reset, then Run.
Expand Down
4 changes: 2 additions & 2 deletions examples/conways_game_of_life/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ The "game" is a zero-player game, meaning that its evolution is determined by it

## How to Run

To run the model interactively, run ``run.py`` in this directory:
To run the model interactively, run ``mesa run`` in this directory. e.g.

```
$ python run.py
$ mesa run
```

Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and press ``run``.
Expand Down
4 changes: 2 additions & 2 deletions examples/forest_fire/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ The [forest fire model](http://en.wikipedia.org/wiki/Forest-fire_model) is a sim

## How to Run

Launch an interactive server by Running ``run.py``:
To run the model interactively, run ``mesa run`` in this directory. e.g.

```
$ python run.py
$ mesa run
```

Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and press Reset, then Run.
Expand Down
4 changes: 2 additions & 2 deletions examples/hex_snowflake/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ In this model, each dead cell will become alive if it has exactly one neighbor.

## How to Run

To run the model interactively, run ``run.py`` in this directory:
To run the model interactively, run ``mesa run`` in this directory. e.g.

```
$ python run.py
$ mesa run
```

Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and press ``run``.
Expand Down
2 changes: 1 addition & 1 deletion examples/pd_grid/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The Demographic Prisoner's Dilemma demonstrates how simple rules can lead to the

##### Web based model simulation

Run ``python run.py``.
To run the model interactively, run ``mesa run`` in this directory.

##### Jupyter Notebook

Expand Down
4 changes: 2 additions & 2 deletions examples/shape_example/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ e.g.

## How to Run

To run the model interactively, run ``run.py`` in this directory. e.g.
To run the model interactively, run ``mesa run`` in this directory. e.g.

```
$ python run.py
$ mesa run
```

Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and
Expand Down
4 changes: 2 additions & 2 deletions examples/sugarscape_cg/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ To install the dependencies use pip and the requirements.txt in this directory.

## How to Run

To run the model interactively, run ``run.py`` in this directory. e.g.
To run the model interactively, run ``mesa run`` in this directory. e.g.

```
$ python run.py
$ mesa run
```

Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and press Reset, then Run.
Expand Down
4 changes: 2 additions & 2 deletions examples/virus_on_network/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ To install the dependencies use pip and the requirements.txt in this directory.

## How to Run

To run the model interactively, run ``run.py`` in this directory. e.g.
To run the model interactively, run ``mesa run`` in this directory. e.g.

```
$ python run.py
$ mesa run
```

Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and press Reset, then Run.
Expand Down
4 changes: 2 additions & 2 deletions examples/wolf_sheep/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ To install the dependencies use pip and the requirements.txt in this directory.

## How to Run

To run the model interactively, run ``run.py`` in this directory. e.g.
To run the model interactively, run ``mesa run`` in this directory. e.g.

```
$ python run.py
$ mesa run
```

Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and press Reset, then Run.
Expand Down
26 changes: 26 additions & 0 deletions mesa/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import sys
import os
import click

PROJECT_PATH = click.Path(exists=True, file_okay=False, dir_okay=True, resolve_path=True)


@click.group()
def cli():
'Manage Mesa projects'


@cli.command()
@click.argument('project', type=PROJECT_PATH, default='.')
def run(project):
'''Run mesa project PROJECT
PROJECT is the path to the directory containing `run.py`, or the current
directory if not specified.
'''
sys.path.insert(0, project)
os.chdir(project)

with open("run.py") as f:
code = compile(f.read(), "run.py", 'exec')
exec(code, {}, {})
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ tornado==4.5.3
flake8==2.5.2
tqdm
networkx==2.1

click==6.7

5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'numpy',
'pandas',
'tqdm',
'click',
]

version = ''
Expand Down Expand Up @@ -47,4 +48,8 @@
'Development Status :: 3 - Alpha',
'Natural Language :: English',
],
entry_points='''
[console_scripts]
mesa=mesa.main:cli
''',
)
28 changes: 28 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os
import sys
import unittest
from unittest.mock import patch
from click.testing import CliRunner

from mesa.main import cli


class TestCli(unittest.TestCase):
'''
Test CLI commands
'''

def setUp(self):
self.old_sys_path = sys.path[:]
self.runner = CliRunner()

def tearDown(self):
sys.path[:] = self.old_sys_path

def test_run(self):
with patch('mesa.visualization.ModularVisualization.ModularServer') as ModularServer:
example_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '../examples/wolf_sheep'))
with self.runner.isolated_filesystem():
result = self.runner.invoke(cli, ['run', example_dir])
assert result.exit_code == 0, result.output
assert ModularServer().launch.call_count == 1

0 comments on commit 409b479

Please sign in to comment.