Is there a Python container that acts like a dictionary but doesn't need both key and value?

Advertisements Say I have: @dataclass class Foo: foo_id: int # Other interesting fields. def __hash__(self): return self.foo_id.__hash__() And I make a foos_set = {Foo(i) for i in range(10)}. I had always assumed that set.remove used the hash for constant-time lookup. So I thought it would be reasonable to think that foos_set.remove(6) should work. But actually,… Read More Is there a Python container that acts like a dictionary but doesn't need both key and value?

How to set up API get query parameters in python request

Advertisements I’m trying to access an API endpoint in python. Generically, the request needs to look like this. In my real world example, (replacing URL with the actual endpoint), this works as expected. Notice the metanames arg is called multiple times res = requests.get(‘url?someargs&metanames=part1&metanames=part2′) However, I’m now trying to do it this way: params =… Read More How to set up API get query parameters in python request

Issue Finding Keys in 2nd Dictionary which Has Different Value Compare to Dictionary One

Advertisements I can use diff = set(dict2) – set(dict1) to know that the dict1 and dict2 are not equal but what I need is to find which KEY in dict2 has a different value than dict1 dict1 = {‘c1’: ‘Iran’, ‘c2’: ‘India’, ‘c3’: ‘Iraq’, ‘c4’: ‘Qatar’} dict2 = {‘c1’: ‘Iran’, ‘c2’: ‘India’, ‘c3’: ‘Iraqs’,’c4′: ‘Qeter’}… Read More Issue Finding Keys in 2nd Dictionary which Has Different Value Compare to Dictionary One

Can I pass variables to the compare function in sorted method in Python?

Advertisements Here is the code: if(order=="dsc"): return sorted(qs, key=compare_function, reverse=True) elif (order=="asc"): # asc # return sorted(qs, key=lambda obj: obj.teacher.name) return sorted(qs, key=compare_function) else: pass def compare_function(obj): if obj == None: return "aaaaaaa" if obj.teacher == None: return "aaaaaaa" else: test = {"teacher": obj.teacher} return test.get("teacher").name.lower() What I expect is to pass some additional params… Read More Can I pass variables to the compare function in sorted method in Python?

Can I make the class name to be a variable in python?

Advertisements Can I make the class name to be a variable in the newer python release? I mean can I short the following instructions to be briefer? if target == "course": return Course.objects.all()[start:end]` elif target == "Subject": return Subject.objects.all()[start:end] elif target == "Teacher": return Teacher.objects.all()[start:end] to be briefer: return VARIABLE.objects.all()[start:end] >Solution : This looks like… Read More Can I make the class name to be a variable in python?

How to Consolidate if statements in Python for Text replacement

Advertisements I have the following code I wrote in python, but I want to make it easier to maintain. from tkinter import * from tkinter import filedialog import os from docx import document def replace_text(file_path, b1o, b1a, b1p, b1e): document = document(file_path) for paragraph in document.paragraphs: if ‘B1O’ in paragraph.text: paragraph.text = paragraph.text.replace(‘B1O’, b1o) if… Read More How to Consolidate if statements in Python for Text replacement

How to create a dataset of pedestrian walking trajectories in python?

Advertisements I am trying to use python to simulate the trajectories of pedestrians walking in an area. But the searched articles all use the random walk method, which is not quite like the actual pedestrian trajectory. Is there a better way to simulate pedestrian trajectories? >Solution : There are several model such as Social Force… Read More How to create a dataset of pedestrian walking trajectories in python?