Repository navigation
Expand file tree
/
Copy pathExtendedDictionary.cs
More file actions
347 lines (316 loc) · 15.1 KB
/
Copy pathExtendedDictionary.cs
File metadata and controls
347 lines (316 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using Python.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Interfaces;
using System.Collections;
using QuantConnect.Python;
namespace QuantConnect
{
/// <summary>
/// Provides a base class for types holding key value pairs with helper methods for easy usage in Python
/// </summary>
[PandasNonExpandable]
#pragma warning disable CA1708 // Identifiers should differ by more than case
public abstract class ExtendedDictionary<TKey, TValue> : IExtendedDictionary<TKey, TValue>
#pragma warning restore CA1708 // Identifiers should differ by more than case
{
/// <summary>
/// Gets the number of elements contained in the dictionary
/// </summary>
public abstract int Count { get; }
/// <summary>
/// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
/// </summary>
/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only. </exception>
public virtual void Clear()
{
if (IsReadOnly)
{
throw new InvalidOperationException(Messages.ExtendedDictionary.ClearInvalidOperation(this));
}
throw new NotImplementedException(Messages.ExtendedDictionary.ClearMethodNotImplemented);
}
/// <summary>
/// Gets the value associated with the specified key.
/// </summary>
/// <returns>
/// true if the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/> contains an element with the specified key; otherwise, false.
/// </returns>
/// <param name="key">The key whose value to get.</param>
/// <param name="value">When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the <paramref name="value"/> parameter. This parameter is passed uninitialized.</param>
/// <exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null.</exception>
public abstract bool TryGetValue(TKey key, out TValue value);
/// <summary>
/// Checks if the dictionary contains the specified key.
/// </summary>
/// <param name="key">The key to locate in the dictionary</param>
/// <returns>true if the dictionary contains an element with the specified key; otherwise, false.</returns>
public virtual bool ContainsKey(TKey key)
{
return key != null && TryGetValue(key, out _);
}
/// <summary>
/// Gets all the items in the dictionary
/// </summary>
/// <returns>All the items in the dictionary</returns>
public abstract IEnumerable<KeyValuePair<TKey, TValue>> GetItems();
/// <summary>
/// Gets an <see cref="T:System.Collections.Generic.ICollection`1"/> containing the key objects of the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
/// </summary>
/// <returns>
/// A <see cref="T:System.Collections.Generic.ICollection`1"/> containing the key objects of the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/>.
/// </returns>
protected abstract IEnumerable<TKey> GetKeys { get; }
/// <summary>
/// Gets an <see cref="T:System.Collections.Generic.ICollection`1"/> containing the values in the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.Generic.ICollection`1"/> containing the values in the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/>.
/// </returns>
protected abstract IEnumerable<TValue> GetValues { get; }
/// <summary>
/// Gets a value indicating whether the <see cref="IDictionary"/> object is read-only.
/// </summary>
/// <remarks>IDictionary implementation</remarks>
public virtual bool IsReadOnly => true;
/// <summary>
/// Removes the value with the specified key
/// </summary>
/// <param name="key">The key object of the element to remove.</param>
/// <returns>true if the element is successfully found and removed; otherwise, false.</returns>
public virtual bool Remove(TKey key)
{
if (IsReadOnly)
{
throw new InvalidOperationException(Messages.ExtendedDictionary.RemoveInvalidOperation(this));
}
throw new NotImplementedException(Messages.ExtendedDictionary.RemoveMethodNotImplemented);
}
/// <summary>
/// Indexer method for the base dictioanry to access the objects by their symbol.
/// </summary>
/// <remarks>IDictionary implementation</remarks>
/// <param name="key">Key object indexer</param>
/// <returns>Object of <typeparamref name="TValue"/></returns>
public abstract TValue this[TKey key] { get; set; }
/// <summary>
/// Removes all keys and values from the <see cref="IExtendedDictionary{TKey, TValue}"/>.
/// </summary>
public void clear()
{
Clear();
}
/// <summary>
/// Creates a shallow copy of the <see cref="IExtendedDictionary{TKey, TValue}"/>.
/// </summary>
/// <returns>Returns a shallow copy of the dictionary. It doesn't modify the original dictionary.</returns>
public PyDict copy()
{
return fromkeys(GetKeys.ToArray());
}
/// <summary>
/// Creates a new dictionary from the given sequence of elements.
/// </summary>
/// <param name="sequence">Sequence of elements which is to be used as keys for the new dictionary</param>
/// <returns>Returns a new dictionary with the given sequence of elements as the keys of the dictionary.</returns>
public PyDict fromkeys(TKey[] sequence)
{
return fromkeys(sequence, default);
}
/// <summary>
/// Creates a new dictionary from the given sequence of elements with a value provided by the user.
/// </summary>
/// <param name="sequence">Sequence of elements which is to be used as keys for the new dictionary</param>
/// <param name="value">Value which is set to each each element of the dictionary</param>
/// <returns>Returns a new dictionary with the given sequence of elements as the keys of the dictionary.
/// Each element of the newly created dictionary is set to the provided value.</returns>
public PyDict fromkeys(TKey[] sequence, TValue value)
{
using var _ = Py.GIL();
var pyDict = new PyDict();
foreach (var key in sequence.Where(k => k != null))
{
var pyValue = get(key, value);
pyDict.SetItem(key.ToPython(), pyValue.ToPython());
}
return pyDict;
}
/// <summary>
/// Returns the value for the specified key if key is in dictionary.
/// </summary>
/// <param name="key">key to be searched in the dictionary</param>
/// <returns>The value for the specified key if key is in dictionary.
/// None if the key is not found, or if the key is None.</returns>
public TValue get(TKey key)
{
return get(key, default);
}
/// <summary>
/// Returns the value for the specified key if key is in dictionary.
/// </summary>
/// <param name="key">key to be searched in the dictionary</param>
/// <param name="default_value">Value to be returned if the key is not found or if the key is None.</param>
/// <returns>The value for the specified key if key is in dictionary.
/// default_value if the key is not found, or if the key is None.</returns>
public TValue get(TKey key, TValue default_value)
{
if (key == null) return default_value;
if (TryGetValue(key, out TValue data))
{
return data;
}
return default_value;
}
/// <summary>
/// Returns a view object that displays a list of dictionary's (key, value) tuple pairs.
/// </summary>
/// <returns>Returns a view object that displays a list of a given dictionary's (key, value) tuple pair.</returns>
public PyList items()
{
using var _ = Py.GIL();
var pyList = new PyList();
foreach (var (key, value) in GetItems())
{
using var pyKey = key.ToPython();
using var pyValue = value.ToPython();
using var pyKvp = new PyTuple([pyKey, pyValue]);
pyList.Append(pyKvp);
}
return pyList;
}
/// <summary>
/// Returns and removes an arbitrary element (key, value) pair from the dictionary.
/// </summary>
/// <returns>Returns an arbitrary element (key, value) pair from the dictionary
/// removes an arbitrary element(the same element which is returned) from the dictionary.
/// Note: Arbitrary elements and random elements are not same.The popitem() doesn't return a random element.</returns>
public PyTuple popitem()
{
throw new NotSupportedException(Messages.ExtendedDictionary.PopitemMethodNotSupported(this));
}
/// <summary>
/// Returns the value of a key (if the key is in dictionary). If not, it inserts key with a value to the dictionary.
/// </summary>
/// <param name="key">Key with null/None value is inserted to the dictionary if key is not in the dictionary.</param>
/// <returns>The value of the key if it is in the dictionary
/// None if key is not in the dictionary</returns>
public TValue setdefault(TKey key)
{
return setdefault(key, default);
}
/// <summary>
/// Returns the value of a key (if the key is in dictionary). If not, it inserts key with a value to the dictionary.
/// </summary>
/// <param name="key">Key with a value default_value is inserted to the dictionary if key is not in the dictionary.</param>
/// <param name="default_value">Default value</param>
/// <returns>The value of the key if it is in the dictionary
/// default_value if key is not in the dictionary and default_value is specified</returns>
public TValue setdefault(TKey key, TValue default_value)
{
if (TryGetValue(key, out TValue data))
{
return data;
}
if (IsReadOnly)
{
throw new KeyNotFoundException(Messages.ExtendedDictionary.KeyNotFoundDueToNoData(this, key));
}
this[key] = default_value;
return default_value;
}
/// <summary>
/// Removes and returns an element from a dictionary having the given key.
/// </summary>
/// <param name="key">Key which is to be searched for removal</param>
/// <returns>If key is found - removed/popped element from the dictionary
/// If key is not found - KeyError exception is raised</returns>
public TValue pop(TKey key)
{
if (key == null)
{
throw new KeyNotFoundException(Messages.ExtendedDictionary.KeyNotFoundDueToNone);
}
if (TryGetValue(key, out TValue data))
{
Remove(key);
}
return data;
}
/// <summary>
/// Removes and returns an element from a dictionary having the given key.
/// </summary>
/// <param name="key">Key which is to be searched for removal</param>
/// <param name="default_value">Value which is to be returned when the key is not in the dictionary</param>
/// <returns>If key is found - removed/popped element from the dictionary
/// If key is not found - value specified as the second argument(default)</returns>
public TValue pop(TKey key, TValue default_value)
{
if (key != null && TryGetValue(key, out TValue data))
{
Remove(key);
return data;
}
return default_value;
}
/// <summary>
/// Updates the dictionary with the elements from the another dictionary object or from an iterable of key/value pairs.
/// The update() method adds element(s) to the dictionary if the key is not in the dictionary.If the key is in the dictionary, it updates the key with the new value.
/// </summary>
/// <param name="other">Takes either a dictionary or an iterable object of key/value pairs (generally tuples).</param>
public void update(PyObject other)
{
if (IsReadOnly)
{
throw new InvalidOperationException(Messages.ExtendedDictionary.UpdateInvalidOperation(this));
}
var dictionary = other.ConvertToDictionary<TKey, TValue>();
foreach (var kvp in dictionary)
{
this[kvp.Key] = kvp.Value;
}
}
/// <summary>
/// Returns a view object that displays a list of all the key objects in the dictionary
/// </summary>
/// <returns>Returns a view object that displays a list of all the key objects.
/// When the dictionary is changed, the view object also reflect these changes.</returns>
public PyList keys()
{
return GetKeys.ToPyList();
}
/// <summary>
/// Returns a view object that displays a list of all the values in the dictionary.
/// </summary>
/// <returns>Returns a view object that displays a list of all values in a given dictionary.</returns>
public PyList values()
{
return GetValues.ToPyList();
}
/// <summary>
/// Checks if the symbol is implicitly created from a string, in which case it is not in the symbol cache,
/// and throws a KeyNotFoundException.
/// </summary>
protected void CheckForImplicitlyCreatedSymbol(Symbol symbol)
{
if (symbol.ID == new SecurityIdentifier(symbol.ID.Symbol, 0))
{
throw new KeyNotFoundException(Messages.ExtendedDictionary.TickerNotFoundInSymbolCache(symbol.ID.Symbol));
}
}
}
}