eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: Lets say that i have the following array (note that there is a 1 in the [2,0] position and a 2 in the [3,4] position): [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] [1, 0, 0, 0, 0] [0, 0, 0, 0, 2] [0, 0, 0, 0, 0]  and I want to flip it along the diagonal efficiently such that: [0, 0, 1, 0, 0] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0] [0, 0, 0, 2, 0]  This does not work with fliplr or rot90 or flipud. Would like efficient answer rather than just an answer since unfortunately this is not being performed on matrices this small.

Answer: Both np.rot90(np.fliplr(x)) and transposing the array solves this. a = np.random.uniform(size=(5,5)) a.T == np.rot90(np.fliplr(a))
Question: Every time I run my code, the terminal at the bottom is displaying this long name (I think the file location) as well as whatever output it's supposed to display.  Is there a way to get that to go away? This is what it looks like:     administrator@Machintosh-2 Exercise Files Python % /user/bin/python3... Hello world! administrator@Machintosh-2 Exercise Files Python %  

Answer: AFAIK, there is no way to hide the paths, because the VS Code Integrated Terminal is basically using your OS/system's underlying terminal. And running Python scripts on a terminal requires the following form: <path/to/python/interpreter> <path/to/python/file>  If you want a "cleaner" console output, you could create a debug/launch configuration for Python, then set the console configuration to internalConsole: .vscode/launch.json { "version": "0.2.0", "configurations": [ { "name": "run-my-script", "type": "python", "request": "launch", "program": "${workspaceFolder}/path/to/your_script.py", "console": "internalConsole" } ] }  That would display the output in the Debug Console tab rather than the Terminal tab. There will be no more paths, just whatever your script prints out to the console. sample output:  Unfortunately, if your Python code uses and requires manual input() from the user, using the Debug Console and the internalConsole solution would not work. It will show this error:  There is no direct solution to that, to having a "clean" output and allowing manual user input(). You might want to consider instead reading your program inputs from a file or passing them as command line arguments. VS Code supports passing in arguments ([args]) when running your script <path/to/python/interpreter> <path/to/python/file> [args]  using the "args" option in the launch.json file. Here is a simple example with sys.argv: python code import sys # This is always path to the script arg_0 = sys.argv[0] # Same sequence as "args" in launch.json arg_1 = sys.argv[1] arg_2 = sys.argv[2] print(arg_1) print(arg_2)  .vscode/launch.json { "version": "0.2.0", "configurations": [ { "name": "run-my-script", "type": "python", "request": "launch", "program": "${workspaceFolder}/path/to/your_script.py", "console": "internalConsole", "args": [ "89", "abc" ] } ] }  sample output
Question: I am trying to DM myself using a self bot. I am trying to use the get_user() function in my code. bot = commands.Bot(command_prefix='', self_bot=True) counter = 0 userID = 695724603406024726 @bot.event async def dm(userID): print('Running Function') global counter if counter <= 0: print('Finding user.') counter += 1 user = bot.get_user(userID) print('user:',user) await user.send("Hello") print('message sent') return bot.loop.create_task(dm(userID)) bot.run(token, bot=False)  Instead, I am returned with this error:  File "<ipython-input-1-90e5e962a6e9>", line 24, in dm await user.send("Hello") AttributeError: 'NoneType' object has no attribute 'send'  The bot can't find the user and returns a None value. I have tested multiple ID's and am unsure what the problem is.

Answer: You could always use the coroutine client.fetch_user(id) to get it done. get_user() takes it from cache so when fresh, doesn't work most of the times. In your case: bot = commands.Bot(command_prefix='', self_bot=True) counter = 0 userID = 695724603406024726 async def dm(userID): print('Running Function') global counter if counter <= 0: print('Finding user.') counter += 1 user = await bot.fetch_user(userID) print('user:',user) await user.send("Hello") print('message sent') return bot.loop.create_task(dm(userID)) bot.run(token, bot=False)```
Question: When i hit insomnia with this request bellow then it shows this response. How can i solve this issue? Request: query{ datewiseCoronaCasesList{ updatedAt, affected, death, recovered } }  Response: { "errors": [ { "message": "Received incompatible instance \"{'updated_at': datetime.date(2020, 4, 8), 'affected': 137, 'death': 42, 'recovered': 104}\"." } ], "data": { "datewiseCoronaCasesList": [ null ] } }  My expectation which i have already gotten in errors message but this way: { 'updated_at': datetime.date(2020, 4, 8), 'affected': 137, 'death': 42, 'recovered': 104 }  My GraphQL query: class CoronaQuery(graphene.ObjectType): datewise_corona_cases_list = graphene.Field(CoronaCaseType) def resolve_datewise_corona_cases_list(self, info, **kwargs): return CoronaCase.objects.values('updated_at').annotate( affected=Sum('affected'),death=Sum('death'), recovered=Sum('recovered'))  My model: class CoronaCase(models.Model): affected = models.IntegerField(default=0) death = models.IntegerField(default=0) recovered = models.IntegerField(default=0) district = models.CharField(max_length=265, null=False, blank=False) created_at = models.DateTimeField(default=timezone.now) updated_at = models.DateTimeField(default=timezone.now) def __str__(self): return "Affected from: {}".format(self.district) 

Answer: Under CoronaQuery class, since you are returning a list of objects (instances), graphene.Field should be changed to graphene.List. I mean: class CoronaQuery(graphene.ObjectType): datewise_corona_cases_list = graphene.List(CoronaCaseType)
Question: My function foo accepts an argument things which is turned into a list internally. def foo(things): things = list(things) # more code  The list constructor accepts any iterable. However, annotating things with typing.Iterable does not give the user a clue that the iterable must be finite, not something like itertools.count(). What's the correct type hint to use in this case?

Answer: I am not aware of any possible way to achieve this in Python as you cannot provide such constraints in type hints.  However, probably the Collection type might be useful in your context as a workaround:  class collections.abc.Collection ABC for sized iterable container classes.  This requires objects to have a __len__, which is a more strict requirement than being finite. For example, finite generators don't count as Collection.
Question: I've read a .xlsb file and parsed date columns using a code below:  dateparser = lambda x: pd.to_datetime(x) data = pd.read_excel(r"test.xlsb", engine="pyxlsb", parse_dates=["start_date","end_date"], date_parser=dateparser )  My input columns in the .xlsb file have format DD/MM/YYYY (e.g. 26/01/2008). As an output of the above-mentioned code I get, for example: 1970-01-01 00:00:00.000038840. Only the last 5 digits changes. If I read the same file without parsing dates, the same columns are of float64 type and containing only the last 5 digits of output before (e.g. 38840.0). I assume this is a problem associated with date encoding itself. Does anyone know how to fix this issue?

Answer: I am not sure if you were able to figure out the answer to this problem. But, below is how I resolved it: from pyxlsb import convert_date self.data: pd.DataFrame = pd.read_excel(self.file, sheet_name=self.sheet, engine='pyxlsb', header=0) self.data["test"] = self.data.apply(lambda x: convert_date(x.SomeStupidDate), axis=1)  More details can be found here: https://pypi.org/project/pyxlsb/ by doing ctrl+F for "convert_date".
Question: I am trying to visualize CNN filters by optimizing a random 'image' so that it produces a high mean activation on that filter which is somehow similar to the neural style transfer algorithm. For that purpose, I am using TensorFlow==2.2.0-rc. But during the optimization process, an error occurs saying 'tensorflow.python.framework.ops.EagerTensor' object has no attribute '_in_graph_mode'. I tried debugging it and it somehow works when I don't use the opt.apply_gradients() and instead, apply its gradient manually like img = img - lr * grads but I want to use the "Adam" optimizer rather than the simple SGD. Here is my code for the optimization part opt = tf.optimizers.Adam(learning_rate=lr, decay = 1e-6) for _ in range(epoch): with tf.GradientTape() as tape: tape.watch(img) y = model(img)[:, :, :, filter] loss = -tf.math.reduce_mean(y) grads = tape.gradient(loss, img) opt.apply_gradients(zip([grads], [img])) 

Answer: The reason for the bug is that the tf.keras optimizers apply gradients to variable objects (of type tf.Variable), while you are trying to apply gradients to tensors (of type tf.Tensor). Tensor objects are not mutable in TensorFlow, thus the optimizer cannot apply gradients to it. You should initialize the variable img as a tf.Variable. This is how your code should be: # NOTE: The original image is lost here. If this is not desired, then you can # rename the variable to something like img_var. img = tf.Variable(img) opt = tf.optimizers.Adam(learning_rate=lr, decay = 1e-6) for _ in range(epoch): with tf.GradientTape() as tape: tape.watch(img) y = model(img.value())[:, :, :, filter] loss = -tf.math.reduce_mean(y) grads = tape.gradient(loss, img) opt.apply_gradients(zip([grads], [img]))  Also, it is recommended to calculate the gradients outside the tape's context. This is because keeping it in will lead to the tape tracking the gradient calculation itself, leading to higher memory usage. This is only desirable if you want to calculate higher-order gradients. Since you don't need those, I have kept them outside. Note I have changed the line y = model(img)[:, :, :, filter] to y = model(img.value())[:, :, :, filter]. This is because tf.keras models need tensors as input, not variables (bug, or feature?).
Question: I am trying to setup roles for my discord bot but keep getting this error:  discord.errors.Forbidden: 403 Forbidden (error code: 50013)  My Code: @client.event async def on_member_join(member): guild = client.get_guild(688568885968109756) role = discord.utils.get(member.guild.roles, id=689916456871133311) await member.add_roles(role) 

Answer: If your bot have enough permission, Then it's coming due to the hierarchy of roles. Check-in server settings for the hierarchy. For changing the hierarchy, you can move the roles up down in the settings.
Question: Sorry beforehand for the long post. I'm new to python and to plotly, so please bear with me. I'm trying to make a scatterplot with a trendline to show me the legend of the plot including the regression parameters but for some reason I can't understand why px.scatter doesn't show me the legend of my trace. Here is my code fig1 = px.scatter(data_frame = dataframe, x="xdata", y="ydata", trendline = 'ols') fig1.layout.showlegend = True fig1.show()  This displays the scatterplot and the trendline, but no legend even when I tried to override it. I used pio.write_json(fig1, "fig1.plotly") to export it to jupyterlab plotly chart studio and add manually the legend, but even though I enabled it, it won't show either in the chart studio. I printed the variable with print(fig1) to see what's happening, this is (part of) the result (Scatter({ 'hovertemplate': '%co=%{x}<br>RPM=%{y}<extra></extra>', 'legendgroup': '', 'marker': {'color': '#636efa', 'symbol': 'circle'}, 'mode': 'markers', 'name': '', 'showlegend': False, 'x': array([*** some x data ***]), 'xaxis': 'x', 'y': array([*** some y data ***]), 'yaxis': 'y' }), Scatter({ 'hovertemplate': ('<b>OLS trendline</b><br>RPM = ' ... ' <b>(trend)</b><extra></extra>'), 'legendgroup': '', 'marker': {'color': '#636efa', 'symbol': 'circle'}, 'mode': 'lines', 'name': '', 'showlegend': False, 'x': array([*** some x data ***]), 'xaxis': 'x', 'y': array([ *** some y data ***]), 'yaxis': 'y' }))  As we can see, creating a figure with px.scatter by default hides the legend when there's a single trace (I experimented adding a color property to px.scatter and it showed the legend), and searching the px.scatter documentation I can't find something related to override the legend setting. I went back to the exported file (fig1.plotly.json) and manually changed the showlegend entries to True and then I could see the legend in the chart studio, but there has to be some way to do it directly from the command. Here's the question: Does anyone know a way to customize px.express graphic objects? Another workaround I see is to use low level plotly graph object creation, but then I don't know how to add a trendline. Thank you again for reading through all of this.

Answer: You must specify that you'd like to display a legend and provide a legend name like this: fig['data'][0]['showlegend']=True fig['data'][0]['name']='Sepal length'  Plot:  Complete code: import plotly.express as px df = px.data.iris() # iris is a pandas DataFrame fig = px.scatter(df, x="sepal_width", y="sepal_length", trendline='ols', trendline_color_override='red') fig['data'][0]['showlegend']=True fig['data'][0]['name']='Sepal length' fig.show()  Complete code:
Question: For all my projects, I load all env variables at the start and check that all the expected keys exist as described by an .env.example file following the dotenv-safe approach. However, the env variables are strings, which have to be manually cast whenever they're used inside the Python code. This is annoying and error-prone. I'd like to use the information from the .env.example file to cast the env variables and get Python typing support in my IDE (VS Code). How do I do that? env.example PORT: int SSL: boolean  Python Ideal Behavior # Set the env in some way (doesn't matter) import os os.environment["SSL"] = "0" os.environment["PORT"] = "99999" env = type_env() if not env["SSL"]: # <-- I'd like this to be cast to boolean and typed as a boolean print("Connecting w/o SSL!") if 65535 < env["PORT"]: # <-- I'd like this to be cast to int and typed as an int print("Invalid port!")  In this code example, what would the type_env() function look like assuming it only supported boolean, int, float, and str? It's not too hard to do the casting as shown in e.g. https://stackoverflow.com/a/11781375/1452257, but it's unclear to me how to get it working with typing support.

Answer: I will suggest using pydantic. From StackOverflow pydantic tag info  Pydantic is a library for data validation and settings management based on Python type hinting (PEP484) and variable annotations (PEP526). It allows for defining schemas in Python for complex structures.  let's assume that you have a file with your SSL and PORT envs: with open('.env', 'w') as fp: fp.write('PORT=5000\nSSL=0')  then you can use: from pydantic import BaseSettings class Settings(BaseSettings): PORT : int SSL : bool class Config: env_file = '.env' config = Settings() print(type(config.SSL), config.SSL) print(type(config.PORT), config.PORT) # <class 'bool'> False # <class 'int'> 5000  with your code: env = Settings() if not env.SSL: print("Connecting w/o SSL!") if 65535 < env.PORT: print("Invalid port!")  output: Connecting w/o SSL!
Question: I have an ETL job written in python, which consist of multiple scripts with following directory structure; my_etl_job | |--services | | | |-- __init__.py | |-- dynamoDB_service.py | |-- __init__.py |-- main.py |-- logger.py  main.py is the entrypoint script that imports other scripts from above directories. The above code runs perfectly fine on dev-endpoint, after uploading on the ETL cluster created by dev endpoint. Since now I want to run it in production, I want to create a proper glue job for it. But when I compress the whole directory my_etl_job in .zip format, upload it in artifacts s3 bucket, and specify the .zip file location into script location as follows s3://<bucket_name>/etl_jobs/my_etl_job.zip  This is the code I see on glue job UI dashboard; PK ���P__init__.pyUX�'�^"�^A��)PK#7�P logger.pyUX��^1��^A��)]�Mk�0����a�&v+���A�B���`x����q��} ...AND ALLOT MORE...  Seems like the glue job doesn't accepts .zip format ? if yes, then what compression format shall I use ?  UPDATE: I checked out that glue job has option of taking in extra files Referenced files path where I provided a comma separated list of all paths of the above files, and changed the script_location to refer to only main.py file path. But that also didn't worked. Glue job throws error no module found logger (and I defined this module inside logger.py file) 

Answer: You'll have to pass the zip file as extra python lib , or build a wheel package for the code package and upload the zip or wheel to s3, provide the same path as extra python lib option Note: Have your main function written in the glue console it self , referencing the required function from the zipped/wheel dependency, you script location should never be a zip file  https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-python-libraries.html
Question: I am trying to convert my Jupyter Notebook file (.ipynb) into an HTML file for easier reading. Every time I try to save the notebook I get a "Notebook validation failed" error: Notebook validation failed: ['outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend'] has non-unique elements: [ "outputPrepend", "outputPrepend", "outputPrepend", "outputPrepend", "outputPrepend", "outputPrepend", "outputPrepend", "outputPrepend", "outputPrepend", "outputPrepend", "outputPrepend", "outputPrepend" ]  message when file is saved When I try to download as .html using File > Download as, I get a further more detailed error but I still cannot make out what it means or what needs to be done to solve the problem and finally download as HTML: nbconvert failed: ['outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend'] has non-unique elements Failed validating 'uniqueItems' in code_cell['properties']['metadata']['properties']['tags']: On instance['cells'][70]['metadata']['tags']: ['outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend', 'outputPrepend']  message when trying to download as HTML This is only happening on the specific notebook I am working on. I have tried to save and export other notebooks as HTML successfully. Any help is appreciated. Thanks 

Answer: I recently experienced this from using the VSCode Notebook editor. I solved it by opening the notebook in a regular text editor and deleting all the extra outputPrepend-items, leaving only a single one in each array.
Question: I was creating a flask application and tried to call a python function in which I wanted to invoke some javascript function/code regarding the HTML template that I returned on the initial app.route('/') If the user did something, then I called another function that should invoke or call a js function I have tried looking everywhere but I cannot make any sense of the solutions. Here is the structure of my code: @app.route('/', methods=['GET', 'POST']) def upload_file(): if request.method == 'POST': #verify if the file is valid #here invoke js to do something (for example flash("test")) return ''' <!doctype html> <title>Upload new File</title> <h1>Upload new File</h1> <form method=post enctype=multipart/form-data> <input type=file name=file> <input type=submit value=Upload> </form> ''' 

Answer: You could execute a JavaScript function on load and have the function check for the condition. You can influence the outcome of this check by changing the condition with Python. If you use the render_template function of Flask, you do not have to write your HTML code within your Python file. For better readability I am using this functionality, but you can always put the HTML code into your Python code, as you have done before. Your HTML template, e.g. named upload.html:  <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Upload new File</title> </head> <body onload="flashMessage()"> <script> function flashMessage() { if ("{{ flash_message }}" == "True") { alert("[YOUR_MESSAGE_HERE]"); } } </script> <h1>Upload new File</h1> <form method=post enctype=multipart/form-data> <input type=file name=file> <input type=submit value=Upload> </form> </body> </html>  Your Python code: from flask import Flask, render_template app = Flask(__name__) @app.route('/', methods=['GET', 'POST']) def upload_file(): if request.method == 'POST': #verify if the file is valid #here invoke js to do something (for example flash("test")) return render_template('upload.html', flash_message="True") return render_template('upload.html', flash_message="False")  So, the condition line in your HTML file will render to if ("True" == "True")  or if ("False" == "True")  depending on if you want the flash message to show or not.
Question: I want to parse 2 generators of (potentially) different length with zip: for el1, el2 in zip(gen1, gen2): print(el1, el2)  However, if gen2 has less elements, one extra element of gen1 is "consumed". For example, def my_gen(n:int): for i in range(n): yield i gen1 = my_gen(10) gen2 = my_gen(8) list(zip(gen1, gen2)) # Last tuple is (7, 7) print(next(gen1)) # printed value is "9" => 8 is missing gen1 = my_gen(8) gen2 = my_gen(10) list(zip(gen1, gen2)) # Last tuple is (7, 7) print(next(gen2)) # printed value is "8" => OK  Apparently, a value is missing (8 in my previous example) because gen1 is read (thus generating the value 8) before it realizes gen2 has no more elements. But this value disappears in the universe. When gen2 is "longer", there is no such "problem". QUESTION: Is there a way to retrieve this missing value (i.e. 8 in my previous example)? ... ideally with a variable number of arguments (like zip does). NOTE: I have currently implemented in another way by using itertools.zip_longest but I really wonder how to get this missing value using zip or equivalent. NOTE 2: I have created some tests of the different implementations in this REPL in case you want to submit and try a new implementation :) https://repl.it/@jfthuong/MadPhysicistChester

Answer: If you want to reuse code, the easiest solution is: from more_itertools import peekable a = peekable(a) b = peekable(b) while True: try: a.peek() b.peek() except StopIteration: break x = next(a) y = next(b) print(x, y) print(list(a), list(b)) # Misses nothing.  You can test this code out using your setup: def my_gen(n: int): yield from range(n) a = my_gen(10) b = my_gen(8)  It will print: 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 [8, 9] []