{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "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": 2, "metadata": { "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, TextLexer\n", "from pygments.formatters import HtmlFormatter\n", "from IPython.display import display, HTML\n", "from IPython.core.interactiveshell import InteractiveShell\n", "\n", "InteractiveShell.ast_node_interactivity = \"all\"\n", "\n", "def json_print(inpt):\n", " string = str(inpt)\n", " formatter = HtmlFormatter()\n", " if string[0] == '{':\n", " lexer = JsonLexer()\n", " else:\n", " lexer = TextLexer()\n", " return HTML('{}'.format(\n", " formatter.get_style_defs('.highlight'),\n", " highlight(string, lexer, formatter)))\n", "\n", "globals()['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": 3, "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": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
{\n",
" "type": "identity",\n",
" "id": "identity--e7fd0fe0-25ff-4fcb-abe5-b6254a9d1a22",\n",
" "created": "2019-07-25T18:18:18.241Z",\n",
" "modified": "2019-07-25T18:18:18.241Z",\n",
" "name": "John Smith",\n",
" "identity_class": "individual",\n",
" "x_foo": "bar"\n",
"}\n",
"
{\n",
" "type": "identity",\n",
" "id": "identity--033b5f42-c94f-488f-8efa-2b6a167f0d6f",\n",
" "created": "2019-07-25T18:18:21.352Z",\n",
" "modified": "2019-07-25T18:18:21.352Z",\n",
" "name": "John Smith",\n",
" "identity_class": "individual",\n",
" "x_foo": "bar"\n",
"}\n",
"
bar\n",
"
{\n",
" "type": "identity",\n",
" "id": "identity--311b2d2d-f010-4473-83ec-1edf84858f4c",\n",
" "created": "2015-12-21T19:59:11.000Z",\n",
" "modified": "2019-07-25T18:18:25.927Z",\n",
" "name": "John Smith",\n",
" "identity_class": "individual"\n",
"}\n",
"
{\n",
" "type": "x-animal",\n",
" "id": "x-animal--b1e4fe7f-7985-451d-855c-6ba5c265b22a",\n",
" "created": "2018-04-05T18:38:19.790Z",\n",
" "modified": "2018-04-05T18:38:19.790Z",\n",
" "species": "lion",\n",
" "animal_class": "mammal"\n",
"}\n",
"
shark\n",
"
{\n",
" "type": "x-new-observable",\n",
" "a_property": "something",\n",
" "property_2": 10\n",
"}\n",
"
foobaz\n",
"
5\n",
"
{\n",
" "property1": "something",\n",
" "property2": 10\n",
"}\n",
"
bla\n",
"
50\n",
"