How to use urllib with username/password authentication in python 3?

Thankfully to you guys I finally figured out the way it works. Here is my code: request = urllib.request.Request(‘http://mysite/admin/index.cgi?index=127’) base64string = base64.b64encode(bytes(‘%s:%s’ % (‘login’, ‘password’),’ascii’)) request.add_header(“Authorization”, “Basic %s” % base64string.decode(‘utf-8’)) result = urllib.request.urlopen(request) resulttext = result.read() After all, there is one more difference with urllib: the resulttext variable in my case had the type of … Read more

Python 3.4 on Sublime Text 3

You need to provide the full path to python3, since Sublime Text does not read your ~/.bash_profile file. Open up Terminal, type which python3, and use that full path: { “cmd”: [“path/to/python3”, “$file”], “selector”: “source.python”, “file_regex”: “file \”(…*?)\”, line ([0-9]+)” }

Fit sigmoid function (“S” shape curve) to data using Python

After great help from @Brenlla the code was modified to: def sigmoid(x, L ,x0, k, b): y = L / (1 + np.exp(-k*(x-x0))) + b return (y) p0 = [max(ydata), np.median(xdata),1,min(ydata)] # this is an mandatory initial guess popt, pcov = curve_fit(sigmoid, xdata, ydata,p0, method=’dogbox’) The parameters optimized are L, x0, k, b, who are … Read more

How does one insert statistical annotations (stars or p-values) into matplotlib / seaborn plots?

Here how to add statistical annotation to a Seaborn box plot: import seaborn as sns, matplotlib.pyplot as plt tips = sns.load_dataset(“tips”) sns.boxplot(x=”day”, y=”total_bill”, data=tips, palette=”PRGn”) # statistical annotation x1, x2 = 2, 3 # columns ‘Sat’ and ‘Sun’ (first column: 0, see plt.xticks()) y, h, col = tips[‘total_bill’].max() + 2, 2, ‘k’ plt.plot([x1, x1, x2, … Read more

How i can get new ip from tor every requests in threads?

If you want different IPs for each connection, you can also use Stream Isolation over SOCKS by specifying a different proxy username:password combination for each connection. With this method, you only need one Tor instance and each requests client can use a different stream with a different exit node. In order to set this up, … Read more

How to make python 3 print() utf8

Clarification: TestText = “Test – āĀēĒčČ..šŠūŪžŽ” # this not UTF-8…it is a Unicode string in Python 3.X. TestText2 = TestText.encode(‘utf8′) # this is a UTF-8-encoded byte string. To send UTF-8 to stdout regardless of the console’s encoding, use the its buffer interface, which accepts bytes: import sys sys.stdout.buffer.write(TestText2)

Flexible appending new data to yaml files

You can use PyYAML’s low-level event interface. Assuming you have an input YAML file and want to write the modifications to an output YAML file, you can write a function that goes through PyYAML’s generated event stream and inserts the requested additional values at the specified locations: import yaml from yaml.events import * class AppendableEvents: … Read more

How do I integrate custom exception handling with the FastAPI exception handling?

Your custom exception can have any custom attributes that you want. Let’s say you write it this way: class ExceptionCustom(HTTPException): pass in your custom handler, you can do something like def exception_404_handler(request: Request, exc: HTTPException): return JSONResponse(status_code=status.HTTP_404_NOT_FOUND, content={“message”: exc.detail}) Then, all you need to do is to raise the exception this way: raise ExceptionCustom(status_code=404, detail=”error … Read more