How to get all attributes and values of an Active Directory object using LDAP path with win32com package ?
Example:
import win32com.client
ldap_path = "LDAP://CN=aaa,DC=bbb,DC=ccc,DC=eee"
ldap_object = win32com.client.GetObject(ldap_path)
My own answer:
import win32com.client
def get_attributes_and_values(ldap_path):
ldap_object = win32com.client.GetObject(ldap_path)
ldap_object.GetInfo()
attributes = [
ldap_object.Item(x).Name
for x in range(0, ldap_object.PropertyCount)
]
for attribute in attributes:
value = getattr(ldap_object, attribute)
yield {attribute: value}
ldap_path = "LDAP://CN=aaa,DC=bbb,DC=ccc,DC=eee"
attr_val = get_attributes_and_values(ldap_path)
for x in attr_val:
print(x)
Item, Name, PropertyCount are objects/attributes of "COM" object that has been wrapped by win32com.client.
Related
I am using Azure Confidential Ledger, and I am trying to query it. But when I want to print a variable that contain value I encounter following line as an output:
bound method ConfidentialLedgerClientOperationsMixin.list_ledger_entries of <azure.confidentialledger._patch.ConfidentialLedgerClient object at 0x000001BAD31ED048>>
My code is as follow:
from azure.confidentialledger import ConfidentialLedgerClient
from azure.confidentialledger.certificate import ConfidentialLedgerCertificateClient
from azure.identity import DefaultAzureCredential
identity_client = ConfidentialLedgerCertificateClient()
network_identity = identity_client.get_ledger_identity(
ledger_id="MyLedger"
)
ledger_tls_cert_file_name = "ledger_certificate.pem"
with open(ledger_tls_cert_file_name, "w") as cert_file:
cert_file.write(network_identity["ledgerTlsCertificate"])
credential = DefaultAzureCredential()
ledger_client = ConfidentialLedgerClient(
endpoint="https://MyLedger.confidential-ledger.azure.com",
credential=credential,
ledger_certificate_path=ledger_tls_cert_file_name
)
d = ledger_client.list_ledger_entries
print(d)
In addition, I have read following questions:
How to print the value of the object?
Is there a built-in function to print all the current properties and values of an object?
Try this:
d = ledger_client.list_ledger_entries()
instead of this
d = ledger_client.list_ledger_entries
in your code, you did not assign the the result of the method call to the var d but instead assigned the method list_ledger_entries's object of ledger_client to d
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
I'm trying to create an ETL that extracts from mongo, process the data and loads into elastic. I will do a daily load so I thought of naming my index with the current date. This will help me for a later processing I need to do with this first index.
I used elasticsearch dsl guide: https://elasticsearch-dsl.readthedocs.io/en/latest/persistence.html
The problem that I have comes from my little experience with working with classes. I don't know how to reset the Index name from the class.
Here is my code for the class (custom_indices.py):
from elasticsearch_dsl import Document, Date, Integer, Keyword, Text
from elasticsearch_dsl.connections import connections
from elasticsearch_dsl import Search
import datetime
class News(Document):
title = Text(analyzer='standard', fields={'raw': Keyword()})
manual_tagging = Keyword()
class Index:
name = 'processed_news_'+datetime.datetime.now().strftime("%Y%m%d")
def save(self, ** kwargs):
return super(News, self).save(** kwargs)
def is_published(self):
return datetime.now() >= self.processed
And this is the part of the code where I create the instance to that class:
from custom_indices import News
import elasticsearch
import elasticsearch_dsl
from elasticsearch_dsl.connections import connections
import pandas as pd
import datetime
connections.create_connection(hosts=['localhost'])
News.init()
for index, doc in df.iterrows():
new_insert = News(meta={'id': doc.url_hashed},
title = doc.title,
manual_tagging = doc.customTags,
)
new_insert.save()
Every time I call the "News" class I would expect to have a new name. However, the name doesn't change even if I load the class again (from custom_indices import News). I know this is only a problem I have when testing but I'd like to know how to force that "reset". Actually, I originally wanted to change the name outside the class with this line right before the loop:
News.Index.name = "NEW_NAME"
However, that didn't work. I was still seeing the name defined on the class.
Could anyone please assist?
Many thanks!
PS: this must be just an object oriented programming issue. Apologies for my ignorance on the subject.
Maybe you could take advantage of the fact that Document.init() accepts an index keyword argument. If you want the index name to get set automatically, you could implement init() in the News class and call super().init(...) in your implementation.
A simplified example (python 3.x):
from elasticsearch_dsl import Document
from elasticsearch_dsl.connections import connections
import datetime
class News(Document):
#classmethod
def init(cls, index=None, using=None):
index_name = index or 'processed_news_' + datetime.datetime.now().strftime("%Y%m%d")
return super().init(index=index_name, using=using)
You can override the index when you call save() .
new_insert.save('processed_news_' + datetime.datetime.now().strftime("%Y%m%d"))
Example as following.
# coding: utf-8
import datetime
from elasticsearch_dsl import Keyword, Text, \
Index, Document, Date
from elasticsearch_dsl.connections import connections
HOST = "localhost:9200"
index_names = [
"foo-log-",
"bar-log-",
]
default_settings = {"number_of_shards": 4, "number_of_replicas": 1}
index_settings = {
"foo-log-": {
"number_of_shards": 40,
"number_of_replicas": 1
}
}
class LogDoc(Document):
level = Keyword(ignore_above=256)
date = Date(format="yyyy-MM-dd'T'HH:mm:ss.SSS")
hostname = Text(fields={'fields': Keyword(ignore_above=256)})
message = Text()
createTime = Date(format="yyyy-MM-dd'T'HH:mm:ss.SSS")
def auto_create_index():
'''自动创建ES索引'''
connections.create_connection(hosts=[HOST])
for day in range(3):
dt = datetime.datetime.now() + datetime.timedelta(days=day)
for index in index_names:
name = index + dt.strftime("%Y-%m-%d")
settings = index_settings.get(index, default_settings)
idx = Index(name=name)
idx.document(LogDoc)
idx.settings(**settings)
try:
idx.create()
except Exception as e:
print(e)
continue
print("create index %s" % name)
if __name__ == '__main__':
auto_create_index()
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.
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)