{ "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", " value.__cause__ = None # suppress chained exceptions\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",
" "spec_version": "2.1",\n",
" "id": "identity--a4c49251-0ad1-44e6-8cfc-3dbd75e73fbd",\n",
" "created": "2020-06-24T18:29:07.107425Z",\n",
" "modified": "2020-06-24T18:29:07.107425Z",\n",
" "name": "John Smith",\n",
" "identity_class": "individual",\n",
" "x_foo": "bar"\n",
"}\n",
"
{\n",
" "type": "identity",\n",
" "spec_version": "2.1",\n",
" "id": "identity--50c33f36-362b-4815-9f97-f3c7f39aa691",\n",
" "created": "2020-06-24T18:29:15.435425Z",\n",
" "modified": "2020-06-24T18:29:15.435425Z",\n",
" "name": "John Smith",\n",
" "identity_class": "individual",\n",
" "x_foo": "bar"\n",
"}\n",
"
bar\n",
"
{\n",
" "type": "identity",\n",
" "spec_version": "2.1",\n",
" "id": "identity--311b2d2d-f010-4473-83ec-1edf84858f4c",\n",
" "created": "2015-12-21T19:59:11.000Z",\n",
" "modified": "2020-06-24T18:29:24.099Z",\n",
" "name": "John Smith",\n",
" "identity_class": "individual"\n",
"}\n",
"
{\n",
" "type": "x-animal",\n",
" "spec_version": "2.1",\n",
" "id": "x-animal--c7dbda16-360a-4622-b9c7-91f0497167cc",\n",
" "created": "2020-06-24T18:33:29.856926Z",\n",
" "modified": "2020-06-24T18:33:29.856926Z",\n",
" "species": "lion",\n",
" "animal_class": "mammal"\n",
"}\n",
"
shark\n",
"
{\n",
" "type": "x-new-observable",\n",
" "id": "x-new-observable--fdb5fd26-533e-44f4-9463-e8ade73e08c0",\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--cafee477-4edc-58fd-81c1-2e23e93f9326",\n",
" "a_property": "A property",\n",
" "property_2": 2000\n",
"}\n",
"
{\n",
" "type": "x-new-observable-2",\n",
" "id": "x-new-observable-2--cafee477-4edc-58fd-81c1-2e23e93f9326",\n",
" "a_property": "A property",\n",
" "property_2": 3000\n",
"}\n",
"
{\n",
" "type": "x-new-observable-2",\n",
" "id": "x-new-observable-2--2945b948-7361-5204-a630-31b828af920c",\n",
" "a_property": "A different property",\n",
" "property_2": 3000\n",
"}\n",
"
{\n",
" "property1": "something",\n",
" "property2": 10\n",
"}\n",
"
bla\n",
"
50\n",
"