eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: Right now I do this in a few tests to mock a 3rd party API: @patch("some.api.execute") def sometest( mock_the_api, blah, otherstuff ): mock_the_api.return_value = "mocked response" my_func_that_uses_api(blah, otherstuff)  This works and prevents my_func_that_uses_api() (which calls the API) from making actual outbound calls. But I do this 4-5 times and will probably add more in the future. I'd like to mock this globally for all my tests. I see in the docs this example: @pytest.fixture(autouse=True) def no_requests(monkeypatch): """Remove requests.sessions.Session.request for all tests.""" monkeypatch.delattr("requests.sessions.Session.request")  How do I do that but with patching the API response? I tried monkeypatch.patch("some.api.execute") but get error AttributeError: 'MonkeyPatch' object has no attribute 'patch' Also, to add, I'm not using any classes in my pytest tests (like test cases) - I'd like to avoid uses classes in my pytests tests for now.

Answer: You can just use: from unittest import mock import pytest @pytest.fixture(autouse=True) def my_api_mock(): with mock.patch("some.api.execute") as api_mock: api_mock.return_value = "mocked response" yield api_mock def test_something(blah, otherstuff): my_func_that_uses_api(blah, otherstuff)  The fixture lives as long as each function, so the patching is reverted at the end of each function. Note that yielding the mock is not needed in this case, but if we want to change the mock in some test case, this gives you the possibility to access it: def test_something(blah, otherstuff): my_func_that_uses_api(blah, otherstuff) def test_something_else(my_api_mock, blah, otherstuff): my_api_mock.return_value = "other response for this test" my_func_that_uses_api(blah, otherstuff)  For completeness, without auto-use it would be: @pytest.fixture def my_api_mock(): with mock.patch("some.api.execute") as api_mock: api_mock.return_value = "mocked response" yield api_mock def test_something(my_api_mock, blah, otherstuff): my_func_that_uses_api(blah, otherstuff)
Question: I am aware that in discord.py, you can make the set_image of an embed a url of an image. But, I want to use a local file on my computer for the set_image instead of a url of an image.  embed = discord.Embed(title="Title", description="Desc", color=0x00ff00) embed.set_image(url = "https://example.com/image.png") #this is for set_image using url  How can I achieve this? Is there another function or something?

Answer: Ok I just got it. The code for it is the following: embed = discord.Embed(title="Title", description="Desc", color=0x00ff00) #creates embed file = discord.File("path/to/image/file.png", filename="image.png") embed.set_image(url="attachment://image.png") await ctx.send(file=file, embed=embed)  The only thing you should be changing is line 2 where it says "path/to/image/file.png"  Note: on lines 2 and 3, there is an image.png. Fret not about it since thats what Discord is calling the uploaded file (Example: I have a file called duck.png, Discord uploads it to their servers as image.png). So you don't need to change the image.png part. However, if you are using a file that the specific extension matters, remember to change the image.png to the desired extension. An example of a file that requires a specific extension is a GIF so remember to change image.png to for example, an image.gifif you are using a GIF. You can read more here at discord.py's official documentation: https://discordpy.readthedocs.io/en/latest/faq.html#how-do-i-use-a-local-image-file-for-an-embed-image
Question: In order to be able to sign a PDF document using a token based DSC, I need a so-called signature field in my PDF. This is a rectangular field you can fill with a digital signature using e.g. Adobe Reader or Adobe Acrobat.  I want to create this signable PDF in Python. I'm starting from plain text, or a rich-text document (Image & Text) in .docx format.  How do I generate a PDF file with this field, in Python? 

Answer: Unfortunately, I couldn't find any (free) solutions. Just Python programs that sign PDF documents. But there is a Python PDF SDK called PDFTron that has a free trial. Here's a link to a specific article showing how to "add a certification signature field to a PDF document and sign it". # Open an existing PDF doc = PDFDoc(docpath) page1 = doc.GetPage(1) # Create a text field that we can lock using the field permissions feature. annot1 = TextWidget.Create(doc.GetSDFDoc(), Rect(50, 550, 350, 600), "asdf_test_field") page1.AnnotPushBack(annot1) # Create a new signature form field in the PDFDoc. The name argument is optional; # leaving it empty causes it to be auto-generated. However, you may need the name for later. # Acrobat doesn't show digsigfield in side panel if it's without a widget. Using a # Rect with 0 width and 0 height, or setting the NoPrint/Invisible flags makes it invisible. certification_sig_field = doc.CreateDigitalSignatureField(cert_field_name) widgetAnnot = SignatureWidget.Create(doc, Rect(0, 100, 200, 150), certification_sig_field) page1.AnnotPushBack(widgetAnnot) ... # Save the PDFDoc. Once the method below is called, PDFNet will also sign the document using the information provided. doc.Save(outpath, 0)
Question: I can convert a mp4 to wav, using ffmpeg, by doing this: ffmpeg -vn test.wav -i test.mp4  I can also use subprocess to do the same, as long as my input and output are filepaths.  But what if I wanted to use ffmpeg directly on bytes or a "file-like" object like io.BytesIO()? Here's an attempt at it: import subprocess from io import BytesIO b = BytesIO() with open('test.mp4', 'rb') as stream: command = ['ffmpeg', '-i'] proc = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=b) proc.communicate(input=stream.read()) proc.wait() proc.stdin.close() proc.stdout.close()  Gives me: --------------------------------------------------------------------------- UnsupportedOperation Traceback (most recent call last) <ipython-input-84-0ddce839ebc9> in <module> 5 with open('test.mp4', 'rb') as stream: 6 command = ['ffmpeg', '-i'] ----> 7 proc = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=b) ... 1486 # Assuming file-like object -> 1487 c2pwrite = stdout.fileno() 1488 1489 if stderr is None: UnsupportedOperation: fileno  Of course, I could use temp files to funnel my bytes, but I'd like to be able to avoid writing to the disk (because this step is just one link in a pipeline of transformations). 

Answer: Base on @thorwhalen's answer, here's how it would work from bytes to bytes. What you were probably missing @thorwhalen, is the actual pipe-to-pipe way to send and get data when interacting with a process. When sending bytes, the stdin should be closed before the process can read from it.  def from_bytes_to_bytes( input_bytes: bytes, action: str = "-f wav -acodec pcm_s16le -ac 1 -ar 44100")-> bytes or None: command = f"ffmpeg -y -i /dev/stdin -f nut {action} -" ffmpeg_cmd = subprocess.Popen( shlex.split(command), stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=False ) b = b'' # write bytes to processe's stdin and close the pipe to pass # data to piped process ffmpeg_cmd.stdin.write(input_bytes) ffmpeg_cmd.stdin.close() while True: output = ffmpeg_cmd.stdout.read() if len(output) > 0: b += output else: error_msg = ffmpeg_cmd.poll() if error_msg is not None: break return b
Question: How do I execute a shell command from Ipython/Jupyter notebook passing the value of a python string variable as a string in the bash argument like in this example: sp_name = 'littleGuy' #the variable sp_details = !az ad app list --filter "DisplayName eq '$sp_name'" #the shell command  I've tried using $sp_name alone, ${sp_name}, {sp_name} etc as outlined in this related question, but none have worked.  The kicker here is the variable name needs to be quoted as a string in the shell command. EDIT1:  @manu190466. I was judging from the string output that your solution worked. It appears for some reason it does not in practice. I wonder if az ad app list URL encodes the query or something...?  Thoughts? 

Answer: The main problem you encounters seems to come from the quotes needed in your string. You can keep the quotes in your string by using a format instruction and a raw string. Use a 'r' before the whole string to indicate it is to be read as raw string, ie: special caracters have to not be interpreted. A raw string is not strictly required in your case because the string constructor of python is able to keep single quotes in a double quotes declaration but I think it's a good habit to use raw string declarators when there are non alphanumerics in it.  There are at least two way to format strings :  Older method herited from ancient langages with % symbols: sp_name = 'littleGuy' #the variable sp_query = r"DisplayName eq '%s'"%(sp_name) sp_details = !az ad app list --filter {sp_query}  Newer method with {} symbols and the format() method : sp_name = 'littleGuy' #the variable sp_query = r"DisplayName eq '{}'".format(sp_name) sp_details = !az ad app list --filter {sp_query}
Question: I'm trying to make an executable file (.exe file for windows) for the code here. The main file to run is src/GUI.py. I found that pyinstaller is a better option to create the exe file. I tried both one folder and single executable file options. I tried creating the exe from root directory and as well as in src directory. pyinstaller src/GUI.py pyinstaller src/GUI.py -F cd src pyinstaller GUI.py pyinstaller GUI.py -F  GUI.exe gets created with all the above methods. But whenever I tried to run the GUI.exe file, I get the error no module named pkg_resources.py2_warn pyinstaller. I tried running GUI.exe in the dist directory where it is created, in the root directory and in the src directory as well. Everywhere, I get the same error. How can I fix this? PS: Ideally I would like to have a single .exe file which I can distribute and they can run it standalone, without any need to install dependencies or recreating the folder structure. But I got to know that pyinstaller only packages the code files and I've to share the images separately and when running the exe file, the same structure has to be recreated. I'm okay with this as well. I'm even okay to share the one folder exe as well. I just want to share a file or a folder, which users can run without installing any dependencies. Is it possible at all? PPS: I'm open to using tools other than pyinstaller as well.

Answer: This is an issue with setuptools as explained in this github ticket. Consider downgrading your setuptools to 44.0 or below with the command pip install --upgrade 'setuptools<45.0.0'
Question: With kubectl I can execute the following command:  kubectl get serviceentries  Then I receive some information back. But serviceentries is a custom resource. So how do I go about getting the same information back but then with the kubernetes client?  Yaml looks like this for example:  apiVersion: networking.istio.io/v1alpha3 kind: ServiceEntry metadata: name: external-svc-https spec: hosts: - api.dropboxapi.com - www.googleapis.com - api.facebook.com location: MESH_EXTERNAL ports: - number: 443 name: https protocol: TLS resolution: DNS  Anyone know the right method to use? 

Answer: you should be able to pull it using the python client like this: kubernetes.client.CustomObjectsApi().list_cluster_custom_object(group="networking.istio.io", version="v1alpha3", plural="serviceentries")  That method applies to every custom resource within kubernetes and doesn't require any further definition to the python client.
Question: I am looking to pass a user entered arguments from the command line to an entry point for a python script. Thus far I have tried to used argparse to pass the arguments from the command line to the test.py script. When I try to pass the arguments they are not recognised and I recieve the following error.   load_entry_point('thesaurus==0.1', 'console_scripts', 'thesaurus')() TypeError: find_synonym() missing 1 required positional argument: 'argv'  I have looked at other examples on here but have not been able to get any of the solutions to work. def main(argv): if argv is None: argv = sys.argv parser = argparse.ArgumentParser(description='Enter string') parser.add_argument('string', type=str, help='Enter word or words', nargs='*') args = parser.parse_args(argv[1:]) print(args) if __name__ == "__main__": sys.exit(main(sys.argv))  My setup.py script entry point looks like the following setup(entry_points={'console_scripts': ['test=test_folder.main:main']})  What I expect to happen is similar to when I run python main.py main foo. Which will successfully print out hello when it is passes to the function.

Answer: So it works now when I remove the the arguments from the function.  def main(): parser = argparse.ArgumentParser(description='Enter string') parser.add_argument('string', type=str, help='Enter word or words', nargs='*') args = parser.parse_args() print(args.string) if __name__ == "__main__": main()  I believe that it might work with the sys.arv used in the first example, as my actual problem was that when I re-downloaded this script from GitHub using.  pip install git+ URL  The script was not updating so the error persisted, This required deleting all the files related to the GitHub repository and re-installing it using the same pip command.
Question: I'm looking for a possibility to use uvicorn.run() with a FastAPI app but without uvicorn.run() is blocking the thread. I already tried to use processes, subprocessesand threads but nothing worked. My problem is that I want to start the Server from another process that should go on with other tasks after starting the server. Additinally I have problems closing the server like this from another process. Has anyone an idea how to use uvicorn.run() non blocking and how to stop it from another process?

Answer: According to Uvicorn documentation there is no programmatically way to stop the server. instead, you can stop the server only by pressing ctrl + c (officially). But I have a trick to solve this problem programmatically using multiprocessing standard lib with these three simple functions :   A run function to run the server.  A start function to start a new process (start the server). A stop function to join the process (stop the server).  from multiprocessing import Process import uvicorn # global process variable proc = None def run(): """ This function to run configured uvicorn server. """ uvicorn.run(app=app, host=host, port=port) def start(): """ This function to start a new process (start the server). """ global proc # create process instance and set the target to run function. # use daemon mode to stop the process whenever the program stopped. proc = Process(target=run, args=(), daemon=True) proc.start() def stop(): """ This function to join (stop) the process (stop the server). """ global proc # check if the process is not None if proc: # join (stop) the process with a timeout setten to 0.25 seconds. # using timeout (the optional arg) is too important in order to # enforce the server to stop. proc.join(0.25)   With the same idea you can :  use threading standard lib instead of using multiprocessing standard lib. refactor these functions into a class.   Example of usage : from time import sleep if __name__ == "__main__": # to start the server call start function. start() # run some codes .... # to stop the server call stop function. stop()    You can read more about :  Uvicorn server. multiprocessing standard lib. threading standard lib. Concurrency to know more about multi processing and threading in python.
Question: I'm looking for a pythonic way to define multiple related constants in a single file to be used in multiple modules. I came up with multiple options, but all of them have downsides. Approach 1 - simple global constants # file resources/resource_ids.py FOO_RESOURCE = 'foo' BAR_RESOURCE = 'bar' BAZ_RESOURCE = 'baz' QUX_RESOURCE = 'qux'  # file runtime/bar_handler.py from resources.resource_ids import BAR_RESOURCE # ... def my_code(): value = get_resource(BAR_RESOURCE)  This is simple and universal, but has a few downsides:  _RESOURCE has to be appended to all constant names to provide context Inspecting the constant name in IDE will not display other constant values  Approach 2 - enum # file resources/resource_ids.py from enum import Enum, unique @unique class ResourceIds(Enum): foo = 'foo' bar = 'bar' baz = 'baz' qux = 'qux'  # file runtime/bar_handler.py from resources.resource_ids import ResourceIds # ... def my_code(): value = get_resource(ResourceIds.bar.value)  This solves the problems of the first approach, but the downside of this solution is the need of using .value in order to get the string representation (assuming we need the string value and not just a consistent enum value). Failure to append .value can result in hard to debug issues in runtime. Approach 3 - class variables # file resources/resource_ids.py class ResourceIds: foo = 'foo' bar = 'bar' baz = 'baz' qux = 'qux'  # file runtime/bar_handler.py from resources.resource_ids import ResourceIds # ... def my_code(): value = get_resource(ResourceIds.bar)  This approach is my favorite, but it may be misinterpreted - classes are made to be instantiated. And while code correctness wouldn't suffer from using an instance of the class instead of the class itself, I would like to avoid this waste.  Another disadvantage of this approach that the values are not actually constant. Any code client can potentially change them. Is it possible to prevent a class from being instantiated? Am I missing some idiomatic way of grouping closely related constants?

Answer: Use Enum and mix in str: @unique class ResourceIds(str, Enum): foo = 'foo' bar = 'bar' baz = 'baz' qux = 'qux'  Then you won't need to compare against .value: >>> ResourceIds.foo == 'foo' True  And you still get good debugging info: >>> ResourceIds.foo <ResourceIds.foo: 'foo'> >>> list(ResourceIds.foo.__class__) [ <ResourceIds.foo: 'foo'>, <ResourceIds.bar: 'bar'>, <ResourceIds.baz: 'baz'>, <ResourceIds.qux: 'qux'>, ]
Question: As per the question title: Is it possible to have a red squiggly line appear under words in a Tkinter text widget without using a canvas widget? (The same squiggle as when you misspell a word) I'm going for something like this:  If so where would I start?

Answer: This is just an example of using user-defined XBM as the bgstipple of part of the text inside a Text widget to simulate the squiggly line effect:  create a XBM image, for example squiggly.xbm, like below:   A XBM with 10x20 pixels  then you can config a tag in Text widget using the above XBM image file as bgstipple in red color:  # config a tag with squiggly.xbm as bgstipple in red color textbox.tag_config("squiggly", bgstipple="@squiggly.xbm", background='red')   and apply the tag to the portion of text inside Text widget:  textbox.insert("end", "hello", "squiggly") # add squiggly line  Below is a sample code: import tkinter as tk root = tk.Tk() textbox = tk.Text(root, width=30, height=10, font=('Courier New',12), spacing1=1) textbox.pack() # config a tag with squiggly.xbm as bgstipple in red color textbox.tag_config("squiggly", bgstipple="@squiggly.xbm", background='red') textbox.insert("end", "hello", "squiggly") # add squiggly line textbox.insert("end", " world! ") textbox.insert("end", "Python", "squiggly") # add squiggly line textbox.insert("end", "\nthis is second line") root.mainloop()  And the output:  Note that the height of the XBM image need to match the font size and spacing between lines.
Question: I have a DataFrame that I want to change using  df.loc[rowId,colId] = myDict  to assign a dict to the entry [rowId,colId]. As a result I get following error: ValueError: Must have equal len keys and value when setting with an iterable  Setting df.loc[rowId,colId] = 0  works! In my pinion the style to assign the value in the first approach is the right one, so what is wrong?

Answer: It is possible by adding list: df.loc[rowId,colId] = [myDict]  But for better performance in pandas is best working in pandas only with scalars.
Question: I'm trying to transform a Python string from its original form to its vowel/consonant combinations. Eg - 'Dog' becomes 'cvc' and 'Bike' becomes 'cvcv' In R I was able to employ the following method:  con_vowel <- gsub("[aeiouAEIOU]","V",df$col_name) con_vowel <- gsub("[^V]","C",con_vowel) df[["composition"]] <- con_vowel  This would assess whether the character is vowel and if true assign the character 'V', then assess that string and replace anything that wasn't 'V' with 'C', then place the results into a new column called 'composition' within the dataframe. In Python I have written some code in an attepmpt to replicate the functionality but it does not return the desired result. Please see below. word = 'yoyo' for i in word.lower(): if i in "aeiou": word = i.replace(i ,'v') else: word = i.replace(i ,'c') print(word)  The theory here is that each character would be evaluated and, if it isn't a vowel, then by deduction it must be a consonant. However the result I get is: v  I underastand why this is happening, but I am no clearer as to how to achieve my desired result. Please note that I also need the resultant code to be applied to a dataframe column and create a new column from these results. If you could explain the workings of your answer it would help me greatly. Thanks in advance.

Answer: use string.replace with some regex to avoid the loop df = pd.DataFrame(['Cat', 'DOG', 'bike'], columns=['words']) # use string.replace df['new_word'] = df['words'].str.lower().str.replace(r"[^aeiuo]", 'c').str.replace(r"[aeiou]", 'v') print(df) words new_word 0 Cat cvc 1 DOG cvc 2 bike cvcv
Question: I've just noticed something strange and was wondering if someone could explain what's going on? I have a dictionary i'm plotting a horizontal and vertical bar charts from. plt.bar(food.keys(), food.values()) #works fine, but: plt.barh(food.keys(), food.values() #gives "unhashable type: 'dict_keys'" error.  if dictionary is unhashable, why does it let me plot a normal bar graph? Is this just a quirk of the barh function or am I doing something wrong? Here's my test dataset:  food = {'blueberries':2, 'pizza':3.50, 'apples':0.50}  Thanks. 

Answer: The other answer is of course correct in how to solve the problem - simply convert to a list. The reason why it doesn't work in the first place involves digging into the matplotlib source code. bar and barh do both call matplotlib.axes.Axes.bar under the hood. When you plot a bar, x is the x position of the bars and height is the y value.  However, calling a barh, the y values (i.e. ['blueberries', 'pizza', 'apples']) is actually set to the argument bottom and the length of the bars is given by width. The following line is called within the source code func(ax, *map(sanitize_sequence, args), **kwargs)  Where sanitize_sequence is: def sanitize_sequence(data): """ Convert dictview objects to list. Other inputs are returned unchanged. """ return (list(data) if isinstance(data, collections.abc.MappingView) else data)  The problem is that the argument bottom is a kwarg, and is therefore not passed to sanitize_sequence and is never converted to a list. Whereas in a normal bar, x is converted to a list by sanitize_sequence So the issue is with how barh is implemented under the hood.
Question: This works but is unwieldy and not very 'Pythonic'. I'd also like to be able to run through different values for 'numValues', say 4 to 40... innerList = [] outerList = [] numValues = 12 loopIter = 0 for i in range(numValues): innerList.append(0) for i in range(numValues): copyInnerList = innerList.copy() outerList.append(copyInnerList) for i in range(len(innerList)): for j in range(loopIter + 1): outerList[i][j] = 1 loopIter += 1 print(outerList) 

Answer: numValues = 12 result = [ [1] * i + [0] * (numValues - i) for i in range(1, numValues+1) ]