def transform_many(self, inputs, options=None, **kwargs): """ Override the standard behavior of the transform_many by only accepting list inputs which we use to perform the choose operation. """ if not inputs: if options is not None and options.get('default') is not None: return options.get('default') return u'' if not isinstance(inputs, list): self.raise_exception('Choose requires a list of inputs') if options is None: options = {} default = options.get('default') op = options.get('operation', None) if op is None or op == '': self.raise_exception('Missing Operation') if op in self._operations: op_func = self._operations[op] return op_func(inputs, default=default) i = try_parse_number(op, cls=int, default=None) if i is None: return default return self.choose_nth(i, inputs, default=default)
def transform(self, str_input, separator=u'', index=0, **kwargs): if not str_input: return u'' separator = expand_special_chargroups(separator) if separator: segments = str_input.split(separator) else: segments = str_input.split() if index == 'all': return segments if index == 'fields': return { u'Item {}'.format(i + 1): s for i, s in enumerate(segments) } index = try_parse_number(index, cls=int) try: return segments[index] except: pass return u''
def transform(self, str_input, find=u'', offset=0, **kwargs): str_input = str_input or u'' find = find or u'' pos = -1 if isinstance(offset, basestring): offset = try_parse_number(offset, cls=int, default=None) if not isinstance(offset, (int, long)): self.raise_exception('offset must be a number') if str_input and find: pos = str_input.find(find, offset) return pos
def transform(self, str_input, separator=u'', index=0, **kwargs): if not str_input: return u'' separator = expand_special_chargroups(separator) if separator: segments = str_input.split(separator) else: segments = str_input.split() if index == 'all': return segments index = try_parse_number(index, cls=int) try: return segments[index] except: pass return u''
def transform( self, input_key, my_dict=None, decimals=2, subtotal_toggle=False, **kwargs ): """Take a dict input and output an array of one or more Zapier standard line-items. Subtotal value takes the values from two other fields like Price and Quantity and multiplies them Example: User inputs some strings and a dict like this: input_key = "Order Lines" my_dict = { "Price": "5,3.5,4", "Description": "Hat,Shoes,Shirt", "Quantity": "1,2,1" } decimals = "2" subtotal_toggle=True, Expected output (Zapier Standard Line Items with optional, calculated Subtotal property): { Order Lines": [ { "Price": "5", "Description": "Hat", "Quantity": "1", "Subtotal": "5.00" }, { "Price": "3.5", "Description": "Shoes", "Quantity": "2", "Subtotal": "7.00 }, { "Price": "4", "Description": "Shirt", "Quantity": "1", "Subtotal": "4.00" } ] } """ # make sure that a name is set for the line-item group if not input_key: input_key = "Line-item(s)" # initialize variables if my_dict is None: my_dict = {} output = {input_key: []} longest_array = 0 subtotal = "subtotal" price = "Price" qty = "Quantity" # check for alternative cases for property names for price and quantity if price not in my_dict: for k in my_dict: if k.lower() == "price": price = k if qty not in my_dict: for k in my_dict: if k.lower() == "quantity": qty = k elif k.lower() == "qty": qty = k decimals = try_parse_number(decimals) try: my_places = Decimal(10) ** (-1 * int(decimals)) except ValueError as e: self.raise_exception( "Please enter an integer for the 'Decimal Places for Subtotal Values' field." ) # filter out entries with no key my_dict.pop("", None) # split each string value in the dict by ',' and determine the # length of longest array for k, v in my_dict.items(): # might not need iteritems my_dict[k] = v.split(",") if len(my_dict[k]) > longest_array: longest_array = len(my_dict[k]) # make individual objects for each line (example: could include 1 value each of qty, description, price) to add to the output object for num in range(0, longest_array): # initialize a new individual line-item this_line_item = {} for k, v in my_dict.items(): # Try to add each property from my_dict to the individual line-item. # Skips if the list isn't long enough for an available property. try: this_line_item[k] = v[num] except IndexError as e: pass if price in this_line_item and qty in this_line_item and subtotal_toggle: # Try to create a subtotal value for this line item if: # Create Subtotal Property is "Yes" # There is no conflict between a Line-item property with the same name as the Subtotal field # The Price and Quantity fields both exist in this line item. try: decimal_price = Decimal(this_line_item[price]) decimal_qty = Decimal(this_line_item[qty]) # must convert to string or float here, or else output will include the text "Decimal()" this_line_item[subtotal] = str( (decimal_price * decimal_qty).quantize(my_places) ) except (KeyError, ValueError, InvalidOperation) as e: # These are the three error types we'd expect to happen in this block, although KeyError is minimized # by the if statement pass # try adding the individual line item object to the main output array. Skips if no object is available (unlikely). try: output[input_key].append(this_line_item) except IndexError as e: pass return output