eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I generate a line chart in Altair. I'd like to control which lines are "on top" of the stack of lines. In my example here, I wish for the red line to be on top (newest date) and then descend down to the yellow (oldest date) to be on the bottom.  I tried to control this with the sort parameter of of alt.Color but regardless of sort='ascending' or sort='descending' the order of the line overlap will not change. How can I control this? Was hoping I can do this without sorting my source dataframe itself. data = [{'review_date': dt.date(year=2022, month=2, day=24), 'a':19, 'b':17, 'c':12, 'd':8}, {'review_date': dt.date(year=2022, month=2, day=23), 'a':20, 'b':16, 'c':14, 'd':8}, {'review_date': dt.date(year=2022, month=2, day=22), 'a':22, 'b':16, 'c':14, 'd':10}, {'review_date': dt.date(year=2022, month=2, day=21), 'a':14, 'b':13, 'c':12, 'd':5},] df = pd.DataFrame(data).melt(id_vars=['review_date'], value_name='price', var_name='contract') df.review_date = pd.to_datetime(df.review_date) domain = df.review_date.unique() range_ = ['red', 'blue', 'gray', 'yellow'] alt.Chart(df, title='foo').mark_line().encode( x=alt.X('contract:N'), y=alt.Y('price:Q',scale=alt.Scale(zero=False)), color=alt.Color('review_date:O', sort="ascending", scale=alt.Scale(domain=domain, range=range_) ) ).interactive() 

Answer: By default, graphical marks are plotted in the order they occur in the dataframe (as you noted), which means that the elements last in the dataframe will be plotted last and end up on top in the chart (called the highest "layer" or the highest "z-order"): import pandas as pd import altair as alt df = pd.DataFrame({ 'a': [1, 2, 1, 2], 'b': [1.1, 2.1, 1.0, 2.2], 'c': ['point1', 'point1', 'point2', 'point2'] }) alt.Chart(df).mark_circle(size=1000).encode( x='a', y='b', color='c' )   When you set the sort parameter of the color encoding, you are not changing the z-order for the dots, you are only changing the order in which they are assigned a color. In the plot below, "point2" is still on top, but it is now blue instead of orange: alt.Chart(df).mark_circle(size=1000).encode( x='a', y='b', color=alt.Color('c', sort='descending') )   If we wanted to change the z-ordering so that "point1" is on top, we would have to specify this with the order encoding: alt.Chart(df).mark_circle(size=1000).encode( x='a', y='b', color='c', order=alt.Order('c', sort='descending') )   However, as you can read in the Vega-Lite documentation the order encoding has a special behavior for stacked and path marks, including line mark, where it controls the order in which the points are connected in a line rather than their z-ordering/layering. Therefore, I believe the only way you can achieve the desired behavior is by sorting that column. You can do this during chart construction: alt.Chart(df).mark_line(size=10).encode( x='a', y='b', color='c' )   alt.Chart(df.sort_values('c', ascending=False)).mark_line(size=10).encode( x='a', y='b', color='c' )
Question: As described here, some built-in generic types are Protocols. This means that as long as they implement certain methods, type-checkers will mark them as being compatible with the type:  If a class defines a suitable __iter__ method, mypy understands that it implements the iterable protocol and is compatible with Iterable[T].  So why is Mapping not a protocol? It clearly feels like it should be one, as evidenced by this well up-voted SO answer:  typing.Mapping is an object which defines the __getitem__,__len__,__iter__ magic methods  If it were one, I could pass things which behave like mappings into function which require a mapping, but doing that is not allowed: from typing import Mapping class IntMapping: def __init__(self): self._int_map = {} def __repr__(self): return repr(self._int_map) def __setitem__(self, key: int, value: int): self._int_map[key] = value def __getitem__(self, key: int): return self._int_map[key] def __len__(self): return len(self._int_map) def __iter__(self): return iter(self._int_map) def __contains__(self, item: int): return item in self._int_map def keys(self): return self._int_map.keys() def items(self): return self._int_map.items() def values(self): return self._int_map.values() def get(self, key: int, default: int) -> int: return self._int_map.get(key, default) def __eq__(self, other): return self == other def __ne__(self, other): return self != other x: Mapping = IntMapping() # Type checkers don't like this  Shout-out to this answer for pointing me to the link about protocols)

Answer: It appears to be deliberate, and basically boils down to 'we think that type is too complex to be a protocol.' See https://www.python.org/dev/peps/pep-0544/#changes-in-the-typing-module. Note that you can get this effect by having your own class extend abc.Mapping
Question: I'm trying to return a GET request from an API using HTTPBasicAuth. I've tested the following in Postman, and received the correct response URL:"https://someapi.data.io" username:"username" password:"password"  And this returns me the data I expect, and all is well. When I've tried this in python however, I get kicked back a 403 error, alongside a ""error_type":"ACCESS DENIED","message":"Please confirm api-key, api-secret, and permission is correct."  Below is my code: import requests from requests.auth import HTTPBasicAuth URL = 'https://someapi.data.io' authBasic=HTTPBasicAuth(username='username', password='password') r = requests.get(URL, auth = authBasic) print(r)  I honestly can't tell why this isn't working since the same username and password passes in Postman using HTTPBasicAuth

Answer: You have not conveyed all the required parameters. And postman is doing this automatically for you. To be able to use in python requests just specify all the required parameters. headers = { 'Host': 'sub.example.com', 'User-Agent': 'Chrome v22.2 Linux Ubuntu', 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate, br', 'Connection': 'keep-alive', 'X-Requested-With': 'XMLHttpRequest' } url = 'https://sub.example.com' response = requests.get(url, headers=headers)
Question: Is there a way to make the processes in concurrent.futures.ProcessPoolExecutor terminate if the parent process terminates for any reason? Some details: I'm using ProcessPoolExecutor in a job that processes a lot of data. Sometimes I need to terminate the parent process with a kill command, but when I do that the processes from ProcessPoolExecutor keep running and I have to manually kill them too. My primary work loop looks like this: with concurrent.futures.ProcessPoolExecutor(n_workers) as executor: result_list = [executor.submit(_do_work, data) for data in data_list] for id, future in enumerate( concurrent.futures.as_completed(result_list)): print(f'{id}: {future.result()}')  Is there anything I can add here or do differently to make the child processes in executor terminate if the parent dies?

Answer: You can start a thread in each process to terminate when parent process dies: def start_thread_to_terminate_when_parent_process_dies(ppid): pid = os.getpid() def f(): while True: try: os.kill(ppid, 0) except OSError: os.kill(pid, signal.SIGTERM) time.sleep(1) thread = threading.Thread(target=f, daemon=True) thread.start()  Usage: pass initializer and initargs to ProcessPoolExecutor with concurrent.futures.ProcessPoolExecutor( n_workers, initializer=start_thread_to_terminate_when_parent_process_dies, # + initargs=(os.getpid(),), # + ) as executor:  This works even if the parent process is SIGKILL/kill -9'ed.
Question: I have a checkbutton: from tkinter import * master = Tk() Checkbutton(master, text="Here...").grid(row=0, sticky=W) mainloop()  Which looks like this:  I tried to move the checkbutton to the other side (to support RTL languages), so it'll be like: Here...[] I know that I can draw a label next to the checkbutton, but this way clicking the text won't effect the checkbutton. How can I do it?

Answer: You can bind the left mouse button click event of the label, to a lambda construct that toggles the checkbutton -: label.bind("<Button-1>", lambda x : check_button.toggle())  The label can then be placed before the checkbutton using grid(as mentioned in the OP at the end) -: from tkinter import * master = Tk() l1 = Label(master, text = "Here...") cb = Checkbutton(master) l1.grid(row = 0, column = 0) cb.grid(row = 0, column = 1, sticky=W) l1.bind("<Button-1>", lambda x : cb.toggle()) mainloop()  This will toggle, the checkbutton even if the label is clicked. OUTPUT -:   NOTE: The checkbutton, has to now be fetched as an object(cb), to be used in the lambda construct for the label's bind function callback argument. Thus, it is gridded in the next line. It is generally a good practice to manage the geometry separately, which can prevent error such as this one.  Also, as mentioned in the post linked by @Alexander B. in the comments, if this assembly is to be used multiple times, it can also be made into a class of it's own that inherits from the tkinter.Frame class -: class LabeledCheckbutton(Frame): def __init__(self, root, text = ""): Frame.__init__(self, root) self.checkbutton = Checkbutton(self) self.label = Label(self, text = text) self.label.grid(row = 0, column = 0) self.checkbutton.grid(row = 0, column = 1) self.label.bind('<Button-1>', lambda x : self.checkbutton.toggle()) return pass  Using this with grid as the geometry manager, would make the full code look like this -: from tkinter import * class LabeledCheckbutton(Frame): def __init__(self, root, text = ""): Frame.__init__(self, root) self.checkbutton = Checkbutton(self) self.label = Label(self, text = text) self.label.grid(row = 0, column = 0) self.checkbutton.grid(row = 0, column = 1) self.label.bind('<Button-1>', lambda x : self.checkbutton.toggle()) return pass master = Tk() lcb = LabeledCheckbutton(master, text = "Here...") lcb.grid(row = 0, sticky = W) mainloop()  The output of the above code remains consistent with that of the first approach. The only difference is that it is now more easily scalable, as an object can be created whenever needed and the same lines of code need not be repeated every time.
Question: I'm using Python's pyrogram lib to login to multiple accounts. I need to create a function just to send verification code to account and then read it from other user input (not the default pyrogram login prompt). When I use send_code it sends code and waits for user input from console and that what I don't want it to do. I need simply a function that takes phone number as parameter and send confirmation code to it, and a function then to login with that confirmation code (got from user input somewhere else, e.g.: from telegram message to a linked bot or ....

Answer: I found a way to do it but with Telethon : client = TelegramClient('sessionfile',api_id,api_hash) def getcode(): code = ... # get the code from somewhere ( bot, file etc.. ) return code client.start(phone=phone_number,password=password,code_callback=getcode)  this will login , gets confirmation code from specific function and then uses it to login , and store session file
Question: I need to match cases where the input is iterable. Here's what I tried: from typing import Iterable def detector(x: Iterable | int | float | None) -> bool: match x: case Iterable(): print('Iterable') return True case _: print('Non iterable') return False  That is producing this error: TypeError: called match pattern must be a type  Is it possible to detect iterability with match/case? Note, these two questions address the same error message but neither question is about how to detect iterability:  how to fix TypeError: called match pattern must be a type in Python 3.10  Pattern Matching in Python getting Error : called match pattern must be a type  

Answer: The problem is the typing.Iterable is only for type hints and is not considered a "type" by structural pattern matching. Instead, you need to use an abstract base class for detecting iterability: collections.abc.Iterable. The solution is distinguish the two cases, marking one as being a type hint and the other as a class pattern for structural pattern matching: def detector(x: typing.Iterable | int | float | None) -> bool: match x: case collections.abc.Iterable(): print('Iterable') return True case _: print('Non iterable') return False  Also note that at the time of this writing mypy does not support the match statement.
Question: I need to install Python 3.8 on a Windows computer and hope to use the latest minor version of 3.8.12. The official release web page provides tarball files of the source code but no Windows installer. Python 3.8.10 provides Windows installers, but it is not the latest version. I am wondering:  Why v3.8.12 does not provide any Windows installer? If we want to use v3.8.12, what can we do?  I appreciate your help and suggestions.

Answer: https://devguide.python.org/#status-of-python-branches provides a summary of the various release statuses.  features new features, bugfixes, and security fixes are accepted. prerelease feature fixes, bugfixes, and security fixes are accepted for the upcoming feature release. bugfix  bugfixes and security fixes are accepted, new binaries are still released. (Also called maintenance mode or stable release) security  only security fixes are accepted and no more binaries are released, but new source-only versions can be released end-of-life release cycle is frozen; no further changes can be pushed to it.  Python 3.8 is currently in security mode, so only source releases (and no binary installers) are provided from Python itself. As the page you linked to says,  According to the release calendar specified in PEP 569, Python 3.8 is now in the "security fixes only" stage of its life cycle: 3.8 branch only accepts security fixes and releases of those are made irregularly in source-only form until October 2024. Python 3.8 isn't receiving regular bug fixes anymore, and binary installers are no longer provided for it. Python 3.8.10 was the last full bugfix release of Python 3.8 with binary installers.  Python 3.8.10 was the last release at the bug fix stage, and so the last for which Python officially provided binary installers. If you want a binary installers for 3.8.12, you'll have to either make one yourself from the source or find someone else who has done it or will do it for you.
Question: Is it possible to connect to AuraDB with neomodel? AuraDB connection URI is like neo4j+s://xxxx.databases.neo4j.io. This is not contained user/password information. However, connection config of neomodel is bolt and it is contained user/password information. config.DATABASE_URL = 'bolt://neo4j:password@localhost:7687'

Answer: Connecting to neo4j Aura uses neo4j+s protocol so you need to use the provided uri by Aura. Reference: https://neo4j.com/developer/python/#driver-configuration In example below; you can set the database url by setting the userid and password along with the uri. It works for me so it should also work for you. from neomodel import config user = 'neo4j' psw = 'awesome_password' uri = 'awesome.databases.neo4j.io' config.DATABASE_URL = 'neo4j+s://{}:{}@{}'.format(user, psw, uri) print(config.DATABASE_URL) Result: neo4j+s://neo4j:awesome_password@awesome.databases.neo4j.io
Question: I'm wondering what is the proper way to test if a named capture group exists. Specifically, I have a function that takes a compiled regex as an argument. The regex may or may not have a specific named group, and the named group may or may not be present in a string being passed in: some_regex = re.compile("^foo(?P<idx>[0-9]*)?$") other_regex = re.compile("^bar$") def some_func(regex, string): m = regex.match(regex, string) if m.group("idx"): # get *** IndexError: no such group here... print(f"index found and is {m.group('idx')}") print(f"no index found") some_func(other_regex, "bar")  I'd like to test if the group exists without using try -- as this would short circuit the rest of the function, which I would still need to run if the named group was not found.

Answer: You can check the groupdict of the match object: import re some_regex = re.compile("^foo(?P<idx>[0-9]*)?$") match = some_regex.match('foo11') print(True) if match and 'idx' in match.groupdict() else print(False) # True match = some_regex.match('bar11') print(True) if match and 'idx' in match.groupdict() else print(False) # False
Question: When I try to use the autocomplete using Pylance it is stuck there for some time and After Some time like 3 ~ 5 seconds the pop up with auto-complete shows up Python Language Server is already set to Pylance What I've tried so far. Reinstall Python Extension. Reinstall VS Code Restarted Python Language Server Reset VS Code Reinstall Pylance. But None of the above seems to work

Answer: It works well on my computer, how do you open this python file? Try moving your code to its own folder and opening that up instead of opening up some big folder that contains a lot of files. This does show a performance hole where large workspaces take a while to load. You can refer to this page for more details.
Question: Suppose I have the function: def myF(a, b): return a*b-2*b  and let's say that I want a default value for b to be a-1. If I write: def myF(a, b=a-1): return a*b-2*b  I get the error message: NameError: name 'a' is not defined  I can use the code below: def myF(a, b): return a*b-2*b def myDefaultF(a): return myF(a, a-1)  to have myF with default value, but I don't like it. How can I avoid myDefaultF and have myF with default value a-1 for b without errors?

Answer: You can do the following: def myF(a, b=None): if b is None: b = a - 1 return a * b - 2 * b
Question: In the numpy library, one can pass a list into the numpy.searchsorted function, whereby it searched through a different list one element at a time and returns an array of the same sizes as the indices needed to preserve order. However, it seems to be wasting performance if both lists are sorted. For example: m=[1,3,5,7,9] n=[2,4,6,8,10] numpy.searchsorted(m,n)  would return [1,2,3,4,5] which is the correct answer, but it looks like this would have complexity O(n ln(m)), whereby if one were to simply loop through m, and have some kind of pointer to n, it seems like the complexity is more like O(n+m)? Is there some kind of function in NumPy which does this?

Answer: AFAIK, this is not possible to do that in linear time only with Numpy without making additional assumptions on the inputs (eg. the integer are small and bounded). An alternative solution is to use Numba to do the merge manually: import numba as nb # Note: Numba requires a function signature with well defined array types @nb.njit('int64[:](int64[::1], int64[::1])') def search_both_sorted(a, b): i, j = 0, 0 result = np.empty(b.size, np.int64) while i < a.size and j < a.size: if a[i] < b[j]: i += 1 else: result[j] = i j += 1 for k in range(j, b.size): result[k] = i return result a, b = np.cumsum(np.random.randint(0, 100, (2, 1000000)).astype(np.int64), axis=1) result = search_both_sorted(a, b)  A faster implementation consists in using a branch-less approach so to remove the overhead of branch mis-prediction (especially on random/unpredictable inputs) when a and b are about the same size. Additionally, the O(n log m) algorithm can be faster when b is small so using np.searchsorted in that case is very efficient as pointed out by @MichaelSzczesny. Note that the Numba implementation of np.searchsorted can be a bit slower than the one of Numpy so it is better to pick the Numpy implementation. Here is the optimized version: @nb.njit('int64[:](int64[::1], int64[::1])') def search_both_sorted_opt_numba(a, b): sa, sb = a.size, b.size # Choose the best algorithm if sb < sa * 0.15: # Use a version with branches because `a[i] < b[j]` # should be most of the time true. i, j = 0, 0 result = np.empty(b.size, np.int64) while i < a.size and j < b.size: if a[i] < b[j]: i += 1 else: result[j] = i j += 1 for k in range(j, b.size): result[k] = i else: # Use a branchless approach to avoid miss-predictions i, j = 0, 0 result = np.empty(b.size, np.int64) while i < a.size and j < b.size: tmp = a[i] < b[j] result[j] = i i += tmp j += ~tmp for k in range(j, b.size): result[k] = i return result def search_both_sorted_opt(a, b): sa, sb = a.size, b.size # Choose the best algorithm if 2 * sb * np.log2(sa) < sa + sb: return np.searchsorted(a, b) else: return search_both_sorted_opt_numba(a, b)  searchsorted: 19.1 ms snp_search: 11.8 ms search_both_sorted: 6.5 ms search_both_sorted_branchless: 4.3 ms  The optimized branchless Numba implementation is about 4.4 times faster than searchsorted which is pretty good considering that the code of searchsorted is already highly optimized. It can be even faster when a and b are huge because of cache locality.
Question: I have a simple model like class Author(models.Model): name = models.CharField(max_length=100) def __str__(self): return self.name class Blog(models.Model): title = models.CharField(max_length=100) author = models.ForeignKey(Author, on_delete=models.CASCADE)  Here I want to queryall authors with the title of all the blogs they have written like Author One : [Blog One, Blog Two, Blog Three]  I want this from query and not loop Here my approach is to use subquery like blogs =Blog.objects.filter(author=OuterRef('pk')).values("title") authors = Author.objects.annotate(blogs=Subquery(blogs), output_field=CharField())  But here I am getting error like sub-select returns 2 columns - expected 1 How can I get all the authors with all the blogs they have written ? I do not want to use loop. I want this through query

Answer: If you are using a postgres as a database, then you can use an ArrayAgg function: from django.contrib.postgres.aggregates import ArrayAgg authors = Author.objects.annotate(blogs=ArrayAgg('blog_set__title'))
Question: Python version 3.6 I have the following folder structure . ├── main.py ├── tests/ | └── test_Car.py └── automobiles/ └── Car.py  my_program.py from automobiles.Car import Car p = Car("Grey Sedan") print(p.descriptive_name())  Car.py class Car(): description = "Default" def __init__(self, message): self.description = message def descriptive_name(self): return self.description  test_Car.py from automobiles.Car import Car def test_descriptive_name(): input_string = "Blue Hatchback" p = Car(input_string) assert(p.descriptive_name() == input_string)  When running pytest in the commandline from the project root folder, I get the following error- Traceback: ..\..\..\AppData\Local\Programs\Python\Python36\lib\importlib\__init__.py:126: in import_module return _bootstrap._gcd_import(name[level:], package, level) tests\test_Car.py:2: in <module> from automobiles.Car import Car E ModuleNotFoundError: No module named 'automobiles'  I've been battling this for awhile now and I think I'm missing something obvious. I don't think it is anything to do with a missing __init__.py - I've tried placing an empty __init.py__ alongside the car.py file, with no difference in the error. What do I have to change to get the test_Car.py to run successfully ?

Answer: You have 2 options:  Run python -m pytest instead of pytest, which will also add the current directory to sys.path (see official docs for details).  Add a __init__.py file under tests/, then you can simply run pytest. This basically enables pytest to discover the tests if they live outside of the application code. You can find some more details about this in the Tests outside application code section in the official docs.   Hope that helped!