6Document and Label Designer Expressions

Document and Label Designer Expressions

Expressions in Document Label Designer are python expressions that fit on one line. An expression has to be one line of code to be executed. With expressions you can do a lookup for a database table and field.

Expressions

With the new functionality, UI column name can be used in expressions to do a lookup for a database table and field. The following are examples of lookup:

u"\~carrier_lpn.tracking_nbr"

where carrier_lpn is the database table and the tracking_nbr is the field separated by a .

Two lookups separated by a space as a string:

u"\~order_header.custom_long_text_3
        \~order_header.custom_long_text_4"

The following is an example of writing an if condition statement: Note the "" around the field lookup so that the result is treated as a string.

"Not Manifested!" if "\~carrier_lpn.tracking_nbr " == "" else "\~carrier_lpn.tracking_nbr"

Slicing and length expression example: Note this example starts from the 3rd position to the end of the string, extracting that portion of the string.

"\~order_header.order_nbr "[slice(2,len( "\~order_header.order_nbr "))]

Split expression with accessing the first element example:

split( "\~order_header.custom_long_text_3 ",
        ";")[0]

Complex split expression example:

split("\~order_header.custom_long_text_3", ";")[0] + "\n" +
        split("\~order_header.custom_long_text_3", ";")[1] + " " +
        split("\~order_header.custom_long_text_3", ";")[2] + " " +
        split("\~order_header.custom_long_text_3", ";")[3] + "\n" +
        split("\~order_header.custom_long_text_3", ";")[4] + ", " +
        split("\~order_header.custom_long_text_3", ";")[5] + " " +
        split("\~order_header.custom_long_text_3", ";")[6]

The following is an example of a more complex inline if split expression:

str("(No \~order_header.customer_number)") if
        len(split("\~order_header.custom_field_5", ";")) < 12 else str("(No " +
        split("\~order_header.custom_field_5", ";")[11] + "\~order_header.customer_number" +
        ")")

New Expressions Supported

u"str(5) + str(5)"

get_country_name("AD")

get_country_code("UNITED STATES")

get_us_state_name("GA")

get_us_state_code("GEORGIA")

"strftime(\~order_header.mod_ts, "%m/%Y")"

The following is an example of an expression for container weight:

str(int((10*\~container.weight)-0.5)+1).zfill(7) + "A"

A container weight of 65.46 would result in 0000655A. Zfill does the padding for the resulting string and int((10*\~container.weight)-0.5)+1 rounds the number up and moves up one decimal place.

To test your expression on your own computer, you will need simple eval python. You can look at more examples on the following website:

https://pypi.python.org/pypi/simpleeval

Passing Function Parameters

Example Of Date Time Formatting:

%d/%m/%Y - %H:%M:%S

See Python strftime Directives for more details on Date Time Formatting.

Blind Label Mappings:

"container.container_nbr"

"order_header.destination_company.code"

"order_header.dest_facility.code"

"company.code"

dict_get(get_obj(),
        "order_header.destination_company.code")
str(dict_get(get_obj(), "company.code"))[slice(5,len(dict_get(get_obj(),
        "company.code")))]

Python strftime Directives

Note: Examples are based on datetime.datetime(2013, 9, 30, 7, 6, 5.)

Code Meaning Example
%a Weekday as locale’s abbreviated name. Mon
%A Weekday as locale’s full name. Monday
%w Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. 1
%d Day of the month as a zero-padded decimal number. 30
%-d Day of the month as a decimal number. (Platform specific) 30
%b Month as locale’s abbreviated name. Sep
%B Month as locale’s full name. September
%m Month as a zero-padded decimal number. 09
%-m Month as a decimal number. (Platform specific) 9
%y Year without century as a zero-padded decimal number. 13
%Y Year with century as a decimal number. 2013
%H Hour (24-hour clock) as a zero-padded decimal number. 07
%-H Hour (24-hour clock) as a decimal number. (Platform specific) 7
%I Hour (12-hour clock) as a zero-padded decimal number. 07
%-I Hour (12-hour clock) as a decimal number. (Platform specific) 7
%p Locale’s equivalent of either AM or PM. AM
%M Minute as a zero-padded decimal number. 06
%-M Minute as a decimal number. (Platform specific) 6
%S Second as a zero-padded decimal number. 05
%-S Second as a decimal number. (Platform specific) 5
%f Microsecond as a decimal number, zero-padded on the left. 000000
%z UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).
%Z Time zone name (empty string if the object is naive).
%j Day of the year as a zero-padded decimal number. 273
%-j Day of the year as a decimal number. (Platform specific) 273
%U Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0. 39
%W Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0. 39
%c Locale’s appropriate date and time representation. Mon Sep 30 07:06:05 2013
%x Locale’s appropriate date representation. 09/30/13
%X Locale’s appropriate time representation. 07:06:05
%% A literal '%' character. %

Using Functions within Expressions

You can use functions within expressions in Document and Label Designer. The following are some examples:

Example of older method:

u"MYTEXT" + unicode(get_total_num_packed_containers_on_order(get_qs()))

Example of newer better method:

u"MYTEXT" + unicode(get_total_num_packed_containers_on_order(get_qs_list()))

The reason this is better is that some label types can have multiple querysets that should be passed into the function so get_qs might not be enough in some label templates.

Note: if your function has arguments you need to pass in you can do this as part of your expression:
u"MYTEXT" + unicode(get_total_num_packed_containers_on_order(get_qs_list(),
        args="MY_ARGS_GO_HERE"))

An example of passing actual arguments would be:

facility_time(get_qs_list(), args="d/%m/%Y - %H:%M")

Adjusting Timestamp using Expressions

You can use expressions to adjust the timestamp in your labels:

The following is an example of a received time stamp before trying to print it with specialized formatting:

strftime(\~container.received_time_stamp, "%d/%m/%Y %H:%M:%S") if
        \~container.received_time_stamp else "-"

Example of offsetting the date by 3 days earlier:

strftime(\~container.received_time_stamp - timedelta(days = 3), "%d/%m/%Y %H:%M:%S") if
        \~container.received_time_stamp else "-"

Example of offsetting the time by 3 hours earlier:

strftime(\~container.received_time_stamp - timedelta(hours = 3), "%d/%m/%Y %H:%M:%S") if
        \~container.received_time_stamp else "-"
Note: All expressions in document label designer must use “ and not ‘. If you need to have “ inside of another “ you can put a slash in front of the inner “.

Example:

get_us_state_name(\"GA\")”

Most of the time this is not needed and there are other ways around it. For instance this could have also been written like the following.

Example:

str( get_us_state_name("GA") )

Testing Your Expressions

Install python IDLE from Ninite:

https://ninite.com/

       
        functions={"slice": slice,
                    "len":
        len,
                   
        "replace":str.replace,
                    "split":
        str.split,
                    "splitlines":
        str.splitlines,
                    "get_literal":
        ast.literal_eval,
                    "dict_get":
        dict.get,
                    "str":
        str,
                    "int":
        int,
                    "float":
        float,
                    "unicode":
        unicode,
                    "date":
        datetime.date,
                    "time":
        datetime.time,
                    "datetime":
        datetime,
                    "strftime":
        datetime.strftime,
                    "datetimenow":
        datetime.now,
                    "strptime":
        datetime.strptime,
                    "tznow":
        tznow,
                    "get_obj":
        self.get_obj,
                    "str_upper":
        str.upper,
                    "str_lower":
        str.lower,
                    "get_qs":
        self.get_qs,
                    "chr" :
        chr,
                    "gs" :
        gs,
                    "gs1_li_code" :
        gs1_li_code,
                    "gs1_dm_code" :
        gs1_dm_code}
Note: the above functions do not get looked up with the \~ because they are built-in.

\~ is used for field look ups like column name or field functions. Additionally, standard functions get embedded so they do not need to be looked up.

If you are not able to install simple eval, you need to test on your local windows, mac, or linux computer with eval instead.

Note: you will need to look at the functions mapping to see how to map python commands to simple eval. For instance, "dict_get": dict.get, from above.

obj = {'container.container_nbr': 'x',

'order_header.destination_company.code': 'AE',

'order_header.dest_facility.code': '*',

'company.code': 'ACME'}

expression = 'str(dict.get(obj, "company.code"))[slice(5, len(dict.get(obj,
        "company.code")))]'

print eval(expression)

You will need to swap back your expression afterwards so that it says the following:

str(dict_get(get_obj(), "company.code"))[slice(5,len(dict_get(get_obj(),
        "company.code")))]

This allows it to work within the expression field in document label designer.

Another example:

obj = {'container.container_nbr': 'x',

'order_header.destination_company.code': 'AE',

'order_header.dest_facility.code': '*',

'company.code': 'ACME'}

expression = '"" if str(dict.get(obj,
        "order_header.destination_company.code")) == "*" else str(dict.get(obj,
        "order_header.destination_company.code"))'

print eval(expression)

The expression that goes into document label designer is:

"" if
        str(dict_get(get_obj(), "order_header.destination_company.code")) == "*" else
        str(dict_get(get_obj(), "order_header.destination_company.code"))

Another example of developing an expression in IDLE:

expression = 'str("(No 000111)") if len(cust.split(";")) < 12 else str("(No " +
        cust.split(";")[11] + "000111" + ")")'
print eval(expression)

The expression that goes into document label designer is:

str("(No \~order_header.customer_number)") if
        len(split("\~order_header.custom_field_5", ";")) < 12 else str("(No " +
        split("\~order_header.custom_field_5", ";")[11] + "\~order_header.customer_number" +
        ")")