eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I'm trying to (roughly) equally space the points of a line to a predefined distance. It's ok to have some tolerance between the distances but as close as possible would be desirable. I know I could manually iterate through each point in my line and check the p1 distance vs p2 and add more points if needed. But I wondered if anyone knows if there is a way to achieve this with shapely as I already have the coords in a LineString. 

Answer: One way to do that is to use interpolate method that returns points at specified distances along the line. You just have to generate a list of the distances somehow first. Taking the input line example from Roy2012's answer: import numpy as np from shapely.geometry import LineString from shapely.ops import unary_union line = LineString(([0, 0], [2, 1], [3, 2], [3.5, 1], [5, 2]))   Splitting at a specified distance: distance_delta = 0.9 distances = np.arange(0, line.length, distance_delta) # or alternatively without NumPy: # points_count = int(line.length // distance_delta) + 1 # distances = (distance_delta * i for i in range(points_count)) points = [line.interpolate(distance) for distance in distances] + [line.boundary[1]] multipoint = unary_union(points) # or new_line = LineString(points)   Note that since the distance is fixed you can have problems at the end of the line as shown in the image. Depending on what you want you can include/exclude the [line.boundary[1]] part which adds the line's endpoint or use distances = np.arange(0, line.length, distance_delta)[:-1] to exclude the penultimate point. Also, note that the unary_union I'm using should be more efficient than calling object.union(other) inside a loop, as shown in another answer. Splitting to a fixed number of points: n = 7 # or to get the distances closest to the desired one: # n = round(line.length / desired_distance_delta) distances = np.linspace(0, line.length, n) # or alternatively without NumPy: # distances = (line.length * i / (n - 1) for i in range(n)) points = [line.interpolate(distance) for distance in distances] multipoint = unary_union(points) # or new_line = LineString(points)
Question: I am making an app in kotlin. But I know python a lot and made the logic in python. The kotlin is only used for the display. Is there a way to call a python function in kotlin?. A python script can call python scripts but can a Kotlin script call one?

Answer: I think there are two normal solutions.  Using Polyglot API of GraalVM (https://www.graalvm.org/sdk/javadoc/org/graalvm/polyglot/package-summary.html).  Creating a C interface implemented with Python (https://www.linuxjournal.com/article/8497) and calling it with JNI (https://docs.oracle.com/javase/8/docs/technotes/guides/jni/). However, this option needs you to manage your objects and memory very carefully (dispose all C pointers properly etc.)
Question: I having replace issue while I try to replace a string with value from another column. I want to replace 'Length' with df['Length']. df["Length"]= df["Length"].replace('Length', df['Length'], regex = True)  Below is my data Input: **Formula** **Length** Length 5 Length+1.5 6 Length-2.5 5 Length 4 5 5 Expected Output: **Formula** **Length** 5 5 6+1.5 6 5-2.5 5 4 4 5 5  However, with the code I used above, it will replace my entire cell instead of Length only. I getting below output: I found it was due to df['column'] is used, if I used any other string the behind offset (-1.5) will not get replaced. **Formula** **Length** 5 5 6 6 5 5 4 4 5 5  May I know is there any replace method for values from other columns? Thank you.

Answer: If want replace by another column is necessary use DataFrame.apply: df["Formula"]= df.apply(lambda x: x['Formula'].replace('Length', str(x['Length'])), axis=1) print (df) Formula Length 0 5 5 1 6+1.5 6 2 5-2.5 5 3 4 4 4 5 5  Or list comprehension: df["Formula"]= [x.replace('Length', str(y)) for x, y in df[['Formula','Length']].to_numpy()]
Question: I have some vertices whose coordinates were stored as NumPy array. xyz_np: array([[ 7, 53, 31], [ 61, 130, 116], [ 89, 65, 120], ..., [ 28, 72, 88], [ 77, 65, 82], [117, 90, 72]], dtype=int32)  I want to save these vertices as a point cloud file(such as .ply) and visualize it in Blender. I don't have face information.

Answer: You can use Open3D to do this. # Pass numpy array to Open3D.o3d.geometry.PointCloud and visualize xyz = np.random.rand(100, 3) pcd = o3d.geometry.PointCloud() pcd.points = o3d.utility.Vector3dVector(xyz) o3d.io.write_point_cloud("./data.ply", pcd)  You can also visualize the point cloud using Open3D. o3d.visualization.draw_geometries([pcd])
Question: I am trying to build an app using Dash in Python based on Plotly. I am having hard time in adjusting the width of Dropdown menu options. I have attached code and image below. I would like the width of Dropdown options to be same as the menu width.  app.layout = html.Div(children=[ html.H1(children='Welcome to Portfolio Construction Engine!'), html.Div(children='What would you like to do?', style={ 'font-style': 'italic', 'font-weight': 'bold' }), html.Div([ dcc.Dropdown( id='demo-dropdown', options=[ {'label': 'Upload Scores', 'value': 'upload_scores'}, {'label': 'Analyze Portfolios', 'value': 'analyze_portfoliio'}, {'label': 'Generate Data for IC Engine', 'value': 'gen_data_ic_engine'} ], placeholder='Select a task...', style={ 'width': '50%' } ), html.Div(id='dd-output-container') ]) ]) 

Answer: While its correct to place width: 50% to change the width of the dropdown component, you've placed it in the inner component rather than the parent Div. app.layout = html.Div( children=[ html.H1(children="Welcome to Portfolio Construction Engine!"), html.Div( children="What would you like to do?", style={"font-style": "italic", "font-weight": "bold"}, ), html.Div( [ dcc.Dropdown( id="demo-dropdown", options=[ {"label": "Upload Scores", "value": "upload_scores"}, {"label": "Analyze Portfolios", "value": "analyze_portfoliio"}, { "label": "Generate Data for IC Engine", "value": "gen_data_ic_engine", }, ], placeholder="Select a task...", # style={"width": "50%"}, NOT HERE ), html.Div(id="dd-output-container"), ], style={"width": "50%"}, ), ] )
Question: I might have misunderstood the requests.session object. headers ={'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.28 Safari/537.36'} s = requests.Session() r = s.get('https://www.barchart.com/', headers = headers) print(r.status_code)  This works fine and return 200 as expected. However this following return 403 and shows that the headers from the first request have not been saved in the session like it would manually using a browser: headers ={'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.28 Safari/537.36'} s = requests.Session() r = s.get('https://www.barchart.com/', headers = headers) r = s.get('https://www.barchart.com/futures/quotes/CLQ20') print(r.status_code) print(s.headers)  I thought there would be a way to compound headers, cookies etc from 1 requests to the other using the session object... am i wrong ?

Answer: You can use session.headers (doc) property to specify headers that are sent with each request: import requests headers ={'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.28 Safari/537.36'} s = requests.session() s.headers = headers # <-- set default headers here r = s.get('https://www.barchart.com/') print(r.status_code) print(s.headers) print('-' * 80) r = s.get('https://www.barchart.com/futures/quotes/CLQ20') print(r.status_code) print(s.headers) s.close()  Prints: 200 {'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.28 Safari/537.36'} -------------------------------------------------------------------------------- 200 {'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.28 Safari/537.36'}
Question: I have the following script in Python. I am calculating the Fourier Transform of an array. When I want to plot the results (Fourier transform) I am using the absolute value of that calculation. However, I do not know how the absolute value of complex numbers is being produced. Does anyone know how it calculates? I need this to reproduce in Java. import numpy as np import matplotlib.pyplot as plt from numpy import fft inp = [1,2,3,4] res = fft.fft(inp) print(res[1]) # returns (-2+2j) complex number print(np.abs(res[1])) # returns 2.8284271247461903 

Answer: sqrt(Re(z)**2 + Im(z)**2)  for z = a + ib this becomes: sqrt(a*a + b*b)  It's just the euclidean norm. You have to sum the square of real part and imaginary part (without the i) and do the sqrt of it.  https://www.varsitytutors.com/hotmath/hotmath_help/topics/absolute-value-complex-number
Question: I currently have a Python3 project set up with Poetry as the main package manager. Next to that I also have set up a build and some automated testing via Github workflows. My package depends on Tensorflow, although the automated tests can run without it. Unfortunately Tensorflow (which is quite big) is installed every time the Github workflow runs these tests. As Tensorflow is not needed for these tests and as I would like to speed up my build, I would like to ignore the Tensorflow dependency when poetry install is called from the build pipeline. Does anybody know a way to exclude a dependency when using Poetry?

Answer: The only other approach that comes to mind would be to move the tensorflow dependency to an extra category, which in poetry would look like this: $ poetry add --extras tensorflow  This means that it won't be installed when you run poetry install, unless it is part of a named group that you install explicitly. This can be achieved by adding this to your pyproject.toml: [tool.poetry.extras] runtime = ["tensorflow"] # any name goes, I chose "runtime" because it sounded like it'd make sense  The list can be extended with any other package that you only need during runtime, and not during tests. If you want to install your code to actually run it, you'll have to execute this before: $ poetry install --extras runtime   This will cleanly separate your dependencies, you'll have to evaluate whether it makes sense in your case. As a rule of thumb, it's usually better to run hacks to make tests work rather than worsen the client experience, so your current workflow has a good chance of being better than what I just wrote.
Question: Not sure if title is correct terminology. If you have to compare the characters in 2 strings (A,B) and count the number of matches of chars in B against A: sum([ch in A for ch in B])  is faster on %timeit than sum(ch in A for ch in B)  I understand that the first one will create a list of bool, and then sum the values of 1. The second one is a generator. I'm not clear on what it is doing internally and why it is slower? Thanks. Edit with %timeit results: 10 characters generator expression list 10000 loops, best of 3: 112 µs per loop 10000 loops, best of 3: 94.6 µs per loop 1000 characters generator expression list 100 loops, best of 3: 8.5 ms per loop 100 loops, best of 3: 6.9 ms per loop 10,000 characters generator expression list 10 loops, best of 3: 87.5 ms per loop 10 loops, best of 3: 76.1 ms per loop 100,000 characters generator expression list 1 loop, best of 3: 908 ms per loop 1 loop, best of 3: 840 ms per loop

Answer: I took a look at the disassembly of each construct (using dis). I did this by declaring these two functions: def list_comprehension(): return sum([ch in A for ch in B]) def generation_expression(): return sum(ch in A for ch in B)  and then calling dis.dis with each function. For the list comprehension:  0 BUILD_LIST 0 2 LOAD_FAST 0 (.0) 4 FOR_ITER 12 (to 18) 6 STORE_FAST 1 (ch) 8 LOAD_FAST 1 (ch) 10 LOAD_GLOBAL 0 (A) 12 COMPARE_OP 6 (in) 14 LIST_APPEND 2 16 JUMP_ABSOLUTE 4 18 RETURN_VALUE  and for the generator expression:  0 LOAD_FAST 0 (.0) 2 FOR_ITER 14 (to 18) 4 STORE_FAST 1 (ch) 6 LOAD_FAST 1 (ch) 8 LOAD_GLOBAL 0 (A) 10 COMPARE_OP 6 (in) 12 YIELD_VALUE 14 POP_TOP 16 JUMP_ABSOLUTE 2 18 LOAD_CONST 0 (None) 20 RETURN_VALUE  The disassembly for the actual summation is:  0 LOAD_GLOBAL 0 (sum) 2 LOAD_CONST 1 (<code object <genexpr> at 0x7f49dc395240, file "/home/mishac/dev/python/kintsugi/KintsugiModels/automated_tests/a.py", line 12>) 4 LOAD_CONST 2 ('generation_expression.<locals>.<genexpr>') 6 MAKE_FUNCTION 0 8 LOAD_GLOBAL 1 (B) 10 GET_ITER 12 CALL_FUNCTION 1 14 CALL_FUNCTION 1 16 RETURN_VALUE  but this sum disassembly was constant between both your examples, with the only difference being the loading of generation_expression.<locals>.<genexpr> vs list_comprehension.<locals>.<listcomp> (so just loading a different local variable). The differing bytecode instructions between the first two disassemblies are LIST_APPEND for the list comprehension vs. the conjunction of YIELD_VALUE and POP_TOP for the generator expression. I won't pretend I know the intrinsics of Python bytecode, but what I gather from this is that the generator expression is implemented as a queue, where the value is generated and then popped. This popping doesn't have to happen in a list comprehension, leading me to believe there'll be a slight amount of overhead in using generators. Now this doesn't mean that generators are always going to be slower. Generators excel at being memory-efficient, so there will be a threshold N such that list comprehensions will perform slightly better before this threshold (because memory use won't be a problem), but after this threshold, generators will significantly perform better.
Question: I have installed pyenv on a raspberry pi but now I want to uninstall it. I already ran the command rm -rf $(pyenv root) but now it says to delete lines from my "shell startup configuration". What does it mean? I found this lines in my .bash_profile files: if command -v pyenv 1>/dev/null 2>&1; then eval"$(pyenv init-)" fi  And in .bashrc file at the end there is: export PYENV_ROOT="$HOME/.pyenv" export PATH="$PYENV_ROOT/bin:$PATH"  Should I only delete (or comment with #) the ones from .bash_profile? Or maybe from both files?

Answer: Delete all 5 lines from both files. Then make sure that in your .bash_profile it tells .bashrc to load
Question: I am a setting up Poetry in combination with Tox to automate builds and testing. The project I am working on however is private and I want to avoid anyone working on it accidentally publishing it to PyPi. I have initialized a project using poetry init and my assumption is that the resulting setup does not result in a viable package that can be published without any further setup to begin with. Is this correct? How could I further configure poetry so that even if someone accidentally runs poetry publish in the future the package will not actually be published.

Answer: As I know poetry does not support such straightforward option yet. But the workaround is possible: [tool.poetry] exclude = ["**"]  In TOML format: * denotes a single level wildcard, and ** denotes all files in the given directory hierarchy. exclude = ["**"] option prevents project files from getting into the package when poetry build is executed. It will show: [ModuleOrPackageNotFound] No file/folder found for package package_name  But nevertheless, poetry will create a tar.gz file and include three files in it: pyproject.toml, setup.py, and PKG-INFO. And it can be published
Question: Here is my code: import plotly.graph_objects as go fig = go.Figure() fig.add_trace(go.Bar( name='Group 1', x=['Var 1', 'Var 2', 'Var 3'], y=[3, 6, 4], error_y=dict(type='data', array=[1, 0.5, 1.5]), width=0.15 )) fig.add_trace(go.Bar( name='Group 2', x=['Var 1', 'Var 2', 'Var 3'], y=[4, 7, 3], error_y=dict(type='data', array=[0.5, 1, 2]), width=0.15 )) fig.update_layout(barmode='group') fig.show()  output:   I read this but code from seems depreciated and does not works. Question: How to set background color of the plot and color for grid lines?

Answer: This should help import plotly.graph_objects as go from plotly.graph_objects import Layout # Set layout with background color you want (rgba values) # This one is for white background layout = Layout(plot_bgcolor='rgba(0,0,0,0)') # Use that layout here fig = go.Figure(layout=layout) fig.add_trace(go.Bar( name='Group 1', x=['Var 1', 'Var 2', 'Var 3'], y=[3, 6, 4], error_y=dict(type='data', array=[1, 0.5, 1.5]), width=0.15 )) fig.add_trace(go.Bar( name='Group 2', x=['Var 1', 'Var 2', 'Var 3'], y=[4, 7, 3], error_y=dict(type='data', array=[0.5, 1, 2]), width=0.15 )) fig.update_layout(barmode='group') # Change grid color and axis colors fig.update_xaxes(showline=True, linewidth=2, linecolor='black', gridcolor='Red') fig.update_yaxes(showline=True, linewidth=2, linecolor='black', gridcolor='Red') fig.show()   Sources:  Grid line customization Background Customization
Question: I have a function with the following signature: def wait_for_namespaced_objects_condition( obj_type: Type[NamespacedAPIObject], obj_condition_fun: Callable[[NamespacedAPIObject], bool], ) -> List[NamespacedAPIObject]: ...  Important part here are NamespacedAPIObject parameters. This function takes an obj_type as type spec, then creates an object(instance) of that type(class). Then some other objects of that type are added to a list, which is then filtered with obj_condition_fun and returned as a result of type List[NamespacedAPIObject]. This works fine and also evaluates OK with mypy`. Now, I want to make this function generic, so that in the place of NamespacedAPIObject any subtype of it can be used. My attempt was to do it like this: T = TypeVar("T", bound=NamespacedAPIObject) def wait_for_namespaced_objects_condition( obj_type: Type[T], obj_condition_fun: Callable[[T], bool], ) -> List[T]:  But Type[T] is TypeVar, so it's not the way to go. The question is: what should be the type of obj_type parameter to make this work? I tried Generic[T], which seemed the most reasonable to me, but it doesn't work. If i put there just obj_type: T, mypy evaluates this OK, but it seems to wrong to me. In my opinion this means that obj_type is an instance of a class that is a subtype of NamespacedAPIObject, while what I want to say is that "T is a class variable that represents subtype of NamespacedAPIObject. How to solve this? [Edit] To explain a little bit better (see comment below) why Type[T] doesn't work for me: in my case all subtype of T implement class method objects(), but when I try to write my code like this: obj_type.objects()  mypy returns: pytest_helm_charts/utils.py:36: error: "Type[T]" has no attribute "objects" 

Answer:  But Type[T] is TypeVar, so it's not the way to go.  No, you are on the right track - TypeVar is definitely the way to go. The problem here is rather in pykube.objects.APIObject class being wrapped in a decorator that mypy cannot deal with yet. Adding type stubs for pykube.objects will resolve the issue. Create a directory _typeshed/pykube and add minimal type stubs for pykube:  _typeshed/pykube/__init__.pyi: from typing import Any def __getattr__(name: str) -> Any: ... # incomplete   _typeshed/pykube/objects.pyi: from typing import Any, ClassVar, Optional from pykube.query import Query def __getattr__(name: str) -> Any: ... # incomplete class ObjectManager: def __getattr__(self, name: str) -> Any: ... # incomplete def __call__(self, api: Any, namespace: Optional[Any] = None) -> Query: ... class APIObject: objects: ClassVar[ObjectManager] def __getattr__(self, name: str) -> Any: ... # incomplete class NamespacedAPIObject(APIObject): ...    Now running $ MYPYPATH=_typeshed mypy pytest_helm_charts/  resolves obj_type.objects correctly: T = TypeVar('T', bound=NamespacedAPIObject) def wait_for_namespaced_objects_condition(obj_type: Type[T]) -> List[T]: reveal_type(obj_type.objects)  Output: pytest_helm_charts/utils.py:29: note: Revealed type is 'pykube.objects.ObjectManager'
Question: I'm trying to write a nice error handler for my code, so that when it fails, the logs, traceback and other relevant info get emailed to me. I can't figure out how to take an exception object and extract the traceback. I find the traceback module pretty confusing, mostly because it doesn't deal with exceptions at all. It just fetches some global variables from somewhere, assuming that I want the most recent exception. But what if I don't? What if I want to ignore some exception in my error handler? (e.g. if I fail to send me email and want to retry.) What I want import traceback as tb # some function that will fail after a few recursions def myfunc(x): assert x > 0, "oh no" return myfunc(x-1) try: myfunc(3) except Exception as e: traceback_str = tb.something(e)  Note that tb.something takes e as an argument. There's lots of questions on Stack Overflow about using the traceback module to get a traceback string. The unique thing about this question is how to get it from the caught exception, instead of global variables. Result: traceback_str contains the string: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in myfunc File "<stdin>", line 3, in myfunc File "<stdin>", line 3, in myfunc File "<stdin>", line 2, in myfunc AssertionError: oh no  Note that it contains not just the most recent function call, but the whole stack, and includes the "AssertionError" at the end

Answer: The correct function for tb.something(e) is ''.join(tb.format_exception(None, e, e.__traceback__))
Question: In Visual Studio Code, with git extensions installed, how do you add files or complete folders to the .gitignore file so the files do not show up in untracked changes. Specifically, using Python projects, how do you add the pycache folder and its contents to the .gitignore. I have tried right-clicking in the folder in explorer panel but the pop-menu has no git ignore menu option. Thanks in advance. I know how to do it from the command line. Yes, just edit the .gitignore file. I was just asking how it can be done from within VS Code IDE using the git extension for VS Code.

Answer: So after further investigation, it is possible to add files from the pycache folder to the .gitignore file from within VS Code by using the list of untracked changed files in the 'source control' panel. You right-click a file and select add to .gitignore from the pop-up menu. You can't add folders but just the individual files.
Question: Both pandas.Series.map and pandas.Series.replace seem to give the same result. Is there a reason for using one over the other? For example: import pandas as pd df = pd.Series(['Yes', 'No']) df 0 Yes 1 No dtype: object  df.replace(to_replace=['Yes', 'No'], value=[True, False]) 0 True 1 False dtype: bool  df.map({'Yes':True, 'No':False}) 0 True 1 False dtype: bool  df.replace(to_replace=['Yes', 'No'], value=[True, False]).equals(df.map({'Yes':True, 'No':False})) True 

Answer: Both of these methods are used for substituting values. From Series.replace docs:  Replace values given in to_replace with value.  From Series.map docs:  Used for substituting each value in a Series with another value, that may be derived from a function, a dict or a Series.  They differ in the following:  replace accepts str, regex, list, dict, Series, int, float, or None. map accepts a dict or a Series. They differ in handling null values. replace uses re.sub under the hood.The rules for substitution for re.sub are the same.  Take below example: In [124]: s = pd.Series([0, 1, 2, 3, 4]) In [125]: s Out[125]: 0 0 1 1 2 2 3 3 4 4 dtype: int64 In [126]: s.replace({0: 5}) Out[126]: 0 5 1 1 2 2 3 3 4 4 dtype: int64 In [129]: s.map({0: 'kitten', 1: 'puppy'}) Out[129]: 0 kitten 1 puppy 2 NaN 3 NaN 4 NaN dtype: object   As you can see for s.map method, values that are not found in the dict are converted to NaN, unless the dict has a default value (e.g. defaultdict)   For s.replace, it just replaces the value to be replaced keeping the rest as it is.