{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true, "nbsphinx": "hidden" }, "outputs": [], "source": [ "# Delete this cell to re-enable tracebacks\n", "import sys\n", "ipython = get_ipython()\n", "\n", "def hide_traceback(exc_tuple=None, filename=None, tb_offset=None,\n", " exception_only=False, running_compiled_code=False):\n", " etype, value, tb = sys.exc_info()\n", " return ipython._showtraceback(etype, value, ipython.InteractiveTB.get_exception_only(etype, value))\n", "\n", "ipython.showtraceback = hide_traceback" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true, "nbsphinx": "hidden" }, "outputs": [], "source": [ "# JSON output syntax highlighting\n", "from __future__ import print_function\n", "from pygments import highlight\n", "from pygments.lexers import JsonLexer\n", "from pygments.formatters import HtmlFormatter\n", "from IPython.display import HTML\n", "\n", "original_print = print\n", "\n", "def json_print(inpt):\n", " string = str(inpt)\n", " if string[0] == '{':\n", " formatter = HtmlFormatter()\n", " return HTML('{}'.format(\n", " formatter.get_style_defs('.highlight'),\n", " highlight(string, JsonLexer(), formatter)))\n", " else:\n", " original_print(inpt)\n", "\n", "print = json_print" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Custom STIX Content" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Custom Properties\n", "\n", "Attempting to create a STIX object with properties not defined by the specification will result in an error. Try creating an ``Identity`` object with a custom ``x_foo`` property:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "ename": "ExtraPropertiesError", "evalue": "Unexpected properties for Identity: (x_foo).", "output_type": "error", "traceback": [ "\u001b[0;31mExtraPropertiesError\u001b[0m\u001b[0;31m:\u001b[0m Unexpected properties for Identity: (x_foo).\n" ] } ], "source": [ "from stix2 import Identity\n", "\n", "Identity(name=\"John Smith\",\n", " identity_class=\"individual\",\n", " x_foo=\"bar\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To create a STIX object with one or more custom properties, pass them in as a dictionary parameter called ``custom_properties``:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
{\n",
" "type": "identity",\n",
" "id": "identity--00c5743f-2d5e-4d66-88f1-1842584f4519",\n",
" "created": "2017-11-09T16:17:44.596Z",\n",
" "modified": "2017-11-09T16:17:44.596Z",\n",
" "name": "John Smith",\n",
" "identity_class": "individual",\n",
" "x_foo": "bar"\n",
"}\n",
"
{\n",
" "x_foo": "bar",\n",
" "type": "identity",\n",
" "id": "identity--1e8188eb-245f-400b-839d-7f612169c514",\n",
" "created": "2017-09-26T21:02:22.708Z",\n",
" "modified": "2017-09-26T21:02:22.708Z",\n",
" "name": "John Smith",\n",
" "identity_class": "individual"\n",
"}\n",
"
{\n",
" "type": "x-animal",\n",
" "id": "x-animal--caebdf17-9d2a-4c84-8864-7406326618f0",\n",
" "created": "2017-09-26T21:02:34.724Z",\n",
" "modified": "2017-09-26T21:02:34.724Z",\n",
" "species": "lion",\n",
" "animal_class": "mammal"\n",
"}\n",
"
{\n",
" "type": "x-new-observable",\n",
" "a_property": "something",\n",
" "property_2": 10\n",
"}\n",
"
{\n",
" "property1": "something",\n",
" "property2": 10\n",
"}\n",
"