Skip to content

Instantly share code, notes, and snippets.

d = {}
with open("dictfile.txt") as f:
d.update([map(str.strip, line.split("=")) for line in f])
# alternatively:
# for line in f:
# k, v = line.split('=')
# d[k.strip()] = v.strip()
with open("wordfile.txt") as f:
from enum import Enum
puzzle = "L5, R1, L5, L1, R5, R1, R1, L4, L1, L3, R2, R4, L4, L1, L1, R2, R4, R3, L1, R4, L4, L5, L4, R4, L5, R1, R5, L2, R1, R3, L2, L4, L4, R1, L192, R5, R1, R4, L5, L4, R5, L1, L1, R48, R5, R5, L2, R4, R4, R1, R3, L1, L4, L5, R1, L4, L2, L5, R5, L2, R74, R4, L1, R188, R5, L4, L2, R5, R2, L4, R4, R3, R3, R2, R1, L3, L2, L5, L5, L2, L1, R1, R5, R4, L3, R5, L1, L3, R4, L1, L3, L2, R1, R3, R2, R5, L3, L1, L1, R5, L4, L5, R5, R2, L5, R2, L1, L5, L3, L5, L5, L1, R1, L4, L3, L1, R2, R5, L1, L3, R4, R5, L4, L1, R5, L1, R5, R5, R5, R2, R1, R2, L5, L5, L5, R4, L5, L4, L4, R5, L2, R1, R5, L1, L5, R4, L3, R4, L2, R3, R3, R3, L2, L2, L2, L1, L4, R3, L4, L2, R2, R5, L1, R2"
Directions = Enum("Directions", "NORTH EAST SOUTH WEST")
def turn(facing, vector):
# Handle wrapping
if facing == Directions.WEST and vector == "R":
return Directions.NORTH
from enum import Enum
Ranks = Enum("Ranks", "Ace Two Three Four Five Six Seven Eight "
"Nine Ten Jack Queen King")
Suits = Enum("Suits", "Diamonds Clubs Hearts Spades")
class Card(object):
__slots__ = ('_rank', '_suit')
# can be memory saving, especially if you're creating 6+ decks of cards for your shoe, e.g. 300+ cards.
# this can *definitely* be seen as premature optimization, though! Chances are you'll never need to implement __slots__
def prune(f):
def wrapper(*args):
def wrapped(*args):
if wrapper.min:
print("wrapper.min is true")
else:
print("wrapper.min is false")
if random.random() > 0.5:
# recurse
return wrapped(*args)
import csv
with open("file2.txt") as b:
reader = csv.reader(b, delimiter="|")
ranges = {range(int(start), int(end)): [three, four] for start, end, three, four, *_ in reader}
with open("file1.txt") as a, \
open("file3.txt", 'wb') as outf:
writer = csv.writer(outf, delimiter="|")
for line in a:
def convert_temp():
'''Prints fahrenheit, celsius, and kelvin temps from user input'''
# helper functions to convert from fahrenheit
def celsius(temp_f):
return (5 / 9) * (temp_f - 32)
def kelvin(temp_f):
return (5 / 9) * (temp_f - 32) + 273.15
f = float(input("Enter a temperature (in Fahrenheit): "))
def Dish(object):
def __init__(self, name, price, calories):
self.name = name
self.price = price
self.calories = calories
@classmethod
def _get_info(cls):
name = input("Please enter the dish's name: ")
price = float(input("Please enter the dish's price: "))
$ python -c "import tmp; print(tmp.spaceitout('I love dogs', 2))"
I l o v e d o g s
$ python -c "import tmp; print(tmp.spaceitout2('I love dogs', 2))"
I love dogs
$ cat expectedresult.txt
I l o v e d o g s
BLANK = "_"
X = "X"
O = "O"
def print_board(board):
for line in board:
print(" ".join(board))
def is_game_over(board):
from sqlalchemy.orm import Query
from sqlalchemy.orm.exc import MultipleResultsFound
class MyQuery(Query):
def get_byusername(self, username):
ret = list(self.filter(username=username))
r_len = let(ret)
if r_len == 1:
return ret[0]