add .serialize(pretty=True) around docs/guide/ to accommodate this change

pull/1/head
Emmanuelle Vargas-Gonzalez 2021-03-31 13:56:02 -04:00
parent dd6b847b10
commit 294bd7309e
14 changed files with 58 additions and 58 deletions

View File

@ -27,7 +27,7 @@ the repository on GitHub and clone your fork instead of the main repo:
git clone https://github.com/yourusername/cti-python-stix2.git git clone https://github.com/yourusername/cti-python-stix2.git
2. Install develoment-related dependencies: 2. Install development-related dependencies:
.. prompt:: bash .. prompt:: bash

View File

@ -173,7 +173,7 @@
"indicator = Indicator(name=\"File hash for malware variant\",\n", "indicator = Indicator(name=\"File hash for malware variant\",\n",
" pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\",\n", " pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\",\n",
" pattern_type=\"stix\")\n", " pattern_type=\"stix\")\n",
"print(indicator)" "print(indicator.serialize(pretty=True))"
] ]
}, },
{ {
@ -503,7 +503,7 @@
"\n", "\n",
"malware = Malware(name=\"Poison Ivy\",\n", "malware = Malware(name=\"Poison Ivy\",\n",
" is_family=False)\n", " is_family=False)\n",
"print(malware)" "print(malware.serialize(pretty=True))"
] ]
}, },
{ {
@ -627,7 +627,7 @@
"relationship = Relationship(relationship_type='indicates',\n", "relationship = Relationship(relationship_type='indicates',\n",
" source_ref=indicator.id,\n", " source_ref=indicator.id,\n",
" target_ref=malware.id)\n", " target_ref=malware.id)\n",
"print(relationship)" "print(relationship.serialize(pretty=True))"
] ]
}, },
{ {
@ -736,7 +736,7 @@
], ],
"source": [ "source": [
"relationship2 = Relationship(indicator, 'indicates', malware)\n", "relationship2 = Relationship(indicator, 'indicates', malware)\n",
"print(relationship2)" "print(relationship2.serialize(pretty=True))"
] ]
}, },
{ {
@ -876,7 +876,7 @@
"from stix2 import Bundle\n", "from stix2 import Bundle\n",
"\n", "\n",
"bundle = Bundle(indicator, malware, relationship)\n", "bundle = Bundle(indicator, malware, relationship)\n",
"print(bundle)" "print(bundle.serialize(pretty=True))"
] ]
}, },
{ {
@ -993,7 +993,7 @@
" resolves_to_refs=[\"mac-addr--43f380fd-37c6-476d-8643-60849bf9240e\"]\n", " resolves_to_refs=[\"mac-addr--43f380fd-37c6-476d-8643-60849bf9240e\"]\n",
")\n", ")\n",
"\n", "\n",
"print(ip4)" "print(ip4.serialize(pretty=True))"
] ]
}, },
{ {
@ -1111,7 +1111,7 @@
" resolves_to_refs=[mac_addr_a.id, mac_addr_b.id]\n", " resolves_to_refs=[mac_addr_a.id, mac_addr_b.id]\n",
")\n", ")\n",
"\n", "\n",
"print(ip4_valid_refs)" "print(ip4_valid_refs.serialize(pretty=True))"
] ]
} }
], ],

View File

@ -201,7 +201,7 @@
" custom_properties={\n", " custom_properties={\n",
" \"x_foo\": \"bar\"\n", " \"x_foo\": \"bar\"\n",
" })\n", " })\n",
"print(identity)" "print(identity.serialize(pretty=True))"
] ]
}, },
{ {
@ -313,7 +313,7 @@
" identity_class=\"individual\",\n", " identity_class=\"individual\",\n",
" x_foo=\"bar\",\n", " x_foo=\"bar\",\n",
" allow_custom=True)\n", " allow_custom=True)\n",
"print(identity2)" "print(identity2.serialize(pretty=True))"
] ]
}, },
{ {
@ -533,7 +533,7 @@
], ],
"source": [ "source": [
"identity4 = identity3.new_version(x_foo=None)\n", "identity4 = identity3.new_version(x_foo=None)\n",
"print(identity4)" "print(identity4.serialize(pretty=True))"
] ]
}, },
{ {
@ -671,7 +671,7 @@
"source": [ "source": [
"animal = Animal(species=\"lion\",\n", "animal = Animal(species=\"lion\",\n",
" animal_class=\"mammal\")\n", " animal_class=\"mammal\")\n",
"print(animal)" "print(animal.serialize(pretty=True))"
] ]
}, },
{ {
@ -956,7 +956,7 @@
"\n", "\n",
"new_observable = NewObservable(a_property=\"something\",\n", "new_observable = NewObservable(a_property=\"something\",\n",
" property_2=10)\n", " property_2=10)\n",
"print(new_observable)" "print(new_observable.serialize(pretty=True))"
] ]
}, },
{ {
@ -1458,13 +1458,13 @@
" pass\n", " pass\n",
"\n", "\n",
"new_observable_a = NewObservable2(a_property=\"A property\", property_2=2000)\n", "new_observable_a = NewObservable2(a_property=\"A property\", property_2=2000)\n",
"print(new_observable_a)\n", "print(new_observable_a.serialize(pretty=True))\n",
"\n", "\n",
"new_observable_b = NewObservable2(a_property=\"A property\", property_2=3000)\n", "new_observable_b = NewObservable2(a_property=\"A property\", property_2=3000)\n",
"print(new_observable_b)\n", "print(new_observable_b.serialize(pretty=True))\n",
"\n", "\n",
"new_observable_c = NewObservable2(a_property=\"A different property\", property_2=3000)\n", "new_observable_c = NewObservable2(a_property=\"A different property\", property_2=3000)\n",
"print(new_observable_c)" "print(new_observable_c.serialize(pretty=True))"
] ]
}, },
{ {
@ -1588,7 +1588,7 @@
"\n", "\n",
"new_ext = NewExtension(property1=\"something\",\n", "new_ext = NewExtension(property1=\"something\",\n",
" property2=10)\n", " property2=10)\n",
"print(new_ext)" "print(new_ext.serialize(pretty=True))"
] ]
}, },
{ {

View File

@ -332,11 +332,11 @@
"\n", "\n",
"# get an object that is only in the filesystem\n", "# get an object that is only in the filesystem\n",
"intrusion_set = cs.get('intrusion-set--f3bdec95-3d62-42d9-a840-29630f6cdc1a')\n", "intrusion_set = cs.get('intrusion-set--f3bdec95-3d62-42d9-a840-29630f6cdc1a')\n",
"print(intrusion_set)\n", "print(intrusion_set.serialize(pretty=True))\n",
"\n", "\n",
"# get an object that is only in the TAXII collection\n", "# get an object that is only in the TAXII collection\n",
"ind = cs.get('indicator--a740531e-63ff-4e49-a9e1-a0a3eed0e3e7')\n", "ind = cs.get('indicator--a740531e-63ff-4e49-a9e1-a0a3eed0e3e7')\n",
"print(ind)" "print(ind.serialize(pretty=True))"
] ]
}, },
{ {
@ -593,7 +593,7 @@
} }
], ],
"source": [ "source": [
"print(mem.creator_of(mal))" "print(mem.creator_of(mal).serialize(pretty=True))"
] ]
}, },
{ {

View File

@ -225,7 +225,7 @@
} }
], ],
"source": [ "source": [
"print(env.get(\"indicator--a740531e-63ff-4e49-a9e1-a0a3eed0e3e7\"))" "print(env.get(\"indicator--a740531e-63ff-4e49-a9e1-a0a3eed0e3e7\").serialize(pretty=True))"
] ]
}, },
{ {
@ -360,7 +360,7 @@
"ind = factory.create(Indicator,\n", "ind = factory.create(Indicator,\n",
" pattern_type=\"stix\",\n", " pattern_type=\"stix\",\n",
" pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\")\n", " pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\")\n",
"print(ind)" "print(ind.serialize(pretty=True))"
] ]
}, },
{ {
@ -486,7 +486,7 @@
" created_by_ref=None,\n", " created_by_ref=None,\n",
" pattern_type=\"stix\",\n", " pattern_type=\"stix\",\n",
" pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\")\n", " pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\")\n",
"print(ind2)" "print(ind2.serialize(pretty=True))"
] ]
}, },
{ {
@ -593,7 +593,7 @@
" created_by_ref=\"identity--962cabe5-f7f3-438a-9169-585a8c971d12\",\n", " created_by_ref=\"identity--962cabe5-f7f3-438a-9169-585a8c971d12\",\n",
" pattern_type=\"stix\",\n", " pattern_type=\"stix\",\n",
" pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\")\n", " pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\")\n",
"print(ind3)" "print(ind3.serialize(pretty=True))"
] ]
}, },
{ {
@ -712,7 +712,7 @@
" pattern_type=\"stix\",\n", " pattern_type=\"stix\",\n",
" pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\")\n", " pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\")\n",
"environ.add(i)\n", "environ.add(i)\n",
"print(environ.get(i.id))" "print(environ.get(i.id).serialize(pretty=True))"
] ]
} }
], ],

View File

@ -262,7 +262,7 @@
"mal = fs.get(\"malware--92ec0cbd-2c30-44a2-b270-73f4ec949841\")\n", "mal = fs.get(\"malware--92ec0cbd-2c30-44a2-b270-73f4ec949841\")\n",
"\n", "\n",
"# for visual purposes\n", "# for visual purposes\n",
"print(mal)" "print(mal.serialize(pretty=True))"
] ]
}, },
{ {

View File

@ -176,7 +176,7 @@
"indicator = Indicator(pattern_type=\"stix\",\n", "indicator = Indicator(pattern_type=\"stix\",\n",
" pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\",\n", " pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\",\n",
" object_marking_refs=TLP_AMBER)\n", " object_marking_refs=TLP_AMBER)\n",
"print(indicator)" "print(indicator.serialize(pretty=True))"
] ]
}, },
{ {
@ -290,7 +290,7 @@
" definition_type=\"statement\", \n", " definition_type=\"statement\", \n",
" definition=StatementMarking(statement=\"Copyright 2017, Example Corp\")\n", " definition=StatementMarking(statement=\"Copyright 2017, Example Corp\")\n",
")\n", ")\n",
"print(marking_definition)" "print(marking_definition.serialize(pretty=True))"
] ]
}, },
{ {
@ -405,7 +405,7 @@
"indicator2 = Indicator(pattern_type=\"stix\",\n", "indicator2 = Indicator(pattern_type=\"stix\",\n",
" pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\",\n", " pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\",\n",
" object_marking_refs=marking_definition)\n", " object_marking_refs=marking_definition)\n",
"print(indicator2)" "print(indicator2.serialize(pretty=True))"
] ]
}, },
{ {
@ -513,7 +513,7 @@
"indicator3 = Indicator(pattern_type=\"stix\",\n", "indicator3 = Indicator(pattern_type=\"stix\",\n",
" pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\",\n", " pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\",\n",
" object_marking_refs=\"marking-definition--f88d31f6-486f-44da-b317-01333bde0b82\")\n", " object_marking_refs=\"marking-definition--f88d31f6-486f-44da-b317-01333bde0b82\")\n",
"print(indicator3)" "print(indicator3.serialize(pretty=True))"
] ]
}, },
{ {
@ -650,7 +650,7 @@
" \"marking_ref\": TLP_WHITE\n", " \"marking_ref\": TLP_WHITE\n",
" }\n", " }\n",
" ])\n", " ])\n",
"print(malware)" "print(malware.serialize(pretty=True))"
] ]
}, },
{ {
@ -808,7 +808,7 @@
], ],
"source": [ "source": [
"indicator4 = indicator.add_markings(marking_definition)\n", "indicator4 = indicator.add_markings(marking_definition)\n",
"print(indicator4)" "print(indicator4.serialize(pretty=True))"
] ]
}, },
{ {

View File

@ -187,7 +187,7 @@
"mem.add(ind)\n", "mem.add(ind)\n",
"\n", "\n",
"# for visual purposes\n", "# for visual purposes\n",
"print(mem.get(ind.id))\n" "print(mem.get(ind.id).serialize(pretty=True))\n"
] ]
}, },
{ {
@ -304,7 +304,7 @@
"mem.add([ind2,ind3, mal])\n", "mem.add([ind2,ind3, mal])\n",
"\n", "\n",
"# for visual purposes\n", "# for visual purposes\n",
"print(mem.get(ind3.id))" "print(mem.get(ind3.id).serialize(pretty=True))"
] ]
}, },
{ {
@ -412,7 +412,7 @@
"from stix2 import Filter\n", "from stix2 import Filter\n",
"\n", "\n",
"mal = mem.query([Filter(\"malware_types\",\"=\", \"rootkit\")])[0]\n", "mal = mem.query([Filter(\"malware_types\",\"=\", \"rootkit\")])[0]\n",
"print(mal)" "print(mal.serialize(pretty=True))"
] ]
}, },
{ {
@ -533,7 +533,7 @@
"report = mem_2.get(\"malware--6cee28b8-4d42-4e72-bd77-ea47897672c0\")\n", "report = mem_2.get(\"malware--6cee28b8-4d42-4e72-bd77-ea47897672c0\")\n",
"\n", "\n",
"# for visual purposes\n", "# for visual purposes\n",
"print(report)" "print(report.serialize(pretty=True))"
] ]
} }
], ],

View File

@ -282,7 +282,7 @@
"\n", "\n",
"obj = parse(input_string)\n", "obj = parse(input_string)\n",
"print(type(obj))\n", "print(type(obj))\n",
"print(obj)" "print(obj.serialize(pretty=True))"
] ]
}, },
{ {
@ -483,7 +483,7 @@
"\n", "\n",
"obj = parse(input_dict)\n", "obj = parse(input_dict)\n",
"print(type(obj))\n", "print(type(obj))\n",
"print(obj)" "print(obj.serialize(pretty=True))"
] ]
}, },
{ {
@ -677,7 +677,7 @@
"\n", "\n",
"obj = parse(file_handle)\n", "obj = parse(file_handle)\n",
"print(type(obj))\n", "print(type(obj))\n",
"print(obj)" "print(obj.serialize(pretty=True))"
] ]
}, },
{ {

View File

@ -173,14 +173,21 @@
" pattern_type=\"stix\",\n", " pattern_type=\"stix\",\n",
" pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\")\n", " pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\")\n",
"\n", "\n",
"print(str(indicator))" "print(indicator.serialize(pretty=True))"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"However, the string representation can be slow, as it sorts properties to be in a more readable order. If you need performance and don't care about the human-readability of the output, use the object's `serialize()` function:" "---\n",
"**New in 3.0.0:** \n",
"\n",
"Calling `str()` on a STIX object will call `serialize()` without any formatting options. The change was made to address the performance penalty induced by unknowingly calling with the pretty formatted option. As shown above, to get effect `str()` would in the past versions of the library use the method directly and pass the pretty argument directly `serialize(pretty=True)`.\n",
"\n",
"---\n",
"\n",
"However, the pretty formatted string representation can be slow, as it sorts properties to be in a more readable order. If you need performance and don't care about the human-readability of the output, use the object's `serialize()` function to pass in any arguments `json.dump()` would understand:"
] ]
}, },
{ {
@ -384,13 +391,6 @@
"source": [ "source": [
"print(indicator.serialize(indent=4))" "print(indicator.serialize(indent=4))"
] ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The only difference between this and the string representation from using `str()` is that this will not sort the keys. This works because the keyword arguments are passed to `json.dumps()` internally."
]
} }
], ],
"metadata": { "metadata": {

View File

@ -557,10 +557,10 @@
"stix_obj_versions = tc_source.all_versions(\"indicator--6770298f-0fd8-471a-ab8c-1c658a46574e\")\n", "stix_obj_versions = tc_source.all_versions(\"indicator--6770298f-0fd8-471a-ab8c-1c658a46574e\")\n",
"\n", "\n",
"#for visual purposes\n", "#for visual purposes\n",
"print(stix_obj)\n", "print(stix_obj.serialize(pretty=True))\n",
"print(\"-------\")\n", "print(\"-------\")\n",
"for so in stix_obj_versions:\n", "for so in stix_obj_versions:\n",
" print(so)\n" " print(so.serialize(pretty=True))\n"
] ]
}, },
{ {
@ -959,7 +959,7 @@
"\n", "\n",
"#for visual purposes\n", "#for visual purposes\n",
"for indicator in indicators:\n", "for indicator in indicators:\n",
" print(indicator)" " print(indicator.serialize(pretty=True))"
] ]
}, },
{ {
@ -1113,7 +1113,7 @@
"# TAXIICollectionStore\n", "# TAXIICollectionStore\n",
"stix_obj2 = tc_source.get(\"malware--c0931cc6-c75e-47e5-9036-78fabc95d4ec\")\n", "stix_obj2 = tc_source.get(\"malware--c0931cc6-c75e-47e5-9036-78fabc95d4ec\")\n",
"\n", "\n",
"print(stix_obj2)" "print(stix_obj2.serialize(pretty=True))"
] ]
}, },
{ {

View File

@ -356,7 +356,7 @@
" \"pattern\": \"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\",\n", " \"pattern\": \"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\",\n",
" \"valid_from\": \"2017-09-26T23:33:39.829952Z\"\n", " \"valid_from\": \"2017-09-26T23:33:39.829952Z\"\n",
"}\"\"\", version=\"2.0\")\n", "}\"\"\", version=\"2.0\")\n",
"print(indicator)" "print(indicator.serialize(pretty=True))"
] ]
}, },
{ {

View File

@ -185,7 +185,7 @@
"\n", "\n",
"indicator2 = indicator.new_version(name=\"File hash for Foobar malware\",\n", "indicator2 = indicator.new_version(name=\"File hash for Foobar malware\",\n",
" labels=[\"malicious-activity\"])\n", " labels=[\"malicious-activity\"])\n",
"print(indicator2)" "print(indicator2.serialize(pretty=True))"
] ]
}, },
{ {
@ -326,7 +326,7 @@
], ],
"source": [ "source": [
"indicator3 = indicator.new_version(description=None)\n", "indicator3 = indicator.new_version(description=None)\n",
"print(indicator3)" "print(indicator3.serialize(pretty=True))"
] ]
}, },
{ {
@ -443,7 +443,7 @@
], ],
"source": [ "source": [
"indicator4 = indicator3.revoke()\n", "indicator4 = indicator3.revoke()\n",
"print(indicator4)" "print(indicator4.serialize(pretty=True))"
] ]
} }
], ],

View File

@ -506,7 +506,7 @@
"source": [ "source": [
"for i in indicators():\n", "for i in indicators():\n",
" for obj in i.related():\n", " for obj in i.related():\n",
" print(obj)" " print(obj.serialize(pretty=True))"
] ]
}, },
{ {
@ -621,7 +621,7 @@
"source": [ "source": [
"malware = get('malware--c0931cc6-c75e-47e5-9036-78fabc95d4ec')\n", "malware = get('malware--c0931cc6-c75e-47e5-9036-78fabc95d4ec')\n",
"indicator = malware.related(filters=Filter('type', '=', 'indicator'))\n", "indicator = malware.related(filters=Filter('type', '=', 'indicator'))\n",
"print(indicator[0])" "print(indicator[0].serialize(pretty=True))"
] ]
}, },
{ {