1

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

metis
  • 1,024
  • 2
  • 10
  • 26
  • 1
    Check apt/__init__.py file - maybe it is an import alias? – Mikko Ohtamaa Mar 09 '15 at 01:13
  • There is nothing special in this file. Check that file from there : https://github.com/jolicloud/python-apt/blob/master/apt/__init__.py – metis Mar 09 '15 at 01:19
  • 1
    See line https://github.com/jolicloud/python-apt/blob/master/apt/__init__.py#L26 and `__all__` exports. Further reading: http://stackoverflow.com/questions/44834/can-someone-explain-all-in-python – Mikko Ohtamaa Mar 09 '15 at 01:55
  • @MikkoOhtamaa , OK. I understand why apt.Cache is used in the first code i mentioned. However, in the second code, there are 2 mistakes i think. second and third rows are wrong, and they should be : cache=Cache() and changed = FilteredCache(cache) , it think. Can you please check that rows whether i'm right or not? – metis Mar 09 '15 at 11:49

1 Answers1

1

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
Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240