Python学习笔记

经过mod_wsgi模块,Apache能够运行用Python编写的Web程序javascript

使用Python语言编写的 Gunicorn做为Web服务器,也可以运行Python语言编写的Web程序html

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

gevent这个流行的第三方库,一样可以支持高性能高并发的网络开发。python

爬虫相关的库有lxml、re、urllib二、BeautifulSoup、scrapy等git

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

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

 

python requests实现windows身份验证登陆

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

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

pip install requests_ntlmwindows

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 ''

相关文章
相关标签/搜索