This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import singledispatch | |
from collections import Iterable | |
from json import JSONEncoder, dumps | |
json_converter = singledispatch(JSONEncoder().default) | |
json_converter.register(Iterable)(list) | |
class A: | |
def __init__(self, a, b): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os | |
# This is basically shutil.copyfileobj, but ignoring IO errors when reading. | |
# After doing a simple experiment with btrfs checksumming, I wondered if it was possible | |
# to still read a file that gave error with `cp`. The answer is obviously yes, since | |
# the checksum errors will work at the block-level (thus, why the chunksize is bsize) | |
# If you actually want to recover corrupted files from btrfs, without having to restore | |
# every single file in the fs, you might want to use instead: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE OverloadedStrings #-} | |
{-# LANGUAGE GADTs #-} | |
module Models where | |
import Database.Persist.Sqlite (runSqlite) | |
-- without the explicit type signature, it fails | |
-- with an ambiguous Control.Monad.IO.Class.MonadIO instance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE ScopedTypeVariables #-} | |
import System.Environment (getArgs, getEnv) | |
import Control.Exception (catch, SomeException) | |
import Data.Map.Strict (fromList, lookup, findWithDefault, insert, assocs) | |
import Prelude hiding (lookup) | |
partitionPairs' Nothing ys (x:xs) = partitionPairs' (Just x) ys xs | |
partitionPairs' Nothing ys [] = ys | |
partitionPairs' (Just x) ys (y:xs) = partitionPairs' Nothing ((x,y):ys) xs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Bits (clearBit, testBit) | |
import Data.Foldable (foldl') | |
data FindHoleError = MaxTooBig deriving (Show) | |
findholes :: Integer -> Integer -> [Int] -> Either FindHoleError [Int] | |
findholes min max xs | |
| max < (fromIntegral (maxBound :: Int)) = Right $ map (+ imin) (findholes' mask $ map (+ (-imin)) xs) | |
| otherwise = Left MaxTooBig | |
where |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mmap import mmap, PROT_READ | |
import re | |
import sys | |
def strings(fname, n=6): | |
with open(fname, 'rb') as f, mmap(f.fileno(), 0, prot=PROT_READ) as m: | |
for match in re.finditer(('([\w/]{%s}[\w/]*)' % n).encode(), m): | |
yield match.group(0) | |
if __name__ == '__main__': |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import ceil | |
from collections import namedtuple | |
def calc_bitmasks(bitlengths): | |
size = sum(bitlengths) | |
assert size % 8 == 0 | |
assert all(x < 65 for x in bitlengths) | |
remaining = size | |
for x in bitlengths: | |
remaining -= x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import System.Environment (getArgs) | |
import Data.Map (fromList, findWithDefault) | |
import qualified Data.Set as S | |
nub = S.toList . S.fromList | |
applyIf f = (\x flag -> if flag then f x else x) | |
flagCombinations n = sequence $ replicate n [True, False] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import struct | |
from collections import namedtuple | |
from mmap import mmap, PROT_READ | |
Header = namedtuple('Header', 'magic_number version_major version_minor thiszone sigfigs snaplen network') | |
RecordHeader = namedtuple('RecordHeader', 'timestamp timestamp_us size original_size') | |
def get_parser(record_type, struct_fmt, bigendian=False): | |
struct_fmt = ('>' if bigendian else '<') + struct_fmt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> raw_assessments = [('sslyze', ('ssl', 'infra')), ('nmap common', ('infra', ))] | |
>>> from collections import defaultdict | |
>>> types = defaultdict(set) | |
>>> for (name, _types) in raw_assessments: | |
... for t in _types: | |
... types[t].add(name) | |
... | |
>>> types | |
defaultdict(<class 'set'>, {'ssl': {'sslyze'}, 'infra': {'sslyze', 'nmap common'}}) |