eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I am trying to do the equivalent of: class A: def __init__(self): self.b = self.get_b() def get_b(self): return 1  using @dataclass. I want to use a @dataclass here because there are other (omitted) fields initialized from provided constructor arguments. b is one of the fields that is initialized from an instance method's result. Executing this: @dataclass class A: b = self.get_b() def get_b(self): return 1  shows that self is not defined in b's scope. Is it possible to get a reference to self here?

Answer: Use the __post_init__ method. from dataclasses import dataclass, field @dataclass class A: b: int = field(init=False) def __post_init__(self): self.b = self.get_b()  Not exactly an improvement, is it? The default value assigned to a field is equivalent to a default parameter value in __init__, e.g., def __init__(self, b=0): self.b = b  Just like you can't use self in such a parameter default, you can't use it as the field default value. The default value has to exist before the instance actually exists. We create the field explicitly so that we can pass init=False, preventing the generated __init__ method from expecting an argument to initialize self.b.  If the function get_b is really independent of an instance (that is, you don't need an instance of A in order to return the value 1), you can use an already defined function as a default factory. from dataclasses import dataclass, field @dataclass class A: def get_b(self=None): return 1 b: int = field(default_factory=get_b, init=False)  Here, get_b will be called as a function with zero arguments (hence the default value for self in the definition) rather than be invoked from a class instance. This is, though, rather unorthodox and I wouldn't recommend structuring your code like this.
Question: I try to do a bar graph using plotly.express but I find this problem  All arguments should have the same length. The length of argument y is 51, whereas the length of previously-processed arguments ['x'] is 4399 and this my code  import pandas as pd import plotly.express as px df= pd.read_csv('...../datasets-723010-1257097-fatal-police-shootings-data1.csv.xls') c = df['state'].value_counts() fig =px.bar(c , x = df['state']) fig.show()  and this sample of data enter image description here

Answer: df['state'] has all the rows from the dataframe while c only contains a row for each unique value of state. you should use c.index instead: px.bar(y=c, x=c.index)
Question: I've been working to create a chart similar to this EIA Chart (data at linked page):  I've seen a similar example using Altair in the line chart with confidence interval band gallery example but I do not see a way to explicitly set the "extent" with my own values using the mark_errorband method. The documentation provides that you can use one of 4 methods to set the extent but I can't figure out how to pass in my own values. The mark_errorband examples make me believe that this must be possible however I am at a loss as to how to accomplish it. I'd appreciate any guidance on how an min-max band may be achieved in Altair.

Answer: You can use an area mark with the y and y2 encodings. For example: import altair as alt import pandas as pd import numpy as np x = np.linspace(0, 10) y = np.sin(x) + 0.1 * np.random.randn(len(x)) df = pd.DataFrame({ 'x': x, 'y': y, 'upper': y + 0.5 * (1 + np.random.rand(len(x))), 'lower': y - 0.5 * (1 + np.random.rand(len(x))) }) line = alt.Chart(df).mark_line( color='black' ).encode( x='x', y='y' ) band = alt.Chart(df).mark_area( opacity=0.5, color='gray' ).encode( x='x', y='lower', y2='upper' ) band + line   Under the hood, mark_errorband is essentially a macro within Vega-Lite that computes lower/upper bounds and automatically populates the y and y2 encodings for you.
Question: I am trying to type the __new__ method in a metaclass in Python so that it pleases mypy. The code would be something like this (taken from pep-3115 - "Metaclasses in Python 3000" and stripped down a bit): from __future__ import annotations from typing import Type # The metaclass class MetaClass(type): # The metaclass invocation def __new__(cls: Type[type], name: str, bases: tuple, classdict: dict) -> type: result = type.__new__(cls, name, bases, classdict) print('in __new__') return result class MyClass(metaclass=MetaClass): pass  With this, mypy complains, Incompatible return type for "__new__" (returns "type", but must return a subtype of "MetaClass"), pointing at the line def __new__. I have also tried with: def __new__(cls: Type[MetaClass], name: str, bases: tuple, classdict: dict) -> MetaClass:  Then mypy complains (about the return result line): Incompatible return value type (got "type", expected "MetaClass"). I have also tried with a type var (TSubMetaclass = TypeVar('TSubMetaclass', bound='MetaClass')) and the result is the same as using MetaClass. Using super().__new__ instead of type.__new__ gave similar results. What would be the correct way to do it?

Answer: First, the return type is MetaClass, not type. Second, you need to explicitly cast the return value, since type.__new__ doesn't know it is returning an instance of MetaClass. (Its specific return type is determined by its first argument, which isn't known statically.) from __future__ import annotations from typing import Type, cast # The metaclass class MetaClass(type): # The metaclass invocation def __new__(cls: Type[type], name: str, bases: tuple, classdict: dict) -> MetaClass: result = type.__new__(cls, name, bases, classdict) print('in __new__') return cast(MetaClass, result) class MyClass(metaclass=MetaClass): pass  To use super, you need to adjust the static type of the cls parameter. class MetaClass(type): # The metaclass invocation def __new__(cls: Type[MetaClass], name: str, bases: tuple, classdict: dict) -> MetaClass: result = super().__new__(name, bases, classdict) print('in __new__') return cast(MetaClass, result)
Question: I want to populate an attribute of a dataclass using the default_factory method. However, since the factory method is only meaningful in the context of this specific class, I want to keep it inside the class (e.g. as a static or class method). For example: from dataclasses import dataclass, field from typing import List @dataclass class Deck: cards: List[str] = field(default_factory=self.create_cards) @staticmethod def create_cards(): return ['King', 'Queen']  However, I get this error (as expected) on line 6:  NameError: name 'self' is not defined  How can I overcome this issue? I don't want to move the create_cards() method out of the class.

Answer: One possible solution is to move it to __post_init__(self). For example: @dataclass class Deck: cards: List[str] = field(default_factory=list) def __post_init__(self): if not self.cards: self.cards = self.create_cards() def create_cards(self): return ['King', 'Queen']  Output: d1 = Deck() print(d1) # prints Deck(cards=['King', 'Queen']) d2 = Deck(["Captain"]) print(d2) # prints Deck(cards=['Captain'])
Question: I tried to define a simple model in Pytorch. The model computes negative log prob for a gaussian distribution: import torch import torch.nn as nn class GaussianModel(nn.Module): def __init__(self): super(GaussianModel, self).__init__() self.register_parameter('mean', nn.Parameter(torch.zeros(1), requires_grad=True)) self.pdf = torch.distributions.Normal(self.state_dict()['mean'], torch.tensor([1.0])) def forward(self, x): return -self.pdf.log_prob(x) model = GaussianModel()  Then I tried to optimize the mean parameter: optimizer = torch.optim.SGD(model.parameters(), lr=0.002) for _ in range(5): optimizer.zero_grad() nll = model(torch.tensor([3.0], requires_grad=True)) nll.backward() optimizer.step() print('mean : ', model.state_dict()['mean'], ' - Negative Loglikelihood : ', nll.item())  But it seems the gradient is zero and mean does not change: mean : tensor([0.]) - Negative Loglikelihood : 5.418938636779785 mean : tensor([0.]) - Negative Loglikelihood : 5.418938636779785 mean : tensor([0.]) - Negative Loglikelihood : 5.418938636779785 mean : tensor([0.]) - Negative Loglikelihood : 5.418938636779785 mean : tensor([0.]) - Negative Loglikelihood : 5.418938636779785  Did I register and use the mean parameter correctly? can autograd compute the gradient for torch.distributions.Normal.log_prob or I should implement the backward() for the model?

Answer: You're over complicating registering your parameter. You can just assign a new self.mean attribute to be an nn.Parameter then use it like a tensor for the most part. nn.Module overrides the __setattr__ method which is called every time you assign a new class attribute. One of the things it does is check to see if you assigned an nn.Parameter type, and if so, it adds it to the modules dictionary of registered parameters. Because of this, the easiest way to register your parameter is as follows: import torch import torch.nn as nn class GaussianModel(nn.Module): def __init__(self): super(GaussianModel, self).__init__() self.mean = nn.Parameter(torch.zeros(1)) self.pdf = torch.distributions.Normal(self.mean, torch.tensor([1.0])) def forward(self, x): return -self.pdf.log_prob(x)
Question: I just started to work with selenium web driver with chromedrivers. I am using MacOS and When I try to set the path for the chrome browser as a binary path I always face the same error saying no chrome binary at so and so path given. import os from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.keys import Keys chrome_options = Options() chrome_options.binary_location = "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome" driver = webdriver.Chrome(executable_path = os.path.abspath("drivers/chromedriver") , chrome_options = chrome_options)  The given path in chrome_options.binary_location is correct and that is where I find my chrome browser. I also have included my chromedriver inside the project folder itself  /Applications/Codes/Selenium/seleniumproject/ChromeBinary.py:11: DeprecationWarning: use options instead of chrome_options driver = webdriver.Chrome(executable_path = os.path.abspath("drivers/chromedriver") , chrome_options = chrome_options) Traceback (most recent call last): File "/Applications/Codes/Selenium/seleniumproject/ChromeBinary.py", line 11, in <module> driver = webdriver.Chrome(executable_path = os.path.abspath("drivers/chromedriver") , chrome_options = chrome_options) File "/Users/apple/opt/anaconda3/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__ desired_capabilities=desired_capabilities) File "/Users/apple/opt/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__ self.start_session(capabilities, browser_profile) File "/Users/apple/opt/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "/Users/apple/opt/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute self.error_handler.check_response(response) File "/Users/apple/opt/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: no chrome binary at /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome 

Answer: This resolved the issue chrome_options.binary_location = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
Question: I have a code for finding the possible combinations of a given string in the list. But facing an issue with leading zero getting error like SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers . How to overcome this issue as I wanted to pass values with leading zeros(Cannot edit the values manually all the time). Below is my code def permute_string(str): if len(str) == 0: return [''] prev_list = permute_string(str[1:len(str)]) next_list = [] for i in range(0,len(prev_list)): for j in range(0,len(str)): new_str = prev_list[i][0:j]+str[0]+prev_list[i][j:len(str)-1] if new_str not in next_list: next_list.append(new_str) return next_list list = [129, 831 ,014] length = len(list) i = 0 # Iterating using while loop while i < length: a = list[i] print(permute_string(str(a))) i += 1; 

Answer: Finally, I got my answer. Below is the working code. def permute_string(str): if len(str) == 0: return [''] prev_list = permute_string(str[1:len(str)]) next_list = [] for i in range(0,len(prev_list)): for j in range(0,len(str)): new_str = prev_list[i][0:j]+str[0]+prev_list[i][j:len(str)-1] if new_str not in next_list: next_list.append(new_str) return next_list #Number should not be with leading Zero actual_list = '129, 831 ,054, 845,376,970,074,345,175,965,068,287,164,230,250,983,064' list = actual_list.split(',') length = len(list) i = 0 # Iterating using while loop while i < length: a = list[i] print(permute_string(str(a))) i += 1;
Question: I am running FastApi via docker by creating a sevice called ingestion-data in docker-compose. My Dockerfile : FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7 # Environment variable for directory containing our app ENV APP /var/www/app ENV PYTHONUNBUFFERED 1 # Define working directory RUN mkdir -p $APP WORKDIR $APP COPY . $APP # Install missing dependencies RUN pip install -r requirements.txt  AND my docker-compose.yml file version: '3.8' services: ingestion-service: build: context: ./app dockerfile: Dockerfile ports: - "80:80" volumes: - .:/app restart: always  I am not sure why this is not picking up any change automatically when I make any change in any endpoint of my application. I have to rebuild my images and container every time.

Answer: Quick answer: Yes :) In the Dockerfile, you copying your app into /var/www/app. The instructions form the Dockerfile are executed when you build your image (docker build -t <imgName>:<tag>) If you change the code later on, how could the image be aware of that?  However, you can mount a volume(a directory) from your host machine, into the container when you execute the docker run / docker-compose up command, right under /var/www/app. You'll then be able to change the code in your local directory and the changes will automatically be seen in the container as well. Perhaps you want to mount the current working directory(the one containing your app) at /var/www/app? volumes: - .:/var/www/app
Question: I've been using matplotlib for a while and I don't actually understand what this line does. fig, ax = plt.subplots()  Could someone explain?

Answer: plt.subplots() is basically a (very nice) shortcut for initializing a figure and subplot axes. See the docs here. In particular, >>> fig, ax = plt.subplots(1, 1)  is essentially equivalent to >>> fig = plt.figure() >>> ax = fig.add_subplot(1, 1)  But plt.subplots() is most useful for constructing several axes at once, for example, >>> fig, axes = plt.subplots(2, 3)  makes a figure with 2 rows and 3 columns of subplots, essentially equivalent to >>> fig = plt.figure() >>> axes = np.empty((2,3)) >>> for i in range(2): ... for j in range(3): ... axes[i,j] = fig.add_subplot(2, 3, (i*j)+j+1)  I say "essentially" because plt.subplots() also has some nice features, like sharex=True forces each of the subplots to share the same x axis (i.e., same axis limits / scales, etc.). This is my favorite way to initialize a figure because it gives you the figure and all of the axes handles in one smooth line.
Question: I am trying to visualization a bar plot of many statistic data, and wanna set y-axis as a integer (there is no float type data in my dataset) This is one of the charts, which I want to change the axis. Image Link This is my python source code to visualization this chart def plot_3(data,x,y,width): selector = alt.selection_single(encodings=['x', 'color']) bars = alt.Chart(data).mark_bar(opacity=0.8).encode( alt.X('Tahun:O', title=''), alt.Y('N:Q', title=x, axis=alt.Axis(format='.0f')), # this format axis has no effect alt.Column('Keterangan:N', title=y),color=alt.condition(selector, 'Tahun:O', alt.value('lightgray')), tooltip = ['Tahun','Keterangan','N','Satuan'] ).add_selection(selector ).interactive( ).resolve_scale(x='independent') return bars.properties(width=width )  waiting for the solution, thank you :)

Answer: You could set tickMinStep=1. import altair as alt import pandas as pd source = pd.DataFrame({ 'a': ['A', 'B', 'C', 'D'], 'b': [2.0, 1.0, 1.0, 3.0] }) alt.Chart(source).mark_bar().encode( alt.X('a:N'), alt.Y('b:Q', axis=alt.Axis(tickMinStep=1)) )
Question: I want my code to create a plot with a title. With the code below the plot gets created but no title. Can someone clue me in on what I am doing wrong? import pandas as pd import networkx as nx from networkx.algorithms import community import matplotlib.pyplot as plt from datetime import datetime ... G = nx.from_pandas_edgelist((df), 'AXIS1', 'AXIS2'); nx.draw(G,with_labels=True) plt.title('TITLE') plt.axis('off') plt.savefig('test.png'); 

Answer: I can only think of some intermediate step triggering a call to plt.show before your call to plt.title (though it doesn't look like that should be the case with the shared code). Try setting the title beforehand, and setting an ax, here's an example: plt.figure(figsize=(10,5)) ax = plt.gca() ax.set_title('Random graph') G = nx.fast_gnp_random_graph(10,0.2) nx.draw(G,with_labels=True, node_color='lightgreen', ax=ax) _ = ax.axis('off')
Question: Code is below import json from decimal import Decimal from pprint import pprint import boto3 def update_movie(title, year, rating=None, plot=None, actors=None, dynamodb=None): if not dynamodb: dynamodb = boto3.resource('dynamodb') table = dynamodb.Table('Movies') response = table.update_item( Key={ 'year': year, 'title': title }, UpdateExpression="set info.rating=:r, info.plot=:p, info.actors=:a", ExpressionAttributeValues={ ':r': Decimal(rating), ':p': plot, ':a': actors }, ReturnValues="UPDATED_NEW" ) return response def lambda_handler(event, context): update_response = update_movie( "Rush", 2013, 8.3, "Car show", ["Daniel", "Chris", "Olivia"]) print("Update movie succeeded:") pprint(update_response, sort_dicts=False)  While updating a key in the dynamodb i got the error below  "errorMessage": "[<class 'decimal.Inexact'>, <class 'decimal.Rounded'>]", "errorType": "Inexact",  If i am changing 8.3 to 8 My code is working fine update_response = update_movie( "Rush", 2013, 8.3, "Car show", ["Daniel", "Chris", "Olivia"]) print("Update movie succeeded:")``` 

Answer: The problem is that DynamoDB's representation of floating-point numbers is different from Python's:  DynamoDB represents floating point numbers with a decimal representation. So "8.3" can be represented exactly - with no rounding or inexactness. Python uses, as traditional, base-2 representation, so it can't represent 8.3 exactly. 8.3 is actually represented as 8.3000000000000007105 and is known to be inexact (python doesn't know which digits you intended at the very end).  The SDK knows the floating-point 8.3 is inexact, and refuses to use it. The solution is to use the Decimal class as intended: It should be constructed with a string parameter, not a floating-point one. I.e., use Decimal("8.3") (note the quotes), not Decimal(8.3). In your code above fixing this is as trivial as changing 8.3 to "8.3", with quotes. That's the best approach. The other, not as good, approach is to do Decimal(str(8.3))), but be prepared for the potential of inexact representation of the numbers. Moreover, creating a Decimal with a string allows you to create numbers which are simply not supported in Python. For example, Decimal("3.1415926535897932384626433832795028841") will get you 38 decimal digits of precision (the maximum supported by DynamoDB) - something you cannot do in Python floating point.
Question: I'm new to Atom (and relatively new to programming) and I just installed it about a few hours ago. I was trying to set it up by installing some new packages and themes in the Install Packages section of Settings. It was working fine for a while but now I'm getting errors when I try to search. A red box appears below the search field with this error: Searching for “pre” failed.Hide output… i.filter is not a function [object Object]  I'm on Windows 10 with Atom 1.49.0 x64 installed. Python 3.8 is also installed with the path set. It seems to run code fine. I even found a theme on the Atom website and was able to install from there, I just can't search. I feel like I messed something up. I've searched Google, Stack Overflow, and the discussion section for Atom and I'm finding nothing that fixes this issue. I've restarted my computer and uninstalled/reinstalled Atom. I will say that after I reinstalled Atom, it still had all my setting changes I had made and it had all the packages that I had already installed still there. So maybe the uninstall didn't remove those with the program. But I wouldn't know where to go to clear that. Any help would be appreciated. Thanks!

Answer: Atom Server seems to have a problem today. Packages that were installed well a few days ago are not available today.
Question: I have a dataset that contains full width human images I want to remove all the backgrounds in those Images and just leave the full width person, my questions: is there any python code that does that ? and do I need to specify each time the coordinate of the person object? 

Answer: Here is one way to use Python/OpenCV.  Read the input Convert to gray Threshold and invert as a mask Optionally apply morphology to clean up any extraneous spots Anti-alias the edges Convert a copy of the input to BGRA and insert the mask as the alpha channel Save the results   Input:  import cv2 import numpy as np # load image img = cv2.imread('person.png') # convert to graky gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # threshold input image as mask mask = cv2.threshold(gray, 250, 255, cv2.THRESH_BINARY)[1] # negate mask mask = 255 - mask # apply morphology to remove isolated extraneous noise # use borderconstant of black since foreground touches the edges kernel = np.ones((3,3), np.uint8) mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel) mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) # anti-alias the mask -- blur then stretch # blur alpha channel mask = cv2.GaussianBlur(mask, (0,0), sigmaX=2, sigmaY=2, borderType = cv2.BORDER_DEFAULT) # linear stretch so that 127.5 goes to 0, but 255 stays 255 mask = (2*(mask.astype(np.float32))-255.0).clip(0,255).astype(np.uint8) # put mask into alpha channel result = img.copy() result = cv2.cvtColor(result, cv2.COLOR_BGR2BGRA) result[:, :, 3] = mask # save resulting masked image cv2.imwrite('person_transp_bckgrnd.png', result) # display result, though it won't show transparency cv2.imshow("INPUT", img) cv2.imshow("GRAY", gray) cv2.imshow("MASK", mask) cv2.imshow("RESULT", result) cv2.waitKey(0) cv2.destroyAllWindows()   Transparent result: