不了解你完整的業(yè)務(wù)場景,但是跨線程的話,一般是需要傳遞app_context的。
# 線程一,然后將這個參數(shù)傳遞給線程二
app_context = flask.current_app.app_context()
# 線程二,此處的app_context為線程一中傳遞來的參數(shù)
with app_context:
........get 后臺當然可以拿到數(shù)據(jù)啊,換個方法取而已
>>> import re
>>> p = re.compile('\d+')
>>> s = 'hello12 world334'
>>> re.sub('\d+', '', s)
'hello world'tableData = [
['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']
]
def print_table(data):
col_width = [max(len(item) for item in col) for col in data]
for row_idx in range(len(data[0])):
for col_idx, col in enumerate(data):
item = col[row_idx]
align = '<' if not col_idx==0 else '>'
print(('{:'+align+str(col_width[col_idx])+'}').format(item), end=' ')
print()
print_table(tableData)
結(jié)果:
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
我回答過的問題: Python-QA
每小時一次,每個關(guān)鍵詞都抓: age=你想要的小時數(shù) * 3600
1.match2的條件 這個None是啥意思,如果是想查詢consume這個字段是否存在,可以改成
{'$match': {'consume': {$exist:true}}}
2.這種優(yōu)化,我曾經(jīng)問過專業(yè)的大神@Mongoing中文社區(qū),我來班門弄斧一下,你這種查詢我猜測會很慢,但是慢的原因不在于match寫得有問題,而是lookup太慢了,十分影響效率,mongodb是非關(guān)系型數(shù)據(jù)庫,本身設(shè)計就不想要關(guān)系(我猜的哈),所以lookup和其它查詢關(guān)系的并不是它的強項,而且你這里查詢的是消費記錄和充值記錄,這兩個完全是可能不會更改的數(shù)據(jù),所以我建議是更改數(shù)據(jù)結(jié)構(gòu),將consume和recharge直接存放在這個user下面,然后不用lookup,直接篩選就可以了,這樣絕對會很快,當然這前提是在業(yè)務(wù)允許的情況下,修改數(shù)據(jù)結(jié)構(gòu)。
下面貼出我大哥的回答
mongodb的關(guān)聯(lián)查詢$lookup
ps:而且我做過一個實驗,20條數(shù)據(jù)都要lookup的情況下,我查兩次數(shù)據(jù)庫(先查出lookup之前的數(shù)據(jù),再用lookup的根據(jù)(比如說_id)去再查一次數(shù)據(jù)庫,再把這兩者的結(jié)果整合成我想要的數(shù)據(jù))都比直接lookup要快。。。。
是可以返回的,假如說你返回的參數(shù)是字典類型的,那么可以在前端按照字典的取值來取值,如果是一個循環(huán)體的話在{% for i in index %}
i.xx
{% endfor %}
這樣來取值。
# 因為這個地方涉及到了python中的元類,而我元類理解的不到家
# 所以就不詳細解釋了,只給樓主說下大致流程吧
# 免得元類那塊說錯了,再誤導(dǎo)樓主
# 因為樓主沒有寫,我就認為樓主使用的插件是flask-wtf了
# 直接看Form的父類 flask_wtf.form.FlaskForm 和 wtforms.form就可以了
# 在 flask_wtf.form.FlaskForm 中定義了一個 Meta 內(nèi)部類
# 在這個內(nèi)部類中有如下方法,就是在這個方法中獲取的表單中的值的
def wrap_formdata(self, form, formdata):
if formdata is _Auto:
if _is_submitted():
if request.files:
return CombinedMultiDict((
request.files, request.form
))
elif request.form:
return request.form
elif request.get_json():
return ImmutableMultiDict(request.get_json())
return None
return formdata
# 然后在wtforms.form.BaseForm中通過如下方法,為自定義的每個filed賦值
def process(self, formdata=None, obj=None, data=None, **kwargs):
# 這個就是form表單中的內(nèi)容了
formdata = self.meta.wrap_formdata(self, formdata)
if data is not None:
kwargs = dict(data, **kwargs)
# 這里通過form表單中的值,為每個自定義filed賦值
for name, field, in iteritems(self._fields):
if obj is not None and hasattr(obj, name):
field.process(formdata, getattr(obj, name))
elif name in kwargs:
field.process(formdata, kwargs[name])
else:
field.process(formdata)
# 注:
# 如下操作確實是會創(chuàng)建一個新的對象,但是因為這個新的對象中的 __init__ 方法
# 是會對這個對象進行一定初始化的,而這個初始化的過程中會通過如上方式
# 獲取form表單中的值,并賦值給每個filed
# 所以才會出現(xiàn) POST 請求時 form.validate_on_submit() 為 True
form = NameForm()給tablewidget添加右鍵監(jiān)聽事件
用setTimeInterval每隔100ms請求一次,返回響應(yīng)
1.聞所未聞。我不知道你有沒有用過react,你直接修改一個組件里的state,this.state.name = 'a',也是可以的,但是你這樣做過嗎?肯定是不行的,因為你這樣就算改了state,也不會觸發(fā)react的重新渲染,反而會導(dǎo)致數(shù)據(jù)和視圖不一致。所以react提供里setState這個api用來改變state,這樣可以重新觸發(fā)react的渲染周期
2.mobx和redux以及state一樣,都是儲存數(shù)據(jù)的對象而已。但是mobx和redux是全局的,說到底也只是個對象,你這么改它當然能改,但只是改了而已,并沒有觸發(fā)mobx和redux的一些動作。所以從這個意義上來說,action就像是setState,是改變這個狀態(tài)樹的方式,通過調(diào)用action觸發(fā)reducer,從而改變數(shù)據(jù)樹。這個可以保證mobx或redux正常工作,且數(shù)據(jù)和視圖保持一致。
3.不是修改一個值寫一個action,而是一個action對應(yīng)一件事。不同的事對應(yīng)不同的action,觸發(fā)對應(yīng)的reducer,這樣可以讓mobx或redux知道,是什么動作導(dǎo)致了什么結(jié)果。
因為你pymysql沒裝啊,
super() 就是找爹
super().__init__(...) 就是說:"爹,先執(zhí)行一下你的__init__方法,等你干完了我再來做我的事。"
用途就是,你繼承一個父類了,原先父類的初始化方法里有很多東西,你又不打算自己重新寫一遍,那就叫父類先執(zhí)行原來的邏輯,再執(zhí)行后邊的。
ConvertEmptyStringsToNull 中間件是 Laravel 5.4 才開始加入的。
By default, Laravel includes the TrimStrings and ConvertEmptyStringsToNull middleware in your application's global middleware stack. These middleware are listed in the stack by the AppHttpKernel class. These middleware will automatically trim all incoming string fields on the request, as well as convert any empty string fields to null. This allows you to not have to worry about these normalization concerns in your routes and controllers.
If you would like to disable this behavior, you may remove the two middleware from your application's middleware stack by removing them from the $middleware property of your AppHttpKernel class.
看官方描述的意思就是為了規(guī)范化數(shù)據(jù)。
如果你確實不想這樣處理,可以在 app/Http/Kernel.php 文件中注釋掉此 middleware
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, //注釋掉此行
];
或者:
從數(shù)據(jù)庫層面,把 remark 字段的默認值設(shè)置為 空字符串
re.compile(r'<=(.*?)=>').findall(str)就是類似于sf這種投票、反對的功能吧?如果是我來做的話,我會這樣搞:
id article_id user_id is_like
自增ID 文章ID 用戶ID 是否喜歡(1喜歡0不喜歡)
如上是表的數(shù)據(jù)結(jié)構(gòu),應(yīng)該符合你的功能需求;
而且取值、查詢也方便;
laravel中如何操作不清楚,但是如果你的欄位非要存1,2,3,4,5這種數(shù)據(jù)結(jié)構(gòu)的話,那么原生的mysql可以采用find_in_set函數(shù)來操作;
join是等待線程結(jié)束,
至于一個線程或是兩個線程出錯,要怎么重啟,
如果線程出錯是異常,可以這樣做
class ExceptionThread(threading.Thread):
def __init__(self, group=None, target=None, name=None, args=(), kwargs=None, verbose=None):
"""
Redirect exceptions of thread to an exception handler.
"""
threading.Thread.__init__(self, group, target, anme, args, kwargs, verbose)
if kwargs is None:
kwargs = {}
self._target = target
self._args = args
self._kwargs = kwargs
self._exc = None
def run(self):
try:
if self._target:
except BaseException as e:
import sys
self._exc = sys.exc_info()
finally:
#Avoid a refcycle if the thread is running a function with
#an argument that has a member that points to the thread.
del self._target, self._args, self._kwargs
def join(self):
threading.Thread.join(self)
if self._exc:
msg = "Thread '%s' threw an exception: %s" % (self.getName(), self._exc[1])
new_exc = Exception(msg)
raise new_exc.__class__, new_exc, self._exc[2]
t = ExceptionThread(target=my_func, name='my_thread', args=(arg1, arg2, ))
t.start()
try:
t.join()
except:
print 'Caught an exception'
join(timeout=None)
Wait until the thread terminates. This blocks the calling thread until the thread whose join() method is called terminates – either normally or through an unhandled exception – or until the optional timeout occurs.
選取名為 alt 的所有屬性應(yīng)該用//@alt.
改成:
title_list=etree.HTML(response.content.decode('utf-8')).xpath('//div[@class="img"]/a[contains(@target,"_blank")]//@alt')
看了一下你的鏈接,發(fā)現(xiàn)后面就是你想附帶的參數(shù)
想確認一下你是準備用get帶params
或者還是post提交data
確認一下
requests.get(url=url,params=data)
#
requests.post(url=url,data=data)北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
達內(nèi)教育集團成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
北大課工場是北京大學(xué)校辦產(chǎn)業(yè)為響應(yīng)國家深化產(chǎn)教融合/校企合作的政策,積極推進“中國制造2025”,實現(xiàn)中華民族偉大復(fù)興的升級產(chǎn)業(yè)鏈。利用北京大學(xué)優(yōu)質(zhì)教育資源及背
博為峰,中國職業(yè)人才培訓(xùn)領(lǐng)域的先行者
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負責(zé)iOS教學(xué)及管理工作。
浪潮集團項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。