python 一些乱七八糟的东西

import random
import os
import sys
import re

class _is:
    def __init__(self,reg):
        self.cr=re.compile(reg)
    def __call__(self,string):
        return self.cr.fullmatch(string)!=None

def ecall(x):
    if hasattr(x,"__call__"):
        return x()
    else:
        return x

def ifcc(cond,c1,c2):
    if cond:
        return ecall(c1)
    else:
        return ecall(c2)

def iff(c,c2):
    if c==None:
        return c2
    else:
        return c

## System random state manipulator
# A redefined reseeding function seeding with a large amount of entropy
def reseed(a=None,bytec=32):
    if a==None:
        a=os.urandom(bytec)
    random.seed(a)
# aliases
gstate=lambda:random.getstate()
sstate=lambda x:random.setstate(x)
gbits =lambda x:random.getrandbits(x)
choose=lambda x:random.choice(x)

## Mathematical
import math
import numbert as nt

##classes
class udist:
    def __init__(self,l,u):
        self.l=l
        self.u=u
    def __call__(self):
        r=random.randint(self.l,self.u)
        return r

class rdist:
    def __init__(self,l,u):
        self.l=l
        self.u=u
        self.avail=set()
    def __call__(self,l=None,u=None,avail=None):
        l=iff(l,self.l)
        u=iff(u,self.u)
        avail=iff(avail,self.avail)
        # print(l,u,avail)
        r=random.randint(l,u)
        while r in avail:
            r=random.randint(l,u)
        avail.add(r)
        return r

## utilities
serialize=lambda x,sep=' ',processor=str:sep.join(map(lambda x:processor(ecall(x)),x))
sfloat=lambda prec=6:lambda x,y="%%.%df"%prec:y%x
loop=lambda x,y:[y(i) for i in range(x)]
uloop=lambda x,y:[ecall(y) for i in range(x)]
isint=_is('[0-9]+')
def choo(rndfn,cond):
    q=rndfn()
    while not cond(q):
        q=rndfn()
    return q

args=list(map(lambda x:ifcc(isint(x),lambda:int(x),x),sys.argv[1:]))
compose=lambda x,y:lambda:x(ecall(y))
cr=lambda x,y:y()
cct=lambda x,y:lambda:cr(x(),y)
# Repl test
def Repl(gl,lc):
    s=input()
    while s!='end':
        ret=eval(s,gl,lc)
        print(">",ret)
        s=input()


posted @ 2018-07-03 15:32  zball  阅读(244)  评论(0编辑  收藏  举报