Decorator for overloading in Python

I know it's not Pythonic to write functions that care about the type of the arguments, but there are cases when it's simply impossible to ignore types because they are handled differently. Having a bunch of isinstance checks in your function is just ugly; is there any function decorator available that enables function overloads? Something like this: @overload(str) def func(val):

装饰器在Python中重载

我知道编写关注参数类型的函数并不是Pythonic,但有些情况下,由于它们的处理方式不同,因此不可能忽略类型。 在你的函数中进行一堆isinstance检查只是丑陋的; 有没有可用的函数装饰器来启用函数重载? 像这样的东西: @overload(str) def func(val): print('This is a string') @overload(int) def func(val): print('This is an int') 更新: 以下是我在David Zaslavsky的回答中留下的一些评论: 通过一些

How to you check the status or kill an external process with python

I have a python script that runs on my web server. The main function is called then when it returns it just sleeps for a few seconds and gets called again. It's purpose is to pick up any new uploaded videos that users have added and convert them to webm, pull out the middle frame as an image and a bunch of other funky stuff. I am using an external call to ffmpeg. The code clip below shows

如何检查状态或用python杀死一个外部进程

我有一个运行在我的web服务器上的python脚本。 主函数被调用,然后当它返回它只是睡几秒钟,再次被调用。 目的是获取用户添加的所有新上传的视频并将其转换为webm,将中间框架作为图像和其他一些时髦的东西拉出。 我正在使用ffmpeg的外部呼叫。 下面的代码片段显示了我如何称它。 duration = output[durationIndex+10:durationIndex+18] durationBits = duration.split(":") lengthInSeconds = (int(durationBit

support' on a Mac. Where can I find it?

If I am on a debian based system and using python and I want to update some modules, there is a feature of python called 'python-support'. I can install it by sudo apt-get install python-support I can't manage to find it anywhere for mac, and I have attempted to install it using mac ports via the ... sudo port install python-support ... command. I can't seem to find it anywh

支持'在Mac上。 我在哪里可以找到它?

如果我在一个基于debian的系统上并且使用python并且我想更新一些模块,那么python的一个特性叫做'python-support'。 我可以通过安装它 sudo apt-get install python-support 我不能设法找到它的任何地方的Mac,我已经试图通过使用Mac端口安装它... sudo port install python-support ...命令。 我似乎无法在网络上的任何地方找到它。 在Debian中,我试图做的事情如下所示: update-python-modules <module-na

Django form is not valid with django

I'm building a Django application in Google's cloud and I'm attempting to use django-filetransfers to upload video data. forms.py: class MontageCreateForm(forms.Form): title = forms.CharField(label='Montage title', max_length=1000, widget=forms.TextInput(attrs={'placeholder': 'Montage Name',

Django表单对django无效

我正在Google云中构建一个Django应用程序,我试图使用django-filetransfers来上传视频数据。 forms.py: class MontageCreateForm(forms.Form): title = forms.CharField(label='Montage title', max_length=1000, widget=forms.TextInput(attrs={'placeholder': 'Montage Name', 'class': 'form-control',

cant pass parameter in HttpResponseRedirect

I have read that after successfully dealing with post data, you should use HttpResponseRedirect to redirect to another page. I am building a URL shortener for learning purposes, the code from views.py is shown below: (Not working) def makeurl(request): # get url from form post_url = request.POST['url'] # shorten the url and have the short code returned shortened_url = shorten_ur

无法在HttpResponseRedirect中传递参数

在成功处理发布数据后,我已经阅读过,您应该使用HttpResponseRedirect重定向到另一个页面。 我正在构建一个用于学习的URL缩写器,来自views.py的代码如下所示:(不工作) def makeurl(request): # get url from form post_url = request.POST['url'] # shorten the url and have the short code returned shortened_url = shorten_url(post_url) return HttpResponseRedirect('create') def create(

How force requests post xml to django's request.FILES but not request.POST?

I'm using requests to post xml file to django 1.7b4, seems requests would try convert file content to string, if convert successfully then post it to request.POST, or failed to requist.FILES. but, how force it always post to request.FILES? Here is my code: f = open(full_name, 'rb') files = {'file': f} res = requests.post(url, files=files, data={'filename':filename}) f.

如何强制请求xml后django的request.FILES但不request.POST?

我正在使用请求将xml文件发布到django 1.7b4,似乎请求会尝试将文件内容转换为字符串,如果转换成功,然后将其发布到request.POST或未能requist.FILES。 但是, 如何强制它总是发布request.FILES? 这是我的代码: f = open(full_name, 'rb') files = {'file': f} res = requests.post(url, files=files, data={'filename':filename}) f.close() 和难看的功能: if 'file' in request.FILES: log_t

Django FileField not validating with a SimpleUploadedFile Object

I am uploading a file via ajax, and then creating a SimpleUploadFile object and passing it to replace request.FILES -- this does not pass form.is_valid(). I've logged the SimpleUploadedFile dictionary and request.FILES replacement below; as you can see, the SimpleUploadedFile object is able to be created: SimpleUploadedFile [This does not work.]: {'book_pics': <SimpleUploadedFile: movie

Django FileField不用SimpleUploadedFile对象进行验证

我通过ajax上传文件,然后创建一个SimpleUploadFile对象并传递它来替换request.FILES - 这不会传递form.is_valid()。 我已经记录下SimpleUploadedFile字典和request.FILES替换; 正如你所看到的,SimpleUploadedFile对象能够被创建: SimpleUploadedFile [This does not work.]: {'book_pics': <SimpleUploadedFile: movies.jpg (text/plain)>} # Yielded by printing uploaded_file 当我不使用ajax并简单地使用表单

How does the Django csrf token work?

Im not clear on the csrf token using Django forms. I have this in my form submit and I see it generated dynamically. If capture my session with fiddler and try to submit my form without that token I get a 403 error. But what I don't understand is I can use fiddler to submit as much data as I want with that same token, so I don't understand the security this token does. If someone hack

Django csrf令牌如何工作?

我不清楚使用Django表单的csrf标记。 我在表单提交中有这个,我看到它是动态生成的。 如果捕捉我的会议与提琴手,并尝试提交我的表单没有该令牌,我得到一个403错误。 但我不明白的是,我可以使用提琴手来提交尽可能多的数据,因为我需要使用相同的标记,所以我不明白这个标记的安全性。 如果有人盗用你的表格,他们可以使用相同的标记。 我是否缺少一些额外步骤以确保令牌始终是唯一的? 然后你的应用程序准备好表单,D

How to set class name in table cell in python django?

I am learning django. I have a simple model named customer. Here is my model: class Year(models.Model): year = models.CharField(max_length=255) created_at = models.DateTimeField(auto_now=False, auto_now_add=True) updated_at = models.DateTimeField(auto_now=True, auto_now_add=False) def __unicode__(self): return self.year def __str__(self): return self.year

如何在python django的表格单元格中设置类名称?

我正在学习Django。 我有一个名为customer的简单模型。 这是我的模特: class Year(models.Model): year = models.CharField(max_length=255) created_at = models.DateTimeField(auto_now=False, auto_now_add=True) updated_at = models.DateTimeField(auto_now=True, auto_now_add=False) def __unicode__(self): return self.year def __str__(self): return self.year class Cus

CSRF token just fires randomly in django

I have a form in my html: <form id="form" action="" method="post"> {% csrf_token %} <div>{{form.input1}}</div> <div>{{form.input2}}</div> <div>{{form.input3}}</div> <input type="submit" class="btn" name="submit" value="submit"> </form> and in my urls.py: urlpatterns = [ url(r'^$', views.MyView.as_view(success_url='/'), name=

CSRF令牌只是在Django中随机激发

我在我的html中有一个表单: <form id="form" action="" method="post"> {% csrf_token %} <div>{{form.input1}}</div> <div>{{form.input2}}</div> <div>{{form.input3}}</div> <input type="submit" class="btn" name="submit" value="submit"> </form> 并在我的urls.py: urlpatterns = [ url(r'^$', views.MyView.as_view(success_url='/'), name=