问题列表 - 第19824页

用Python重新打开文件?

说我有这个简单的python脚本:

file = open('C:\\some_text.txt')
print file.readlines()
print file.readlines()
Run Code Online (Sandbox Code Playgroud)

运行时,第一个打印件打印包含文件文本的列表,而第二个打印件打印空白列表.我想这并不完全出乎意料.但有没有办法"回卷"文件,以便我可以再次阅读?或者是重新打开它的最快方式?

python file

34
推荐指数
1
解决办法
4万
查看次数

限制django应用程序模型中的单个记录?

我想用一个模型来保存django应用程序的系统设置,所以我想限制模型只能有一条记录,如何做限制?

python django

3
推荐指数
4
解决办法
5507
查看次数

解析文本中的特殊字符串(例如"%var%")?

我正在提供带命令的Javascript函数字符串(SVG路径命令):

eg. "move 10 10 line 50 50"
Run Code Online (Sandbox Code Playgroud)

move和line命令

数字x,y坐标

我想为这些命令添加特殊字符串,这将指示函数使用特定变量

eg. "move %mouseX%+1 %mouseY%+1"
Run Code Online (Sandbox Code Playgroud)

其中%mouseX%和%mouseY%将是鼠标x,y坐标

我该如何解析和替换这些?

javascript

0
推荐指数
1
解决办法
117
查看次数

使用jquery跨浏览器右键单击插件?

我有一个上下文菜单插件,在右键单击元素时将显示上下文菜单.但这对ajax嵌入式元素不起作用.所以我必须使用ajax live,当它感觉到右键单击时触发上下文菜单功能.

是否有一个jquery检测右键点击的插件?

javascript ajax jquery

0
推荐指数
1
解决办法
1425
查看次数

Java:实例化没有默认构造函数的泛型类

我想这样做:

public class BaseTable<T extends TableEntry>

{

    protected int mRows;
    protected int mCols;
    protected ArrayList<T> mEntries;

    public BaseTable(int rows, int cols)
    {
        mRows = rows;
        mCols = cols;
        mEntries = new ArrayList<T>();
        for (int i = 0; i < rows; i++)
        {
            mEntries.add(new T(cols)); //this obv. doesn't work
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

实例化泛型很难实现,但更难以实现的是,T这里没有默认构造函数,它int在构造函数中需要一个参数.

如何才能做到这一点?


我也在这里问了一个跟进问题.如果你能回答这个问题,我将不胜感激.

这个问题是相关的,但只有在假定类具有默认构造函数时才有意义.

java generics instantiation default-constructor

4
推荐指数
1
解决办法
7854
查看次数

有没有比这更快的方法来查找目录和所有子目录中的所有文件?

我正在编写一个程序,需要在目录及其所有子目录中搜索具有特定扩展名的文件.这将在本地和网络驱动器上使用,因此性能有点问题.

这是我现在使用的递归方法:

private void GetFileList(string fileSearchPattern, string rootFolderPath, List<FileInfo> files)
{
    DirectoryInfo di = new DirectoryInfo(rootFolderPath);

    FileInfo[] fiArr = di.GetFiles(fileSearchPattern, SearchOption.TopDirectoryOnly);
    files.AddRange(fiArr);

    DirectoryInfo[] diArr = di.GetDirectories();

    foreach (DirectoryInfo info in diArr)
    {
        GetFileList(fileSearchPattern, info.FullName, files);
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以将SearchOption设置为AllDirectories而不使用递归方法,但将来我会插入一些代码来通知用户当前正在扫描的文件夹.

现在我正在创建一个FileInfo对象列表,我真正关心的是文件的路径.我将有一个现有的文件列表,我想将其与新的文件列表进行比较,以查看添加或删除了哪些文件.有没有更快的方法来生成这个文件路径列表?有什么办法可以优化这个文件搜索来查询共享网络驱动器上的文件吗?


更新1

我尝试创建一个非递归方法,通过首先查找所有子目录,然后迭代扫描每个目录中的文件来执行相同的操作.这是方法:

public static List<FileInfo> GetFileList(string fileSearchPattern, string rootFolderPath)
{
    DirectoryInfo rootDir = new DirectoryInfo(rootFolderPath);

    List<DirectoryInfo> dirList = new List<DirectoryInfo>(rootDir.GetDirectories("*", SearchOption.AllDirectories));
    dirList.Add(rootDir);

    List<FileInfo> fileList = new List<FileInfo>();

    foreach (DirectoryInfo dir in dirList)
    {
        fileList.AddRange(dir.GetFiles(fileSearchPattern, SearchOption.TopDirectoryOnly));
    }

    return fileList;
}
Run Code Online (Sandbox Code Playgroud)

更新2

好吧,所以我在本地和远程文件夹上运行了一些测试,这两个文件夹都有很多文件(~1200).以下是我运行测试的方法.结果如下. …

.net c# directory file-io

36
推荐指数
5
解决办法
5万
查看次数

如何通过委托查询方法属性?

我有一个自定义属性的方法.如果我有一个引用此方法的委托,我可以判断委托引用的方法是否具有该属性?

c#

6
推荐指数
1
解决办法
543
查看次数

以下C++代码是否相同?(在智能指针实现中)

代码1:

template<class T>
const PtrInterface<T>*
PtrInterface<T>::newRef() const {
  PtrInterface<T>* me = (PtrInterface<T>*) this;
  ++me->references_;
  //++this->references_;
  return this;
} 
Run Code Online (Sandbox Code Playgroud)

代码2:

template<class T>
const PtrInterface<T>*
PtrInterface<T>::newRef() const {
  //PtrInterface<T>* me = (PtrInterface<T>*) this;
  //++me->references_;
  ++this->references_;
  return this;
}
Run Code Online (Sandbox Code Playgroud)

是否有任何情况下这两个代码块会做不同的事情?谢谢!

c++ templates pointers

2
推荐指数
1
解决办法
120
查看次数

.NET:静态方法的推断泛型类型

假设我有

public static List<T2> Map<T,T2>(List<T> inputs, Func<T, T2> f)
{
    return inputs.ConvertAll((x) => f(x));
}

private int Square(int x) { return x*x; }

public void Run()
{
    var inputs = new List<Int32>(new int[]{2,4,8,16,32,64,128,256,512,1024,2048});

    // this does not compile
    var outputs = Map(inputs, Square); 

    // this is fine
    var outputs2 = Map<Int32,Int32>(inputs, Square);

    // this is also fine (thanks, Jason)
    var outputs2 = Map<Int32,Int32>(inputs, (x)=>x*x);

    // also fine
    var outputs2 = Map(inputs, (x)=>x*x);
}
Run Code Online (Sandbox Code Playgroud)

为什么不编译?

编辑:错误是:

错误CS0411:无法从用法推断出方法'Namespace.Map <T,T2>(System.Collections.Generic.List <T>,System.Func <T,T2>)'的类型参数.尝试显式指定类型参数. …

.net c# generics type-inference c#-3.0

15
推荐指数
1
解决办法
3458
查看次数

如何删除所有performSelector:withObject:afterDelay:?

我在运行EXC_BAD_ACCESS时遇到了一个对象,这个对象几乎没有performSelector:withObject:afterDelay选择器方法调用另一个对象而我正在释放该对象......

//我班上的某些地方,我正在打电话

[self performSelector:@selector(callObject1Function) withObject:nil afterDelay:2.0];
Run Code Online (Sandbox Code Playgroud)

我只是不明白我想当你dealloc的对象,与对象相关的一切都应该删除或取消,即使performSelector有延迟!有人可以解释一下,谢谢......

iphone exc-bad-access objective-c

16
推荐指数
2
解决办法
1万
查看次数