小编het*_*sch的帖子

正常关闭asyncio协同程序

我目前在关闭应用程序的CTRL-C期间关闭asyncio协同程序时遇到问题.以下代码是我现在所拥有的精简版:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import asyncio
import time
import functools
import signal


class DummyProtocol(asyncio.Protocol):

    def __init__(self, *args, **kwargs):
        self._shutdown = asyncio.Event()
        self._response = asyncio.Queue(maxsize=1)
        super().__init__(*args, **kwargs)

    def connection_made(self, transport):
        self.transport = transport

    def close(self):
        print("Closing protocol")
        self._shutdown.set()

    def data_received(self, data):

        #data = b'OK MPD '

        # Start listening for commands after a successful handshake
        if data.startswith(b'OK MPD '):
            print("Ready for sending commands")
            self._proxy_task = asyncio.ensure_future(self._send_commands())
            return

        # saving response for later consumption in self._send_commands
        self._response.put_nowait(data)

    async …
Run Code Online (Sandbox Code Playgroud)

python python-asyncio

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

Three.js - 如何检查对象是否对相机可见

我很难弄清楚什么是检查相机眼睛是否可以看到Object3d的最佳方法.

我在屏幕中间有一个球体.一些立方体随机添加在其表面上.我需要的是一种方法来检查哪些立方体是可见的(在球体的前半部分),哪一个是不可见的(在球体的后半部分)用于相机的眼睛.

到目前为止我发现的似乎是正确的方向 - 但我必须错过THREE.Raytracer类的东西.

这是我正在使用的代码的小提琴:jsfiddle.我试图让它尽可能清楚.

这部分小提琴可能包含错误的代码:

var raycaster = new THREE.Raycaster();
var origin = camera.position, direction, intersects, rayGeometry = new THREE.Geometry(), g;
pointGroup.children.forEach(function(pointMesh) {
    direction = pointMesh.position.clone();
    // I THINK THIS CALCULATION MIGHT BE WRONG - BUT DON'T KNOW HOW TO CORRECT IT
    raycaster.set(origin, direction.sub(origin).normalize());
    // if the pointMesh's position is on the back half of the globe, the ray should intersect with globe first and the hit the point as second target - because the cube …
Run Code Online (Sandbox Code Playgroud)

javascript three.js

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

从4个角落颜色内插的二维颜色渐变(256x256矩阵)

我想要实现的是以编程方式创建由256x256颜色值矩阵表示的二维颜色渐变.预期结果可以在附图中看到.我的起点是矩阵的4个角色,其间应该插入剩余的254种颜色.虽然我在插入一个轴的颜色方面取得了一些成功,但二维计算却给我带来了一些不好的麻烦.虽然图像似乎具有非线性颜色渐变,但我会对线性渐变感到满意.

如果你能给我一些提示,如何用numpy或其他工具做到这一点,我将非常感激.

在此输入图像描述

python numpy image-processing

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

基于一些给定的参考元素插值颜色矩阵的元素

这或多或少是对从4角色插值的二维颜色渐变(256x256矩阵)的后续问题,今天jadsq深刻回答了这个问题.

对于线性渐变,前面的答案非常有效.但是,如果想要更好地控制渐变的停止颜色,这种方法似乎不太实用.在这种情况下可能有用的是在矩阵(查找表)中有一些参考色点,用于插入查找表中空位置的颜色值.我的意思可能更容易从下面的图像中读出来.

在此输入图像描述

整个想法来自http://cartography.oregonstate.edu/pdf/2006_JennyHurni_SwissStyleShading.pdf第4页到第6页.我已经阅读了论文,理论上我理解发生了什么但由于我的经验不足而惨遭失败插值方法,说实话,一般数学技能.可能还有兴趣的是,他们使用S形高斯钟作为插值方法(第6页).他们认为高斯加权产生了视觉上最好的结果并且计算简单(方程1,对于每256个单元256个表,k = 0.0002).


编辑(更好的插图):

用于插值颜色的加权函数

公式1


我已经将其呈现方法的其他部分放在适当位置,但填充矩阵中的空值确实是一个关键部分,并使我无法继续.再一次,谢谢你的帮助!

我现在拥有的:

#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt 

# the matrix with the reference color elements
ref=np.full([7, 7, 3], [255,255,255], dtype=np.uint8)
ref[0][6] = (239,238,185)
ref[1][1] = (120,131,125)
ref[4][6] = (184,191,171)
ref[6][2] = (150,168,158)
ref[6][5] = (166,180,166)

# s = ref.shape
#
# from scipy.ndimage.interpolation import zoom
# zooming as in https://stackoverflow.com/a/39485650/1230358 doesn't seem to work here anymore, because we have no corner point as reference but …
Run Code Online (Sandbox Code Playgroud)

python numpy image-processing matrix scipy

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

python混合单和双unicode转义序列

我有一些奇怪的逃脱unicode字符串的问题.我的脚本通过请求库使用web服务,response.text包含以下unicode字符串:

 u'\\u003c? abc ?\\u003eDas Modell des Adaptiven Zyklus wurde aus vergleichenden Untersuchungen zur Dynamik von \xd6kosystemen abgeleitet.\\u003c? /abc ?\\u003e'

 **Updated** Martijn solution works with the upper one, but breaks with this one because of len="12"
 u'\\u003c?abc len="12"?\\u003eResilienz sollte als st\xe4ndiger Anpassungsprozess zwischen Systemen und der Umwelt begriffen werden.\\u003c? /abc ?\\u003e'
Run Code Online (Sandbox Code Playgroud)

服务器的响应如下所示:

\u003c? abc ?\u003eDas Modell des Adaptiven Zyklus wurde aus vergleichenden Untersuchungen zur Dynamik von Ökosystemen abgeleitet.\u003c?dpf /sent ?\u003e
Run Code Online (Sandbox Code Playgroud)

问题是双转义的unicode序列,如\ u003c,\ u003c通常代表<char.\ xd6是正确的,代表德国Ö.这个双重逃逸完全搞砸了我的unicode字符串:-)

我在这篇文章中发现了一个类似的问题: Stack Overflow - 在python中转换字符串如\ uXXXX

使用string.decode('unicode-escape')的解决方案似乎只有在所有unicode序列都被转义但没有混合的单转义和双转义时才能工作.用单个替换双重转义会给我一个损坏的unicode字符串. …

python unicode

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

从里到外迭代嵌套列表

我有以下python嵌套列表结构:

test = ['a', ['c', ['e'], 'd'], 'b']
Run Code Online (Sandbox Code Playgroud)

或者相同,只是格式化:

test = [
    'a', 
        [
            'c', 
                [
                    'e'
                ], 
             'd'
        ], 
    'b'
]
Run Code Online (Sandbox Code Playgroud)

我想知道迭代完整列表的最佳方法是什么,从最里面的嵌套列表对象('e')到最外面的列表('a',[...],'b')以相反的顺序开始.对反向(测试)的调用只是没有嵌套列表的技巧.它应该能够在迭代的每个深度上调用回调函数.

迭代应该看起来像这样([xx] ==来自先前调用的回调的计算值):

1st e --> callback(e)
2nd c [e] d --> callback(c [e] d)
3rd a [c e d] b --> callback(a [c e d] b)
Run Code Online (Sandbox Code Playgroud)

希望这能解释我的问题并感谢您的帮助

python python-3.x

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