{ "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--87aac643-341b-413a-b702-ea5820416155",\n",
" "created": "2018-04-05T18:38:10.269Z",\n",
" "modified": "2018-04-05T18:38:10.269Z",\n",
" "name": "John Smith",\n",
" "identity_class": "individual",\n",
" "x_foo": "bar"\n",
"}\n",
"
{\n",
" "type": "identity",\n",
" "id": "identity--a1ad0a6f-39ab-4642-9a72-aaa198b1eee2",\n",
" "created": "2018-04-05T18:38:12.270Z",\n",
" "modified": "2018-04-05T18:38:12.270Z",\n",
" "name": "John Smith",\n",
" "identity_class": "individual",\n",
" "x_foo": "bar"\n",
"}\n",
"
bar\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",
"