Mailing List Archive

Python equivilent of pack()?
I'm writing a small program that needs to be able to decode a RADIUS
packet obtained from a snoop trace. I'd like to do it in Python 'cause its
just such a darn handy little programming language.

My question is this: is there a functional equivilent to the Perl pack()
and unpack() functions in Python? Or even better, is there an easier way
to do what I want with Python? If not, how many people think this would be
a useful addition to the Python standard library?

Thanks in advance.

ACT
Python equivilent of pack()? [ In reply to ]
Bit> My question is this: is there a functional equivilent to the Perl
Bit> pack() and unpack() functions in Python? Or even better, is there
Bit> an easier way to do what I want with Python? If not, how many
Bit> people think this would be a useful addition to the Python standard
Bit> library?

Bit,

Check out the struct module docs at:

http://www.python.org/doc/lib/module-struct.html

It'll most likely do just what you want. It even calls its methods "pack"
and "unpack", so it should be fairly familiar to your Perl neurons...

Skip Montanaro | http://www.mojam.com/
skip@mojam.com | http://www.musi-cal.com/~skip/
847-971-7098 | Python: Programming the way Guido indented...
Python equivilent of pack()? [ In reply to ]
Bit Nik <bitwise@io.com> wrote:

> My question is this: is there a functional equivilent to the Perl pack()
> and unpack() functions in Python?

See the standard struct module. It's very much like the Perl
equivalent, except that the documentation makes sense.
Python equivilent of pack()? [ In reply to ]
Bit Nik writes:
> My question is this: is there a functional equivilent to the Perl pack()
> and unpack() functions in Python? Or even better, is there an easier way
> to do what I want with Python? If not, how many people think this would be

Take a look at the struct module; the pack() and unpack() functions
are exactly what you're looking for!
(What names were you looking up in the index of the Library
Reference? I'll add them to the index if they make sense to others.)


-Fred

--
Fred L. Drake, Jr. <fdrake@acm.org>
Corporation for National Research Initiatives
Python equivilent of pack()? [ In reply to ]
>>>>> "Fred" == Fred L Drake, <fdrake@acm.org> writes:

Bit> My question is this: is there a functional equivilent to the
Bit> Perl pack() and unpack() functions in Python? Or even better,
Bit> is there an easier way to do what I want with Python? If not,
Bit> how many people think this would be

Fred> Take a look at the struct module; the pack() and unpack()
Fred> functions are exactly what you're looking for! (What names
Fred> were you looking up in the index of the Library Reference?
Fred> I'll add them to the index if they make sense to others.)

Fred, I think the problem is that the link to the index of all
functions in the library reference is hidden way down at the bottom of
the page, and as a very non-descript button hidden at the upper-right
hand corner of the page. In fact, I had no idea until just now that
such an index even existed for Python!

I think it must not be too uncommon for folks to miss out on the fact
that there's a function index to the library reference. Perhaps there
should be a big fat bold link to it at both the top and bottom of the
page.

It would also be a good idea to use better icons than 'toc' 'm' and
'i' at the top of the page; I know I completely overlooked them until
just now because they weren't very noticeable.

Ben

--
Brought to you by the letters R and Z and the number 4.
"If you turn both processors off, you will have to reboot."
Debian GNU/Linux maintainer of Gimp and GTK+ -- http://www.debian.org/
Python equivilent of pack()? [ In reply to ]
Ben> I think it must not be too uncommon for folks to miss out on the
Ben> fact that there's a function index to the library
Ben> reference. Perhaps there should be a big fat bold link to it at
Ben> both the top and bottom of the page.

Ben> It would also be a good idea to use better icons than 'toc' 'm' and
Ben> 'i' at the top of the page; I know I completely overlooked them
Ben> until just now because they weren't very noticeable.

In fact, the Module Index is much more useful than the contents of the lib
ref manual once you're fairly well settled in. I used to generate an index
myself before Fred started doing it (it was a pain to keep up with the TOC
format changes!). Now I simply link to

http://www.python.org/doc/lib/modindex.html

from my private home page and skip the lib manual's front page entirely.
Most everything I need is no more than two or three clicks away.

Skip Montanaro | http://www.mojam.com/
skip@mojam.com | http://www.musi-cal.com/~skip/
847-971-7098 | Python: Programming the way Guido indented...
Python equivilent of pack()? [ In reply to ]
>>>>> "Skip" == Skip Montanaro <skip@mojam.com> writes:

Skip> In fact, the Module Index is much more useful than the
Skip> contents of the lib ref manual once you're fairly well
Skip> settled in. I used to generate an index myself before Fred
Skip> started doing it (it was a pain to keep up with the TOC
Skip> format changes!). Now I simply link to

Skip> http://www.python.org/doc/lib/modindex.html

Skip> from my private home page and skip the lib manual's front
Skip> page entirely. Most everything I need is no more than two
Skip> or three clicks away.

You're absolutely right, Skip. The current Python lib ref manual front
page is not nearly as useful as that module index page.

Perhaps it's time for a reorganization? The main lib ref page has
gotten rather bloated, and it's pretty hard to find the necessary
information. Maybe we could go for a Yahoo-style categorization
rather than the huge delimited list we have now.

--
Brought to you by the letters C and T and the number 4.
"Moshimoshi. Kikoemasu ka?" "Kakenaoshimasu kara ne! 1-do kitte kudasai."
Debian GNU/Linux maintainer of Gimp and GTK+ -- http://www.debian.org/