小编Muh*_*mad的帖子

Google Appengine NDB祖先vs关键查询

我将实体的密钥存储为另一个实体的属性以便将它们关联起来.我们在项目的这个阶段处于重构阶段,所以我在考虑引入祖先.这两种方法之间是否存在性能差异?如果我们介绍祖先,我可能获得的任何优势?

class Book(ndb.Model):
  ...

class Article(ndb.Model):
  book_key = ndb.KeyProperty(kind=Book, required=True)


book_key =  ndb.Key("Book", 12345)
Run Code Online (Sandbox Code Playgroud)

第一个祖先查询方法

qry = Article.query(ancestor=book_key)
Run Code Online (Sandbox Code Playgroud)

第二种简单的密钥查询方法

qry = Article.query(book_key=book_key)
Run Code Online (Sandbox Code Playgroud)

python google-app-engine app-engine-ndb

10
推荐指数
1
解决办法
4154
查看次数

计算嵌套列表中相等子列表的出现次数的更快方法是什么?

我有一个Python列表,我希望(尽可能快地:非常重要......)在每个子列表中附加它在嵌套列表中出现的时间.

我已经用一些pandas数据框完成了这个,但是这看起来非常慢,我需要非常大规模地运行这些行.我完全愿意牺牲好读的代码来实现高效的代码.

所以例如我的嵌套列表在这里:

l = [[1, 3, 2], [1, 3, 2] ,[1, 3, 5]]
Run Code Online (Sandbox Code Playgroud)

我需要:

res = [[1, 3, 2, 2], [1, 3, 5, 1]]
Run Code Online (Sandbox Code Playgroud)

编辑

订单res根本无关紧要.

python

9
推荐指数
2
解决办法
506
查看次数

C ++错误:抛出'std :: bad_alloc'实例后终止调用

我编写了下面粘贴的代码,以按声明的顺序执行以下任务:

  1. 读取输入文件并计算其中的条目数
  2. 创建一个适当大小的数组(大小等于条目数)
  3. 返回输入文件的开头并再次阅读
  4. 将条目存储在数组中
  5. 打印出文件中的条目数以及条目本身。

这是我的代码:

#include <iostream>
#include <fstream>
#include <exception>

using namespace std;

int main(int argc, char* argv[]){

    ifstream inFile(argv[1]); //passing arguments to the main function
    int numEntries;

    if(!inFile){
        cout << "file not found" << endl;
        return 1;
    }

    string entry;
    while (!inFile.eof()){ //counting the number of entries
        getline(inFile,entry);
        ++numEntries;
    }

    const int length = numEntries;  //making an array of appropriate length
    int*arr = new int[length];

    inFile.clear();             //going back to the beginning of the file
    inFile.seekg(0, ios::beg); …
Run Code Online (Sandbox Code Playgroud)

c++

7
推荐指数
2
解决办法
4万
查看次数

在发送 stdin 输入之前等待来自子进程的提示

我有一个 linux x86 二进制文件,它要求输入密码并打印出密码是正确还是不正确。我想使用 python 来模糊输入。

下面是我运行二进制文件的屏幕截图,然后给它字符串“asdf”,并收到字符串“不正确”

截屏:

截屏

到目前为止,我已经尝试使用 Python3 subprocess 模块来

  1. 将二进制文件作为子进程运行
  2. 收到密码提示
  3. 发送一个字符串。
  4. 收到回复

这是我的脚本

p = subprocess.Popen("/home/pj/Desktop/L1/lab1",stdin=subprocess.PIPE, stdout=subprocess.PIPE)
print (p.communicate()[0])
Run Code Online (Sandbox Code Playgroud)

运行这个脚本的结果是

b'Please supply the code: \nIncorrect\n'
Run Code Online (Sandbox Code Playgroud)

我希望只收到提示,但是在我有机会发送我的输入之前,二进制文件也返回了不正确的响应。

我如何改进我的脚本以便成功地与这个二进制文件交互?

python subprocess fuzzing

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

替换打开的 xml 元素中的内部文本?

我正在使用 open xml SDK 2.0,对此我很陌生。

我实际上在我的 word 2007 文档中创建了一个名为“hello.docx”的快速部分(包含内容控件)。现在我需要将快速部分复制到名为“hello.docx”的同一文档的其他位置。我非常感谢这篇文章http://www.techques.com/question/1-3448297/Replacing-Content-Controls-in-OpenXML,同样的事情也发布在堆栈溢出论坛上,我非常感谢: )...这篇文章只是删除了内容控件,但保留了内容控件中的内容。

在上面的链接的帮助下,我能够修改代码以克隆内容控件并附加到同一文档(我的代码的这一部分正在工作)。但我的内部文本有问题。虽然我替换了打开的 Xml 元素中的innerText,但它没有反映在文档中。

public static void AddingSdtBlock(string filename, string sdtBlockTag)
{
    using (WordprocessingDocument doc = WordprocessingDocument.Open(filename,true))
    {
        MainDocumentPart mainDocumentPart = doc.MainDocumentPart;
        List<SdtBlock> sdtList = mainDocumentPart.Document.Descendants<SdtBlock>().ToList();
        SdtBlock sdtA = null;

        foreach (SdtBlock sdt in sdtList)
        {
            if (sdt.SdtProperties.GetFirstChild<Tag>().Val.Value == sdtBlockTag)
            {
                sdtA = sdt;
                break;
            }
        }
        SdtBlock cloneSdkt = (SdtBlock)sdtA.Clone();



        OpenXmlElement sdtc = cloneSdkt.GetFirstChild<SdtContentBlock>();
      //  OpenXmlElement parent = cloneSdkt.Parent;

        OpenXmlElementList elements = cloneSdkt.ChildElements;

       // var mySdtc = new …
Run Code Online (Sandbox Code Playgroud)

controls openxml innertext openxml-sdk

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

使用Html Agility Pack在两个HTML标记之间获取内容

我们在Word中创建了一个绝对庞大的帮助文档,这个文档用于生成更大规模且非常复杂的HTM文档.使用C#和这个库,我想只在我的应用程序中的任何一点抓取并显示该文件的一部分.部分分为这样:

<!--logical section starts here -->
<div>
<h1><span style='mso-spacerun:yes'></span><a name="_Toc325456104">Section A</a></h1>
</div>
 <div> Lots of unnecessary markup for simple formatting... </div>
 .....
<!--logical section ends here -->

<div>
<h1><span style='mso-spacerun:yes'></span><a name="_Toc325456104">Section B</a></h1>
</div>
Run Code Online (Sandbox Code Playgroud)

从逻辑上讲,标签中H1有一个部分名称a.我想从外部包含div中选择所有内容,直到我遇到另一个h1并排除该div.

  • 每个部分名称都位于一个<a>标签下面,h1该标签有多个孩子(每个约6个)
  • 逻辑部分标有注释
  • 实际文档中不存在这些注释

我的尝试:

var startNode = helpDocument.DocumentNode.SelectSingleNode("//h1/a[contains(., '"+sectionName+"')]");
//go up one level from the a node to the h1 element
startNode=startNode.ParentNode;

//get the start index as the index of the div containing the h1 element …
Run Code Online (Sandbox Code Playgroud)

.net c# html-agility-pack

5
推荐指数
1
解决办法
5007
查看次数

默认情况下在Word Client中显示"查看窗格"

我想要的是:我正在编辑一个WordprocessingDocument,并在其中添加一些跟踪的更改.这部分完成了.现在,我希望MS word 默认显示所有修订版,即,它不应要求用户单击红色侧栏以打开文档中的跟踪更改.

红边吧

我做了什么:对于这一点,我发现了一类RevisionView,它增加了XML元素<w:revisionView />settings.xmlw:settings元素.该RevisionView有一个像一些属性Comments,DisplayRevision,Formatting等我明确它们都设置为true.

RevisionView revView = new RevisionView();
revView.DisplayRevision = new OnOffValue(true);
revView.Formatting = new OnOffValue(true);
revView.InkAnnotations = new OnOffValue(true);
revView.Markup = new OnOffValue(true);
revView.Comments = new OnOffValue(true);
Run Code Online (Sandbox Code Playgroud)

然后我把它添加revViewSettings:

Settings settings = wordprocessingDocument.MainDocumentPart.DocumentSettingsPart.Settings;
settings.RemoveAllChildren<RevisionView>();
settings.AppendChild(revView);
settings.Save();
Run Code Online (Sandbox Code Playgroud)

然后我明确地检查了文档xml,它在以下内容中添加了以下xml settings:

<w:revisionView w:markup="true" w:comments="true" w:insDel="true" w:formatting="true" w:inkAnnotations="true" />
Run Code Online (Sandbox Code Playgroud)

但是在设置中添加此元素不会影响视图.它没有显示默认打开的修订版.

然后,出于测试目的,我将手中的zoom …

c# openxml openxml-sdk wordprocessingml

5
推荐指数
1
解决办法
478
查看次数

使用用户插入的元素填充数组

大家早上好.我试图用一个用户输入的元素填充一个数组; 问题是,虽然我已经在互联网上找到了答案,但它说我的代码应该可行,但事实并非如此.具体来说,计算机说我试图运行后程序停止工作,说实话,我真的不明白我犯了什么错误.代码如下:

#include <iostream>
using namespace std;

void getarray (int[],int);
void print (int[],int);

int main(){
    const int size=10;
    int n[]={0};
    getarray (n,size);
    print (n,size);
}

void getarray (int n[],const int size){
    cout<<"Insert elements to fill the array:\n";
    for (int i=0;i<size;i++){
        cin>>n[i];
    }
    cout<<"Filling completed.\n";
}
void print (int n[],const int size){
    cout<<"The inserted array is:\n";
    for (int k=0; k<size; k++)
        cout<<n[k]<<" ";
}
Run Code Online (Sandbox Code Playgroud)

仅用于获取阵列并打印它.

c++ arrays

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