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

相關文章
相關標籤/搜索