In addition to providing a layer of syntax sugar for the DAZzle XML interface, |
In addition to providing a layer of syntax sugar for the DAZzle XML interface, |
PyDicia provides a novel adaptive interface that lets you smoothly integrate |
PyDicia provides a novel adaptive interface that lets you smoothly integrate |
its functions with your application's core types (like invoice, customer, or |
its functions with your application's core types (like invoice, customer, or |
packing ticket objects) without subclassing. (This is particularly useful if |
"packing slip" objects) without subclassing. (This is particularly useful if |
you are extending a CRM or other database that was written by somebody else.) |
you are extending a CRM or other database that was written by somebody else.) |
|
|
PyDicia uses the ElementTree, simplegeneric, and DecoratorTools packages, and |
PyDicia uses the ElementTree, simplegeneric, and DecoratorTools packages, and |
requires Python 2.4 or higher. |
requires Python 2.4 or higher (due to use of decorators and the ``Decimal`` |
|
type). |
|
|
TODO: |
|
|
|
* ``~`` operator for DocInfo, make DocInfo public |
TODO: |
|
|
* global defaults |
* global defaults |
|
|
Basic Use |
Basic Use |
========= |
========= |
|
|
|
>>> from pydicia import * |
|
>>> s = Shipment() |
|
>>> s.batches |
|
[] |
|
|
|
>>> s.ship(ToName('Phillip Eby'), Test) |
|
>>> len(s.batches) |
|
1 |
|
>>> s.batches[0].packages |
|
[(DocInfo('ToName', 'Phillip Eby', None), DocInfo('DAZzle', 'YES', 'Test'))] |
|
|
|
>>> print s.batches[0].tostring() |
|
<DAZzle Test="YES"><Package ID="1"><ToName>Phillip |
|
Eby</ToName></Package></DAZzle> |
|
|
|
>>> s.ship(ToName('Ty Sarna')) |
|
>>> len(s.batches) |
|
1 |
|
>>> print s.batches[0].tostring() |
|
<DAZzle Test="YES"><Package ID="1"><ToName>Phillip |
|
Eby</ToName></Package><Package ID="2"><ToName>Ty |
|
Sarna</ToName></Package></DAZzle> |
|
|
|
>>> s.ship(ToName('PJE'), ~Test) |
|
>>> len(s.batches) |
|
2 |
|
>>> print s.batches[1].tostring() |
|
<DAZzle Test="NO"><Package ID="1"><ToName>PJE</ToName></Package></DAZzle> |
|
|
|
|
|
|
Application Integration |
Application Integration |
|
|
MailClass(text), NoPostage |
MailClass(text), NoPostage |
|
|
DateAdvance |
DateAdvance, Today, Tomorrow |
WeightOz |
WeightOz(), Width(), Length(), Depth() |
Value |
Value |
Description |
Description |
|
|
Addresses |
Addresses |
========= |
========= |
|
|
ToName(text), ToTitle(text), ToCompany(text) |
>>> ToName("Phillip J. Eby") |
|
DocInfo('ToName', 'Phillip J. Eby', None) |
|
|
|
>>> ToTitle("President") |
|
DocInfo('ToTitle', 'President', None) |
|
|
|
>>> ToCompany("Dirt Simple, Inc.") |
|
DocInfo('ToCompany', 'Dirt Simple, Inc.', None) |
|
|
|
|
ToAddress(*lines) |
ToAddress(*lines) |
ToCity(text), ToState(text), ToPostalCode(text), ToZIP4(text), ToCountry(text) |
ToCity(text), ToState(text), ToPostalCode(text), ToZIP4(text), ToCountry(text) |
|
|
Service Options |
Service Options |
=============== |
=============== |
|
|
DomesticFlatRateEnvelope |
FlatRateEnvelope |
DomesticFlatRateBox |
FlatRateBox |
|
RectangularParcel |
|
NonRectangularParcel |
|
Postcard |
|
Flat |
|
Envelope |
|
|
|
NonMachinable |
|
BalloonRate |
|
|
ReplyPostage |
ReplyPostage |
Stealth |
Stealth |
Oversize |
|
SignatureWaiver |
SignatureWaiver |
NoWeekendDelivery |
NoWeekendDelivery |
NoHolidayDelivery |
NoHolidayDelivery |
ReturnToSender |
ReturnToSender |
|
|
RegisteredMail |
RegisteredMail |
|
|
Insurance.USPS |
Insurance.USPS |
Insurance.Endicia |
Insurance.Endicia |
Insurance.UPIC |
Insurance.UPIC |
|
Insurance.NONE |
|
|
|
Domestic. |
|
|
CertifiedMail |
CertifiedMail |
RestrictedDelivery |
RestrictedDelivery |
CertificateOfMailing |
CertificateOfMailing |
|
|
Item(desc, weight, value, qty=1, origin='United States') |
Item(desc, weight, value, qty=1, origin='United States') |
|
|
|
CustomsSigner(text) |
|
CustomsCertify |
|
|
|
|
Processing Options |
Processing Options |
Internals and Tests |
Internals and Tests |
------------------- |
------------------- |
|
|
DocInfo applications:: |
>>> from pydicia import add_to_package, ET, DocInfo, Batch, Package |
|
|
>>> from pydicia import docinfo_to_etree, ET, _DocInfo, make_tree |
Packages:: |
|
|
>>> root = ET.Element('DAZzle') |
>>> b = Batch() |
>>> pkg = ET.SubElement(root, 'Package', ID='1') |
>>> p = Package(b) |
|
|
>>> print ET.tostring(root) |
>>> print b.tostring() |
<DAZzle><Package ID="1" /></DAZzle> |
<DAZzle><Package ID="1" /></DAZzle> |
|
|
>>> Box = _DocInfo('FlatRate', 'BOX') |
>>> Box = DocInfo('FlatRate', 'BOX') |
>>> docinfo_to_etree(Box, root, False) |
>>> add_to_package(Box, p, False) |
|
|
>>> print ET.tostring(root) |
>>> print b.tostring() |
<DAZzle><Package ID="1"><FlatRate>BOX</FlatRate></Package></DAZzle> |
<DAZzle><Package ID="1"><FlatRate>BOX</FlatRate></Package></DAZzle> |
|
|
>>> Envelope = _DocInfo('FlatRate', 'TRUE') |
>>> Envelope = DocInfo('FlatRate', 'TRUE') |
>>> docinfo_to_etree(Envelope, root, False) |
>>> add_to_package(Envelope, p, False) |
Traceback (most recent call last): |
Traceback (most recent call last): |
... |
... |
DocinfoConflict: Can't set 'FlatRate=TRUE' when 'FlatRate=BOX' already set |
DocInfoConflict: Can't set 'FlatRate=TRUE' when 'FlatRate=BOX' already set |
|
|
>>> print ET.tostring(root) |
>>> print b.tostring() |
<DAZzle><Package ID="1"><FlatRate>BOX</FlatRate></Package></DAZzle> |
<DAZzle><Package ID="1"><FlatRate>BOX</FlatRate></Package></DAZzle> |
|
|
>>> docinfo_to_etree(Box, root, False) |
>>> add_to_package(Box, p, False) |
>>> print ET.tostring(root) |
>>> print b.tostring() |
<DAZzle><Package ID="1"><FlatRate>BOX</FlatRate></Package></DAZzle> |
<DAZzle><Package ID="1"><FlatRate>BOX</FlatRate></Package></DAZzle> |
|
|
>>> docinfo_to_etree(Envelope, root, True) |
>>> add_to_package(Envelope, p, True) |
>>> print ET.tostring(root) |
>>> print b.tostring() |
<DAZzle><Package ID="1"><FlatRate>BOX</FlatRate></Package></DAZzle> |
<DAZzle><Package ID="1"><FlatRate>BOX</FlatRate></Package></DAZzle> |
|
|
>>> del pkg[-1] |
>>> del p.element[-1] |
>>> print ET.tostring(root) |
>>> print b.tostring() |
<DAZzle><Package ID="1" /></DAZzle> |
<DAZzle><Package ID="1" /></DAZzle> |
|
|
>>> verify_zip = _DocInfo('DAZzle', 'DAZ', 'Start') |
>>> verify_zip = DocInfo('DAZzle', 'DAZ', 'Start') |
|
|
>>> docinfo_to_etree(verify_zip, root, False) |
>>> add_to_package(verify_zip, p, False) |
>>> print ET.tostring(root) |
>>> print b.tostring() |
<DAZzle Start="DAZ"><Package ID="1" /></DAZzle> |
<DAZzle Start="DAZ"><Package ID="1" /></DAZzle> |
|
|
>>> docinfo_to_etree(_DocInfo('DAZzle', 'PRINTING', 'Start'), root, False) |
>>> add_to_package(DocInfo('DAZzle', 'PRINTING', 'Start'), p, False) |
Traceback (most recent call last): |
Traceback (most recent call last): |
... |
... |
DocinfoConflict: Can't set 'DAZzle.Start=PRINTING' when 'DAZzle.Start=DAZ' already set |
DocInfoConflict: Can't set 'DAZzle.Start=PRINTING' when 'DAZzle.Start=DAZ' already set |
|
|
>>> root = ET.Element('DAZzle') |
>>> root = ET.Element('DAZzle') |
>>> pkg = ET.SubElement(root, 'Package', ID='1') |
>>> pkg = ET.SubElement(root, 'Package', ID='1') |
>>> print ET.tostring(root) |
>>> print ET.tostring(root) |
<DAZzle><Package ID="1" /></DAZzle> |
<DAZzle><Package ID="1" /></DAZzle> |
|
|
>>> docinfo_to_etree([verify_zip, Envelope], root, False) |
>>> b = Batch() |
>>> print ET.tostring(root) |
>>> p = Package(b) |
|
>>> add_to_package([verify_zip, Envelope], p, False) |
|
>>> print b.tostring() |
|
<DAZzle Start="DAZ"><Package ID="1"><FlatRate>TRUE</FlatRate></Package></DAZzle> |
|
|
|
>>> p.should_queue(COD) |
|
True |
|
>>> print b.tostring() |
<DAZzle Start="DAZ"><Package ID="1"><FlatRate>TRUE</FlatRate></Package></DAZzle> |
<DAZzle Start="DAZ"><Package ID="1"><FlatRate>TRUE</FlatRate></Package></DAZzle> |
|
>>> p.finish() |
|
>>> print b.tostring() |
|
<DAZzle Start="DAZ"><Package ID="1"><FlatRate>TRUE</FlatRate><Services |
|
COD="ON" /></Package></DAZzle> |
|
>>> p.should_queue(COD) |
|
False |
|
|
|
|
>>> root = make_tree([(Box,), (Envelope,)], verify_zip) |
Batch rollback:: |
>>> print ET.tostring(root) # doctest: +NORMALIZE_WHITESPACE |
|
|
>>> b = Batch() |
|
>>> print b.tostring() |
|
<DAZzle /> |
|
|
|
>>> b.ship(FlatRateEnvelope, FlatRateBox) |
|
Traceback (most recent call last): |
|
... |
|
DocInfoConflict: Can't set 'PackageType=FLATRATEBOX' when |
|
'PackageType=FLATRATEENVELOPE' already set |
|
|
|
>>> print b.tostring() # rollback on error |
|
<DAZzle /> |
|
|
|
|
|
Misc shipment:: |
|
|
|
>>> s = Shipment(verify_zip) |
|
>>> s.ship(Box) |
|
>>> s.ship(Envelope) |
|
>>> root, = s.batches |
|
>>> print root.tostring() |
<DAZzle Start="DAZ"><Package |
<DAZzle Start="DAZ"><Package |
ID="1"><FlatRate>BOX</FlatRate></Package><Package |
ID="1"><FlatRate>BOX</FlatRate></Package><Package |
ID="2"><FlatRate>TRUE</FlatRate></Package></DAZzle> |
ID="2"><FlatRate>TRUE</FlatRate></Package></DAZzle> |
|
|
|
|
|
DocInfo inversion:: |
|
|
|
>>> ~Envelope |
|
DocInfo('FlatRate', 'FALSE', None) |
|
>>> ~~Envelope |
|
DocInfo('FlatRate', 'TRUE', None) |
|
|
|
>>> ~DocInfo('Services', 'ON', 'RegisteredMail') |
|
DocInfo('Services', 'OFF', 'RegisteredMail') |
|
>>> ~~DocInfo('Services', 'ON', 'RegisteredMail') |
|
DocInfo('Services', 'ON', 'RegisteredMail') |
|
|
|
>>> ~DocInfo('DAZzle', 'YES', 'Prompt') |
|
DocInfo('DAZzle', 'NO', 'Prompt') |
|
>>> ~~DocInfo('DAZzle', 'YES', 'Prompt') |
|
DocInfo('DAZzle', 'YES', 'Prompt') |
|
|
|
|
|
|
|
|
The ``iter_docinfo()`` generic function yields "docinfo" objects for an |
The ``iter_docinfo()`` generic function yields "docinfo" objects for an |
application object. The default implementation is to raise an error:: |
application object. The default implementation is to raise an error:: |
|
|
>>> list(iter_docinfo(['a', 'b'])) |
>>> list(iter_docinfo(['a', 'b'])) |
['a', 'b'] |
['a', 'b'] |
|
|
This routine is used internally by ``docinfo_to_etree()``. |
This routine is used internally by ``add_to_package()``. |
|
|
|
|