- 如何打造数据治理闭环?以保[2022-06-16]
- 什么样的公司需要数据治理?[2022-06-15]
- “互联网+政务服务”下的数[2022-06-14]
- 我国数据安全治理研究[2022-06-13]
- 反洗钱视角下的数据治理[2022-06-10]
- 浅谈数据质量管理[2022-06-08]
- 做数据治理前,必须了解并避[2022-06-07]
- 企业数据治理团队的十大职[2022-06-06]
- 浅谈数据质量管理[2022-06-02]
- 数据治理的三种共享范式[2022-06-01]
编程大神进阶之路:Python技巧小贴士
user_input = "This\nstring has\tsome whitespaces...\r\n"
character_map = {
ord('\n') : ' ',
ord('\t') : ' ',
ord('\r') : None
}
user_input.translate(character_map) # This string has some whitespaces...
import itertools
s = itertools.islice(range(50), 10, 20) # <itertools.islice object at 0x7f70fab88138>
for val in s:
...
string_from_file = """
// Author: ...
// License: ...
//
// Date: ...
Actual content...
"""
import itertools
for line in itertools.dropwhile(lambda line: line.startswith("//"), string_from_file.split("\n")):
print(line)
def test(*, a, b):
pass
test("value for a", "value for b") # TypeError: test() takes 0 positional arguments...
test(a="value", b="value 2") # Works...
class Connection:
def __init__(self):
...
def __enter__(self):
# Initialize connection...
def __exit__(self, type, value, traceback):
# Close connection...
with Connection() as c:
# __enter__() executes
...
# conn.__exit__() executes
from contextlib import contextmanager
@contextmanager
def tag(name):
print(f"<{name}>")
yield
print(f"</{name}>")
with tag("h1"):
print("This is Title.")
class Person:
__slots__ = ["first_name", "last_name", "phone"]
def __init__(self, first_name, last_name, phone):
self.first_name = first_name
self.last_name = last_name
self.phone = phone
import signal
import resource
import os
# To Limit CPU time
def time_exceeded(signo, frame):
print("CPU exceeded...")
raise SystemExit(1)
def set_max_runtime(seconds):
# Install the signal handler and set a resource limit
soft, hard = resource.getrlimit(resource.RLIMIT_CPU)
resource.setrlimit(resource.RLIMIT_CPU, (seconds, hard))
signal.signal(signal.SIGXCPU, time_exceeded)
# To limit memory usage
def set_max_memory(size):
soft, hard = resource.getrlimit(resource.RLIMIT_AS)
resource.setrlimit(resource.RLIMIT_AS, (size, hard))
def foo():
pass
def bar():
pass
__all__ = ["bar"]
from functools import total_ordering
@total_ordering
class Number:
def __init__(self, value):
self.value = value
def __lt__(self, other):
return self.value < other.value
def __eq__(self, other):
return self.value == other.value
print(Number(20) > Number(3))
print(Number(1) < Number(5))
print(Number(15) >= Number(15))
print(Number(10) <= Number(2))
并非本文中所有提到的功能在日常的 Python 编程中都是必需或有用的,但是其中某些功能可能会不时派上用场,而且它们也可能简化一些原本就很冗长且令人烦恼的任务。还需指出的是,所有这些功能都是 Python 标准库的一部分。而在我看来,其中一些功能似乎并不像标准库中包含的标准内容,所以当你使用 Python 实现本文提到的某些功能时,请先参阅 Python 的标准库,如果你不能找到想要的功能,可能只是因为你还没有尽力查找(如果真的没有,那它肯定也存在于一些第三方库)。