Vue3+Vite+TS implements secondary encapsulation of element-plus business components sfasga

Vue3+Vite+TS implements secondary encapsulation of element-plus business components sfasga

1. Structure string

You will often need to print strings. If you have many variables, avoid the following:

name = "Raymond"
age = 22
born_in = "Oakland, CA"
string = "Hello my name is " + name + "and I'm " + str(age) + " years old. I was born in " + born_in + "."
print(string)


How messy does this look? You can use a nice and simple alternative to .format.

as follows:

name = "Raymond"
age = 22
born_in = "Oakland, CA"
string = "Hello my name is {0} and I'm {1} years old. I was born in {2}.".format(name, age, born_in)
print(string)


2. Return tuple

Python allows you to return multiple elements from a function, which makes life much easier. But when unpacking tuples, I make a common mistake like this:

def binary(): return 0, 1
result = binary()
zero = result[0]
one = result[1]


This is not necessary, you can just do this:

def binary(): return 0, 1
zero, one = binary()


If you need all elements to be returned, use an underscore _

zero, _ = binary()


It’s so efficient!

3. Access Dict dictionary

You will also often write key , value to dicts .

If you try to access a key that doesn't exist in dict , you might be tempted to do this to avoid KeyError :

countr = {}
bag = [2, 3, 1, 2, 5, 6, 7, 9, 2, 7]
for i in bag:
if i in countr:
countr[i] += 1 else:
countr[i] = 1
for i in range(10):
if i in countr:
print("Count of {}: {}".format(i, countr[i]))
else:
print("Count of {}: {}".format(i, 0))


However, using get() is a better approach.

countr = {}
bag = [2, 3, 1, 2, 5, 6, 7, 9, 2, 7]
for i in bag:
countr[i] = countr.get(i, 0) + 1
for i in range(10):
print("Count of {}: {}".format(i, countr.get(i, 0)))


Of course you can also use setdefault instead.

There is also a simpler but more expensive method:

bag = [2, 3, 1, 2, 5, 6, 7, 9, 2, 7]


{2: 3, 3: 1, 1: 1, 5: 1, 6: 1, 7: 2, 9: 1}:

countr = dict([(num, bag.count(num)) for num in bag])
for i in range(10):
print("Count of {}: {}".format(i, countr.get(i, 0)))


We can also use dict comprehensions.

countr = {num: bag.count(num) for num in bag}


Both of these methods are expensive because they traverse the list every time count is called.

4. Use the library

Existing libraries can be imported to do exactly what you want.

Let's go back to the previous example and create a function to count the number of times a number appears in a list. Well, there used to be a library that could do this.

from collections import Counter
bag = [2, 3, 1, 2, 5, 6, 7, 9, 2, 7]
countr = Counter(bag)for i in range(10):
print("Count of {}: {}".format(i, countr[i]))


Some reasons to use libraries:

  • The code is correct and tested.
  • Their algorithms may be optimal, so they run faster.
  • Abstract: They are clear and well documented, and you can focus on what is yet to be done.

In the end, it was all there before, you don't have to reinvent the wheel.

5. Slicing/stepping through lists

We can specify start point and the stop point, like this list[start:stop:step] . We take the first 5 elements of the list:

bag = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for elem in bag[:5]:
print(elem)


This is slicing. We specify stop point as 5, and then 5 elements will be taken from the list before stopping.

What if the last 5 elements?

bag = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for elem in bag[-5:]:
print(elem)


Don’t you understand? -5 means take 5 elements from the end of the list.

If you want to perform a distance operation on the elements in a list, you might do this:

bag = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for index, elem in enumerate(bag):
if index % 2 == 0:
print(elem)


But you should do it like this:

bag = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for elem in bag[::2]:
print(elem)

6. Use ranges

bag = list(range(0,10,2))
print(bag)


This is the stepping in the list. list[::2] means traversing the list and taking out an element in two steps.

You can do cool reverse operations on a list with list[::-1] .

This is the end of this article about Vue3+Vite+TS implementing secondary encapsulation of element-plus business component sfasga . For more related secondary encapsulation of element-plus business component sfasga, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Getting Started Tutorial on Using TS (TypeScript) in Vue Project
  • Basic method of Vue accessing ts

<<:  CSS style reset and clear (to make different browsers display the same effect)

>>:  Analysis and summary of the impact of MySQL transactions on efficiency

Recommend

How to monitor global variables in WeChat applet

I recently encountered a problem at work. There i...

jQuery implements time selector

This article example shares the specific code of ...

A simple way to restart QT application in embedded Linux (based on QT4.8 qws)

Application software generally has such business ...

Use IISMonitor to monitor web pages and automatically restart IIS

Table of contents 1. Tool Introduction 2. Workflo...

Vue basic instructions example graphic explanation

Table of contents 1. v-on directive 1. Basic usag...

Master-slave synchronization configuration of Mysql database

Table of contents Mysql master-slave synchronizat...

Complete steps to use mock.js in Vue project

Using mock.js in Vue project Development tool sel...

How to change mysql password under Centos

1. Modify MySQL login settings: # vim /etc/my.cnf...

JS calculates the probability of winning based on the prize weight

Table of contents 1. Example scenario 1.1. Set th...

How to set horizontal navigation structure in Html

This article shares with you two methods of setti...

Simple example of using Docker container

Table of contents 1. Pull the image 2. Run the im...

Example code for implementing photo stacking effect with CSS

Achieve results step 1. Initial index.html To bui...

MySQL 5.7.19 winx64 free installation version configuration tutorial

mysql-5.7.19-winx64 installation-free version con...

Detailed explanation of the execution process of MySQL query statements

Table of contents 1. Communication method between...