Does “\d” in regex mean a digit?

I found that in 123, \d matches 1 and 3 but not 2. I was wondering if \d matches a digit satisfying what kind of requirement? I am talking about Python style regex. Regular expression plugin in Gedit is using Python style regex. I created a text file with its content being Only 1 and 3 are matched by the regex \d; 2 is not. Generally for a sequence of digit numbers … Read more

Python function execution

If it is a function, it needs to return something. Otherwise, running it is kind of useless. So you probably need to say: You probably want to read more about functions in Python and when this would make sense (passing by reference, for example). This can be a good starting point: Python functions.

Get the data received in a Flask request

The docs describe the attributes available on the request object (from flask import request) during a request. In most common cases request.data will be empty because it’s used as a fallback: request.data Contains the incoming request data as string in case it came with a mimetype Flask does not handle. request.args: the key/value pairs in the URL query string request.form: the key/value … Read more

How do I parallelize a simple Python loop?

Using multiple threads on CPython won’t give you better performance for pure-Python code due to the global interpreter lock (GIL). I suggest using the multiprocessing module instead: Note that this won’t work in the interactive interpreter. To avoid the usual FUD around the GIL: There wouldn’t be any advantage to using threads for this example anyway. You want to … Read more

Get the data received in a Flask request

The docs describe the attributes available on the request object (from flask import request) during a request. In most common cases request.data will be empty because it’s used as a fallback: request.data Contains the incoming request data as string in case it came with a mimetype Flask does not handle. request.args: the key/value pairs in the URL query string request.form: the key/value … Read more

proper name for python * operator?

In Ruby and Perl 6 this has been called “splat”, and I think most people from those communities will figure out what you mean if you call it that. The Python tutorial uses the phrase “unpacking argument lists”, which is long and descriptive. It is also referred to as iterable unpacking, or in the case of **, dictionary unpacking.

How do I parallelize a simple Python loop?

Using multiple threads on CPython won’t give you better performance for pure-Python code due to the global interpreter lock (GIL). I suggest using the multiprocessing module instead: Note that this won’t work in the interactive interpreter. To avoid the usual FUD around the GIL: There wouldn’t be any advantage to using threads for this example anyway. You want to … Read more

How to find all occurrences of a substring?

There is no simple built-in string function that does what you’re looking for, but you could use the more powerful regular expressions: #[0, 5, 10, 15] If you want to find overlapping matches, lookahead will do that: If you want a reverse find-all without overlaps, you can combine positive and negative lookahead into an expression like this: #[1] … Read more