{ "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--d6996982-5fb7-4364-b716-b618516989b6",\n",
" "created": "2020-03-05T05:06:27.349Z",\n",
" "modified": "2020-03-05T05:06:27.349Z",\n",
" "name": "John Smith",\n",
" "identity_class": "individual",\n",
" "x_foo": "bar"\n",
"}\n",
"
{\n",
" "type": "identity",\n",
" "id": "identity--a167d2de-9fc4-4734-a1ae-57a548aad22a",\n",
" "created": "2020-03-05T05:06:29.180Z",\n",
" "modified": "2020-03-05T05:06:29.180Z",\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": "2020-03-05T05:06:32.934Z",\n",
" "name": "John Smith",\n",
" "identity_class": "individual"\n",
"}\n",
"
{\n",
" "type": "x-animal",\n",
" "id": "x-animal--1f7ce0ad-fd3a-4cf0-9cd7-13f7bef9ecd4",\n",
" "created": "2020-03-05T05:06:38.010Z",\n",
" "modified": "2020-03-05T05:06:38.010Z",\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",
" "type": "x-new-observable-2",\n",
" "id": "x-new-observable-2--6bc655d6-dcb8-52a3-a862-46848c17e599",\n",
" "a_property": "A property",\n",
" "property_2": 2000\n",
"}\n",
"
{\n",
" "type": "x-new-observable-2",\n",
" "id": "x-new-observable-2--6bc655d6-dcb8-52a3-a862-46848c17e599",\n",
" "a_property": "A property",\n",
" "property_2": 3000\n",
"}\n",
"
{\n",
" "type": "x-new-observable-2",\n",
" "id": "x-new-observable-2--1e56f9c3-a73b-5fbd-b348-83c76523c4df",\n",
" "a_property": "A different property",\n",
" "property_2": 3000\n",
"}\n",
"
{\n",
" "property1": "something",\n",
" "property2": 10\n",
"}\n",
"
bla\n",
"
50\n",
"