1
0
Fork 0
mirror of https://github.com/notwa/mm synced 2024-11-10 11:39:04 -08:00
mm/util.py
2015-03-02 07:02:11 -08:00

33 lines
815 B
Python

import os
import struct, array
R1 = lambda data: struct.unpack('>B', data)[0]
R2 = lambda data: struct.unpack('>H', data)[0]
R4 = lambda data: struct.unpack('>I', data)[0]
W1 = lambda data: struct.pack('>B', data)
W2 = lambda data: struct.pack('>H', data)
W4 = lambda data: struct.pack('>I', data)
def dump_as(b, fn):
with open(fn, 'w+b') as f:
f.write(b)
def swap_order(f, size='H'):
f.seek(0)
a = array.array(size, f.read())
a.byteswap()
f.seek(0)
f.write(a.tobytes())
class SubDir:
def __init__(self, d):
self.d = d
def __enter__(self):
self.cwd = os.getcwd()
try:
os.mkdir(self.d)
except FileExistsError:
pass
os.chdir(self.d)
def __exit__(self, type_, value, traceback):
os.chdir(self.cwd)