Skip to main content

Module namedstruct

namedstruct encapsulates struct.unpack() and struct.pack() with results accessed by name.

Classes

class namedstruct.namedstruct

__init__( description, struct_definition )

The description is used to title a dump of unpacked data.

struct_definition is a single string that defines the type and name of each field as a space seperated list. Each field’s type and name are seperated by a “:”. If the name is left blank that field cannot be accessed by name, useful for padding fields. If a field’s type has a repeat count the value returned with be a tuple of all the fields parts.

len()

Returns the size of the struct.

unpack( bytes )

Unpack the bytes into a namestruct.namestructresults object used to access the decoded fields.

packer( from_unpacked=None )

Return a namedstructpacker object that can be used to pack the fields into a buffer. The optional from_unpacked will initialise the fields from a previously unpacked buffer.

class namedstruct.namedstructresults

.*name*

Returns the value of the field called name.

dump( writer )

Using the writer function to output a formatted dump of the whole struct.

class namedstruct.namedstructpacker

.*name* = value

Assign a value to the named field

init_from( from_unpacked )

Assign each field from a previous unpacked buffer’s namedstructresults.

pack()

Pack the fields and return the bytes buffer.

Example

import namedstruct

# Windows EXE files start with a DOS header
struct_windows_exe_header = namedstruct.namedstruct( 'DOS header', '<'
  '2s:dos_magic '
  'h:lastsize '
  'h:nblocks '
  'h:nreloc '
  'h:hdrsize '
  'h:minalloc '
  'h:maxalloc '
  'H:ss '
  'H:sp '
  'h:checksum '
  'H:ip '
  'H:cs '
  'h:relocpos '
  'h:noverlay '
  '4H:reserved1 '
  'h:oem_id '
  'h:oem_info '
  '10H:reserved2 '
  'L:pe_offset'
  )

dos_image = open( 'c:\\windows\\system32\\cmd.exe', 'rb' ).read()
header = struct_windows_exe_header.unpack( dos_image[0:len(struct_windows_exe_header)] )

if debug:
  header.dump( print )

print( 'dos_magic %r' % (header.dos_magic,) )
print( 'PE header offer: 0x%8.8x' % (header.pe_offset,) )

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

namedstruct-1.2.3.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

namedstruct-1.2.3-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file namedstruct-1.2.3.tar.gz.

File metadata

  • Download URL: namedstruct-1.2.3.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for namedstruct-1.2.3.tar.gz
Algorithm Hash digest
SHA256 f466378bea6f185e52c38054b91e585bfab7b0f619f8021de97788891e547ad1
MD5 114637771c6d054e8868e7d61c40d934
BLAKE2b-256 b646adde3edd7992f1726704d19824725359185538ddef256c1be059b6e163d0

See more details on using hashes here.

File details

Details for the file namedstruct-1.2.3-py3-none-any.whl.

File metadata

  • Download URL: namedstruct-1.2.3-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for namedstruct-1.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a0b89de3b1d5631758dbb7e11976ddbb83f5fe3e9e7562b2214e4fe357c14a15
MD5 60baad8f9781b68b80303d501ab66b2d
BLAKE2b-256 189efcedff38fd21fce022c88d7a2d6270f7f566755e1455de3de9d6072e3a57

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.2.3 This release

2 files

1.2.2

2 files

1.2.1

2 files

1.1.0

2 files

1.0.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page