eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: When i executing this function i get this error: Fail to create pixmap with Tk_GetPixmap in TkImgPhotoInstanceSetSize. This error occur in this line: fig, ax = plt.subplots()(line 12). The strange thing is, that this error only occur sometimes (Maybe only 10% of their executes). Here is my whole function: from bs4 import BeautifulSoup from PIL import ImageDraw, Image, ImageFont from colour import Color import base64 from io import BytesIO import matplotlib.pyplot as plt def getCurrentBatterySOCImage(batterySOC): red = Color("red") colors = list(red.range_to(Color("#23b818"), 101)) color = colors[round(batterySOC)].hex fig, ax = plt.subplots(facecolor="black") ax.set_facecolor("black") fig.tight_layout() ax.axis("off") ax.bar([1], batterySOC, color=color) ax.axis([0.6,1.4,0,100]) ax.set_aspect(.02) tmpfile = BytesIO() fig.savefig(tmpfile, format="png", pad_inches=0, bbox_inches="tight") plt.close(fig) bar = Image.open(tmpfile) bar = bar.resize((107, 208)) img = Image.new("RGBA", (136, 248), (255, 0, 0, 0)) img.paste(bar, (14, 26), bar) battery = Image.open("img/battery.png") img.paste(battery, (0, 0), battery) font = ImageFont.truetype('font/RobotoMono.ttf', 40) message = f"{batterySOC}%" draw = ImageDraw.Draw(img) w, h = draw.textsize(message, font=font) draw.text(((136-w)/2, (248-h)/2), f"{batterySOC}%", fill=(255, 255, 255), font=font, stroke_width=2, stroke_fill="black") tmpfile1 = BytesIO() img.save(tmpfile1, format="png") encoded = base64.b64encode(tmpfile1.getvalue()).decode('utf-8') with open("index.html") as fp: soup = BeautifulSoup(fp, "html.parser") soup.find("img", attrs={'id': 'battery'})['src'] = f"data:image/png;base64,{encoded}" with open("index.html", "w") as fp: fp.write(soup.decode()) return  Can someone help me to fix this?

Answer: This seems to be a memory issue from this issue, new in Matplotlib 3.5 (vs 3.3) The recommended way is to switch to 'Agg' backend if possible [EDIT] Issue is now resolved with default backend in newer versions of Matplotlib: 3.6 (sept 2022), 3.7... If possible, as per wisbucky answer you may upgrade lib: pip install --upgrade matplotlib
Question: I am trying IB very the first time. I am trying to fetch historical data of $EUR but I am getting an error:  Error 162, reqId 3: Historical Market Data Service error message:No historical market data for EUR/CASH@FXSUBPIP Last 1800, contract: Contract(secType='CASH', symbol='EUR', exchange='IDEALPRO', currency='USD')  Below is my code: import datetime from ib_insync import * if __name__ == '__main__': ib = IB() r = ib.connect('127.0.0.1', port=7497, clientId=1) contract = Contract() contract.symbol = "EUR" contract.secType = "CASH" contract.currency = "USD" contract.exchange = "IDEALPRO" data = ib.reqHistoricalData( contract=contract, endDateTime='', durationStr='100 D', barSizeSetting='30 mins', useRTH=True, whatToShow='ADJUSTED_LAST' ) 

Answer: The issue here is not on the definition of the FOREX contract itself, but the bar settings requested.  durationStr='100 D',  barSizeSetting='30 mins',  The barSizeSetting is not available for the durationStr selected. If you check the TWS GUI and pull the historical data chart for the FX contract you'll see the lowest bar size available for 100 Day duration is 2 hours. You should always check if the functionality you are demanding from the API is available through the TWS GUI. IBKR's API talks to either TWS or IBGateway which in turn send the request to IBKR. If the functionality is not available through the GUI it most likely is not available through the API.
Question: I would like to open csv data but keep getting the same error, what can I do to succesfully open csv files using Python? #Reading in the files import pandas as pd data1 = pd.read_csv("data1.csv")   UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte 

Answer: byte 0xff in position 0 means that your .csv is probably encoded in utf-16. Try this : data1 = pd.read_csv("data1.csv", encoding="utf-16")
Question: See the below Python 3.10 snippet: import contextlib class Foo: @contextlib.contextmanager @classmethod def state(cls): try: yield finally: pass with Foo.state(): pass  It throws a TypeError: Traceback (most recent call last): File "/path/to/code/play/quick_play.py", line 12, in <module> with Foo.state(): File "/path/to/.pyenv/versions/3.10.5/lib/python3.10/contextlib.py", line 281, in helper return _GeneratorContextManager(func, args, kwds) File "/path/to/.pyenv/versions/3.10.5/lib/python3.10/contextlib.py", line 103, in __init__ self.gen = func(*args, **kwds) TypeError: 'classmethod' object is not callable  Is it possible to decorate a classmethod with contextlib.contextmanager? And if yes, how can it be done?

Answer: This should work: import contextlib class Foo: @classmethod @contextlib.contextmanager def state(cls): try: print("A") yield print("C") finally: pass with Foo.state(): print("B")  This prints A B C.
Question: I am trying to deploy my dash app which uses dash_extensions, Dash_proxy and has multiple pages in the pages folder on GCP cloud run using gunicorn but the app cannot find the pages folder. It works perfectly fine when I use the development server but breaks in the production server because it cannot find the folder path. The app (following code is inside the app.py file): app = DashProxy(use_pages=True, pages_folder=pages_folder, external_stylesheets=[dbc.themes.SIMPLEX])  The app.py file and the pages folder are in the same directory I have tried tried to following methods to get the folder path: pages_folder="pages"  pages_folder=os.path.join(os.path.dirname(__file__), "pages")  for p in Path('.').rglob('*'): if str(p).endswith('pages'): pages_folder = str(p) break  None of the above three work in when deploying on gcp using gunicorn through docker: Dockerfile command: CMD ["gunicorn" , "-b", "0.0.0.0:8000", "app:server"]   But if I use dev server through docker like following code it works: CMD python app.py  Does anyone have any ideas of how to make it work with gunicorn? Thanks for the help! -Rexon

Answer: Yes I did. Just had to specify the root folder. This is what I did and it seems to work for me. pages_folder=os.path.join(os.path.dirname(__name__), "pages") app = DashProxy(__name__,use_pages=True, pages_folder=pages_folder, external_stylesheets=[dbc.themes.SIMPLEX]) server=app.server
Question: In a file whose only content is def test_sanity(): pass  I am trying to run the file, named test_something.py. The folder structure is uv_metadata |---uv_metadata |------tests |----------test_something.py  Getting the error ssh://noam.s@ML:2202/home/noam.s/src/uv_metadata/venv/bin/python -u /home/noam.s/.pycharm_helpers/pycharm/_jb_pytest_runner.py --target test_something.py::test_sanity Testing started at 14:34 ... sh: 1: cd: can't cd to C:/Users/noam.s/src/uv_metadata/uv_metadata/tests Launching pytest with arguments test_something.py::test_sanity --no-header --no-summary -q in /home/noam.s ============================= test session starts ============================== collected 0 items ============================ no tests ran in 0.00s ============================= ERROR: file or directory not found: test_something.py::test_sanity Process finished with exit code 4 Empty suite  I notice the line sh: 1: cd: can't cd to C:/Users/noam.s/src/uv_metadata/uv_metadata/tests, which doesn't make sense. Here is how my remote interpreter is configured:  This just stopped working, I don't know what has changed. How to make Pycharm recognize the test folder again?

Answer: PyCharm 2021.2.3 has a buggy deployment configuration, which can cause clashes and sometimes not work properly when some mappings are configured separately on the same interpreter, even from different projects. Likely, I was using the same interpreter with a different mapping, from some other Pycharm instance. I had to go to Build, Execution, Deployment -> Deployment and remove everything from the list. Following is the state after everything was clean and I added a new interpreter.  I also cleaned the interpreter list  and it went back to work.  A very inconvenient method to avoid this, but be safe My method of working to avoid this is having only a single path mapping per interpreter. If I need the same interpreter with different mappings, I create a new docker container, to make Pycharm think it is actually a different interpreter. If I need the same mapping with a different interpreter, there isn't a problem. For accessing the interpreter that runs inside a remote docker, I am tunneling PORT to 22 on the docker, and making ssh server run on the docker on startup, like so, and mapping ~ to ~ in the docker, so the actual same venv is reflected in all dockers.
Question: my code: import pyautogui from time import sleep x, y = pyautogui.locateOnScreen("slorixsh.png", confidence=0.5)  error: Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> x, y = pyautogui.locateOnScreen("slorixsh.png", confidence=0.5) File "C:\Users\mrnug\AppData\Local\Programs\Python\Python310\lib\site-packages\pyautogui\__init__.py", line 231, in _couldNotImportPyScreeze raise PyAutoGUIException( pyautogui.PyAutoGUIException: PyAutoGUI was unable to import pyscreeze. (This is likely because you're running a version of Python that Pillow (which pyscreeze depends on) doesn't support currently.) Please install this module to enable the function you tried to call.  line 231 in that error:  raise PyAutoGUIException( "PyAutoGUI was unable to import pyscreeze. (This is likely because you're running a version of Python that Pillow (which pyscreeze depends on) doesn't support currently.) Please install this module to enable the function you tried to call." )  somehow it doesnt work i just reinstalled and updated that modules: pyscreeze, opencv, pyautogui, pillow 

Answer: Yeah, you can fix it by this pip install pyscreeze import pyscreeze import time time.sleep(5) x, y = pyscreeze.locateOnScreen("slorixsh.png", confidence=0.5)
Question: I have a CSV file with ";" as separator made like this: col1;col2;col3;col4 4;hello;world;1;1 4;hi;1;1 4;hi;1;1 4;hi;1;1  Obviously by using ";" as sep it gives me the error about tokenizing data (obviously from the header less columns are expected), how can i obtain a dataframe like this: col1 col2 col3 col4 4 hello;world 1 1 4 hi 1 1 4 hi 1 1 4 hi 1 1  It could be read even with others packages and other data type (even if i prefer pandas because of the following operations in the code)

Answer: You could split off the outer cols until you are left with the remaining col2. This could be done in Pandas as follows: import pandas as pd df_raw = pd.read_csv("input.csv", delimiter=None, header=None, skiprows=1) df_raw[['col1', 'rest']] = df_raw[0].str.split(";", n=1, expand=True) df_raw[['col2', 'col3', 'col4']] = df_raw.rest.str.rsplit(";", n=2, expand=True) df = df_raw[['col1', 'col2', 'col3', 'col4']] print(df)  Giving df as:  col1 col2 col3 col4 0 4 hello;world 1 1 1 4 hi 1 1 2 4 hi 1 1 3 4 hi 1 1   First read in the CSV file without using any delimiters to get a single column.  Use a .str.split() with n=1 to split out just col1 using the ; delimiter from the left.  Take the remaining rest and apply .str.rsplit() with n=2 to do a reverse split using the ; delimiter to get the remaining columns. This allows col2 to have any have any characters.   This assume that only col2 can have additional ; separators and the last two are fixed.
Question: I have the following dataframe: client_id location_id region_name location_name 1 123 Florida location_ABC 6 123 Florida(P) location_ABC 6 845 Miami(P) location_THE 1 386 Boston location_WOP 6 386 Boston(P) location_WOP  What I'm trying to do is:  If some location_id has more than one client_id, I'll pick the client_id == 1. If some location_id has only one client_id, I'll pick whatever row it is.  If we were implementing only one logic, it should be as simple as df[df['client_id'] == 1]. But I can not figure out how to perform this type of filtering that requires verifying more rows at the same time (figure out how to check if some location_id has more then one client_id, for example). So, in this scenario, the resulting data frame would be: client_id location_id region_name location_name 1 123 Florida location_ABC 6 845 Miami(P) location_THE 1 386 Boston location_WOP  Any ideas?

Answer: You can use idxmax with a custom groupby on the boolean Series equal to your preferred id, then slice: out = df.loc[df['client_id'].eq(1).groupby(df['location_id'], wort=False).idxmax()]  output:  client_id location_id region_name location_name 0 1 123 Florida location_ABC 2 6 845 Miami(P) location_THE 3 1 386 Boston location_WOP
Question: The code below works to overlay two imshow plots, and to create a slider which changes the value of the global variable OPACITY. Unfortunately, img1.set_data(y); fig.canvas.draw_idle() doesn't redraw the new opacity. How to make an overlay of two imshow plots with a slider to change the opacity of the 2nd layer?  import numpy as np, matplotlib.pyplot as plt, matplotlib.widgets as mpwidgets OPACITY = 0.5 x = np.random.random((100, 50)) y = np.linspace(0, 0.1, 100*50).reshape((100, 50)) # PLOT fig, (ax0, ax1) = plt.subplots(2, 1, gridspec_kw={'height_ratios': [5, 1]}) img0 = ax0.imshow(x, cmap="jet") img1 = ax0.imshow(y, cmap="jet", alpha=OPACITY) def update(value): global OPACITY OPACITY = value print(OPACITY) img1.set_data(y) fig.canvas.draw_idle() slider0 = mpwidgets.Slider(ax=ax1, label='opacity', valmin=0, valmax=1, valinit=OPACITY) slider0.on_changed(update) plt.show() 

Answer: You can re-set the opacity of the overlaying img1 within the update function by img1.set_alpha():   Code: import numpy as np, matplotlib.pyplot as plt, matplotlib.widgets as mpwidgets OPACITY = 0.5 x = np.random.random((100, 50)) y = np.linspace(0, 0.1, 100*50).reshape((100, 50)) # PLOT fig, (ax0, ax1) = plt.subplots(2, 1, gridspec_kw={'height_ratios': [5, 1]}) img0 = ax0.imshow(x, cmap="jet") img1 = ax0.imshow(y, cmap="jet", alpha=OPACITY) def update(value): img1.set_alpha(value) fig.canvas.draw_idle() slider0 = mpwidgets.Slider(ax=ax1, label='opacity', valmin=0, valmax=1, valinit=OPACITY) slider0.on_changed(update) plt.show()
Question: I'm trying to understand the behaviour of the logging module when using basicConfig with multiple handlers. My aim is to have warning level on syslog messages, and debug level to a log file. This is my setup: import logging import logging.handlers import os from datetime import datetime log_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') f_handler = logging.FileHandler(f'{os.path.expanduser("~")}/logs/test_{datetime.now().strftime("%Y%m%d%H%M%S")}.log') s_handler = logging.handlers.SysLogHandler(address='/dev/log') f_handler.setLevel(logging.DEBUG) s_handler.setLevel(logging.WARN) f_handler.setFormatter(log_format) s_handler.setFormatter(log_format) logging.basicConfig(handlers=[f_handler, s_handler]) print (logging.getLogger().getEffectiveLevel()) logging.warning('This is a warning') logging.error('This is an error') logging.info('This is an info') logging.debug('This is debug')  This does not work - my log file only contains the warning and error messages, not the debug and info messages as intended. If I change this line to: logging.basicConfig(handlers=[f_handler, s_handler], level=logging.DEBUG)  Then this does seem to work, i.e. the file contains all messages and syslog contains only the error and warning message. I don't understand why the original code did not work, given that I set the level on the file handler specifically.

Answer: It's because you need to set a level on the logger, as that's checked first, and if there is further processing to be done, then the event is passed to handlers, where it's checked against their levels.  Level on logger - overall verbosity of application Level on handler - verbosity for the destination represented by that handler  See the diagram below for more information.
Question: I want to replace multiple substring at once, for instance, in the following statement I want to replace dog with cat and cat with dog: I have a dog but not a cat. However, when I use sequential replace string.replace('dog', 'cat') and then string.replace('cat', 'dog'), I get the following. I have a dog but not a dog. I have a long list of replacements to be done at once so a nested replace with temp will not help.

Answer: One way using re.sub: import re string = "I have a dog but not a cat." d = {"dog": "cat", "cat": "dog"} new_string = re.sub("|".join(d), lambda x: d[x.group(0)], string)  Output: 'I have a cat but not a dog.'
Question: I have a dataframe like this: df3 = pd.DataFrame({'ID': ['Stay home, T5006, T5006, Stay home', 'Go for walk, T5007, T5007, Go for walk'], 'Name': ['Stay home, Go for walk, Stay home', 'Go outside, Go outside, Go outside'] }) ID Name 0 Stay home, T5006, T5006, Stay home Stay home, Go for walk, Stay home 1 Go for walk, T5007, T5007, Go for walk Go outside, Go outside, Go outside  I want to delete the dulicates from ID column. Expected outcome:  ID Name 0 Stay home,T5006 Stay home, Go for walk, Stay home 1 Go for walk,T5007 Go outside, Go outside, Go outside  Any ideas?

Answer: Use  dict.fromkey trick for remove duplicates of splitted values, then join by ,  in lambda function: df3['ID'] = df3['ID'].apply(lambda x: ', '.join(dict.fromkeys(x.split(', '))))  Or use list comprehension: df3['ID'] = [', '.join(dict.fromkeys(x.split(', '))) for x in df3['ID']] print (df3) ID Name 0 Stay home, T5006 Stay home, Go for walk, Stay home 1 Go for walk, T5007 Go outside, Go outside, Go outside  Of if possible order is not important use sets: df3['ID'] = df3['ID'].apply(lambda x: ', '.join(set(x.split(', ')))) df3['ID'] = [', '.join(set(x.split(', '))) for x in df3['ID']] print (df3) ID Name 0 Stay home, T5006 Stay home, Go for walk, Stay home 1 T5007, Go for walk Go outside, Go outside, Go outside
Question: I have made a deplyoment to Digital ocean, on staging (Heroku server) the app is working well, but Digital ocean it's failing with the error below, what could be the issue : AttributeError at /admin/ 'EntryPoints' object has no attribute 'get' Request Method: GET Request URL: https://xxxx/admin/ Django Version: 3.1 Exception Type: AttributeError Exception Value: 'EntryPoints' object has no attribute 'get' Exception Location: /usr/local/lib/python3.7/site-packages/markdown/util.py, line 85, in <module> Python Executable: /usr/local/bin/python Python Version: 3.7.5 Python Path: ['/opt/app', '/usr/local/bin', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/site-packages', '/usr/local/lib/python3.7/site-packages/odf', '/usr/local/lib/python3.7/site-packages/odf', '/usr/local/lib/python3.7/site-packages/odf', '/usr/local/lib/python3.7/site-packages/odf', '/usr/local/lib/python3.7/site-packages/odf', '/usr/local/lib/python3.7/site-packages/odf', '/usr/local/lib/python3.7/site-packages/odf'] Server time: Sun, 02 Oct 2022 21:41:00 +0000 

Answer: Because importlib-metadata releases v5.0.0 yesterday which it remove deprecated endpoint. You can set importlib-metadata<5.0 in ur setup.py so it does not install latest version. Or if you use requirements.txt, you can as well set importlib-metadata below version 5.0 e.g importlib-metadata==4.13.0 For more info: https://importlib-metadata.readthedocs.io/en/latest/history.html
Question: say, I have very simple piece of code py = """ a = 1 print (f'before all {a=}') def bar(n): print(f'bye {n=}') def foo(n): print(f'hello {n=}') bar(n) bar ('just works') foo ('sara') """ loc = {} glo = {} bytecode = compile(py, "script", "exec") exec(bytecode, glo, loc)  as you can see I defined two functions: bar & foo and called both of them with results: before all a=1 bye n='just works' hello n='sara' Traceback (most recent call last): File "/home/bla-bla/pythonProject/main_deco2.py", line 43, in <module> exec(bytecode, glo, loc) File "script", line 13, in <module> File "script", line 10, in foo NameError: name 'bar' is not defined  and this leaves me puzzled, as I don't understand why function foo doesn't see bar when just a second ago I was able to call bar without a problem ?

Answer: From the docs:  If exec gets two separate objects as globals and locals, the code will be executed as if it were embedded in a class definition.  That's not what you want, so don't provide a locals. glo = {} exec(bytecode, glo)  Output, for reference: before all a=1 bye n='just works' hello n='sara' bye n='sara'
Question: In Drake/pydrake, all of the examples I have seen create an instance of Simulator and then advance the simulation by a set duration, e.g. sim.AdvanceTo(10) will step the simulation until it has simulated 10 seconds of activity. How can I instead run a simulation indefinitely, where I don't have a specific duration to advance to, just have the simulation running in its own node and step at a fixed interval forever? My intention is to just have the simulator running, and then send requests to update commands that will be sent to the robot. Is there a straightforward way to achieve this that won't break the simulation? I figured there would be a public Step function on Simulator, but have only encountered AdvanceTo. My first thought was to try to take the context from a call to AdvanceTo over a short interval, e.g. AdvanceTo(0.01) and then overwrite the context for the next call so that it's updating from the new context instead of the original, but I'm not sure it will work. Maybe there's a more official way to achieve this scheme?

Answer: You can do simulator.AdvanceTo(std::numeric_limits<double>::infinity()); to keep the simulation running indefinitely. https://github.com/RobotLocomotion/drake/tree/master/examples/kuka_iiwa_arm may be helpful as an example.
Question: The models.py IntegerField is set-up: class DMCAModel(models.Model): tick = models.CharField(max_length=20) percent_calc = models.FloatField() ratio = models.IntegerField(blank=True, null=True, default=None) #The problem is here created_at = models.DateTimeField(auto_now_add=True) owner = models.ForeignKey(User, related_name='dmca', on_delete=models.CASCADE, null=True)  serilaizers.py class DMCASerializer(serializers.ModelSerializer): class Meta: model = DMCAModel fields = '__all__' # fields = ['id', 'tick', 'percent_calc', 'ratio'] # I have tried removing fields, if I remove 'ratio' then even if I send an input it is ignored.  api.py class DmcaViewSet(viewsets.ModelViewSet): # queryset = DMCAModel.objects.all() permission_classes = [ permissions.IsAuthenticated ] serializer_class = DMCASerializer def get_queryset(self): return self.request.user.dmca.all() def perform_create(self, serializer): # print(self.request.user) serializer.save(owner=self.request.user)  When I to make the following POST request from postman: Authorization: Token xxxxxx Body JSON: { "tick":"XYZ", "percent_calc":"3", "ratio":"" }  I get the error:  { "ratio": [ "A valid integer is required." ] }  I think I have set the properties of the Models field correctly if I understand them correctly: null=True database will set empty to NULL, blank=True form input will be accepted, default=None incase nothing is entered. I have tried to use the properties one by one and see what happens. But can't work out the error.

Answer: Inside your serializer, you can use a CharField for the ratio field if you want to send an empty string and then convert to Int in the validation method. class DMCASerializer(serializers.ModelSerializer): ratio = serializers.CharField(required=False, allow_null=True, allow_blank=True) class Meta: model = DMCAModel fields = '__all__' def validate_ratio(self, value): if not value: return None try: return int(value) except ValueError: raise serializers.ValidationError('Valid integer is required')  Also, you need to change serializer_class inside DmcaViewSet from: serializer_class = DcaSerializer  to serializer_class = DMCASerializer  When I send an empty string:  When I send an Integer: