{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Creating a Simple Application\n",
    "\n",
    "The Streams Python API allows a user to create a streams application using only python. The Python API allows for the definition of data sources, transformations, and sinks by performing operations on `Stream` and `Topology` objects.\n",
    "\n",
    "First, it is necessary to import the `Topology` class from the `streamsx.topology` package. This will be used to begin create your application. In addition, the `context` module must be imported, allowing your application to be submitted.\n",
    "\n",
    "Lastly, import the classes of the operators you wish to use. Here we use `Counter`, a source operator which counts from 0 to infinity, and `negative_one`, an operator which returns the negative of every number it is given"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from streamsx.topology.topology import Topology\n",
    "from streamsx.topology import context\n",
    "from my_module import Counter, negative_one"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The Topology Object\n",
    "A Topology object is a container for the structure of your application. Give it a unique name, otherwise it may overwrite other compiled streams applications."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "top = Topology(\"myTop\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Define and Create Your Data Source\n",
    "By creating an instance of the `Counter` operator, we can use it as a data source by invoking the `source` method on your `Topology` object and passing it the `my_counter` object. The output of a source is a `Stream` which represents the flow of data in your applications. A streams consists of a potentially infinite sequence of Python object upon which you can perform subsequent operations (such as multiplying by negative one)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Define source\n",
    "my_counter = Counter()\n",
    "stream_1 = top.source(my_counter)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Performing Operations On Your Data Stream\n",
    "A user might want to perform a number of operations on a stream of live data; for example, extracting sentiment from tweets, keeping track of a GPS device, or monitoring live traffic data. All of these operations take data items of one type and modify them or produce data of another type. With the Python API, this can be achieved by calling `map` on a Stream and passing it an operator.\n",
    "\n",
    "As mentioned before, `negative_one` is a callable class which takes a number, and returns its negative. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# multiple by negative one\n",
    "neg_one = negative_one()\n",
    "stream_2 = stream_1.map(neg_one)\n",
    "\n",
    "# Print stream\n",
    "stream_2.print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Submission\n",
    "Submit the application to be run locally in a single process. The output will be sent to standard output and printed to the screen."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "out = context.submit(\"STANDALONE\", top.graph)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
