GVKun编程网logo

SQLAlchemy:使用`and`和`or`时出现意外结果(sqlalchemy or)

19

在本文中,您将会了解到关于SQLAlchemy:使用`and`和`or`时出现意外结果的新资讯,同时我们还将为您解释sqlalchemyor的相关在本文中,我们将带你探索SQLAlchemy:使用`a

在本文中,您将会了解到关于SQLAlchemy:使用`and`和`or`时出现意外结果的新资讯,同时我们还将为您解释sqlalchemy or的相关在本文中,我们将带你探索SQLAlchemy:使用`and`和`or`时出现意外结果的奥秘,分析sqlalchemy or的特点,并给出一些关于2.SQLAlchemy 文档 - SQLAlchemy ORM(中文版)、3.SQLAlchemy 文档 - SQLAlchemy Core(中文版)、android-从代码而不是从xml创建选择器时出现意外结果、apscheduler遇到错误:SQLAlchemyJobStore requires SQLAlchemy的实用技巧。

本文目录一览:

SQLAlchemy:使用`and`和`or`时出现意外结果(sqlalchemy or)

SQLAlchemy:使用`and`和`or`时出现意外结果(sqlalchemy or)

我有一个声明式基类News

class News(Base):    __tablename__ = "news"    id = Column(Integer, primary_key = True)    title = Column(String)    author = Column(String)    url = Column(String)    comments = Column(Integer)    points = Column(Integer)    label = Column(String)

我还有一个函数f(title),该函数获取一个字符串并返回3个字符串变体之一:“ good”,“ maybe”或“ never”。我尝试获取过滤的行:

rows = s.query(News).filter(News.label == None and f(News.title) == ''good'').all()

但是程序失败,引发此错误:

raise TypeError("Boolean value of this clause is not defined")

我该如何解决?

答案1

小编典典

问题是这样的:

News.label == None and f(News.title) == ''good''#                  ^^^ here

Python不允许覆盖布尔 操作
and和的行为or。您可以__bool__在Python
3和__nonzero__Python
2中在一定程度上影响它们,但所做的只是定义对象的真实值。

如果有问题的对象没有实现__bool__并抛出错误,或者实现没有抛出,则由于and的短路特性and``or,您可能会获得相当神秘的错误:

In [19]: (News.label == ''asdf'') and TrueOut[19]: <sqlalchemy.sql.elements.BinaryExpression object at 0x7f62c416fa58>In [24]: (News.label == ''asdf'') or TrueOut[24]: True

因为

In [26]: bool(News.label == ''asdf'')Out[26]: False

这可能并且将导致以不正确的SQL表达式的形式出现问题:

In [28]: print(News.label == ''asdf'' or News.author == ''NOT WHAT YOU EXPECTED'')news.author = :author_1

为了产生布尔SQL表达式要么使用and_()or_()not_()SQL表达式的功能,或二进制&|~操作者重载:

# Parentheses required due to operator precedencefilter((News.label == None) & (f(News.title) == ''good''))

要么

filter(and_(News.label == None, f(News.title) == ''good''))

或将多个条件传递给对的调用Query.filter()

filter(News.label == None, f(News.title) == ''good'')

或组合多个呼叫至filter()

filter(News.label == None).filter(f(News.title) == ''good'')

2.SQLAlchemy 文档 - SQLAlchemy ORM(中文版)

2.SQLAlchemy 文档 - SQLAlchemy ORM(中文版)

接下来,我们将会对对象关系映射器进行全面的介绍和描述。如果你想用它为你的应用程序构建更高层次的 SQL 操作模式,以及为你的 Python 对象提供自动化的持久性功能,那么首先进行下列教程的学习吧。

  • 首先请看: 对象关系理论指南

  • ORM 的基本配置: 映射器的配置 | 关联的配置 | 继承映射 | 对象集合的高级配置

  • 配置的扩展: 声名式的扩展 | 辅助代理 | 混合属性 | 可变标量 | 排序列表

  • ORM 的使用: 会话的使用和指南 | 查询 API | 关联数据延迟加载技术

  • ORM 的扩展 : ORM 事件接口 | 内部 API

  • 其它: 实例介绍 | 已过时的事件接口 | ORM 异常 | 水平数据分片 | 备用说明


http://www.uifanr.com/


Here, the Object Relational Mapper is introduced and fully described. If you want to work with higher-level SQL which is constructed automatically for you, as well as automated persistence of Python objects, proceed first to the tutorial.

  • Read this first: Object Relational Tutorial

  • ORM Configuration: Mapper Configuration | Relationship Configuration | Inheritance Mapping | Advanced Collection Configuration

  • Configuration Extensions: Declarative Extension |Association Proxy | Hybrid Attributes | Mutable Scalars |Ordered List

  • ORM Usage: Session Usage and Guidelines | Query API reference | Relationship Loading Techniques

  • Extending the ORM: ORM Event Interfaces | Internals API

  • Other: Introduction to Examples | Deprecated Event Interfaces | ORM Exceptions | Horizontal Sharding | Alternate Instrumentation


3.SQLAlchemy 文档 - SQLAlchemy Core(中文版)

3.SQLAlchemy 文档 - SQLAlchemy Core(中文版)

这里的文描述了关于 SQLAlchemy 的的 SQL 渲染引擎的相关内容,包括数据库 API 的集成,事务的集成和数据架构描述服务。与以领域为中心的 ORM 使用模式相反,SQL 表达式语言提供了一个数据构架为中心的使用模式。

  • 首先请阅读: SQL 表达语言指南

  • SQL 内置的内容: SQL 表达式 API

  • 引擎,连接,缓冲池: 引擎配置 | 连接,事务 | 连接池

  • 数据构架定义: 数据表和数据列 | 数据库注入(反射) | 默认的插入与更新 | 数据约束和索引 | 使用数据定义语言 (DDL)

  • 数据类型: 基本概况 | 基础数据类型 | SQL 的标准数据类型 | 与数据库有关的特定数据类型 | 内置自定义数据类型 | 定义新的操作符 | API

  • 扩展核心: SQLAlchemy 的事件 | 核心事件接口 | 自定义 SQL 结构 | 内部 API

  • 其它: 运行时检查 API | 已过时事件接口 | 核心异常


http://www.uifanr.com/


The breadth of SQLAlchemy’s SQL rendering engine, DBAPI integration, transaction integration, and schema description services are documented here. In contrast to the ORM’s domain-centric mode of usage, the SQL Expression Language provides a schema-centric usage paradigm.

  • Read this first: SQL Expression Language Tutorial

  • All the Built In SQL: SQL Expression API

  • Engines, Connections, Pools: Engine Configuration |Connections, Transactions | Connection Pooling

  • Schema Definition: Tables and Columns | Database Introspection (Reflection) | Insert/Update Defaults |Constraints and Indexes | Using Data Definition Language (DDL)

  • Datatypes: Overview | Generic Types | SQL Standard Types| Vendor Specific Types | Building Custom Types | Defining New Operators | API

  • Extending the Core: SQLAlchemy Events | Core Event Interfaces | Creating Custom SQL Constructs | Internals API

  • Other: Runtime Inspection API | Deprecated Event Interfaces| Core Exceptions


android-从代码而不是从xml创建选择器时出现意外结果

android-从代码而不是从xml创建选择器时出现意外结果

我在将一些有效的XML文件转换为代码时遇到麻烦.
我有一个ListView,并且我需要能够在运行时从高级资源中未知的资源中切换其按下/选中的可绘制对象(因此为什么不使用XML);

以下配置非常有效:

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:choiceMode="singleChoice" >
    </ListView>

</LinearLayout>

选择器.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/blue" android:state_pressed="true"/>
    <item android:drawable="@drawable/green" android:state_checked="true"/>
    <item android:drawable="@drawable/orange"/>
</selector>

list_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/selector"
    android:padding="10dp" />

Main.java:

public class Main extends Activity {

    StateListDrawable selector; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ListView listView1 = (ListView) findViewById(R.id.listView1);
        StringAdapter adapter = new StringAdapter(this, R.layout.list_row);
        adapter.add("one");     adapter.add("two");     adapter.add("three");       adapter.add("four");
        adapter.add("five");        adapter.add("six");     adapter.add("seven");       adapter.add("eight");
        adapter.add("nine");        adapter.add("ten");     adapter.add("eleven");      adapter.add("twelve");
        listView1.setAdapter(adapter);
    }

    private class StringAdapter extends ArrayAdapter<String>{

        public StringAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);         
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final CheckedTextView  tv = (CheckedTextView ) inflater.inflate(R.layout.list_row, parent, false);
            tv.setText(getItem(position));
            return tv;
        }
    }
}

结果是这样的,很棒(按下:蓝色,选中:绿色,否则:橙色):

但是,如果我删除了

android:background="@drawable/selector

从list_row_xml,并通过代码应用它:

    Drawable blue = getResources().getDrawable(R.drawable.blue);
    Drawable green = getResources().getDrawable(R.drawable.green);
    Drawable orange = getResources().getDrawable(R.drawable.orange);

    selector = new StateListDrawable();
    selector.addState(new int[] { android.R.attr.state_pressed }, blue);
    selector.addState(new int[] { android.R.attr.state_checked }, green);
    selector.addState(new int[] { }, orange);

       tv.setBackgroundDrawable(selector);

我得到以下内容(一切都得到state_pressed可绘制的蓝色):

什么地方出了错?我很确定我将选择器正确地转换为代码.

解决方法:

我复制了您的代码,一切正常.
不过,您应该确保在getView()中创建选择器的新实例.否则,如果对所有项目使用相同的选择器,则按一个项目将影响所有项目.

这是您的getView()方法的外观

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final CheckedTextView tv = (CheckedTextView) inflater.inflate(R.layout.list_row, parent, false);
        tv.setText(getItem(position));

        Drawable blue = getResources().getDrawable(R.drawable.blue);
        Drawable green = getResources().getDrawable(R.drawable.green);
        Drawable orange = getResources().getDrawable(R.drawable.orange);

        selector = new StateListDrawable();
        selector.addState(new int[] { android.R.attr.state_pressed }, blue);
        selector.addState(new int[] { android.R.attr.state_checked }, green);
        selector.addState(new int[] {}, orange);

        tv.setBackgroundDrawable(selector);

        return tv;
    }

当然,您可以进行一些优化,但这将起作用.

apscheduler遇到错误:SQLAlchemyJobStore requires SQLAlchemy

apscheduler遇到错误:SQLAlchemyJobStore requires SQLAlchemy

file

英文:SQLAlchemyJobStore requires SQLAlchemy installed

翻译下:SQLAlchemyJobStore需要安装SQLAlchemy

说白了就是需要安装SQLAlchemy

安装也很简单

pip install sqlalchemy

关注我获取更多内容

关于SQLAlchemy:使用`and`和`or`时出现意外结果sqlalchemy or的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于2.SQLAlchemy 文档 - SQLAlchemy ORM(中文版)、3.SQLAlchemy 文档 - SQLAlchemy Core(中文版)、android-从代码而不是从xml创建选择器时出现意外结果、apscheduler遇到错误:SQLAlchemyJobStore requires SQLAlchemy的相关信息,请在本站寻找。

本文标签: