eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I need to install the eb command on windows. I would like to try to deploy an application on AWS using the elasticbeanstalk service, and through this command you can configure and deploy an environment directly with a configuration file. To do this I followed the guide. I first installed python via the site (Python version 3.12.0), and then all the steps described in the guide link. Now if I run the eb command from cmd I always get this error. Traceback (most recent call last): File "<frozen runpy>", line 198, in _run_module_as_main File "<frozen runpy>", line 88, in _run_code File "C:\Users\Utente\.ebcli-virtual-env\Scripts\eb.exe\__main__.py", line 4, in <module> File "C:\Users\Utente\.ebcli-virtual-env\Lib\site-packages\ebcli\core\ebcore.py", line 16, in <module> from cement.core import foundation, handler, hook File "C:\Users\Utente\.ebcli-virtual-env\Lib\site-packages\cement\core\foundation.py", line 11, in <module> from ..core import output, extension, arg, controller, meta, cache, mail File "C:\Users\Utente\.ebcli-virtual-env\Lib\site-packages\cement\core\extension.py", line 8, in <module> from imp import reload # pragma: no cover ^^^^^^^^^^^^^^^^^^^^^^ ModuleNotFoundError: No module named 'imp'  I've tried several things but can't come to a conclusion. Does anyone know how to help me? I also tried installing previous versions of python, even though I didn't like it as a solution, but still I still have the problem.

Answer: I encountered this as well. As far as I understand its a deprecation issue. awsebcli will install with Python 3.12 but imp will not. If you type import imp into Python 3.11 you will get the following response  DeprecationWarning: the imp module is deprecated in favour of importlib and slated for removal in Python 3.12; see the module's documentation for alternative uses  At the time of writing this, Elastic Beanstalk is only supporting 3.8, 3.9 & 3.11 https://docs.aws.amazon.com/elasticbeanstalk/latest/platforms/platforms-supported.html#platforms-supported.python
Question: I'm trying to reproduce this answer but getting the following error:  AttributeError: 'GrouperView' object has no attribute 'join'  --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[283], line 7 4 flights = flights.pivot("month", "year", "passengers") 5 f,(ax1,ax2,ax3, axcb) = plt.subplots(1,4, 6 gridspec_kw={'width_ratios':[1,1,1,0.08]}) ----> 7 ax1.get_shared_y_axes().join(ax2,ax3) 8 g1 = sns.heatmap(flights,cmap="YlGnBu",cbar=False,ax=ax1) 9 g1.set_ylabel('') AttributeError: 'GrouperView' object has no attribute 'join'  Also, seaborn version is as below: print(sns.__version__) #0.13.0 import matplotlib print('matplotlib: {}'.format(matplotlib.__version__) #matplotlib: 3.8.1)  I checked some working around but couldn't solve the problem in the plot since there is no real string ax2,ax3 when I print :  I am getting an error as list object has no attribute 'join'  Even I downgraded seaborn but didn't solve the problem based on this thread

Answer: As proposed in the 3.6 API changes, (and repeated in the 3.8 API changes), use Axes.sharey. ax2.sharey(ax1) ax3.sharey(ax1)  Since an Axes can only sharey with one other Axes, I'm unaware of an alternative that lets ax1 share with both ax2 and ax3 in one step.
Question: I have a function using Polars Expressions to calculate the standard deviation of the residuals from a linear regression (courtesy of this post). Now I would like to apply this function using a rolling window over a dataframe. My approaches below fail because I don't know how to pass two columns as arguments to the function, since rolling_map() applies to an Expr. Is there a way to do this directly in Polars, or do I need to use a workaround with Pandas? Thank you for your support! (feels like I'm missing something obvious here...) import polars as pl def ols_residuals_std(x: pl.Expr, y: pl.Expr) -> pl.Expr: # Calculate linear regression residuals and return the standard deviation thereof x_center = x - x.mean() y_center = y - y.mean() beta = x_center.dot(y_center) / x_center.pow(2).sum() e = y_center - beta * x_center return e.std() df = pl.DataFrame({'a': [45, 76, 4, 88, 66, 5, 24, 72, 93, 87, 23, 40], 'b': [77, 11, 56, 43, 61, 25, 63, 7, 66, 17, 64, 75]}) # Applying the function over the full length - works df = df.with_columns(ols_residuals_std(pl.col('a'), pl.col('b')).alias('e_std')) df.with_columns(pl.col('a').rolling_map(ols_residuals_std(pl.col('a'), pl.col('b')), window_size=4, min_periods=1).alias('e_std_win')) # PanicException: python function failed: PyErr { type: <class 'TypeError'>, value: TypeError("'Expr' object is not callable"), traceback: None } df.with_columns(pl.col('a', 'b').rolling_map(ols_residuals_std(), window_size=4, min_periods=1).alias('e_std_win')) # TypeError: ols_residuals_std() missing 2 required positional arguments: 'x' and 'y' 

Answer: One thing to note about in rolling_map is that it is used for a custom function. While your expression is defined with a function, it isn't what they mean. What they mean is a python function which takes in values and outputs a value. This is also hinted at by the name having map which coincides to map_elements and map_batches. Additionally there's a Warning that it will be extremely slow which also hints at its expectation. To get at what you want to do, you can use rolling which unfortunately doesn't infer an index column so you have to manually create it. ( df .with_row_index('i') .with_columns( ols_residuals_std(pl.col('a'), pl.col('b')) .rolling('i',period='4i').alias('e_std_win') ) .drop('i') ) shape: (12, 3) ┌─────┬─────┬───────────┐ │ a ┆ b ┆ e_std_win │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ f64 │ ╞═════╪═════╪═══════════╡ │ 45 ┆ 77 ┆ 0.0 │ │ 76 ┆ 11 ┆ 0.0 │ │ 4 ┆ 56 ┆ 26.832826 │ │ 88 ┆ 43 ┆ 23.440663 │ │ … ┆ … ┆ … │ │ 93 ┆ 66 ┆ 28.72105 │ │ 87 ┆ 17 ┆ 28.981351 │ │ 23 ┆ 64 ┆ 29.063269 │ │ 40 ┆ 75 ┆ 22.362099 │ └─────┴─────┴───────────┘
Question: I'm writing a script with the Pandas library that involves reading the contents of an excel file. The line currently looks like this: test = pd.read_excel(archive_contents['spreadsheet.xlsx'])  The script works as intended with no issues, but I get a future warning depicting the following: FutureWarning: Passing bytes to 'read_excel' is deprecated and will be removed in a future version. To read from a byte string, wrap it in a `BytesIO` object. test = pd.read_excel(archive_contents['spreadsheet.xlsx'])  In the interest of future proofing my code, how would I go about doing that?

Answer: As user459827 has commented, this will do the trick: from io import BytesIO test = pd.read_excel(BytesIO(archive_contents['spreadsheet.xlsx']))
Question: I am working on a React frontend to chart some data from a fastapi backend. I am using a couple of dropdown components to change the month and year for the requested data. With the initial render the fetch request works fine and returns the data and the charts display. Once I change the dropdowns, I get the following CORS Policy Error in the browser console.  Access to fetch at 'https://fake-url.com/endpoint/' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.  React code snippet with the fetch call:  const [month, setMonth] = useState(1); const [year, setYear] = useState('2023'); const [revenueData, setRevenueData] = useState({}); useEffect(() => { const inputs = { "month": month, "year": year, "duration": 1 } const myHeaders = new Headers(); myHeaders.append("X-API-KEY", "fake-api-key"); myHeaders.append("Content-Type", "application/json"); const requestOptions = { method: 'POST', headers: myHeaders, body: JSON.stringify(inputs), redirect: 'follow' }; fetch("https://fake-url.com/endpoint/", requestOptions) .then(response => response.json()) .then(data => { setRevenueData((data)) }).catch(error => { console.log('error', error) }); }, [month, year]);  I confirmed that I am using CORSMiddleware in fastapi with the following settings: app.add_middleware(HTTPSRedirectMiddleware) app.add_middleware( CORSMiddleware, allow_origins=['*'], allow_methods=['*'], allow_headers=['*'] )  I also confirmed that the backend is returning access-control headers for preflight with an options request in postman as shown:  UPDATE The network panel shows that the second request preflight is successful but ultimately fails in an Internal Server Error. Which lead me to: CORS and Internal Server Error responses 

Answer: CORS headers are not added when the request ends in an error, that is, when a response is returned with a status code such as 4xx or 5xx. As shown in the screenshot you provided, when calling the /dashboard_data API endpoint for the third time, the server responds with 500 Internal Server Error response code, indicating that the server encountered an unexpected condition that prevented it from fulfilling the request. Hence, in that case, the server won't include the appropriate Access-Control-Allow-Headers / Access-Control-Allow-Origin headers, and the browser will report a CORS error in the devtools console. Thus, please check what causes the error on server side in the first place and try to fix it (the console running the server should give you insights on that). Related answers to the concept of CORS issues that might prove helpful to you and future readers can be found here, as well as here and here.
Question: I want to use OpenTelemetry with an Async application, and I want to be 101% sure that it will work as intended. Specifically, I'm worried about what happens with the current_span when we switch back and forth between asynchronous functions. I have this fear that if I rely on tracer.start_as_current_span to set the span in each function, and then I pass execution to another function which also sets the current_span, then when execution passes back to the first function it won't be tied to the correct span anymore. Now, I have tried testing this a bit, and found no evidence that it breaks. But I also haven't found any documentation that says it explicitly won't break. Can anyone confirm? I've done the following basic test, but I'm worried it misses something: async def async_span(): with tracer.start_as_current_span(name=f"span_{uuid.uuid4}") as span: for x in range(1000): assert trace.get_current_span() == span await asyncio.sleep(0.0001 * randint(1, 10)) async def main(): with tracer.start_as_current_span(name="parent"): await asyncio.gather(*(async_span() for _ in range(10))) 

Answer: OpenTelemetry for Python supports asynchronous code. Went through the code for version 1.24.0/0.45b0 of opentelemetry-python. The code contains abstract context class _RuntimeContext. _RuntimeContext has single implementation ContextVarsRuntimeContext that utilizes contextvars. ContextVarsRuntimeContext is used as a default context.
Question: The parameter usedforsecurity was added to every hash function in hashlib in Python 3.9.  Changed in version 3.9: All hashlib constructors take a keyword-only argument usedforsecurity with default value True. A false value allows the use of insecure and blocked hashing algorithms in restricted environments. False indicates that the hashing algorithm is not used in a security context, e.g. as a non-cryptographic one-way compression function.  However, this provides zero guidance on  When you should use usedforsecurity When you should not use usedforsecurity What "restricted environments" are  And while I'm not a security researcher, I darn well know md5 is not secure in any sense of the word. Consequently, the name usedforsecurity boggles my mind in more ways than one. What is the point of usedforsecurity?

Answer: TL;DR For almost everyone, ignore the flag, it has no effect whatsoever.  The full story involves FIPS and how that gets exposed as a python API. For our purposes, FIPS is a standard that supposedly specifies a safe set of practices. In certain scenarios (e.g. writing software for US government agencies), you are forced to comply with FIPS. To comply with FIPS, your python would have had FIPS mode turned on by building python with FIPS enabled from source. This is the "restricted environment" mentioned in the documentation. If you have a standard python build, then you aren't complying with FIPS and the flag literally does nothing. One aspect of FIPS is restricting the hash functions you are allowed to use. In particular, MD5 is not allowed under FIPS. When you use MD5 in a FIPS environment, you will encounter an error. That is what the introduction of usedforsecurity is supposed to fix: give you an escape hatch in the case that you truly want to use MD5 in a FIPS environment. The parameter is designed to be specified at each call site so it can be audited on a case by case basis. There seems to be confusion on many sides that usedforsecurity has anything to do with security. It's not. Having it set to False doesn't reduce your security. On regular python builds, you use the exact same hash function regardless of usedforsecurity. On FIPS enabled environments, it does however switch your implementation of (allowed) hash functions between those that were explicitly certified† or not. In conclusion, for all intents and purposes, the parameter might as well have been called exceptionforfips because that's the singular purpose it serves: as an escape hatch if you happen to work under a FIPS environment and still need to use a FIPS non-compliant hash. It is quite unfortunate it is part of the API for all users with a seriously misleading name. † However, the certified version doesn't use a different algorithm, certification is very much bureaucratic in nature.
Question: I try to load a dataset using the datasets python module in my local Python Notebook. I am running a Python 3.10.13 kernel as I do for my virtual environment. I cannot load the datasets I am following from a tutorial. Here's the error: --------------------------------------------------------------------------- NotImplementedError Traceback (most recent call last) /Users/ari/Downloads/00-fine-tuning.ipynb Celda 2 line 3 1 from datasets import load_dataset ----> 3 data = load_dataset( 4 "jamescalam/agent-conversations-retrieval-tool", 5 split="train" 6 ) 7 data File ~/Documents/fastapi_language_tutor/env/lib/python3.10/site-packages/datasets/load.py:2149, in load_dataset(path, name, data_dir, data_files, split, cache_dir, features, download_config, download_mode, verification_mode, ignore_verifications, keep_in_memory, save_infos, revision, token, use_auth_token, task, streaming, num_proc, storage_options, **config_kwargs) 2145 # Build dataset for splits 2146 keep_in_memory = ( 2147 keep_in_memory if keep_in_memory is not None else is_small_dataset(builder_instance.info.dataset_size) 2148 ) -> 2149 ds = builder_instance.as_dataset(split=split, verification_mode=verification_mode, in_memory=keep_in_memory) 2150 # Rename and cast features to match task schema 2151 if task is not None: 2152 # To avoid issuing the same warning twice File ~/Documents/fastapi_language_tutor/env/lib/python3.10/site-packages/datasets/builder.py:1173, in DatasetBuilder.as_dataset(self, split, run_post_process, verification_mode, ignore_verifications, in_memory) 1171 is_local = not is_remote_filesystem(self._fs) 1172 if not is_local: -> 1173 raise NotImplementedError(f"Loading a dataset cached in a {type(self._fs).__name__} is not supported.") 1174 if not os.path.exists(self._output_dir): 1175 raise FileNotFoundError( 1176 f"Dataset {self.dataset_name}: could not find data in {self._output_dir}. Please make sure to call " 1177 "builder.download_and_prepare(), or use " 1178 "datasets.load_dataset() before trying to access the Dataset object." 1179 ) NotImplementedError: Loading a dataset cached in a LocalFileSystem is not supported.  How do I resolve this? I don't understand how this error is applicable, given that the dataset is something I am fetching and thus cannot be cached in my LocalFileSystem in the first place.

Answer: Try doing: pip install -U datasets   This error stems from a breaking change in fsspec. It has been fixed in the latest datasets release (2.14.6). Updating the installation with pip install -U datasets should fix the issue.  git link : https://github.com/huggingface/datasets/issues/6352  If you are using fsspec, then do: pip install fsspec==2023.9.2  There is a problem with fsspec==2023.10.0 git link : https://github.com/huggingface/datasets/issues/6330   Edit: Looks like it broken again in 2.17 and 2.18 downgrading to 2.16 should work.
Question: I want to change the text for an item variably. to do this, i tried to update the menu using update_menu(). unfortunately, this didn't work and i couldn't find anything more detailed in the pystray documentation. I hope you can help me. thank you. from pystray import Icon as icon, Menu as menu, MenuItem as item import PIL.Image image = PIL.Image.new('RGB', (100, 100), 255) adapter = 'before' def test(): global ps global adapter adapter = 'after' ps.update_menu() ps = icon(name='name', icon=image, menu=menu( item(text='Adapter', action=menu(item(text='Test', action=test))), item(text=adapter, action=lambda: test()), ) ) ps.run() 

Answer: I've got to a similar problem earlier, I wanted to update the submenu while running. I made a version for what you wanted to adjust: def test(icon, this_item): global adapter adapter = 'after' global menu_items menu_items.pop() # remove last element, here containing 'adapter' # add new item with updated adapter value menu_items.append(item(text=adapter, action=test)) icon.update_menu() menu_items = [ item(text='Adapter', action=menu(item(text='Test', action=test))), item(text=adapter, action=test) ] ps = icon(name='name', icon=image, menu=menu(lambda: menu_items)) ps.run()  The MenuItem takes (*items) so giving it a list with items in a lambda function works to update it later. You have to alter the menu_items list to your needs and update. Couldn't figure out tho how to just adjust the text, with a lambda e.g. either. Found also this example #17, they changed the whole menu each time, but well...  UPDATE: Here's what you wanted, cleaner than my previous answer: adapter = 'before' def test(icon, this_item): global adapter adapter = 'after' icon.update_menu() ps = icon(name='name', icon=image, menu=menu( item(text='Adapter', action=menu(item(text='Test', action=test))), item(lambda text: adapter, action=test) ) )  found here
Question: I am trying to trigger a GCP Cloud Run job from a python script following the run_job documentation (https://cloud.google.com/python/docs/reference/run/latest/google.cloud.run_v2.services.jobs.JobsClient#google_cloud_run_v2_services_jobs_JobsClient_run_job). However, I'm getting errors that I haven't been able to debug. This is a job that already exists, but I need to overwrite an environment variable. Here's is my code: env_vars = [ run_v2.EnvVar(name="VAR_1", value="var_1_value"), run_v2.EnvVar(name="VAR_2", value="var_2_value"), ] # Set the env vars as container overrides container_overrides = run_v2.RunJobRequest.Overrides.ContainerOverride( name="myjobname", env=env_vars ) request_override = run_v2.RunJobRequest.Overrides( container_overrides=container_overrides ) # Initialize the request job_name = f"projects/myproject/locations/mylocation/jobs/myjob" request = run_v2.RunJobRequest( name=job_name, overrides=request_override ) # Make the request operation = client.run_job(request=request) logging.info("Waiting for operation to complete...") response = operation.result() logging.info(f"Operation result: {response}")  And this is the error I'm getting: Traceback (most recent call last): File "/opt/python3.8/lib/python3.8/site-packages/airflow/decorators/base.py", line 220, in execute return_value = super().execute(context) File "/opt/python3.8/lib/python3.8/site-packages/airflow/operators/python.py", line 181, in execute return_value = self.execute_callable() File "/opt/python3.8/lib/python3.8/site-packages/airflow/operators/python.py", line 198, in execute_callable return self.python_callable(*self.op_args, **self.op_kwargs) File "/home/airflow/gcs/dags/etl.py", line 128, in run_acolite request = run_v2.RunJobRequest( File "/opt/python3.8/lib/python3.8/site-packages/proto/message.py", line 604, in __init__ super().__setattr__("_pb", self._meta.pb(**params)) TypeError: Message must be initialized with a dict: google.cloud.run.v2.RunJobRequest  Thank you!

Answer: We recently started using the Cloud Run Jobs service in my workplace and I found myself needing to carry out the same task. A dictionary with the Override Specification is required. I've amended the Initialize request block as per your example. override_spec = { 'container_overrides': [ { 'env': [ {'name': 'VAR_1', 'value': 'var_1_value'} ] } ] } # Initialize the request job_name = f"projects/myproject/locations/mylocation/jobs/myjob" request = run_v2.RunJobRequest( name=job_name, overrides=override_spec )  Ref: https://cloud.google.com/python/docs/reference/run/latest/google.cloud.run_v2.types.RunJobRequest.Overrides
Question: Python's standard library is vast, and my intuition tells that there must be a way in it to accomplish this, but I just can't figure it out. This is purely for curiosity and learning purposes: I have two simple functions: def increment(x): return x + 1 def double(x): return x * 2  and I want to compose them into a new function double_and_increment. I could of course simply do that as such: double_and_increment = lambda x: increment(double(x))  but I could also do it in a more convoluted but perhaps more "ergonomically scalable" way: import functools double_and_increment = functools.partial(functools.reduce, lambda acc, f: f(acc), [double, increment])  Both of the above work fine: >>> double_and_increment(1) 3  Now, the question is, is there tooling in the standard library that would allow achieving the composition without any user-defined lambdas, regular functions, or classes. The first intuition is to replace the lambda acc, f: f(acc) definition in the functools.reduce call with operator.call, but that unfortunately takes the arguments in the reverse order: >>> (lambda acc, f: f(acc))(1, str) # What we want to replace. >>> '1' >>> import operator >>> operator.call(str, 1) # Incorrect argument order. >>> '1'  I have a hunch that using functools.reduce is still the way to accomplish the composition, but for the life of me I can't figure out a way to get rid of the user-defined lambda. Few out-of-the-box methods that got me close: import functools, operator # Curried form, can't figure out how to uncurry. functools.partial(operator.methodcaller, '__call__')(1)(str) # The arguments needs to be in the middle of the expression, which does not work. operator.call(*reversed(operator.attrgetter('args')(functools.partial(functools.partial, operator.call)(1, str))))  Have looked through all the existing questions, but they are completely different and rely on using user-defined functions and/or lambdas.

Answer: As mentioned in the other answer of mine I don't agree that the test suite discovered by @AKX should be considered as part of the standard library per the OP's rules. As it turns out, while researching for an existing function to modify for my other answer, I found that there is this helper function _int_to_enum in the signal module that perfectly implements operator.call for a callable with a single argument, but with parameters reversed, exactly how the OP wants it, and is available since Python 3.5: def _int_to_enum(value, enum_klass): """Convert a numeric value to an IntEnum member. If it's not a known member, return the numeric value itself. """ try: return enum_klass(value) except ValueError: return value  So we can simply repurpose/abuse it: from signal import _int_to_enum as rcall from functools import reduce, partial def increment(x): return x + 1 def double(x): return x * 2 double_and_increment = partial(reduce, rcall, [double, increment]) print(double_and_increment(1))  This outputs: 3  Demo: here