laike9m / pdir2

Pretty dir() printing with joy:beer:
MIT License
1.32k stars 47 forks source link

Add attribute filters #37

Closed rndev-io closed 6 years ago

rndev-io commented 6 years ago

Very useful thing - filter attribute by types, e.g public, private, property, magic, not inherited.

Some examples:

pdir(obj).public()
pdir(obj).not_inherited()
pdir(obj).magic()
pdir(obj).model_fields()
laike9m commented 6 years ago

I agree that would be useful. What's the most common use case for you if this is added?

rndev-io commented 6 years ago
  1. show public methods/properties
  2. show not inherited methods/properties, for example when you inspect django models.
  3. show methods only.
laike9m commented 6 years ago

I've started working on this, and found an interesting phenomenon that dir() a Django model class does not give me all fields, which affects pdir(because I use results of dir() as input source for pdir). So I decide to first investigate a bit more on this.

Looks like an intended behavior of Django 1.8.x. In Django 2.x this issue no longer exists.

laike9m commented 6 years ago

@rusnasonov Basic functionality has been added. Docs are yet to be added. Currently for a model

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    category = models.CharField(max_length=30)
    description = models.TextField(blank=True)

    def was_published_recently(self):
        now = timezone.now()
        return now - datetime.timedelta(days=1) <= self.pub_date <= now
    was_published_recently.admin_order_field = 'pub_date'
    was_published_recently.boolean = True
    was_published_recently.short_description = 'Published recently?'

    def __str__(self):
        return self.question_text

result is

>>> pdir(Question).own.properties.public
descriptor:
    category: class DeferredAttribute with getter, A wrapper for a deferred-loading field. When the value is read from this
    choice_set: class ReverseManyToOneDescriptor with getter, setter, Accessor to the related objects manager on the reverse side of a
    description: class DeferredAttribute with getter, A wrapper for a deferred-loading field. When the value is read from this
    get_next_by_pub_date: class partialmethod with getter, Method descriptor with partial application of the given arguments
    get_previous_by_pub_date: class partialmethod with getter, Method descriptor with partial application of the given arguments
    id: class DeferredAttribute with getter, A wrapper for a deferred-loading field. When the value is read from this
    objects: class ManagerDescriptor with getter
    pub_date: class DeferredAttribute with getter, A wrapper for a deferred-loading field. When the value is read from this
    question_text: class DeferredAttribute with getter, A wrapper for a deferred-loading field. When the value is read from this

Django adds some properties like get_next_by_pub_date, objects. I'm not that familiar with Django, and I don't want to add some very specific rules for Django. So unless there's a nice&general way to filter out the added properties(hopefully you can teach me how), this will be the final form.

Later I'll probably change the group's name from descriptor to something else(maybe like descriptor property), but that's another problem.

laike9m commented 6 years ago

Fixed in 0.3.0

rndev-io commented 6 years ago

Cool, thanks!