This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Help me implement the Python function, | |
def locate_line(filename, pattern: str, after: str|None, until: str|None) -> int: ... | |
It finds the first line in file matching the re pattern and returns the 1-based lineno, 0 means nothing found. The after and until params specify the optional searching range. | |
Please write concise, clear but readable code. | |
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def make_chunks(text, chunk_size: int, overlap: int = 0): | |
"""Split text into chunks with overlap.""" | |
chunks = [] | |
len_ = len(text) | |
for i in range(0, len_, chunk_size - overlap): | |
chunks.append(text[i: i + chunk_size]) | |
if i + chunk_size >= len_: | |
break | |
return chunks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
import urllib.parse | |
import textwrap | |
import psycopg2 | |
import psycopg2.extras | |
def main(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Any | |
import sqlalchemy as sa | |
from sqlalchemy.dialects.postgresql import array as pg_array, JSONB | |
def jsonb_set(target, path: list[str], val: Any): | |
""" | |
An easy wrapper over sa.func.jsonb_set(). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from contextlib import contextmanager | |
from tempfile import mkstemp | |
@contextmanager | |
def MakeTemporaryFile(suffix=None, prefix=None, dir=None): | |
"""An alternative to tempfile.NamedTemporaryFile working with Windows NT.""" | |
try: | |
fd, name = mkstemp(suffix=suffix, prefix=prefix, dir=dir) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A Task-Step model that can describe steps' upstream and downstream relationships. | |
Also refer to https://docs.sqlalchemy.org/en/20/orm/join_conditions.html#self-referential-many-to-many-relationship | |
""" | |
from typing import Annotated, List | |
import sqlalchemy as sa | |
from sqlalchemy import ForeignKey | |
from sqlalchemy.orm import Mapped, mapped_column, relationship, backref | |
from sqlalchemy.orm import MappedAsDataclass, DeclarativeBase |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AsyncSQLAlchemy: | |
def __init__( | |
self, | |
url: str, | |
engine_options: Optional[Dict[str, Any]] = None, | |
session_options: Optional[Dict[str, Any]] = None, | |
): | |
from sqlalchemy.orm import sessionmaker | |
from sqlalchemy.ext.asyncio import AsyncSession |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
{ | |
"env": { | |
"browser": true, | |
"es2021": true, | |
"commonjs": true, | |
"jest/globals": true | |
}, | |
"plugins": ["jest"], | |
"extends": "eslint:recommended", | |
"overrides": [ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Performs a deep merge of objects and returns new object. Does not modify | |
* objects (immutable) and merges arrays via concatenation. | |
* | |
* @param {...object} objects - Objects to merge | |
* @returns {object} New object with merged key/values | |
*/ | |
export default function mergeDeep(...objects) { | |
const isObject = obj => obj && typeof obj === 'object'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @param {Object[]} dataset | |
* @param {string} key | |
* @returns {Object.<string, Object[]>} | |
* @example groupBy([ | |
* {scope: 'step:1', name: 'API'}, | |
* {scope: 'step:1', name: 'amount'}, | |
* {scope: 'step:2', name: 'Solvent'} | |
* ], 'scope') => { |
NewerOlder