eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I want to add a text box to a presentation with python pptx. I would like to add a text box with several paragraphs in the specific place and then format it (fonts, color, etc.). But since text shape object always comes with the one paragraph in the beginning, I cannot edit first of my paragraphs. The code sample looks like this:  txBox = slide.shapes.add_textbox(left, top, width, height) tf = txBox.text_frame p = tf.add_paragraph() p.text = "This is a first paragraph" p.font.size = Pt(11) p = tf.add_paragraph() p.text = "This is a second paragraph" p.font.size = Pt(11)  Which creates output like this:  I can add text to this first line with tf.text = "This is text inside a textbox", but it won't be editable in terms of fonts or colors. So is there any way how I can omit or edit that line, so all paragraphs in the box would be the same?

Answer: Access the first paragraph differently, using: p = tf.paragraphs[0]  Then you can add runs, set fonts and all the rest of it just like with a paragraph you get back from tf.add_paragraph().
Question: I'm trying to do this (see title), but it's a bit complicated since the string I'm trying to build has to have the following properties:  mulitiline contains curly braces I want to inject variables into it  Using a normal '''''' multiline string makes injecting variables difficult. Using multiple f-strings makes injecting variables easy, but every curly brace, of which there are a lot, has to be doubled. And an f has to be prepended to each line. On the other hand, if I try using format, it also gets confused by all the curly braces. Is there a better way that I haven't considered yet?

Answer: You can use the """ multiline string method. For injecting variables, make sure to use the $ sign while defining the string and use the variables object in the JSON parameter of the requests.post method. Here is an example. ContactInput is one of the types I defined in my GraphQL schema. query = """ mutation ($input:[ContactInput!]!) { AddContacts(contacts: $input) { user_id } } """ variables = {'input': my_arrofcontacts} r = requests.post(url, json={'query': query , 'variables': variables})
Question: I have this list of dicts: [{'score': '1.9', 'id': 756, 'factors': [1.25, 2.25, 2.5, 2.0, 1.75]}, {'score': '2.0', 'id': 686, 'factors': [2.0, 2.25, 2.75, 1.5, 2.25]}, {'score': '2.0', 'id': 55, 'factors': [1.5, 3.0, 2.5, 1.5, 1.5]}, {'score': '1.9', 'id': 863, 'factors': [1.5, 3.0, 1.5, 2.5, 1.5]}]  I can sort by score with : sorted(l, key=lambda k: k['score'], reverse=True). However I have tied scores. How can I sort first by score and then by id asc or desc? 

Answer: You can sort by a tuple: sorted(l, key=lambda k: (float(k['score']), k['id']), reverse=True)  This will sort by score descending, then id descending. Note that since score is a string value, it needs to be converted to float for comparison. [ {'score': '2.0', 'id': 686, 'factors': [2.0, 2.25, 2.75, 1.5, 2.25]}, {'score': '2.0', 'id': 55, 'factors': [1.5, 3.0, 2.5, 1.5, 1.5]}, {'score': '1.9', 'id': 863, 'factors': [1.5, 3.0, 1.5, 2.5, 1.5]}, {'score': '1.9', 'id': 756, 'factors': [1.25, 2.25, 2.5, 2.0, 1.75]} ]  To sort by id ascending, use -k['id'] (sorting negated numbers descending is equivalent to sorting the non-negated numbers ascending): sorted(l, key=lambda k: (float(k['score']), -k['id']), reverse=True) [ {'score': '2.0', 'id': 55, 'factors': [1.5, 3.0, 2.5, 1.5, 1.5]}, {'score': '2.0', 'id': 686, 'factors': [2.0, 2.25, 2.75, 1.5, 2.25]}, {'score': '1.9', 'id': 756, 'factors': [1.25, 2.25, 2.5, 2.0, 1.75]}, {'score': '1.9', 'id': 863, 'factors': [1.5, 3.0, 1.5, 2.5, 1.5]} ]
Question: I have a Python library written in Rust with PyO3, and it involves some expensive calculations (up to 10 minutes for a single function call). How can I abort the execution when calling from Python ? Ctrl+C seems to only be handled after the end of the execution, so is essentially useless. Minimal reproducible example: # Cargo.toml [package] name = "wait" version = "0.0.0" authors = [] edition = "2018" [lib] name = "wait" crate-type = ["cdylib"] [dependencies.pyo3] version = "0.10.1" features = ["extension-module"]  // src/lib.rs use pyo3::wrap_pyfunction; #[pyfunction] pub fn sleep() { std::thread::sleep(std::time::Duration::from_millis(10000)); } #[pymodule] fn wait(_py: Python, m: &PyModule) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(sleep)) }  $ rustup override set nightly $ cargo build --release $ cp target/release/libwait.so wait.so $ python3 >>> import wait >>> wait.sleep()  Immediately after having entered wait.sleep() I type Ctrl + C, and the characters ^C are printed to the screen, but only 10 seconds later do I finally get >>> wait.sleep() ^CTraceback (most recent call last): File "<stdin>", line 1, in <module> KeyboardInterrupt >>>  The KeyboardInterrupt was detected, but was left unhandled until the end of the call to the Rust function. Is there a way to bypass that ? The behavior is the same when the Python code is put in a file and executed from outside the REPL.

Answer: One option would be to spawn a separate process to run the Rust function. In the child process, we can set up a signal handler to exit the process on interrupt. Python will then be able to raise a KeyboardInterrupt exception as desired. Here's an example of how to do it: // src/lib.rs use pyo3::prelude::*; use pyo3::wrap_pyfunction; use ctrlc; #[pyfunction] pub fn sleep() { ctrlc::set_handler(|| std::process::exit(2)).unwrap(); std::thread::sleep(std::time::Duration::from_millis(10000)); } #[pymodule] fn wait(_py: Python, m: &PyModule) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(sleep)) }  # wait.py import wait import multiprocessing as mp def f(): wait.sleep() p = mp.Process(target=f) p.start() p.join() print("Done")  Here's the output I get on my machine after pressing CTRL-C: $ python3 wait.py ^CTraceback (most recent call last): File "wait.py", line 9, in <module> p.join() File "/home/kerby/miniconda3/lib/python3.7/multiprocessing/process.py", line 140, in join res = self._popen.wait(timeout) File "/home/kerby/miniconda3/lib/python3.7/multiprocessing/popen_fork.py", line 48, in wait return self.poll(os.WNOHANG if timeout == 0.0 else 0) File "/home/kerby/miniconda3/lib/python3.7/multiprocessing/popen_fork.py", line 28, in poll pid, sts = os.waitpid(self.pid, flag) KeyboardInterrupt
Question: I have a python discord bot and I need it to get user input after a command, how can I do this? I am new to python and making discord bots. Here is my code: import discord, datetime, time from discord.ext import commands from datetime import date, datetime prefix = "!!" client = commands.Bot(command_prefix=prefix, case_insensitive=True) times_used = 0 @client.event async def on_ready(): print(f"I am ready to go - {client.user.name}") await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=f"{client.command_prefix}python_help. This bot is made by drakeerv.")) @client.command(name="ping") async def _ping(ctx): global times_used await ctx.send(f"Ping: {client.latency}") times_used = times_used + 1 @client.command(name="time") async def _time(ctx): global times_used from datetime import date, datetime now = datetime.now() if (now.strftime("%H") <= "12"): am_pm = "AM" else: am_pm = "PM" datetime = now.strftime("%m/%d/%Y, %I:%M") await ctx.send("Current Time:" + ' ' + datetime + ' ' + am_pm) times_used = times_used + 1 @client.command(name="times_used") async def _used(ctx): global times_used await ctx.send(f"Times used since last reboot:" + ' ' + str(times_used)) times_used = times_used + 1 @client.command(name="command") #THIS LINE async def _command(ctx): global times_used await ctx.send(f"y or n") times_used = times_used + 1 @client.command(name="python_help") async def _python_help(ctx): global times_used msg = '\r\n'.join(["!!help: returns all of the commands and what they do.", "!!time: returns the current time.", "!!ping: returns the ping to the server."]) await ctx.send(msg) times_used = times_used + 1 client.run("token")  I am using python version 3.8.3. I have already looked at other posts but they did not answer my question or gave me errors. Any help would be greatly appreciated!

Answer: You'll be wanting to use Client.wait_for(): @client.command(name="command") async def _command(ctx): global times_used await ctx.send(f"y or n") # This will make sure that the response will only be registered if the following # conditions are met: def check(msg): return msg.author == ctx.author and msg.channel == ctx.channel and \ msg.content.lower() in ["y", "n"] msg = await client.wait_for("message", check=check) if msg.content.lower() == "y": await ctx.send("You said yes!") else: await ctx.send("You said no!") times_used = times_used + 1  And with a timeout: import asyncio # To get the exception @client.command(...) async def _command(ctx): # code try: msg = await client.wait_for("message", check=check, timeout=30) # 30 seconds to reply except asyncio.TimeoutError: await ctx.send("Sorry, you didn't reply in time!")   References:  Client.wait_for() - More examples in here Message.author Message.channel Message.content asyncio.TimeoutError
Question: I'm trying to calculate and show a convex hull for some random points in python. This is my current code: import numpy as np import random import matplotlib.pyplot as plt import cv2 points = np.random.rand(25,2) hull = ConvexHull(points) plt.plot(points[:,0], points[:,1], 'o',color='c') for simplex in hull.simplices: plt.plot(points[simplex, 0], points[simplex, 1], 'r') plt.plot(points[hull.vertices,0], points[hull.vertices,1], 'r', lw=-1) plt.plot(points[hull.vertices[0],0], points[hull.vertices[0],1], 'r-') plt.show()  My questions:  How can I change the X,Y labels and points limit between 0 to 9? For example (0,1,2,3,4,5,6,7,8,9) How can I mark the points on the convex hull with a circle? As in example 

Answer: Replacing np.rand() with randint(0, 10) will generate the coordinates as integers from 0,1,... to 9. Using '.' as marker will result in smaller markers for the given points. Using 'o' as marker, setting a markeredgecolor and setting the main color to 'none' will result in a circular marker, which can be used for the points on the hull. The size of the marker can be adapted via markersize=. fig, axes = plt.subplots(ncols=..., nrows=...) is a handy way to create multiple subplots. Here is some code for a minimal example: from scipy.spatial import ConvexHull import matplotlib.pyplot as plt import numpy as np points = np.random.randint(0, 10, size=(15, 2)) # Random points in 2-D hull = ConvexHull(points) fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(10, 3)) for ax in (ax1, ax2): ax.plot(points[:, 0], points[:, 1], '.', color='k') if ax == ax1: ax.set_title('Given points') else: ax.set_title('Convex hull') for simplex in hull.simplices: ax.plot(points[simplex, 0], points[simplex, 1], 'c') ax.plot(points[hull.vertices, 0], points[hull.vertices, 1], 'o', mec='r', color='none', lw=1, markersize=10) ax.set_xticks(range(10)) ax.set_yticks(range(10)) plt.show()   PS: To show the plots in separate windows: from scipy.spatial import ConvexHull import matplotlib.pyplot as plt import numpy as np points = np.random.randint(0, 10, size=(15, 2)) # Random points in 2-D hull = ConvexHull(points) for plot_id in (1, 2): fig, ax = plt.subplots(ncols=1, figsize=(5, 3)) ax.plot(points[:, 0], points[:, 1], '.', color='k') if plot_id == 1: ax.set_title('Given points') else: ax.set_title('Convex hull') for simplex in hull.simplices: ax.plot(points[simplex, 0], points[simplex, 1], 'c') ax.plot(points[hull.vertices, 0], points[hull.vertices, 1], 'o', mec='r', color='none', lw=1, markersize=10) ax.set_xticks(range(10)) ax.set_yticks(range(10)) plt.show()
Question: In python 3.9, dictionaries gained combine | and update |= operators. Is there a dunder/magic method which will enable this to be used for other classes? I've tried looking in the python source but found it a bit bewildering.

Answer: Yes, | and |= correspond to __or__ and __ior__. Don't look at the python source code, look at the documentation. In particular, the data model.  See here And note, this isn't specific to python 3.9.
Question: What is the difference between using a single cursor in psycopg2 to perform all your queries against using multiple cursors.  I.e, say I do this: import psycopg2 as pg2 con = psycopg2.connect(...) cur = con.cursor() cur.execute(...) .... .... cur.execute(...) ...  and every time I wish to execute a query thereafter, I use the same cursor cur. Alternatively I could do this every time I want to query my database: with cur as con.cursor(): cur.execute(...)  In which case, my cursor cur would be deleted after every use.  Which method is better? Does one have an advantage over another? Is one faster than the other? More generally, why are multiple cursors for one connection even needed?

Answer: The two options are comparable; you can always benchmark both to see if there's a meaningful difference, but psycopg2 cursors are pretty lightweight (they don't represent an actual server-side, DECLAREd cursor, unless you pass a name argument) and I wouldn't expect any substantial slowdown from either route. The reason psycopg2 has cursors at all is twofold. The first is to be able to represent server-side cursors for situations where the result set is larger than memory, and can't be retrieved from the DB all at once; in this case the cursor serves as the client-side interface for interacting with the server-side cursor. The second is that psycopg2 cursors are not thread-safe; a connection object can be freely used by any thread, but each cursor should be used by at most one thread. Having a cursor-per-thread allows for multithreaded applications to access the DB from any thread, while sharing the same connection. See the psycopg2 usage docs on server side cursors and thread and process safety for more details.
Question: I'm trying to delete an item from a tensor. In the example below, How can I remove the third item from the tensor ? tensor([[-5.1949, -6.2621, -6.2051, -5.8983, -6.3586, -6.2434, -5.8923, -6.1901, -6.5713, -6.2396, -6.1227, -6.4196, -3.4311, -6.8903, -6.1248, -6.3813, -6.0152, -6.7449, -6.0523, -6.4341, -6.8579, -6.1961, -6.5564, -6.6520, -5.9976, -6.3637, -5.7560, -6.7946, -5.4101, -6.1310, -3.3249, -6.4584, -6.2202, -6.3663, -6.9293, -6.9262]], grad_fn=<SqueezeBackward1>) 

Answer: You can first filter array through indices and then concat both t.shape torch.Size([1, 36]) t = torch.cat((t[:,:3], t[:,4:]), axis = 1) t.shape torch.Size([1, 35])
Question: I am trying to read a xlsx file from an Azure blob storage to a pandas dataframe without creating a temporary local file. I have seen many similar questions, e.g. Issues Reading Azure Blob CSV Into Python Pandas DF, but haven't managed to get the proposed solutions to work.  Below code snippet results in a UnicodeDecodeError: 'utf-8' codec can't decode byte 0x87 in position 14: invalid start byte exception.  from io import StringIO import pandas as pd from azure.storage.blob import BlobClient, BlobServiceClient blob_client = BlobClient.from_blob_url(blob_url = url + container + "/" + blobname, credential = token) blob = blob_client.download_blob().content_as_text() df = pd.read_excel(StringIO(blob))  Using a temporary file, I do manage to make it work with the following code snippet: blob_service_client = BlobServiceClient(account_url = url, credential = token) blob_client = blob_service_client.get_blob_client(container=container, blob=blobname) with open(tmpfile, "wb") as my_blob: download_stream = blob_client.download_blob() my_blob.write(download_stream.readall()) data = pd.read_excel(tmpfile) 

Answer: Similar to what you have already done, we could use download_blob() to get the StorageStreamDownloader object into memory, then context_as_text() to decode the contents to a string. Then we can read the the contents from the CSV StringIO buffer into a pandas Dataframe with pandas.read_csv().  from io import StringIO import pandas as pd from azure.storage.blob import BlobClient, BlobServiceClient import os connection_string = os.getenv('AZURE_STORAGE_CONNECTION_STRING') blob_service_client = BlobServiceClient.from_connection_string(connection_string) blob_client = blob_service_client.get_blob_client(container="blobs", blob="test.csv") blob = blob_client.download_blob().content_as_text() df = pd.read_csv(StringIO(blob))   Update If we are working with XLSX files, use content_as_bytes() to return bytes instead of a string, and convert to a pandas dataframe with pandas.read_excel(): from io import StringIO import pandas as pd from azure.storage.blob import BlobClient, BlobServiceClient import os connection_string = os.getenv('AZURE_STORAGE_CONNECTION_STRING') blob_service_client = BlobServiceClient.from_connection_string(connection_string) blob_client = blob_service_client.get_blob_client(container="blobs", blob="test.xlsx") blob = blob_client.download_blob().content_as_bytes() df = pd.read_excel(blob)  Since content_as_text() uses UTF-8 encoding by default, this is probably causing the UnicodeDecodeError when decoding bytes.  We could still use this with pandas.read_excel() if we set the encoding to None: blob = blob_client.download_blob().content_as_text(encoding=None) df = pd.read_excel(blob)
Question: s.index=[0.0,1.1,2.2,3.3,4.4,5.5] s.index # Float64Index([0.0, 1.1, 2.2, 3.3, 4.4, 5.5], dtype='float64') s # 0.0 141.125 # 1.1 142.250 # 2.2 143.375 # 3.3 143.375 # 4.4 144.500 # 5.5 145.125 s.index=s.index.astype('float32') # s.index # Float64Index([ 0.0, 1.100000023841858, 2.200000047683716, # 3.299999952316284, 4.400000095367432, 5.5], # dtype='float64')  What's the intuition behind floating point indices? Struggling to understand when we would use them instead of int indices (it seems like you can have three types of indices: int64, float64, or object, e.g. s.index=['a','b','c','d','e','f']). From the code above, it also looks like Pandas really wants float indices to be in 64-bit, as these 64-bit floats are getting cast to 32-bit floats and then back to 64-bit floats, with the dtype of the index remaining 'float64'. How do people use float indicies? Is the idea that you might have some statistical calculation over data and want to rank on the result of it, but those results may be floats? And we want to force float64 to avoid losing resolution?

Answer: Float indices are generally useless for label-based indexing, because of general floating point restrictions. Of course, pd.Float64Index is there in the API for completeness but that doesn't always mean you should use it. Jeff (core library contributor) has this to say on github:  [...] It is rarely necessary to actually use a float index; you are often better off served by using a column. The point of the index is to make individual elements faster, e.g. df[1.0], but this is quite tricky; this is the reason for having an issue about this.  The tricky part there being 1.0 == 1.0 isn't always true, depending on how you represent that 1.0 in bits. Floating indices are useful in a few situations (as cited in the github issue), mainly for recording temporal axis (time), or extremely minute/accurate measurements in, for example, astronomical data. For most other cases there's pd.cut or pd.qcut for binning your data because working with categorical data is usually easier than continuous data.
Question: I am trying to build a new package, however, when I try to run any of the files from inside VSCode or from terminal, I am coming across this error: ModuleNotFoundError: No module name 'x'  My current folder structure is as follows: package |---module |------__init__.py |------calculations.py |------miscfuncs.py |---tests |------__init__.py |------test_calcs.py |---setup.py |---requirements.txt  However, when I run my tests (PyTest) through VSCode and using import module.calculations as calc or from module.calculations import Class in test_calcs.py, the tests work as expected - which is confusing me. I know this is a commonly asked question, but I cannot fathom out a solution that will work here. I have tried checking the working directory is in system path using the code below. The first item on the returned list of directories is the one I am working in. import sys print(sys.path)  I have also used the following in the files to no avail: import module.calculations import .module.calculations from . import miscfuncs  When trying import .module.calculations, I get the following: ModuleNotFoundError: No module named '__main__.module'; '__main__' is not a package  When trying from . import miscfuncs in calculations.py, I get the following error: ImportError: cannot import name 'miscfuncs'  When working on a file within the module folder, I can use a relative import: import calculations and it works fine. This is fine for files within module, but not when I am working in test_calcs.py. In my setup.py, I have a line for: packages=['module']  Happy to post more info if required or a link to my repo for the full code. EDIT Following remram's solution: I have updated launch.json to include the CWD and PYTHONPATH variables. The module name is still not recognised, however, IntelliSense within VSCode is picking up the functions within the imported file just fine. "version": "0.2.0", "configurations": [ { "name": "Python: Current File", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal", "cwd": "${workspaceFolder}", "env": {"PYTHONPATH": "${cwd}" } } ] 

Answer: Make sure you are running from the package folder (not from package/module) if you want import module.calculations to work. You can also set the PYTHONPATH environment variable to the path to the package folder.
Question: I know that I can create a wheel by first writing a setup.py and then typing python setup.py bdist_wheel  If my wheels depend only on packages in pypi I know that I can install them by doing: pip install mypkg.whl  Question: if my wheels depend on other of my wheels, can I have pip automatically install them from a folder? Essentially using a folder as a poor man's private pypi To be more specific, if in pkg1 I have a setup.py: from setuptools import setup setup( ... name = "pkg1", install_requires = ["requests"], ... )  And in pkg2 I have: from setuptools import setup setup( ... name = "pkg2", install_requires = ["pkg1"], ... )  This will fail on installation because pip will try to look for pkg1 in pypi. Is it possible to tell it to just look in a folder?

Answer: pip install --find-links /path/to/wheel/dir/ pkg2  If you want to completely disable access to PyPI add --no-index: pip install --no-index --find-links /path/to/wheel/dir/ pkg2
Question: I use pyCharm software in windows 10, and when I tried to install open3d the following error appeared: ERROR: Could not find a version that satisfies the requirement open3d (from versions: none) ERROR: No matching distribution found for open3d  I tried to install it using cmd but the same error appeared, also pip version is 20.1.1. 

Answer: Solved: by installing python version 3.77 and run code using it instead of 3.8