小编cla*_*ius的帖子

print .__ doc__ vs getattr(__ builtin __,"print").__ doc__

print.__doc__ 输出:

SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

在哪里

>>> getattr(__builtin__,"print").__doc__
Run Code Online (Sandbox Code Playgroud)

输出:

print(value, ..., sep=' ', end='\n', file=sys.stdout)
Run Code Online (Sandbox Code Playgroud)

将值打印到流,或sys.stdout默认情况下打印.可选关键字参数:

file:类文件对象(stream); 默认为当前sys.stdout.
sep:在值之间插入的字符串,默认为空格.
end:在最后一个值后附加的字符串,默认为换行符.

任何人都可以帮助我理解为什么print.__doc__给出语法错误而不是打印文档字符串

python python-2.x

10
推荐指数
2
解决办法
759
查看次数

dynamic_cast的VS的static_cast为void*

在最后两行以下程序,static_cast<void*>dynamic_cast<void *>表现不同.据我所知,的结果dynamic_cast<void*>总是解析为完整的对象的地址.所以它以某种方式使用RTTI.谁能解释一下编译器如何使用RTTI两者之间的区分.

#include <iostream>
using namespace std;
class Top {
protected:
int x;
public:
    Top(int n) { x = n; }
    virtual ~Top() {} 
    friend ostream& operator<<(ostream& os, const Top& t) {
        return os << t.x;
    }
};
class Left : virtual public Top {
protected:
    int y;
public:
    Left(int m, int n) : Top(m) { y = n; }
};
class Right : virtual public Top {
protected:
    int z;
public:
    Right(int m, …
Run Code Online (Sandbox Code Playgroud)

c++ dynamic-cast casting void-pointers

8
推荐指数
1
解决办法
741
查看次数

python检查是否调用了一个方法而不嘲笑它

class A():
    def tmp(self):
        print("hi")

def b(a):
    a.tmp()
Run Code Online (Sandbox Code Playgroud)

要检查是否在b中调用了tmp方法,推荐的方法是

a = A()
a.tmp = MagicMock()
b(a)
a.tmp.assert_called()
Run Code Online (Sandbox Code Playgroud)

但是这里的tmp被嘲笑了,并没有导致"hi"被打印出来。

我希望我的单元测试可以检查方法tmp是否被调用而不会模拟它。

这可能吗?

我知道在编写unitest时这不是标准的事情。但是我的用例(有点棘手)需要这样做。

python unit-testing mocking python-3.x magicmock

7
推荐指数
1
解决办法
1612
查看次数

py Spark 的内存分析

我一直在阅读有关如何分析我的火花簇的信息。注意:我正在使用 pyspark。

我已经能够集成 cProfiler 以获取驱动程序级别和每个 RDD 级别的时间指标。但 cProfile 只能帮助节省时间。

如何分析我的 Spark 应用程序(使用 py-spark 编写)的内存使用情况?

我有兴趣找到内存和时间瓶颈,以便我可以重新访问/重构该代码。

另外,有时当我将更改推送到生产时,会导致 OOM(在执行器处),并且我最终会被动地修复代码。我认为将它与一些内存分析器集成将帮助我在测试过程中检测到问题。

python out-of-memory memory-profiling apache-spark pyspark

5
推荐指数
0
解决办法
671
查看次数

C++ 右值shared_ptr 和右值weak_ptr

std::shared_ptr<std::string> test() {
  return std::make_shared<std::string>("sdsd");
}
Run Code Online (Sandbox Code Playgroud)
cout << *test() << endl;
Run Code Online (Sandbox Code Playgroud)

上面的代码有效。有人可以告诉我“sdsd”字符串存储在哪里吗?我原以为它会出错,因为右值是一个临时对象。右值复制并存储在哪里?

使用weak_ptr

std::weak_ptr<std::string> test() {
  return std::make_shared<std::string>("sdsd");
}
Run Code Online (Sandbox Code Playgroud)
  cout << *test().lock() << endl;
Run Code Online (Sandbox Code Playgroud)

有趣的是上面的代码出错了。有什么不同?

c++ weak-references shared-ptr

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

条件变量的谓词

我是多线程的新手.在使用条件变量在C++ 11中编写多线程代码时,我使用以下构造

while(predicate) {
    cond_var.wait(&lock);
}
Run Code Online (Sandbox Code Playgroud)

但是,我一直在阅读Deitel关于操作系统的第三版书(第6页),其中使用了以下结构

if(predicate) {
    cond_var.wait(&lock);
}
Run Code Online (Sandbox Code Playgroud)

那么,有什么区别?为什么这本书不能用呢?是不是虚假的称呼问题?

c++ concurrency multithreading condition-variable

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

面试问:给定m station和n house,输出每个房子最近的k个站点

有m个车站和n个房屋,每个车站和房屋的(x,y)坐标给出,每个房屋输出最近的车站.

后来,这个问题被推广到从每个房子找到最近的车站.

我的看法:对于每个房子,建立一堆距离(自下而上)到车站,然后弹出k.对所有房屋都这样做.为O(n*(M + klogm));

或者,对于每个房屋,为工作站构建订单统计树,然后查找具有等级的节点并遍历该节点下方的整个树.对所有房屋都这样做.为O(n*(mlogm + 10gm的+ k))的

还有更好的替代品吗?任何基于图DS的解决方案,哪个比这更好?

algorithm data-structures

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

通用堆栈类的异常安全代码

我正在尝试编写一个异常安全的通用堆栈.这就是我到目前为止所做的.

#include <iostream>
#include <memory>
#include <exception>

class stk_exception:public exception 
{
    virtual const char* what() const throw()
    {
        return "stack underflow";
    }
} stk_ex;

template <class T>
struct node
{
    T data;
    node<T> *next;
};

template <class T>
class stack_generic
{
public:
    stack_generic() : _head(nullptr) {
    }

    void push(T x) {
        node<T> *temp(new node<T>());
        temp->data = x;
        temp->next = _head;
        _head = temp;
    }            

    void pop() {
        if (_head == nullptr) {
            throw stk_ex;                
        } else {
            node<T> *temp = …
Run Code Online (Sandbox Code Playgroud)

c++ generics exception c++11

3
推荐指数
1
解决办法
417
查看次数

从模板中的迭代器推导出变量的类型,而不使用auto

在下面的代码中,我使用auto来定义一个与使用的迭代器兼容的新变量"temp".我只是好奇我是否可以不使用auto关键字做同样的事情?

换句话说,我不知道容器是什么.那么如何在不损害代码通用性的情况下定义临时变量呢?

作为C++的新手,我一直在使用SO作为我的网站来解决任何问题.我找了类似的问题,但我没有找到.任何帮助,将不胜感激!!

/* InsertionSort.c */
#include<iostream>
#include <typeinfo>


namespace sort {

template <class RandomAccessIterator, class Compare >
void InsertionSort(RandomAccessIterator begin, RandomAccessIterator end, Compare comp) {
    RandomAccessIterator itrOuter,itrInner,itrShift;

    for(itrOuter = begin+1; itrOuter!=(end); itrOuter++) {
     // ***************HERE***********************************************   
             auto temp= *itrOuter;

        for(itrInner=begin;itrInner<itrOuter;itrInner++) {
            if(!comp(*itrInner,*itrOuter)) {
                for(itrShift=itrOuter;itrShift> itrInner;itrShift--)
                    *itrShift=*(itrShift -1);
            *itrInner=temp;
            break;
            }       
        }
    }       
}
}

bool Compa(int& x, int& y) {return (x<y);}

int main(void) {
    using namespace sort;
    int arr[] = {3,5,1,7,2,6,9,12};
    InsertionSort(arr, arr+8, Compa );

    for(int i=0;i < 8;i++) 
        std::cout<<arr[i]<<" …
Run Code Online (Sandbox Code Playgroud)

c++ templates auto

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

C++ 11线程编译错误,删除了复制构造函数和std :: thread,为什么?

这会发出大约100行错误.错误:使用run_me复制构造函数的已删除函数等.那么,这里的问题是什么?

#include<thread>
#include<iostream>  
#include<vector>
#include<fstream>
#include<string>
#include<chrono>
using namespace std;
//creating thread to print line from file
class run_me {
    ifstream fs;
    public:
    string s;
        run_me(): fs("lliftOff.cpp",ifstream::in) {}
    void operator()(){
        cout<<"hi";
        while(getline(fs,s) ) {
            cout<<s<<endl;
        this_thread::sleep_for (chrono::seconds(1));
        }
    }
    ~run_me() {
        fs.close();
    }
};
int main() {
    thread t1{run_me()};
t1.join();
cout<<"shutting down now"<<endl;
}
Run Code Online (Sandbox Code Playgroud)

c++ c++11

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

嵌套 unique_ptr 和 stl 容器

我最近阅读了有关 RAII 的内容并开始使用它。我试图将图定义为邻接列表并使用unique_ptr. 我知道我可以将它们定义为堆栈对象,但我正在尝试使用unique_ptr's 来习惯它们。

我正在做以下事情

unique_ptr to vector  --
                       |-->(unique_ptr)list<edge>
                       |-->(unique_ptr)list<edge>
                       |-->(unique_ptr)list<edge>
                       |-->(unique_ptr)list<edge>
Run Code Online (Sandbox Code Playgroud)

代码:

#include<memory>
#include<vector>
#include<list>
using namespace std;

struct edge {
    int vertex;
    int weight;
};
int vertices = 10;
unique_ptr<vector<unique_ptr<list<struct edge > > > >
    adj_list(new vector<list<struct edge> >(10)); // THIS COMPILES

unique_ptr<vector<unique_ptr<list<struct edge > > > >
    adj_list(new vector<list<struct edge> >(10, new list<edge>())); //THIS GIVES COMPILATION ERROR
Run Code Online (Sandbox Code Playgroud)

谁能帮我纠正这个问题?

编辑:

我对向量是 RAII 类有疑问。如果我执行以下操作

vector<int> *v;
v = new vector<int> (10);
Run Code Online (Sandbox Code Playgroud)

我必须删除变量吗v …

c++ smart-pointers unique-ptr c++11

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