第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Sqlalchemy 為父母過(guò)濾父子表

Sqlalchemy 為父母過(guò)濾父子表

冉冉說(shuō) 2022-10-18 14:39:26
我認(rèn)為@jrjc 的這個(gè)功能解決方案對(duì)我的理解來(lái)說(shuō)相當(dāng)令人困惑,因此,我更愿意稍微改變我自己的東西并進(jìn)行調(diào)整。所以,我花了一些時(shí)間才明白,當(dāng)為情節(jié)創(chuàng)建第二個(gè)圖例時(shí),python 會(huì)自動(dòng)刪除第一個(gè)圖例,這是add_artist()必須使用的時(shí)候。添加第二個(gè)圖例的另一個(gè)先決條件是命名圖并將 .add_artist() 方法應(yīng)用于該特定圖,以便 python 知道將新部分粘貼在哪里。簡(jiǎn)而言之,這就是我設(shè)法創(chuàng)建我想到的情節(jié)的方式,我希望這些評(píng)論能讓它更清晰,對(duì)任何人都有用。import matplotlib.pyplot as pltfrom matplotlib.colors import LinearSegmentedColormap as coloringimport matplotlib.patches as mpatches# copy the dfs below and use pd.read_clipboard() to reproducedf_1     A   B   C   D   EMg  10  15  23  25  27Ca  30  33   0  20  17df_2     A   B   C   D   EMg  20  12   8  40  10Ca   7  26  12  22  16hatches=(' ', '//')colors_ABCDE=['tomato', 'gold', 'greenyellow', 'forestgreen', 'palevioletred']dfs=[df_1,df_2]for each_df, df in enumerate(dfs):    #I name the plot as "figure"    figure=df.plot(ax=plt.subplot(111), kind="barh", \            stacked=True, hatch=hatches[each_df], \            colormap=coloring.from_list("my_colormap", colors_ABCDE), \            figsize=(7,2.5), position=len(dfs)-each_df-1, \            align='center', width=0.2, edgecolor="darkgrey", \            legend=False) #I had to False the legend toolegend_1=plt.legend(df_1.columns, loc='center left', bbox_to_anchor=(1.0, 0.5), fontsize=12)patch_hatched = mpatches.Patch(facecolor='beige', hatch='///', edgecolor="darkgrey", label='hatched')patch_unhatched = mpatches.Patch(facecolor='beige', hatch=' ', edgecolor="darkgrey", label='non-hatched')legend_2=plt.legend(handles=[patch_hatched, patch_unhatched], loc='center left', bbox_to_anchor=(1.15, 0.5), fontsize=12)# as soon as a second legend is made, the first disappears and needs to be added back againfigure.add_artist(legend_1) #python now knows that "figure" must take the "legend_1" along with "legend_2"有兩個(gè)傳說(shuō)的情節(jié)我很確定它可以更加優(yōu)雅和自動(dòng)化。
查看完整描述

1 回答

?
嚕嚕噠

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超7個(gè)贊

此代碼顯示如何使用顯式連接或子查詢獲取結(jié)果:


import sqlalchemy as sa

from sqlalchemy import orm

from sqlalchemy.ext.declarative import declarative_base


Base = declarative_base()



Parent_images = sa.Table(

    'Parent_images', Base.metadata,

    sa.Column('parent_id', sa.Integer, sa.ForeignKey('parents.id')),

    sa.Column('child_id', sa.Integer, sa.ForeignKey('children.id'))

)



class Parent(Base):

    __tablename__ = 'parents'

    id = sa.Column(sa.Integer, primary_key=True)

    name = sa.Column(sa.Unicode(100), nullable=False, unique=True)

    colours = orm.relationship('Child_s3', secondary=Parent_images, backref='parents')


    def __repr__(self):

        return 'Parent(name=%s)' % self.name


    __str__ = __repr__



class Child_s3(Base):

    __tablename__ = 'children'

    id = sa.Column(sa.Integer, primary_key=True)

    name = sa.Column(sa.Unicode)


    def __repr__(self):

        return 'Child_s3(name=%s)' % self.name


    __str__ = __repr__



if __name__ == '__main__':

    engine = sa.create_engine('sqlite:///')

    Base.metadata.drop_all(engine)

    Base.metadata.create_all(engine)

    Session = orm.sessionmaker(bind=engine)


    session = Session()

    for parent, child in [('boofoo', 'spam'), ('baz', 'foobar'), ('bar', 'quux')]:

        p1 = Parent(name=parent)

        session.add(p1)

        p1.colours.append(Child_s3(name=child))

    session.commit()


    print('Join')

    session = Session()

    q = (session.query(Parent)

                .join(Child_s3, Parent.colours)

                .filter(sa.or_(Parent.name.ilike('%foo%'),

                               Child_s3.name.ilike('%foo%'))))

    for p in q.all():

        print(p, p.colours)

    session.commit()

    print()


    print('Subquery')

    session = Session()

    q = (session.query(Parent)

                .filter(sa.or_(Parent.name.ilike('%foo%'),

                               Parent.colours.any(Child_s3.name.ilike('%foo%')))))

    for p in q.all():

        print(p, p.colours)

    session.commit()

    print()

連接查詢


q = (session.query(Parent)

            .join(Child_s3, Parent.colours)

            .filter(sa.or_(Parent.name.ilike('%foo%'),

                           Child_s3.name.ilike('%foo%'))))

生成此 SQL


SELECT parents.id AS parents_id, parents.name AS parents_name 

FROM parents JOIN "Parent_images" AS "Parent_images_1" ON parents.id = "Parent_images_1".parent_id JOIN children ON children.id = "Parent_images_1".child_id 

WHERE lower(parents.name) LIKE lower(?) OR lower(children.name) LIKE lower(?)

子查詢


q = (session.query(Parent)

            .filter(sa.or_(Parent.name.ilike('%foo%'),

                            Parent.colours.any(Child_s3.name.ilike('%foo%')))))

生成此 SQL:


SELECT parents.id AS parents_id, parents.name AS parents_name            

FROM parents                                                                                                                        

WHERE lower(parents.name) LIKE lower(?) OR (EXISTS (SELECT 1                                                                        

FROM "Parent_images", children                                                                                                      

WHERE parents.id = "Parent_images".parent_id AND children.id = "Parent_images".child_id AND lower(children.name) LIKE lower(?)))

該腳本從示例數(shù)據(jù)生成此輸出:


Join

Parent(name=baz) [Child_s3(name=foobar)]

Parent(name=boofoo) [Child_s3(name=spam)]


Subquery

Parent(name=boofoo) [Child_s3(name=spam)]

Parent(name=baz) [Child_s3(name=foobar)]


查看完整回答
反對(duì) 回復(fù) 2022-10-18
  • 1 回答
  • 0 關(guān)注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)