eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I deployed a Django 4 app with Daphne (ASGI) in a docker container. I use Caddy as a reverse proxy in front. It works, except I can't fill in any form because the CSRF protection kicks in. So no admin login, for example. I can currently access the admin interface in two ways:  Directly through docker, via a SSH tunelled port Through Caddy, which is then forwarding to the Docker container.  Option 1 works. I can log into the admin interface just as if I was running the development server locally. All is working as expected. However, option 2 (caddy reverse proxy) doesn't work. I can access Django and load pages, but any form submission will be blocked because the CSRF protection kicks in. CSRF verification failed. Request aborted. Reason given for failure: Origin checking failed - https://<mydomain.com> does not match any trusted origins.  My Caddyfile contains this: <mydomain.com> { reverse_proxy localhost:8088 }  localhost:8088 is the port exposed by my docker container. In an effort to eliminate potential issues, I've set the following to false in my config file:  SECURE_SSL_REDIRECT (causes a redirect loop, probably related to the reverse proxying) SESSION_COOKIE_SECURE (I'd rather have it set to True, but I don't know at this point) CSRF_COOKIE_SECURE (same remark)  The only Django-Caddy examples I could find online are outdated and refer to older versions of Caddy and/or Django. Django is deployed on ASGI with Daphne. I've seens posts suggesting to change CSRF_TRUSTED_ORIGINS, but it doesn't seem right that I would have to add a host that is already in the ALLOWED_HOSTS list. That also wouldn't explain why it works directly on the docker container, unless localhost is a special case for CSRF. Versions:  Caddy: 2.5.1 Django: 4.0.5 Daphne: 3.0.2 Python: 3.10.5  Any idea what goes wrong, and how I should go about debugging such issues?

Answer: Finally found out what was happening. I first wanted to know the exact HTTP request that was sent from caddy to django: sudo tcpdump -i lo -A -n port 8088  This confirmed that:  the Origin and Referer headers were set properly the csrftoken cookie was sent properly  Once that was known, I could dig in the code from django. Specifically, this function in the CSRF middleware. In conclusion:  Caddy forwards the http request to django unencrypted (so HTTP-non-S between caddy and django). Django considers that request non-secure The CSRF protection expects the Origin header sent by the browser to be http:// because the request is not secure. In my case, it is https:// because my browser is talking to Caddy over https Because the Origin header does not match what the CSRF middleware expects, the request is rejected  It's actually a simple fix. Since we know that Caddy will always ignore X-Forwarded-Proto from the browser and sets it itself, we can add SECURE_PROXY_SSL_HEADER to the settings.py in django: SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')  And voilà! Now I can also set these to true:  SECURE_SSL_REDIRECT SESSION_COOKIE_SECURE CSRF_COOKIE_SECURE  EDIT Here's the Caddyfile, as per requested: service.mywebsite.com { reverse_proxy localhost:8088 }
Question: I am writing the code for Autonomous Driving using RL. I am using a stable baseline3 and an open ai gym environment. I was running the following code in the jupyter notebook and it is giving me the following error: # Testing our model episodes = 5 # test the environment 5 times for episodes in range(1,episodes+1): # looping through each episodes bs = env.reset() # observation space # Taking the obs and passing it through our model # tells that which kind of the action is best for our work done = False score = 0 while not done: env.render() action, _ = model.predict(obs) # now using model here # returns model action and next state # take that action to get the best reward # for observation space we get the box environment # rather than getting random action we are using model.predict(obs) on our obs for an curr env to gen the action inorder to get best possible reward obs, reward, done, info = env.step(action) # gies state, reward whose value is 1 # reward is 1 for every step including the termination step score += reward print('Episode:{},Score:{}'.format(episodes,score))''' env.close()  Error    The link for the code that I have written is given below: https://drive.google.com/file/d/1JBVmPLn-N1GCl_Rgb6-qGMpJyWvBaR1N/view?usp=sharing The version of python I am using is Python 3.8.13 in Anaconda Environment. I am using Pytorch CPU version and the OS is Windows 10. Please help me out in solving this question.

Answer: Using .copy() for numpy arrays should help (because PyTorch tensors can't handle negative strides): action, _ = model.predict(obs.copy())  I haven't managed to run your notebook quickly because of dependencies problems, but I had the same error with AI2THOR simulator, and adding .copy() has helped. Maybe someone with more technical knowledge about numpy, torch or AI2THOR will explain why the error occurs in more detail.
Question: The only mechanism I can find for handling a keyboard interrupt is to poll. Without the while loop below, the signal processing never happens and the process hangs forever. Is there any graceful mechanism for allowing a keyboard interrupt to function when given a concurrent future object? Putting polling loops all over my code base seems to defeat the purpose of using futures at all. More info:  Waiting on the future in the main thread in Windows blocks all signal handling, even if it's fully cancellable and even if it has not "started" yet. The word "exiting" doesn't even print. So 'cancellability' is only part (the easy part) of the issue. In my real code, I obtain futures via executors (run coro threadsafe, in this case), this was just a simplified example  import concurrent.futures import signal import time import sys fut = concurrent.futures.Future() def handler(signum, frame): print("exiting") fut.cancel() signal.signal(signal.SIGINT, orig) sys.exit() orig = signal.signal(signal.SIGINT, handler) # a time sleep is fully interruptible with a signal... but a future isnt # time.sleep(100) while True: try: fut.result(.03) except concurrent.futures.TimeoutError: pass 

Answer: OK, I wrote a solution to this based on digging in cypython source and some bug reports - but it's not pretty. If you want to be able to interrupt a future, especially on Windows, the following seems to work: @contextlib.contextmanager def interrupt_futures(futures): # pragma: no cover """Allows a list of futures to be interrupted. If an interrupt happens, they will all have their exceptions set to KeyboardInterrupt """ # this has to be manually tested for now, because the tests interfere with the test runner def do_interr(*_): for ent in futures: try: ent.set_exception(KeyboardInterrupt) except: # if the future is already resolved or cancelled, ignore it pass return 1 if sys.platform == "win32": from ctypes import wintypes # pylint: disable=import-outside-toplevel kernel32 = ctypes.WinDLL("kernel32", use_last_error=True) CTRL_C_EVENT = 0 CTRL_BREAK_EVENT = 1 HANDLER_ROUTINE = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.DWORD) @HANDLER_ROUTINE def handler(ctrl): if ctrl == CTRL_C_EVENT: handled = do_interr() elif ctrl == CTRL_BREAK_EVENT: handled = do_interr() else: handled = False # If not handled, call the next handler. return handled if not kernel32.SetConsoleCtrlHandler(handler, True): raise ctypes.WinError(ctypes.get_last_error()) was = signal.signal(signal.SIGINT, do_interr) yield signal.signal(signal.SIGINT, was) # restore default handler kernel32.SetConsoleCtrlHandler(handler, False) else: was = signal.signal(signal.SIGINT, do_interr) yield signal.signal(signal.SIGINT, was)  This allows you to do this: with interrupt_futures([fut]): fut.result()  For the duration of that call, interrupt signals will be intercepted and will result in the future raising a KeyboardInterrupt to the caller requesting the result - instead of simply ignoring all interrupts.
Question: I'm trying to install pycaret==3.0.0 in google colab, But I'm having a problem, the library requires Jinja2 to be installed which I did, but then It finally throws off another error. ImportError Traceback (most recent call last) <ipython-input-26-4f8843d24b3a> in <module>() ----> 1 import jinja2 2 from pycaret.regression import * 3 frames /usr/local/lib/python3.7/dist-packages/jinja2/filters.py in <module>() 11 from markupsafe import escape 12 from markupsafe import Markup ---> 13 from markupsafe import soft_unicode 14 15 from ._compat import abc ImportError: cannot import name 'soft_unicode' from 'markupsafe' (/root/.local/lib/python3.7/site-packages/markupsafe/__init__.py) 

Answer: This is caused by upgrade in MarkupSafe:2.1.0 where they have removed soft_unicode, try using: pip install markupsafe==2.0.1
Question: I want to work with python 3.6 due to some file compatibility issue, can anyone help me out, where I can use python 3.6 in google Colab, apart from that I want to use tensorflow 2.0 and opencv 4.0.0.21, which I have already install in Colab am only stuck with python 3.6. any help would be appreciated

Answer: You can use the command below and then select the alternative version to change the python version. !sudo update-alternatives --config python3  Output: There are 2 choices for the alternative python3 (providing /usr/bin/python3). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/bin/python3.7 2 auto mode 1 /usr/bin/python3.6 1 manual mode 2 /usr/bin/python3.7 2 manual mode Press <enter> to keep the current choice[*], or type selection number: 1 update-alternatives: using /usr/bin/python3.6 to provide /usr/bin/python3 (python3) in manual mode  To check the python version use the following code. python --version # 3.6.9
Question: I have a Python script that will run on certain intervals as the cron schedule will invoke the Python script inside a Docker container. I want the cron schedule expression to be set through an environment variable, like this: CRON_SCHEDULE="*/5 * * * *"  So the user can freely choose how often the script will run. On the other hand, I have a hard time making a bash script that will read this environment file and replace the existing crontab using sed as I need to escape any possible character. That brings me to the point where I'm wondering if there is any better solution for running a Python script on a schedule while also having easy configuration of the running schedule?

Answer: You should use the solution described in this answer https://stackoverflow.com/a/70897876/3669093 Whenever the environment variable is changed the container is restarted and the schedule is updated. If you need more guidance, shoot your questions.
Question: I've read about and understand floating point round-off issues such as: >>> sum([0.1] * 10) == 1.0 False >>> 1.1 + 2.2 == 3.3 False >>> sin(radians(45)) == sqrt(2) / 2 False  I also know how to work around these issues with math.isclose() and cmath.isclose(). The question is how to apply those work arounds to Python's match/case statement. I would like this to work: match 1.1 + 2.2: case 3.3: print('hit!') # currently, this doesn't match 

Answer: The key to the solution is to build a wrapper that overrides the __eq__ method and replaces it with an approximate match: import cmath class Approximately(complex): def __new__(cls, x, /, **kwargs): result = complex.__new__(cls, x) result.kwargs = kwargs return result def __eq__(self, other): try: return isclose(self, other, **self.kwargs) except TypeError: return NotImplemented  It creates approximate equality tests for both float values and complex values: >>> Approximately(1.1 + 2.2) == 3.3 True >>> Approximately(1.1 + 2.2, abs_tol=0.2) == 3.4 True >>> Approximately(1.1j + 2.2j) == 0.0 + 3.3j True  Here is how to use it in a match/case statement: for x in [sum([0.1] * 10), 1.1 + 2.2, sin(radians(45))]: match Approximately(x): case 1.0: print(x, 'sums to about 1.0') case 3.3: print(x, 'sums to about 3.3') case 0.7071067811865475: print(x, 'is close to sqrt(2) / 2') case _: print('Mismatch')  This outputs: 0.9999999999999999 sums to about 1.0 3.3000000000000003 sums to about 3.3 0.7071067811865475 is close to sqrt(2) / 2
Question: In pytorch, if I do something like import torch x = torch.randn(3) y = x + 5  all tensors correspond to the "cpu" device by default. Is there some way to make to make it so, by default, all tensors are on another device (e.g. "cuda:0")? I know I can always be careful to add .cuda() or specify cuda whenever creating a tensor, but it would be great if I could just change the default device directly at the beginning of the program and be done with it, so that torch.randn(3) comes from the desired device without having to specify it every time. Or would that be a bad thing to do for some reason? E.g. is there any reason I wouldn't want every tensor/operation to be done on cuda by default?

Answer: Pytorch has an optional function to change the default type of tensor set_default_tensor_type. Applying the default type on the main script: >>> import torch >>> >>> if __name__ == '__main__': ... cuda = torch.cuda.is_available() ... if cuda: ... torch.set_default_tensor_type('torch.cuda.FloatTensor') ... a = torch.randn(3,3) ... print(a.device) ... cuda:0   Or would that be a bad thing to do for some reason? E.g. is there any reason I wouldn't want every tensor/operation to be done on cuda by default?  I couldn't find any reference or any document to answer this question. However, in my opinion, it's to avoid the memory fragmentation in GPU memory. I'm not an expert, but the data in memory should be arranged in an efficient way, if not, the redundant space will cause OOM. That's why, in default, Tensorflow will take all of your GPU's memory no matter how many parameters your model has. You can improve the space and speed just by setting the tensor shape multiples of 8 amp documents.  In practice, higher performance is achieved when A and B dimensions are multiples of 8.  In conclusion, I think it's better to control the device of tensor manually instead of setting it gpu as default.
Question: I am trying to check the lint on the gitubaction. my github action steps are as below  lint: name: Lint runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v3 - name: Set up Python uses: actions/setup-python@v4 with: python-version-file: '.python-version' cache: 'pip' cache-dependency-path: 'requirements.txt'  Error screenshot Attached below  Could you please help me how to fix this?

Answer: lint: name: Lint runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v3 - name: Set up Python uses: actions/setup-python@v4 with: python-version-file: '.python-version' - name: Cache dependencies uses: actions/cache@v3 with: path: ~/.cache/pip key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }} restore-keys: | ${{ runner.os }}-pip- ${{ runner.os }}-  I too faced the same problem. It is because of the cache server not responding that includes internal server error or any other. You can use actions/cache@v3 instead of the automatic cache by python using cache: 'pip' because the action/cache does the same but only gives warning on the server error
Question: I was using PyPdf to read text from a pdf file. However pyPDF does not read the text in pdf line by line, Its reading in some haphazard manner. Putting new line somewhere when its not even present in the pdf. import PyPDF2 pdf_path = r'C:\Users\PDFExample\Desktop\Temp\sample.pdf' pdfFileObj = open(pdf_path, 'rb') pdfReader = PyPDF2.PdfFileReader(pdfFileObj) page_nos = pdfReader.numPages for i in range(page_nos): # Creating a page object pageObj = pdfReader.getPage(i) # Printing Page Number print("Page No: ",i) # Extracting text from page # And splitting it into chunks of lines text = pageObj.extractText().split(" ") # Finally the lines are stored into list # For iterating over list a loop is used for i in range(len(text)): # Printing the line # Lines are seprated using "\n" print(text[i],end="\n\n") print()  This gives me content as Our Ref : 21 1 8 88 1 11 5 Name: S ky Blue Ref 1 : 1 2 - 34 - 56789 - 2021/2 Ref 2: F2021004 444 Amount: $ 1 00 . 11 ...  Whereas expected was Our Ref :2118881115 Name: Sky Blue Ref 1 :12-34-56789-2021/2 Ref 2:F2021004444 Amount: $100.11 Total Paid:$0.00 Balance: $100.11 Date of A/C: 01/08/2021 Date Received: 10/12/2021 Last Paid: Amt Last Paid: A/C Status: CLOSED Collector : Sunny Jane  Here is the link to the pdf file https://pdfhost.io/v/eCiktZR2d_sample2

Answer: I tried a different package called as pdfplumber. It was able to read the pdf line by line in exact way in which I wanted. 1. Install the package pdfplumber pip install pdfplumber  2. Get the text and store it in some container import pdfplumber pdf_text = None with pdfplumber.open(pdf_path) as pdf: first_page = pdf.pages[0] pdf_text = first_page.extract_text()
Question: I need to average the Y values corresponding to the values in the X array... X=np.array([ 1, 1, 2, 2, 2, 2, 3, 3 ... ]) Y=np.array([ 10, 30, 15, 10, 16, 10, 15, 20 ... ])  In other words, the equivalents of the 1 values in the X array are 10 and 30 in the Y array, and the average of this is 20, the equivalents of the 2 values are 15, 10, 16, and 10, and their average is 12.75, and so on... How can I calculate these average values?

Answer: import numpy as np X = np.array([ 1, 1, 2, 2, 2, 2, 3, 3]) Y = np.array([ 10, 30, 15, 10, 16, 10, 15, 20]) # Only unique values unique_vals = np.unique(X); # Loop for every value for val in unique_vals: # Search for proper indexes in Y idx = np.where(X == val) # Mean for finded indexes aver = np.mean(Y[idx]) print(f"Average for {val}: {aver}")  Result: Average for 1: 20.0 Average for 2: 12.75 Average for 3: 17.5
Question: New to AWS CDK and I'm trying to create a load balanced fargate service with the construct ApplicationLoadBalancedFargateService. I have an existing image on ECR that I would like to reference and use. I've found the ecs.ContainerImage.from_ecr_repository function, which I believe is what I should use in this case. However, this function takes an IRepository as a parameter and I cannot find anything under aws_ecr.IRepository or aws_ecr.Repository to reference a pre-existing image. These constructs all seem to be for making a new repository. Anyone know what I should be using to get the IRepository object for an existing repo? Is this just not typically done this way? Code is below. Thanks in Advance. from aws_cdk import ( # Duration, Stack, # aws_sqs as sqs, ) from constructs import Construct from aws_cdk import (aws_ec2 as ec2, aws_ecs as ecs, aws_ecs_patterns as ecs_patterns, aws_route53,aws_certificatemanager, aws_ecr) class NewStack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) _repo = aws_ecr.Repository(self, 'id1', repository_uri = repo_uri) vpc = ec2.Vpc(self, "applications", max_azs=3) # default is all AZs in region cluster = ecs.Cluster(self, "id2", vpc=vpc) hosted_zone = aws_route53.HostedZone.from_lookup(self, 'id3', domain_name = 'domain' ) certificate = aws_certificatemanager.Certificate.from_certificate_arn(self, id4, 'cert_arn' ) image = ecs.ContainerImage.from_ecr_repository(self, _repo) ecs_patterns.ApplicationLoadBalancedFargateService(self, "id5", cluster=cluster, # Required cpu=512, # Default is 256 desired_count=2, # Default is 1 task_image_options=ecs_patterns.ApplicationLoadBalancedTaskImageOptions( image = image, container_port=8000), memory_limit_mib=2048, # Default is 512 public_load_balancer=True, domain_name = 'domain_name', domain_zone = hosted_zone, certificate = certificate, redirect_http = True) 

Answer: You are looking for from_repository_attributes() to create an instance of IRepository from an existing ECR repository.
Question: Question: Is it possible to turn off "corner rounding" when using plot in matplotlib? Setup:  I am trying to present a complicated nonsmooth function in a presentation. As a default (understandably) matplotlib rounds corners. (This is especially visible when the linewidth is increased.) I need more linewidth so the plot can be seen when the figure is projected.  Example: import numpy as np import matplotlib.pyplot as plt x = np.linspace(-1, 1, 1001) y = abs(x) plt.plot(x, y, linewidth=10) plt.show()  produces the image:  Attempts:  Increasing the number of points in x does not resolve this issue. Note that the point x=0 is included in the plot.  Summary: The plotted curve above appears rounded at x=0 when the function is not.

Answer: You can specify a JoinStyle: plt.plot(x, y, linewidth=10, solid_joinstyle='miter')
Question: I'm using a miniforge environment on an M1 mac, and unable to import psutil: ImportError: dlopen(/Users/caspsea/miniforge3/lib/python3.9/site-packages/psutil/_psutil_osx.cpython-39-darwin.so, 0x0002): tried: '/Users/caspsea/miniforge3/lib/python3.9/site-packages/psutil/_psutil_osx.cpython-39-darwin.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e')), '/usr/local/lib/_psutil_osx.cpython-39-darwin.so' (no such file), '/usr/lib/_psutil_osx.cpython-39-darwin.so' (no such file) I tried uninstalling and reinstalling using pip but that did not work. I'm using python 3.9, OS Monterey 12.2.1

Answer: Have you tried: pip uninstall psutil followed by: pip install --no-binary :all: psutil
Question: Formatting appears to work differently if the object you're formatting is a date. today = "aaa" print(f'{today:>10}')  returns  aaa  i.e. it has the padding. If I now do this: today = datetime.date.today() print(f'{today:>10}')  then the response is >10  Which is obviously not what I want. I've tried various other combinations where I put in the date format as well, but all it does is draw the date out and then also add in '>10' also. How do I format a date with padding?

Answer: Python's scheme for formatting via f-strings (and the .format method of strings) allows the inserted data to override how the format specification works, using the __format__ magic method: >>> class Example: ... def __format__(self, template): ... return f'{template} formatting of {self.__class__.__name__} instance' ... >>> f'{Example():test}' 'test formatting of Example instance'  datetime.date does this, so that time.strftime is used to do the formatting (after some manipulation, e.g. inserting a proxy time for dates and vice-versa): >>> help(today.__format__) Help on built-in function __format__: __format__(...) method of datetime.date instance Formats self with strftime.  This means that codes like %Y etc. can be used, but field width specifiers (like >10) are not supported. The format string >10 doesn't contain any placeholders for any components of the date (or time), so you just get a literal >10 back. Fortunately, it is trivial to work around this. Simply coerce the date to string, and pad the string: >>> f'{str(today):>20}' ' 2022-06-13'  Or better yet, use the built-in syntax for such coercion: >>> f'{today!s:>20}' # !s for str(), !r for repr() ' 2022-06-13'  If you want to use the strftime formatting as well, do the formatting in two steps: >>> formatted = f'{today:%B %d, %Y}' >>> f'{formatted:>20}' ' June 13, 2022'  Note that it does not work to nest format specifiers: >>> # the {{ is interpreted as an escaped literal { >>> f'{{today:%B %d, %Y}:>20}' File "<stdin>", line 1 SyntaxError: f-string: single '}' is not allowed >>> # the inner {} looks like a dict, but %B isn't an identifier >>> f'{ {today:%B %d, %Y}:>20}' File "<fstring>", line 1 ( {today:%B %d, %Y}) ^ SyntaxError: invalid syntax  However, f-strings themselves can be nested (this is obviously not very elegant and will not scale well): >>> # instead of trying to format the result from another placeholder, >>> # we reformat an entire separately-formatted string: >>> f'{f"{today:%B %d, %Y}":>20}' ' June 13, 2022'