我有一个目录结构如下
/home/damon/dev/python/misc/path/
/project/mycode.py
/app/templates/
Run Code Online (Sandbox Code Playgroud)
我需要从mycode.py获取模板文件夹的绝对路径
我试着写mycode.py为
import os
if __name__=='__main__':
PRJ_FLDR=os.path.dirname(os.path.abspath(__file__))
print 'PRJ_FLDR=',PRJ_FLDR
apptemplates = os.path.join(PRJ_FLDR,'../app/templates')
print 'apptemplates=',apptemplates
Run Code Online (Sandbox Code Playgroud)
我希望apptemplates是
/home/damon/dev/python/misc/path/app/templates
Run Code Online (Sandbox Code Playgroud)
但我越来越
/home/damon/dev/python/misc/path/project/../app/templates
Run Code Online (Sandbox Code Playgroud)
如何获得正确的路径?
我在SO中发现了一个帖子,其中算法是用python代码实现的.这是本文中伪代码的直接实现.
但是,在伪代码中,有一行,其中count由数组'a'中剩余元素的数量递增.在上面的python代码中,它给出为
count += len(a) - i
Run Code Online (Sandbox Code Playgroud)
我们不能这样做
count += len(a[i:])
Run Code Online (Sandbox Code Playgroud)
而且,而不是
c += a[i:] + b[j:]
Run Code Online (Sandbox Code Playgroud)
我写,
c.append(a[i:])
c.append(b[j:])
Run Code Online (Sandbox Code Playgroud)
总的来说,我的版本看起来像这样
def merge(left,right):
i=0
j=0
count=0
c=[]
while i<len(left) and j<len(right):
c.append(min(left[i],right[j]))
if right[j]< left[i]:
count+=len(left[i:])
j+=1
else:
i+=1
c.append(left[i:])
c.append(right[j:])
return count,c
def dnc(input):
if len(input)==1:
return 0,input
mid=len(input)/2
left=input[:mid]
right=input[mid:]
l,left=dnc(left)
r,right=dnc(right)
m,result=merge(left,right)
count=l+r+m
return count,result
Run Code Online (Sandbox Code Playgroud)
唉!,当我在排序的数组上计算它时,我得到3而不是0
if __name__=='__main__':
input =[1,2,3,4,5]
ct,res=dnc(input)
print ct
Run Code Online (Sandbox Code Playgroud)
我做错了什么?有人可以帮我找出来吗?
当我创建我的第一个项目时,我创建了一个repo(比如说project1)github account,然后在我的机器上克隆它.然后在我的机器中的代码更新时推送到它.
现在我创建了另一个项目 - 比如project2我的机器.测试它,使用创建本地仓库git init.我想将这个项目推向github.为了这个目的,我创建了一个repo关于github命名project2.它现在有一个.gitignore和readme.md文件.
现在,如何将现有代码添加到此?当我cd到我的project2目录并尝试时git push,我收到此错误:
fatal: No destination configured to push to.
Run Code Online (Sandbox Code Playgroud)
我试过了 git push project2
fatal: 'project2' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
Run Code Online (Sandbox Code Playgroud)
请帮我纠正这个错误
我有一个(python) list of lists如下
biglist=[ ['1','123-456','hello','there'],['2','987-456','program'],['1','123-456','list','of','lists'] ]
Run Code Online (Sandbox Code Playgroud)
我需要以下面的格式得到它
biglist_modified=[ ['1','123-456','hello there'],['2','987-456','program'],['1','123-456','list of lists'] ]
Run Code Online (Sandbox Code Playgroud)
我需要third element onwards在每个内部列表中连接.我试图通过使用list comprehensions,
def modify_biglist(bigl):
ret =[]
for alist in bigl:
alist[2] = ' '.join(alist[2:])
del alist[3:]
ret.append(alist)
return ret
Run Code Online (Sandbox Code Playgroud)
这样做了..但它看起来有点复杂 - 有一个局部变量ret并使用del?有人可以提出更好的建议
对于我的django1.4应用程序,我正在尝试使用south来进行数据迁移.我正在使用postgres8.3
我有一个MyCategory模型,我需要添加一个名为creatordjango.contrib.auth.models.User 的字段
我修改了类并添加了字段
from django.contrib.auth.models import User
class MyCategory(models.Model):
name=models.CharField(unique=True,max_length=50)
description=models.TextField(blank=True)
creator = models.ForeignKey(User,null=True)#added
slug=models.SlugField(editable=False)
Run Code Online (Sandbox Code Playgroud)
我跑了 python manage.py schemamigration myapp --auto
输出+ Added field creator on myapp.MyCategory
并创建了一个文件0003_auto__add_field_mycategory_creator.py
然后我希望所有旧记录都将webapp的超级用户作为创建者.所以我尝试了数据迁移
我跑了 python manage.py datamigration myapp add_creator
这输出了一个文件add_creator.py,我在其中实现了如下的forwards方法.(后来我添加了一个print stmt用于调试目的)
def forwards(self, orm):
from django.contrib.auth.models import User
suser = User.objects.filter(is_superuser=True)[0]
# I also tried User.objects.get(is_superuser=True)
print 'suser=',suser,'of type=',type(suser)
for category in orm.MyCategory.objects.all():
category.creator = suser
category.save()
Run Code Online (Sandbox Code Playgroud)
然后我运行了migrate命令
python manage.py migrate myapp
这个输出一个 ValueError
Running migrations for …Run Code Online (Sandbox Code Playgroud) 在Page 112 of CHAPTER 5 GENERICS in the book - Effective Java,这些句子出现
原始类型
List和参数化类型之间的区别是什么List<Object>...虽然您可以将a传递List<String>给List类型的参数,但您无法将其传递给类型参数List<Object>
我试过这个
public static void getMeListOfObjs(List<Object> al){
System.out.println(al.get(0));
}
public static void main(String[] args) {
List<Object> al = new ArrayList<Object>();
String mys1 = "jon";
al.add(mys1);
getMeListOfObjs(al);
}
Run Code Online (Sandbox Code Playgroud)
它运行没有任何错误......这是书内容中的错误吗?我引用了第二版
我有一个名为BST的Java接口(二进制搜索树的简称),它具有泛型类型Key,Value,其中Key扩展为Comparable.我将其定义如下.
public interface BST<Key extends Comparable<Key>,Value> {
public void put(Key key,Value value);
public Value get(Key key);
public void delete(Key key);
public Iterable<Key> keys();
}
Run Code Online (Sandbox Code Playgroud)
现在我想定义上面接口的实现.我试过这个
public class BSTImpl<Key extends Comparable<Key> ,Value> implements BST<Key extends Comparable<Key>,Value> {
...
}
Run Code Online (Sandbox Code Playgroud)
上面的定义在eclipse IDE中导致错误信息. extends后面的令牌implements BST<Key似乎是罪魁祸首
令牌"extends"的语法错误,预期
如果我从定义中省略"extends"标记(如下所示),则错误消失,我可以通过eclipse正确生成未实现的方法
public class BSTImpl<Key extends Comparable<Key> ,Value> implements BST<Key ,Value> {
@Override
public void put(Key key, Value value) {
// TODO Auto-generated method stub
}
@Override
public Value get(Key key) {
// TODO Auto-generated …Run Code Online (Sandbox Code Playgroud)