Skip to content

Instantly share code, notes, and snippets.

View AdamGold's full-sized avatar
🌏
Trust the process

Adam Goldschmidt AdamGold

🌏
Trust the process
  • Israel
View GitHub Profile
@AdamGold
AdamGold / gist:c0f6e05eb434a9327f80f7d13cd43386
Last active October 28, 2018 20:15
SQLAlchemy Load CSV Files
# from MODELS_MODULE import MODEL
import pandas as pd
CSV_FOLDER = 'csvs'
def load_csv(file, model_name):
df = pd.read_csv('{}/{}.csv'.format(CSV_FOLDER, file))
csv = df.where((pd.notnull(df)), None) # replace Nan with None
for row in csv.itertuples(index=False):
In [2]: import typing
In [3]: class BetterLookingArticle(typing.NamedTuple):
...: title: str
...: id: int
...: description: str = "No description given."
...:
In [4]: BetterLookingArticle(title="Python is cool.", id=1)
BetterLookingArticle(title='Python is cool.', id=1, description='No description given.')
In [6]: import collections
In [7]: Article = collections.namedtuple("Article", ["title", "description", "id"])
In [8]: Article(title="Python is cool.", id=1, description="")
Article(title='Python is cool.', description='', id=1)
In [9]: import array
In [10]: import pickle
In [11]: double_array = array.array("i", range(10 ** 6))
...: start_time = time.time()
...: with open("array_temp.bin", "wb") as f:
...: double_array.tofile(f)
...: array_end_time = time.time() - start_time
In [12]: int_list = list(range(10 ** 6))
...: start_time = time.time()
...: with open("list_temp.bin", "wb") as f:
In [16]: import itertools
In [17]: list(itertools.combinations([1, 2, 3, 4], 2))
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
In [18]: dict.fromkeys(["key1", "key2", "key3"], "DEFAULT_VALUE")
{'key1': 'DEFAULT_VALUE', 'key2': 'DEFAULT_VALUE', 'key3': 'DEFAULT_VALUE'}
In [22]: t = (1, 2, [3, 4])
In [23]: t[2] += [30, 40]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-25-af836a8d44a2> in <module>
----> 1 t[2] += [30, 40]
TypeError: 'tuple' object does not support item assignment
In [24]: t
In [25]: dis.dis("t[a] += b")
1 0 LOAD_NAME 0 (t)
2 LOAD_NAME 1 (a)
4 DUP_TOP_TWO
6 BINARY_SUBSCR
8 LOAD_NAME 2 (b)
10 INPLACE_ADD --> (value in t[a]) += b --> succeeds because list is mutable
12 ROT_THREE
14 STORE_SUBSCR --> Assign t[a] = our list --> Fails, t[a] is immutable.
16 LOAD_CONST 0 (None)
@AdamGold
AdamGold / malloc_struct.c
Last active March 4, 2024 18:17
glibc malloc struct
/* https://github.com/lattera/glibc/blob/895ef79e04a953cac1493863bcae29ad85657ee1/malloc/malloc.c#L1044 */
struct malloc_chunk {
INTERNAL_SIZE_T mchunk_prev_size; /* Size of previous chunk (if free). */
INTERNAL_SIZE_T mchunk_size; /* Size in bytes, including overhead. */
struct malloc_chunk* fd; /* double links -- used only if free. */
struct malloc_chunk* bk;
/* Only used for large blocks: pointer to next larger size. */
class Entry(object):
def __init__(self, key: str, value=None):
self.key = key
self.value = value
@property
def hash(self) -> int:
"""return entry's hash"""
def __repr__(self):