我希望能够使用print()或类似的方法执行python调试,在此方法中,除了通常的输出外,它还会打印传递的表达式。
例如,以下代码:
print(42 + 42)
print(type(list))
print(datetime.now())
Run Code Online (Sandbox Code Playgroud)
电流输出:
84
<class 'type'>
2019-08-15 22:43:57.805861
Run Code Online (Sandbox Code Playgroud)
预期产量:
42 + 42 : 84
type(list) : <class 'type'>
datetime.now() : 2019-08-15 22:43:57.805861
Run Code Online (Sandbox Code Playgroud)
当前,可以通过手动添加表达式字符串来实现相同目的(不是那么优雅,而且违反了DRY原理)。
print("42 + 42 : ", 42 + 42)
print("type(list) : ", type(list))
print("datetime.now() : ", datetime.now())
Run Code Online (Sandbox Code Playgroud)
我试图覆盖内置打印,但是没有成功:
import builtins
def print(*args, **kwargs):
return builtins.print(*args, **kwargs) # passed expression isn't available here as string!
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?谢谢!
我正在尝试用7个列创建一个表。最后一列包含一个长文本,这似乎会导致错误。似乎当单元格超出页面大小时,它将引发异常。
from reportlab.lib.pagesizes import landscape, A4
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import SimpleDocTemplate, LongTable, TableStyle, Paragraph
from reportlab.lib import colors
from reportlab.lib.units import mm
from datetime import date
doc = SimpleDocTemplate(response, pagesize=A4, rightMargin=30,leftMargin=30, topMargin=30,bottomMargin=18)
doc.pagesize = landscape(A4)
elements = []
styles = getSampleStyleSheet()
cell_style = styles['BodyText']
cell_style.wordWrap = 1
cell_style.fontName = 'Courier'
cell_style.spaceBefore = 30
cell_style.spaceAfter = 30
title_style = styles['Title']
title_style.fontName = 'Courier'
title = Paragraph('Export Issue Tracker (%s)' % (date.today().isoformat()), title_style)
elements.append(title)
data2 = [[Paragraph(cell, cell_style) for …Run Code Online (Sandbox Code Playgroud) 鉴于 Python 中没有真正的常量,约定是用大写字母命名它们以表达意图。
在以下示例代码中,FIRST和SECOND是常量:
def fibonacci_generator(count):
FIRST, SECOND = 0, 1
a, b = FIRST, SECOND
for _ in range(count):
yield a
a, b = b, a + b
print(list(fibonacci_generator(10)))
Run Code Online (Sandbox Code Playgroud)
但是对于这两个常量,PyCharm 给出了警告:
函数中的变量应该是小写的
有没有其他正确的方法来定义函数内的常量?(不抑制 PyCharm 警告)
I have a method called import_customers() which loads csv-like data.
This methods logs to log-level INFO.
In one case I want to avoid this logging.
I see several ways:
Variant 1: a new kwarg like do_logging=True which I can switch to false.
Variant 2: Use some magic context which ignores this line.
with IgnoreLoggingContext() as context:
import_customers()
Run Code Online (Sandbox Code Playgroud)
How could I implement IgnoreLoggingContext()?
If you think V1 is better, then please leave a comment.
有没有办法从 MongoDB 查询结果的文档(查找或聚合)中省略空字段(例如空字符串或空数组)。
数据库中的文档:
{
"_id" : ObjectId("5dc3fcb388c1c7c5620ed496"),
"name": "Bill",
"emptyString" : "",
"emptyArray" : []
}
Run Code Online (Sandbox Code Playgroud)
输出:
{
"_id" : ObjectId("5dc3fcb388c1c7c5620ed496"),
"name": "Bill"
}
Run Code Online (Sandbox Code Playgroud)
Elasticsearch 的类似问题:Omit null fields from elasticsearch results
使用 Python 3.7、Windows 10 专业版、Pywin32
我有一个测试脚本,它启动一个服务并在发出不同的命令时将一些基本行推送到日志文件中。代码如下:
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import logging
class AppServerSvc(win32serviceutil.ServiceFramework):
_svc_name_ = "TestService"
_svc_display_name_ = "Test Service"
_svc_description_ = "New Test Service"
logging.basicConfig(filename='search_server.log', level=logging.INFO)
logging.info('Class opened')
def __init__(self, args):
logging.basicConfig(filename='search_server.log', level=logging.INFO)
logging.info('Init')
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
socket.setdefaulttimeout(60)
def SvcStop(self):
logging.basicConfig(filename='search_server.log', level=logging.INFO)
logging.info('Stop')
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
logging.basicConfig(filename='search_server.log', level=logging.INFO)
logging.info('Run')
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
self.main()
def main(self):
print("running")
logging.basicConfig(filename='search_server.log', level=logging.INFO)
logging.info('Main')
if __name__ == '__main__':
logging.basicConfig(filename='search_server.log', level=logging.INFO) …Run Code Online (Sandbox Code Playgroud)