eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I have been wondering this for a while now, and I hope this isn't a stupid question with an obvious answer I'm not realizing: Why can't I just call the __init__ method of super() like super()()? I have to call the method like this instead: super().__init__() Here is an example that gets a TypeError: 'super' object is not callable error when I run it (specifically on line 6 that comes from line 3 in __init__): class My_Int(int): def __init__(self,value,extr_data=None): super()(value) self.extr_data=extr_data x=My_Int(3)  Doesn't super() get the inherited class int making super()(value) the same as int(value)? Furthermore, why can't I use the len function with super() when inheriting from the class list? Doesn't len() do the same as __len__()? class My_List(list): def some_method1(self): print(len(super())) def some_method2(self): print(super().__len__()) x=My_List((1,2,3,4)) x.some_method2() x.some_method1()  This example prints 4 and then an error as expected. Here is the output exactly: 4 Traceback (most recent call last): File "/home/user/test.py", line 11, in <module> x.some_method1() File "/home/user/test.py", line 3, in some_method1 print(len(super())) TypeError: object of type 'super' has no len()  Notice I called some_method2 before calling some_method1 (sorry for the confusion). Am I missing something obvious here?  P.S. Thanks for all the help! 

Answer: super() objects can't intercept most special method calls, because they bypass the instance and look up the method on the type directly, and they don't want to implement all the special methods when many of them won't apply for any given usage. This case gets weirder, super()() would try to lookup a __call__ method on the super type itself, and pass it the super instance. They don't do this because it's ambiguous, and not particularly explicit. Does super()() mean invoke the super class's __init__? Its __call__? What if we're in a __new__ method, do you invoke __new__, __init__ or both? Does this mean all super uses must implicitly know which method they're called in (even more magical than knowing the class they were defined in and the self passed when constructed with zero arguments)? Rather than deal with all this, and to avoid implementing all the special methods on super just so it can delegate them if they exist on the instance in question, they required you to explicitly specify the special method you intend to call.
Question: i go first straight for my questions: Why would one rather use engine.connect() instead of engine.begin(), if the second is more reliable? Then, why is it still on the tutorial page of SQLAlchemy and everywhere in stackoverflow? Performance? Why does engine.connect() work so inconsistently? Is the problem withing the autocommit? My backgroundstory to this is, that i just resolved an issue. Normal SQL-queries like SELECT, CREATE TABLE and DELETE would work flawlessly when using engine.connect(). Though, using MERGE would work very inconsistently. Sometimes committing, sometimes blocking other queries, sometimes nothing. Here it is recommended to use engine.begin() for MERGE queries. So i substituted the following code: with engine.connect() as connection: connection.execute('MERGE Table1 USING Table2 ON .....')  by with engine.begin() as connection: connection.execute('MERGE Table1 USING Table2 ON .....')  and now everything works perfectly. Inlcuding the queries of SELECT,CREATE TABLE and DELETE. In the SQLAlchemy docs it says the second option uses transactions with a transaction-commit, but the scope of with engine.connect() does an autocommit aswell. Sorry i am a complete newbie to SQL.

Answer:  the scope of with engine.connect() does an autocommit as well  No, it doesn't. That's the most striking difference between with engine.connect() and with engine.begin() with engine.connect() as conn: # do stuff # on exit, the transaction is automatically rolled back with engine.begin() as conn: # do stuff # on exit, the transaction is automatically committed if no errors occurred  As mentioned in the tutorial, engine.connect() is used with the "[explicitly] commit as you go" style of code, while engine.begin() represents the "begin once" style. Transactions are used in both cases. However, engine.begin() begins the transaction immediately, while engine.connect() waits until a statement is executed before beginning the transaction. That permits us to alter the characteristics of the transaction that will eventually be started. A common use of this engine.connect() feature is to use non-default transaction isolation: # default isolation level with engine.connect() as conn: print(conn.get_isolation_level()) # REPEATABLE READ # using another isolation level with engine.connect().execution_options( isolation_level="SERIALIZABLE" ) as conn: print(conn.get_isolation_level()) # SERIALIZABLE
Question: In a program I am writing in python I need to completely restart the program if a variable becomes true, looking for a while I found this command: while True: if reboot == True: os.execv(sys.argv[0], sys.argv)  When executed it returns the error [Errno 8] Exec format error. I searched for further documentation on os.execv, but didn't find anything relevant, so my question is if anyone knows what I did wrong or knows a better way to restart a script (by restarting I mean completely re-running the script, as if it were been opened for the first time, so with all unassigned variables and no thread running).

Answer: There are multiple ways to achieve the same thing. Start by modifying the program to exit whenever the flag turns True. Then there are various options, each one with its advantages and disadvantages. Wrap it using a bash script. The script should handle exits and restart your program. A really basic version could be: #!/bin/bash while : do python program.py sleep 1 done  Start the program as a sub-process of another program. Start by wrapping your program's code to a function. Then your __main__ could look like this: def program(): ### Here is the code of your program ... while True: from multiprocessing import Process process = Process(target=program) process.start() process.join() print("Restarting...")  This code is relatively basic, and it requires error handling to be implemented. Use a process manager There are a lot of tools available that can monitor the process, run multiple processes in parallel and automatically restart stopped processes. It's worth having a look at PM2 or similar. IMHO the third option (process manager) looks like the safest approach. The other approaches will have edge cases and require implementation from your side to handle edge cases.
Question: Before 3.10, I was using Union to create union parameter annotations: from typing import Union class Vector: def __mul__(self, other: Union["Vector", float]): pass  Now, when I use the new union shorthand syntax: class Vector: def __mul__(self, other: "Vector" | float): pass  I get the error:  TypeError: unsupported operand type(s) for |: 'str' and 'type'  Is this not supported?

Answer: The fact that it's being used as a type hint doesn't really matter; fundamentally the expression "Vector" | float is a type error because strings don't support the | operator, they don't implement __or__. To get this passing, you have three options:  Defer evaluation (see PEP 563): from __future__ import annotations class Vector: def __mul__(self, other: Vector | float): ...   Make the whole type a string (effectively the same as deferring evaluation): class Vector: def __mul__(self, other: "Vector | float"): ...   Keep using the Union: from typing import Union class Vector: def __mul__(self, other: Union["Vector", float]): ...    You can see further discussion on this bug, resolved by documenting the behaviour explicitly:  Note: The | operand cannot be used at runtime to define unions where one or more members is a forward reference. For example, int | "Foo", where "Foo" is a reference to a class not yet defined, will fail at runtime. For unions which include forward references, present the whole expression as a string, e.g. "int | Foo".
Question: I tried looking for the answer earlier but couldn't seem to figure out a few things. I'm creating my form in a form.py file so its a python file. Here is my forms.py file : class UploadForm(ModelForm): name = forms.TextInput(attrs={'class': 'myfieldclass'}) details = forms.TextInput() littype = forms.TextInput() image = forms.ImageField() class Meta: model = info fields = ["name", "details", "littype", "image"]  Here is my views.py function for it if it helps find the solution : def uploadform(request): if request.method == 'POST': form = UploadForm(request.POST, request.FILES) print(request.FILES) if form.is_valid(): form.save() redirect(home) return render(request, 'uploadform.html', {'form': UploadForm})  To style it I thought I could do something like this which I found in another question : class MyForm(forms.Form): myfield = forms.CharField(widget=forms.TextInput(attrs={'class': 'myfieldclass'}))  Except I have no idea how to link a css page to that python file. This is what I tried writing but i think it doesnt work because its meant for html but its in a python file : <link type="text/css" rel="stylesheet" href="templates/form.css">  And so I'm just not sure how to style my form. Thanks for any answers!

Answer: Within your template you have to just import the css file in the head tag, but do ensure you load static first. html file: {% load static %} <!doctype html> <html lang="en"> <head> # import the css file here <link rel="stylesheet" href="{% static 'path to css file' %}"> </head> ... </html>  Within the css file: # Styling the class .myfieldclass{ width: 30% !important; height: 100px !important; ... }  But please note that you don't have to import a css file since you can add the style tag in the head tag as well. For example: <!doctype html> <html lang="en"> <head> <style type="text/css"> .myfieldclass{ width: 30% !important; height: 100px !important; ... } </style> </head> ... </html>  You could apply css from the python file itself as well. Approach from the python file. # Custom way to set css styling on a form field in python code def field_style(): styles_string = ' ' # List of what you want to add to style the field styles_list = [ 'width: 30% !important;', 'height: 100px !important;', ] # Converting the list to a string styles_string = styles_string.join(styles_list) # or # styles_string = ' '.join(styles_list) return styles_string class MyForm(forms.Form): myfield = forms.CharField(widget=forms.TextInput(attrs={'class': 'myfieldclass', 'style': field_style()})) # 'style': field_style() .... field_style() will return in this case 'width: 30% !important; height: 100px !important;'  Any of those should work, but I recommend styling from the html or css file instead.
Question: I'm looking for a function along the lines of df.group_by('column').agg(sample(10))  so that I can take ten or so randomly-selected elements from each group. This is specifically so I can read in a LazyFrame and work with a small sample of each group as opposed to the entire dataframe. Update: One approximate solution is: df = lf.group_by('column').agg( pl.all().sample(.001) ) df = df.explode(df.columns[1:])  Update 2 That approximate solution is just the same as sampling the whole dataframe and doing a groupby after. No good.

Answer: Let start with some dummy data: n = 100 seed = 0 df = pl.DataFrame({ "groups": (pl.int_range(n, eager=True) % 5).shuffle(seed=seed), "values": pl.int_range(n, eager=True).shuffle(seed=seed) })  shape: (100, 2) ┌────────┬────────┐ │ groups ┆ values │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞════════╪════════╡ │ 0 ┆ 55 │ │ 0 ┆ 40 │ │ 2 ┆ 57 │ │ 4 ┆ 99 │ │ 4 ┆ 4 │ │ … ┆ … │ │ 0 ┆ 90 │ │ 2 ┆ 87 │ │ 1 ┆ 96 │ │ 3 ┆ 43 │ │ 4 ┆ 44 │ └────────┴────────┘  This gives us 100 / 5, is 5 groups of 20 elements. Let's verify that: df.group_by("groups").agg(pl.len())  shape: (5, 2) ┌────────┬─────┐ │ groups ┆ len │ │ --- ┆ --- │ │ i64 ┆ u32 │ ╞════════╪═════╡ │ 0 ┆ 20 │ │ 4 ┆ 20 │ │ 2 ┆ 20 │ │ 3 ┆ 20 │ │ 1 ┆ 20 │ └────────┴─────┘  Sample our data Now we are going to use a window function to take a sample of our data. df.filter( pl.int_range(pl.len()).shuffle().over("groups") < 10 )  shape: (50, 2) ┌────────┬────────┐ │ groups ┆ values │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞════════╪════════╡ │ 0 ┆ 55 │ │ 2 ┆ 57 │ │ 4 ┆ 99 │ │ 4 ┆ 4 │ │ 1 ┆ 81 │ │ … ┆ … │ │ 2 ┆ 22 │ │ 1 ┆ 76 │ │ 3 ┆ 98 │ │ 0 ┆ 90 │ │ 4 ┆ 44 │ └────────┴────────┘  For every group in over("group") the pl.int_range(pl.len()) expression creates an index row. We then shuffle that range so that we take a sample and not a slice. Then we only want to take the index values that are lower than 10. This creates a boolean mask that we can pass to the filter method.
Question: I have been trying to setup a CI pipeline through Github Actions to docker-hub. I have written the following .yml file part of .github\workflow and getting the error as indicated below in Build and Push step of the job. I have tried to find it on Internet but I could not able to find it. name: Build and Deploy Code on: [push, pull_request] jobs: job1: # This will tell the job to run on ubuntu machine runs-on: ubuntu-latest env: DB_USER: postgres DB_PASSWORD: root123 DB_HOST: postgres DB_PORT: 5432 DATABASE: postgres SECRET_KEY: 09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7 ALGORITHM: HS256 ACCESS_TOKEN_EXPIRE_DAYS: 300 REDIS_HOST: redis REDIS_PORT: 6379 services: postgres: image: postgres env: POSTGRES_PASSWORD: root123 options: >- --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 ports: - 5432:5432 redis: image: redis #Docker hub image options: >- #Set health checks to wait until redis has started --health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5 ports: - 6379:6379 steps: - name: running git repo uses: actions/checkout@v3 # from marketplace - name: install python version 3.9 uses: actions/setup-python@v2 - name: update pip run: python -m pip install --upgrade pip - name: Install all dependencies run: pip install -r backend/requirements.txt - name: login to docker-hub uses: docker/login-action@v1 with: username: pankeshpatel password: Access-token - name: Build and push id: docker_build uses: docker/build-push-action@v2 with: context: ./ file: ./Dockerfile builder: ${{ steps.buildx.outputs.name }} push: true tags: pankeshpatel/bmw:latest cache-from: type=local, src=/tmp/.buildx-cache cache-to: type=local,dest=/tmp/.buildx-cache - name: Image digest run: echo ${{ steps.docker_build.outputs.diget }}  My dockerfile is # Use an existing image as a base FROM python:3.9.7 WORKDIR /usr/src/app COPY requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt COPY . . CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]  The following is the error, when I build on github actions /usr/bin/docker buildx build --cache-from type=local, src=/tmp/.buildx-cache --cache-to type=local,dest=/tmp/.buildx-cache --file ./Dockerfile --iidfile /tmp/docker-build-push-XZhTaT/iidfile --tag pankeshpatel/bmw:latest --metadata-file /tmp/docker-build-push-XZhTaT/metadata-file --push ./ error: cache export feature is currently not supported for docker driver. Please switch to a different driver (eg. "docker buildx create --use") Error: buildx failed with: error: cache export feature is currently not supported for docker driver. Please switch to a different driver (eg. "docker buildx create --use") 

Answer: You'll need something like this in your yml workflow file:  - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3  You can see a complete example at Configuring your GitHub Action builder
Question: In python, how can I add a patch that does not add a mock input argument? I want to add a patch on all methods in a class like so: @patch('django.utils.timezone.now', return_value=datetime.datetime(2022, 4, 22, tzinfo=timezone.utc)) class TestmanyMethods(unittest.TestCase): # my test methods here  But when I do that, a mock must be added to all test methods. How can I patch the function without adding the mock input to all test methods?

Answer: One can do this by using the new keyword arg in the patch like so: @patch('django.utils.timezone.now', new=Mock(return_value=datetime.datetime(2022, 4, 22, tzinfo=timezone.utc))) class TestmanyMethods(unittest.TestCase): # my test methods here  With that, the mock input is not added to the class's test methods
Question: I want to upload an image file to my Github Repository using Pygithub. from github import Github g=Github("My Git Token") repo=g.get_repo("My Repo") content=repo.get_contents("") f=open("1.png") img=f.read() repo.create_file("1.png","commit",img)  But I am getting the following error: File "c:\Users\mjjha\Documents\Checkrow\tempCodeRunnerFile.py", line 10, in <module> img=f.read() File "C:\Program Files\Python310\lib\encodings\cp1252.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 119: character maps to <undefined>  This method is working fine for text files. But I am not able to upload image files to my repository. When I use open-CV to read the image file I get the following error:  assert isinstance(content, (str, bytes)) AssertionError  My code while using cv2 is: from github import Github import cv2 g=Github("") repo=g.get_repo("") content=repo.get_contents("") f=cv2.imread("1.png") img=f repo.create_file("1.png","commit",img)  I think createFile() only takes string as an argument and thus these errors are showing up. Is there any way to upload the image file to Github using Pygithub (or any library)?

Answer: Just Tested my solution and it's working Explanation: The AssertionError assert isinstance(content, (str, bytes)) Tells us that it only takes string and bytes. So we just have to convert our image to bytes. #1 Converting image to bytearray file_path = "1.png" with open(file_path, "rb") as image: f = image.read() image_data = bytearray(f)  #2 Pushing image to Github Repo by converting the data to bytes repo.create_file("1.png","commit", bytes(image_data))  #CompleteCode in a Fun way from github import Github g=Github("Git Token") repo=g.get_repo("Repo") file_path = "Image.png" message = "Commit Message" branch = "master" with open(file_path, "rb") as image: f = image.read() image_data = bytearray(f) def push_image(path,commit_message,content,branch,update=False): if update: contents = repo.get_contents(path, ref=branch) repo.update_file(contents.path, commit_message, content, sha=contents.sha, branch) else: repo.create_file(path, commit_message, content, branch) push_image(file_path,message, bytes(image_data), branch, update=False)  My References For this Answer.  Converting Images to ByteArray Programmatically Update File PyGithub (StringFile Not Image)
Question: When I fit the model with: model.fit(X, y, epochs=40, batch_size=32, validation_split=0.2, verbose=2) it prints one log line for each epoch as: Epoch 1/100 0s - loss: 0.2506 - acc: 0.5750 - val_loss: 0.2501 - val_acc: 0.3750 Epoch 2/100 0s - loss: 0.2487 - acc: 0.6250 - val_loss: 0.2498 - val_acc: 0.6250 Epoch 3/100 0s - loss: 0.2495 - acc: 0.5750 - val_loss: 0.2496 - val_acc: 0.6250 .....  How can I print the log line per very 10 epochs as follows? Epoch 10/100 0s - loss: 0.2506 - acc: 0.5750 - val_loss: 0.2501 - val_acc: 0.3750 Epoch 20/100 0s - loss: 0.2487 - acc: 0.6250 - val_loss: 0.2498 - val_acc: 0.6250 Epoch 30/100 0s - loss: 0.2495 - acc: 0.5750 - val_loss: 0.2496 - val_acc: 0.6250 ..... 

Answer: This callback will create and write on a log text file what you want: log_path = "text_file_name.txt" # it will be created automatically class print_training_on_text_every_10_epochs_Callback(Callback): def __init__(self, logpath): self.logpath = logpath def on_epoch_end(self, epoch, logs=None): with open(self.logpath, "a") as writefile: # put log_path here with redirect_stdout(writefile): if (int(epoch) % 10) == 0: print( f"Epoch: {epoch:>3}" + f" | Loss: {logs['loss']:.4e}" + f" | Accuracy: {logs['accuracy']:.4e}" + f" | Validation loss: {logs['val_loss']:.4e}" + f" | Validation accuracy: {logs['val_accuracy']:.4e}" ) writefile.write("\n") my_callbacks = [ print_training_on_text_every_10_epochs_Callback(logpath=log_path), ]  You want to call it like this. model.fit( training_dataset, epochs=60, verbose=0, # Must be zero, else it would still print log per epoch. validation_data=validation_dataset, callbacks=my_callbacks, )  The text file will be updated only after 10 epochs have passed This is what i get on the text file Epoch: 0 | Loss: 5.3454e+00 | Valid loss: 4.2420e-01 Epoch: 10 | Loss: 3.1342e-02 | Valid loss: 3.4554e-02 Epoch: 20 | Loss: 1.6330e-02 | Valid loss: 2.2512e-02  The first epoch is numbered 0, the second 1 and so on.
Question: Is there a "in" operator in python 3.10 Match Case like with if else statements if "\n" in message: the in operator doesn't work in match case match message: case "\n" in message:  This doesn't work. How to have something like the "in" operator in Match-Case.

Answer: For completeness, it can be done indirectly using guards (as used in the other answer). match message: case message if "\n" in message: ... some code... case message if "foo" in message: ... some other code...  Note using the name message in each case statement is not required (this does have the side effect of binding the name x to a value): match message: case x if "\n" in x: ... some code... case x if "foo" in x: ... some other code...  Or you can also use wildcards (which skips the name binding): match message: case _ if "\n" in message: ... some code... case _ if "foo" in message: ... some other code...  But you're probably better off just using an if statement since using match-case is more code and has worse readability for this situation if "\n" in message: ... some code ... elif "foo" in message: ... some code ... else: ... some code ...
Question: I am new to Flask and Babel and I have just started a project which will contain several languages. After I have generated the babel.cfg file, when I attempt to extract it with the command pybabel extract -F babel.cfg -o messages.pot ., I get the AttributeError: module 'jinja2.ext' has no attribute 'autoescape'  error. What can be the reason for this error and how can I fix it? Thank you

Answer: With Jinja2 3.1, WithExtension and AutoEscapeExtension are built-in now. So you don't need these extensions anymore. Delete these extension from babel.cfg file [python: **.py] [jinja2: **/templates/**.html] ;extensions=jinja2.ext.auto escape,jinja2.ext.with_  https://jinja.palletsprojects.com/en/3.1.x/changes/#version-3-0-0