eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I am confused why FooBar.__mro__ doesn't show <class '__main__.Parent'> like the above two. I still don't know why after some digging into the CPython source code. from typing import NamedTuple from collections import namedtuple A = namedtuple('A', ['test']) class B(NamedTuple): test: str class Parent: pass class Foo(Parent, A): pass class Bar(Parent, B): pass class FooBar(Parent, NamedTuple): pass print(Foo.__mro__) # prints (<class '__main__.Foo'>, <class '__main__.Parent'>, <class '__main__.A'>, <class 'tuple'>, <class 'object'>) print(Bar.__mro__) # prints (<class '__main__.Bar'>, <class '__main__.Parent'>, <class '__main__.B'>, <class 'tuple'>, <class 'object'>) print(FooBar.__mro__) # prints (<class '__main__.FooBar'>, <class 'tuple'>, <class 'object'>) # expecting: (<class '__main__.FooBar'>, <class '__main__.Parent'>, <class 'tuple'>, <class 'object'>) 

Answer: This is because typing.NamedTuple is not really a proper type. It is a class. But its singular purpose is to take advantage of meta-class magic to give you a convenient nice way to define named-tuple types. And named-tuples derive from tuple directly. Note, unlike most other classes, from typing import NamedTuple class Foo(NamedTuple): pass print(isinstance(Foo(), NamedTuple))  prints False. This is because in NamedTupleMeta essentially introspects __annotations__ in your class to eventually use it to return a class created by a call to collections.namedtuple: def _make_nmtuple(name, types): msg = "NamedTuple('Name', [(f0, t0), (f1, t1), ...]); each t must be a type" types = [(n, _type_check(t, msg)) for n, t in types] nm_tpl = collections.namedtuple(name, [n for n, t in types]) # Prior to PEP 526, only _field_types attribute was assigned. # Now __annotations__ are used and _field_types is deprecated (remove in 3.9) nm_tpl.__annotations__ = nm_tpl._field_types = dict(types) try: nm_tpl.__module__ = sys._getframe(2).f_globals.get('__name__', '__main__') except (AttributeError, ValueError): pass return nm_tpl class NamedTupleMeta(type): def __new__(cls, typename, bases, ns): if ns.get('_root', False): return super().__new__(cls, typename, bases, ns) types = ns.get('__annotations__', {}) nm_tpl = _make_nmtuple(typename, types.items()) ... return nm_tpl  And of course, namedtuple essentially just creates a class which derives from tuple. Effectively, any other classes your named-tuple class derives from in the class definition statement are ignored, because this subverts the usual class machinery. It might feel wrong, in a lot of ways it is ugly, but practicality beats purity. And it is nice and practical to be able to write things like: class Foo(NamedTuple): bar: int baz: str
Question: I'm relatively new in all that and I have a problem with the row of the actions. Say that you created a directory and you want a python virtual environment for some project and clone a few git repos (say, from GitHub). Then you cd in that directory and create a virtual environment using the venv module (for python3). To do so you run the following command,  python3 -m venv my_venv  which will create in your directory a virtual environment called my_env. To activate this environment you run the following.  source ./my_env/bin/activate  If in addition inside that directory you have a requirements.txt file you can run,  pip3 install -r ./requirements.txt  to install your various dependencies and packages with pip3. Now this is where I'm getting confused. If you want to clone git repos where exactly you do that? In the same directory you just run git clone and creates the git repos or you need to cd in another folder. In order to let python venv pick up the cloned repos is the above enough, or venv must be installed after you have cloned the repos in your directory?

Answer: First of all, you need to understand what is virtual environments, when you understand what it is for, the order of actions will be more clear.  Python applications will often use packages and modules that don’t come as part of the standard library. Applications will sometimes need a specific version of a library, because the application may require that a particular bug has been fixed or the application may be written using an obsolete version of the library’s interface. This means it may not be possible for one Python installation to meet the requirements of every application. If application A needs version 1.0 of a particular module but application B needs version 2.0, then the requirements are in conflict and installing either version 1.0 or 2.0 will leave one application unable to run. The solution for this problem is to create a virtual environment, a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages. Different applications can then use different virtual environments. To resolve the earlier example of conflicting requirements, application A can have its own virtual environment with version 1.0 installed while application B has another virtual environment with version 2.0. If application B requires a library be upgraded to version 3.0, this will not affect application A’s environment.  ※ Reference: 12. Virtual Environments and Packages  Generally, the following order is the most appropriated.  $ git clone <Project A> # Cloning project repository $ cd <Project A> # Enter to project directory $ python3 -m venv my_venv # If not created, creating virtualenv $ source ./my_venv/bin/activate # Activating virtualenv (my_venv)$ pip3 install -r ./requirements.txt # Installing dependencies (my_venv)$ deactivate # When you want to leave virtual environment  All installed dependencies at step 5 will be unavailable after you leave virtual environment.
Question: I have an async function that listens on a specific port. I want to run the function on a few ports at a time and when the user wants to stop listening on a specific port, stop the function listening on that port. Previously I was using the asyncio library for this task and I tackled this problem by creating tasks with a unique id as their name. asyncio.create_task(Func(), name=UNIQUE_ID)  Since trio uses nurseries to spawn tasks, I can see the running tasks by using nursery.child_tasks but the tasks don't have a way to name them and even a way to cancel a task on demand TL;DR Since trio doesn't has a cancel() function that cancels a specific task, how can I manually cancel a task.

Answer: Easy. You create a cancel scope, return that from the task, and cancel this scope when required: async def my_task(task_status=trio.TASK_STATUS_IGNORED): with trio.CancelScope() as scope: task_status.started(scope) pass # do whatever async def main(): async with trio.open_nursery() as n: scope = await n.start(my_task) pass # do whatever scope.cancel() # cancels my_task()  The magic part is await n.start(task), which waits until the task calls task_status.started(x) and returns the value of x (or None if you leave that empty).
Question: In VS Code's Python extension I sometimes find that autocompletion can include options for things that are not yet imported in the file I'm editing. When selecting one of those options imports sometimes get inserted at the top of the module without notification. While I can see the utility in this feature I don't really like the behavior since it does this silently and puts them in alphabetical order disregarding any other sorting I may choose. Is there a way to disable this feature?

Answer: Using Pylance (as of v2020.8.0), you can disable this by setting "python.analysis.autoImportCompletions": false  https://github.com/microsoft/pylance-release/blob/master/CHANGELOG.md#202080-5-august-2020
Question: I see the functions for uploading in an API, but I don't see how to download. Am I missing something? I want to create an API for a file download site. Is there a different API I should be using? from typing import List from fastapi import FastAPI, Query app = FastAPI() PATH "some/path" @app.get("/shows/") def get_items(q: List[str] = Query(None)): ''' Pass path to function. Returns folders and files. ''' results = {} query_items = {"q": q} entry = PATH + "/".join(query_items["q"]) + "/" dirs = os.listdir(entry) results["folders"] = [val for val in dirs if os.path.isdir(entry+val)] results["files"] = [val for val in dirs if os.path.isfile(entry+val)] results["path_vars"] = query_items["q"] return results  Here is the sample bit of code for python to fetch files and dirs for a path, you can return the path as a list with a new entry in a loop to go deeper into a file tree. Passing a file name should trigger a download function, but I cant seem to get a download func going.

Answer: This worked For me from starlette.responses import FileResponse return FileResponse(file_location, media_type='application/octet-stream',filename=file_name)  This will download the file with filename
Question: I have a point cloud which looks something like this:  The red dots are the points, the black dots are the red dots projected to the xy plane. Although it is not visible in the plot, each point also has a value, which is added to the given pixel when the point is moved to the xy plane. The points are represented by a numpy (np) array like so: points=np.array([[x0,y0,z0,v0],[x1,y1,z1,v1],...[xn,yn,zn,vn]])  The obvious way to put these points into some image would be through a simple loop, like so: image=np.zeros(img_size) for point in points: #each point = [x,y,z,v] image[tuple(point[0:2])] += point[3]  Now this works fine, but it is very slow. So I was wondering if there is some way using vectorization, slicing and other clever numpy/python tricks of speeding it up, since in reality I have to this many times for large point clouds. I had come up with something using np.put: def points_to_image(xs, ys, vs, img_size): img = np.zeros(img_size) coords = np.stack((ys, xs)) #put the 2D coordinates into linear array coordinates abs_coords = np.ravel_multi_index(coords, img_size) np.put(img, abs_coords, ps) return img  (in this case the points are pre-split into vectors containing the x, y and v components). While this works fine, it of course only puts the last point to each given pixel, i.e. it is not additive. Many thanks for your help!

Answer: Courtesy of @Paul Panzer: def points_to_image(xs, ys, ps, img_size): coords = np.stack((ys, xs)) abs_coords = np.ravel_multi_index(coords, img_size) img = np.bincount(abs_coords, weights=ps, minlength=img_size[0]*img_size[1]) img = img.reshape(img_size)  On my machine, the loop version takes 0.4432s vs 0.0368s using vectorization. So a neat 12x speedup. ============ EDIT ============ Quick update: using torch... def points_to_image_torch(xs, ys, ps, sensor_size=(180, 240)): xt, yt, pt = torch.from_numpy(xs), torch.from_numpy(ys), torch.from_numpy(ps) img = torch.zeros(sensor_size) img.index_put_((yt, xt), pt, accumulate=True) return img  I get all the way down to 0.00749. And that's still all happening on CPU, so 59x speedup vs python loop. I also had a go at running it on GPU, it doesn't seem to make a difference in speed, I guess with accumulate=True it's probably using some sort of atomics on the GPU that slows it all down.
Question: trying to figure out how to modify the line properties (color, thickness, style etc) of the quartiles in a seaborn violinplot. Example code from their website: import seaborn as sns sns.set(style="whitegrid") tips = sns.load_dataset("tips") ax = sns.violinplot(x="day", y="total_bill", hue="sex", data=tips, palette="Set2", split=True,linestyle=':', scale="count", inner="quartile")   The desired outcome would be to be able to change e.g. the color of the two parts of the violinplot individually as for example like this to improve readability:  How can I do this? Thankful for any insights UPDATE: Based on the response by @kynnem the following can be used to change the median and quartile lines separately: import seaborn as sns sns.set(style="whitegrid") tips = sns.load_dataset("tips") ax = sns.violinplot(x="day", y="total_bill", hue="sex", data=tips, palette="Set2", split=True,linestyle=':', scale="count", inner="quartile") for l in ax.lines: l.set_linestyle('--') l.set_linewidth(0.6) l.set_color('red') l.set_alpha(0.8) for l in ax.lines[1::3]: l.set_linestyle('-') l.set_linewidth(1.2) l.set_color('black') l.set_alpha(0.8)  Result: 

Answer: You can access the lines from your ax variable using the following to set line type, color, and saturation:  for l in ax.lines: l.set_linestyle('-') l.set_color('black') l.set_alpha(0.8)  This creates a solid black line for all horizontal lines. If you can figure out which of the lines in ax correspond with your lines of interest, you can then specify different colors and styles as you wish
Question:  If i run this code, it will hang without throwing ZeroDivisionError.  If i move await asyncio.gather(*tasks, return_exceptions=True) above await queue.join(), it will finally throw ZeroDivisionError and stop. If i then comment out 1 / 0 and run, it will execute everything, but will hang in the end.  Now the question is, how can i achive both:   Being able to see unexpected exceptions as in the case 2 above, and...  Actually stop when all task are done in the Queue  . import asyncio import random import time async def worker(name, queue): while True: print('Get a "work item" out of the queue.') sleep_for = await queue.get() print('Sleep for the "sleep_for" seconds.') await asyncio.sleep(sleep_for) # Error on purpose 1 / 0 print('Notify the queue that the "work item" has been processed.') queue.task_done() print(f'{name} has slept for {sleep_for:.2f} seconds') async def main(): print('Create a queue that we will use to store our "workload".') queue = asyncio.Queue() print('Generate random timings and put them into the queue.') total_sleep_time = 0 for _ in range(20): sleep_for = random.uniform(0.05, 1.0) total_sleep_time += sleep_for queue.put_nowait(sleep_for) print('Create three worker tasks to process the queue concurrently.') tasks = [] for i in range(3): task = asyncio.create_task(worker(f'worker-{i}', queue)) tasks.append(task) print('Wait until the queue is fully processed.') started_at = time.monotonic() print('Joining queue') await queue.join() total_slept_for = time.monotonic() - started_at print('Cancel our worker tasks.') for task in tasks: task.cancel() print('Wait until all worker tasks are cancelled.') await asyncio.gather(*tasks, return_exceptions=True) print('====') print(f'3 workers slept in parallel for {total_slept_for:.2f} seconds') print(f'total expected sleep time: {total_sleep_time:.2f} seconds') asyncio.run(main()) 

Answer: There are several ways to approach this, but the central idea is that in asyncio, unlike in classic threading, it is straightforward to await multiple things at once. For example, you can await queue.join() and the worker tasks, whichever completes first. Since workers don't complete normally (you cancel them later), a worker completing means that it has raised. # convert queue.join() to a full-fledged task, so we can test # whether it's done queue_complete = asyncio.create_task(queue.join()) # wait for the queue to complete or one of the workers to exit await asyncio.wait([queue_complete, *tasks], return_when=asyncio.FIRST_COMPLETED) if not queue_complete.done(): # If the queue hasn't completed, it means one of the workers has # raised - find it and propagate the exception. You can also # use t.exception() to get the exception object. Canceling other # tasks is another possibility. for t in tasks: if t.done(): t.result() # this will raise
Question: I'm working with DRF and came across this issue. I have a third-party view which I'm importing in my urls.py file like this : from some_package import some_view urlpatterns = [ path('view/',some_view) ]  but the issue I'm facing is since I have enabled default permission classes in my settings.py like this: REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.TokenAuthentication', ), 'DEFAULT_PERMISSION_CLASSES':( 'rest_framework.permissions.IsAuthenticated', ), }  now when I call that view using the url , it gives me authentication error as I'm not providing token .Is there a way I can bypass authentication error without having to make changes in view directly,I know that we can remove permission for that particular view , but for that I'll have to make changes to that some_view function code. But I don't want to do that,let's say we don't have access to that function we can only pass data and receive response. How can I bypass authentication without having to change that functions code . I tried searching but couldn't find what I'm looking for.  I was assuming that there might be someway we can do that from urls.py like specifying any parameter or something like that which make that particular view to bypass authentication without having to change functions code. somthing like this : from some_package import some_view urlpatterns = [ path('view/',some_view,"some_parameter") #passing someparameter from here or something like that ]  Is it possible what I'm looking for ? Thanks in advance :)

Answer: So, the most appropriate way for third-party views is to use decorators by defining them inside your urls.py: Case 1 I assume that some_view is a class inherited from rest_framework.views.APIView: urls.py from django.urls import path from rest_framework.decorators import permission_classes, authentication_classes from rest_framework.permissions import AllowAny from some_package import some_view urlpatterns = [ path('', authentication_classes([])(permission_classes([AllowAny])(some_view)).as_view()) ]  Case 2 I assume that some_view is a simple Django view function and you need to define it for GET method:  urls.py from django.urls import path from rest_framework.decorators import api_view, permission_classes, authentication_classes from rest_framework.permissions import AllowAny from some_package import some_view urlpatterns = [ path('', api_view(['GET'])(authentication_classes([])(permission_classes([AllowAny])(some_view)))) ]  Case 3 I assume that some_view is an api_view decorated DRF view function. This is the hardest and most probably the most impossible part because you have to undecorate the previous api_view decorator. If view function is decorated with api_view, then it is already converted into Django view function so neither permision_classes nor authentication_classes can be appended to class:
Question: I am trying to find a way to programmatically solve a 24-piece sliding puzzle in a reasonable amount of time and moves. Here is an example of the solved state in the puzzle I am describing:   I have already found that the IDA* algorithm works fairly well to accomplish this for a 15-puzzle (4x4 grid). The IDA* algorithm is able to find the lowest number of moves for any 4x4 sliding puzzle in a very reasonable amount of time. I ran an adaptation of this code to test 4x4 sliding puzzles and was able to significantly reduce runtime further by using PyPy. Unfortunately, when this code is adapted for 5x5 sliding puzzles it runs horribly slow. I ran it for over an hour and eventually just gave up on seeing it finish, whereas it ran for only a few seconds on 4x4 grids. I understand this is because the number of nodes that need to searched goes up exponentially as the grid increases. However, I am not looking to find the optimal solution to a 5x5 sliding puzzle, only a solution that is close to optimal. For example, if the optimal solution for a given puzzle was 120 moves, then I would be satisfied with any solution that is under 150 moves and can be found in a few minutes. Are there any specific algorithms that might accomplish this?

Answer: It as been proved that finding the fewest number of moves of n-Puzzle is NP-Complete, see Daniel Ratner and Manfred Warmuth, The (n2-1)-Puzzle and Related Relocation Problems, Journal of Symbolic Computation (1990) 10, 111-137. Interesting facts reviewed in Graham Kendall, A Survey of NP-Complete Puzzles, 2008:  The 8-puzzle can be solved with A* algorithm; The 15-puzzle cannot be solved with A* algorithm but the IDA* algorithm can; Optimal solutions to the 24-puzzle cannot be generated in reasonable times using IDA* algorithm.  Therefore stopping the computation to change the methodology was the correct things to do. It seems there is an available algorithm in polynomial time that can find sub-optimal solutions, see Ian Parberry, Solving the (n^2−1)-Puzzle with 8/3n^3 Expected Moves, Algorithms 2015, 8(3), 459-465. It may be what you are looking for.
Question: Is there an easy way to know the filename of a Python wheel before running the setup script? I'm trying to generate a Bazel rule that builds a .whl for each Python version installed in the machine, the library contains native code so it needs to be compiled for each version separately. The thing with Bazel is that it requires to declare any outputs in advance, and what I'm observing is that each Python version generates a different filename without obvious consistency (different prefixes for malloc and unicode) 2.7 --> lib-0.0.0-cp27-cp27mu-linux_x86_64.whl 3.6m --> lib-0.0.0-cp36-cp36m-linux_x86_64.whl 3.8 --> lib-0.0.0-cp36-cp38-linux_x86_64.whl  I know as a workaround I could zip the wheel to pass it around, but I was wondering if there is a cleaner way to do it.

Answer: Update See also a more detailed answer here. You can get the name by querying the bdist_wheel command, for that you don't even need to build anything or writing a setup.py script (but you need the metadata you pass to the setup function). Example: from distutils.core import Extension from setuptools.dist import Distribution fuzzlib = Extension('fuzzlib', ['fuzz.pyx']) # the files don't need to exist dist = Distribution(attrs={'name': 'so', 'version': '0.1.2', 'ext_modules': [fuzzlib]}) bdist_wheel_cmd = dist.get_command_obj('bdist_wheel') bdist_wheel_cmd.ensure_finalized() distname = bdist_wheel_cmd.wheel_dist_name tag = '-'.join(bdist_wheel_cmd.get_tag()) wheel_name = f'{distname}-{tag}.whl' print(wheel_name)  will print you the desired name. Notice that attrs passed to Distribution should contain the same metadata you pass to the setup function, otherwise you will likely get a wrong tag. To reuse the metadata, in a setup.py script this could be combined like e.g. setup_kwargs = {'name': 'so', 'version': '0.1.2', ...} dist = Distribution(attrs=setup_kwargs) ... setup(**setup_kwargs)