New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add attribute filters #37

Closed
rusnasonov opened this Issue Jan 30, 2018 · 6 comments

Comments

Projects
None yet
2 participants
@rusnasonov

rusnasonov commented Jan 30, 2018

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

This comment has been minimized.

Owner

laike9m commented Jan 30, 2018

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

@rusnasonov

This comment has been minimized.

rusnasonov commented Jan 30, 2018

  1. show public methods/properties
  2. show not inherited methods/properties, for example when you inspect django models.
  3. show methods only.
@laike9m

This comment has been minimized.

Owner

laike9m commented Feb 4, 2018

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

This comment has been minimized.

Owner

laike9m commented Feb 9, 2018

@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

This comment has been minimized.

Owner

laike9m commented Feb 10, 2018

Fixed in 0.3.0

@laike9m laike9m closed this Feb 10, 2018

@rusnasonov

This comment has been minimized.

rusnasonov commented Feb 11, 2018

Cool, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment