我有一个需要手动管理事务的视图功能,但是当我应用@transaction.commit_manually装饰器时,django总是提出以下异常.
从下面的代码跟踪中可以看出,事务是在从视图返回之前提交的.
我使用django 1.4在Windows和Linux上使用sqlite.
以下是django_trace的输出,后跟异常.要清楚:无论我是否使用django_trace,都会发生这种情况,并且当没有装饰器时,不会引发任何异常.这不是由"吞噬"异常引起的.
请注意,下面的第60行位于上下文处理器内部,因此位于commit_manually包装视图之外.
01->mainapp.views:1321: transaction.commit()
01->mainapp.views:1322: return render_to_response('mainapp/templates/incorporate.html',
01->mainapp.views:1323: RequestContext(request, form_params))
02-->mainapp.views:60: transaction.rollback_unless_managed()
02-->mainapp.views:61: return {'home_login_form': AuthenticationForm(request)}
Traceback (most recent call last):
File "C:\Users\Marcin\Documents\oneclickcos\lib\site-packages\django\contrib\staticfiles\handlers.py", line 67, in __call__
return self.application(environ, start_response)
File "C:\Users\Marcin\Documents\oneclickcos\lib\site-packages\django\contrib\staticfiles\handlers.py", line 67, in __call__
return self.application(environ, start_response)
File "C:\Users\Marcin\Documents\oneclickcos\lib\site-packages\django\core\handlers\wsgi.py", line 241, in __call__
response = self.get_response(request)
File "C:\Users\Marcin\Documents\oneclickcos\lib\site-packages\django\core\handlers\base.py", line 179, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "C:\Users\Marcin\Documents\oneclickcos\lib\site-packages\django\core\handlers\base.py", line 221, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "C:\Users\Marcin\Documents\oneclickcos\lib\site-packages\django\core\handlers\base.py", line 111, …Run Code Online (Sandbox Code Playgroud) 我想要球拍程序和python程序之间的通信.
我的球拍代码:
#lang racket
(define-values (sp o i e) (subprocess #f #f #f "hello.exe" ))
(display "server" i)
(flush-output i)
(display (read o))
Run Code Online (Sandbox Code Playgroud)
我的python代码:
input_var = raw_input("Enter something: ")
print ("you entered " + input_var)
Run Code Online (Sandbox Code Playgroud)
如果我只是在我的python程序中打印它工作正常.如果我正在阅读球拍程序的输入,它会挂起.我想从球拍中读取消息.
我有一个这样的列表:
[[(video1,4)], [(video2,5),(video3,8)], [(video1,5)], [(video5, 7), (video6,9)]...]
Run Code Online (Sandbox Code Playgroud)
此列表中的每个项目可能包含单个数据对或元组,我想将此列表更改为
[(video1,4),(video2,5),(video3,8),(video1,5),(video5,7),(video6,9)...]
Run Code Online (Sandbox Code Playgroud)
然后这样做:
for item in list:
reqs = reqs + item[1]
b.append(item[0])
c = set(b)
Run Code Online (Sandbox Code Playgroud)
我不知道如何更改列表结构,或者如何根据原始列表进行相同的计算?
我是编程和尝试自学的新手.我目前正在尝试学习如何从类构建对象,我想我理解.我当前的任务是将对象添加到列表中并打印该列表.最终,我正在尝试构建一个创建对象的程序,并列出已在编号列表中创建的每个对象,即:
1 - tomato, red
2 - corn, yellow
etc...
Run Code Online (Sandbox Code Playgroud)
首先,我只是想构建这个的基本部分.这是我做的:
# Builds objects on instantiation for a vegetable and color
class Veg:
def __init__(self, name, color):
self.name = name
self.color = color
print('You have created a new', self.color, self.name, end='.\n')
# Function to create a new vegetable and store it in a list
def createVeg():
name = input('What is the name of the Vegetable? ')
color = input('What color is the vegetable? ')
Veg(name, color)
vegList.append(Veg)
return
# Initialize …Run Code Online (Sandbox Code Playgroud) 我正在努力编写一个基本单元测试来创建数据框,使用Spark提供的示例文本文件,如下所示.
class dataLoadTest extends FunSuite with Matchers with BeforeAndAfterEach {
private val master = "local[*]"
private val appName = "data_load_testing"
private var spark: SparkSession = _
override def beforeEach() {
spark = new SparkSession.Builder().appName(appName).getOrCreate()
}
import spark.implicits._
case class Person(name: String, age: Int)
val df = spark.sparkContext
.textFile("/Applications/spark-2.2.0-bin-hadoop2.7/examples/src/main/resources/people.txt")
.map(_.split(","))
.map(attributes => Person(attributes(0),attributes(1).trim.toInt))
.toDF()
test("Creating dataframe should produce data from of correct size") {
assert(df.count() == 3)
assert(df.take(1).equals(Array("Michael",29)))
}
override def afterEach(): Unit = {
spark.stop()
}
Run Code Online (Sandbox Code Playgroud)
}
我知道代码本身是有效的(来自spark.implicits._ .... toDF()),因为我已经在Spark-Scala …
当我尝试创建一个新用户时,我得到:no such table: tastypie_apikey.有谁知道为什么会这样?tastypie文档没有引用任何需要创建的数据库表,或者确实是一种让tastypie进行任何此类更改的方法.
考虑以下:
>>> from django.conf import settings
>>> import os
>>> settings.VIRTUAL_ENV
'C:/Users/Marcin/Documents/oneclickcos'
>>> settings.EXTRA_BASE
'/oneclickcos/'
>>> os.path.join(settings.VIRTUAL_ENV,settings.EXTRA_BASE)
'/oneclickcos/'
Run Code Online (Sandbox Code Playgroud)
你可以想像,我指望或希望的串联'C:/Users/Marcin/Documents/oneclickcos'和'/oneclickcos/'要'/oneclickcos/'.
奇怪的是,反转路径组件再一次显示python忽略了第一个路径组件:
>>> os.path.join(settings.EXTRA_BASE,settings.VIRTUAL_ENV)
'C:/Users/Marcin/Documents/oneclickcos'
Run Code Online (Sandbox Code Playgroud)
虽然这有点像预期的:
>>> os.path.join('/foobar',settings.VIRTUAL_ENV,'barfoo')
'C:/Users/Marcin/Documents/oneclickcos\\barfoo'
Run Code Online (Sandbox Code Playgroud)
我当然是在Windows(Windows 7)上使用本机python运行的.
为什么会发生这种情况,我该怎么办呢?
我在模型上有一个如下所示的IntegerField:
amount = models.IntegerField()
Run Code Online (Sandbox Code Playgroud)
访问它时,它有时会返回一个字符串.造成这种情况的最直接原因是它已经为其分配了一个字符串.到目前为止,如此愚蠢.它甚至在保存后也会返回一个字符串.
这让我感到有些惊讶:如果IntegerField在赋值时将其值强制转换为整数(或者,最迟在保存时),那将是很好的,因此用户可以依赖它作为整数.
(我的应用程序使用sqlite.)
有没有办法让IntegerField只返回整数?或者我是否必须创建自定义字段才能执行此操作?
我正在通过在线教程学习Python.我的问题是,当我运行脚本时,无论我输入什么响应,我得到的是if go =="kitchen"......
def go_to():
go = raw_input("Go to? ")
if go == Kitchen or breakfast:
print "You rumble down stairs and into the kitchen. Your mom has left some microwaved waffles on the table for you. Your big boy step sits by the counter."
elif go == "back to bed" or "back to sleep" or bed or sleep:
print "You hit snooze and roll over."
elif go == "bathroom" or "toilet" or "potty" or "pee" or "poop" or "take …Run Code Online (Sandbox Code Playgroud) 我正在用这个简单的代码创建一个字典:
pixel_histogram = {}
min_value = -0.2
max_value = 0.2
interval_size = (math.fabs(min_value) + math.fabs(max_value))/bins
for i in range(bins):
key = min_value+(i*interval_size)
print key
pixel_histogram[key] = 0
print pixel_histogram
Run Code Online (Sandbox Code Playgroud)
但我有点惊讶,因为我的印刷品得到了这些值:
#Printing keys
-0.2
-0.16
-0.12
-0.08
-0.04
0.0
0.04
0.08
0.12
0.16
#Printing the dictionary
{0.0: 0,
-0.08000000000000002: 0,
0.15999999999999998: 0,
-0.16: 0,
0.12: 0,
-0.12000000000000001: 0,
0.08000000000000002: 0,
-0.04000000000000001: 0,
-0.2: 0,
0.03999999999999998: 0}
Run Code Online (Sandbox Code Playgroud)
我没弄清楚为什么值不同,我怎么能解决这个问题.任何帮助,将不胜感激.谢谢.
python ×8
django ×3
django-orm ×2
apache-spark ×1
class ×1
dictionary ×1
django-admin ×1
key ×1
list ×1
numbers ×1
path ×1
python-3.x ×1
racket ×1
scala ×1
sqlite ×1
tastypie ×1
unit-testing ×1
windows ×1