oop - How can I use a list[customClass] as type with @dataclass in Python 3.7.x - Stack Overflow
How to declare an array or a list in a Python @dataclass? - Stack Overflow
Uniquefy list of dataclass
python 3.x - dictionary get method for list of dataclass objects - find dataclass with specific variable value in list of dataclasses - Stack Overflow
I have a nested dataclass as thus:
@dataclass()
class oeFile(object):
@dataclass
class contentParticle:
identifier ='p'
material = 0
index = 0
layer = 0xff
color = 0xffffffff
xPos = 0.0
yPos = 0.0
xVel = 0.0
yVel = 0.0
xOrg = 0.0
yOrg = 0.0
angle = 0.0
angVel = 0.0
pressure = 0
content = ''
particle: contentParticle = contentParticle()
contentOeFile = oeFile()How can I set it up so that I can run something like this? Making a new instance of oeFile, or can I make an array something like particleData = oeFile.particle[1000]?
random spaghetti pseudocode roughly envisioning what I'm hoping for
while i <= len(particleData):
(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o) = particleData[i]
i+=1where a to o would be fed from a separate iterator pulling those values from a list, perhaps as an object that returns those values.
Yes I've been told pandas does database stuff easily but this is a programming exercise for me.
I have a list of dataclass instances that i want to uniquefy. So i want to remove all duplicates while preserving order.
[3,1,1,4,5,6,6,3] -> [3,1,4,5,6]
I have two options.
Do list(dict.fromkeys(my_list)) which is O(N) but requires me to make the dataclass unsafe_hash=True (all of its attributes are hashable but the class is mutable).
Or write the O(N2) function that works also on unhashable types.
At the moment i am torn between which way to go.
The answer depends on whether or not you have access to an object of the class.
Just using the class
If you only have access to the class, then you can use dataclasses.fields(C) which returns a list of field objects (each of which has a .name property):
[field.name for field in dataclasses.fields(C)]
From an existing object
If you have a constructed object of the class, then you have two additional options:
- Use
dataclasses.fieldson the object:
[field.name for field in dataclasses.fields(obj)]
- Use
dataclasses.asdict(obj)(as pointed out by this answer) which returns a dictionary from field name to field value. It sounds like you are only interested in the.keys()of the dictionary:
dataclasses.asdict(obj).keys() # gives a dict_keys object
list(dataclasses.asdict(obj).keys()) # gives a list
list(dataclasses.asdict(obj)) # same
Full example
Here are all of the options using your example:
from dataclasses import dataclass, fields, asdict
@dataclass
class C:
x: int
y: int
z: int
t: int
# from the class
print([field.name for field in fields(C)])
# using an object
obj = C(1, 2, 3, 4)
print([field.name for field in fields(obj)])
print(asdict(obj).keys())
print(list(asdict(obj).keys()))
print(list(asdict(obj)))
Output:
['x', 'y', 'z', 't']
['x', 'y', 'z', 't']
dict_keys(['x', 'y', 'z', 't'])
['x', 'y', 'z', 't']
['x', 'y', 'z', 't']
You can use the asdict method of the dataclasses module. For example:
from dataclasses import dataclass, asdict
@dataclass
class Person:
age: int
name: str
adam = Person(25, 'Adam')
# if you want the keys
print(asdict(adam).keys()) # dict_keys(['age', 'name'])
# if you want the values
print(asdict(adam).values()) # dict_values([25, 'Adam'])
Both methods above return a View object which you can iterate on, or you can convert it to list using list(...).