我想在python中生成一个列表如下 -
[1, 1, 2, 4, 3, 9, 4, 16, 5, 25 .....]
Run Code Online (Sandbox Code Playgroud)
你会想通的,它只不过是 n, n*n
我尝试在python中编写这样的列表理解如下 -
lst_gen = [i, i*i for i in range(1, 10)]
Run Code Online (Sandbox Code Playgroud)
但这样做会产生语法错误.
通过列表理解生成上面列表的好方法是什么?
我编写了以下脚本来将目录中的所有文件连接成一个文件.
就此而言,这可以进行优化吗?
惯用的蟒蛇
时间
这是片段:
import time, glob
outfilename = 'all_' + str((int(time.time()))) + ".txt"
filenames = glob.glob('*.txt')
with open(outfilename, 'wb') as outfile:
for fname in filenames:
with open(fname, 'r') as readfile:
infile = readfile.read()
for line in infile:
outfile.write(line)
outfile.write("\n\n")
Run Code Online (Sandbox Code Playgroud) 鉴于我有两个变量{{ profile }}
,其值为"test",另一个{{ element.author }}
值为"test".在jinja2中,当我尝试使用if比较它们时,没有任何显示.我做的比较如下:
{% if profile == element.author %}
{{ profile }} and {{ element.author }} are same
{% else %}
{{ profile }} and {{ element.author }} are **not** same
{% endif %}
Run Code Online (Sandbox Code Playgroud)
我得到输出test and test are not same
什么是错的,我怎么比较?
我正在使用bootstrap 2.0版
我有以下html结构 -
现在当我点击Filter by Team
下拉菜单时显示正确.现在当我点击链接时,我应该被带到页面.但链接也不能正常工作.我的意思是,当我点击下拉元素时,他们应该把我带到一个网址,这是他们所反映的,这不会发生.
<li style="margin-left: 12px;">
<div class="dropdown" style="margin-top: 5px;">
<a class="dropdown-toggle" style="margin-left: -2px;" data-toggle="dropdown" href="#">
Filter by Team
</a>
<ul class="dropdown-menu" data-toggle="dropdown" role="menu" aria-labelledby="dropdownMenu">
<li>
<a tabindex="-1" class="disabled" href="/task/list/orgteam/8/">funvilla</a>
</li>
<li class="divider"></li>
<li>
<a tabindex="-1" class="disabled" href="/task/list/orgteam/6/">Dev Team</a>
</li>
<li class="divider"></li>
<li>
<a tabindex="-1" class="disabled" href="/task/list/orgteam/5/">Design Team</a>
</li>
<li class="divider"></li>
</ul>
</div>
</li>
Run Code Online (Sandbox Code Playgroud)
小提琴可以在这里找到 - http://jsfiddle.net/ktgrw/
我在一个独立的数据库服务器(ec2实例)上安装了redis.它已经正确安装和配置.现在,我想要做的就是从我的网络服务器,我连接到它,并对其键值存储进行更改.
我有一个在heroku上运行的python/django应用程序,我正在使用PostgreSQL用于其他一切,我使用redis只是为了在KV集中存储一些临时变量.
现在,我在本地服务器和网络服务器上安装https://github.com/andymccurdy/redis-py.
要测试连接并检查一切是否正常,我在我的环境中尝试以下操作:
>>> pool = redis.ConnectionPool(host='MY_DBSERVER_IP_ADDRESS', port=6379, db=0)
>>> r = redis.Redis(connection_pool=pool)
>>> r.set('foo', 'bar')
Run Code Online (Sandbox Code Playgroud)
这给了我一个错误 - ConnectionError: Error 111 connecting 54.235.xxx.xxx:6379. Connection refused.
我该如何连接?我错过了什么?
我试图从基于django类的视图做一个CRUD应用程序.这是我更新/创建笔记的观点.
class CreateNoteView(CreateView):
model = Note
template_name = 'edit_note.html'
def get_success_url(self):
return reverse('notes-list')
def get_context_data(self):
context = super(CreateNoteView, self).get_context_data(**kwargs)
context['action'] = reverse('notes-create')
return context
class UpdateNoteView(UpdateView):
model = Note
template_name = 'edit_note.html'
def get_success_url(self):
return reverse('notes-list')
def get_context_data(self, **kwargs):
context = super(UpdateNoteView, self).get_context_data(**kwargs)
context['action'] = reverse('notes-edit',
kwargs={'pk':self.get_object().id})
return context
Run Code Online (Sandbox Code Playgroud)
这是我的模型文件:
# Create your models here.
class Note(models.Model):
title = models.CharField(max_length=200)
description = models.TextField()
pub_date = models.DateField(auto_now_add=True)
Run Code Online (Sandbox Code Playgroud)
我的网址文件如下所示:
urlpatterns = patterns('',
url(r'^create/$',CreateNoteView.as_view(), name='notes-create',),
url(r'^$', ListNoteView.as_view(), name='notes-list',),
url(r'^edit/(?P<pk>\d+)/$', UpdateNoteView.as_view(), name='notes-edit',),
)
Run Code Online (Sandbox Code Playgroud)
模板如下: …
我对给定的对象有三个条件,1. to start,2. started和3. finished。我过滤视图本身中的对象并将三个变量发送到模板 -
tostart_objects
,started_objects
和finished_objects
。
现在我循环遍历 html 模板中的三个 for 循环,如下所示:
{% for obj in tostart_objects %}
// chunk of html template to display all the object
{% endfor %}
{% for obj in started_objects %}
// similar chunk of html template as above
{% endfor %}
{% for obj in finished_objects %}
// similar chunk of html template as above
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
我可以不在三个不同的地方重用它们而不是放置相同的代码块吗?如何才能做到这一点?请让我知道,谢谢!
我在python中创建了一个类,并__init__
在类方法中调用变量.很少有init变量会出错.
以下是班级:
class MyRedisClass(object):
def __init__(self):
self.DEFAULT_PAGE_SIZE = 10
# the line below gives an error - global name 'pool' is not defined
# if the below line is commented, I can get the value of DEFAULT_PAGE_SIZE inside the some_function
self.pool = redis.ConnectionPool(host='XXX.XXX.XX.XX', port=XXXX, db=0)
self.redis_connection = redis.Redis(connection_pool=pool)
def some_function(self, some_data):
print self.DEFAULT_PAGE_SIZE
pipeline = self.redis_connection.pipeline()
it = iter(some_data)
for member, score in zip(it, it):
pipeline.zadd(leaderboard_name, member, score)
pipeline.execute()
Run Code Online (Sandbox Code Playgroud)
在终端我创建一个类的实例如下 -
mklass = MyRedisClass()
mklass.some_function(['a', 1])
Run Code Online (Sandbox Code Playgroud)
正如指出我得到一个错误 - …
我一直在使用这个repo https://github.com/technomancy/emacs-starter-kit中的 emacs, 它帮助我轻松上手.但问题是,每当我尝试使用C-x k
它来杀死缓冲区时,都会从缓冲区中隐藏文件,但它仍然可用.当我这样做,C-x b
我可以再次看到它,从中选择.
它出什么问题了?
ispell是否必须对它做任何事情?但我已经安装了ispell使用brew install ispell
请让我知道如何解决这个问题.谢谢!
如下所示,显示Cx k绑定的内容,输出如下:
C-x k runs the command ido-kill-buffer, which is an interactive
compiled Lisp function in `ido.el'.
It is bound to C-x k.
(ido-kill-buffer)
Kill a buffer.
The buffer name is selected interactively by typing a substring.
For details of keybindings, see `ido-switch-buffer'.
Run Code Online (Sandbox Code Playgroud) python ×7
django ×3
redis ×2
amazon-ec2 ×1
copy ×1
django-forms ×1
emacs ×1
file ×1
flask ×1
heroku ×1
html ×1
javascript ×1
jinja2 ×1
list ×1
macos ×1