作者文章归档:陈海峰

[ python面试考试[1] ]


# Scoring criteria:
# 5: pythonic     meeting grade 4, and coding in pythonic way
# 4: complete     correct logic and result. no bug
# 3: minor bug        very close to correct answer. missing corner case or a bit format/cosmetic issue
# 2: medium bug   algorithm can run but answer not right, missin

< Read more >

[ traceback模块 ]


异常日志记录

日志保存到文本

import traceback

a = ["hello", "yoyo"]
try:
    print(a[4])
except Exception as e:
    fp = open('log.txt', 'a')
    traceback.print_exc(file=fp)
    fp.close()

如果不想写入到文件,也可以暂时写入到内存(StringIO),后面需要用到的时候再读出来

from io import StringIO
import traceback

< Read more >

[ getopt函数使用 ]


import getopt

使用语法:getopt.getopt(args, options[, long_options])

参数说明:options是单字符,long_options表示字符串

返回结果:是一个list,每个list的项为(选项,值)的元组对

具体实例:#ping -c 12 -6 -s 1024 –help –size 1024

处理结果:

getopt.getopt(sys.argv[1:], ”c:s:6”, [“size=”,”help”])

[('-c', '12'), ('-6', ''), ('-s', '1024'), (“—help”,””), (“--s

< Read more >

[ Mysql面试题目 ]


1. 聚簇索引, 和 非聚簇索引

聚簇索引是由每张表的主键来建立的, 而非聚簇索引是按照非组件列来建立的。
在innodb中没张表只有一个聚簇索引, 而可以有多改非聚簇索引。
聚簇索引的叶子节点存储的是整行的数据, 而非聚簇索引叶子节点储存的是主键的值和对应行的指针

2. 聚簇索引是怎么构建的

在mysql中, 当你指定了表的主键时, mysql会自动为该主键创建聚簇索引。如果没有指定主键,则会自动为第一个非空唯一索引创建聚簇索引。如果没有这样的索引, mysql会自动为innodb表创建一个cen_clust_index的隐藏的聚簇索引。

3. 回表索引和索引覆盖

回表指的是, my

< Read more >

[ Django面试题[1] ]


  1. django, flask, tornado的比较
django框架设计为全栈的web框架,功能齐全, orm, 中间件, 模板语言,表单, 而且生态丰富有类型drf等.., 但是学习成本高
flask框架轻量级, 只有默认的wsgi依赖,与jinja2模板语言依赖, 学习成本低
tornado是异步框架, 非常适合高并发的场景下使用
  1. 什么是wsgi
是python的网络服务网关接口, 规定了web与python语言之间的通信
  1. django的生命周期

  2. django的内置组件

Admin site生成了强大的后台管理系统, 可以方便的管理数据库中的数据
ORM: 方便

< Read more >

[ Mysql测试用 ]


INSERT INTO `tb_user` (`password`, `last_login`, `is_superuser`, `username`, `first_name`, `last_name`, `email`, `is_staff`, `is_active`, `date_joined`, `create_time`, `update_time`, `version`, `delete_token`, `phone`, `gender`, `token`, `status`)
VALUES ('pbkdf2_sha256$390000$GUWtpz0MfJtNvRGzVI

< Read more >

[ @property配合@xx.setter ]


@*.setter

python中的@*.setter装饰器可以总结为两个作用:

对要存入的数据进行预处理
设置可读属性(不可修改)

注意:@.setter装饰器必须在@property装饰器的后面,且两个被修饰的函数的名称必须保持一致, 即为函数名称。

示例1

我们使用和上文一样的测试类,现在我们需要在数据存入user.age前先对其+5,利用@*.setter装饰器我们可以这样写:

class User():
    def __init__(self, name, age):
        self.name = name
        self._age = age

    

< Read more >