Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of Python data processing

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are the methods of Python data processing". In daily operation, I believe many people have doubts about what problems Python data processing methods have. Xiaobian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "what are the methods of Python data processing"! Next, please follow the small series to learn together!

1. preface

Memcached: A high-performance distributed memory object caching system that reduces database reads through memory caching, thereby sharing the pressure on the database and improving the loading speed of the website.

Memcached is actually a simple key-value pair storage system that can store various types of data, including: strings, objects, images, files, videos, etc.

Because Memcached data is stored in memory, all data will be lost after restarting the service or system; in addition, when Memcached usage capacity reaches a specified value, it will automatically delete unused caches based on LRU algorithm.

Memcached protocol is simple and powerful, easy to develop, compatible with most development languages; this article talks about Python operation Memcached correct posture

2. ready

Let's take the example of installing Memcached-Server Cloud Virtual Machine Centos 7.8.

First, install Memcached on the Cloud Virtual Machine

#memcached yum install memcached

Query the directory where Memcached is installed with the whereis command.

#Query memcached installation directory # /usr/bin/memcached [root@VM-16-11-centos ~]#whereas memcached memcached: /usr/bin/memcached/usr/share/man/man/1/memcached. 1.gz

Then, start Memcached service via command line and parameters

#Run memcached service # -p: Port # -m: Classified memory # -d: Daemon process, background running # -u: Users running memcached # -l: Set which IPs can connect to service, 0.0.0.0: Allow external IPs to access/usr/bin/memcached -p 11211 -m 64m -d -u root -l 0.0.0.0

Common startup parameters include:

-d: Run in the background through daemons

-m: Maximum memory allocated, default size is 64 MB

-u: Users running Memcached

-l: Set the IP address that can access Memecache service. By default, it can only be accessed through local computer. If you want to access it from external network, you need to set it to 0.0.0.0.

-p: Set the port number that Memcached listens on, default is 111211

-c: Maximum number of concurrent connections running, default is 1024

Next, open the firewall port

It should be noted that if it is a Cloud Virtual Machine, the corresponding port number needs to be opened in the security group

#Open port number firewall-cmd --add-port=11211/tcp --permanent #Restart takes effect immediately firewall-cmd --reload

To do this, we have two ways to manipulate the data

They are:

1. Connect Memecached service through telnet and use command line to operate it.

#Connect telnet IP address 11211

2, through programming languages, including: Python, Java, Php, to operate Memcached data

Python, for example, requires python-memcached dependencies.

#install dependencies pip3 install python-memcached

3. actual combat

Before manipulating the data in Memcache, we need to import memcache, specify the Memecache service to operate on using the Client() method, and build a data connection object.

def __init__(self): #Connection memcached service, can contain multiple service ip self.mc = memcache.Client(['ip:11211'], debug=True)

Next, let's take new, query, update, append, delete, as examples, and talk about Python's methods for manipulating these data.

1. Add new operation

Add(key,value,timeout)

where, the parameter timeout indicates the data retention time, timeout will be automatically cleared

It should be noted that if the key to be inserted into the key-value pair does not exist in the original dataset, a new record will be added to the dataset; otherwise, the addition will fail and a warning prompt will be given.

def __add(self): """ add records :return: """ #Add a piece of data, if it already exists, add a failure, and raise an exception # time: save time, timeout will be cleared, unit is seconds, default is to save permanently MemCached: while expecting 'STORED', got unexpected response 'NOT_STORED' self.mc.add("name", "xag", time=60 * 5) self.mc.add("age", 23)

2. Query operation

Memcached query operations are divided into:

Query a single record through Key

Query multiple records through a list of multiple Keys

The corresponding method for a single record query is: get(key)

def __query_one(self, key): """ Query Single Record :return: """ result = self.mc.get(key) print ('single record query,key:', key, ",value:", result)

The corresponding method for querying multiple records is: get_multi(keys)

def __query_many(self, keys): """ Query multiple records :param keys: List of keys :return: """ values = self.mc.get_multi(keys) # dict, key-value pair print(type(values)) print ('multiple records query:', values)

3. Update operation

The update operation consists of three methods, namely:

Update a record, if the key does not exist, the update fails

Replace(key,value)

Update a record or add a new record if the key does not exist

Set(key,value)

Update multiple records. If there is a key that does not exist, add the corresponding key-value pair to the dataset.

Corresponding method: set_multi({key1:value1...})

Specific example codes are as follows:

def __update_default(self): """ Update data, including: replace, set, set_multi :return: """ # 1. Update a record with replace # self.mc.replace("name","xag1") # self.__ query_one("name") # 2. Use set to update records. If key does not exist, add a record. # set = replace + add # self.mc.set("name", "xag2") # self.__ query_one("name") # 3. Use set_multi to update multiple records. If there is a key that does not exist, create a new key-value pair. self.mc.set_multi({"name": "xag3", "age": 28}) self.__ query_many(["name", "age"])

4. Additional operations

appending is equivalent to modifying the value of a key, appending data at the head or tail

Of which:

append(str): appends a piece of data at the end

prepend(str): adds a new piece of data to the header

The code of practice is as follows:

def __update_append(self): """ Add updates, including: append, prepend :return: """ # 1、append # value Add content at the end self.mc.append("name","I am new") self.__ query_one("name") # 2、prepend #New content in the head self.mc.prepend("name","new content in header") self.__ query_one("name")

5. Delete operation

Similar to query operation, delete operation also supports deletion of single key value pair and deletion of multiple key value pairs.

delete(key): delete a key-value pair

delete_multi(keys): deletes all key-value pairs corresponding to all keys in the list

The corresponding operation codes are as follows:

def __delete(self): """ Delete data, including: delete, delete_multi :return:delete_multi """ # 1、delete #Delete a single key-value pair # self.mc.delete("age") # self.__ query_one("age") # 2、delete_multi #Delete multiple records keys = ["name","age"] self.mc.delete_multi(keys) self.__ query_many(keys) At this point, the study of "What are the methods of Python data processing" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report