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,) )

Release files for namedstruct 1.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for namedstruct 1.2.2
File Size Uploaded
namedstruct-1.2.2.tar.gz 8.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for namedstruct 1.2.2
File Interpreter ABI Platform
namedstruct-1.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 17.0 kB

Release files / namedstruct-1.2.2.tar.gz

Download URL namedstruct-1.2.2.tar.gz
Size 8.6 kB
Tags Source
SHA-256 checksum
How to use checksums
4b5c29bf1f775b45c18f87b7dc022fd69d93af914d60de17006bbada12bcf617
BLAKE2b-256 checksum
How to use checksums
6c560e84f1050c72bf4d8e8f1abefaf012b391a974b655723672799f312b538d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

Release files / namedstruct-1.2.2-py3-none-any.whl

Download URL namedstruct-1.2.2-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
67727624e4a290ecb5202660bd67423d1538d20fc64b70f80935f81ef522701f
BLAKE2b-256 checksum
How to use checksums
3aab8ca10f811c49c07b4bd539620a88964b272730e9a48a0d71c972e60da45b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

Release history Release notifications | RSS feed

1.2.3

2 release files

This release

1.2.2 This release

2 release files

1.2.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page