Skip to main content

This app allows you to convert objects into fixed width records.

In version 0.1.0 we introduce an entirely new (albeit similar) interface that breaks all dependency on Django and removes the necessity of manually setting the order of fields. We call it ‘fixedwidth’.

Usage:

from djcopybook.fixedwidth import Record from djcopybook.fixedwidth import fields

class Person(Record):

first_name = fields.StringField(length=20) last_name = fields.StringField(length=30) siblings = fields.IntegerField(length=2) birth_date = fields.DateField(length=10, format=”%Y-%m-%d”)

>>> p = Person(first_name="Joe", last_name="Smith", siblings=3, birth_date="1982-09-11")
>>> p.birth_date
datetime.date(1982, 9, 11)
>>> p.to_record()
'Joe                 Smith                         031982-09-11'

You can also set attributes after a record has been instantiated, give fields default values, and other fun stuff.

When you have a record instance, the data values will always be their python value, and when you do a to_record on the Record as a whole or an individual field it will have the fixedwidth format.

New in version 0.1.1:

ListField: lets you have one field whose values are made of another complete record. Similar to COBOL’s OCCURS functionality. Declaring length on the ListField tells how many times that record repeats.

USAGE:
class PhoneNumber(Record):

identifier = fields.StringField(length=10, default=”Mobile”) area_code = fields.IntegerField(length=3) prefix = fields.IntegerField(length=3) line_number = fields.IntegerField(length=4)

class Person(Record):

first_name = fields.StringField(length=20) last_name = fields.StringField(length=30) siblings = fields.IntegerField(length=2) birth_date = fields.DateField(length=10, format=”%Y-%m-%d”) phone_numbers = fields.ListField(record=PhoneNumber, length=3)

>>> phone_one = PhoneNumber(area_code=515, prefix=555, line_number=2222)
>>> person = Person(first_name="Joe", last_name="Smith", siblings=3,
               birth_date="1982-09-11", phone_numbers=[phone_one])
>>> person.to_record()
'Joe                 Smith                         031982-09-11Mobile    5155552222Mobile    0000000000Mobile    0000000000'
New in version 0.1.2:

Convert records from a fixed width format back into record objects

USAGE:
class Person(Record):

first_name = fields.StringField(length=20) last_name = fields.StringField(length=30) siblings = fields.IntegerField(length=2) birth_date = fields.DateField(length=10, format=”%Y-%m-%d”)

>>> fixedwidth_record = 'Joe                 Smith                         031982-09-11'
>>> person = Person.from_record(fixedwidth_record)
>>> person.first_name
'Joe'
>>> person.last_name
'Smith'
>>> person.siblings
3
>>> person.birth_date
datetime.date(1982, 9, 11)
New in version 0.1.6:

FragmentField: Similar to the ListField, but only occurring one time. Useful if you have a common element included in multiple Records.

USAGE:
class Phone(Record):

area_code = fields.IntegerField(length=3) prefix = fields.IntegerField(length=3) line_number = fields.IntegerField(length=4)

class Contact(Record):

name = fields.StringField(length=30) phone_number = fields.FragmentField(record=Phone) email = fields.StringField(length=30)

>>> phone = PhoneNumber(area_code=515, prefix=555, line_number=2222)
>>> contact = Contact(name="Joe", phone_number=phone, email="joe@example.com")
>>> contact.to_record()
'Joe                 5155552222joe@example.com               '

or:

>>> contact = Contact(name="Joe", email="joe@example.com")
>>> contact.phone_number.area_code = 515
>>> contact.phone_number.prefix = 555
>>> contact.phone_number.line_number = 2222
>>> contact.to_record()
'Joe                 5155552222joe@example.com               '

New in version 0.1.7: You can now populate a record object with a dictionary containing more items than the record has fields. The record will use the values if it has the associated fields, but will ignore extra data.

This is useful when populating records from a form’s cleaned_data for instance if the form has more data than that specific record wants.

New in version 0.1.9:

Record now has an auto_truncate attribute you can set to truncate each field down to the expected size instead of raising an error.

USAGE:

class Sample(Record):

field = fields.StringField(length=5)

>>> s = Sample(field='this is too long')
>>> str(s)
'this '
Notes:

Because we are using OrderedDict, the new fixedwidth implementation will only work on Python 2.7 and above. (you can copy the OrderdDict class yourself if you need < 2.7)

The previous Django model implementation is pending deprecation.

Release files for django-copybook 1.3.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 django-copybook 1.3.2
File Size Uploaded
django_copybook-1.3.2.tar.gz 15.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-copybook 1.3.2
File Interpreter ABI Platform
django_copybook-1.3.2-py3-none-any.whl Python 3 none any Details

Total release size: 37.5 kB

Release files / django_copybook-1.3.2.tar.gz

Download URL django_copybook-1.3.2.tar.gz
Size 15.1 kB
Tags Source
SHA-256 checksum
How to use checksums
63797999bf5fe0456ed40bb204b90e8a026f94696b2ebcdb982dfd0c95e9b72f
BLAKE2b-256 checksum
How to use checksums
34c2356c67c0d8f62ad724ee1c25a70d8dd8c13b15398ff1fdf979b79111b41f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.7

Release files / django_copybook-1.3.2-py3-none-any.whl

Download URL django_copybook-1.3.2-py3-none-any.whl
Size 22.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8367486ab25b122286c259d6d2822584980efbff8e410a8710b1d1f171aa5343
BLAKE2b-256 checksum
How to use checksums
6245b0b6308e6a60cb90413f2664414dca54e0b8d1032eebfa96d0bd1f0c2b28
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.7

Release history Release notifications | RSS feed

This release

1.3.2 This release

2 release files

1.3.1

2 release files

1.2.1

1 release file

1.2.0

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

0.2.4

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.1.9

1 release file

0.1.8

1 release file

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

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