Questions tagged [python-datamodel]
For questions about various details related to Python data model: built-in types, classes, metaclasses, magic __dunder__ methods, operators, object initialization, attribute lookup, etc. Always remember to use 'python' tag together with this one. Using specific tag like 'operators' or 'metaclass' when appropriate is encouraged.
python-datamodel
102
questions
2
votes
1
answer
142
views
best way to create serializable data model
somewhat inexperienced in python. I am coming from C# world, and I am trying to figure out what is the best practice to create a data structure in python that:
can have empty fields (None)
can have ...
0
votes
1
answer
47
views
python: create empty dataclass and populate it on demand
python newbie: I need to create an empty data model class, populate it with values and then convert it to JSON string, however i am missing something important here. see error below
my data class:
...
0
votes
2
answers
71
views
Python: Magic method for when the object's reference count changes?
I want to a method that runs when object reference count is changed. is there a method like the following code ?
class Foo():
def __method__(self):
print("ojbect reference count is ...
0
votes
0
answers
60
views
Why cls() in a classmethod does not provide argument hints the way that directly calling the class constructor does?
I'm trying to make a subclass with a @classmethod that would take as arguments an instance of its superclass and kwargs for its other fields. The goal is to be able to take an object of the general ...
0
votes
0
answers
46
views
Should I use Model objects from library or make it myself?
I'm working with gitlab webhooks and I need to choose which data model to use in my project.
I'm using the Python-gitlab library to work with gitlab, and this library has its own data models for all ...
6
votes
2
answers
144
views
Python dunder methods wrapped as property
I stumbled upon this code that I found weird as it seems to violate the fact that python builtins call dunder methods directly from the class of the object. Using __call__ as an example, if we define ...
0
votes
3
answers
67
views
How to represent the numbers 0 to 3, taking up 2 bits of memory in python
I'm writing code for a Deep Q Network in python. My computer has 32GB of memory but I run into significant issues as the training goes on because the replay buffer maxes out the RAM.
I'm looking ...
1
vote
1
answer
28
views
classmethods and data model methods, implementing operands over classes or types
Here is what you would expect if you try and use the add operand over a type and int or between two type objects.
>>> class Foo:
... pass
...
>>> class Bar:
... pass
...
>&...
0
votes
1
answer
292
views
Are there any unique features provided only by metaclasses in Python?
I have read answers for this question: What are metaclasses in Python? and this question: In Python, when should I use a meta class? and skimmed through documentation: Data model.
It is very possible ...
3
votes
2
answers
1k
views
Wrapping derived class method from base class
Imagine I have a base and a derived class like so:
class A:
def foo(self):
pass
class B(A):
def foo(self):
pass
I wish to wrap calls foo calls made by instances of B. ...
2
votes
1
answer
390
views
How can I access the weakref object of the class itself through the class?
As far as I know, __weakref__ is a descriptor defined in class, so that if it invoked from the instances of the class, it will give the weakref object:
from weakref import ref
class A:
pass
obj =...
11
votes
1
answer
2k
views
Where is the default behavior for object equality (`==`) defined?
According to the object.__eq__() documentation, the default (that is, in the object class) implementation for == is as follows:
True if x is y else NotImplemented
Still following the documentation ...
0
votes
1
answer
407
views
Usage of __setattr__ to rewrite whole method of library class issue: missing 1 required positional argument: 'self'
I've got some imported packages with tricky structure
and need to call some method that bases on lots of other methods
with non-default parameters, which are not class attributes themself like ...
0
votes
1
answer
533
views
What to input for testing a completed Data Model (Python)?
I am a data science beginner, and I now have built a Python data model.
In the data cleaning part, I had to drop some columns, add new columns, hash some columns into new columns, change some columns ...
1
vote
1
answer
60
views
Does a metaclass-level variable cascade down to the metaclass's instantiated classes's instantiated objects the way a class-level variable does?
I've been intensively consulting the Python docs for descriptors, and I can't wrap my head around some points in it regarding descriptor invocation and the given Python equivalent of object....
0
votes
0
answers
37
views
id( ) function returns different value when using same float number [duplicate]
When declared an integer to a variable, both the integer and variable returns the same id value. But, in case of float, it returns a different id value. What is the reason behind it?
As below,
a=10
&...
-1
votes
1
answer
331
views
Create class and represent it without quotation marks
I need to represent my datatype PS without the leading and finishing quotation marks.
(so that '+' becomes +)
I tried overriding repr but cannot figure out, how to properly do it. My problem:
class E: ...
0
votes
1
answer
400
views
How to model POST body in a clean architecture
I'm asking myself a question about clean architecture.
Let's imagine a small api that allow us to create and get a user using that type of archi. This app has two endpoints and store the data in a ...
3
votes
1
answer
341
views
Overwrite * and ** unpacking with datamodel methods?
I'm trying to figure out how to give an object the ability to unpack values.
The use case I came up with is the following:
Let's have an Interval class, which we'll use to evaluate real valued ...
2
votes
0
answers
36
views
Does the default min() method in python uses heap binary tree implementation to find the minimum value in a list?
Hi so i have a list u = [2,45,0,56,78,13].
We can use min() method to find the minimum value in a list. I was going through the heap binary tree.
The python has a module heapq and there is a method ...
2
votes
1
answer
755
views
Why are the class __dict__ and __weakref__ never re-defined in Python?
Class creation seems to never re-define the __dict__ and __weakref__ class attributes (i.e. if they already exist in the dictionary of a superclass, they are not added to the dictionaries of its ...
3
votes
1
answer
998
views
Why do __setattr__ and __delattr__ raise an AttributeError in this case?
In Python, what is the rationale for which object.__setattr__ and type.__setattr__ raise an AttributeError during attribute update if the type has an attribute which is a data descriptor without a ...
-1
votes
1
answer
291
views
how do i Determine a Cut-Off or Threshold When Working With Fuzzymatcher in python
Please help on the photo is a screenshot of my output and code as well, how do i use the best_match_score I NEED TO FILTER BY THE RETURNED "PRECISION SCORE" THE COLUMN ONLY COMES AFTER THE ...
0
votes
1
answer
277
views
imported variable update is not reflecting in the parent module
i have two modules, main and update.
in main, i declare a dictionary of references and a list for each value. In update i update the age, but i do not see the update reflecting in the main module ...
-1
votes
1
answer
118
views
A lean interface for making python decorator classes
I've been looking to make an object-oriented setup to make decorator factories. A simple version can be found in this stackoverflow answer. But I'm not totally satisfied with the simplicity of the ...
1
vote
1
answer
1k
views
Python: Filter iterable class
Is there a hook/dunder that an Iterable object can hold so that the builtin filter function can be extended to Iterable classes (not just instances)?
Of course, one can write a custom filter_iter ...
0
votes
2
answers
385
views
Usage of python function comparison dunders
Question
Python functions have comparison dunders (see print out below). But they are NotImplemented. Fair enough. But what is their intended use, and how does one go about using them? When I assign ...
3
votes
1
answer
897
views
What is `operator.__inv__` existing for?
What is the difference with inv and invert?
>>> import operator
>>> operator.inv is operator.invert
False
>>> operator.__inv__ is operator.__invert__
False
I gather that ...
4
votes
1
answer
228
views
Why doesn't str() use __getattribute__ to get __str__ and how to produce the effect?
When I call str() on an object that has an overloaded __getattribute__ method it doesn't seem to use it and instead calls __str__ directly. Is there some other functionality I should be modifying or ...
0
votes
1
answer
466
views
How to do ETL on millon rows of data using python?
I've a pgAdmin database which contains millions of rows in geojson format.Using this table I create Tableau dashboard. Since the rows contain data in geojson format I've to query like this:
select ...
0
votes
0
answers
135
views
Custom binary operations and getattr
I would like to implement a class with custom binary operators, such that
class One:
def __lt__(self,other):
return 1 < other
Python seems to be able to automatically create the ...
2
votes
1
answer
3k
views
Proper use of __format__
I have a class that defines __str__ to return the integer value in hex and a __format__ to return the value formatted with the user's format spec:
class MyClass:
def __init__(self, value: int):
...
1
vote
1
answer
1k
views
What offers better performance for large datasets? Nested dictionaries or a dictionary of objects?
I find myself repeating this pattern when I am fetching from multiple database tables:
records = {'p_key': { "record": r, "A": list(), "B": list(), "C" : list() } for r in db_records}
I often have ...
1
vote
1
answer
188
views
Example of a custom deleter method
I have come across various examples of a custom getter or setter, but what would be a use case of when a custom deleter would be used? So far an example that I have is just something like this:
def ...
1
vote
1
answer
1k
views
Error handling in `__getattribute__` and `__getattr__`
Note this questions is answered in the comments
I've noticed some behaviour with exception handling in __getattribute__ and __getattr__ that I can't find mentioned in the docs, or other posts on here....
0
votes
1
answer
1k
views
When the Python __call__ method gets extra first argument?
The following sample
import types
import pprint
class A:
def __call__(self, *args):
pprint.pprint('[A.__call__] self=%r, args=%r'
% (self, list(args)))
class B:
...
1
vote
2
answers
6k
views
From a python dictionary how do I save the key and value to a *.txt file [closed]
How do I print the key and value to a *.txt file from a dictionary?
I have tried to read the data and to print it to a *.txt file but the name.txt file is empty.
#The code that I have tried
#...
11
votes
3
answers
721
views
Is there any way to tell if a function object was a lambda or a def?
Consider the two functions below:
def f1():
return "potato"
f2 = lambda: "potato"
f2.__name__ = f2.__qualname__ = "f2"
Short of introspecting the original source code, is there any way to ...
27
votes
1
answer
14k
views
Which Python dunder/magic methods do you need to implement to correctly proxy an object?
I'm trying to create an object proxy.
Attribute/property lookup can be done by simply implementing the __getattribute__, __setattr__ and __delattr__ methods.
However, other functionalities like len(x),...
8
votes
1
answer
189
views
When can dict_values views be set-like (and why)?
The docs say that values views are not treated as set-like, but sometimes they are:
>>> d = {1: 1}
>>> d.values() | d.keys()
{1}
>>> d.values() & d.keys()
{1}
>>...
-1
votes
1
answer
177
views
Class with '__getitem__' has no 'get' method
I realize that naturally a class with no get method has no get method. What would be the best way to add a get method to a class implementing __getitem__ [without boilerplate]?
I tried:
class Foo():...
1
vote
1
answer
233
views
Fast data structure in Python for indexing a bunch of images as duplicates
Introduction: I want to replace about 280'000 images of math formulas on the Encyclopedia of Mathematics by their corresponding TEX code. To do this, I have classified all of these images (or better: ...
1
vote
0
answers
29
views
Given a method, how to obtain the class that defines this method?
If this is a classmethod, this task is simple:
class MyClass:
@classmethod
def some_fun(cls):
pass
fun = MyClass.some_fun
my_class = fun.__self__
print (my_class is MyClass) # True
...
5
votes
5
answers
12k
views
python ImportError: cannot import name 'Faker' from 'faker' [duplicate]
hello so I've been writing this script to pre-populate my Django database but when I stopped writing it I got a weird error:
My Script:
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '...
1
vote
2
answers
2k
views
How to implement the __getitem__ dunder method in python to get attribute values?
I have the following code:
class Personne:
def __init__(self, name, age):
self.name = name
self.age = age
def __getitem__(self, *args):
keys = list(*args)
...
3
votes
2
answers
1k
views
isinstance(type, object) = True, why? [duplicate]
I have an understanding that object is an instance of type and type inherits from object. So the following relationships makes sense:
isinstance(object, type) # returns True, ok
issubclass(...
13
votes
1
answer
5k
views
Setting a class __name__ declaratively
Why can't you override a class name declaratively, e.g. to use a class name which is not a valid identifier?
>>> class Potato:
... __name__ = 'not Potato'
...
>>> Potato....
3
votes
1
answer
66
views
What data model methods implement argument keyword unpacking?
I have a class that I would like to be able to unpack into an argument list using the *args and **kwargs syntax.
class MyFoo:
x = 1
y = 2 # unpack x and y
z = 3 # do not unpack z
def bar(...
0
votes
1
answer
97
views
Python datastructure for storage, computing and mapping
My data will likely be in this format:
1-universal
Type_A=10, Type_B=20, Type_C=30
Local set 1:
Type_D=40, Type_C =30, Type_B=10, Type_A=15, Type_E=45
(ie A,B,C,D,E may not be in order)
Local ...
0
votes
1
answer
210
views
Is there a way to access the underlying dict from a dict subclass?
I'd like to build a special dict class that has the option to export it's underlying dict as a whole (not just the individual items), i.e. something like this:
class CustomDict(dict):
def export(...