eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I found this code in the wild (simplified here), and I was wondering whether this is considered a good practice or not, that is, making a shallow copy (of an object's attribute in this case) for the sole purpose of updating the attribute (and reduce verbosity?). I ask because it is not the first time I see this kind of pattern and it bothers me a bit. class DrawingBoard: def __init__(self): self.raw_points = [] def add_point(self, x, y): Point = collections.namedtuple('Point', ['x', 'y']) points = self.raw_points points.append(Point(x, y)) if len(points) > ......: # DO SOMETHING  board = DrawingBoard() for x in np.arange(0, 10, 0.1): board.add_point(x, np.sin(x))  For context, the actual class is performing some action when a certain amount of points have been added. On my first read, I miss this update was happening (the code is a fair bit more complex). My question is: Is it just me not used to this pattern, and this is a mere question of less verbosity vs clarity? or is there something else I am missing? I can see that if we were updating multiple attributes, it could get quite verbose, but I find calling self.raw_points.append(Points(x,y)) instead of points.append(Points(x,y))so much clearer.

Answer: There is no copy of any kind, shallow or otherwise, made in your example. points = self.raw_points means points is self.raw_points. It makes the name points an additional reference to the same object that self.raw_points refers to, so that: points.append(Points(x,y))  is functionally equivalent to: self.raw_points.append(Points(x,y))  without the overhead of an additional attribute lookup in self.raw_points, which is why this is generally considered a good practice if self.raw_points is to be referenced repeatedly.
Question: I would like to rotate PDF pages around the center (other than just multiples of 90°) in a PDF document and optionally scale them to fit into the original page. Here on StackOverflow, I found a few similar questions, however, mostly about the outdated PyPDF2. And in the latest pypdf documentation, I could not find (or overlooked) a recipe to rotate pages around the center, e.g. for slightly tilted scanned documents, which require rotation of a few degrees. I know that there is the Transformation Class, but the standard rotation is around the lower left corner and documentation is not explaining in detail what the matrix elements actually are. How to rotate a PDF page around the center and optionally scale it that it fits into the original page?

Answer: It took me some time to figure out how to rotate a page around the center and scale it to fit the original page. Although, it just requires a matrix with the correct elements at the right place and some trigonometric functions, it was not too obvious for me. Hence, I post the script I ended up with for my own memories and maybe it is helpful to others not having to re-invent the wheel. After all, you can also achieve it somehow via a combination of rotate(), translate(), and scale(). If anybody has ideas to simplify or improve the following script, please let me know. Script: ### rotate around center (and optional scale) pdf pages from pypdf import PdfReader, PdfWriter, Transformation from math import sin, cos, atan, sqrt, radians, pi pdf_input = PdfReader("Test.pdf") pdf_output = PdfWriter() rotation_angle = 7.3 # in degrees shrink_page = True for page in pdf_input.pages: x0 = (page.mediabox.right - page.mediabox.left)/2 y0 = (page.mediabox.top - page.mediabox.bottom)/2 a = radians(rotation_angle) a0 = atan(max(x0,y0)/min(x0,y0)) s0 = min(x0,y0)/cos(a0 - abs((a-pi/2)%pi - pi/2))/sqrt(x0**2 + y0**2) if shrink_page else 1 rotate_center = Transformation(( s0*cos(a), s0*sin(a), -s0*sin(a), s0*cos(a), s0*(-x0*cos(a)+y0*sin(a))+x0, s0*(-x0*sin(a)-y0*cos(a))+y0)) pdf_output.add_page(page).add_transformation(rotate_center) pdf_output.write("Test_out.pdf") ### end of script  Alternatively, as @MartinThoma commented, instead of using the transformation matrix (or tuple) use translate(), rotate() and again translate() and optionally scale(), but in the right order with the proper numbers. This means, replace the line rotate_center = ... with the following: rotate_center = Transformation().translate(tx=-x0,ty=-y0).scale(sx=s0,sy=s0).rotate(rotation_angle).translate(tx=x0,ty=y0)  Result: (screenshot from Test_out.pdf)
Question: I'm a beginner programmer, and I took a class in C and got (what I believe and hope to be) a really good understanding of how different functions and data types allocate memory differently. So, with that in mind, could someone explain how range() in Python uses memory, please? I know range() in Python 2 would create a list of values, so that's pretty straightforward to understand, but in Python 3, I keep seeing people say it "creates a new object" or makes an "iterator object", but what does the computer do internally with regards to memory? Also, from what I understand, for-loops work the way for-each loops work in Java, so how does a function like for n in range(6) work if it's not iterating through a list of 6 numbers from 0-5? Just to be clear, I know that, as a beginner, memory usage shouldn't be a concern, but I just like knowing how things work under the hood.

Answer: The Python 3 range() object does not allocate memory by creating it (other than the heap memory allocated by itself object, of course); it is a sequence object. All it contains is the start, stop and step values. It 'generates' numbers on demand, in this case, the 'demand' is the loop for n in range(6):. As you 'iterate' over the object the next integer is calculated each iteration. The range object returns an iterator when called __iter__(). The for loop automatically calls this function. We can get the next value by calling next() to it. For example: r = range(6) i = r.__iter__() print(next(i)) # 0 print(next(i)) # 1 print(next(i)) # 2 print(next(i)) # 3 print(next(i)) # 4 print(next(i)) # 5 print(next(i)) # raises 'StopIteration'  The loop automatically stops when next() raises StopIteration. A possible implementation would be as: def range_func(stop): i = 0 while i < stop: yield i i += 1  Note: This is not the real implementation of range; The real one is way more complicated than this. The above function does not allocate any memory for all of the numbers that it generates. It can be used like range(). for n in range_func(6): print(n)
Question: I installed OpenCV using Anaconda, with the following command. mamba create -n opencv -c conda-forge opencv matplotlib  I know that the installation is fully functional because the below works: import cv2 c = cv2.imread("microphone.png") cv2.imwrite("microphone.jpg",c) import os os.getpid() # returns 13249  Now I try to do the same using C++. #include <opencv2/core.hpp> #include <opencv2/imgcodecs.hpp> #include <iostream> using namespace cv; int main() { std::string image_path = "microphone.png"; Mat img = imread(image_path, IMREAD_COLOR); if(img.empty()) { std::cout << "Could not read the image: " << image_path << std::endl; return 1; } imwrite("microphone.JPG", img); return 0; }  And the compilation: > g++ --version g++ (conda-forge gcc 12.3.0-3) 12.3.0 Copyright (C) 2022 Free Software Foundation, Inc. ... > export PKG_CONFIG_PATH=/home/stetstet/mambaforge/envs/opencv/lib/pkgconfig > g++ opencv_test.cpp `pkg-config --cflags --libs opencv4`  When I run the above, g++ complains that I am missing an OpenGL. /home/stetstet/mambaforge/envs/opencv/bin/../lib/gcc/x86_64-conda-linux-gnu/12.3.0/../../../../x86_64-conda-linux-gnu/bin/ld: warning: libGL.so.1, needed by /home/stetstet/mambaforge/envs/opencv/lib/libQt5Widgets.so.5, not found (try using -rpath or -rpath-link)  After some experimentation I discover that some of the libraries must be from /usr/lib/x86_64-linux-gnu, while others must be used from /home/stetstet/mambaforge/envs/opencv/lib/ (opencv is the name of the venv in use). The following yields an a.out which does what was intended: > /usr/bin/g++ opencv_test.cpp `pkg-config --cflags --libs opencv4` -lpthread -lrt  The /usr/bin/g++ so that it can actually find libGL.so.1 as well as libglapi.so.0, libselinux.so.1, libXdamage.so.1, and libXxf86vm.so.1. Also, without -lpthread -lrt these libraries are used from the venv, which causes "undefined reference to `h_errno@GLIBC_PRIVATE'" Now, I am very bothered by the fact that I now need to know which one of which library (and g++/ld) I should use. I thought package managers were supposed to handle the dependency mess for us! Would there be any way to make the compilation command into something like > g++ opencv_test.cpp `pkg-config --cflags --libs opencv4`  i.e. have all relevant files or binaries inside the venv? For example, is there a way to modify the mamba create command (see top) so that this condition is satisfied? Note: I am tagging both Anaconda, Linux, and OpenCV because I have absolutely no idea what I can use to reach a solution.

Answer: You need pkg-config to also be in the env. The following should work: mamba create -n opencv -c conda-forge opencv matplotlib pkg-config mamba activate opencv g++ opencv_test.cpp `pkg-config --cflags --libs opencv4`
Question: I am a beginner in Python. I am learning currently how to Modify an element with index MyCode: data = [4, 6, 8] for index, value in enumerate(data): data[index] = data[index] + 1 value = value * 2 print(data) print(value)  Terminal Output: [5, 7, 9] 16  Terminal Output Expectation: [5, 7, 9] [8, 12, 16]  Why didn’t I get my expected output? Now I have tried to define an empty_list before my FOR loop and then add the VALUE variable to it, then print(the_empty_list) Here: data = [4, 6, 8] empty_list = [] for index, value in enumerate(data): data[index] = data[index] + 1 value = value * 2 **(How to add VALUE to the empty_list)** print(data) print(empty_list)  ** **I am finding the asterisked part difficult. Who knows what I should do here?

Answer: If your list is empty you can't access it with an index. Use append method to add new elements: data = [4, 6, 8] empty_list = [] for index, value in enumerate(data): data[index] = data[index] + 1 value = value * 2 empty_list.append(value) print(data) print(empty_list)  Output: [5, 7, 9] [8, 12, 16]
Question: I'm trying to make string.replace replace all words except ones starting with a certain character, e.g. ~, but if i have a word with only one letter, that letter gets deleted from the words i want to stay Code import re st1 = "dolphin fish ~shark eel octopus ~squid a" for i in re.findall(r"\b(?<!~)\w+", st1): st1 = st1.replace(i, "") print(st1)  What I want to happen: Input: "dolphin fish ~shark eel octopus ~squid a" Output: "~shark ~squid" What happens: Input: "dolphin fish ~shark eel octopus ~squid a" Output: "~shrk ~squid" I think this is happening because it's replacing all instances of 'a', including the ones within other words. How do I ensure it only deletes the instance intended?

Answer: I would actually suggest using a regex find all approach here: st1 = "dolphin fish ~shark eel octopus ~squid a" matches = re.findall(r'~\w+', st1) output = " ".join(matches) print(output) # ~shark ~squid
Question: I'm currently working on a ray tracing project using Python and have encountered performance issues with rendering the entire scene each time. I want to implement a more efficient rendering approach similar to how Unreal Engine handles it. Specifically, I'm looking for guidance on implementing the following optimizations:  Frustum Culling: I want to avoid rendering objects that are outside the camera's frustum. What is the best way to implement frustum culling in my ray tracing code?  Dynamic Resolution Scaling: I'm interested in rendering each object at a specific resolution based on its distance from the camera. How can I implement dynamic resolution scaling to optimize rendering performance?   I've found a ray tracing code on GitHub, and while it provides a solid foundation, I'm struggling to integrate these optimization techniques into my existing code. Could someone provide guidance or code snippets for achieving these optimizations in a ray tracing context?

Answer: To 1: Frustum culling does not make sense in ray tracing. Frustum culling makes sense for rasterization. Rasterization is a top down approach: For instance you want to render cube. In rasterization you say in a top down approach - to render a cube I just must render it faces. To render a face (quad) of a cube you you just need to render 2 triangles. To render a triangle you must just project it vertices via a projection matrix and then do some clipping. After projection and clipping to render a 2D triangle you must draw its fragments. To draw a fragment you need a Z-Buffer and a Z-Buffer test, maybe some alpha blending. You do rendering in rasterization from top (a cube) down to the fragment/pixel level. When it comes to frustum culling you simply say if the Cube (its 8 vertices) are not within the view frustum I can skip the whole top down approach if projection individual triangles, clipping, etc. Usually you use bounding boxes for more complicated objects to fast reject object outside of the view frustum. Ray tracing is bottom up. You start at the pixel level - you ask one pixel where does your color contribution come from. You trace a ray from the pixel in the scene. Usually, you have a bounding volume hierarchy (BVH). You find out that the ray hits the bounding box of your scene. You go down the hierarchy and find out the ray intersects a triangle. You find out that the triangle belongs to an object (e.g. a cube) which has some specific BRDF. You sample the BRDF and get the color contribution. Frustum culling does not make sense here since rays can bounce around the whole scene (go to a reflecting cube - reflecting ray reflects an object outside of your frustum). I think what you mean is not Frustum Culling, but BVH. There are different ways to implement BVHs. For instance you can use Octrees: https://book.vertexwahn.de/docs/rendering/octree/ To 2: Comes also from rasterization domain. A translation to ray tracing would be to sample objects that far away less than object close to the viewer
Question: When I use PyTorch on the CPU, it works fine. When I try to use the mps device it fails. I'm using miniconda for osx-arm64, and I've tried both python 3.8 and 3.11 and both the stable and nightly PyTorch installs. According to the website (https://pytorch.org/get-started/locally/) mps acceleration is available now without nightly. The code I've written is as follows: import torch mps_device = torch.device("mps") float_32_tensor1 = torch.tensor([3.0, 6.0, 9.0], dtype=torch.float32, device=mps_device, requires_grad=False) float_32_tensor2 = torch.tensor([3.0, 6.0, 9.0], dtype=torch.float32, device=mps_device, requires_grad=False) print(float_32_tensor1.mul(float_32_tensor2))  This results in the following (fairly long) error: https://pastebin.com/svwZj8Ke First line of error is: RuntimeError: Failed to create indexing library, error: Error Domain=MTLLibraryErrorDomain Code=3 "program_source:168:1: error: type 'const constant ulong3 *' is not valid for attribute 'buffer'  How would I go about solving this? edit: meta says pastebin shouldn't be used but the error is too long to include in the question edit 2: Not that torch.backends.mps.is_available() returns true edit 3: seems to work normally on the console but Jupyter has this error

Answer: Jupyter was using the old kernel (the dev one) even though I switched interpreters. Restarting Jupyter with the new anaconda environment (python 3.8 and the release version of PyTorch) works.
Question: While having deep dive into how the built-in unittest.mock was designed, I run into these lines in the official source code of mock.py: class Base(object): _mock_return_value = DEFAULT _mock_side_effect = None def __init__(self, /, *args, **kwargs): pass class NonCallableMock(Base): ... # and later in the init def _init__ (...): ... _safe_super(NonCallableMock, self).__init__( spec, wraps, name, spec_set, parent, _spec_state )  What is the role of Base class besides providing common class attributes _mock_return_value and _mock_side_effect as it has just an empty __init__ ? And why NonCallableMock has to call _safe_super(NonCallableMock, self).__init__(), which I believe is exactly just the empty __init__ method of the Base class ? Thanks a lot for your answers in advance. I tried to understand the code but couldn't see the rationale behind the design.

Answer: It's for multiple inheritance, which unittest.mock uses for classes like class MagicMock(MagicMixin, Mock):. Despite the name, super doesn't mean "call the superclass method". Instead, it finds the next method implementation in a type's method resolution order, which might not come from a parent of the current class when multiple inheritance is involved. When you don't know what method super will call, you have to be a lot more careful about what arguments you pass to that method. Most of the classes in this class hierarchy forward arguments like spec and wraps to the next __init__ implementation when they call _safe_super(...).__init__(...). Even if none of their ancestors need those arguments, a sibling implementation could still need them. If all of the classes were to forward these arguments, then object.__init__ would eventually receive those arguments, and object.__init__ would throw an exception. Some class has to handle the job of specifically not passing those arguments to object.__init__, and that class has to sit at the root of the class hierarchy, right below object itself. Thus, this Base class: a class for all the other classes in this multiple inheritance hierarchy to inherit from, existing almost solely for the purpose of preventing object.__init__ from complaining.
Question: i want to draw a semicircle inside a surface. So i thought to use pygame.draw.arc() but i cant figure out how to draw the arc inside of a surface i have seen the official_docs and am using math.radians() to convert degrees to radians but i cant see the arc. A circle is being drawn tho... import pygame import math pygame.init() width, height = 800, 600 screen = pygame.display.set_mode((width, height)) surf = pygame.Surface((100, 100)) surf.fill("green") rect = surf.get_rect(center=(100, 100)) run = 1 while run: for event in pygame.event.get(): if event.type == pygame.QUIT: run = 0 screen.fill((255, 202, 135)) pygame.draw.circle(surf, (255, 255, 255), (30,30),25) pygame.draw.arc(surf, (255, 255, 255), rect, math.radians(270), math.radians(90)) screen.blit(surf, rect) pygame.display.flip() pygame.quit()  thanks in advance

Answer: The position of the circle segment must of course be relative to the Surface on which the circle segment is drawn and not the absolute position on which the Surface is then placed on the screen: pygame.draw.arc(surf, (255, 255, 255), rect, math.radians(270), math.radians(90)) pygame.draw.arc(surf, (255, 255, 255), (0, 0, 100, 100), math.radians(270), math.radians(90))
Question: I have next pandas DataFrame:  x_1 x_2 x_3 x_4 col_to_replace cor_factor 1 2 3 4 x_2 1 3 3 5 1 x_1 6 2 2 0 0 x_3 0 ...  I want to update the column which name is saved in the col_to_replace with the value from cor_factor and save the result in the corresponded column and also in the car_factor column. Some (ugly) solution could be: for i in len(df.shape[0]): df[df['col_to_replace']].iloc[i] = df[df['col_to_replace']].iloc[i] - df['cor_factor'].iloc[i] df['cor_factor'].iloc[i] = df['cor_factor'].iloc[i] - df[df['col_to_replace']].iloc[i]  This way is absolutely not time efficient. I'm looking for the faster solution. The output for the DF should be in this case:  x_1 x_2 x_3 x_4 col_to_replace cor_factor 1 1 3 4 x_2 -1 -3 3 5 1 x_1 3 2 2 0 0 x_3 0 ... 

Answer: Use a pivot to correct the x_ values and an indexing lookup to correct the last column. Make sure to make a copy before modification since the values change: # perform indexing lookup # save the value for later idx, cols = pd.factorize(df['col_to_replace']) corr = df.reindex(cols, axis=1).to_numpy()[np.arange(len(df)), idx] # pivot and subtract the factor # ensure original order of the columns cols = df.columns.intersection(cols, sort=False) df[cols] = df[cols].sub(df.pivot(columns='col_to_replace', values='cor_factor'), fill_value=0).convert_dtypes() # correct with the saved "corr" df['cor_factor'] -= corr  Output:  x_1 x_2 x_3 x_4 col_to_replace cor_factor 0 1 1 3 4 x_2 -1 1 -3 3 5 1 x_1 3 2 2 2 0 0 x_3 0
Question: I have this code: from collections import Counter import numpy as np def make_data(N): np.random.seed(40) g = np.random.randint(-3, 4, (N, N)) return g N = 100 g = make_data(N) n = g.shape[0] sum_dist = Counter() for i in range(n): for j in range(n): dist = i**2 + j**2 sum_dist[dist] += g[i, j] sorted_dists = sorted(sum_dist.keys()) for i in range(1, len(sorted_dists)): sum_dist[sorted_dists[i]] += sum_dist[sorted_dists[i-1]] # print(sum_dist) print(max(sum_dist, key=sum_dist.get))  The output is 7921. I want to convert it into numpy only code and get rid of Counter. How can I do that?

Answer: Can you just make sum_dist into an array, since you know its maximum index? sum_dist = np.zeros(2 * N * N, dtype=int) for i in range(n): for j in range(n): dist = i**2 + j**2 sum_dist[dist] += g[i, j] print(np.argmax(np.cumsum(sum_dist)))
Question: I have a dataframe with a Date column consisting of stings in this format. I need to strip the end of the string so that I can convert to a datetime object. "20231101 05:00:00 America/New_York" "20231101 06:00:00 America/New_York"  I have tried these approaches unsuccessfully. df['Date'] = df['Date'].replace('^.*\]\s*', '', regex=True) df['Date'] = df['Date'].str.strip(' America/New_York') df['Date'] = df['Date'].map(lambda x: x.rstrip(' America/NewYork'))``  as well as a couple of others based on my searches. Is there an easy way to do this or should I write a function to slice the string by grabbing the first 17 characters and assigning the result back to the df. Note the string could be of the form '20231101 05:00:00 America/Central' Thanks for any and all assistance.

Answer:  If you want to remove a particular suffix, then I recommend str.removesuffix rather than str.strip. Notice that you sometimes write New_York with an underscore and sometimes NewYork without an underscore. If you ask to remove 'NewYork' then 'New_York' won't be removed. After the edit in your question, the suffixes all start with ' America' but differ afterwards; in this case you could use str.split(' America').str[0] to keep everything before ' America'.  import pandas as pd df = pd.DataFrame({ 'Name': ['Alice', 'Bob', 'Charlie'], 'Date': ["20231101 05:00:00 America/New_York", "20231101 06:00:00 America/New_York", "20231101 07:00:00 America/Central"] }) # df['Date'] = df['Date'].str.removesuffix(' America/New_York') df['Date'] = df['Date'].str.split(' America').str[0] print(df) # Name Date # 0 Alice 20231101 05:00:00 # 1 Bob 20231101 06:00:00 # 2 Charlie 20231101 07:00:00