我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用types.LongType()。
def int2bytes(number): """Converts a number to a string of bytes >>> bytes2int(int2bytes(123456789)) 123456789 """ if not (type(number) is types.LongType or type(number) is types.IntType): raise TypeError("You must pass a long or an int") string = "" while number > 0: string = "%s%s" % (byte(number & 0xFF), string) number /= 256 return string
def encrypt_int(message, ekey, n): """Encrypts a message using encryption key 'ekey', working modulo n""" if type(message) is types.IntType: message = long(message) if not type(message) is types.LongType: raise TypeError("You must pass a long or int") if message < 0 or message > n: raise OverflowError("The message is too long") #Note: Bit exponents start at zero (bit counts start at 1) this is correct safebit = bit_size(n) - 2 #compute safe bit (MSB - 1) message += (1 << safebit) #add safebit to ensure folding return pow(message, ekey, n)
def new_looper(a, arg=None): """Helper function for nest() determines what sort of looper to make given a's type""" if isinstance(a,types.TupleType): if len(a) == 2: return RangeLooper(a[0],a[1]) elif len(a) == 3: return RangeLooper(a[0],a[1],a[2]) elif isinstance(a, types.BooleanType): return BooleanLooper(a) elif isinstance(a,types.IntType) or isinstance(a, types.LongType): return RangeLooper(a) elif isinstance(a, types.StringType) or isinstance(a, types.ListType): return ListLooper(a) elif isinstance(a, Looper): return a elif isinstance(a, types.LambdaType): return CalcField(a, arg)
def getValueStrings( val, blnUgly=True ): #Used by joinWithComma function to join list items for SQL queries. #Expects to receive 'valid' types, as this was designed specifically for joining object attributes and nonvalid attributes were pulled. #If the default blnUgly is set to false, then the nonvalid types are ignored and the output will be pretty, but the SQL Insert statement will #probably be wrong. tplStrings = (types.StringType, types.StringTypes ) tplNums = ( types.FloatType, types.IntType, types.LongType, types.BooleanType ) if isinstance( val, tplNums ): return '#num#'+ str( val ) + '#num#' elif isinstance( val, tplStrings ): strDblQuote = '"' return strDblQuote + val + strDblQuote else: if blnUgly == True: return "Error: nonconvertable value passed - value type: %s" % type(val ) else: return None
def to64(number): """Converts a number in the range of 0 to 63 into base 64 digit character in the range of '0'-'9', 'A'-'Z', 'a'-'z','-','_'. """ if not (type(number) is types.LongType or type(number) is types.IntType): raise TypeError("You must pass a long or an int") if 0 <= number <= 9: #00-09 translates to '0' - '9' return byte(number + 48) if 10 <= number <= 35: return byte(number + 55) #10-35 translates to 'A' - 'Z' if 36 <= number <= 61: return byte(number + 61) #36-61 translates to 'a' - 'z' if number == 62: # 62 translates to '-' (minus) return byte(45) if number == 63: # 63 translates to '_' (underscore) return byte(95) raise ValueError('Invalid Base64 value: %i' % number)
def from64(number): """Converts an ordinal character value in the range of 0-9,A-Z,a-z,-,_ to a number in the range of 0-63. """ if not (type(number) is types.LongType or type(number) is types.IntType): raise TypeError("You must pass a long or an int") if 48 <= number <= 57: #ord('0') - ord('9') translates to 0-9 return(number - 48) if 65 <= number <= 90: #ord('A') - ord('Z') translates to 10-35 return(number - 55) if 97 <= number <= 122: #ord('a') - ord('z') translates to 36-61 return(number - 61) if number == 45: #ord('-') translates to 62 return(62) if number == 95: #ord('_') translates to 63 return(63) raise ValueError('Invalid Base64 value: %i' % number)
def isOlderThan(self, arg): if not self.timestamp: return True if isinstance(arg, types.IntType) or isinstance(arg, types.LongType) or isinstance(arg, types.FloatType): return self.timestamp < arg if isinstance(arg, TimeStampFile): if arg.timestamp is None: return False else: return arg.timestamp > self.timestamp elif isinstance(arg, types.ListType): files = arg else: files = [arg] for f in files: if os.path.getmtime(f) > self.timestamp: return True return False
def isNewerThan(self, arg): if not self.timestamp: return False if isinstance(arg, types.IntType) or isinstance(arg, types.LongType) or isinstance(arg, types.FloatType): return self.timestamp > arg if isinstance(arg, TimeStampFile): if arg.timestamp is None: return False else: return arg.timestamp < self.timestamp elif isinstance(arg, types.ListType): files = arg else: files = [arg] for f in files: if os.path.getmtime(f) < self.timestamp: return True return False
def __pow__(self, other): """Raise each element of sparse vector to a power. If power is another sparse vector, compute elementwise power. In this latter case, by convention, 0^0 = 0. """ if not isSparseVector(self): raise TypeError("Argument must be a SparseVector") if isSparseVector(other): rv = SparseVector(max(self.n, other.n), {}) for k in self.values.keys(): rv[k] = self[k]**other[k] return rv if not isinstance(other, types.IntType) and \ not isinstance(other, types.LongType) and \ not isinstance(other, types.FloatType): raise TypeError("Power must be numeric or a sparse vector") rv = SparseVector(self.n, {}) for k in self.values.keys(): rv[k] = math.pow(self[k], other) return rv
def SetRegValue(value, name): """ Set register value @param name: the register name @param value: new register value @note: The debugger should be running It is not necessary to use this function to set register values. A register name in the left side of an assignment will do too. """ rv = idaapi.regval_t() if type(value) == types.StringType: value = int(value, 16) elif type(value) != types.IntType and type(value) != types.LongType: print "SetRegValue: value must be integer!" return BADADDR if value < 0: #ival_set cannot handle negative numbers value &= 0xFFFFFFFF rv.ival = value return idaapi.set_reg_val(name, rv)
def inttopretty(seconds): """Convert num of seconds to readable string like "2 hours".""" partlist = [] hours, seconds = divmod(seconds, 3600) if hours > 1: partlist.append("%d hours" % hours) elif hours == 1: partlist.append("1 hour") minutes, seconds = divmod(seconds, 60) if minutes > 1: partlist.append("%d minutes" % minutes) elif minutes == 1: partlist.append("1 minute") if seconds == 1: partlist.append("1 second") elif not partlist or seconds > 1: if isinstance(seconds, (types.LongType, types.IntType)): partlist.append("%s seconds" % seconds) else: partlist.append("%.2f seconds" % seconds) return " ".join(partlist)
def find_sqltype(val): """ Find sqlite data type which matches the type of `val`. Parameters ---------- val : any python type Returns ------- sqltype : str String with sql type which can be used to set up a sqlile table """ mapping = {\ types.NoneType: 'NULL', types.IntType: 'INTEGER', types.LongType: 'INTEGER', types.FloatType: 'REAL', # 'FLOAT' also works types.StringTypes: 'TEXT', # StringType + UnicodeType types.BufferType: 'BLOB'} for typ in mapping.keys(): if isinstance(val, typ): return mapping[typ] raise StandardError("type '%s' unknown, cannot find mapping " "to sqlite3 type" %str(type(val)))
def __getitem__(self, key): """Called to implement evaluation of self[key]. >>> ip=IP('127.0.0.0/30') >>> for x in ip: ... print hex(x.int()) ... 0x7F000000L 0x7F000001L 0x7F000002L 0x7F000003L >>> hex(ip[2].int()) '0x7F000002L' >>> hex(ip[-1].int()) '0x7F000003L' """ if not isinstance(key, types.IntType) and not isinstance(key, types.LongType): raise TypeError if abs(key) >= self.len(): raise IndexError if key < 0: key = self.len() - abs(key) return self.ip + long(key)