Skip to content

Instantly share code, notes, and snippets.

View arshamalh's full-sized avatar
:octocat:
This Octacat is my pet!

Mohammad Reza Karimi arshamalh

:octocat:
This Octacat is my pet!
View GitHub Profile
@arshamalh
arshamalh / Dockerfile
Last active August 3, 2022 21:24
Typescript multistage dockerfile
# Improvement after each stage: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o4htns2axmh87lieoclw.png
# Resource 1 => https://simplernerd.com/docker-typescript-production/
# Resource 2 => https://medium.com/@ankit.wal/the-why-and-how-of-multi-stage-docker-build-with-typescript-example-bcadbce2686c
FROM node:18-alpine3.15 as ts-compiler
WORKDIR /usr/app
COPY package*.json ./
COPY tsconfig.json ./
RUN npm install --include=dev
COPY src src
RUN ["npm", "run", "build"]
@arshamalh
arshamalh / pymongo_assistant_01.py
Last active March 23, 2022 11:04
Pymongo Assistant
"""
Written by Arsham Arya
Some snippets around pymongo package to prevent my confusion.
"""
from pymongo import MongoClient
# Initializing
client = MongoClient("mongodb://username:password@host:port/default_db?authSource=admin")
database = client.get_database("db_name")
collection = database.get_collection("collection_name")
@arshamalh
arshamalh / 1_running_scrapy_from_anothor_python_file.py
Last active February 7, 2022 13:58
Running Scrapy spider along fastAPI or APScheduler, from another script or main.py.
from scrapy import crawler
from scrapy.utils.project import get_project_settings
# pip install crochet
from crochet import setup as crochet_setup, run_in_reactor
crochet_setup()
runner = crawler.CrawlerRunner(get_project_settings())
@run_in_reactor
@arshamalh
arshamalh / config.py
Last active March 2, 2022 09:21
Python best way to import environment variables for development and production
"""
Written by Arsham Arya
I'm not sure it's the best way,
So any contribution makes everyone happy!
"""
from os import environ
# pip install python-dotenv
from dotenv import dotenv_values
@arshamalh
arshamalh / paginating_tmdb_trends_through_api.py
Last active February 7, 2022 13:06
Getting TMDB trends using their API
@arshamalh
arshamalh / compound_index_pymongo.py
Created January 21, 2022 15:38
Making compound unique index in Mongodb using pymongo
# Compound unique indexs are indexes made of multiple columns together.
# For example if we have this data:
"""
("arsham", 930)
"""
# We can have ("arsham", 920), ("arsham", 910), Also we can have ("atousa", 930)
# but we can't have ("arsham", 930) again.
# I hope I explained it well enough. 😊
from pymongo import MongoClient, ASCENDING
@arshamalh
arshamalh / contact_book.py
Created January 15, 2022 09:12
simple Tkinter contact book.
from tkinter import *
from tkinter import messagebox
root = Tk()
root.geometry("480x170")
root.title("Raha Contacts books")
Name = StringVar()
Number = StringVar()
Address = StringVar()
@arshamalh
arshamalh / pgCursorPagination.py
Created December 23, 2021 09:40
Cursor pagination in PostgreSQL and Python
# Written by Arsham Arya
# I'm not sure it's the best way,
# So any contribution makes me and other readers happy :)
from psycopg2.pool import ThreadedConnectionPool
import config
_CONNECTION_POOL = None
@arshamalh
arshamalh / uniqueArrayElement.sql
Last active December 23, 2021 08:15
Postgres making arrays taking unique elements
-- Written by Arsham Arya
-- Any contribution makes readers happy.
INSERT INTO table_name (user_id, my_array_field)
VALUES (2233, array[112, 333])
ON CONFLICT (user_id) DO
UPDATE SET my_array_field = array(
SELECT DISTINCT unnest(table_name.my_array_field || array[112, 333])
) WHERE table_name.user_id = 2233;
@arshamalh
arshamalh / pgCursorPagination.js
Created December 13, 2021 07:03
Cursor pagination in PostgreSQL and Nodejs
// Written by Arsham Arya
// I'm not sure it's the best way,
// So any contribution makes me and other readers happy :)
import pgPromise from "pg-promise";
import Cursor from "pg-cursor";
require("dotenv").config();
const pgp = pgPromise();