eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I'm writing a testcase to reproduce deadlock in my service. After I use multiprocessing to create a two thread, I found they use the same database connection. So I can't reproduce the deadlock scenario in testcase. How do i resolve this issue? My code is as belows: @transaction.atomic() def process_1(): change = Change.objects.select_for_update().get(id="1") time.sleep(5) change = Change.objects.select_for_update().get(id="2") @transaction.atomic() def process_2(): change = Change.objects.select_for_update().get(id="2") time.sleep(5) change = Change.objects.select_for_update().get(id="1") p1 = Process(target=process_1) p2 = Process(target=process_2) p1.start() p2.start() p1.join() p2.join() self.assertEqual(p1.exitcode, 0) self.assertEqual(p2.exitcode, 0) 

Answer: This is happening cause Django’s database connections are not automatically shared across processes when using multiprocessing (each process should create its own database connection). Just close the database connection before forking the new processes: from django.db import connection # Here your functions ... connection.close() # Ensure each process gets a fresh DB connection p1 = Process(target=process_1) p2 = Process(target=process_2) p1.start() p2.start() p1.join() p2.join()
Question: I have an arbitrary Matrix M which is (N x A). I have a column vector V (N x 1) which has on each row the amount of entries I would like to keep from the original M <= A (starting from the leftmost) As an example, say I have the following V (for an arbitrary 5xA Matrix): [[1] [0] [4] [2] [3]]  i.e. I want to keep the 1st element of the first row, no elements in row 2, 4 from row 3, 2 from row 4, etc. I want this to generate the following mask: [[1 0 0 0 0] [0 0 0 0 0] [1 1 1 1 0] [1 1 0 0 0] [1 1 1 0 0]]  I then apply this mask to my matrix to get the result that I want. What is the fastest way to generate this mask? Naive pythonic approach: A = 20 n = 5 V = np.floor(np.random.rand(n) * (A+1)) x = [np.concatenate(np.repeat(1, x), np.repeat(0, 20 - x), axis=1) for x in V] x = np.array(x)  This code is my current working solution but it is way too slow for large n, so I need a vectorized solution. Using numpy.fromfunction: n = 5 A = 20 V = np.floor(np.random.rand(n, 1) * (A + 1)) mask = np.fromfunction(lambda i,j: V > j, (n,20), dtype=int)  This solution is considerably faster for large n, but I am not sure if I can do better than this. Overall: Any insights on this problem? Not too familiar with the ins and outs of numpy and python so I thought I'd post this here before I tried purusuing any individual solution further. I am also willing to compile to Cython if that would help this at all, though I know absolutely nothing about that language at the moment but I am willing to look into it. Open to pretty much any and all solutions.

Answer: Just use broadcasting and numpy.arange: mask = V > np.arange(M.shape[1])  Or from A: A = 5 mask = V > np.arange(A)  Output: array([[ True, False, False, False, False], [False, False, False, False, False], [ True, True, True, True, False], [ True, True, False, False, False], [ True, True, True, False, False]])  And if you need integers: mask.astype(int) array([[1, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 1, 1, 1, 0], [1, 1, 0, 0, 0], [1, 1, 1, 0, 0]])
Question: I am using cv2.waitKey(1) & 0xFF to capture key events, but I found that it cannot correctly detect the Delete key. My current code is as follows: import cv2 while True: key = cv2.waitKey(1) if key != -1: # -1 : timeout/no key event print(f"Key pressed: {key}") if key == 127: print("Delete key detected!") break cv2.destroyAllWindows()  However, when I run this code and press the Delete key, it is not detected. Environment: Windows 10, Python 3.9, OpenCV 4.5.5 Issue: key == 127 does not work Expected Behavior: When I press Delete, it should print Delete key detected. Questions:    Can cv2.waitKey(1) correctly detect the Delete key?  Is there a more accurate way to detect the Delete key?    

Answer: Use waitKeyEx(). It'll give you every bit of information the operating system has to give. waitKey does not, it only gives you the lowest eight bits. On Windows, Del will cause waitKey to return 0, but waitKeyEx() will return 0x2e0000. waitKeyEx() will return other values on other systems.  Never ever use that & 0xFF pattern. Never ever. Not with waitKeyEx, not with waitKey. That operation is utterly redundant. waitKey does that internally already, and with waitKeyEx the point is to get those bits, not discard them. This operation also mangles the return value indicating "no key event", which is properly -1, and makes it 255.
Question: In Airflow, you're suppose to raise an AirflowException if you want a task to be marked as a failure. But the raised error doesn't seem to be caught in the top-level Airflow module, and so it results in the entire stacktrace being dumped into the logs. If you do your error handling properly, it should be possible to print a helpful error message and then fail the task, without gumming up the logs. How can this be done?

Answer: Yes, you can do it by using Python’s exception chaining syntax. Instead of writing: raise AirflowException("Your error message")  you write: raise AirflowException("Your error message") from None  This “from None” tells Python not to include the exception’s context (i.e. the chained traceback) in the logs. So, when your task fails, you’ll just see your helpful error message instead of a long traceback, making the logs much cleaner. For example: from airflow.exceptions import AirflowException def some_task(): try: # some code that might raise an error pass except Exception as e: # Log a neat error message error_msg = "Something went wrong, please check your configuration." # Now raise the AirflowException without the full traceback raise AirflowException(error_msg) from None
Question: When you don't provide a __repr__ or __str__ method on a custom class, you just get a classname and the Python address of the object (or, to be more specific, what id(self) would return. This is fine most of the time. And it is very helpful when you are debugging some code and you want to see if instances are/are not the same, visually. But to be honest I almost never care about that id value. However it also means that running a program with debugging print functions never looks the same. Ditto if you are comparing log files. Unless you write a lot of __repr__ only to avoid this issue. Or if you pre-format the log files to zero out the hex values on the default object prints. A sample program to illustrate what I would like to do: not have that id printed. class ILookDifferentEveryRun: "baseline behavior" def __init__(self,a): "I don't actually care about `a`, that's why I don't need a `repr`" self.a = a class ILookTheSameEveryRun(ILookDifferentEveryRun): """this is my workaround, a cut and paste of a default __repr__""" def __repr__(self) : return type(self).__name__ class ILookAlmostLikeBuiltinRepr(ILookDifferentEveryRun): "can I do this with a global switch?" def __repr__(self) : """this is more or less what I want""" res = f"<{type(self).__module__}.{type(self).__name__} object> at <dontcare>" return res inst1 = ILookDifferentEveryRun(a=1) inst2 = ILookTheSameEveryRun(a=1) inst3 = ILookAlmostLikeBuiltinRepr(a=1) print(inst1) print(inst2) print(inst3)  run twice: <__main__.ILookDifferentEveryRun object at 0x100573260> ILookTheSameEveryRun <__main__.ILookAlmostLikeBuiltinRepr object> at <dontcare>  <__main__.ILookDifferentEveryRun object at 0x104ca7320> ILookTheSameEveryRun <__main__.ILookAlmostLikeBuiltinRepr object> at <dontcare>  I took a look at the startup flags for the python interpreter, but nothing seems to allow for this. Any workarounds? I know I could also put the repr on a Mixin and reuse that everywhere, but that's ugly too. If I can't, that's fine and that's what I am expecting to hear. Just wondering if someone else had the same problem and found a way. p.s. this is less about dedicated printing of instances and more about things like print(mylist) where mylist=[item1,item2,item3], generally any complex data structures with nested items in them.

Answer: As a workaround you can add your desired __repr__ to every user class that does not have a custom __repr__ defined anywhere in the MRO. This can be done automaticallly by replacing builtins.__build_class__, which implements the class statement, with a wrapper function that applies the patch to the class that the original __build_class__ returns: import builtins def __repr__(self): cls = type(self) return f'<{cls.__module__}.{cls.__qualname__} object>' def build_class(*args, orig_build_class=__build_class__, **kwargs): cls = orig_build_class(*args, **kwargs) if cls.__repr__ is object.__repr__: cls.__repr__ = __repr__ return cls builtins.__build_class__ = build_class  so that: class A: pass print(A())  outputs: <__main__.A object>  Demo: https://ideone.com/L5mFVU
Question: I'm trying to use two classes, A and B, as mixing classes of AB. They both have __init_subclass__ methods. The problem is that both __init__sublass__ methods have the same argument msg. Therefore I've used an adaptor class B_ to rename B's argument msg_b. But I'm having trouble! The nearest I have got is: class A: def __init_subclass__(cls, msg, **kwargs): print(f'{cls=} A: {msg}') super().__init_subclass__(**kwargs) class B: def __init_subclass__(cls, msg, **kwargs): print(f'{cls=} B: {msg}') super().__init_subclass__(**kwargs) # Adaptor class `B_` needed because both `A` and `B` have an argument `msg`. class B_: # Rename `msg` to `msg_b`. def __init_subclass__(cls, msg_b, **kwargs): # `B.__init_subclass__(msg_b, **kwargs)` sets the subclass as `B` not `cls`, but otherwise works. B.__init_subclass__.__func__(cls, msg=msg_b, **kwargs) # Still need a `B`. def __init__(self, *args, **kwargs): self.b = B(*args, **kwargs) # Forward all the attributes to `self.b`. def __getattr__(self, item): return getattr(self.b, item) class AB(A, B_, msg='Hello.', msg_b='Also, hello.' ): ... print(f'{AB()=}, {isinstance(AB(), A)=}, {isinstance(AB(), B_)=}, {isinstance(AB(), B)=}')  Which does call both __init_sublass__s with the correct class argument, it prints: cls=<class '__main__.AB'> A: Hello. cls=<class '__main__.AB'> B: Also, hello.  But then you get the error: Traceback (most recent call last): File <definition of AB>, in <module> class AB(A, B_, msg='Hello.', msg_b='Also, hello.' ): File <A's>, in __init_subclass__ super().__init_subclass__(**kwargs) File <B_'s>, in __init_subclass__ B.__init_subclass__.__func__(cls, msg=msg_b, **kwargs) File <B's>, in __init_subclass__ super().__init_subclass__(**kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: super(type, obj): obj must be an instance or subtype of type  Presumably because B is not a super type of AB (B_ is). I’m not clear why super wants subtypes matching though! Any ideas on how to fix this?

Answer: Presumably you don't have the authorization to modify A or B, or you would've renamed the msg parameter in one of their __init_subclass__s already. One workaround then is to use an intermediary base class between A and B with an __init_subclass__ method that renames the argument msg_b to msg before passing the baton to B.__init_subclass__: class ABGlue: def __init_subclass__(cls, msg_b, **kwargs): super().__init_subclass__(msg=msg_b, **kwargs) class AB(A, ABGlue, B, msg='Hello.', msg_b='Also, hello.' ): ... print(f'{AB()=}') print(f'{isinstance(AB(), A)=}') print(f'{isinstance(AB(), B)=}')  This outputs something like: cls=<class '__main__.AB'> A: Hello. cls=<class '__main__.AB'> B: Also, hello. AB()=<__main__.AB object at 0x14a498119460> isinstance(AB(), A)=True isinstance(AB(), B)=True  Demo: https://ideone.com/soXBWW
Question: I want to fetch an element under the key ID in my json list that looks like this: [{"ID": 0, "login": "admin", "password": "123", "email": "apple@gmail.com"}, {"ID": 1, "login": "admin2", "password": "1234", "email": "grape@gmail.com"}] The list is in the data.json file and it’s not assigned any variable. I have a 'check' variable that takes the value of a number. I want to call the check function from ANOTHER python file.Here’s how: if "ID"==check return True.

Answer: First, create and save a file "fetch_data.py" with the following code: import json def check_id(check): with open("data.json", "r") as file: data = json.load(file) # Read JSON list for item in data: if item.get("ID") == check: return True return False  Create and save the main.py file: from fetch_data import check_id check = 1 #Checking if ID 1 exists if check_id(check): print("ID found!") else: print("ID not found!")  Please ensure that data.json file contains the following data: [ { "ID": 0, "login": "admin", "password": "123", "email": "apple@gmail.com" }, { "ID": 1, "login": "admin2", "password": "1234", "email": "grape@gmail.com" } ]
Question: I have the following code in which I am trying to return the value of e^x using z3. However, this code is returning y = 0 import z3 x, y = z3.Real('x'), z3.Real('y') e = z3.RealVal(str(gmpy2.exp(gmpy2.mpfr(1)))) # or e^1, the Euler's number e print(e) s = z3.Solver() s.add(x == 1134585759063987950064875850350910837993/1361129467683753853853498429727072845824) s.add(y == e ** x) s.check() model = s.model() print(model)  The output of this code is: 3397852285573806544200359339189/1250000000000000000000000000000 [x = 260488115293581/312500000000000, y = 0]  How can I fix this code?

Answer: If you add: print(s.check())  you'll see that z3 prints: unknown  This means that the solver wasn't able to come up with a model that is guaranteed to satisfy the constraints. (Exponentials are hard for SMT solvers: There are very good reasons for this, you can search stack-overflow for it.) So, the model you print is irrelevant. Or, more precisely, is not guaranteed to satisfy the constraints since the solver is in unknown state. So far as "fixing" this is concerned: You can't really. But if x is a constant, and e is a constant (already), so is e^x. Just calculate its value outside of z3, and plug it in.
Question: I know the subject sounds counter-intuitive but bear with me. My goal is to have two rotating file log handlers, mostly for logger.exception() calls, one that includes stack traces and one that doesn't. I find that error conditions can sometimes cause the logs to fill with stack traces, making them difficult to read/parse without creating a separate parsing script, which I'd prefer not to do. I would like to use the built-in RotatingFileHandler class if possible. Here's a simplified code snippet of what I'm trying to accomplish: # getLogger just returns an instance of a logger derived from the main Python logger # where I can override the logger.exception() method from myutils import getLogger logger = getLogger(__name__) try: x = 1 / 0 except ZeroDivisionError: # This one call should log to one file with the trace, one without the trace logger.exception("Oops")  I have the infrastructure in place to have this write to separate log files already using separate handlers, but they both include the stack traces. Is there a mechanism (logging filter or otherwise) where a log handler can strip the stack trace from logger.exception() calls? I am assuming (or hoping) that logging filters attached to a handler can accomplish this, but I'm not sure how it can be done. And just as an FYI, here is the source code of the Python logger for Logging.exception() and Logging.error() calls:  def error(self, msg, *args, **kwargs): """ Delegate an error call to the underlying logger. """ self.log(ERROR, msg, *args, **kwargs) def exception(self, msg, *args, exc_info=True, **kwargs): """ Delegate an exception call to the underlying logger. """ self.log(ERROR, msg, *args, exc_info=exc_info, **kwargs) 

Answer: You can use a custom Formatter that removes the exception info necessary to the logging of the stack trace. See the source code of the format method of the Formatter. You can see that since record.exc_info, record.exc_text and record.stack_info are set to None, no stack trace can be shown. import logging logger = logging.getLogger("foo") class NoStackTraceFormatter(logging.Formatter): def format(self, record): record.exc_info = None record.exc_text = None record.stack_info = None return logging.Formatter.format(self, record) handler_stacktrace = logging.FileHandler("stack.log") handler_no_stacktrace = logging.FileHandler("nostack.log") handler_no_stacktrace.setFormatter(NoStackTraceFormatter()) logger.addHandler(handler_stacktrace) logger.addHandler(handler_no_stacktrace) try: x = 1 / 0 except ZeroDivisionError: logger.exception("Oops")  Then check the 2 files: # nostack.log Oops  # stack.log Oops Traceback (most recent call last): File "main.py", line 23, in <module> x = 1 / 0 ZeroDivisionError: division by zero
Question: The code below draws a button and an axes object in which it's meant to print out the number of times the button has been pressed. However, it never updates the axes with the number of presses. from matplotlib import pyplot as plt from matplotlib.widgets import Button fig, ax = plt.subplots(figsize=(7,7)) ax.set_visible(False) class MyClass: def __init__(self, fig): self.N_presses = 0 def button_fx(self, event): ax_str.clear() self.N_presses += 1 text_out = "Pushed {} times".format(self.N_presses) ax_str.text(0.5, 0.5, text_out) print(text_out) MC = MyClass(fig) ax_str = fig.add_axes((0.25, 0.5, 0.5, 0.1)) ax_button = fig.add_axes((0.25, 0.3, 0.5, 0.1)) my_button = Button(ax_button, "Push this button", color="0.75", hovercolor="0.875") my_button.on_clicked(func=MC.button_fx) plt.show()  As a check, I also have it print out the number of presses to the console, which happens as it should. It's only the axes that seem to be out of reach. Why can't I update the axes ax_str with new text using button_fx? I can't even plot to it. Is there a workaround? (Note: although it doesn't add anything in this MWE, the class is essential for my actual use case, so I need to know how to solve this problem while keeping the class structure.)

Answer: change :  def button_fx(self, event): ax_str.clear() self.N_presses += 1 text_out = "Pushed {} times".format(self.N_presses) ax_str.text(0.5, 0.5, text_out) print(text_out) to def button_fx(self, event): ax_str.clear() self.N_presses += 1 text_out = "Pushed {} times".format(self.N_presses) ax_str.text(0.5, 0.5, text_out) plt.draw() ######## I've added this line print(text_out) and see if you get what you wanted:  this code works too: from matplotlib import pyplot as plt from matplotlib.widgets import Button fig, ax = plt.subplots(figsize=(7,7)) ax.set_visible(False) class MyClass: def __init__(self, fig): self.N_presses = 0 self.ax_str = fig.add_axes((0.25, 0.5, 0.5, 0.1)) self.ax_button = fig.add_axes((0.25, 0.3, 0.5, 0.1)) self.my_button = Button(self.ax_button, "Push this button", color="0.75", hovercolor="0.875") self.my_button.on_clicked(func=self.button_fx) def button_fx(self, event): self.ax_str.clear() self.N_presses += 1 self.text_out = "Pushed {} times".format(self.N_presses) self.ax_str.text(0.5, 0.5, self.text_out) plt.draw() print(self.text_out) MC = MyClass(fig) plt.show()
Question: I have a pandas dataframe as follows:    Athlete ID City No. of Sport Fields     1231 LA 81   4231 NYC 80   2234 NJ 64   1223 SF 75   4531 LA 81   2345 NYC. 80   ...      I want to print the City and No. of Sport Fields columns and group by City and sort by No. of Sport Fields. groupby() won't work here because I am not calculating anything.

Answer: In your example, it seems that the No. of Sports Field remains the same for a given City. You can therefore use first() upon grouping by City, before sorting by No. of Sports Field : df.groupby('City').first().sort_values(by='No. of Sport Fields') This returns:  Athlete ID No. of Sport Fields City NJ 2234 64 SF 1223 75 NYC 4231 80 NYC. 2345 80 LA 1231 81
Question: Let's say I have a class like this. from scipy import interpolate import numpy as np class Bar: def __init__(self,data): self.data = data def mean(self): return self.data.mean(axis=0) def sd(self): return self.data.std(axis=0) bar = Bar(np.random.rand(10,5)) print(bar.mean()) print(bar.sd())  The class Bar may have many such methods such as mean(), sd() etc. I want to add sampled versions of those methods, so that I can simply get results equivalent to this: new_ids = np.linspace(bar.ids[0],bar.ids[-1],100) sampled_mean = interpolate.interp1d(bar.ids,bar.mean(),axis=0)(new_ids)  Current workaround: manually add new methods with help of decorators. from scipy import interpolate import numpy as np def sample(func): def wrapper(self,n_sample=100,*args,**kwargs): new_ids = np.linspace(self.ids[0],self.ids[-1],n_sample) vals = func(self,*args,**kwargs) return interpolate.interp1d(self.ids,vals,axis=0)(new_ids) return wrapper class Bar: def __init__(self,ids,data): self.ids = ids self.data = data def mean(self): return self.data.mean(axis=0) def sd(self): return self.data.std(axis=0) @sample def spl_mean(self): return self.mean() @sample def spl_sd(self): return self.sd() bar = Bar(ids=np.arange(5),data=np.random.rand(10,5)) new_ids = np.linspace(bar.ids[0],bar.ids[-1],100) sampled_mean = interpolate.interp1d(bar.ids,bar.mean(),axis=0)(new_ids) assert np.all(sampled_mean == bar.spl_mean())  However, I have many such methods. Of course I can write them with the help of LLM but what I want to know is:  whether this is already a good practice to achieve this, and whether there's more elegant approache. 

Answer: One DRYer approach that avoids repeating method names for the sampled versions is to make the decorator a custom descriptor whose __get__ method returns an object that calls the decorated function when called, but also provides a sample method that calls the decorated function with the sampling logics around it: class Samplable: def __init__(self, method): self.method = method def __call__(self, *args, **kwargs): return self.method(*args, **kwargs) def sample(self, *args, **kwargs): return f'sampled {self(*args, **kwargs)}' class sample: def __init__(self, func): self.func = func def __get__(self, obj, objtype=None): return Samplable(self.func.__get__(obj)) class Bar: def __init__(self, data): self.data = data @sample def mean(self): return f'mean {self.data}' @sample def sd(self): return f'sd {self.data}'  so that: bar = Bar('data') print(bar.mean()) print(bar.mean.sample()) print(bar.sd()) print(bar.sd.sample())  outputs: mean data sampled mean data sd data sampled sd data  Demo: https://ideone.com/RBa3UJ
Question: I heard the issue comes when trying to compare symbolic values, for example if i enter e^(8*x) as the initial function in the code, so I've tried evalf() with the values to turn them into numeric, but they continue to be symbolic. What else could i do? import math import sympy as sp print("Fixed Point Method") funini = input("Initial function\nx= ") # initial function by user valini = float(input("Initial value of x\n")) # initial value of x valini = float(valini) # convert the initial value into a float errabs = input("Absolute error to obtain\n") # requested absolute error errabs = float(errabs) # convert the absolute error into a float i = 0 errorfin = 100 while True: i += 1 x = sp.symbols('x') # x as a symbolic variable until given a value x.evalf() print(x) funcion = sp.sympify(funini) # convert the function into a symbolic expression resultado = funcion.subs(x, valini).evalf() # substitute x as a symbolic variable with the initial value resultado.evalf() print(resultado) print(f"For iteration {i}, x= {resultado}") errorfin = abs((resultado - valini) / resultado).evalf() # ensure errorfin is numeric print(f"The error is {errorfin}") valini = resultado # the result becomes the new initial value valini.evalf() print(valini) if i == 10: # max 10 iterations print("The values converge, enter a new initial x") break elif errorfin <= errabs: print(f"The error is less than the absolute error, the root is {resultado}") break 

Answer: Sympy thinks that e is another variable. Try passing it exp(8*x) instead. Also, you have a couple of lines where you do something.evalf() but don't save it to a variable. Those lines aren't doing anything.