lihaoyi / macropy

Macros in Python: quasiquotes, case classes, LINQ and more!
3.28k stars 178 forks source link

NameError: name 'case' is not defined #30

Closed mbrezu closed 11 years ago

mbrezu commented 11 years ago

I installed macropy from git (git clone, python setup.py install).

I go to a python shell and try the case classes example.

Python 2.7.4 (default, Apr 19 2013, 18:32:33) 
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from macropy.macros.adt import macros, case
0=[]=====> MacroPy Enabled <=====[]=0
>>> @case
... class Point(x, y): pass
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'case' is not defined

I'm probably missing something obvious :-)

jnhnum1 commented 11 years ago

Because MacroPy modifies the import process, you need to import macropy.core.macros (as in the REPL example in the readme) before you import specific macros.

so

>>> import macropy.core.macros
0=[]=====> MacroPy Enabled <=====[]=0
>>> from macropy.macros.adt import macros, case
>>> @case
... class Point(x, y): pass
>>>

should work fine

mbrezu commented 11 years ago

Thanks!

Maybe adding that extra line to the examples would be useful. Or add a 'getting started' section, which includes all the necessary lines, so people who only want to spend 10 minutes trying out macropy (like me) only have to copy-paste a few lines and be instantly convinced to spend more time.

Macropy looks like a great library, keep up the good work!

lihaoyi commented 11 years ago

It's already in the Rough Overview and Detailed Guide for the non-REPL macros; maybe I should add a reminder for those using the REPL

mbrezu commented 11 years ago

A suggestion: people in a hurry scan for 'Getting Started', 'Quick start' and similar keywords. Starting the documentation with a small, documented REPL session showing case classes (and maybe a bit of pattern matching) should help. Or at least provide a link to such a document at the start of the README.

Also, the 'Rough overview' starts by showing how to define macros. IMHO it's better to start with a few examples of using existing macros. Some people (e.g. me) will start by using the existing macros and later (if ever) be interested in defining their own.

Thanks!

lihaoyi commented 11 years ago

Thanks for your suggestion; you are entirely right! I will find some time to shuffle it around.

lihaoyi commented 11 years ago

Made the top "banner" example into an executable REPL session, and linked to the examples which also have nice-to-use REPL sessions

AndreaCrotti commented 10 years ago

Hi everyone, I am playing around with Macropy and I have the same issue described here earlier, I copied and pasted the example (adding the import you mentioned) and it still fails with python 2.7.8

import macropy.core.macros
from macropy.case_classes import macros, case

@case
class Point(x, y):
    pass

p = Point(1, 2)

print str(p) # Point(1, 2)
print p.x    # 1
print p.y    # 2
print Point(1, 2) == Point(1, 2) # True
x, y = p
print x, y   # (1, 2)

Any idea about what the problem might be?

It seems like the macropy is never enabled because I also don't see that message.. thanks

pigmej commented 10 years ago

It's not that you need import, you need to import the file with macros, not the macropy itself

So if the file a.py contains macros, then you need to create b.py and import a. that's all.