Intern study summary - Jun 28 Wed
Key shortcut
My new key board arrived today. I used to use finger combination on the track pad to switch between screens and scroll. Now, I have to use mouse and keyboard. Thus, I will also summarize some of key board shortcut.
Note: I am using MacOS
Switch screen: Control + Left/Right Arrow
Scroll horizontally: press Shift
while scroll with
mouse
Switch between tabs in chrome: cmd + Shift + A
Multiple __init()__
in one class
In Python, a class can only have one __init__
method. If
you define more than one __init__
method, the last one you
define will overwrite the previous ones.
However, there are ways to handle different numbers of parameters
while initializing an instance in Python. The most common way is to use
default arguments, variable-length arguments or keyword arguments in the
__init__
method.
Here's an example of using default arguments:
1 |
|
Another way is using variable-length arguments (*args
)
or keyword arguments (**kwargs
):
1 |
|
Remember that using *args
and **kwargs
can
make your code less clear, because it's not immediately obvious which
parameters the class expects. If you know which parameters your class
should accept, it's often clearer to use default arguments.
Operation with YAML file
You may find my introduction to YAML format in my blog: Intern study summary Jun 15 Thu.
You can use the yaml
library in Python to write a
dictionary to a YAML file. The yaml
library is not included
in the standard Python library, so you'll need to install it first. You
can do so with pip:
1 |
|
Here is an example of how to write a dictionary to a YAML file:
1 |
|
In this example, yaml.dump()
is used to write the data
to the file. The output file ('output.yaml') will look something like
this:
1 |
|
Note: YAML is a human-readable data-serialization language. It is commonly used for configuration files and in applications where data is being stored or transmitted.
List Comprehension
I had list comprehension questions while I was having an interview for a intern position at a quant company. In fact, I did a terrible job on that. Here I will provide two examples for rewrite a for loop in list comprehension form.
Example 1:
Rewrite:
1 |
|
Answer:
1 |
|
In this single line of code, a new dictionary is created for each
item in top_continuous_pairs
, with "exchange" set to the
value of self.exchange
and "pairDb" set to the current
item. The result is a list of these dictionaries, which is assigned to
symbols
.
This is equivalent to the original for-loop, but more concise. Note that list comprehensions can be harder to read than regular for-loops when they start getting complex, so use them wisely.
Example 2:
Rewrite:
1 |
|
The code above creates a deep copy of each dictionary in
symbols
and then adds a new key-value pair, where "channel"
is the key and self.channel
is the value. After modifying
the dictionary, it is then appended to the backtest_pairs
list.
This for-loop can be rewritten as a list comprehension like so:
1 |
|
In this list comprehension,
{**s, "channel": self.channel}
creates a new dictionary
which is a copy of s
with an additional key-value pair
where "channel" is the key and self.channel
is the value.
The {**s}
syntax is using dictionary unpacking to create a
copy of the dictionary s
.
Remember that list comprehensions are great for writing more concise code, but they can also be more difficult to read if they get too complex. Always strive for the right balance between conciseness and readability in your code.
About the **s
Does the *s
have any relationship with pointer in C?
No, the **s
in Python does not have any relationship
with pointers in C.
In Python, the **
operator is used for different
purposes:
When used in a function call, it's used for unpacking dictionary key-value pairs as keyword arguments in a function. For example:
1
2
3
4
5def func(a=1, b=2):
print(a, b)
args = {'a': 3, 'b': 4}
func(**args) # This will output: 3 4In this case,
**args
is unpacking the dictionary and passing the values to the function as keyword arguments.When used in a function definition, it's used for accepting an arbitrary number of keyword arguments. For example:
1
2
3
4
5def func(**kwargs):
for key, value in kwargs.items():
print(f"{key} = {value}")
func(a=3, b=4, c=5) # This will output: a = 3, b = 4, c = 5Here,
**kwargs
is capturing all keyword arguments passed to the function into a dictionary.When used with a numeric type,
*
is the exponentiation operator. For example:1
print(2 ** 3) # This will output: 8
Here,
**
is used for raising 2 to the power of 3.
In C, the *
operator is used for a completely different
purpose: it's used for creating and dereferencing pointers. Python does
not have pointers like C, and the **
operator in Python
does not have any relationship with the pointer concept in C.