From b45fd9e28bf5894675bbe0151f1941bfb7797aa0 Mon Sep 17 00:00:00 2001 From: yuhuifeng Date: Thu, 17 Feb 2022 18:57:19 +0800 Subject: [PATCH] =?UTF-8?q?Python=20=EF=BC=88=E9=93=BE=E6=8E=A5=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E3=80=81=20=E7=AE=97=E6=B3=95=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- database/mongodb.py | 21 ++++++++++++++++++++ database/mongodb_request.py | 24 +++++++++++++++++++++++ leetcode/matrix_lucky_number.py | 9 +++++++++ "python_note/\347\274\226\347\240\201.py" | 4 ++++ 4 files changed, 58 insertions(+) create mode 100644 database/mongodb.py create mode 100644 database/mongodb_request.py create mode 100644 leetcode/matrix_lucky_number.py create mode 100644 "python_note/\347\274\226\347\240\201.py" diff --git a/database/mongodb.py b/database/mongodb.py new file mode 100644 index 0000000..2bc736b --- /dev/null +++ b/database/mongodb.py @@ -0,0 +1,21 @@ +import pymongo + +# Replace the uri string with your MongoDB deployment's connection string. +conn_str = "mongodb://root:123456@192.168.247.135:27017/?authSource=admin" +# set a 5-second connection timeout +client = pymongo.MongoClient(conn_str, serverSelectionTimeoutMS=5000) +try: + db = client.admin + print(db.name) + collection = db['students'] + student = { + 'id': '201701014', + 'name': 'Jordan', + 'age': 20, + 'gender': 'male' + } + result = collection.insert_one(student) + print(db['students'].info) +except Exception as e: + print("Unable to connect to the server.") + print(f"-{e}") diff --git a/database/mongodb_request.py b/database/mongodb_request.py new file mode 100644 index 0000000..15de6a2 --- /dev/null +++ b/database/mongodb_request.py @@ -0,0 +1,24 @@ +import requests +import pymongo + + +class MongodbDataBase: + conn_str = "mongodb://root:123456@192.168.247.135:27017/?authSource=admin" + + def __init__(self): + self.client = pymongo.MongoClient(self.conn_str, serverSelectionTimeoutMS=5000) + try: + db = client.admin + print(db.name) + collection = db['students'] + student = { + 'id': '201701014', + 'name': 'Jordan', + 'age': 20, + 'gender': 'male' + } + result = collection.insert_one(student) + print(db['students'].info) + except Exception as e: + print("Unable to connect to the server.") + print(f"-{e}") diff --git a/leetcode/matrix_lucky_number.py b/leetcode/matrix_lucky_number.py new file mode 100644 index 0000000..75a1eaa --- /dev/null +++ b/leetcode/matrix_lucky_number.py @@ -0,0 +1,9 @@ + +class Solution: + def luck_number(self, matrix: list(list[int])) -> list[int]: + ans = [] + for row in matrix: + for j, x in enumerate(row): + if max(r[j] for r in matrix) <= x <= min(row): + ans.append(x) + return ans \ No newline at end of file diff --git "a/python_note/\347\274\226\347\240\201.py" "b/python_note/\347\274\226\347\240\201.py" new file mode 100644 index 0000000..9a85a3d --- /dev/null +++ "b/python_note/\347\274\226\347\240\201.py" @@ -0,0 +1,4 @@ +#!/use/bin/python +# coding=utf-8 + +print("你好") \ No newline at end of file -- Gitee