一、基础 1 2 3 4 5 6 7 message = "this is message" ageStr = '12' age = int (ageStr)
字符串 1 2 3 4 5 6 7 8 9 10 11 12 13 name = "ifangcy" name.title() name.upper() name.lower() name.rstrip() name.lstrip() name.strip() age = 30 message = "I'm " + str (age)
数字
列表 list 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 bicycles = ['trek' , 'cannondale' , 'redline' , 'specialized' ] bicycles[-1 ] if 'trek' in bicycles: print('in' ) if 'trek' not in bicycles: print('not in' ) if bicycles: print("非空" ) else : print('空' ) bicycles.append('ducati' ) bicycles.insert(0 , 'ducati' ) del bicycles[0 ]pop_value = bicycles.pop() pop_value2 = bicycles.pop(0 ) bicycles.remove('ducati' ) while 'ducati' in bicycles: bicycles.remove('ducati' ) bicycles.sort() bicycles.sort(reverse=True ) sorted (bicycles)sorted (bicycles, reverse=True )bicycles.reverse() reversed (bicycles)len (bicycles)for item in bicycles: print(item) numbers = list (range (1 , 6 )) numbers = list (range (30 )) list (range (1 , 6 , 2 ))min (numbers)max (numbers)sum (numbers)squares = [value**2 for value in range (1 , 11 )] players = ['charles' , 'mertina' , 'michael' , 'florence' , 'eli' ] players[1 :4 ] players[:4 ] players[1 :] players[:] players[-3 :]
元组 tuple 1 2 dimensions = (200 , 500 )
字典 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 alien = { "color" : "green" , "points" : 5 } alien['color' ] alien['x_position' ] = 10 alien['y_position' ] = 20 del alien['color' ]for key, value in alien.items(): print("key: " + key + ", value: " + value) for key in alien.keys(): print("key: " + key) for value in alien.values(): print("value: " + value) for key in sorted (alien.keys()): print("key: " + key) for value in set (alien.values): print("value: " + value)
条件 1 2 3 age = 30 age > 30 and age < 50 age > 20 or age < 10
函数 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 def describe_pet (animal_type, pet_name ): print("animal_type: " + animal_type + ", pet_name: " + pet_name) def describe_pet (animal_type, pet_name="harry" ): pass def make_pizza (size, *toppings ): pass make_pizza(12 , 'green peppers' , 'extra cheese' ) def make_pizza (size, **toppings ): pass make_pizza(12 , location="shanghai" , field="physics" ) describe_pet("hamster" , "harry" ) describe_pet(animal_type="hamster" , pet_name="harry" ) unprinted_designs = ["iphone case" , "robot pendant" ] completed_models = [] def change (unprinted_designs, completed_models ): while unprinted_designs: value = unprinted_designs.pop() completed_models.append(value) change(unprinted_designs, completed_models) change(unprinted_designs[:], completed_models)
类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class Car (): def __init__ (self, make, model, year ): self.make = make self.model = model self.year = year def get_descriptive_name (self ): pass class ElectricCar (Car ): def __init__ (self, make, model, year ): super ().__init__(self, make, model, year)
模块 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import pizzaimport pizza as newPizzafrom pizza import func1, func2from pizza import func1 as newFuncfrom pizza import Class1, Class2from pizza import *
文件操作 1 2 3 4 5 6 7 8 9 10 11 12 13 with open ('path/demo.txt' ) as file_object: contents = file_object.read() print(contents) for line in file_object: print(line.rstrip()) with open ('path/demo.txt' , 'w' ) as file_object: file_object.write("This is content" )
异常 1 2 3 4 5 6 7 8 9 try : answer = int (first_number) / int (second_number) except ZeroDivisionError: print('You cant divide by 0!' ) else : print(answer)
存储数据 1 2 3 4 5 6 7 8 9 10 players = ['charles' , 'mertina' , 'michael' , 'florence' , 'eli' ] with open ('demo.txt' , 'w' ) as file_object: json.dump(players, file_object) with open ('demo.txt' ) as file_object: newPlayers = json.load(file_object) print(newPlayers)
二、Django 在环境变量中配置参数
为不同环境使用不同 settings.py
写测试,测试覆盖度
三、PyCharm 远程开发 1、SSH 连接服务器 新增 SFTP 方式服务器:
3 处的 Root Path
是服务器上的项目上一级目录,记住是上一级目录。
这时候点击 Test Connection
如果提示连接失败,查看服务器是否开启了 ssh
服务:
1 2 3 4 5 6 sudo service ssh status sudo service ssh start sudo service ssh restart
如果确定 ssh
启动还是连接失败,提示:
1 sshd: no hostkeys available — exiting
修改配置
1 2 3 4 5 6 ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key PasswordAuthentication yes
1 处的 Local Path
是代码拉取到本机时存放的项目目录。
2 处的 Deployment Path
是服务器上的项目目录名称,这个地址会和上一步的 Root Path
合并一起用来查找服务器上的项目目录 ${Root Path}/${Deployment Path}
。
选择刚刚新建的服务器。
1 处 Host
即服务器 IP
地址
2 处 Run Browser
即服务器地址加端口
3 处 Python Interpreter
即上一步设置的 Python Interpreter
4 处 Working Directory
即本机项目目录