TypeError:__ init __()得到了一个意外的关键字参数' customer

时间:2017-08-22 12:36:20

标签: python django

Docx4J.bind
( 
    lc_docxPackage,
    new ByteArrayInputStream( ic_data ), 
    ai_flags
);
Docx4J.save( lc_docxPackage, ac_os );

现在我知道问题出现了:

class RequestProcess(dxmodels.WorkflowEnabled):

    def __init__(self):
        lang = self.customer.user.customerprofile.language

    @xworkflows.on_enter_state('documents')
    def on_documents(self, *args, **kwargs):
        self.update_timestamps()
        ctx = {'request': self}
        EmailFromTemplate('request-required-documents', extra_context=ctx, lang=lang)\
            .send_to(self.customer.user)

    @xworkflows.on_enter_state('validation')
    def on_validation(self, *args, **kwargs):
        self.update_timestamps()
        validate_requirements_for_request(self)

    @xworkflows.on_enter_state('signature')
    def on_signature(self, *args, **kwargs):
        self.customer_product.lock()
        self.contract = create_contract(self)
        self.update_timestamps()
        ctx = {'request': self}
        EmailFromTemplate('product-request-accepted', extra_context=ctx, lang=lang)\
            .send_to(self.customer.user)

    class Meta:
        abstract = True

因为

def create_request_from_external(obj):
    ...
    req = Request(**{
        'state': 'documents',
        'request_type': get_request_type(obj.customer),
        'customer': obj.customer,
        'created_by': get_system_user(),
        'entered_documents_at': datetime.datetime.now(),
    })

    req.save()
    obj.request = req
    obj.save()

    create_customer_product(req, obj.product)

    audit.log('system',
              verb='created',
              target=obj.customer.user,
              action_object=req,
              message=_('Customer created for %s') % obj)

    # Build initial requirements state
    validate_requirements_for_request(req)
    return req

我有以下错误class Request(RequestProcess, FolderLinkMixin, TimeStampedModel): 。我不想在每个方法中添加TypeError: __init__() got an unexpected keyword argument 'customerd。我只想在变量中实例化一次。我该怎么办才能修好它?我是否需要直接在课程lang = self.customer.user.customerprofile.languageRequest

中解决此问题

例如,如果我编写一个方法,删除RequestProcess方法并注释其他方法,它就可以正常工作。

__init__

回溯

@xworkflows.on_enter_state('deposit')
def on_deposit(self, *args, **kwargs):
    self.update_timestamps()
    self.customer_product.lock()
    create_loan_from_request(self)
    ctx = {'request': self, 'deposit_date': self.loan.product.deposit_date}
    EmailFromTemplate('deposit-notice', extra_context=ctx, lang=self.customer.user.customerprofile.language)\
        .send_to(self.customer.user)

1 个答案:

答案 0 :(得分:1)

问题是您已将模型的public interface Classifier { public class Recognition{ private final String id; private final String title; private final Float confidence; private RectF location; public Recognition( final String id, final String title, final Float confidence, final RectF location){ this.id = id; this.title = title; this.confidence = confidence; this.location = location; } public String getId(){return id;} public String getTitle(){return title;} public Float getConfidence(){return confidence;} public RectF getLocation(){return location;} public void setLocation(RectF location){this.location = location;} public String toString(){ String resultString = ""; if (id != null) { resultString += "[" + id + "] "; } if (title != null) { resultString += title + " "; } if (confidence != null) { resultString += String.format("(%.1f%%) ", confidence * 100.0f); } if (location != null) { resultString += location + " "; } return resultString.trim(); } } List<Recognition> recognizeImage(Bitmap bitmap); void enableStatLogging(final boolean debug); String getStatString(); void close(); } 方法替换为不接受任何参数的方法:

public class TensorFlowImageClassifier implements Classifier {
private static final String TAG = "TensorFlowImageClassifier";

private static final int MAX_RESULTS = 3;
private static final float THRESHOLD = 0.1f;

private String inputName;
private String outputName;
private int inputSize;
private int imageMean;
private float imageStd;

private Vector<String> labels = new Vector<String>();
private int[] intValues;
private float[] floatValues;
private float[] outputs;
private String[] outputNames;

private boolean logStats = false;
private TensorFlowInferenceInterface inferenceInterface;
private TensorFlowImageClassifier() {}

/*
assetManager : assets 로드하는데 사용
modelFilename : pb 파일
labelFilename : txt 파일
inputSize : 정사각형 길이, inputSize * inputSize
imageMean : image values 평균값
imageStd : image values 표준값?
inputName : image input 노드 레이블
outputName : output 노드 레이블
 */

public static Classifier create(
        AssetManager assetManager, String modelFilename, String labelFilename, int inputSize, int imageMean, float imageStd, String inputName, String outputName){
    TensorFlowImageClassifier c = new TensorFlowImageClassifier();
    c.inputName = inputName;
    c.outputName = outputName;

    String actualFilename = labelFilename.split("file:///android_asset/")[1];
    Log.d(TAG, "reading labels from : " + actualFilename);
    BufferedReader br = null;

    try {
        br = new BufferedReader(new InputStreamReader(assetManager.open(actualFilename)));
        String line;
        while((line = br.readLine()) != null){
            c.labels.add(line);
        }
        br.close();
    } catch (IOException e) {
        throw new RuntimeException("failed reading labels" , e);
    }

    c.inferenceInterface = new TensorFlowInferenceInterface(assetManager, modelFilename);

    final Operation operation = c.inferenceInterface.graphOperation(outputName);
    final int numClasses = (int)operation.output(0).shape().size(1);
    Log.d(TAG, "reading " + c.labels.size() + " labels, size of output layers : " + numClasses);

    c.inputSize = inputSize;
    c.imageMean = imageMean;
    c.imageStd = imageStd;

    c.outputNames = new String[]{outputName};
    c.intValues = new int[inputSize * inputSize];
    c.floatValues = new float[inputSize * inputSize * 3];
    c.outputs = new float[numClasses];

    return c;
}

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public List<Recognition> recognizeImage(final Bitmap bitmap){
    beginSection("recognizeImage");
    beginSection("preprocessBitmap");

    bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
    for(int i = 0; i < intValues.length; i++){
        final int val = intValues[i];
        floatValues[i*3+0] = (((val >> 16) & 0xFF) - imageMean) / imageStd;
        floatValues[i*3+1] = (((val >> 8) & 0xFF) - imageMean) / imageStd;
        floatValues[i*3+2] = ((val & 0xFF) - imageMean) / imageStd;
    }
    endSection();

    beginSection("feed");
    inferenceInterface.feed(inputName, floatValues, 1, inputSize, inputSize, 3);
    endSection();

    beginSection("run");
    inferenceInterface.run(outputNames, logStats);
    endSection();

    beginSection("fetch");
    inferenceInterface.fetch(outputName, outputs);
    endSection();


    PriorityQueue<Recognition> pq = new PriorityQueue<Recognition>(
            3,
            new Comparator<Recognition>(){
                public int compare(Recognition lhs, Recognition rhs){
                    return Float.compare(rhs.getConfidence(), lhs.getConfidence());
                }
            }
    );

    for(int i = 0; i < outputs.length; ++i){
        if(outputs[i] > THRESHOLD){
            pq.add(
                    new Recognition("" + i, labels.size() > i ? labels.get(i) : "unknown", outputs[i], null));
        }
    }

    final ArrayList<Recognition> recognitions = new ArrayList<Recognition>();
    int recognitionSize = Math.min(pq.size(), MAX_RESULTS);
    for(int i = 0; i < recognitionSize; ++i){
        recognitions.add(pq.poll());
    }
    endSection();

    return recognitions;
}

public void enableStatLogging(boolean logStats){this.logStats = logStats;}
public String getStatString(){return inferenceInterface.getStatString();}
public void close(){inferenceInterface.close();}
}

另请注意,__init__仅适用于class RequestProcess(dxmodels.WorkflowEnabled): def __init__(self): lang = self.customer.user.customerprofile.language 方法,因为您未设置lang,所以无法在其他地方访问它。

我会尽可能避免覆盖模型的__init__方法。在这种情况下,我认为最好定义一个属性。

self.lang

在模型方法中,您可以访问__init__