Skip to content

Instantly share code, notes, and snippets.

with open('new.txt') as f, open('orig.txt') as f2:
def zipper(f, f2):
for line in zip(f, f2):
yield line
next(f2) # skip every other f2 line
with open('out.txt', 'w') as out:
out.writelines(zipper(f,f2))
# then replace orig.txt with out.txt
# maybe os.remove('orig.txt'); os.rename('out.txt', 'orig.txt') ?
def method_real(self, args):
def _validator(self, args):
do-validation
_validator(args)
do-real-thing
# this will read the file line by line
with open("path/to/file") as infile:
for line in infile:
do_something_to_the(line)
# this will see if a sequence adds up to 15
sum(some_sequence) == 15
#Read Custom Animations
import re
import binascii
anim_1 = (r'fire_0_anim.txt')
#repr of anim_1 " '16,\n17,\n18,\n19,\n20,\n21,\n22,\n23,\n24,\n25,\n26,\n27,\n28' "
with open(anim_1, 'rb') as anim_1_:
anim_1_read = anim_1_.read()

Keybase proof

I hereby claim:

  • I am NotTheEconomist on github.
  • I am adsmith (https://keybase.io/adsmith) on keybase.
  • I have a public key whose fingerprint is A492 B2D4 D8F2 31CF 3351 A152 8C09 4DBA 1D8C ADFC

To claim this, I am signing this object:

def binary_search(needle, haystack):
start, end = 0, len(haystack)-1
while start < end:
pivot_idx = (start + end) // 2
pivot = haystack[pivot_idx]
if needle == pivot:
return True
if pivot_idx == start:
# edge case -- I'm sure a better algorithm
# would write this into the while condition
from flask import Flask
from sqlalchemy import SQLAlchemy
db = SQLAlchemy()
def create_app(info=None):
app = Flask(__name__)
# config app commented out
db.init_app(app)
from sqlalchemy import create_engine, Column, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
Base = declarative_base()
class MyRow(Base):
__tablename__ = "m"
id = Column(Integer, primary_key=True)
# assuming you've already populated d as
# d = {"pdf": ... } or whatever
from operator import itemgetter
print("Extension | Count")
for key,val in sorted(d.items(), key=itemgetter(1), reverse=True):
print("{:>9} | {}".format(key, val))
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __sub__(self, other):
"""Distance between two points
Point(1, 1) - Point(1, 0) == 1
"""