Issue
i'm stucked at a point and i can't progress, sorry for this silly question. I searched a lot for that but i couldn't know what i am missing. Please help me.
I studied modules and classes in python. Now i want to make some operations using python and apt. I'm studying from : http://apt.alioth.debian.org/python-apt-doc/library/apt.cache.html However, i couldn't understand, the module is apt.cache, as shown in the top of the page. I expected that object should be created by writing apt.cache.Cache(), but object is created by writing apt.Cache(), as shown below. Why?
import apt
import apt.progress
# First of all, open the cache
cache = apt.Cache()
# Now, lets update the package list
cache.update()
# We need to re-open the cache because it needs to read the package list
cache.open(None)
# Now we can do the same as 'apt-get upgrade' does
cache.upgrade()
# or we can play 'apt-get dist-upgrade'
cache.upgrade(True)
# Q: Why does nothing happen?
# A: You forgot to call commit()!
cache.commit(apt.progress.TextFetchProgress(),
apt.progress.InstallProgress())
Second similar question is about below code, Cache class is imported from module apt.cache. I expected that object would be created by writing apt.cache.Cache(), but it is created by writing apt.Cache(). Why?
>>> from apt.cache import FilteredCache, Cache, MarkedChangesFilter
>>> cache = apt.Cache()
>>> changed = apt.FilteredCache(cache)
>>> changed.set_filter(MarkedChangesFilter())
>>> print len(changed) == len(cache.get_changes()) # Both need to have same length
True
Thanks in advance
Solution
If you look at the __init__.py
file of the apt package, you see the line:
__all__ = ['Cache', 'Cdrom', 'Package']
The python documentation says:
The import statement uses the following convention: if a package’s __ init__.py code defines a list named all, it is taken to be the list of module names that should be imported when from package import * is encountered.
That's the reason why you can use apt.Cache()
For the second part of your question you can import directly the Cache class with
from apt.cache import Cache
cache = Cache()
You can also import the Cache class with
import apt
cache = apt.Cache() //because of the __all__ variable in __init__.py
cache = apt.cache.Cache() //because it's a fully qualified name
Answered By - Ortomala Lokni