# Fix qt import error # Include this file before import PyQt5 import os import sys import logging def _append_run_path(): if getattr(sys, 'frozen', False): pathlist = [] # If the application is run as a bundle, the pyInstaller bootloader # extends the sys module by a flag frozen=True and sets the app # path into variable _MEIPASS'. pathlist.append(sys._MEIPASS) # the application exe path _main_app_path = os.path.dirname(sys.executable) pathlist.append(_main_app_path) # append to system path enviroment os.environ["PATH"] += os.pathsep + os.pathsep.join(pathlist) logging.error("current PATH: %s", os.environ['PATH'])
_append_run_path()
猜想
之前没有安装 Anaconda,打包一切正常,默认会使用虚拟环境的 python 解释器,现在打包的时候,发现默认使用的解释器是 Anaconda 的 base 环境下的,可能这也是问题的原因之一,有待后续考证。。。
When this method (__bool__()) is not defined, __len__() is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither __len__() nor __bool__(), all its instances are considered true.
from blinker import Namespace my_signals = Namespace() model_saved = my_signals.signal('model-saved')
两者本质上是没有任何区别的,原因我们可以看一下 blinker 的源码
1 2
# https://github.com/jek/blinker/blob/master/blinker/base.py signal = Namespace().signal
很显然从源码看两者基本上可以等价起来,前者只是帮助我们简化了一个步骤
Signal 的发送
signal 创建好了之后,接下来就是使用了,使用很简单通过调用 send() 函数.需要注意的是,官方文档给了一个建议:
Try to always pick a good sender. If you have a class that is emitting a signal, pass self as sender. If you are emitting a signal from a random function, you can pass current_app._get_current_object() as sender.
* Serving Flask app "test" (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: on * Restarting with stat * Debugger is active! * Debugger PIN: 326-510-904 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) Got a signal sent by <Flask 'test'>,{'data': 3} 127.0.0.1 - - [24/Jun/2019 15:07:31] "GET / HTTP/1.1" 200 -
似乎少了依次输出?别着急,我们修改一下这个例子,使用 connect
1 2 3 4
# connect @test.connect defsubscriber(sender,**kwargs): print(f'Got a signal sent by {sender},{kwargs}')
再次执行看一下结果
1 2 3 4 5 6 7 8 9 10 11 12
* Serving Flask app "test" (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: on * Restarting with stat * Debugger is active! * Debugger PIN: 326-510-904 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) Got a signal sent by <Flask 'test'>,{'data': 3} Got a signal sent by test,{} 127.0.0.1 - - [24/Jun/2019 15:10:43] "GET / HTTP/1.1" 200 -
deffrom_url(url, output_path, options=None, toc=None, cover=None, configuration=None, cover_first=False): """ Convert file of files from URLs to PDF document :param url: URL or list of URLs to be saved :param output_path: path to output PDF file. False means file will be returned as string. :param options: (optional) dict with wkhtmltopdf global and page options, with or w/o '--' :param toc: (optional) dict with toc-specific wkhtmltopdf options, with or w/o '--' :param cover: (optional) string with url/filename with a cover html page :param configuration: (optional) instance of pdfkit.configuration.Configuration() :param configuration_first: (optional) if True, cover always precedes TOC Returns: True on success """
r = PDFKit(url, 'url', options=options, toc=toc, cover=cover, configuration=configuration, cover_first=cover_first)
deffrom_file(input, output_path, options=None, toc=None, cover=None, css=None, configuration=None, cover_first=False): """ Convert HTML file or files to PDF document :param input: path to HTML file or list with paths or file-like object :param output_path: path to output PDF file. False means file will be returned as string. :param options: (optional) dict with wkhtmltopdf options, with or w/o '--' :param toc: (optional) dict with toc-specific wkhtmltopdf options, with or w/o '--' :param cover: (optional) string with url/filename with a cover html page :param css: (optional) string with path to css file which will be added to a single input file :param configuration: (optional) instance of pdfkit.configuration.Configuration() :param configuration_first: (optional) if True, cover always precedes TOC Returns: True on success """
r = PDFKit(input, 'file', options=options, toc=toc, cover=cover, css=css, configuration=configuration, cover_first=cover_first)
deffrom_string(input, output_path, options=None, toc=None, cover=None, css=None, configuration=None, cover_first=False): """ Convert given string or strings to PDF document :param input: string with a desired text. Could be a raw text or a html file :param output_path: path to output PDF file. False means file will be returned as string. :param options: (optional) dict with wkhtmltopdf options, with or w/o '--' :param toc: (optional) dict with toc-specific wkhtmltopdf options, with or w/o '--' :param cover: (optional) string with url/filename with a cover html page :param css: (optional) string with path to css file which will be added to a input string :param configuration: (optional) instance of pdfkit.configuration.Configuration() :param configuration_first: (optional) if True, cover always precedes TOC Returns: True on success """
r = PDFKit(input, 'string', options=options, toc=toc, cover=cover, css=css, configuration=configuration, cover_first=cover_first)
defcount_x_between_one_and_n(n,x): if n < 0or x < 1or x > 9: return0 high,low,current,tmp,i = 1,1,1,1,1 high = n total = 0 while high !=0: high = int(n/int(math.pow(10,i))) tmp = int(n%int(math.pow(10,i))) current = int(tmp/int(math.pow(10,i-1))) low = int(tmp%int(math.pow(10,i-1))) if current == x: total += high*int(math.pow(10,i-1))+low+1 elif current < x: total += high*int(math.pow(10,i-1)) else: total += (high+1)*int(math.pow(10, i-1)) i+=1 return total
res =count_x_between_one_and_n(866278171,3)) # 796741437 # 求为奇数的时候,当个位为3的时候,均为奇数,除去这一部分,这剩下的 # 奇偶数各占一半 a = 796741437# 总个数 b = 866278170/10# 个位为 3 的个数 c = (a - b)/2 + b # 再加上个位为3的个数,得出奇数列中3出现的个数 441684627