Skip to content

Instantly share code, notes, and snippets.

View DalyaG's full-sized avatar

Dalya Gartzman DalyaG

View GitHub Profile
# When you want to add a new parameter, you only need to add in 2 places:
# In params.py:
class Params:
...
my_new_param: float = 42
# At the "end-point" where this parameter is used:
class VeryDeepModule:
from my_project_params import Params
class SomeMidProcessModule:
def __init__(self, params: Params):
self.params: Params = params # Now, if params is None, we will get an error!
def run(self):
do_somaething(self.params.some_configurable_value)
from my_project_params import Params
class SomeMidProcessModule:
def __init__(self, params: Params = None):
self.params: Params = params or Params() # NO! BAD! if we don't input an instace of Params,
# we will create a new instance with only default values.
def run(self):
do_somaething(self.params.some_configurable_value)
#!/usr/bin/env python3
from optparse import OptionParser
from my_project import MyProject
from my_project_params import Params
if __name__ == "__main__":
parser = OptionParser()
@DalyaG
DalyaG / my_project.py
Last active January 7, 2021 10:15
Instantiate your project with an already existing singlton instance of Params
from my_project_params import Params
class MyProject:
def __init__(self, params: Params):
self.params: Params = params
def run(self):
...
@DalyaG
DalyaG / params.py
Last active March 16, 2023 18:06
One param class for the entire project
import json
import os
from datetime import datetime
from optparse import OptionParser
from dataclasses import dataclass
@dataclass
class Params:
@DalyaG
DalyaG / my_first_cell_in_ipynb.py
Last active December 1, 2021 17:21
Copy paste this to your first cell in Jupyter Notebook to have wide cells, wide outputs, and inline plots.
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:95% !important; }</style>"))
display(HTML("<style>.output_result { max-width:90% !important; }</style>"))
import numpy as np
np.set_printoptions(precision=3)
np.set_printoptions(linewidth=170)
np.set_printoptions(suppress=True)
np.random.seed(42)
@DalyaG
DalyaG / pointers_and_references_arrays_and_pointers.cpp
Created May 18, 2020 17:46
Pointers and references in C++: An Array is a Pointer to an Alias
#include <iostream>
using namespace std;
int main() {
int arr[5];
arr[0] = 1;
cout << "arr[0]=" << arr[0] << endl;
*arr = 2;
cout << "arr[0]=" << arr[0] << endl;
@DalyaG
DalyaG / pointers_and_references_alias_to_pointer.cpp
Created May 18, 2020 17:34
Pointers and references in C++: what happens when c is an alias to the pointer b
#include <iostream>
using namespace std;
void change_val(int &z);
int main() {
int a = 1;
int *b = &a;
int &c = *b;
cout << "c is an alias of whatever b points to\n";
@DalyaG
DalyaG / pointers_and_references_b_gets_value_of_pointer.cpp
Created May 18, 2020 17:31
Pointers and references in C++: what happens when c takes the value that b points to
#include <iostream>
using namespace std;
void change_val(int &z);
int main() {
int a = 1;
int *b = &a;
int c = *b;
cout << "c is an integer that takes the value that b points to\n";