Assign a short name to a class attribute - python

I am using a Python package which read some type of data. From the data, it creates attributes to easily access meta-information related to the data.
How can create a short name to an attribute?
Basically let's assume the package name is read_data and it has an attribute named data_header_infomation_x_location
import read_data
my_data = read_data(file_path)
How can I instead create a short name to this attribute?
x = "data_header_infomation_x_location"
my_data[1].x gives an error no attribute
Here is a full example from my case
from obspy.io.segy.core import _read_segy
file_path = "some_file_in_my_pc)
sgy = _read_segy(file_path, unpack_trace_headers=True)
sgy[1].stats.segy.trace_header.x_coordinate_of_ensemble_position_of_this_trace
The last line gives a number. e.g., x location
what I want is to rename all this long nested attribute stats.segy.trace_header.x_coordinate_of_ensemble_position_of_this_trace with a short name.
trying for example
attribute = "stats.segy.trace_header.x_coordinate_of_ensemble_position_of_this_trace"
getattr(sgy[1], attribute )
does not work

how about:
from obspy.io.segy.core import _read_segy
attribute_tree_x = ['stats', 'segy', 'trace_header', 'x_coordinate_of_ensemble_position_of_this_trace']
def get_nested_attribute(obj, attribute_tree):
for attr in attribute_tree:
obj = getattr(obj, attr)
return obj
file_path = "some_file_in_my_pc"
sgy = _read_segy(file_path, unpack_trace_headers=True)
sgy[1].stats.segy.trace_header.x_coordinate_of_ensemble_position_of_this_trace
x = get_nested_attribute(sgy[1], attribute_tree_x) # should be the same as the line above
You cannot request the attribute of the attribute in one go, but this loops through the layers to obtain the final value you are looking for.

Related

Access STEP Instance ID's with PythonOCC

Let's suppose I'm using this STEP file data as input:
#417=ADVANCED_FACE('face_1',(#112),#405,.F.);
#418=ADVANCED_FACE('face_2',(#113),#406,.F.);
#419=ADVANCED_FACE('face_3',(#114),#407,.F.);
I'm using pythonocc-core to read the STEP file.
Then the following code will print the names of the ADVANCED_FACE instances (face_1,face_2 and face_3):
from OCC.Core.STEPControl import STEPControl_Reader
from OCC.Core.TopExp import TopExp_Explorer
from OCC.Core.TopAbs import TopAbs_FACE
from OCC.Core.StepRepr import StepRepr_RepresentationItem
reader = STEPControl_Reader()
tr = reader.WS().TransferReader()
reader.ReadFile('model.stp')
reader.TransferRoots()
shape = reader.OneShape()
exp = TopExp_Explorer(shape, TopAbs_FACE)
while exp.More():
s = exp.Current()
exp.Next()
item = tr.EntityFromShapeResult(s, 1)
item = StepRepr_RepresentationItem.DownCast(item)
name = item.Name().ToCString()
print(name)
How can I access the identifiers of the individual shapes? (#417,#418 and #419)
Minimal reproduction
https://github.com/flolu/step-occ-instance-ids
Create a STEP model after reader.TransferRoots() like this:
model = reader.StepModel()
And access the ID like this in the loop:
id = model.IdentLabel(item)
The full code looks like this and can also be found on GitHub:
from OCC.Core.STEPControl import STEPControl_Reader
from OCC.Core.TopExp import TopExp_Explorer
from OCC.Core.TopAbs import TopAbs_FACE
from OCC.Core.StepRepr import StepRepr_RepresentationItem
reader = STEPControl_Reader()
tr = reader.WS().TransferReader()
reader.ReadFile('model.stp')
reader.TransferRoots()
model = reader.StepModel()
shape = reader.OneShape()
exp = TopExp_Explorer(shape, TopAbs_FACE)
while exp.More():
s = exp.Current()
exp.Next()
item = tr.EntityFromShapeResult(s, 1)
item = StepRepr_RepresentationItem.DownCast(item)
label = item.Name().ToCString()
id = model.IdentLabel(item)
print('label', label)
print('id', id)
Thanks to temurka1 for pointing this out!
I was unable to run your code due to issues installing the pythonocc module, however, I suspect that you should be able to inspect the StepRep_RepresentationItem object (prior to string conversion) by traversing __dict__ on it to discover/access whatever attributes/properties/methods of the object you may need:
entity = tr.EntityFromShapeResult(s, 1)
item = StepRepr_RepresentationItem.DownCast(entity)
print(entity.__dict__)
print(item.__dict__)
If necessary the inspect module exists to pry deeper into the object.
References
https://docs.python.org/3/library/stdtypes.html#object.__dict__
https://docs.python.org/3/library/inspect.html
https://github.com/tpaviot/pythonocc-core/blob/66d6e1ef6b7552a1110a90e86a1ed34eb12ecf16/src/SWIG_files/wrapper/StepElement.pyi

Getting name is not defined error, but it is defined

I used the exec() function and the for loop to create various class objects and store them in variables.
I think the variables were created successfully as shown in the vs code log, but when I try to access it to change the value or print it, I get this error (name 'membro_1' is not defined) as if it's not defined
What did i do wrong here? how can i access those variables? Is there another better way to create all these objects?
see the variables defined at left
the code from image
class Membros:
def __init__(self, tag, pontos=0) -> None:
self.tag = tag
self.pontos = pontos
# war_cla_member have a len() of 47
war_cla_members = ['#VYQPR', '#82PP2LL20', '#LP0RVCPLV', '#LUVQQ2G2', '#PRP20LUL', '#8GUY0V92R',
'#Y02UVP0UV', '#9U0J8GVJL', '#9P2GGVR9Y', '#20QRJLVU8', '#QRYPRQGP9', '#8GRVUG8', '#PCJYUP2L8', '#22VJPRQRL', '#RJQ8JQ8QR', '#2CVR9C2U9', '#PG2UGPJP', '#L0QG9CG2U', '#9R0PR9Q0U', '#2G8VGQ208', '#8GJ8PGY0C', '#9QLLPJQ90', '#C9PGG8JC', '#8YG8RJV90', '#9YLLQLJGU', '#2GQQ2PU92', '#2PYU080Q', '#22QCRQCPG', '#C9JRU9U2', '#9JQLPGLJJ', '#8RR8QVR09', '#9QY2CLVJR', '#U0V0G2YY', '#28PR280CJ', '#P2RC2G9CL', '#9QVVY2P8', '#CVUGYPCP', '#9PVYQP080', '#29P2V8GLJ', '#YUJ88YRU', '#2RU0UGCUU', '#Y08LY8GJY', '#9R00QQU20', '#P08UJ920', '#2C00L02RU', '#YYQP9JGVC', '#YLULUC8L']
for idx, val in enumerate(war_cla_members):
exec(f'membro_{idx} = Membros("{val}")')
for idx, _ in enumerate(war_cla_members):
if current_river_race['clan']['participants'][idx]['tag'] in war_cla_members: #current_river_race is the return of the clashroyale api request. (https://developer.clashroyale.com/#/documentation)
membro_1.pontos = current_river_race['clan']['participants'][idx]['decksUsedToday']

Subscripting sqlalchemy table columns

I am trying to write a class that will look for certain column types in a sqlalchemy reflected table and then do some operations for a subset of columns based on the data type.
I can correctly reflect the table and grab a list of the 'date' type columns as shown in the date_types list. However, when it gets to table[name] the function fails with the error:
*** TypeError: 'DeclarativeMeta' object is not subscriptable
If I use dot subscripts instead of square brackets i.e. table.col_name I can access the table column attribute but I don't see how I would iterate over the attribute list using that syntax.
Here is my class:
from pdb import set_trace
class dateRangeProfiler():
def __init__(self, session):
self.date_ranges = {}
self.date_types = [Date(), DateTime(), TIMESTAMP()]
self.session = session
print('date data types: ', str(self.date_types))
def __call__(self, table):
date_columns = self.getDateColumns(table)
print(date_columns)
date_column_profile = self.profileColumns(table, date_columns)
return date_column_profile
def getDateColumns(self, table):
columns = [(c.name, c.type) for c in table.__table__.columns if str(c.type) in [str(dt) for dt in self.date_types]]
return columns
def profileColumns(self, table, date_cols):
profile = {}
for (name, _) in date_cols:
set_trace()
print(name)
qry = self.session.query(func.max(table[name]).label("max_date"),
func.min(testTable[name]).label("min_date"),) # <-- fails here
res = qry.one()
max = res.max_date
min = res.min_date
profile.append({name: {'max':max, 'min':min}})
Here is how I call the profiler:
date_range_profiler = dateRangeProfiler(sess)
date_range_profiler(my_table)
And the error:
*** TypeError: 'DeclarativeMeta' object is not subscriptable
The issue doesn't have anything to do with the sqlalchemy module. When accesing attributes of object using variable references, use the getattr() base python function.
qry = self.session.query(func.max(getattr(table,name)).label("max_date"),
func.min(getattr(table,name)).label("min_date"),)

How to get property names from .py file which contains class definition

I get on input file which contains only one class definition (class is just constants container, contains keys for json, similar file is used on Java client to decode json) looks like:
class Constants(object):
VERSION= 'version'
OS = 'os'
PROGRAM = 'program'
# more constants .....
How to get dictionary of all properties defined inside Constants, how to parse file to dictionary ?
I want to compress keys and generate new .py and .java files with same constants keys but shorter keys.
Import the module
I used imp.load_module instead of __import__ in the following code to import abitrary file path.
Find the class object.
Iterate the class attribute using vars:
import imp
path = '/path/to/file'
with open(path, 'U') as f:
mod = imp.load_module('temporary', f, path, ('.py', 'U', imp.PY_SOURCE))
builtins = vars(__builtins__)
cls = next(value for name, value in vars(mod).items() if name not in builtins)
const_dict = {name: value for name, value in vars(mod.Constants).items()
if not name.startswith('_')}
print(const_dict)
# => {'OS': 'os', 'VERSION': 'version', 'PROGRAM': 'program'}
Tested in Python on 2.7.6, 3.3.2, 3.4.0b2.
>>> [elem for elem in dir(Constants) if not elem.startswith("_")]
['OS', 'PROGRAM', 'VERSION']
Expanding on answer number one:
# dir(yourClass) will get you all the methods and properties of yourClass and parents wheather yourClass
# is a definition or an instance
elements = [elem for elem in dir(Constants) if not elem.startswith("_")]
# Using yourClass.__dict__.keys() will give you the same of dir if applied to a definition but only instance members
# if applied to an instance
elements = [elem for elem in Constants.__dict__.keys() if not elem.startswith("_")]
# You can get to the values of the properties with
for el in elements:
print Constants.__dict__[el]
# plus whatever you want to do to those elements
# Or if you're using the __dict__ way
Constants.__dict__.items()
Here is an example of using execfile and python 2.6 (I work on Debian Wheezy). A shorter version to build the dictionary for python version 2.7 and higher is given too. The constants.py file can define several classes, all of them will be parsed.
#!/usr/bin/env python
d = {}
const_d = {}
execfile("constants.py", d)
for k,cls in d.items():
if k not in vars(__builtins__):
if type(cls) is type:
# Python version < 2.7
attributes = {}
for name, value in vars(cls).items():
if not name.startswith('__'):
attributes[name] = value
# Python version >= 2.7
#attributes = {name: value for name, value in vars(cls).items() if not name.startswith('__')}
const_d[cls.__name__] = attributes
pass
pass
pass
print(const_d)

Python Maya - If objectType returns "No object name specified"

I am trying to get maya to check if the listed object is a blendshape node or not.
This is my code:
def bake(self, *args):
self.items["selection"] = cmds.ls(sl = True)
self.items["shapes"] = cmds.listRelatives(self.items["selection"], ad = True)
shapes = ()
for i in self.items["shapes"]:
bs = cmds.listConnections(i, type = "blendShape", exactType = True)
if cmds.objectType(bs, isType = "blendShape"):
print bs
It returns # Error: RuntimeError: file X:/Documents/maya/scripts\jtBakeCharacter.py line 16: No object name specified
Line 16 is: if cmds.objectType(bs, isType = "blendShape"):
Except that I AM specifying an object name, that object name is bs .. I have printed the result of bs and it has many objects listed. Many.
The code is redundant. You don't need most of the lines. The listConnections already ensures that you have only blendshapes. The exact problem is that you are calling something like:
cmds.objectType([])
for some of those extra shapes. And this is illegal. But mostly you code can be encapsulated as follows:
selected = cmds.ls(sl = True, dag=True ,shapes = True)
blends = cmds.listConnections(selected , type = "blendShape", exactType = True)
for item in blends:
print item
But this may not catch your intent perfectly, but shows how may extra steps you take. In reality you don't need the line if cmds.objectType(bs, isType = "blendShape"): for anything
Joojaa's answer is elegant, but you can get it down even shorter by using the default selection behavior:
blendshapes = cmds.ls(cmds.listHistory(pdo=True), type='blendShape') or []
for item in blendshapes:
print item
(In the quest to make it even shorter I'm not checking for the selection, so this one fails if nothing is selected).
PS: if you need to get to the blendshape from one of the upstream shapes, instead of the deformed shape, you can use listHistory (f=True)
You could try this:
from pymel.core import *
for obj in selected():
shapeNode = obj.getChildren()[0]
for output in shapeNode.outputs():
if nodeType(output) == "blendShape":
print obj, "is a blendshape"

Categories

Resources