Skip to content

Instantly share code, notes, and snippets.

@andycasey
Created April 14, 2023 07:00
Show Gist options
  • Save andycasey/ff9ebf251bb80ee9ebfe23161ee48a0b to your computer and use it in GitHub Desktop.
Save andycasey/ff9ebf251bb80ee9ebfe23161ee48a0b to your computer and use it in GitHub Desktop.

Revisions

  1. andycasey revised this gist Apr 14, 2023. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions target_bitfield_demo.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    cartons = [
    SimplifiedCarton.create(id=5, carton="star"),
    SimplifiedCarton.create(id=8, carton="galaxy")
    @@ -32,4 +31,3 @@

    print(int.from_bytes(source.carton_flags._buffer, byteorder='little'))
    # > 6546781215792283740026379393655198304433284092086129578966582736192267592809349109766540184651808314301773368255120142018434513091770786106657055179040

  2. andycasey revised this gist Apr 14, 2023. 1 changed file with 23 additions and 23 deletions.
    46 changes: 23 additions & 23 deletions target_bitfield_demo.py
    Original file line number Diff line number Diff line change
    @@ -1,35 +1,35 @@

    cartons = [
    SimplifiedCarton.create(id=5, carton="star"),
    SimplifiedCarton.create(id=8, carton="galaxy")
    ]
    cartons = [
    SimplifiedCarton.create(id=5, carton="star"),
    SimplifiedCarton.create(id=8, carton="galaxy")
    ]

    source = Source.create(ra=0, dec=0)
    source.carton_flags.set_bit(5) # add to star
    source.carton_flags.set_bit(8) # add to galaxy
    source = Source.create(ra=0, dec=0)
    source.carton_flags.set_bit(5) # add to star
    source.carton_flags.set_bit(8) # add to galaxy

    print(source.carton_flags)
    # > bytearray(b' \x01')
    print(source.carton_flags)
    # > bytearray(b' \x01')

    print(int.from_bytes(source.carton_flags._buffer, byteorder='little'))
    # > 288
    print(int.from_bytes(source.carton_flags._buffer, byteorder='little'))
    # > 288

    assert (2**5 + 2**8) == 288
    assert (2**5 + 2**8) == 288

    # Each byte can store up to 8 bits, so this one should be length 2 because the galaxy just pushes us over
    assert len(source.carton_flags._buffer) == 2
    # Each byte can store up to 8 bits, so this one should be length 2 because the galaxy just pushes us over
    assert len(source.carton_flags._buffer) == 2

    # Let's add a big carton
    ufo = SimplifiedCarton.create(id=501, carton="UFO")
    source.carton_flags.set_bit(ufo.id)
    # Let's add a big carton
    ufo = SimplifiedCarton.create(id=501, carton="UFO")
    source.carton_flags.set_bit(ufo.id)

    print(source.carton_flags._buffer)
    # > 63
    print(source.carton_flags._buffer)
    # > 63


    print(source.carton_flags._buffer)
    # > bytearray(b' \x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 ')
    print(source.carton_flags._buffer)
    # > bytearray(b' \x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 ')

    print(int.from_bytes(source.carton_flags._buffer, byteorder='little'))
    # > 6546781215792283740026379393655198304433284092086129578966582736192267592809349109766540184651808314301773368255120142018434513091770786106657055179040
    print(int.from_bytes(source.carton_flags._buffer, byteorder='little'))
    # > 6546781215792283740026379393655198304433284092086129578966582736192267592809349109766540184651808314301773368255120142018434513091770786106657055179040

  3. andycasey created this gist Apr 14, 2023.
    35 changes: 35 additions & 0 deletions target_bitfield_demo.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@

    cartons = [
    SimplifiedCarton.create(id=5, carton="star"),
    SimplifiedCarton.create(id=8, carton="galaxy")
    ]

    source = Source.create(ra=0, dec=0)
    source.carton_flags.set_bit(5) # add to star
    source.carton_flags.set_bit(8) # add to galaxy

    print(source.carton_flags)
    # > bytearray(b' \x01')

    print(int.from_bytes(source.carton_flags._buffer, byteorder='little'))
    # > 288

    assert (2**5 + 2**8) == 288

    # Each byte can store up to 8 bits, so this one should be length 2 because the galaxy just pushes us over
    assert len(source.carton_flags._buffer) == 2

    # Let's add a big carton
    ufo = SimplifiedCarton.create(id=501, carton="UFO")
    source.carton_flags.set_bit(ufo.id)

    print(source.carton_flags._buffer)
    # > 63


    print(source.carton_flags._buffer)
    # > bytearray(b' \x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 ')

    print(int.from_bytes(source.carton_flags._buffer, byteorder='little'))
    # > 6546781215792283740026379393655198304433284092086129578966582736192267592809349109766540184651808314301773368255120142018434513091770786106657055179040