eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: Begginer here trying to understand how python works and i came up with this doubt. Shouldn't all functions be methods? Here's the code i used to verify this my_module.py: def func(): pass  Main.py: import inspect import my_module print(inspect.ismethod(my_module.func))  Output: False

Answer: Because methods are defined on the class. Any given module is an instance of the class module, not the definition of a new class. Compare to: class Foo: pass f = Foo() f.stuff = lambda: print('stuff')  f.stuff is an instance attribute of that particular instance of Foo, not of Foos in general. The descriptor protocol (invoked when looking up an attribute on an instance, and finding it on the class, not the instance) is what converts functions on classes into methods of instances, and since these functions on a given module are all instance attributes of the module object, not the root class it's an instance of, no descriptor protocol is invoked, and the function doesn't become a method when called. All that said, you can make subclasses of the module type, which would allow a module to have real methods. This is documented under Customizing module attribute access as a fallback in case the special methods they explicitly added support for (e.g. __getattr__, __dir__) aren't enough and you need your module to do other things. Since the __class__ of a module is reassignable, you can reassign it to some other subclass of the module class and the methods of that class will receive self implicitly, so the same subclass can be assigned to multiple modules and provide per-module tailoring of behavior. For example: import sys from types import ModuleType class MethodModule(ModuleType): def dumb_method(self, x): return f"This is module named {self.__name__} which received {x!r}" thismodule = sys.modules[__name__] thismodule.__class__ = MethodModule print(thismodule.dumb_method("foo"))  Try it online! gives the module it's run within an instance method named dumb_method which customizes its behavior based on the self implicitly passed to it (in this case, using self.__name__ to determine the name of the module).
Question: I have a dataframe of app usage in 4 columns that looks like this: Id Timestamp App_Name Event_Type 1 2018/01/16 06:01:05 Instagram Opened 2 2018/01/16 06:01:06 Instagram Closed 3 2018/01/16 06:01:07 Instagram Opened 4 2018/01/16 06:01:08 Instagram Interaction 5 2018/01/16 06:01:09 Instagram Interaction 6 2018/01/16 06:02:08 Instagram Closed 7 2018/01/16 06:01:08 Instagram Opened 8 2018/01/16 06:01:08 Instagram Opened 9 2018/01/16 06:01:09 Instagram Opened 10 2018/01/16 06:01:09 Instagram Closed 11 2018/01/16 06:03:44 Instagram Opened 12 2018/01/16 06:03:44 Instagram Closed 13 2018/01/16 06:03:45 Instagram Closed 14 2018/01/16 06:03:45 Instagram Closed 15 2018/01/16 06:03:47 Instagram Opened  I want to get time difference in seconds between each pair of 'Opened' followed by 'Closed' rows regardless of whether or not there are other 'Event_Types' between them. There can be errors where there is more than one consecutive open or close. I just want difference between last open and first close. So in this case I want time differences between:  Rows 2 and 1 6 and 3 10 and 9 12 and 11  How do I do that? Thanks!

Answer: Try: out, state = [], None for i, e in zip(df["Id"], df["Event_Type"]): if e == "Opened": state = i elif e == "Closed" and state is not None: out.append([state, i]) state = None print(out)  Prints: [[1, 2], [3, 6], [9, 10], [11, 12]]   To get time differences: df["Timestamp"] = pd.to_datetime(df["Timestamp"]) out, state = [], None for i, e in zip(df.index, df["Event_Type"]): if e == "Opened": state = i elif e == "Closed" and state is not None: out.append(df.loc[i, "Timestamp"] - df.loc[state, "Timestamp"]) state = None print(out)  Prints: [Timedelta('0 days 00:00:01'), Timedelta('0 days 00:01:01'), Timedelta('0 days 00:00:00'), Timedelta('0 days 00:00:00')]
Question: So I understand that using async ... await python can prevent blocking and accomplish xyz to follow. But what about the opposite, where I want python to block xyz until an process has completed? For example, suppose I have three functions, A,B & C where A should not block B and vice versa. But C should be blocked by both A and B? async def A(): # some code async def B(): # some code def C(): # some code async def handler(): # A and B not to block each other await A() await B() # Must complete before C C()  As I understand it, asyncio will implicitly infer that C should be blocked by A and B if C is defined in terms of them (or their outputs.) But in the event that C is not defined in terms of A & B, how can it be ensured that C will only commence when A and B are complete?

Answer: If I understand you correctly, you want to run A, B in parallel and if they both finish, call C(). You can create tasks from A(), B(), use asyncio.gather() to wait for them to finish and then call C(): import asyncio async def A(): await asyncio.sleep(1) print("A finished") async def B(): await asyncio.sleep(2) print("B finished") def C(): print("In C!") async def handler(): # A and B not to block each other tasks = {asyncio.create_task(A()), asyncio.create_task(B())} await asyncio.gather(*tasks) # Must complete before C C() asyncio.run(handler())  Prints (and finishes in 2 seconds): A finished B finished In C!
Question: I am trying to create a pystray menu separator, but I am having a hard time doing so. I have searched here on SO and in its documentation, which I find super confusing and unhelpful and I even tried to read the Menu class: class Menu(object): """A description of a menu. A menu description is immutable. It is created with a sequence of :class:`Menu.Item` instances, or a single callable which must return a generator for the menu items. First, non-visible menu items are removed from the list, then any instances of :attr:`SEPARATOR` occurring at the head or tail of the item list are removed, and any consecutive separators are reduced to one. """ #: A representation of a simple separator SEPARATOR = MenuItem('- - - -', None) def __init__(self, *items): self._items = tuple(items)  In which I found the following representation, which I used like this: sep = pystray.MenuItem("- - - -", None)  but instead of creating a separator, it created a menu item with this text: - - - - You can find a minimal reproducible example below: import pystray from PIL import Image def item1_action(icon, item): print("Item 1 clicked") def item2_action(icon, item): print("Item 2 clicked") def quit_action(icon, item): print("Quit clicked") item1 = pystray.MenuItem("Item 1", item1_action) item2 = pystray.MenuItem("Item 2", item2_action) sep = pystray.MenuItem("- - - -", None) quit_item = pystray.MenuItem("Quit", quit_action) menu = (item1, item2,s, quit_item) image = Image.open('icon.png') icon = pystray.Icon("test", image, "test", menu) icon.run() 

Answer: You have correctly identified the point at which the use of the separator is indicated. However, you should specifically use the SEPARATOR attribute instead of its assigned value because someone might actually want to create a menu item with four hyphens, and it wouldn't be appropriate for pystray to automatically convert it to a separator against their will. That being said, you need to replace this: sep = pystray.MenuItem("- - - -", None)  with this: sep = pystray.Menu.SEPARATOR
Question: I created a surface plot of a gaussian using matplotlib: num_pts = 1000 σ = 1.5 x = np.linspace(-5, 5, num_pts) kernel1d = np.exp(-np.square(x) / (2 * σ * σ)) kernel2d = np.outer(kernel1d, kernel1d) X, Y = np.meshgrid(x, x) fig, ax = plt.subplots(figsize=(6,6), subplot_kw={"projection":"3d"}) ax.plot_surface(X, Y, kernel2d, cmap="viridis", linewidth=0, antialiased=True) ax.set(xticks=[], yticks=[], zticks=[]) ax.grid(False) ax.axis('off') fig.savefig("test.svg", format="svg", transparent=True)  The output has a lot of dead space around the actual plot.  I tried layout=tight which did not have any effect. How can I maximize the size of the surface withing the figure assuming no need for any axis: ticks, labels, grid, etc...

Answer: You can play around with the pad_inches value: fig.savefig("test.svg", format="svg", transparent=True, bbox_inches='tight', pad_inches=-0.4)  Otherwise, perhaps you can try getting the bounding box containing the paths, and set the axes limits based on that: https://stackoverflow.com/a/76076555/7750891
Question: For me the following extras = ["extra0", "extra1"] func_with_list_arg([ "base0", "base1", ] + extras)  is nicer to read with a spread operator like the following extras = ["extra0", "extra1"] func_with_list_arg([ "base0", "base1", *extras, ])  Is there a lint rule in ruff or pylint that would detect this situation?

Answer: Yes, there is! c = [3] b = [1, 2] + c print(b)  When I run (my configured) ruff on this, I get:  RUF005 [*] Consider [1, 2, *c] instead of concatenation  This is part of the ruff specific ruleset: https://docs.astral.sh/ruff/rules/#ruff-specific-rules-ruf and is a rule which is actually automatically fixable.
Question: I'm trying to turn a series of m input sequences with n items each (sm,n) into a tensor where tp,q,r = sp,qsp,r While my code does work, I feel there must be a better solution. Here's what I got. # nseqs is the number of sequences # seq_length is the sequence length # seq is a list of sequences output = np.empty((nseqs, seq_length, seq_length)) for n in range(nseqs): for i, j in enumerate(seq[n]): output[n, i, :] = j output[n, :, :] *= output[n, :, :].T  More to the point, is there a way to rejuggle the output tensor so that the multiplication phase is done in a single step and without those loops?

Answer: You can achieve the desired result with broadcasting. The first dimensions line up, and the others can be coerced into the right shape using unit dimensions introduced with None: t = s[:, :, None] * s[:, None, :]  You can implement your original approach without loops by assigning the elements of output with broadcasting and then taking the transpose only of the last dimensions: output = np.empty((seq.shape[0], seq.shape[1], seq.shape[1]), seq.dtype) output[:] = seq[..., None] output *= output.transpose(0, 2, 1)
Question: I'm making an app using gpt-neo and I'm trying to install torch, but it won't install. The error message is as follows: C:\Users\Ben>pip install torch ERROR: Could not find a version that satisfies the requirement torch (from versions: none) ERROR: No matching distribution found for torch  Is there any way to install torch without pip?

Answer: There are now released versions of pytorch available for python 3.12, starting with pytorch 2.2 There should be no need to use the pre-release nightly build for cuda 11.8 and python 3.12, at least there seems to be installation candidates in https://download.pytorch.org/whl/cu118 for python 3.12: torch-2.2.0+cu118-cp312-cp312-linux_x86_64.whl torch-2.2.0+cu118-cp312-cp312-win_amd64.whl So at least on windows and linux, you should be able to do pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118  for a cuda enabled installation of pytorch
Question: I am attempting to transcribe an audio file using the Whisper library which contains alternating English and Indonesian speech. Some of the Indonesian speech is correctly transcribed into Indonesian text, but some of it is translated into English and transcribed. This behaviour seems to be random, different passes with the same model and different models give different results. Is there any way to only transcribe and not translate? Setting the language to Indonesian causes everything to be translated to Indonesian. Setting it to English causes the behaviour I described.

Answer: You could use WhisperX and leverage its speaker diarization.  Make two (or more) transcription passes, one for each language. Merge both results based on speaker and time stamps.
Question: Both validators and extra columns have different ways of configuration in pydantic 2.*, I would like to know if I can still do a validation to take all the extra fields and put them in a single dictionary field The expected behavior is: class A(BaseModel): name: str extra_columns: Optional[Dict] a = A(name="john", age=24, address="Brazil") print(a) >>> A(name="john", extra_columns={"age": 24, "address": "Brazil"})  My previous code:  @root_validator(pre=True) def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: all_required_field_names = { field.alias for field in cls.__fields__.values() if field.alias != "extra_columns" } extra: Dict[str, Any] = {} for field_name in list(values): if field_name not in all_required_field_names: extra[field_name] = values.pop(field_name) values["extra_columns"] = extra return values  Is possible to take the same behavior in pydantic 2.*?

Answer: This can be solved with model_validator: from typing import Any from pydantic import BaseModel, model_validator class A(BaseModel): name: str extra_columns: dict | None = None @model_validator(mode="before") @classmethod def set_extra_columns(cls, data: Any): if isinstance(data, dict): extra_fields = data.keys() - cls.model_fields.keys() if extra_fields: if "extra_columns" not in data: data["extra_columns"] = {} data["extra_columns"].update( {field_name: data[field_name] for field_name in extra_fields} ) return data a = A(name="john", age=24, address="Brazil") print(a) # >>> name='john' extra_columns={'age': 24, 'address': 'Brazil'}
Question: This is my JsonFeedOptions class. class JsonFeedOptions(BaseModel): address: Optional[AddressType] = None signer: Optional[Union[AccountAPI, str]] = None Type: Optional[FeedType] = None  And I'm trying to validate the data like this. opts = {"signer": "abcd"} options = JsonFeedOptions.model_validate(opts)  But got this error > options = JsonFeedOptions.model_validate(opts) E TypeError: validate() takes 2 positional arguments but 3 were given 

Answer: Which Pydantic version do you use? You can check this by pip list and review the library version (do not forget to activate virtual environment if you have one) The error TypeError: BaseModel.validate() takes 2 positional arguments, but 3 were given is relevant to the difference between Pydantic versions 1 and 2. In Pydantic version 2, the method signature for BaseModel.validate() has been modified, which may lead to this error. In any case, you cannot touch the Pydantic library method, but you have two options defined here. Option 1: Migrate your code to new Pydantic version Option 2: Use the old version with from pydantic.v1 import BaseModel instead of from pydantic import BaseModel. Keep in the mind that this v1 call is possible if you have new version.
Question: I am trying to run this program using Python 3.11.5 on macOS Ventura 13.6: import pyttsx3 engine = pyttsx3.init() engine.say("Hello, how are you today?") engine.runAndWait()  But I am getting this error and I don't know where to start looking: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyttsx3/__init__.py", line 20, in init eng = _activeEngines[driverName] ~~~~~~~~~~~~~~^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/weakref.py", line 136, in __getitem__ o = self.data[key]() ~~~~~~~~~^^^^^ KeyError: None During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/raelynmarie/Desktop/Tester.py", line 3, in <module> engine = pyttsx3.init() ^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyttsx3/__init__.py", line 22, in init eng = Engine(driverName, debug) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyttsx3/engine.py", line 30, in __init__ self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyttsx3/driver.py", line 50, in __init__ self._module = importlib.import_module(name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<frozen importlib._bootstrap>", line 1204, in _gcd_import File "<frozen importlib._bootstrap>", line 1176, in _find_and_load File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 690, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 940, in exec_module File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyttsx3/drivers/nsss.py", line 12, in <module> class NSSpeechDriver(NSObject): File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyttsx3/drivers/nsss.py", line 13, in NSSpeechDriver @objc.python_method ^^^^ NameError: name 'objc' is not defined. Did you mean: 'object'?  I reinstalled the dependencies following a different order and that did not fix the error. Order of installation:  pyobjc-core pyobjc-framework-Cocoa pyobjc-framework-Quartz pyobjc 

Answer: I checked pypi and I found py3-tts: https://pypi.org/project/py3-tts/ I followed the installation for py3-tts and it works now without the dummy parameter!
Question: Environment: Python 3.10.11 Flask==2.2.2  I run my Flask backend code in docker container, with BASE Image: FROM pytorch/pytorch:2.0.1-cuda11.7-cudnn8-runtime But when I run the pytest with version pytest 7.4.2, pip install pytest pytest  it raised an Error, with logs: ==================================== ERRORS ==================================== _____________ ERROR collecting tests/test_fiftyone_utils_utils.py ______________ ImportError while importing test module '/builds/kw/data-auto-analysis-toolkit-backend/tests/test_fiftyone_utils_utils.py'. Hint: make sure your test modules/packages have valid Python names. Traceback: /opt/conda/lib/python3.10/importlib/__init__.py:126: in import_module return _bootstrap._gcd_import(name[level:], package, level) tests/test_fiftyone_utils_utils.py:2: in <module> import daat # noqa: F401 /opt/conda/lib/python3.10/site-packages/daat-1.0.0-py3.10.egg/daat/__init__.py:1: in <module> from daat.app import app /opt/conda/lib/python3.10/site-packages/daat-1.0.0-py3.10.egg/daat/app/__init__.py:6: in <module> from flask import Flask, jsonify, request /opt/conda/lib/python3.10/site-packages/flask/__init__.py:5: in <module> from .app import Flask as Flask /opt/conda/lib/python3.10/site-packages/flask/app.py:30: in <module> from werkzeug.urls import url_quote E ImportError: cannot import name 'url_quote' from 'werkzeug.urls' (/opt/conda/lib/python3.10/site-packages/werkzeug/urls.py)  My codes works well when I directly run it with python run.py run.py shown below from daat import app app.run(host='0.0.0.0')  I guess it should be the pytest versions issue, because it used to work well without changing any related code, and I use pip install pytest without defined a specific version. And my backend runs well without pytest.

Answer: I had the same problem. It is because Werkzeug 3.0.0 was released and Flask doesn't specify the dependency correctly (requirements says Werkzeug>=2.2.0). This is why, Werkzeug 3.0.0 is still installed and Flask 2.2.2 isn't made for Werkzeug 3.0.0. Solution: Just set a fix version for Werkzeug such as Werkzeug==2.2.2 in your requirements.txt and it should work.
Question: How can I efficiently implement the fill forward logic (inspired for pandas ffill) for a vector shaped NxLxC (batch, sequence dimension, channel). Because each channel sequence is independent this can be equivalent to working with a tensor shaped (N*C)xL. The computation should keep the torch variable so that the actual output is differentiable. I managed to make something with advanced indexing, but it is L**2 in the memory and number of operations, so not very great and gpu friendly.  Example: Assuming you have the sequence [0,1,2,0,0,3,0,4,0,0,0,5,6,0] in a tensor shaped 1x14 the fill forward will give you the sequence [0,1,2,2,2,3,3,4,4,4,4,5,6,6]. An other example shaped 2x4 is [[0, 1, 0, 3], [1, 2, 0, 3]] which should be forward filled into [[0, 1, 1, 3], [1, 2, 2, 3]].  Method used today: We use the following code that is highly unoptimized but still faster than non vectorized loops: def last_zero_sequence_start_indices(t: torch.Tensor) -> torch.Tensor: """ Given a 3D tensor `t`, this function returns a two-dimensional tensor where each entry represents the starting index of the last contiguous sequence of zeros up to and including the current index. If there's no zero at the current position, the value is the tensor's length. In essence, for each position in `t`, the function pinpoints the beginning of the last contiguous sequence of zeros up to that position. Args: - t (torch.Tensor): Input tensor with shape [Batch, Channel, Time]. Returns: - torch.Tensor: Three-dimensional tensor with shape [Batch, Channel, Time] indicating the starting position of the last sequence of zeros up to each index in `t`. """ # Create a mask indicating the start of each zero sequence start_of_zero_sequence = (t == 0) & torch.cat([ torch.full(t.shape[:-1] + (1,), True, device=t.device), t[..., :-1] != 0, ], dim=2) # Duplicate this mask into a TxT matrix duplicated_mask = start_of_zero_sequence.unsqueeze(2).repeat(1, 1, t.size(-1), 1) # Extract the lower triangular part of this matrix (including the diagonal) lower_triangular = torch.tril(duplicated_mask) # For each row, identify the index of the rightmost '1' (start of the last zero sequence up to that row) indices = t.size(-1) - 1 - lower_triangular.int().flip(dims=[3]).argmax(dim=3) return indices 

Answer: Here is an approach to this problem, without creating TxT matrix: import torch def forward_fill(t: torch.Tensor) -> torch.Tensor: n_dim, t_dim = t.shape # Generate indices range rng = torch.arange(t_dim) rng_2d = rng.unsqueeze(0).repeat(n_dim, 1) # Replace indices to zero for elements that equal zero rng_2d[t == 0] = 0 # Forward fill of indices range so all zero elements will be replaced with previous non-zero index. idx = rng_2d.cummax(1).values t = t[torch.arange(n_dim)[:, None], idx] return t  Note that this is a solution for 2D input but can be easily modified for more dimensions.
Question: I would like to use the experimental typing.Unpack in my project. In the CLI command, it works when adding --enable-incomplete-feature=Unpack. However, I have mypy issues reported by pyright (in neovim), therefore I would like to add this option in the section mypy of pyproject.toml. How can I achieve it?

Answer: It should be like this [tool.mypy] enable_incomplete_feature = ["Unpack"]