diff --git a/docs/contributing.rst b/docs/contributing.rst index 20cab3a..a904c31 100644 --- a/docs/contributing.rst +++ b/docs/contributing.rst @@ -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 -2. Install develoment-related dependencies: +2. Install development-related dependencies: .. prompt:: bash diff --git a/docs/guide/creating.ipynb b/docs/guide/creating.ipynb index 4919452..d390875 100644 --- a/docs/guide/creating.ipynb +++ b/docs/guide/creating.ipynb @@ -173,7 +173,7 @@ "indicator = Indicator(name=\"File hash for malware variant\",\n", " pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\",\n", " pattern_type=\"stix\")\n", - "print(indicator)" + "print(indicator.serialize(pretty=True))" ] }, { @@ -503,7 +503,7 @@ "\n", "malware = Malware(name=\"Poison Ivy\",\n", " is_family=False)\n", - "print(malware)" + "print(malware.serialize(pretty=True))" ] }, { @@ -627,7 +627,7 @@ "relationship = Relationship(relationship_type='indicates',\n", " source_ref=indicator.id,\n", " target_ref=malware.id)\n", - "print(relationship)" + "print(relationship.serialize(pretty=True))" ] }, { @@ -736,7 +736,7 @@ ], "source": [ "relationship2 = Relationship(indicator, 'indicates', malware)\n", - "print(relationship2)" + "print(relationship2.serialize(pretty=True))" ] }, { @@ -876,7 +876,7 @@ "from stix2 import Bundle\n", "\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", ")\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", ")\n", "\n", - "print(ip4_valid_refs)" + "print(ip4_valid_refs.serialize(pretty=True))" ] } ], diff --git a/docs/guide/custom.ipynb b/docs/guide/custom.ipynb index 8a4ab10..793702f 100644 --- a/docs/guide/custom.ipynb +++ b/docs/guide/custom.ipynb @@ -201,7 +201,7 @@ " custom_properties={\n", " \"x_foo\": \"bar\"\n", " })\n", - "print(identity)" + "print(identity.serialize(pretty=True))" ] }, { @@ -313,7 +313,7 @@ " identity_class=\"individual\",\n", " x_foo=\"bar\",\n", " allow_custom=True)\n", - "print(identity2)" + "print(identity2.serialize(pretty=True))" ] }, { @@ -533,7 +533,7 @@ ], "source": [ "identity4 = identity3.new_version(x_foo=None)\n", - "print(identity4)" + "print(identity4.serialize(pretty=True))" ] }, { @@ -671,7 +671,7 @@ "source": [ "animal = Animal(species=\"lion\",\n", " animal_class=\"mammal\")\n", - "print(animal)" + "print(animal.serialize(pretty=True))" ] }, { @@ -956,7 +956,7 @@ "\n", "new_observable = NewObservable(a_property=\"something\",\n", " property_2=10)\n", - "print(new_observable)" + "print(new_observable.serialize(pretty=True))" ] }, { @@ -1458,13 +1458,13 @@ " pass\n", "\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", "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", "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", "new_ext = NewExtension(property1=\"something\",\n", " property2=10)\n", - "print(new_ext)" + "print(new_ext.serialize(pretty=True))" ] }, { diff --git a/docs/guide/datastore.ipynb b/docs/guide/datastore.ipynb index 9f8e310..6cad628 100644 --- a/docs/guide/datastore.ipynb +++ b/docs/guide/datastore.ipynb @@ -332,11 +332,11 @@ "\n", "# get an object that is only in the filesystem\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", "# get an object that is only in the TAXII collection\n", "ind = cs.get('indicator--a740531e-63ff-4e49-a9e1-a0a3eed0e3e7')\n", - "print(ind)" + "print(ind.serialize(pretty=True))" ] }, { @@ -593,7 +593,7 @@ } ], "source": [ - "print(mem.creator_of(mal))" + "print(mem.creator_of(mal).serialize(pretty=True))" ] }, { diff --git a/docs/guide/environment.ipynb b/docs/guide/environment.ipynb index a3417b5..deb8e71 100644 --- a/docs/guide/environment.ipynb +++ b/docs/guide/environment.ipynb @@ -225,7 +225,7 @@ } ], "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", " pattern_type=\"stix\",\n", " pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\")\n", - "print(ind)" + "print(ind.serialize(pretty=True))" ] }, { @@ -486,7 +486,7 @@ " created_by_ref=None,\n", " pattern_type=\"stix\",\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", " pattern_type=\"stix\",\n", " pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\")\n", - "print(ind3)" + "print(ind3.serialize(pretty=True))" ] }, { @@ -712,7 +712,7 @@ " pattern_type=\"stix\",\n", " pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\")\n", "environ.add(i)\n", - "print(environ.get(i.id))" + "print(environ.get(i.id).serialize(pretty=True))" ] } ], diff --git a/docs/guide/filesystem.ipynb b/docs/guide/filesystem.ipynb index ec09422..a4aa4d0 100644 --- a/docs/guide/filesystem.ipynb +++ b/docs/guide/filesystem.ipynb @@ -262,7 +262,7 @@ "mal = fs.get(\"malware--92ec0cbd-2c30-44a2-b270-73f4ec949841\")\n", "\n", "# for visual purposes\n", - "print(mal)" + "print(mal.serialize(pretty=True))" ] }, { diff --git a/docs/guide/markings.ipynb b/docs/guide/markings.ipynb index 1e1609c..942f629 100644 --- a/docs/guide/markings.ipynb +++ b/docs/guide/markings.ipynb @@ -176,7 +176,7 @@ "indicator = Indicator(pattern_type=\"stix\",\n", " pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\",\n", " object_marking_refs=TLP_AMBER)\n", - "print(indicator)" + "print(indicator.serialize(pretty=True))" ] }, { @@ -290,7 +290,7 @@ " definition_type=\"statement\", \n", " definition=StatementMarking(statement=\"Copyright 2017, Example Corp\")\n", ")\n", - "print(marking_definition)" + "print(marking_definition.serialize(pretty=True))" ] }, { @@ -405,7 +405,7 @@ "indicator2 = Indicator(pattern_type=\"stix\",\n", " pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\",\n", " object_marking_refs=marking_definition)\n", - "print(indicator2)" + "print(indicator2.serialize(pretty=True))" ] }, { @@ -513,7 +513,7 @@ "indicator3 = Indicator(pattern_type=\"stix\",\n", " pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\",\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", " }\n", " ])\n", - "print(malware)" + "print(malware.serialize(pretty=True))" ] }, { @@ -808,7 +808,7 @@ ], "source": [ "indicator4 = indicator.add_markings(marking_definition)\n", - "print(indicator4)" + "print(indicator4.serialize(pretty=True))" ] }, { diff --git a/docs/guide/memory.ipynb b/docs/guide/memory.ipynb index d8a7000..cf9f035 100644 --- a/docs/guide/memory.ipynb +++ b/docs/guide/memory.ipynb @@ -187,7 +187,7 @@ "mem.add(ind)\n", "\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", "\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", "\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", "\n", "# for visual purposes\n", - "print(report)" + "print(report.serialize(pretty=True))" ] } ], diff --git a/docs/guide/parsing.ipynb b/docs/guide/parsing.ipynb index 5fd9499..0e89126 100644 --- a/docs/guide/parsing.ipynb +++ b/docs/guide/parsing.ipynb @@ -282,7 +282,7 @@ "\n", "obj = parse(input_string)\n", "print(type(obj))\n", - "print(obj)" + "print(obj.serialize(pretty=True))" ] }, { @@ -483,7 +483,7 @@ "\n", "obj = parse(input_dict)\n", "print(type(obj))\n", - "print(obj)" + "print(obj.serialize(pretty=True))" ] }, { @@ -677,7 +677,7 @@ "\n", "obj = parse(file_handle)\n", "print(type(obj))\n", - "print(obj)" + "print(obj.serialize(pretty=True))" ] }, { diff --git a/docs/guide/serializing.ipynb b/docs/guide/serializing.ipynb index 9e7e268..fd91c60 100644 --- a/docs/guide/serializing.ipynb +++ b/docs/guide/serializing.ipynb @@ -173,14 +173,21 @@ " pattern_type=\"stix\",\n", " pattern=\"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\")\n", "\n", - "print(str(indicator))" + "print(indicator.serialize(pretty=True))" ] }, { "cell_type": "markdown", "metadata": {}, "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": [ "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": { diff --git a/docs/guide/taxii.ipynb b/docs/guide/taxii.ipynb index 2ec5c24..c36962e 100644 --- a/docs/guide/taxii.ipynb +++ b/docs/guide/taxii.ipynb @@ -557,10 +557,10 @@ "stix_obj_versions = tc_source.all_versions(\"indicator--6770298f-0fd8-471a-ab8c-1c658a46574e\")\n", "\n", "#for visual purposes\n", - "print(stix_obj)\n", + "print(stix_obj.serialize(pretty=True))\n", "print(\"-------\")\n", "for so in stix_obj_versions:\n", - " print(so)\n" + " print(so.serialize(pretty=True))\n" ] }, { @@ -959,7 +959,7 @@ "\n", "#for visual purposes\n", "for indicator in indicators:\n", - " print(indicator)" + " print(indicator.serialize(pretty=True))" ] }, { @@ -1113,7 +1113,7 @@ "# TAXIICollectionStore\n", "stix_obj2 = tc_source.get(\"malware--c0931cc6-c75e-47e5-9036-78fabc95d4ec\")\n", "\n", - "print(stix_obj2)" + "print(stix_obj2.serialize(pretty=True))" ] }, { diff --git a/docs/guide/ts_support.ipynb b/docs/guide/ts_support.ipynb index 99ef893..8c07134 100644 --- a/docs/guide/ts_support.ipynb +++ b/docs/guide/ts_support.ipynb @@ -356,7 +356,7 @@ " \"pattern\": \"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\",\n", " \"valid_from\": \"2017-09-26T23:33:39.829952Z\"\n", "}\"\"\", version=\"2.0\")\n", - "print(indicator)" + "print(indicator.serialize(pretty=True))" ] }, { diff --git a/docs/guide/versioning.ipynb b/docs/guide/versioning.ipynb index 45686bf..62d743f 100644 --- a/docs/guide/versioning.ipynb +++ b/docs/guide/versioning.ipynb @@ -185,7 +185,7 @@ "\n", "indicator2 = indicator.new_version(name=\"File hash for Foobar malware\",\n", " labels=[\"malicious-activity\"])\n", - "print(indicator2)" + "print(indicator2.serialize(pretty=True))" ] }, { @@ -326,7 +326,7 @@ ], "source": [ "indicator3 = indicator.new_version(description=None)\n", - "print(indicator3)" + "print(indicator3.serialize(pretty=True))" ] }, { @@ -443,7 +443,7 @@ ], "source": [ "indicator4 = indicator3.revoke()\n", - "print(indicator4)" + "print(indicator4.serialize(pretty=True))" ] } ], diff --git a/docs/guide/workbench.ipynb b/docs/guide/workbench.ipynb index c0c4ee0..def48b8 100644 --- a/docs/guide/workbench.ipynb +++ b/docs/guide/workbench.ipynb @@ -506,7 +506,7 @@ "source": [ "for i in indicators():\n", " for obj in i.related():\n", - " print(obj)" + " print(obj.serialize(pretty=True))" ] }, { @@ -621,7 +621,7 @@ "source": [ "malware = get('malware--c0931cc6-c75e-47e5-9036-78fabc95d4ec')\n", "indicator = malware.related(filters=Filter('type', '=', 'indicator'))\n", - "print(indicator[0])" + "print(indicator[0].serialize(pretty=True))" ] }, {