Skip to content

Instantly share code, notes, and snippets.

View salesforce_stream_test.py
#!/Users/ant/temp/.venv/bin/python3
import io
import csv
import random
import string
import requests
import responses
import tracemalloc
@grubberr
grubberr / pty_spawn.py
Created Feb 12, 2022
Usage: pty_spawn.py COMMAND
View pty_spawn.py
#!/usr/bin/env python3
import os
import sys
import pty
def waitstatus_to_exitcode(status):
"""
https://github.com/python/cpython/blob/e0bc8ee945af96f9395659bbe3cc30b082e7a361/Modules/posixmodule.c#L14658-L14748
View iter_lines.py
def iter_lines(gen):
buffer = ""
for chunk in gen:
buffer += chunk
while True:
pos = buffer.find('\n')
if pos <= -1:
break
yield buffer[:pos+1]
buffer = buffer[pos+1:]
@grubberr
grubberr / python_not_free_mem.py
Last active Apr 11, 2019
python not free memory ?
View python_not_free_mem.py
#!/usr/bin/python3
import os
import uuid
import psutil
RELEASE_MEMORY = True
def get_rss_memory():
p = psutil.Process(pid=os.getpid())
@grubberr
grubberr / walk_chdir.py
Last active May 14, 2018
file system walk which can go very deep ( no recursion limit )
View walk_chdir.py
#!/usr/local/bin/python3
import os
import logging
logger = logging.getLogger(__name__)
def commonpath(d1, d2):
" returns the length of longest common leading component "
View mongolock.py
import os
import time
import logging
from datetime import datetime
import pymongo
from mongoengine.connection import DEFAULT_CONNECTION_NAME, get_db
logger = logging.getLogger(os.path.basename(__file__))
@grubberr
grubberr / atomic_get_or_create.py
Created Nov 15, 2017
mongoengine atomic get_or_create using findAndModify
View atomic_get_or_create.py
#!/usr/bin/env python3
from mongoengine import Document, IntField, StringField, connect
connect('test')
class ChatUser(Document):
_access = IntField() # internal access counter
chat_id = IntField(required=True, unique=True)
View counts.py
def counts(nums, maxes):
_nums = sorted(nums)
_maxes = sorted(maxes)
count = 0
res = {}
n = None
m = None
@grubberr
grubberr / walk_traversal_problem.py
Last active Jun 10, 2016
python os.walk traversal problem
View walk_traversal_problem.py
#!/usr/bin/python3
"""
os.walk traversal problem
"""
import os
import sys
import logging
@grubberr
grubberr / earliest_date.py
Last active May 21, 2018
find earliest possible legal date in A/B/C format
View earliest_date.py
#!/usr/bin/python3
import sys
import itertools
from datetime import datetime
def main(s):
" find earliest possible legal date or raise ValueError "