from attribute in json on python - python

I called an url that returns me a json object with an attribute called from. So when i want to get this in python i try:
object.from.username
$ python sample_app.py
File "sample_app.py", line 63
print comment.from.username
^
SyntaxError: invalid syntax

from is a python keyword and should not be used as an attribute.
Lucky for you, when using the json library that comes with python, JSON objects become dictionaries instead:
object['from']['username']
For situations where you actually do have a object with a python keyword used as an attribute, you'd have to resort to using the getattr() function instead:
frm = getattr(object, 'from')

Related

Is it possible to access builtins or any other useful functions through an Ellipsis object?

I have a challenge where I'm given a function where I can pass only a single argument which must be a builtin (no modules of any kind), for example chr or IndexError and use its attributes and call its functions to get access to other builtin types.
For example, if I choose the getattr function, I can access the builtins like this:
def main(a):
builtins = a(a, '__self__')
main(getattr)
Most other functions aren't of much help for my challenge. I know that the attributes are deep and a lot of information can be extracted.
This is a good reference: https://book.hacktricks.xyz/misc/basic-python/bypass-python-sandboxes
What can I get access to using an Ellipsis object, in Python written as ... ?
Subclasses can be accessed using ....__class__.__base__.__subclasses__() which returns a list and eventually get access back using a for loop to find which of those classes's __name__ attribute is catch-warnings, and that class's _module attribute has all the builtins (Code). I cannot use that because the index at which it will appear is always different
The python version I target is 3.9.

Calling class methods dinamically using Python [duplicate]

I would like to call an object method dynamically.
The variable "MethodWanted" contains the method I want to execute, the variable "ObjectToApply" contains the object.
My code so far is:
MethodWanted=".children()"
print eval(str(ObjectToApply)+MethodWanted)
But I get the following error:
exception executing script
File "<string>", line 1
<pos 164243664 childIndex: 6 lvl: 5>.children()
^
SyntaxError: invalid syntax
I also tried without str() wrapping the object, but then I get a "cant use + with str and object types" error.
When not dynamically, I can just execute this code to get the desired result:
ObjectToApply.children()
How to do that dynamically?
Methods are just attributes, so use getattr() to retrieve one dynamically:
MethodWanted = 'children'
getattr(ObjectToApply, MethodWanted)()
Note that the method name is children, not .children(). Don't confuse syntax with the name here. getattr() returns just the method object, you still need to call it (jusing ()).

How do I port this line from Python 2 to Python 3?

This is the line:
if type(tup) is types.TupleType:
Seriously, I'm lost. I tried Google without success. This is the error I get:
AttributeError: module 'types' has no attribute 'TupleType'
Thanks.
if type(tup) is tuple:
will work on both 2.7 and 3.x. types.TupleType was already an alias of the built-in tuple type in 2.x (tuple is types.TupleType returns True).
If you want to be more flexible and allow tuple subclasses, I'd suggest:
if isinstance(tup, tuple):
but that could conceivably change behavior (e.g. allowing namedtuples to pass the test, not just raw base class tuples).

Namespacing issue in Python

I am using a program where Python is the native scripting language. Unfortunately, they have a native function that uses the name bytes. This causes a problem when I am trying to use the actual bytes built-in function, and it thinks I am referencing that built-in variable. I will show you what I mean, one object as the following built-in code:
def receive(row, table, message, bytes):
#This is defined in the GUI
So, row, table, message, and bytes are all passed in as arguments, effectively overwriting the name bytes. So if I were to say bytes(something).decode() I get a TypeError: 'bytes' object is not callable
Is there any way to get out of this jam?
Use a different name for the fourth parameter (if you can change the signature of the function)
def receive(row, table, message, bytes_):
#This is defined in the GUI
Your problem is similar to this one. Just from builtins import bytes as _bytes; this will let you do _bytes(something).decode().
Although renaming the fourth argument is a better solution.

how to assign variable to module name in python function

I have a set of modules, and I want to be able to call one of them within a function based on an argument given to that function. I tried this, but it doesn't work:
from my.set import modules
def get_modules(sub_mod):
variable = sub_mod
mod_object = modules.variable
function(mod_object)
I get:
AttributeError: 'module' object has no attribute 'variable'
It's not taking the argument I give it, which would be the name of a module that exists under my.set.modules. so if I called the function get_modules(name_of_mod_under_modules), I would like the line modules.variable to be "modules.name_of_mod_under_modules" which I could then have as an object passed to mod_object.
In your current code, you're looking for modules.variable which doesn't exist, hence the error! That's not how you get an attribute of an object.
To achieve what you wanted, use the getattr function.
mod_object = getattr(modules, variable)

Categories