introspection

SQLAlchemy introspect column type with inheritance

我与影子孤独终老i 提交于 2019-12-04 00:09:27
Considering this code (and using SQLAlchemy 0.7.7): class Document(Base): __tablename__ = 'document' __table_args__ = { 'schema': 'app' } id = Column(types.Integer, primary_key=True) nom = Column(types.Unicode(256), nullable=False) date = Column(types.Date()) type_document = Column(types.Enum('arrete', 'photographie', name='TYPES_DOCUMENT_ENUM')) __mapper_args__ = {'polymorphic_on': type_document} class Arrete(Document): __tablename__ = 'arrete' __table_args__ = { 'schema': 'app' } __mapper_args__ = {'polymorphic_identity': 'arrete'} id = Column(types.Integer, ForeignKey('app.document.id'),

Is it possible to write to a python frame object as returned by sys._getframe() from python code running within the interpreter?

不羁的心 提交于 2019-12-03 21:39:16
Apropos of This question , there is a bit of scaffolding within the interpreter to inspect frame objects, which can be retrieved by sys._getframe() . The frame objects appear to be read only, but I can't find anything obvious in the docs that explicitly states this. Can someone confirm whether these objects are writeable (in some way) or read only? import sys def foobar(): xx='foo' ff = sys._getframe() ff.f_locals['xx'] = 'bar' print xx if __name__ == '__main__': foobar() This prints out ' foo ' when run but the post below demonstrates the variable being writable when run from the current

Actionscript 3 introspection — function names

江枫思渺然 提交于 2019-12-03 17:11:31
I am trying to iterate through each of the members of an object. For each member, I check to see if it is a function or not. If it is a function, I want to get the name of it and perform some logic based on the name of the function. I don't know if this is even possible though. Is it? Any tips? example: var mems: Object = getMemberNames(obj, true); for each(mem: Object in members) { if(!(mem is Function)) continue; var func: Function = Function(mem); //I want something like this: if(func.getName().startsWith("xxxx")) { func.call(...); } } I'm having a hard time finding much on doing this.

SQLAlchemy introspection of ORM classes/objects

自闭症网瘾萝莉.ら 提交于 2019-12-03 16:32:53
I am looking for a way to introspect SQLAlchemy ORM classes/entities to determine the types and other constraints (like maximum lengths) of an entity's properties. For example, if I have a declarative class: class User(Base): __tablename__ = "USER_TABLE" id = sa.Column(sa.types.Integer, primary_key=True) fullname = sa.Column(sa.types.String(100)) username = sa.Column(sa.types.String(20), nullable=False) password = sa.Column(sa.types.String(20), nullable=False) created_timestamp = sa.Column(sa.types.DateTime, nullable=False) I would want to be able to find out that the ' fullname ' field should

What are the arguments to the types.CodeType() python call?

家住魔仙堡 提交于 2019-12-03 16:01:12
问题 I'm currently trying to roll my own "marshal" code for python so i can store compiled python code on Google App Engine to serve scripts on a dynamic way. As you all can verify, "marshal" isn't supported on GAE and "pickle" can't serialize code objects. I found out i can construct a code object with types.CodeType() but it expects 12 arguments. As much as i've tried, i can't find any documentation on this call and i really need to construct the code object so i can exec() it. My question is,

Python introspection - how to check current module / line of call from within function

£可爱£侵袭症+ 提交于 2019-12-03 15:48:48
I have a function: # utils.py def hello(name='World'): # Detect where I'm being called from. print('Hi, %s. You called this from %s at line # %d.' % (name, mod, lineno)) # ``mod`` and ``lineno`` on previous line would have been set in real use. I import that function and run it elsewhere # other.py (this comment at line # 138) from utils import hello hello('Johnny') # From inside ``hello`` I want to be able to detect that this # was called from other.py at line # 140 Access the enclosing frame of inspect.currentframe() : import inspect def hello(name='World'): f = inspect.currentframe().f_back

Inferring Spark DataType from string literals

时光怂恿深爱的人放手 提交于 2019-12-03 12:57:43
问题 I am trying to write a Scala function that can infer Spark DataTypes based on a provided input string: /** * Example: * ======== * toSparkType("string") => StringType * toSparkType("boolean") => BooleanType * toSparkType("date") => DateType * etc. */ def toSparkType(inputType : String) : DataType = { var dt : DataType = null if(matchesStringRegex(inputType)) { dt = StringType } else if(matchesBooleanRegex(inputType)) { dt = BooleanType } else if(matchesDateRegex(inputType)) { dt = DateType }

How to detect at runtime whether symbols are stripped?

回眸只為那壹抹淺笑 提交于 2019-12-03 12:35:22
In my C++ program, how can I detect programmatically at runtime whether symbols have been stripped via the 'strip' gnu development tool on Linux? I'd like a function definition which returns true if stripped, otherwise false. Would using dlsym() on "main()" work to detect this reliably? I know the file command can tell the difference, so you could possibly look at its source to see what mechanism it uses. jschmier From a comment left for another answer : A stripped ELF will lack a .symtab entry. The file command traverses through all the ELF section headers until a symbol table section is

How does the “#map(&proc)” idiom work when introspecting module classes?

*爱你&永不变心* 提交于 2019-12-03 10:48:47
Presenting the Idiom I found an interesting but unexplained alternative to an accepted answer. The code clearly works in the REPL. For example: module Foo class Bar def baz end end end Foo.constants.map(&Foo.method(:const_get)).grep(Class) => [Foo::Bar] However, I don't fully understand the idiom in use here. In particular, I don't understand the use of &Foo , which seems to be some sort of closure, or how this specific invocation of #grep operates on the result. Parsing the Idiom So far, I've been able to parse bits and pieces of this, but I'm not really seeing how it all fits together. Here

Python introspection: access function name and docstring inside function definition

╄→尐↘猪︶ㄣ 提交于 2019-12-03 10:34:44
Consider the following python code: def function(): "Docstring" name = ??? doc = ??? return name, doc >>> function() "function", "Docstring" What do I need to replace the question marks with so that I get the name and the docstring of the function from inside the same function? EDIT: Most of the answers so far explicitly hardcode the name of the function inside its definition. Is it possible do something like below where a new function get_name_doc would access the function from the outer frame from which it is called, and return its name and doc? def get_name_doc(): ??? def function():