Series entry

"Python3 Programming Actual Tetris Robot"

Design ideas

Our general ORM, the basic mode is to be separated from the database, almost all of the model is built at the programming language level, and the program is used to deal with the database. Although we are separated from the specific operation of the database, we have to establish various model documents, use code to write the relationship between the tables, and so on, so that beginners will feel like a cloud for a while. My approach is to add logic to the Python dictionary, and the program automatically maps objects into standard SQL query statements. As long as we understand the standard SQL language, we can complete database query operations.

Intelligent query method design

Query reserved words: page, size, sort, search, lks, ins, ors, count, sum, group

page, size, sort

Example query:

dao = BaseDao()
rs = dao.select("users",{"page": 1, "size":10, "sort":"age desc"})
print(rs)

生成sql:   SELECT * FROM users  ORDER BY age desc LIMIT 0,10

search, fuzzy query switching parameters, if not provided, it is an exact match

提供字段查询的精确匹配与模糊匹配的切换。
```
dao = BaseDao()
rs = dao.select("users",{"username": "john", "password":"123", "search":"1"})
print(rs)

生成sql:   SELECT * FROM users  WHERE username like '%john%'  and password like '%123%'
```

ins, lks, ors

These are the three most important query methods. How to find the common ground between them and reduce redundant code is the key.

  • ins, database form field in query, one field has multiple values, for example:
    Example query:

    dao = BaseDao()
    rs = dao.select("users",{"ins":["age", 11,22,26]})
    print(rs)
    
    生成sql:   SELECT * FROM users  WHERE age in ( 11,22,26 )
  • ors, database table multi-field precise query, or connection, multiple fields to multiple values, for example:
    Example query:

    dao = BaseDao()
    rs = dao.select("users",{"ors":["age", 11,26]})
    print(rs)
    
    生成sql:   SELECT * FROM users  WHERE  ( age = 11  or age = 26 )
  • lks, database table multi-field fuzzy query, or connection, multiple fields to multiple values, for example:
    Example query:

    dao = BaseDao()
    rs = dao.select("users",{"lks":["username", "john","password","123"]})
    print(rs)
    
    生成sql:   SELECT * FROM users  WHERE  ( username like '%john%'  or password like '%123%'  )

count, sum

The sum of these two statistics is handled in a similar way. Generally, it should be used in conjunction with group and fields when querying.

  • count, database query function count, row statistics, for example:
    Example query:

    dao = BaseDao()
    rs = dao.select("users",{"count":["1", "total"]})
    print(rs)
    
    生成sql:   SELECT *,count(1) as total  FROM users
  • sum, database query function sum, field summation, for example:
    Example query:

    dao = BaseDao()
    rs = dao.select("users",{"sum":["age", "ageSum"]})
    print(rs)
    
    生成sql:   SELECT username,sum(age) as ageSum  FROM users

group, database grouping function group, for example:

Example query:

dao = BaseDao()
rs = dao.select("users",{"group":"age"})
print(rs)

生成sql:   SELECT * FROM users  GROUP BY age

Unequal operator query support

The supported unequal operators are: >, >=, <, <=, <>, =; the comma is a separator, and a field supports one or two operations.
Special: Use "=" to make a field skip the search effect, and make fuzzy matching and exact matching appear in a query statement at the same time

  • One operation per field, example:
    Example query:

    dao = BaseDao()
    rs = dao.select("users",{"age":">,10"})
    print(rs)
    
    生成sql:   SELECT * FROM users  WHERE age> 10
  • Two operations per field, example:
    Example query:

    dao = BaseDao()
    rs = dao.select("users",{"age":">=,10,<=33"})
    print(rs)
    
    生成sql:   SELECT * FROM users  WHERE age>= 10 and age<= 33
  • Use "=" to remove the search effect of the field, example:
    Example query:

    dao = BaseDao()
    rs = dao.select("users",{"age":"==,18","username":"john","search":"1"})
    print(rs)
    
    生成sql:   SELECT * FROM users  WHERE age= 18  and username like '%john%'

project address

https://gitee.com/zhoutk/ptetris
或
https://github.com/zhoutk/ptetris

Operation method

1. install python3, git
2. git clone https://gitee.com/zhoutk/ptetris (or download and unzip source code)
3. cd ptetris
4. python3 tetris

This project surpport windows, linux, macOs

on linux, you must install tkinter first, use this command:  
sudo apt install python3-tk

Related items

C++ version has been implemented, project address:

https://gitee.com/zhoutk/qtetris

zhoutk
2.6k 声望1.2k 粉丝

自由程序员,技术路线c,delphi,c++,c#,java,php,node.js,python,golang,typescript;超喜欢react.js的设计思路,全栈开发成为我的终极目标。开发机器macbook pro,或装ubuntu、fedora的机器,编程用vim...