Python备忘录

  1. 简述

    Python常用代码片段及相关工具类脚本

  2. 常用代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    # 字符串反转
    Str = input("Please input String:\n")
    RString = Str[::-1]
    print("This String Reversal:\n%s" % RString)



    # 首字母大写
    my_string = "my name is chaitanya baweja"
    new_string = my_string.title()
    print(new_string)



    # 在字符串中查找唯一元素
    my_string = "aavvccccddddeee"
    temp_set = set(my_string)
    new_string = ''.join(temp_set)
    print(new_string)


    # 次打印字符串或列表
    n = 3 # number of repetitions
    my_string = "abcd"
    my_list = [1,2,3]
    print(my_string*n)
    # abcdabcdabcd
    print(my_list*n)
    # [1,2,3,1,2,3,1,2,3]



    # 列表推导式
    original_list = [1,2,3,4]
    new_list = [2*x for x in original_list]
    print(new_list)
    # [2,4,6,8]



    # 变量交换
    a = 1
    b = 2
    a, b = b, a
    print(a) # 2
    print(b) # 1



    # 将字符串拆分为子字符串列表
    string_1 = "My name is Chaitanya Baweja"
    string_2 = "sample/ string 2"
    print(string_1.split())
    # ['My', 'name', 'is', 'Chaitanya', 'Baweja']
    print(string_2.split('/'))
    # ['sample', ' string 2']



    # 将字符串列表组合成单个字符串
    list_of_strings = ['My', 'name', 'is', 'Chaitanya', 'Baweja']
    print(','.join(list_of_strings))
    # My,name,is,Chaitanya,Baweja



    # 检查回文字符串
    my_string = "abcba"
    if my_string == my_string[::-1]:
    print("palindrome")
    else:
    print("not palindrome")
    # Output
    # palindrom



    # 列表中的元素统计


    from collections import Counter
    my_list = ['a','a','b','b','b','c','d','d','d','d','d']
    count = Counter(my_list) # defining a counter object
    print(count) # Of all elements
    # Counter({'d': 5, 'b': 3, 'a': 2, 'c': 1})
    print(count['b']) # of individual element
    # 3
    print(count.most_common(1)) # most frequent element
    # [('d', 5)]



    # 查找两个字符串是否为字母
    from collections import Counter
    str_1, str_2, str_3 = "acbde", "abced", "abcda"
    cnt_1, cnt_2, cnt_3 = Counter(str_1), Counter(str_2), Counter(str_3)
    if cnt_1 == cnt_2:
    print('1 and 2 anagram')
    if cnt_1 == cnt_3:
    print('1 and 3 anagram')



    # 使用 try-except-else 块
    a, b = 1,0
    try:
    print(a/b)
    # exception raised when b is 0
    except ZeroDivisionError:
    print("division by zero")
    else:
    print("no exceptions raised")
    finally:
    print("Run this always")



    # 使用枚举获取索引 / 值对
    my_list = ['a', 'b', 'c', 'd', 'e']
    for index, value in enumerate(my_list):
    print('{0}: {1}'.format(index, value))

    # 0: a
    # 1: b
    # 2: c
    # 3: d
    # 4: e



    # 检查对象的内存使用情况
    import sys
    num = 21
    print(sys.getsizeof(num))
    # In Python 2, 24
    # In Python 3, 28



    # 合并两个字典
    dict_1 = {'apple': 9, 'banana': 6}
    dict_2 = {'banana': 4, 'orange': 8}
    combined_dict = {dict_1, dict_2}
    print(combined_dict)
    # Output
    # {'apple': 9, 'banana': 4, 'orange': 8}



    # 执行一段代码所需的时间
    import time
    start_time = time.time()
    a, b = 1,2
    c = a+ b
    end_time = time.time()
    time_taken_in_micro = (end_time- start_time)*(106)
    print(" Time taken in micro_seconds: {0} ms").format(time_taken_in_micro)



    # 展平列表清单
    from iteration_utilities import deepflatten
    def flatten(l):
    return [item for sublist in l for item in sublist]
    l = [[1,2,3],[3]]
    print(flatten(l))
    # [1, 2, 3, 3]

    l = [[1,2,3],[4,[5],[6,7]],[8,[9,[10]]]]
    print(list(deepflatten(l, depth=3)))
    # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]



    # 从列表中随机取样
    import random
    my_list = ['a', 'b', 'c', 'd', 'e']
    num_samples = 2
    samples = random.sample(my_list,num_samples)
    print(samples)
    # [ 'a', 'e'] this will have any 2 random values



    # 数字化
    num = 123456
    # using map
    list_of_digits = list(map(int, str(num)))
    print(list_of_digits)
    # [1, 2, 3, 4, 5, 6]

    list_of_digits = [int(x) for x in str(num)]
    print(list_of_digits)
    # [1, 2, 3, 4, 5, 6]



    # 检查唯一性

    def unique(l):
    if len(l)==len(set(l)):
    print("All elements are unique")
    else:
    print("List has duplicates")

    unique([1,2,3,4])
    # All elements are unique

    unique([1,1,2,3])
    # List has duplicates
  3. 操作数据库
    • Mysql 数据库

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      #!/usr/bin/env python
      # -*- coding: UTF-8 -*-
      import pymysql
      from base_config import DB_HOST, DB_USER, DB_DATABASE, DB_PASSWORD, DB_PORT


      class MysqlHelp():
      """构造
      def __init__(self, host=DB_HOST, user=DB_USER, port=DB_PORT, password=DB_PASSWORD, database=DB_DATABASE):
      self.host = host
      self.user = user
      self.port = port
      self.password = password
      self.db = database
      """
      # 创建连接
      def open_coon(self,host=DB_HOST, user=DB_USER, port=DB_PORT, password=DB_PASSWORD, database=DB_DATABASE):
      self.coon = pymysql.connect(host=host, port=port, user=user, passwd=password, db=database)
      self.cursor = self.coon.cursor(cursor=pymysql.cursors.DictCursor)

      # 关闭连接
      def close(self):
      self.cursor.close()
      self.coon.cursor()

      # 调用语句
      def insert_delete_update(self, sql, params=[]):
      try:
      # self.open_coon()

      self.cursor.execute(sql, params)
      self.coon.commit()

      # self.close()

      return "OK"
      except Exception as erorr:
      return erorr

      # 查询 接收全部的返回结果行

      def select_fetchall(self, sql, params=[]):
      try:
      # self.open_coon()

      self.cursor.execute(sql, params)

      results = self.cursor.fetchall()

      self.coon.commit()

      # self.close()
      return results

      except Exception as erorr:
      return erorr
    • PostgreSQL,ssh通道操作

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      #!/usr/bin/env python
      # -*- coding: UTF-8 -*-

      import psycopg2
      import psycopg2.extras
      from sshtunnel import SSHTunnelForwarder
      import pandas as pd

      server = SSHTunnelForwarder(
      # 指定ssh登录的跳转机的address
      ssh_address_or_host=('ip', 22),
      ssh_username='root',
      # 设置密钥
      ssh_pkey='',
      ssh_private_key_password='',

      # 设置数据库服务地址及端口
      remote_bind_address=('***', 5432))
      server.start()
      conn = psycopg2.connect(database='kong',
      user='admin',
      password='****',
      host='127.0.0.1',
      port=server.local_bind_port)
      # cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
      sql = 'SELECT * FROM "public"."oauth2_tokens" LIMIT 1000 OFFSET 0'
      cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
      cur.execute(sql)
      rows = cur.fetchall()


      # df = pd.read_sql(sql, conn)
      conn.close()
      print(rows,type(rows))
      server.close()

      # conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432")