Giter Club home page Giter Club logo

ntypes's Introduction

Native Types

Emulate native integer and floating-point types in Python 2.x and 3.x.

Install the package via:

pip install nativetypes

Comparison

There are several alternatives to ntypes, specifically: ctypes, numpy, fixedint, cinc, cint. However, ntypes also offers some features not present across all these packages.

ntypes ctypes numpy fixedint cinc cint
Floating-point Yes Yes Yes - - -
Implicit casts Yes - - Yes - Yes
Custom aliases Yes - - - - -
Slicing Yes - - Yes - -
High-performance - Yes - - Yes Yes

Other reasons might include that numpy is way too large dependency to be imported just for the sake of fixed-size integers. Note that high-performance is not a goal for this library.

FAQ

What's the point of this library?

This library is syntactic sugar for developers and reverse-engineers that want to port code from C/C++/ASM into Python and need all this low-level quirks: Overflows, underflows, casts, etc.

Although Python prevents many headaches with its arbitrarily large integers, writing code equivalent to functions written in C/C++/ASM means masking every operation, and doing dozens of conversions manually between distinct types. This library does that work for you.

Where can read about the API?

The documentation is quite incomplete at this moment. Check the examples below or check the tests.

Why is this package called nativetypes?

I'm not good with naming. Ideally, I would have registered ntypes instead, but that one was apparently taken.

Examples

  • Fast inverse square root (see [1])
def rsqrt(number: float32):
    i = reinterpret_cast(int32, number)
    i = 0x5F3759DF - (i >> 1)
    y = reinterpret_cast(float32, i)
    y *= (1.5 - (0.5 * number * y * y))
    return y
  • Ranbyus DGA (see [2]):
def ranbyus_dga(timestamp):
    s = uint32(self.seed)
    t1 = uint32(t.day)
    t2 = uint32(t.month)
    t3 = uint32(t.year)
    
    name = ""
    for i in xrange(12):
        t1 = (t1 >> 15) ^ (16 * (t1 & 0x1FFF ^ 4 * (t1 ^ s)))
        t2 = ((t2 ^ (4 * t2)) >>  8) ^ ((t2 & 0xFFFFFFFE) * 14)
        t3 = ((t3 ^ (7 * t3)) >> 11) | ((t3 & 0xFFFFFFF0) << 17)
        s = (s >> 6) ^ (((t1 + 8 * s) << 8) & 0x3FFFF00)
        name += string.ascii_lowercase[int(t1 ^ t2 ^ t3) % 25]

    # TLD omitted
    return name

ntypes's People

Contributors

alexaltea avatar

Watchers

 avatar

Forkers

eamon-cai

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.