By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,317 Members | 1,314 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

Creating Fixed Length Records

P: n/a
Hello-

I'm creating fixed-length record layouts for various record translations
I need to perform. I have been using a home-grown object,
"FixedLengthRecord" for about 4 years now and am very happy with it.
Almost. The burr under my saddle occurs when I serialize the record.
Currently, I create an empty text field and for each record in my
database (all record layouts, data types, lengths, defaults, etc. are
held in an SQL server) I concatenate the field to the text segment. I
have been led to believe this is bad form because Python will copy the
entire segment each time I add a field. Up until now, it was not a big
deal because the segments had at most 20 fields. I have just been
handed a record layout that is almost 5000 bytes long with 350 fields in
it. Gulp!! Although I could let my object churn away on this bad boy,
I'd like to know if there is a more pythonic way to serialize the record.

One thought I had, which might lead to an addition to the language, was
to use the struct module. If I could feed the pack method a format
string then a tuple of values (instead of individual values), then I
could create the format string once, then pass it a tuple with the
values for that record. Just a thought.

So, gurus, what are your suggestions to tame this record? Are there
easier ways that I'm just not seeing?

Thanks,
--greg
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Greg Lindstrom wrote:
[...] I concatenate the field to the text segment. I have been led to
believe this is bad form because Python will copy the entire segment
each time I add a field. Up until now, it was not a big deal because
the segments had at most 20 fields. I have just been handed a record
layout that is almost 5000 bytes long with 350 fields in it. Gulp!!
Although I could let my object churn away on this bad boy, I'd like to
know if there is a more pythonic way to serialize the record.

The standard Python way would be to read your fields into a list, and
then join() the list. Psuedocode-ish example:

values = []
for field in list_of_fields:
this_value = database.get(field) # or however you want to
retrieve it
values.append(this_value)
text = ''.join(values)

Of course, if you want to be able to reverse the process, then you will
need some kind of format definition. This could be an explicit list of
column numbers where fields break, or an implicit list in the form of a
struct module format string, or whatever. Using the struct module would
(I believe) be a reasonable way of getting data into/out of the
fixed-length block.

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #2

P: n/a
On Wed, 08 Dec 2004 17:29:19 -0600, Greg Lindstrom wrote:

One thought I had, which might lead to an addition to the language, was
to use the struct module. If I could feed the pack method a format
string then a tuple of values (instead of individual values), then I
could create the format string once, then pass it a tuple with the
values for that record. Just a thought.


The language extension has been around since the early days. It was
originally called 'apply'. But now is called '*'.
import struct
fmt = '8s6s2s'
v = ('foo','bar','AA')
struct.pack(fmt,*v)

'foo\x00\x00\x00\x00\x00bar\x00\x00\x00AA'

--
Stuart D. Gathman <st****@bmsi.com>
Business Management Systems Inc. Phone: 703 591-0911 Fax: 703 591-6154
"Confutatis maledictis, flamis acribus addictis" - background song for
a Microsoft sponsored "Where do you want to go from here?" commercial.

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.