Python学习笔记

本文介绍了Python在Web开发领域的应用,包括通过mod_wsgi模块和Gunicorn运行Web程序,以及Django、Flask等流行Web框架。同时,探讨了gevent库在高性能网络开发中的作用,列举了爬虫库如lxml、BeautifulSoup和Scrapy,以及GUI开发库Tkinter、wxPython和PyQt。此外,还提到了Python在性能测试、Windows身份验证、字符串处理、数组比较等方面的实用代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过mod_wsgi模块,Apache可以运行用Python编写的Web程序

使用Python语言编写的 Gunicorn作为Web服务器,也能够运行Python语言编写的Web程序

Web框架,如Django、Pyramid、TurboGears、web2py、Zope、Flask、tornoda

gevent这个流行的第三方库,同样能够支持高性能高并发的网络开发。

爬虫相关的库有lxml、re、urllib2、BeautifulSoup、scrapy等

Python本身包含的Tkinter库能够支持简单的GUI开发,但是越来越多的Python程序员选择wxPython或者PyQt来开发跨平台的桌面软件。使用它们开发的桌面软件运行速度快,与用户的桌面环境相契合。通过PyInstaller还能将程序发布为独立的安装程序包。

Python的性能测试库multi-mechanize和locustio、funkload等模块具备强大的编程能力,通常扩展性和执行效率远强于Loadrunner和Jmeter。

 

python requests实现windows身份验证登录

来源:https://www.cnblogs.com/xbzhu/p/7743584.html 

1.安装ntlm  https://github.com/requests/requests-ntlm

pip install requests_ntlm

2.使用

import requests
from requests_ntlm import HttpNtlmAuth

requests.get("http://ntlm_protected_site.com",auth=HttpNtlmAuth('domain\\username','password'))

 implement a difference function, which subtracts one list from another and returns the result.

It should remove all values from list a, which are present in list b.

def array_diff(a, b): return [x for x in a if x not in b]

 

Take 2 strings s1 and s2 including only letters from ato z. Return a new sorted string, the longest possible, containing distinct letters,

  • each taken only once - coming from s1 or s2. #Examples: ``` a = "xyaabbbccccdefww" b = "xxxxyyyyabklmopq" longest(a, b) -> "abcdefklmopqwxy"

a = "abcdefghijklmnopqrstuvwxyz" longest(a, a) -> "abcdefghijklmnopqrstuvwxyz" ```

Test.describe("longest")
Test.it("Basic tests")
Test.assert_equals(longest("aretheyhere", "yestheyarehere"), "aehrsty")
Test.assert_equals(longest("loopingisfunbutdangerous", "lessdangerousthancoding"), "abcdefghilnoprstu")
Test.assert_equals(longest("inmanylanguages", "theresapairoffunctions"), "acefghilmnoprstuy")

def longest(s1, s2):
#print(sorted(s1+s2))
return(''.join(sorted([ x for x in set(s1)|set(s2)])))

 

def longest(a1, a2): return "".join(sorted(set(a1 + a2)))

 

Given two arrays a and b write a function comp(a, b) (compSame(a, b) in Clojure) that checks whether the two arrays have the "same" elements, with the same multiplicities. "Same" means, here, that the elements in b are the elements in a squared, regardless of the order.

Examples

Valid arrays

a = [121, 144, 19, 161, 19, 144, 19, 11]  
b = [121, 14641, 20736, 361, 25921, 361, 20736, 361]

comp(a, b) returns true because in b 121 is the square of 11, 14641 is the square of 121, 20736 the square of 144, 361 the square of 19, 25921 the square of 161, and so on. It gets obvious if we write b's elements in terms of squares:

a = [121, 144, 19, 161, 19, 144, 19, 11] 
b = [11*11, 121*121, 144*144, 19*19, 161*161, 19*19, 144*144, 19*19]

Invalid arrays

If we change the first number to something else, comp may not return true anymore:

a = [121, 144, 19, 161, 19, 144, 19, 11]  
b = [132, 14641, 20736, 361, 25921, 361, 20736, 361]

comp(a,b) returns false because in b 132 is not the square of any number of a.

a = [121, 144, 19, 161, 19, 144, 19, 11]  
b = [121, 14641, 20736, 36100, 25921, 361, 20736, 361]

comp(a,b) returns false because in b 36100 is not the square of any number of a.

Remarks

a or b might be [] (all languages except R, Shell). a or b might be nil or null or None or nothing (except in Haskell, Elixir, C++, Rust, R, Shell).

If a or b are nil (or null or None), the problem doesn't make sense so return false.

If a or b are empty the result is evident by itself.

 

Best Pracice:

def comp(array1, array2): try: return sorted([i ** 2 for i in array1]) == sorted(array2) except: return False

 

this time we want to write calculations using functions and get the results. Let's have a look at some examples:

JavaScript:

seven(times(five())); // must return 35
four(plus(nine())); // must return 13
eight(minus(three())); // must return 5
six(dividedBy(two())); // must return 3

Ruby:

seven(times(five)) # must return 35
four(plus(nine)) # must return 13
eight(minus(three)) # must return 5
six(divided_by(two)) # must return 3

Requirements:

  • There must be a function for each number from 0 ("zero") to 9 ("nine")
  • There must be a function for each of the following mathematical operations: plus, minus, times, dividedBy (divided_by in Ruby)
  • Each calculation consist of exactly one operation and two numbers
  • The most outer function represents the left operand, the most inner function represents the right operand
  • Divison should be integer division, e.g eight(dividedBy(three()))/eight(divided_by(three)) should return 2, not 2.666666...

Best Practice

def zero(f = None): return 0 if not f else f(0)

def one(f = None): return 1 if not f else f(1)

def two(f = None): return 2 if not f else f(2)

def three(f = None): return 3 if not f else f(3)

def four(f = None): return 4 if not f else f(4)

def five(f = None): return 5 if not f else f(5)

def six(f = None): return 6 if not f else f(6)

def seven(f = None): return 7 if not f else f(7)

def eight(f = None): return 8 if not f else f(8)

def nine(f = None): return 9 if not f else f(9)

 

def plus(y): return lambda x: x+y

def minus(y): return lambda x: x-y

def times(y): return lambda x: x*y

def divided_by(y): return lambda x: x/y

 

 

Build Tower

Build Tower by the following given argument:
number of floors (integer and always greater than 0).

Tower block is represented as *

  • Python: return a list;
  • JavaScript: returns an Array;
  • C#: returns a string[];
  • PHP: returns an array;
  • C++: returns a vector<string>;
  • Haskell: returns a [String];
  • Ruby: returns an Array;

Have fun!


for example, a tower of 3 floors looks like below

[
  '  *  ', 
  ' *** ', 
  '*****'
]

and a tower of 6 floors looks like below

[
  '     *     ', 
  '    ***    ', 
  '   *****   ', 
  '  *******  ', 
  ' ********* ', 
  '***********'
]

Best Practice:
def tower_builder(n): return [("*" * (i*2-1)).center(n*2-1) for i in range(1, n+1)]




Given: an array containing hashes of names

Return: a string formatted as a list of names separated by commas except for the last two names, which should be separated by an ampersand.

Example:

namelist([ {'name': 'Bart'}, {'name': 'Lisa'}, {'name': 'Maggie'} ]) # returns 'Bart, Lisa & Maggie' namelist([ {'name': 'Bart'}, {'name': 'Lisa'} ]) # returns 'Bart & Lisa' namelist([ {'name': 'Bart'} ]) # returns 'Bart' namelist([]) # returns '' 

Note: all the hashes are pre-validated and will only contain A-Z, a-z, '-' and '.'.

 

Best Practice:

def namelist(names): if len(names) > 1: return '{} & {}'.format(', '.join(name['name'] for name in names[:-1]), names[-1]['name']) elif names: return names[0]['name'] else: return ''

 

 


转载于:https://www.cnblogs.com/jdjh/p/10094879.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值