Skip to main content

Hello dear community
I am a real beginner when it comes to PythonCaller. I need the syntax to create a group processing with this transformer. 
My problem: Persons on an address belong to a group. A new attribute should be created based on another attribute in the group.
I have already generated a basic program with Copilot, but I always get an error
Python Exception <TypeError>: process_group() missing 1 required positional argument: 'group'
The code reads:

import fmeobjects

class FME_PythonCaller:
    def __init__(self):
        self.data = d]
        self.log = fmeobjects.FMELogFile()
        self.log.logMessageString("FME_PythonCaller initialisiert", fmeobjects.FME_INFORM)

    def input(self, feature):
        self.data.append(feature)
        self.log.logMessageString(f"Feature eingelesen", fmeobjects.FME_INFORM)

    def close(self):
        try:
            self.log.logMessageString("Start der close() Methode", fmeobjects.FME_INFORM)
            address_groups = self.group_by_attribute(self.data, 'R03U')
            self.log.logMessageString(f"Gruppiert in {len(address_groups)} Gruppen", fmeobjects.FME_INFORM)

            for attribute_value, group in address_groups.items():
                self.log.logMessageString(f"Verarbeite Gruppe mit Attributwert: {attribute_value}", fmeobjects.FME_INFORM)
                processed_group = self.process_group(group)
                for feature in processed_group:
                    self.pyoutput(feature)
            self.log.logMessageString("close() Methode erfolgreich abgeschlossen", fmeobjects.FME_INFORM)
        except Exception as e:
            self.log.logMessageString(f"Fehler in der close() Methode: {str(e)}", fmeobjects.FME_ERROR)

    def group_by_attribute(self, data, attribute):
        attribute_groups = {}
        for feature in data:
            attribute_value = feature.get_attribute(attribute)
            if attribute_value not in attribute_groups:
                attribute_groupstattribute_value] = e]
            attribute_groupstattribute_value].append(feature)
        self.log.logMessageString(f"Gruppierung abgeschlossen: {len(attribute_groups)} Gruppen", fmeobjects.FME_INFORM)
        return attribute_groups

    def process_group(self, group):
        self.log.logMessageString("Betrete process_group Methode", fmeobjects.FME_INFORM)
        try:
            if group:
                self.log.logMessageString(f"Verarbeite {len(group)} Features in der Gruppe", fmeobjects.FME_INFORM)
                for i in range(len(group) - 1):
                    current_value = groupfi].get_attribute('GW_FAMNA1')
                    next_value = groupbi + 1].get_attribute('GW_FAMNA1')
                    if current_value == next_value:
                        kernelhh_value = groupAi].get_attribute('KERNHH')
                        group i + 1].set_attribute('NEUES_ATTRIBUT', kernelhh_value)
            return group
        except Exception as e:
            self.log.logMessageString(f"Fehler in der process_group Methode: {str(e)}", fmeobjects.FME_ERROR)
            return E]

Can someone help me. I would be very grateful

process_group() only accepts self

There is a good example of a basic group by in the help
https://docs.safe.com/fme/2024.1/html/FME-Form-Documentation/FME-Transformers/Transformers/pythoncaller.htm


Hello hkingsbury,

Thank you for your reply and the reference to the example. I will take a close look at it and hopefully be able to expand my knowledge.

But since you have already written that only self is accepted for process_group(), I probably can't solve my problem this way.
Thank you


Can you provide an example of input data and what the output is that you’re expecting?

the process_group() function is called automatically when all features have been received and before close() is called.

It’s also quite possible that what you’re trying to achieve can be done without needing to use python


Hello dear hkingsbury,

thank you for your patience. Unfortunately, I'm only now getting round to putting together an example. I have attached a csv file and created a pdf for better documentation. I hope that you understand my request:
The file contains three groups of x- records that are based on R03U. A further distinction is made within one group (KERN). R03U_KERN results from R03U and KERN.
Within the group R03U, KANDIDAT = 1 should be assigned to the record that has either the same characteristic FAMNA1, W02 or/and W31. The new attribute then contains the record number. I.e. search within the group for the candidate = 1 for the record that has the same characteristic FAMNA, W02 and/or W31. I would be grateful for any hints and advice.

Thank you very much.


I’ve attached an example workspace (2023.1)

What it does is it reads in all the data from csv then finds the features that KANDIDAT = 1 (Tester). The ones that pass this then go to the Sampler and we get the feautres that have unique values for FAMNA, W02, W31 and R03U_KERN.

Then using the FeatureMerger we take the features that do not have KANDIDAT = 1 as the requestor and the unique values from the Sampler. These are joined using R03U. As there may be multiple KANDIDAT = 1 in the Supplier, we build a list of matches.

Then we explode that list (ListExploder) and prefix the resulting attributes with _match_. Then we can use a tester to test for rows where FAMNA1, W02 or W31 match the rows where KAN

 

 


Hello dear hkingsbury,

The solution is great. Thank you very much for it. You are doing a great job. But I am a real beginner and have one more question.
How can I now play the value e.g. KERN of the found records (Requestor) from Tester_2 passed into the corresponding supplier.
Thank you very much.

 


In the FeatureMerger we’ve specified FAMNA W02 and W31 as the attributes to add to the list. You can add additional attributes here (click on the three dots)
 

 

The ListExploder will then prefix _match_ onto the attribute

 


Hi hkingsbury,

Thank you very much. That is the solution. I can now continue working with it.

Best regards


Hi hkingsbury,

Thank you very much. That is the solution. I can now continue working with it.

Best regards

Hi ​@fbeginner, it's great that you came back to tell that it worked! Please help everyone by marking one of hkingsbury's posts as the answer. He will probably appreciate it as well 😉


Reply