小编Cha*_*eon的帖子

如何在Django中加入懒惰翻译?

我需要使用懒惰翻译但我还需要翻译 - 如何处理?

这段代码正在做我需要的:

print ugettext_lazy('Hello world!')
Run Code Online (Sandbox Code Playgroud)

现在我想加入两个懒惰的翻译并单独翻译(我现在不会工作,为什么但是想要有两个翻译字符串).

print ugettext_lazy('Hello world!') + ' ' + ugettext_lazy('Have a fun!')
Run Code Online (Sandbox Code Playgroud)

我可以做这样的代码,但它产生的翻译比需要的多.

print ugettext_lazy('Hello world! Have a fun!')
Run Code Online (Sandbox Code Playgroud)

是否有可能有两个翻译字符串和延迟翻译?

python django translation django-i18n

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

如何强制 Google 云端点仅在整个 API 或特定方法上使用 https?

我想确保端点 API 仅通过https提供信息:

  1. 如何强制 API仅在整个 API上使用https
  2. 如何强制特定方法仅使用https

我应该在 Google App Engine Java 或 Python27 中包含什么样的代码?

google-app-engine google-cloud-endpoints

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

如何使用反射或解析读取Python类中声明成员的顺序(禁止元类替换)?

避免太快/无效重复标记的重要注意事项 - 请在回答前阅读。

不要建议任何更改原始类代码的解决方案- 不允许更改代码反射和解析。

  1. 如何按照声明的顺序读取类属性?是解决方案 - 它需要替换所有类中的元类并增加开销 - 绝对不是反射使用。

  2. 考虑到我不能或不想更改代码来扫描类成员顺序。类可以具有或已经具有未知的元类 - 不可能无缘无故地添加元类或增加性能开销。

  3. 只能使用反射或者解析文件。


我想避免编写解析器并按声明顺序读取类属性。

如何在 Python 中使用反射(和简单的解析)?

让我举一些例子:

class A(object):
  b = 1
  a = 1
  c = 1
Run Code Online (Sandbox Code Playgroud)

dir(A)给出字母顺序,但要求是声明顺序。如何做到这一点 - 请帮忙?

python reflection class python-2.7

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

如何在 Python 中使用 pl196x 从 nltk 中提取引理以进行波兰语?

我写了一些基本的程序,我想提取波兰语单词的引理,这是非常重要的,因为波兰语使用屈折。

我正在创建简单的程序来加载数据并将单词转换为引理,但不知道该怎么做:

from nltk import corpus

pl = corpus.pl196x
print dir(pl)
print iter(pl.tagged_words()).next()
Run Code Online (Sandbox Code Playgroud)

例如,我想做这样的词形还原(忽略一些词形还原可能含糊不清——这在波兰语中是正常的):

kot, kota, kota, kotu, kotem, kocie, kocie == kot (singular male)
kotka, kotki, kotk?, kotce, kotk?, kotce, kotka == kot (singular female!)
koci?tko, koci?tka, koci?tko, koci?tku, koci?tkiem, koci?tku, koci?tko == kot (singular neutral)
etc. (plural males, plural females, plural neutrals)
Run Code Online (Sandbox Code Playgroud)

如何使用 pl196x 在或 nltk 中做这样的工作。


语料库的源数据包含这样的屈折变化和引理 - 所以它是可能的,但如何访问它:

<w id="pu147125" lemma="kot" ana="SSNA---------P">kot</w>
<w id="pr021633" lemma="kot" ana="SSAA---------P">kota</w>
etc.
Run Code Online (Sandbox Code Playgroud)

python nlp polish nltk python-2.7

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

为什么我不能在Windows 7上使用Cygwin SSH从Github克隆存储库?

我想使用Git + Cygwin + Ssh从Github克隆我自己的存储库.

怎么做 - 我创建密钥,我在github.com上发布公钥,我做ssh-add?

我做错了什么?

$git clone git@github.com:ChameleonRed/test.git
Cloning into 'test'...
Warning: Permanently added the RSA host key for IP address '192.30.252.123' to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

记录更多细节:

OpenSSH_7.2p2, OpenSSL 1.0.2g  1 Mar 2016
debug1: Reading configuration data /etc/ssh_config
debug1: Connecting to github.com [192.30.252.123] port 22.
debug1: Connection established.
debug1: identity file …

git ssh cygwin github

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

如何将文件名从 Filebeat 传递到 Logstash?

如何将每个日志文件名从 Filebeat 传递到 Logstash?

我想在 Graylog 中查看源文件名以进行深入分析。

我研究了文档但没有找到解释。你能帮助我吗?

logstash graylog2 filebeat

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

如何在 Python 中将 dict 绑定到准备好的 Cassandra 语句?

如何传递字典和查询参数?

我可以做这样的代码。

prepared = session.prepare('select name from task where id = ?;')
bound = prepared.bind([1])
session.execute(bound)
Run Code Online (Sandbox Code Playgroud)

如何使用 dict 作为参数以及查询语法是什么?

这不起作用:

prepared = session.prepare('select name from task where id = %(id)s;')
bound = prepared.bind({"id": 1})
session.execute(bound)
Run Code Online (Sandbox Code Playgroud)

你能帮忙解决这个基本代码吗 - 看起来这是可能的,但我不知道有效的查询语法?

python cassandra cassandra-driver

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

编辑距离算法中的“转置”是什么意思?

我正在研究编辑距离算法,但无法理解换位 - 这是什么意思?

http://www.nltk.org/_modules/nltk/metrics/distance.html

def edit_distance(s1, s2, transpositions=False):
Run Code Online (Sandbox Code Playgroud)

文档中没有明确解释。

你能举一些简单的例子来帮助我理解吗?

python nlp nltk python-2.7

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

如何通过一个循环传递对多个对象属性求和(make totals)?

我想在一个循环中一次总结多个属性:

class Some(object):
    def __init__(self, acounter, bcounter):
        self.acounter = acounter
        self.bcounter = bcounter

someList = [Some(x, x) for x in range(10)]
Run Code Online (Sandbox Code Playgroud)

我可以做一些比它更简单,更快速的事情吗?

atotal = sum([x.acounter for x in someList])
btotal = sum([x.bcounter for x in someList])
Run Code Online (Sandbox Code Playgroud)

python python-2.7

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

Python 3 asyncio 和 GIL(如何使用所有 cpu 内核 - 除 ProcessPoolExecutor 之外的任何其他选项)?

如何使用所有 cpu 内核进行 asyncio - 除了 ProcessPoolExecutor 之外的任何其他选项?

我认为 asyncio 无法打破 GIL 限制(也许我错了),因此程序的执行速度将比踩踏版本快,但会在一个核心上执行。

我研究了一些例子,我发现一种方法是多处理和 ProcessPoolExecutor。

https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.run_in_executor

# 3. Run in a custom process pool:
with concurrent.futures.ProcessPoolExecutor() as pool:
    result = await loop.run_in_executor(
        pool, cpu_bound)
    print('custom process pool', result)
Run Code Online (Sandbox Code Playgroud)

这很好,但需要在进程之间进行“pickle”,因此需要一些开销并对传递的参数进行一些优化以减少“pickle”序列化。

使用上面这个简单的模式,我写了这样的测试代码(如果你不喜欢它,你可以跳过这段代码阅读,因为它和以前一样)。顺便说一句,这是我解析文件问题的最快解决方案。这部分代码不是整个程序。

def _match_general_and_specific_file_chunk(file_name):
    with codecs.open(file_name, encoding='utf8') as f:
        while True:
            lines = f.readlines(sizehint=10000)
            if not lines:
                break
            for line in lines:
                general_match = RE_RULES.match(line)
                if general_match:
                    specific_match = RULES[general_match.lastindex].match(line)
                    groups = list(specific_match.groups())
                    continue


async def _async_process_executor_match_general_and_specific_read_lines_with_limit_file_chunk():
    loop = asyncio.get_event_loop()
    with ProcessPoolExecutor() …
Run Code Online (Sandbox Code Playgroud)

python python-3.x python-asyncio python-multiprocessing

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

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

如何在Python中编码位流(不是字节) - 它是什么简单的模块?

我希望编码和解码变量可数位的比特流成二进制字符串, 数字,64个碱基编码的字符串.流的最大长度约为21 + 20 = 41位,但可以稍长43,45.

假设位将由某个数组表示.

bits = [1]
encoded = someEncoder(bits)
decoded = someDecoder(encoded)
assert bits == decoded
Run Code Online (Sandbox Code Playgroud)

比特流可以更长,例如:

import random

def generateRandomBits(l):
  bitsBytes = random.getrandbits(l)
  bits = []
  for i in range(l):
    bitsBytes
    bits.append(int(bitsBytes >> i & 1))
  return bits

bits = generateRandomBits(21)
# [0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1]
Run Code Online (Sandbox Code Playgroud)

考虑它是可变 …

python python-2.7

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