I work mostly on the back end, so I’ll leave that out. This article focuses on some of the other things I do with Python.

1 Data export support

You might have to export some data once or twice a month, and each time the requirements are a little different. In the beginning, it’s ok to use database connection tools such as Sequel Pro and DataGrip to export directly. Later, some fields need to be customized or linked to tables, which is not convenient to use those tools, so you have to write your own scripts. I searched online and found a records library written by Kenneth Reitz, which is super cool to use.

SQL execution is simple and crude

import records

db = records.Database('mysql+pymysql://'
                      'username:password@host:port'
                      '/database? charset=utf8')
rows = db.query('select * from books') 
Copy the code

Read results are also more convenient to use, iteration using dictionaries or objects can directly obtain the corresponding value, compared with some class libraries need to convert their own much more convenient

for r in rows:
    print(r['name'], r['price'])
    # or
    # print(r.name, r.price)
Copy the code

Export formats are also supported, including CSV, YAML, and XLS. For example, to export an XLS file

with open('books.xls'.'wb') as f:
    f.write(rows.export('xls'))
Copy the code

Of course, because the library is simple and flexible to use, it does a few things besides exporting. For example, when some data in a table has exceptions and it is too cumbersome to write SQL to find out which ones, this library can also solve the problem.

It saves a lot of time when you’re doing support work.

Simple code generation automatically

In development, we often find that some code structures are so similar that it’s a waste of time to type line by line, so we should consider using a script.

For example, when creating a new piece of data, you might encounter a similar statement if you were using SQLAlchemy

def create(book):
	new_book = Book()
	new_book.name = book.get('name'.' ')
	new_book.price = book.get('price', 0)
	#...
	db.session.add(new_book)
	db.session.flush()
Copy the code

If the table has a lot of fields, it is too painful to type line by line. So, we can write a script to generate it automatically

# coding=utf-8

import records

db = records.Database('mysql+pymysql://'
                      'username:password@host:port'
                      '/database? charset=utf8')

books = db.query('select * from books limit 1')
columns = list(books.as_dict()[0].keys())

for column in columns:
    print(f"new_book.{column} ="
          f" book.get('{column}', '')")
Copy the code

The result is the following code

new_book.id = book.get('id'.' ')
new_book.cat_id = book.get('cat_id'.' ')
new_book.name = book.get('name'.' ')
new_book.price = book.get('price'.' ')
Copy the code

Copy the above to where you need it, then manually set the default value of get() itself based on the type, and you’re done.

Of course, if there are not many fields, this can be even more time-consuming. Just choose according to your needs.

Here, just as an example, consider writing a script when you have a lot of repetitive tasks at work. It may take a little time at first, but it’s a huge increase in efficiency.

3 Message Push

At some point, I might notice something and want to remind me when it’s appropriate. For example, I follow the same phone on several e-commerce platforms at the same time, and I hope to be reminded when the lowest price of these platforms is lower, so that I can write a script to solve the problem.

The idea is very simple. Write a simple crawler to get the price of this phone on these platforms every once in a while and take the minimum value. When the minimum value is lower than the expected value, tell me that I can send wechat, SMS or push messages to the APP. Wechat is tedious to use, and SMS is free and unreliable, so I like to push messages to the APP. Finally, put the script on the server and run.

Here, I recommend a website called IFTTT (https://ifttt.com). As its name suggests (IF This Then That), the push tool basically creates an Applet on the site and defines events and messages. When the corresponding event occurs, push the message to the App on the mobile phone. Of course, this App is provided by the website, which is currently supported by Android and IOS. Specific how to use, we can see this article under this section of https://realpython.com/python-bitcoin-ifttt Sending a Test IFTTT Notification.

4 other

Python solves many small, everyday problems, and these are just some of them. Developers from different directions and interests come up with different gadgets. Before, for example, look at the Tokyo 喰 kind of cartoon, watching it on web pages load too slowly, one nu, write a script to open next multithreading a minute less than a few hundred words full of good, locally smooth look. I also saw a library for downloading shy videos on GitHub before, so I don’t have to find the resource myself, but SORRY, I forgot the name of the library. Anyway, knowing Python can help you solve a lot of problems even if you don’t do development.

This article was first published on the public account “Little Back end”.