小编lxy*_*xyu的帖子

如何在没有dateutil的情况下将时区感知字符串转换为python中的datetime?

我必须将时区感知字符串转换为python datetime对象.

例如2012-11-01T04:16:13-04:00.

我发现有一个dateutil模块具有解析函数来执行它,但我真的不想使用它,因为它添加了一个依赖项.

那我该怎么办呢?我尝试了类似下面的东西,但没有运气.

datetime.datetime.strptime("2012-11-01T04:16:13-04:00", "%Y-%m-%dT%H:%M:%S%Z")
Run Code Online (Sandbox Code Playgroud)

python timezone datetime rfc3339

60
推荐指数
5
解决办法
7万
查看次数

芹菜:如何忽略和弦或链中的任务结果?

我正在使用芹菜,我有几个需要按顺序执行的任务.

例如,我有这个任务:

@celery.task
def tprint(word):
    print word
Run Code Online (Sandbox Code Playgroud)

我想做这样的事情:

>>> chain(tprint.s('a') | tprint.s('b'))()
Run Code Online (Sandbox Code Playgroud)

然后我明白了TypeError: tprint() takes exactly 1 argument (2 given).

和弦一样,在这种情况下我需要在一组任务后执行任务:

>>> chord([tprint.s('a'), tprint.s('b')])(tprint.s('c'))
Run Code Online (Sandbox Code Playgroud)

那么如何处理这种情况呢?我不关心每项任务的结果,但需要按顺序执行.


添加第二个参数将无法正常工作:

@celery.task
def tprint(word, ignore=None):
    print word

>>> chain(tprint.s('a', 0) | tprint.s('b'))()
Run Code Online (Sandbox Code Playgroud)

这将打印出'a'和'None'.

python asynchronous task celery

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

python isdigit()函数对非数字字符u'\ u2466'返回true

我遇到了一个处理python isdigit函数的奇怪问题.

例如:

>>> a = u'\u2466'
>>> a.isdigit()
Out[1]: True
>>> a.isnumeric()
Out[2]: True
Run Code Online (Sandbox Code Playgroud)

为什么这个角色是数字?

有什么办法让这个返回False,谢谢?


编辑,如果我不想将其视为数字,那么如何过滤掉它?

例如,当我尝试将其转换为int时:

>>> int(u'\u2466')
Run Code Online (Sandbox Code Playgroud)

然后UnicodeEncodeError发生了.

python unicode digit

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

如何从python中的coroutine获取返回值

我正在根据http://www.dabeaz.com/coroutines/Coroutines.pdf尝试coroutines管道

问题是,我怎样才能从中获取价值sink而不仅仅是打印它?

以此代码为例

def coroutine(func):
    def start(*args, **kwargs):
        cr = func(*args, **kwargs)
        next(cr)
        return cr
    return start


@coroutine
def produce(target):
    while True:
        n = (yield)
        target.send(n*10)


@coroutine
def sink():
    try:
        while True:
            n = (yield)
            print(n)
    except GeneratorExit:
        pass


sk = sink()
pipe = produce(sink())
Run Code Online (Sandbox Code Playgroud)

使用此代码,我得到:

>>> pipe.send(10)
100
Run Code Online (Sandbox Code Playgroud)

然后我想得到返回值而不是打印它,我试图从接收器中产生:

@coroutine
def sink():
    try:
        while True:
            yield (yield)
    except GeneratorExit:
        pass
Run Code Online (Sandbox Code Playgroud)

但它似乎不起作用,pipe.send(10)仍然返回None而不是发电机.

那么如何获得返回值呢?

python generator coroutine

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

如何在python2中做surrogateescape

Python3改变了unicode行为以拒绝代理对,而python2则没有.

有一个问题在这里

但它没有提供如何在python2中删除代理对或如何进行代理转义的解决方案.

Python3示例:

>>> a = b'\xed\xa0\xbd\xe4\xbd\xa0\xe5\xa5\xbd'
>>> a.decode('utf-8', 'surrogateescape')
'\udced\udca0\udcbd??'
>>> a.decode('utf-8', 'ignore')
'??'
Run Code Online (Sandbox Code Playgroud)

'\ xed\xa0\xbd'这里不是正确的utf-8字符.我想忽略它们或逃脱它们.

是否有可能在python2中做同样的事情?

python unicode python-2.x surrogate-pairs

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

sed只在文件中更改一次字符串

建议我有一个文件example如下:

c
a
b
a
b
d
Run Code Online (Sandbox Code Playgroud)

我想改变的第一次出现ae.然后我这样做:

sed -i 's/a/e/' example
Run Code Online (Sandbox Code Playgroud)

一切都a变成了e.

那么有没有办法让sed只在文件中替换一次?

谢谢.

bash sed command-line-interface

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

python多处理锁定问题

我想添加一个dicts列表和python多处理模块.

这是我的代码的简化版本:

#!/usr/bin/python2.7
# -*- coding: utf-8 -*-

import multiprocessing
import functools
import time

def merge(lock, d1, d2):
    time.sleep(5) # some time consuming stuffs
    with lock:
        for key in d2.keys():
            if d1.has_key(key):
                d1[key] += d2[key]
            else:
                d1[key] = d2[key]

l = [{ x % 10 : x } for x in range(10000)]
lock = multiprocessing.Lock()
d = multiprocessing.Manager().dict()

partial_merge = functools.partial(merge, d1 = d, lock = lock)

pool_size = multiprocessing.cpu_count()
pool = multiprocessing.Pool(processes = pool_size)
pool.map(partial_merge, l)
pool.close()
pool.join()

print …
Run Code Online (Sandbox Code Playgroud)

python locking multiprocessing

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

如何在cython中执行struct.pack和struct.unpack?

我正在尝试将python模块转换为cython,它会执行大量的序列化和反序列化工作.

目前我必须这样做:

import struct

from libc.stdint cimport (
    int32_t,
    int64_t,
)

cpdef bytes write_int(int32_t i):
    return struct.pack("!i", i)

cpdef bytes write_long(int64_t i):
    return struct.pack("!q", i)

cdef bytes write_double(double val):
    return struct.pack("!d", val)

cdef bytes write_string(bytes val):
    cdef int32_t length = len(val)
    cdef str fmt
    fmt = "!i%ds" % length
    return struct.pack(fmt, length, val)
Run Code Online (Sandbox Code Playgroud)

在c lib中是否有与struct.pack和struct.unpack相同的东西?在cython中做这样的事情最好的方法是什么?

c python serialization struct cython

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

关于python函数返回值的最佳实践

假设我在python中编写一个函数,这个函数可以是成功的,也可以是异常.

我应该使用哪一个:

  1. 返回
  2. 返回True
  3. 没有回报

编辑:

谢谢你的回复.在这种情况下,我的意思是询问返回值是否没有进一步的含义,我还是应该做一个return True什么的.

我正在研究的情况是编写thrift服务器端函数,我在犹豫是否使用voidboolean输入服务api.

python coding-style

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