小编bal*_*lki的帖子

如何编写一个桌面应用程序,它使用HTML和CSS作为用户界面,python/perl/c ++/java进行处理?

不同的语言有不同的GUI工具包,但是看起来很难实现有吸引力的用户界面,就像我们可以用更少的努力使用HTML和CSS一样好.我没有在javascript中执行我的应用程序,因为我怀疑它是否具有所有必需的库并且我想在python中执行它.

如何在python中使用gecko,webkit等渲染引擎?哪一个更适合使用python?.是否有任何'只有html/css'渲染引擎没有javascript?

是否很容易为DOM事件编写事件处理程序并在python中操作DOM?

html javascript css python user-interface

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

如何在不分支子shell的情况下获得shell函数的输出?

我有以下功能.

hello () {
        echo "Hello"
}
func () {
        hello
        echo "world"
}
Run Code Online (Sandbox Code Playgroud)

如果我不希望打印hello函数的输出但想要对它做一些事情,我想在某个变量中捕获输出,唯一可能的方法是分叉子shell,如下所示?这不是一个不必要的新子进程创建吗?这可以优化吗?

func () {
        local Var=$(hello)
        echo "${Var/e/E} world"
}
Run Code Online (Sandbox Code Playgroud)

bash shell zsh

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

检查空状态的python生成器

python生成器是列表的良好替代品,在大多数情况下,我希望检查空条件,这是普通生成器无法实现的.我正在尝试编写一个包装器,它将允许检查空状态,但仍然是懒惰的并且提供了生成器的好处.

class mygen:
  def __init__(self,iterable):
    self.iterable = (x for x in iterable)
    self.peeked = False
    self.peek = None
  def __iter__(self):
    if self.peeked:
      yield self.peek
      self.peeked = False
    for val in self.iterable:
      if self.peeked:
        yield self.peek
        self.peeked = False
      yield val
    if self.peeked:
      yield self.peek
      self.peeked = False
  def __nonzero__(self):
    if self.peeked:
      return True
    try:
      self.peek = self.iterable.next()
      self.peeked = True
      return True
    except:
      return False
Run Code Online (Sandbox Code Playgroud)
  1. 我认为它的行为像普通的发电机一样.有什么角落我不见了吗?
  2. 这看起来并不优雅.是否有更好的更多pythonic方式做同样的事情?

样品用法:

def get_odd(l):
    return mygen(x for x in l if x%2)

def print_odd(odd_nums): …
Run Code Online (Sandbox Code Playgroud)

python generator

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

std :: function的性能与原始函数指针和void*相比较?

图书馆代码:

class Resource 
{
public:
    typedef void (*func_sig)(int, char, double, void*);
//Registration
    registerCallback(void* app_obj, func_sig func)
    {
        _app_obj = app_obj;
        _func = func;
    }

//Calling when the time comes
    void call_app_code()
    {
        _func(231,'a',432.4234,app_obj);
    }
//Other useful methods
private:
    void* app_obj;
    func_sig _func;
//Other members
};
Run Code Online (Sandbox Code Playgroud)

申请代码:

class App
{
public:
    void callme(int, char, double);
//other functions, members;
};

void callHelper(int i, char c, double d, void* app_obj)
{
    static_cast<App*>(app_obj)->callme(i,c,d);
}

int main()
{
    App a;
    Resource r;
    r.registercallback(&a, callHelper);
//Do …
Run Code Online (Sandbox Code Playgroud)

c++ boost-function c++11 std-function

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

Eclipse无法从java 8流中推断出正确的类型

以下代码适用于所有在线java编译器,但eclipse会抛出编译器错误.它是日食中的一个错误还是我错过了一些设置?一个简单的解决方法来沉默eclipse?在线:https://ideone.com/l0bbhz.注意:这是一个简化的简化示例,仅指出问题.flatMap在这种情况下,我理解没有必要.在实际情况下,我真的需要flatMap

package dummy;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static java.util.stream.Collectors.toList;

public class LearnJava {

    public static void main(String[] args) {
        String[] sa = {"ne", "two", "three"};
        List<String> l = Arrays.stream(sa)
                .flatMap(s -> Collections.singleton(s).stream().map(c -> c.toUpperCase()))
                .collect(toList());
        System.out.println(l.get(0));
    }

}
Run Code Online (Sandbox Code Playgroud)

eclipse控制台出错.

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Type mismatch: cannot convert from List<Object> to List<String>

    at dummy.LearnJava.main(LearnJava.java:13)
Run Code Online (Sandbox Code Playgroud)

我的日食版:

面向Web开发人员的Eclipse Java EE IDE.

版本:Luna Service Release 2(4.4.2)Build id:20150219-0600


更新 …

java eclipse generics java-8 java-stream

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

flatMap是否保证是懒惰的?

请考虑以下代码:

urls.stream()
    .flatMap(url -> fetchDataFromInternet(url).stream())
    .filter(...)
    .findFirst()
    .get();
Run Code Online (Sandbox Code Playgroud)

fetchDataFromInternet当第一个足够的时候会被叫第二个网址吗?

我尝试了一个较小的例子,看起来像预期的那样工作.即逐个处理数据但是可以依赖这种行为吗?如果没有,请.sequential().flatMap(...)帮助前打电话吗?

    Stream.of("one", "two", "three")
            .flatMap(num -> {
                System.out.println("Processing " + num);
                // return FetchFromInternetForNum(num).data().stream();
                return Stream.of(num);
            })
            .peek(num -> System.out.println("Peek before filter: "+ num))
            .filter(num -> num.length() > 0)
            .peek(num -> System.out.println("Peek after filter: "+ num))
            .forEach(num -> {
                System.out.println("Done " + num);
            });
Run Code Online (Sandbox Code Playgroud)

输出:

Processing one
Peek before filter: one
Peek after filter: one
Done one
Processing two
Peek before filter: two
Peek after filter: …
Run Code Online (Sandbox Code Playgroud)

java java-8 java-stream flatmap

11
推荐指数
2
解决办法
1304
查看次数

std :: launder alternative pre c ++ 17

它就像std::optional,但不存储额外的布尔.用户必须确保仅在初始化后访问.

template<class T>
union FakeOptional { //Could be a normal struct in which case will need std::aligned storage object.
    FakeOptional(){}  //Does not construct T
    template<class... Args>
    void emplace(Args&&... args){
        new(&t) T{std::forward<Args&&>(args)...};
    }
    void reset(){
        t.~T();
    }
    operator bool() const {
        return true;
    }
    constexpr const T* operator->() const {
        return std::launder(&t);

    }
    constexpr T* operator->() {
        return std::launder(&t);
    }
    T t;
};
Run Code Online (Sandbox Code Playgroud)

如果您想知道为什么我需要这样一个模糊的数据结构,请点击此处:https://gitlab.com/balki/linkedlist/tree/master

  1. 可以忽略std::launder吗?我猜不会.
  2. 由于std::launder仅在c ++ 17中可用,如何在c ++ 14中实现上面的类?boost::optional …

c++ language-lawyer c++14 c++17 stdlaunder

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

什么时候应该使用std :: async同步作为策略?

std :: async有一个重载,它将std :: launch策略作为第一个参数.我什么时候应该使用这个过载?有哪些不同的政策?(我认为同步和异步是两个选项).我应该何时使用同步政策?与直接运行有什么不同?

c++ asynchronous c++11

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

在boost python中公开C++接口

示例代码说明:

struct Base
{
  virtual int foo() = 0;
};

struct Derived : public Base
{
  virtual int foo()
  {
    return 42;
  }
};

Base* get_base()
{
  return new Derived;
}

BOOST_PYTHON_MODULE(libTestMod)
{
  py::class_<Base>("Base", py::no_init)
    .def("foo", py::pure_virtual(&Base::foo));

  py::def("get_base", get_base, py::return_internal_reference<>()); //ignore mem leak
}
Run Code Online (Sandbox Code Playgroud)
  • Base :: foo不会在python中被覆盖
  • Base:foo将在c ++中实现,但不应该暴露给python

尝试上面的代码但无法编译.

更新: 编译错误:

/path/to/boostlib/boost/1.53.0-0/common/include/boost/python/object/value_holder.hpp:66:11: error: cannot declare field 'boost_1_53_0::python::objects::value_holder<Base>::m_held' to be of abstract type 'Base'
Main.C:59:8: note:   because the following virtual functions are pure within 'Base':
Main.C:61:15: note:         virtual int Base::foo()
Run Code Online (Sandbox Code Playgroud)

c++ python boost-python

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

使用冷冻套装作为Dict钥匙是否安全?

它显然有效,但有两种相同的元素碰巧在Dict中添加两个条目的情况?我想我之前得到了这个条件并将我的代码更改frozenset(...)tuple(sorted(frozenset(...))).知道Dict和冻结集实现如何确认是否需要的人可以吗?

python hashmap python-2.7 frozenset

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