Some situations that need aliasing in Python programming

Function aliasing is just an application scenario of aliasing, and there are several places where aliasing is used. This note will summarize all kinds of aliases I have encountered.

Scenario A: pseudonym when importing

When importing other modules, the direct pseudonym is the simple version, which is quite commonly used by me. There are even some pseudonyms commonly used in the industry. Whether it is a module, a function or a variable in a module, it can be simplified in this way.

# Some aliases established by the industry
import pandas as pd
import numpy as np
# The pseudonym of custom module and custom function
from zhihu_base import get_all_topics_detail as zhihu

When I program, the habit of naming a function or variable is to let people know what to do when they see the name, so the name will be long. Because functions are to be called, especially external calls, the complexity of writing can be understood. But if it is a variable that is not called externally, why not define a simple name directly when defining variables?

Scenario B: the same line of statement calls the name multiple times, which is temporarily simplified

There is a situation where most of the statements calling this variable are relatively simple and easier to read with the full name, but occasionally there is a complex statement that needs to call this variable multiple times, resulting in the statement being particularly long. At this time, it is necessary to temporarily use a variable alias to simplify the code.

What I don't know in the learning process
 python learning buckle qun, 784758214
 There are good learning video tutorials, development tools and e-books in the group.
Share with you the current talent needs of python enterprises and how to learn python from scratch, and what to learn
sql_search = 'select url_token,zhihu_name,lase_active_time from zhihu_whos_v;'
df_topics_details = pd.read_sql(sql_search,conn)
#A lot of code is omitted here
 #At this time, a relatively complex and lengthy statement appears, and the variable name appears many times
df_value_v = df_topics_details[(df_topics_details['upvotecount']>100000) & (df_topics_details['last_activity']>'2019-09-01') ]

This statement is used to filter out the data that meets the conditions of upvotecount > 100000 and last Dou activity > 2019-09-01 in the data set df_topics_details. It is a quite common statement in pandas. Later, I found that in such a statement that calls a variable name or function name many times, I can temporarily add an alias to simplify the length of the statement. like this:


df_topics_details = dtd
df_value_v = dtd[(dtd['upvotecount']>100000) & (dtd['last_activity']>'2019-09-01') ]

However, this alias is not suitable for defining the variable. Imagine that I defined this variable as dtd in the first place, and many lines of code would be extremely unreadable - I or other readers of the code could not understand what dtd meant. If more variables adopt this style, how bad the readability of the code will be!

Scenario C: file object alias

In fact, the statement of file object aliasing is not accurate. In essence, it is the assignment of variables: assign a specific file object to a variable to refer to storage. Take it out alone because it's too high frequency. Like fw or fr, writer is also a conventional naming convention.

Example X:

fw = open("my_test.txt", "at")
fw.write("xue.cn The monthly fee is only 15 yuan, which is quite friendly to programming scholars.")
fw.close()

Example Y:

with open("my_test.txt", "at") as fw:
    fw.write("xue.cn You can write code online while reading on the web.")

Example Z:

comms_file = output_path + 'xuecn_comments_statistics_' + str(datetime.datetime.now())[:10] + '.xlsx'
with pd.ExcelWriter(comms_file) as writer:
    comms_counts_monthly.to_excel(writer, sheet_name='Message monthly')
    comms_counts_weekly.to_excel(writer, sheet_name='Message weekly')
    comms_counts_daily.to_excel(writer, sheet_name='Message daily')
    comms_by_reg_date.to_excel(writer, sheet_name='Messages of daily activated users')
    comms_by_reg_week.to_excel(writer, sheet_name='Weekly activation of users' messages')
    comms_counts_by_hour.to_excel(writer, sheet_name='Message active time')
    vote_by_name.to_excel(writer, sheet_name='Users like it')
    name_count_by_vote.to_excel(writer, sheet_name='Distribution of users' likes')
    vote_by_content.to_excel(writer, sheet_name='The message was praised')
    content_count_by_vote.to_excel(writer, sheet_name='The distribution of liked messages')

Scenario D: pseudonym is a universal operation

sql statements also have a "pseudonym" method. The keywords used are the same as those used in python import.

What I don't know in the learning process
python Learning buckle qun,784758214
//There are good learning video tutorials, development tools and e-books in the group.
//Share with you the current talent needs of python enterprises and how to learn python from scratch, and what to learn
with data as(
  select 
    date(created_at) as time,
    user_id
  from user_comments
  union all
  select 
    date(created_at) as time,
    user_id
  from user_activities
)
select
  time,
  count(distinct user_id) as Number of daily learning users
from data
group by time
order by time

def who_is_v_detail

Summary

I'm less concerned about the aliasing of functions, variables or objects. The core is that a pseudonym is just for something with a complex name. In addition, it takes a simple and easy to remember reference to it. No matter what the name is, the characteristics of that thing remain unchanged, and the total reference is ta.

It's like someone's name is "because of lack of thought hall · Nicholas · Jiang · bath Empire V · first class bodyguard with knife · Alaska · dog egg · king". You can call him Lao Wang for short.

"Pseudonym" is a common phenomenon in programming and daily life, but in a specific language, it is realized by what keywords or syntax.

Tags: Programming Python SQL less

Posted on Thu, 23 Apr 2020 01:54:51 -0700 by hdpt00