如何在不使用 IDE 且不了解程序流程的情况下调试应用程序?

2023-12-24

我正在尝试修改优秀书籍提供的朴素贝叶斯分类器的代码集体智慧编程 https://rads.stackoverflow.com/amzn/click/com/0596529325,使其适应 GAE 数据存储(提供的代码使用 pysqlite2)。但尝试这样做时,我遇到了难以调试的错误。错误是这样的:

  File "C:\Users\CG\Desktop\Google Drive\Sci&Tech\projects\naivebayes\main.py", line 216, in post
    sampletrain(nb)
  File "C:\Users\CG\Desktop\Google Drive\Sci&Tech\projects\naivebayes\main.py", line 201, in sampletrain
    cl.train('Nobody owns the water.','good')
  File "C:\Users\CG\Desktop\Google Drive\Sci&Tech\projects\naivebayes\main.py", line 139, in train
    self.incf(f,cat)
  File "C:\Users\CG\Desktop\Google Drive\Sci&Tech\projects\naivebayes\main.py", line 71, in incf
    count=self.fcount(f,cat)
  File "C:\Users\CG\Desktop\Google Drive\Sci&Tech\projects\naivebayes\main.py", line 92, in fcount
    return float(res)
TypeError: float() argument must be a string or a number

错误就在这个块中:

  def fcount(self,f,cat):
    res = db.GqlQuery("SELECT * FROM fc WHERE feature =:feature AND category =:category", feature = f, category = cat).get()
#    res=self.con.execute(
#      'select count from fc where feature="%s" and category="%s"'
#      %(f,cat)).fetchone()
    if res is None: return 0
    else:
        res = fc.count
        return float(res)
#        return float(res[0])

如果我把set_trace()在第 91 行,像这样:

def fcount(self,f,cat):
    res = db.GqlQuery("SELECT * FROM fc WHERE feature =:feature AND category =:category", feature = f, category = cat).get()
    set_trace()
#    res=self.con.execute(
#      'select count from fc where feature="%s" and category="%s"'
#      %(f,cat)).fetchone()
    if res is None: return 0
    else:
        res = fc.count
        set_trace()
        return float(res)

我得到这个错误轨道:

File "C:\Users\CG\Desktop\Google Drive\Sci&Tech\projects\naivebayes\main.py", line 224, in post
    sampletrain(nb)
  File "C:\Users\CG\Desktop\Google Drive\Sci&Tech\projects\naivebayes\main.py", line 209, in sampletrain
    cl.train('Nobody owns the water.','good')
  File "C:\Users\CG\Desktop\Google Drive\Sci&Tech\projects\naivebayes\main.py", line 147, in train
    self.incf(f,cat)
  File "C:\Users\CG\Desktop\Google Drive\Sci&Tech\projects\naivebayes\main.py", line 77, in incf
    count=self.fcount(f,cat)
  File "C:\Users\CG\Desktop\Google Drive\Sci&Tech\projects\naivebayes\main.py", line 95, in fcount
    if res is None: return 0
  File "C:\Users\CG\Desktop\Google Drive\Sci&Tech\projects\naivebayes\main.py", line 95, in fcount
    if res is None: return 0
  File "C:\Python27\lib\bdb.py", line 48, in trace_dispatch
    return self.dispatch_line(frame)
  File "C:\Python27\lib\bdb.py", line 67, in dispatch_line
    if self.quitting: raise BdbQuit
BdbQuit

它与 GqlQuery 有关。我想在Python IDE中测试代码,一步步打印变量和查询,试图找出问题出在哪里。但是当我尝试在 python IDE 中执行此操作时,我收到错误消息(例如"ImportError: No module named webapp2")。而且我不太熟悉成功更改它的程序流程。实际上,我尝试这样做,但迷失了:我是一名新手程序员,最近才开始学习 OOP)。在这种情况下找到错误的最佳方法是什么?

预期的答案应包括此错误标识。

预先感谢您的任何帮助!

这里是整个代码:

#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-

import os

import webapp2

import jinja2

from jinja2 import Environment, FileSystemLoader

jinja_environment = jinja2.Environment(autoescape=True,
    loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')))

import random

from google.appengine.ext import db

import re

import math

def set_trace():
import pdb, sys
debugger = pdb.Pdb(stdin=sys.__stdin__,
    stdout=sys.__stdout__)
debugger.set_trace(sys._getframe().f_back)

class fc(db.Model):
    feature = db.StringProperty(required = True)
    category = db.StringProperty(required = True)
    count = db.IntegerProperty(required = True)

class cc(db.Model):
    category = db.StringProperty(required = True)
    count = db.IntegerProperty(required = True)

def getfeatures(doc):
  splitter=re.compile('\\W*')
  # Split the words by non-alpha characters
  words=[s.lower() for s in splitter.split(doc)
          if len(s)>2 and len(s)<20]
  return dict([(w,1) for w in words])

class classifier:
  def __init__(self,getfeatures, filename=None):
    # Counts of feature/category combinations
    self.fc={}
    # Counts of documents in each category
    self.cc={}
    self.getfeatures=getfeatures

#  def setdb(self,dbfile):
#    self.con=sqlite.connect('db_file')
#    self.con=sqlite3.connect(":memory:")
#    self.con.execute('create table if not exists fc(feature,category,count)')
#    self.con.execute('create table if not exists cc(category,count)')

  def incf(self,f,cat):
    count=self.fcount(f,cat)
    if count==0:
      fc_value = fc(feature = f, category = cat, count = 1)
      fc_value.put()
    else:
        update = db.GqlQuery("SELECT count FROM fc where feature =:feature AND category =:category", feature = f, category = cat).get()
        update.count = count + 1
        update.put()
#      self.con.execute(
 #       "update fc set count=%d where feature='%s' and category='%s'"
  #      % (count+1,f,cat))

  def fcount(self,f,cat):
    res = db.GqlQuery("SELECT * FROM fc WHERE feature =:feature AND category =:category", feature = f, category = cat).get()
#    res=self.con.execute(
#      'select count from fc where feature="%s" and category="%s"'
#      %(f,cat)).fetchone()
    if res is None: return 0
    else:
        res = fc.count
        return float(res)
#        return float(res[0])

  def incc(self,cat):
    count=self.catcount(cat)
    if count==0:
      #  self.con.execute("insert into cc values ('%s',1)" % (cat))
      cc_value = cc(category = cat, count = 1)
      cc_value.put()
    else:
        update = db.GqlQuery("SELECT count FROM cc where category =:category", category = cat).get()
        update.count = count + 1
        update.put()
#      self.con.execute("update cc set count=%d where category='%s'"
#                       % (count+1,cat))

  def catcount(self,cat):
#    res=self.con.execute('select count from cc where category="%s"'
 #                        %(cat)).fetchone()
    res = db.GqlQuery("SELECT count FROM cc WHERE category =:category", category = cat).get()
    if res is None: return 0
#    else: return float(res[0])
    else: return float(res)

  def categories(self):
#    cur = self.con.execute('select category from cc');
    cur = db.GqlQuery("SELECT category FROM cc").fetch(999)
    return [d[0] for d in cur]

  def totalcount(self):
   # res=self.con.execute('select sum(count) from cc').fetchone();
    all_cc = db.GqlQuery("SELECT * FROM cc").fetch(999)
    res = 0
    for cc in all_cc:
        count = cc.count
        res+=count
#    res = db.GqlQuery("SELECT sum(count) FROM cc").get()
#    if res==None: return 0
    if res == 0: return 0
#    return res[0]
    return res

  def train(self,item,cat):
    features=self.getfeatures(item)
    # Increment the count for every feature with this category
    for f in features.keys():
##    for f in features:
      self.incf(f,cat)
    # Increment the count for this category
    self.incc(cat)
#    self.con.commit()

  def fprob(self,f,cat):
    if self.catcount(cat)==0: return 0
    # The total number of times this feature appeared in this
    # category divided by the total number of items in this category
    return self.fcount(f,cat)/self.catcount(cat)

  def weightedprob(self,f,cat,prf,weight=1.0,ap=0.5):
    # Calculate current probability
    basicprob=prf(f,cat)
    # Count the number of times this feature has appeared in
    # all categories
    totals=sum([self.fcount(f,c) for c in self.categories()])

    # Calculate the weighted average
    bp=((weight*ap)+(totals*basicprob))/(weight+totals)
    return bp

class naivebayes(classifier):
  def __init__(self,getfeatures):
    classifier.__init__(self, getfeatures)
    self.thresholds={}

  def docprob(self,item,cat):
    features=self.getfeatures(item)
    # Multiply the probabilities of all the features together
    p=1
    for f in features: p*=self.weightedprob(f,cat,self.fprob)
    return p

  def prob(self,item,cat):
    catprob=self.catcount(cat)/self.totalcount()
    docprob=self.docprob(item,cat)
    return docprob*catprob

  def setthreshold(self,cat,t):
    self.thresholds[cat]=t

  def getthreshold(self,cat):
    if cat not in self.thresholds: return 1.0
    return self.thresholds[cat]

  def classify(self,item,default=None):
    probs={}
    # Find the category with the highest probability
    max=0.0
    for cat in self.categories():
      probs[cat]=self.prob(item,cat)
      if probs[cat]>max:
        max=probs[cat]
        best=cat
    # Make sure the probability exceeds threshold*next best
    for cat in probs:
      if cat==best: continue
      if probs[cat]*self.getthreshold(best)>probs[best]: return default
    return best

def sampletrain(cl):
  cl.train('Nobody owns the water.','good')
  cl.train('the quick rabbit jumps fences','good')
  cl.train('buy pharmaceuticals now','bad')
  cl.train('make quick money at the online casino','bad')
  cl.train('the quick brown fox jumps','good')    

class MainHandler(webapp2.RequestHandler):
    def get(self):
        template_values = {"given_sentence":'put a name here'}
        template = jinja_environment.get_template('index.html')
        self.response.out.write(template.render(template_values))

    def post(self):
        nb = naivebayes(getfeatures)
        sampletrain(nb)
        given_sentence = self.request.get("given_sentence")
        spam_result = nb.classify(given_sentence)
        submit_button = self.request.get("submit_button")
        if submit_button:
            self.redirect('/test_result?spam_result=%s&given_sentence=%s' % (spam_result, given_sentence))

class test_resultHandler(webapp2.RequestHandler):
    def get(self):
        spam_result = self.request.get("spam_result")
        given_sentence = self.request.get("given_sentence")
        test_result_values = {"spam_result": spam_result,
                             "given_sentence": given_sentence}
        template = jinja_environment.get_template('test_result.html')
        self.response.out.write(template.render(test_result_values))

app = webapp2.WSGIApplication([('/', MainHandler), ('/test_result', test_resultHandler)],
                              debug=True)

您遇到了一个类型错误,应该很容易找到,但您似乎在部署服务器上还是在 IDE 中运行它之间做出了错误的选择。

有一个GAE开发服务器 https://developers.google.com/appengine/docs/python/tools/devserver您在本地运行并模拟部署环境。

扔掉你的 IDE,在开发服务器上运行,使用print自由地确保这些值是您期望它们从错误源辐射出来的值。

IDE 无法替代理解代码正在做什么,依赖它会在您和代码之间设置一层隔离,这只会让调试变得更加困难。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在不使用 IDE 且不了解程序流程的情况下调试应用程序? 的相关文章

随机推荐

  • 如何在 Jekyll 中使用 markdownify 显示索引摘录

    我希望在索引页上显示较长帖子或页面的简短文本摘录 我本来打算在 Front Matter 中使用自定义变量并抓住它 但后来我看到了过滤器 excerpt 我看到在Jekyll 文档 http jekyllrb com docs templa
  • PowerShell 相当于“head -n-3”?

    我已经能够追踪基本的头 尾功能 head 10 myfile lt gt cat myfile select first 10 tail 10 myfile lt gt cat myfile select last 10 但是 如果我想列出
  • Java中是否可以在指定时间内停止函数的执行?

    我想知道如何在java中指定时间内停止指定函数的执行 例如 我可以调用一个名为 print data 的函数 如果执行需要更多时间 我将不得不停止该函数的执行 是否可以这样停止执行 提前致谢 您可以向函数添加一些检查 保存函数开始工作时的时
  • 在构建过程中如何从 docker 文件打印到控制台?

    假设您有一些 Dockerfile 需要向该文件添加什么 以便在构建期间将字符串 即 Hello World 打印到控制台 docker build RESEARCH 这个问题是 Google 中该主题的热门问题 我通过谷歌搜索并登陆这里进
  • 如何定期制作动画?

    我有一个故事板 可以使分针滑动 6 度 现在我希望分针永远每 59 秒滑动一次 故事板有什么属性或我可以做的任何其他方式吗 我的故事板
  • 如何在 spring-data mongodb 中将 updateOption 与 arrayFilters 一起使用?

    我在 Mongodb 中有一个如下所示的文档 现在 我想转到基于特定 的文档 id对于该文档 想要转到 计划 列表 其中对于几个特定日期 不仅是一个日期 而是多个日期 我想将状态更新为 已预订 我浏览了此链接 如何使用带有 arrayFil
  • CodeIgniter - CI_DB_mysqli_result 类的对象无法转换为字符串

    我目前正在使用 codeIgniter 我正在尝试根据电子邮件地址更新数据库中的密码 否则我会遇到问题 我使用在 CodeIgniter 网站上找到的以下代码来更新我的数据库 this gt db gt set field field 1
  • 哪里可以找到旧版本的 android ndk [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 好吧 你们很多人都会说这是重复的this https stackoverflow com questions 6849981 where
  • 那么如何将数据推送到承诺内的数组呢?

    我一直在试图弄清楚 如何将结果从 Promise 循环推送到数组 谁能指出我正确的位置 const ids 1 2 3 let results for let id of ids getLight id then light gt resu
  • 将夜间叠加层添加到谷歌地图 API

    我的 Angular 项目中有以下 html 部分
  • 如何更改 TemplateField 中 ItemTemplate 的命令文本和图像按钮

    我有一个带有 ImageButton 的列 我的数据库字段有bit数据类型 我希望我的记录在该列中具有真正的价值True jpg我的命令变成MakeFalse当它有错误值时显示False jpg我的命令变成MakeTrue 我怎样才能做到这
  • 如何在AS3中发送GET请求?

    我在这里看到这个例子 http damn ihateblue net 2011 09 24 actionscript 3 send getpost http damn ihateblue net 2011 09 24 actionscrip
  • 防止 Javascript 转义文本?

    声明 text section main 结果是 节 主 有没有办法阻止 Javascript 解释器将反斜杠视为转义字符并将其删除 我希望能够声明 section main 并在输出中保留反斜杠 节 main 注意 我意识到如果我使用两个
  • JNI 中 jclass 的类名

    这可能是一个愚蠢的问题 暴露出对 JNI 缺乏理解 我正在编写一个封装 Java VM 的 C 程序 我使用 CallVoidMethod 等调用来调用 VM 内的函数 这纯粹是背景知识 与问题不太相关 我希望能够找到给定 jclass 实
  • Struts2 排除模式不起作用

    我正在将 struts2 用于基于 GAE 的应用程序 我有一个 servlet 来上传这样的文件 Override protected void doPost HttpServletRequest req HttpServletRespo
  • 如何在Amazon Linux系统中升级ruby版本?

    我使用 padrino ruby 框架开发了 ruby 应用程序 我想将其部署在亚马逊上 我用了这张图片 Amazon Linux AMI 2017 09 1 HVM SSD 卷类型 图像描述是 Amazon Linux AMI 是一个由
  • AWS Lambda:创建触发器

    当我尝试添加触发器时 出现以下错误 创建触发器时出错 配置定义不明确 如果同一事件类型的前缀重叠 则两个规则中不能有重叠的后缀 我不确定这里出了什么问题 原因之一可能是之前使用相同触发器的其他某个 lambda 函数已被删除 这不会自动清除
  • 提交时合并两个表单中的值

    我在一个 html 页面上有两个表单 使用 jQuery 是否可以在提交第一个表单时将两个表单中的数据放入 POST 数据中 jQuery序列化支持多个表单元素 所以可以这样做 form1 form2 serialize 对于你的情况 你可
  • ElasticSearch PutMapping API:MapperParsingException 解析后根类型映射不为空

    我的本地实例上有一个 River ES 1 3 4 和 JDBC For MySql 1 3 4 4 这条河运行良好 并在 ES 中导入数据 我面临的问题是我的字段之一是文本字段并且其中有空格 例如 实时计算器 ES 将其索引为 实时 时间
  • 如何在不使用 IDE 且不了解程序流程的情况下调试应用程序?

    我正在尝试修改优秀书籍提供的朴素贝叶斯分类器的代码集体智慧编程 https rads stackoverflow com amzn click com 0596529325 使其适应 GAE 数据存储 提供的代码使用 pysqlite2 但