Skip to content

Commit ef6dade

Browse files
authored
Merge pull request #100 from emmanvg/issue-98
Performance, Design and other fixes
2 parents 5aa8c66 + 258ea4b commit ef6dade

File tree

12 files changed

+329
-315
lines changed

12 files changed

+329
-315
lines changed

docs/guide/custom.ipynb

Lines changed: 19 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -99,101 +99,28 @@
9999
},
100100
{
101101
"cell_type": "code",
102-
"execution_count": 5,
102+
"execution_count": 3,
103103
"metadata": {},
104104
"outputs": [
105105
{
106-
"data": {
107-
"text/html": [
108-
"<style type=\"text/css\">.highlight .hll { background-color: #ffffcc }\n",
109-
".highlight { background: #f8f8f8; }\n",
110-
".highlight .c { color: #408080; font-style: italic } /* Comment */\n",
111-
".highlight .err { border: 1px solid #FF0000 } /* Error */\n",
112-
".highlight .k { color: #008000; font-weight: bold } /* Keyword */\n",
113-
".highlight .o { color: #666666 } /* Operator */\n",
114-
".highlight .ch { color: #408080; font-style: italic } /* Comment.Hashbang */\n",
115-
".highlight .cm { color: #408080; font-style: italic } /* Comment.Multiline */\n",
116-
".highlight .cp { color: #BC7A00 } /* Comment.Preproc */\n",
117-
".highlight .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */\n",
118-
".highlight .c1 { color: #408080; font-style: italic } /* Comment.Single */\n",
119-
".highlight .cs { color: #408080; font-style: italic } /* Comment.Special */\n",
120-
".highlight .gd { color: #A00000 } /* Generic.Deleted */\n",
121-
".highlight .ge { font-style: italic } /* Generic.Emph */\n",
122-
".highlight .gr { color: #FF0000 } /* Generic.Error */\n",
123-
".highlight .gh { color: #000080; font-weight: bold } /* Generic.Heading */\n",
124-
".highlight .gi { color: #00A000 } /* Generic.Inserted */\n",
125-
".highlight .go { color: #888888 } /* Generic.Output */\n",
126-
".highlight .gp { color: #000080; font-weight: bold } /* Generic.Prompt */\n",
127-
".highlight .gs { font-weight: bold } /* Generic.Strong */\n",
128-
".highlight .gu { color: #800080; font-weight: bold } /* Generic.Subheading */\n",
129-
".highlight .gt { color: #0044DD } /* Generic.Traceback */\n",
130-
".highlight .kc { color: #008000; font-weight: bold } /* Keyword.Constant */\n",
131-
".highlight .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */\n",
132-
".highlight .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */\n",
133-
".highlight .kp { color: #008000 } /* Keyword.Pseudo */\n",
134-
".highlight .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */\n",
135-
".highlight .kt { color: #B00040 } /* Keyword.Type */\n",
136-
".highlight .m { color: #666666 } /* Literal.Number */\n",
137-
".highlight .s { color: #BA2121 } /* Literal.String */\n",
138-
".highlight .na { color: #7D9029 } /* Name.Attribute */\n",
139-
".highlight .nb { color: #008000 } /* Name.Builtin */\n",
140-
".highlight .nc { color: #0000FF; font-weight: bold } /* Name.Class */\n",
141-
".highlight .no { color: #880000 } /* Name.Constant */\n",
142-
".highlight .nd { color: #AA22FF } /* Name.Decorator */\n",
143-
".highlight .ni { color: #999999; font-weight: bold } /* Name.Entity */\n",
144-
".highlight .ne { color: #D2413A; font-weight: bold } /* Name.Exception */\n",
145-
".highlight .nf { color: #0000FF } /* Name.Function */\n",
146-
".highlight .nl { color: #A0A000 } /* Name.Label */\n",
147-
".highlight .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */\n",
148-
".highlight .nt { color: #008000; font-weight: bold } /* Name.Tag */\n",
149-
".highlight .nv { color: #19177C } /* Name.Variable */\n",
150-
".highlight .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */\n",
151-
".highlight .w { color: #bbbbbb } /* Text.Whitespace */\n",
152-
".highlight .mb { color: #666666 } /* Literal.Number.Bin */\n",
153-
".highlight .mf { color: #666666 } /* Literal.Number.Float */\n",
154-
".highlight .mh { color: #666666 } /* Literal.Number.Hex */\n",
155-
".highlight .mi { color: #666666 } /* Literal.Number.Integer */\n",
156-
".highlight .mo { color: #666666 } /* Literal.Number.Oct */\n",
157-
".highlight .sa { color: #BA2121 } /* Literal.String.Affix */\n",
158-
".highlight .sb { color: #BA2121 } /* Literal.String.Backtick */\n",
159-
".highlight .sc { color: #BA2121 } /* Literal.String.Char */\n",
160-
".highlight .dl { color: #BA2121 } /* Literal.String.Delimiter */\n",
161-
".highlight .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */\n",
162-
".highlight .s2 { color: #BA2121 } /* Literal.String.Double */\n",
163-
".highlight .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */\n",
164-
".highlight .sh { color: #BA2121 } /* Literal.String.Heredoc */\n",
165-
".highlight .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */\n",
166-
".highlight .sx { color: #008000 } /* Literal.String.Other */\n",
167-
".highlight .sr { color: #BB6688 } /* Literal.String.Regex */\n",
168-
".highlight .s1 { color: #BA2121 } /* Literal.String.Single */\n",
169-
".highlight .ss { color: #19177C } /* Literal.String.Symbol */\n",
170-
".highlight .bp { color: #008000 } /* Name.Builtin.Pseudo */\n",
171-
".highlight .fm { color: #0000FF } /* Name.Function.Magic */\n",
172-
".highlight .vc { color: #19177C } /* Name.Variable.Class */\n",
173-
".highlight .vg { color: #19177C } /* Name.Variable.Global */\n",
174-
".highlight .vi { color: #19177C } /* Name.Variable.Instance */\n",
175-
".highlight .vm { color: #19177C } /* Name.Variable.Magic */\n",
176-
".highlight .il { color: #666666 } /* Literal.Number.Integer.Long */</style><div class=\"highlight\"><pre><span></span><span class=\"p\">{</span>\n",
177-
" <span class=\"nt\">&quot;x_foo&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;bar&quot;</span><span class=\"p\">,</span>\n",
178-
" <span class=\"nt\">&quot;type&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;identity&quot;</span><span class=\"p\">,</span>\n",
179-
" <span class=\"nt\">&quot;id&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;identity--8d7f0697-e589-4e3b-aa57-cae798d2d138&quot;</span><span class=\"p\">,</span>\n",
180-
" <span class=\"nt\">&quot;created&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;2017-09-26T21:02:19.465Z&quot;</span><span class=\"p\">,</span>\n",
181-
" <span class=\"nt\">&quot;modified&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;2017-09-26T21:02:19.465Z&quot;</span><span class=\"p\">,</span>\n",
182-
" <span class=\"nt\">&quot;name&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;John Smith&quot;</span><span class=\"p\">,</span>\n",
183-
" <span class=\"nt\">&quot;identity_class&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;individual&quot;</span>\n",
184-
"<span class=\"p\">}</span>\n",
185-
"</pre></div>\n"
186-
],
187-
"text/plain": [
188-
"<IPython.core.display.HTML object>"
189-
]
190-
},
191-
"execution_count": 5,
192-
"metadata": {},
193-
"output_type": "execute_result"
106+
"name": "stdout",
107+
"output_type": "stream",
108+
"text": [
109+
"{\n",
110+
" \"type\": \"identity\",\n",
111+
" \"id\": \"identity--10761df2-93f6-4eb4-9d02-4fccfe5dc91d\",\n",
112+
" \"created\": \"2017-11-03T18:20:48.145Z\",\n",
113+
" \"modified\": \"2017-11-03T18:20:48.145Z\",\n",
114+
" \"name\": \"John Smith\",\n",
115+
" \"identity_class\": \"individual\",\n",
116+
" \"x_foo\": \"bar\"\n",
117+
"}\n"
118+
]
194119
}
195120
],
196121
"source": [
122+
"from stix2 import Identity\n",
123+
"\n",
197124
"identity = Identity(name=\"John Smith\",\n",
198125
" identity_class=\"individual\",\n",
199126
" custom_properties={\n",
@@ -923,14 +850,14 @@
923850
"language_info": {
924851
"codemirror_mode": {
925852
"name": "ipython",
926-
"version": 2
853+
"version": 3
927854
},
928855
"file_extension": ".py",
929856
"mimetype": "text/x-python",
930857
"name": "python",
931858
"nbconvert_exporter": "python",
932-
"pygments_lexer": "ipython2",
933-
"version": "2.7.12"
859+
"pygments_lexer": "ipython3",
860+
"version": "3.5.2"
934861
}
935862
},
936863
"nbformat": 4,

docs/guide/ts_support.ipynb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@
114114
"import stix2\n",
115115
"\n",
116116
"stix2.v20.Indicator()\n",
117-
"\n",
118117
"stix2.v21.Indicator()"
119118
]
120119
},
@@ -169,9 +168,17 @@
169168
},
170169
{
171170
"cell_type": "code",
172-
"execution_count": null,
173-
"metadata": {},
174-
"outputs": [],
171+
"execution_count": 1,
172+
"metadata": {},
173+
"outputs": [
174+
{
175+
"name": "stdout",
176+
"output_type": "stream",
177+
"text": [
178+
"{\n \"type\": \"indicator\",\n \"id\": \"indicator--dbcbd659-c927-4f9a-994f-0a2632274394\",\n \"created\": \"2017-09-26T23:33:39.829Z\",\n \"modified\": \"2017-09-26T23:33:39.829Z\",\n \"name\": \"File hash for malware variant\",\n \"pattern\": \"[file:hashes.md5 = 'd41d8cd98f00b204e9800998ecf8427e']\",\n \"valid_from\": \"2017-09-26T23:33:39.829952Z\",\n \"labels\": [\n \"malicious-activity\"\n ]\n}\n"
179+
]
180+
}
181+
],
175182
"source": [
176183
"from stix2 import parse\n",
177184
"\n",

stix2/base.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,7 @@ def __setattr__(self, name, value):
153153
super(_STIXBase, self).__setattr__(name, value)
154154

155155
def __str__(self):
156-
properties = self.object_properties()
157-
158-
def sort_by(element):
159-
return find_property_index(self, properties, element)
160-
161-
# separators kwarg -> don't include spaces after commas.
162-
return json.dumps(self, indent=4, cls=STIXJSONEncoder,
163-
item_sort_key=sort_by,
164-
separators=(",", ": "))
156+
return self.serialize(pretty=True)
165157

166158
def __repr__(self):
167159
props = [(k, self[k]) for k in self.object_properties() if self.get(k)]
@@ -185,6 +177,38 @@ def new_version(self, **kwargs):
185177
def revoke(self):
186178
return _revoke(self)
187179

180+
def serialize(self, pretty=False, **kwargs):
181+
"""
182+
Serialize a STIX object.
183+
184+
Args:
185+
pretty (bool): If True, output properties following the STIX specs
186+
formatting. This includes indentation. Refer to notes for more
187+
details.
188+
**kwargs: The arguments for a json.dumps() call.
189+
190+
Returns:
191+
dict: The serialized JSON object.
192+
193+
Note:
194+
The argument ``pretty=True`` will output the STIX object following
195+
spec order. Using this argument greatly impacts object serialization
196+
performance. If your use case is centered across machine-to-machine
197+
operation it is recommended to set ``pretty=False``.
198+
199+
When ``pretty=True`` the following key-value pairs will be added or
200+
overridden: indent=4, separators=(",", ": "), item_sort_key=sort_by.
201+
"""
202+
if pretty:
203+
properties = self.object_properties()
204+
205+
def sort_by(element):
206+
return find_property_index(self, properties, element)
207+
208+
kwargs.update({'indent': 4, 'separators': (",", ": "), 'item_sort_key': sort_by})
209+
210+
return json.dumps(self, cls=STIXJSONEncoder, **kwargs)
211+
188212

189213
class _Observable(_STIXBase):
190214

0 commit comments

Comments
 (0)