<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def edit_service_group(self, loadbal_id, group_id, allocation=None, port=None, routing_type=None, routing_method=None):
 """Edit an existing service group. :param int loadbal_id: The id of the loadbal where the service resides :param int group_id: The id of the service group :param int allocation: the % of connections to allocate to the group :param int port: the port of the service group :param int routing_type: the routing type to set on the service group :param int routing_method: The routing method to set on the group """


        mask = 'virtualServers[serviceGroups[services[groupReferences]]]'

        load_balancer = self.lb_svc.getObject(id=loadbal_id, mask=mask)
        virtual_servers = load_balancer['virtualServers']

        for virtual_server in virtual_servers:
            if virtual_server['id'] == group_id:
                service_group = virtual_server['serviceGroups'][0]
                if allocation is not None:
                    virtual_server['allocation'] = allocation
                if port is not None:
                    virtual_server['port'] = port
                if routing_type is not None:
                    service_group['routingTypeId'] = routing_type
                if routing_method is not None:
                    service_group['routingMethodId'] = routing_method
                break

        return self.lb_svc.editObject(load_balancer, id=loadbal_id)