eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I am having problems trying to run TensorFlow on my Windows 10 machine. Code runs fine on my MacOS machine. Traceback (most recent call last): File "c:\Users\Fynn\Documents\GitHub\AlpacaTradingBot\ai.py", line 15, in <module> from keras.models import Sequential, load_model File "C:\Users\Fynn\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\__init__.py", line 24, in <module> from keras import models File "C:\Users\Fynn\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\models\__init__.py", line 18, in <module> from keras.engine.functional import Functional File "C:\Users\Fynn\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\functional.py", line 24, in <module> from keras.dtensor import layout_map as layout_map_lib File "C:\Users\Fynn\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\dtensor\__init__.py", line 22, in <module> from tensorflow.compat.v2.experimental import dtensor as dtensor_api # pylint: disable=g-import-not-at-top ImportError: cannot import name 'dtensor' from 'tensorflow.compat.v2.experimental' (C:\Users\Fynn\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\_api\v2\compat\v2\experimental\__init__.py) 

Answer: I tried many solutions to no avail, in the end this worked for me! pip3 uninstall tensorflow absl-py astunparse flatbuffers gast google-pasta grpcio h5py keras keras-preprocessing libclang numpy opt-einsum protobuf setuptools six tensorboard tensorflow-io-gcs-filesystem termcolor tf-estimator-nightly typing-extensions wrapt  pip3 install --disable-pip-version-check --no-cache-dir tensorflow
Question: I have a GET method with requested parameter in path: @router.get('/users/{user_id}') async def get_user_from_string(user_id: str): return User(user_id)  Is it possible to get base url raw path (i.e., '/users/{user_id}') from the request? I have tried to use the following way: path = [route for route in request.scope['router'].routes if route.endpoint == request.scope['endpoint']][0].path  But it doesn't work and I get:  AttributeError: 'Mount' object has no attribute 'endpoint' 

Answer: You can use the APIRout object property in the request to get the actual path example: raw_path = request.scope['route'].path #'/user/{id}'
Question: I am currently working on adding type hints to a project and can't figure out how to get this right. I have a list of lists, with the nested list containing two elements of type int and float. The first element of the nested list is always an int and the second is always a float. my_list = [[1000, 5.5], [1432, 2.2], [1234, 0.3]]  I would like to type annotate it so that unpacking the inner list in for loops or loop comprehensions keeps the type information. I could change the inner lists to tuples and would get what I'm looking for: def some_function(list_arg: list[tuple[int, float]]): pass  However, I need the inner lists to be mutable. Is there a nice way to do this for lists? I know that abstract classes like Sequence and Collection do not support multiple types.

Answer: I think the question highlights a fundamental difference between statically typed Python and dynamically typed Python. For someone who is used to dynamically typed Python (or Perl or JavaScript or any number of other scripting languages), it's perfectly normal to have diverse data types in a list. It's convenient, flexible, and doesn't require you to define custom data types. However, when you introduce static typing, you step into a tighter box that requires more rigorous design. As several others have already pointed out, type annotations for lists require all elements of the list to be the same type, and don't allow you to specify a length. Rather than viewing this as a shortcoming of the type system, you should consider that the flaw is in your own design. What you are really looking for is a class with two data members. The first data member is named 0, and has type int, and the second is named 1, and has type float. As your friend, I would recommend that you define a proper class, with meaningful names for these data members. As I'm not sure what your data type represents, I'll make up names, for illustration. class Sample: def __init__(self, atomCount: int, atomicMass: float): self.atomCount = atomCount self.atomicMass = atomicMass  This not only solves the typing problem, but also gives a major boost to readability. Your code would now look more like this: my_list = [Sample(1000, 5.5), Sample(1432, 2.2), Sample(1234, 0.3)] def some_function(list_arg: list[Sample]): pass  I do think it's worth highlighting Stef's comment, which points to this question. The answers given highlight two useful features related to this. First, as of Python 3.7, you can mark a class as a data class, which will automatically generate methods like __init__(). The Sample class would look like this, using the @dataclass decorator: from dataclasses import dataclass @dataclass class Sample: atomCount: int atomicMass: float  Another answer to that question mentions a PyPi package called recordclass, which it says is basically a mutable namedtuple. The typed version is called RecordClass from recordclass import RecordClass class Sample(RecordClass): atomCount: int atomicMass: float
Question: I have a conda environment that I would like to convert to a poetry environment. What I have tried is to translate the environment.yaml of the conda environment into a pyproject.toml file that poetry can read. Here you have the steps:  Generate the yaml file conda env export --from-history > environment.yaml The --from-history flag includes only the packages that I explicitly asked for. Here it is how the file looks like after installing numpy. # environment.yaml name: C:\Users\EDOCIC\Screepts\My_projects\Tests\conda2poetry\condaenv channels: - defaults dependencies: - numpy   Manually create the pyproject.toml file out of environment.yaml. I added the numpy version, which I got from conda env export. Here it is the result: # pyproject.toml [tool.poetry] name = "conda2poetry" version = "0.1.0" description = "" authors = [""] [tool.poetry.dependencies] python = "~3.7" numpy = "^1.21.5" [tool.poetry.dev-dependencies] [build-system] requires = ["poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api"   Create the environment with poetry init, which will automatically read the toml file.   The process seems to work but it's quite manual and prone to mistakes. Is there a better way?

Answer: No, there is not a better way. Conda is a generic package manager and does not discern Python versus non-Python packages, therefore this has to be done with manual curation. Additionally, package names might also differ. For example py-opencv(conda-forge) vs opencv-python (PyPi). Tips In addition to pulling down the --from-history YAML, it may also help to dump out a pip list --format=freeze. This could help with resolving any tricky packages that use different names in Conda versus PyPI. If the environment uses any PyPI packages directly, this won't be seen from a conda env export --from-history. However, these will appear when using conda list (entries with channel pypi) or plain conda env export, which would have a dependencies.pip: section if there are any.
Question: I compiled my program with pyinstaller, and it works fine on my computer, but whenever I ty to run it in another computer (with no python), I get the following error: Error loading Python DLL 'C:\Users\perez\AppData\Local\Temp\_MEI28162\python310.dll'. LoadLibrary: Cannot find specified module  What can I do? I'm not allowed to install python on the other computer

Answer: Ok, it was not working because I compiled the script with pyinstaller having python 3.10, but Windows 7's maximum python version is 3.8
Question: I am using jupyter notebook. I have 3 jupyter source files written in python in a folder in the same directory: parser, preprocess, and temp. I am trying to import parser and import preprocess in the temp file so that I can use the methods written in those files. Example: there is method named extract in parser file. I want to use that from temp file. How can I do that?

Answer: The easiest way is to change the files you need to import as py files. As an example, parser.ipynb can be converted to a python file parser.py, and you can import it from another notebook file. If you want to use a function named extract() from parser.py, just import from parser import extract
Question: In the HuggingFace tokenizer, applying the max_length argument specifies the length of the tokenized text. I believe it truncates the sequence to max_length-2 (if truncation=True) by cutting the excess tokens from the right. For the purposes of utterance classification, I need to cut the excess tokens from the left, i.e. the start of the sequence in order to preserve the last tokens. How can I do that? from transformers import AutoTokenizer train_texts = ['text 1', ...] tokenizer = AutoTokenizer.from_pretrained('xlm-roberta-base') encodings = tokenizer(train_texts, max_length=128, truncation=True) 

Answer: Tokenizers have a truncation_side parameter that should set exactly this. See the docs.
Question: While the artifact repository was successfully creating, running a docker push to push the image to the google artifact registry fails with a permissions error even after granting all artifact permissions to the accounting I am using on gcloud cli. Command used to push image: docker push us-central1-docker.pkg.dev/project-id/repo-name:v2  Error message: The push refers to repository [us-central1-docker.pkg.dev/project-id/repo-name] 6f6f4a472f31: Preparing bc096d7549c4: Preparing 5f70bf18a086: Preparing 20bed28d4def: Preparing 2a3255c6d9fb: Preparing 3f5d38b4936d: Waiting 7be8268e2fb0: Waiting b889a93a79dd: Waiting 9d4550089a93: Waiting a7934564e6b9: Waiting 1b7cceb6a07c: Waiting b274e8788e0c: Waiting 78658088978a: Waiting denied: Permission "artifactregistry.repositories.downloadArtifacts" denied on resource "projects/project-id/locations/us-central1/repositories/repo-name" (or it may not exist) 

Answer: I was able to recreate your use case. This happens when you are trying to push an image on a repository in which its specific hostname (associated with it's repository location) is not yet added to the credential helper configuration for authentication. You may refer to this Setting up authentication for Docker  as also provided by @DazWilkin in the comments for more details. In my example, I was trying to push an image on a repository that has a location of us-east1 and got the same error since it is not yet added to the credential helper configuration.  And after I ran the authentication using below command (specifically for us-east1 since it is the location of my repository), the image was successfully pushed: gcloud auth configure-docker us-east1-docker.pkg.dev   QUICK TIP: You may get your authentication command specific for your repository when you open your desired repository in the console, and then click on the SETUP INSTRUCTIONS.
Question: I need to add gettext translation to all the string literals in our code, but it doesn't work with literals in case statements. This failed attempt gives SyntaxError: Expected ':': from gettext import gettext as _ direction = input(_('Enter a direction: ')) # <-- This works match direction: case _('north'): # <-- This fails adj = 1, 0 case _('south'): adj = -1, 0 case _('east'): adj = 0, 1 case _('west'): adj = 0, -1 case _: raise ValueError(_('Unknown direction'))  What does the error mean and how can the directions be marked for translation?

Answer: What does the error mean? The grammar for the match/case statement treats the _ as a wildcard pattern. The only acceptable token that can follow is a colon. Since your code uses an open parenthesis, a SyntaxError is raised. How to fix it Switch from a literal pattern such as case "north": ... to a value pattern such as case Directions.north: ... which uses dot-operator. The translation can then be performed upstream, outside of the case statement: from gettext import gettext as _ class Directions: north = _('north') south = _('south') east = _('east') west = _('west') direction = input(_('Enter a direction: ')) match direction: case Directions.north: adj = 1, 0 case Directions.south: adj = -1, 0 case Directions.east: adj = 0, 1 case Directions.west: adj = 0, -1 case _: raise ValueError(_('Unknown direction'))  Not only do the string literals get translated, the case statements are more readable as well. More advanced and dynamic solution The above solution only works if the choice of language is constant. If the language can change (perhaps in an online application serving users from difference countries), dynamic lookups are needed. First we need a descriptor to dynamically forward value pattern attribute lookups to function calls: class FuncCall: "Descriptor to convert fc.name to func(name)." def __init__(self, func): self.func = func def __set_name__(self, owner, name): self.name = name def __get__(self, obj, objtype=None): return self.func(self.name)  We use it like this: class Directions: north = FuncCall(_) # calls _('north') for every lookup south = FuncCall(_) east = FuncCall(_) west = FuncCall(_) def convert(direction): match direction: case Directions.north: return 1, 0 case Directions.south: return -1, 0 case Directions.east: return 0, 1 case Directions.west: return 0, -1 case _: raise ValueError(_('Unknown direction')) print('Adjustment:', adj)  Here is a sample session: >>> set_language('es') # Spanish >>> convert('sur') (-1, 0) >>> set_language('fr') # French >>> convert('nord') (1, 0)  Namespaces for the Value Pattern Any namespace with dotted lookup can be used in the value pattern: SimpleNamespace, Enum, modules, classes, instances, etc. Here a class was chosen because it is simple and will work with the descriptor needed for the more advanced solution. Enum wasn't considered because it is much more complex and because its metaclass logic interferes with the descriptors. Also, Enum is intended for giving symbolic names to predefined constants rather than for dynamically computed values like we're using here.
Question: My application is going to edit a bunch of large files, completely unrelated to each other (belonging to different users), and I need to store checkpoints of the previous state of the files. Delta compression should work extremely well on this file format. I only need a linear history, not branches or merges. There are low-level libraries that give part of the solution, for example xdelta3 sounds like a good binary diff/patch system. RCS actually seems like a pretty close match to my problem, but doesn't handle binary files well. git provides a complete solution to my problem, but is an enormous suite of programs, and its storage format is an entire directory. Is there anything less complicated than git that would:  work on binary files perform delta compression let me commit new "newest" versions let me recall old versions  Bonus points if it would:  have a single-file storage format be available as a C, C++, or Python library  I can't even find the right combination of words to google for this category of program, so that would also be helpful.

Answer: From RCS manual (1. Overview)  [RCS] can handle text as well as binary files, although functionality is reduced for the latter.  RCS seems a good option worth to try. I work for a Foundation which has been using RCS to keep under version control tens of thousands of completely unrelated files (git or hg are not an option). Mostly text, but also some media files, which are binary in nature. RCS does work quite well with binary files, only make sure not to use the Substitute mode options, to avoid inadvertently substituting binary bits that looks like $ Id. To see if this could work for you, you could for example try with a Photoshop image, put it under version control with RCS. Then change a part, or add a layer, and commit the change. You could then verify how well RCS can manage binary files for you. RCS has been serving us quite well. It is well maintained, reliable, predictable, and definitely worth a try.
Question: While trying to download the "Cats_vs_Dogs" TensorFlow dataset using the tfds module, I get the following error 👇 DownloadError Traceback (most recent call last) <ipython-input-2-244305a07c33> in <module>() 7 split=['train[:80%]', 'train[80%:90%]', 'train[90%:]'], 8 with_info=True, ----> 9 as_supervised=True, 10 ) 21 frames /usr/local/lib/python3.7/dist-packages/tensorflow_datasets/core/download/downloader.py in _assert_status(response) 257 if response.status_code != 200: 258 raise DownloadError('Failed to get url {}. HTTP code: {}.'.format( --> 259 response.url, response.status_code)) DownloadError: Failed to get url https://download.microsoft.com/download/3/E/1/3E1C3F21-ECDB-4869-8368-6DEBA77B919F/kagglecatsanddogs_3367a.zip. HTTP code: 404.  The code I have used is import tensorflow_datasets as tfds tfds.disable_progress_bar() # split the data manually into 80% training, 10% testing, 10% validation (raw_train, raw_validation, raw_test), metadata = tfds.load( 'cats_vs_dogs', split=['train[:80%]', 'train[80%:90%]', 'train[90%:]'], with_info=True, as_supervised=True, )  It worked yesterday but suddenly gave an error today.......

Answer: You can add this before loading to set the new URL : setattr(tfds.image_classification.cats_vs_dogs, '_URL',"https://download.microsoft.com/download/3/E/1/3E1C3F21-ECDB-4869-8368-6DEBA77B919F/kagglecatsanddogs_5340.zip")
Question: I am new to databricks and delta live tables. I have problem with creating delta live table in python.    How to create delta live table from json files in filestore?

Answer: It is a decorator, so I think you also need a function after. Meaning @dlt.table(comment="your comment") def get_bronze(): df=spark.sql("""select * from myDb.MyRegisterdTable""") #If you wanna check logs: #print("bronze",df.take(5),"end") return df  In silver function then you can read it as: @dlt.table def get_silver(): df = dlt.read("get_bronze") [..do_stuff...] return df  Also from your screenshots I am not sure, are you running all this as a pipeline or are you trying to run a Notebook? The latter does not work.
Question: I have a grpc server (in Go) that has a valid TLS certificate and does not require client side TLS. For some reason I can not implement the client without mTLS in Python, even though I can do so in Golang. In Python I have os.environ["GRPC_VERBOSITY"] = "DEBUG" # os.environ["GRPC_DEFAULT_SSL_ROOTS_FILE_PATH"] = "/etc/ssl/certs/ca-bundle.crt" channel = grpc.secure_channel(ADDR, grpc.ssl_channel_credentials()) grpc.channel_ready_future(channel).result(timeout=10)  This gives me the following error D0513 08:02:08.147319164 21092 security_handshaker.cc:181] Security handshake failed: {"created":"@1652446928.147311309","description":"Handshake failed","file":"src/core/lib/security/transport/security_handshaker.cc","file_line":377,"tsi_code":10,"tsi_error":"TSI_PROTOCOL_FAILURE"}  I can get this to work if I use SSL certificates by uncommenting the commented out line. I know for a fact that my server does not request, require or verify client certificates as The following Go code work perfectly conn, err := grpc.DialContext( ctx, gRPCAddr, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")), ) dummyClient := dummy.NewDummyServiceClient(conn) if _, err := dummyClient.Ping(context.Background(), &dummy.PingRequest{ Ping: "go client ping", }); err != nil { return fmt.Errorf("failed to ping: %w", err) } 

Answer: https://grpc.github.io/grpc/python/_modules/grpc.html#secure_channel has the docs for channel = grpc.secure_channel(ORBIUM_ADDR, grpc.ssl_channel_credentials()). This function relies on the class channel, see docs https://grpc.github.io/grpc/python/_modules/grpc/aio/_channel.html. Basically, class Channel wraps C code to provide a secure channel. That wrapped C code expects the certificate. If you can implement in C, it might be easiest to just change the C code.
Question: I'd like to run pytest and then store results and present them to users on demand (e.g. store pytest results to a db and then expose them through web service). I could run pytest from the command line with an option to save the results report into file, then find and parse the file, but it feels silly to have the results in a (pytest) python app, then store them to a file and then instantly look for the file, and parse it back into python code for further processing. I know I can run pytest programmatically via pytest.main(args), however, it only returns some exit code, and not details about test results. How can I retrieve the results when using pytest.main()? I'm looking for something like: args = ARGUMENTS ret_code = pytest.main(args=args) # pytest.main() as is only returns trivial return code my_own_method_to_process(pytest.results) # how to retrieve any kind of pytest.results object that would contain test execution results data (list of executed tests, pass fail info, etc as pytest is displaying into console or saves into file reports)  There are couple of similar questions, but always with some deviation that doesn't work for me. I simply want to run pytest from my code, and - whatever format the output would be - directly grab it and further process it. Note: I'm in a corporate environment, where installing new packages (i.e. pytest plugins) is limited, so I'd like to achieve this without installing any other module/pytest plugin into my environment.

Answer: Write a small plugin that collects and stores reports for each test. Example: import time import pytest class ResultsCollector: def __init__(self): self.reports = [] self.collected = 0 self.exitcode = 0 self.passed = 0 self.failed = 0 self.xfailed = 0 self.skipped = 0 self.total_duration = 0 @pytest.hookimpl(hookwrapper=True) def pytest_runtest_makereport(self, item, call): outcome = yield report = outcome.get_result() if report.when == 'call': self.reports.append(report) def pytest_collection_modifyitems(self, items): self.collected = len(items) def pytest_terminal_summary(self, terminalreporter, exitstatus): print(exitstatus, dir(exitstatus)) self.exitcode = exitstatus.value self.passed = len(terminalreporter.stats.get('passed', [])) self.failed = len(terminalreporter.stats.get('failed', [])) self.xfailed = len(terminalreporter.stats.get('xfailed', [])) self.skipped = len(terminalreporter.stats.get('skipped', [])) self.total_duration = time.time() - terminalreporter._sessionstarttime def run(): collector = ResultsCollector() pytest.main(plugins=[collector]) for report in collector.reports: print('id:', report.nodeid, 'outcome:', report.outcome) # etc print('exit code:', collector.exitcode) print('passed:', collector.passed, 'failed:', collector.failed, 'xfailed:', collector.xfailed, 'skipped:', collector.skipped) print('total duration:', collector.total_duration) if __name__ == '__main__': run()