I have these lines in Python:
page = lxml.html.parse(URL).getroot()
table = only(page.cssselect('table[width=510]'))
What is the only method doing? I can't find it in the Python docs (though that might just be because it's very hard to search for!)
thanks.
There is no only built-in function, as you'll see if you type help(only) into your Python interpreter.
It must be pulled into the namespace with a from <module> import <only|*> instruction in that module. When you find this, you could try importing the module in your Python interpreter and using the help function again to find out what it does.
You can try to figure out which module only is defined in by examining the import statements in your file. Then look up only in the docs for that module. Or just put a print only.__module__ in your code and that might print out the module.
AFAIK, it isn't standard. But I'd guess that it fetches the only element from its input, or raises an exception if the input doesn't have exactly one element. E.g.:
>>> def only(input):
... [result] = input
... return result
...
>>> only([12])
12
>>> only([])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in only
ValueError: need more than 0 values to unpack
>>> only([23, 34])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in only
ValueError: too many values to unpack
>>>
only is not a built-in function. Is it defined or imported anywhere in the .py file that you have? If not, look for from somemodule import *, and then look in each occurrence of somemodule.
Related
I want to add functions to python's built in module. As a example, there is no find function in list. I want to add it. There is some other functions I want to add. Can somebody please tell me how can I do that? Is their any way I can do that?
Adding functions to the builtins module is simple:
import builtins
builtins.find = my_find_function
But the built in classes are usually immutable, so they cannot be changed.
>>> list.hello = "hello"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type 'list'
I know we can import modules and just embed Python code in C++ and evaluate it. But how can I use built-in functions like print or open? These functions off course aren't module. Evaluating embedded open statement just gives me the following error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'open' is not defined
Stuck. Please help me.
Try importing the builtins and io module and if you want any other function just call the __module__ attribute to find about which module to import
>>> print.__module__
'builtins'
>>> open.__module__
'io'
Looking to read a directory of sql queries in separate files into a single file
I'm a beginner in python but I figured this task would be an easy enough start.
EQ = open("EnmaxQueries.SQL","a+")
>>> for file in dir:
... with open(file,"r") as reader:
... EQ.write(reader.read())
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'builtin_function_or_method' object is not iterable
What does the error refer to? Usually it appear to referring to a function rather than its output but I thought my function calls all used ().
Should I be approching this in another way? Have I made an error in scope of the loop or faile dto execute the multi line block properly or something?
EDIT:dir was defined by os.listdir and this refers to the method listdir and not it's output as is typical for this error.
dir was not defined by you and is defaulting to the python dir() function that lists members of python objects.
Maybe you want to have a look at os.listdir.
Something I'm doing in PIL is giving me back an object of a class I don't recognize. It may be a fairly thin wrapper around a C data structure or something. How can I get Python to tell me where to look for more information?
Here are some failed attempts to use introspection to learn more:
>>> import os, PIL
>>> obj = PIL.Image.open(os.path.expanduser("~/Desktop/foo.png")).getdata()
>>> type(obj)
<type 'ImagingCore'>
>>> ImagingCore
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'ImagingCore' is not defined
>>> PIL.ImagingCore
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'ImagingCore'
>>> obj.__class__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: __class__
>>> obj.__module__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: __module__
>>> import inspect
>>> inspect.getsource(obj)
...
TypeError: <ImagingCore object at 0x105e64b50> is not a module, class, method, function, traceback, frame, or code object
>>> inspect.getsource(type(obj))
...TypeError: <module '__builtin__' (built-in)> is a built-in class
>>>
PIL's core functionality is implemented in module _imaging, written, as you guessed, in the C language -- see _imaging.c (3281 lines...:-) in the top level source directory, Imaging-1.1.7. That code doesn't pay any attention to introspection -- rather, it's 100% focused on performance. I believe it doesn't even bother to expose anything but functions (it does implement several types, including ImagingCore, but doesn't even expose those types' names to Python -- only generates and uses them internally).
So Python won't tell you where to look for more info because the library, in turn, doensn't tell Python:-). As the docs for getdata at http://effbot.org/imagingbook/image.htm says:
Note that the sequence object returned by this method is an internal
PIL data type, which only supports certain sequence operations,
including iteration and basic sequence access. To convert it to an
ordinary sequence (e.g. for printing), use list(im.getdata())
...and "that's all she wrote" -- nothing but that small subset of sequence operations gets exposed.
I am new to Python,
and have started working on code written by others.
In the source of packages downloaded from Pypi I have noticed the use of
import __module__
to use functions and classes defined in the src folder of packages.
Is this common practice? I actually can't really understand this kind of syntax,
could you explain it to me or send me to some reference?
It's a python convention for some builtin objects. From PEP8:
__double_leading_and_trailing_underscore__: "magic" objects or attributes that live in user-controlled namespaces. E.g. __init__, __import__ or __file__. Never invent such names; only use them as documented.
But in the end it is not a "kind of syntax" to understand or not, __module__ is just a name with underscores in it. It's different from and completely unrelated to module.
An example to show you it's just a name:
>>> __foo__ = 42
>>> print foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'foo' is not defined
>>> print _foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '_foo' is not defined
>>> print __foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '__foo' is not defined
>>> print __foo__
42
>>> type(__foo__)
<type 'int'>
Nothing inherently special about it.
Without more info about where you saw this though, it's hard to say what the author's intention was. Either they are importing some python builtins (e.g. from __future__ import...) or they are ignoring PEP8 and just named some objects in this style because they think it looks cool or more important.