zeroSteiner / rule-engine

A lightweight, optionally typed expression language with a custom grammar for matching arbitrary Python objects.
https://zerosteiner.github.io/rule-engine/
BSD 3-Clause "New" or "Revised" License
433 stars 54 forks source link

No attribute Rule #88

Closed huuhuannt1998 closed 3 months ago

huuhuannt1998 commented 3 months ago

I have trying the following example:

import rule_engine
import datetime

comics = [
  {
    'title': 'Batman',
    'publisher': 'DC',
    'issue': 89,
    'released': datetime.date(2020, 4, 28)
  },
  {
    'title': 'Flash',
    'publisher': 'DC',
    'issue': 753,
    'released': datetime.date(2020, 5, 5)
  },
  {
    'title': 'Captain Marvel',
    'publisher': 'Marvel',
    'issue': 18,
    'released': datetime.date(2020, 5, 6)
  }
]

rule = rule_engine.Rule(
  # match books published by DC
  'publisher == "DC"'
)

# check if the first object matches
print(rule.matches(comics[0])) # => True

# filter the iterable "comics" and return matching objects
print(rule.filter(comics)) # => <generator object Rule.filter at 0x7f2bdafbe650>

I got this error "AttributeError: partially initialized module 'rule_engine' has no attribute 'Rule' (most likely due to a circular import)". How do I get this fixed?

zeroSteiner commented 3 months ago

I can't reproduce that error. You're not naming your Python source file rule_engine.py are you?

huuhuannt1998 commented 3 months ago

That is the name of my python file.

On Mon, Apr 8, 2024 at 13:25 Spencer McIntyre @.***> wrote:

I can't reproduce that error. You're not naming your Python source file rule_engine.py are you?

— Reply to this email directly, view it on GitHub https://github.com/zeroSteiner/rule-engine/issues/88#issuecomment-2043285531, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALOGWQB46QMLGCFMW5PGI7LY4LHHNAVCNFSM6AAAAABF5C3YAKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANBTGI4DKNJTGE . You are receiving this because you authored the thread.Message ID: @.***>

zeroSteiner commented 3 months ago

Try changing it to anything else. You're defining the rule_engine module yourself and not defining the Rule class. Rename it to test.py or something else.

huuhuannt1998 commented 3 months ago

Try changing it to anything else. You're defining the rule_engine module yourself and not defining the Rule class. Rename it to test.py or something else.

It works. Thanks for your help.