EzDevInfo.com

macropy

Macros in Python: quasiquotes, case classes, LINQ and more!

Best way to define algebraic data types in Python?

I know that Python is NOT Haskell or Ocaml, but which is the best way to define algebraic data types in Python (2 or 3)? Thanks!


Source: (StackOverflow)

Adding Macros to Python

I would like to invoke the following code in-situ wherever I refer to MY_MACRO in my code below.

# MY_MACRO
frameinfo = getframeinfo(currentframe())
msg = 'We are on file ' + frameinfo.filename + ' and line ' +  str(frameinfo.lineno)
# Assumes access to namespace and the variables in which `MY_MACRO` is called. 
current_state = locals().items()

Here is some code that would use MY_MACRO:

def some_function:
    MY_MACRO

def some_other_function:
    some_function()
    MY_MACRO

class some_class:
  def some_method:
     MY_MACRO

In case it helps:

  1. One of the reasons why I would like to have this ability is because I would like to avoid repeating the code of MY_MACRO wherever I need it. Having something short and easy would be very helpful.
  2. Another reason is because I want to embed an IPython shell wihthin the macro and I would like to have access to all variables in locals().items() (see this other question)

Is this at all possible in Python? What would be the easiest way to get this to work?

Please NOTE that the macro assumes access to the entire namespace of the scope in which it's called (i.e. merely placing the code MY_MACRO in a function would not work). Note also that if I place MY_MACRO in a function, lineno would output the wrong line number.


Source: (StackOverflow)

Advertisements

MacroPy installation fails

Trying to install MacroPy with pip on Python 3.4.3, but I get an arror:

$ pip3 install MacroPy

Collecting MacroPy
  Downloading MacroPy-1.0.3.zip
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 20, in <module>
      File "/private/var/folders/hq/r_l8wk257_71w4rtrkj5b7d80000gn/T/pip-build-10_id3bj/MacroPy/setup.py", line 62, in <module>
        from macropy import __version__
      File "/private/var/folders/hq/r_l8wk257_71w4rtrkj5b7d80000gn/T/pip-build-10_id3bj/MacroPy/macropy/__init__.py", line 23, in <module>
        import core.exporters
    ImportError: No module named 'core'

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/hq/r_l8wk257_71w4rtrkj5b7d80000gn/T/pip-build-10_id3bj/MacroPy

System is OSX 10.10.3, with Python from HomeBrew.

Edit: installation on Python 2.x works fine.


Source: (StackOverflow)

Can literals in Python be overridden?

Couldn't find a way to phrase the title better, feel free to correct.

I'm pretty new to Python, currently experimenting with the language.. I've noticed that all built-ins types cannot be extended with other members.. I'd like for example to add an each method to the list type, but that would be impossible. I realize that it's designed that way for efficiency reasons, and that most of the built-ins types are implemented in C.

Well, one why I found to override this behavior is be defining a new class, which extends list but otherwise does nothing. Then I can assign the variable list to that new class, and each time I would like to instantiate a new list, I'd use the list constructor, like it would have been used to create the original list type.

class MyList(list):
    def each(self, func):
       for item in self:
           func(item)
list = MyList

my_list = list((1,2,3,4))
my_list.each(lambda x: print(x))

Output:

1
2
3
4

The idea can be generalize of course by defining a method that gets a built it type and returns a class that extends that type. Further more, the original list variable can be saved in another variable to keep an access to it.

Only problem I'm facing right now, is that when you instantiate a list by its literal form, (i.e. [1,2,3,4]), it will still use the original list constructor (or does it?). Is there a way to override this behavior? If the answer is no, do you know of some other way of enabling the user to extend the built-ins types? (just like javascript allows extending built-ins prototypes).

I find this limitation of built-ins (being unable to add members to them) one of Python's drawbacks, making it inconsistent with other user-defined types... Overall I really love the language, and I really don't understand why this limitation is REALLY necessary.


Source: (StackOverflow)

functional pipes in python like %>% from dplyr

In R (thanks to dplyr) you can now perform operations with a more functional piping syntax via %>%. This means that instead of coding this:

> as.Date("2014-01-01")
> as.character((sqrt(12)^2)

You could also do this:

> "2014-01-01" %>% as.Date 
> 12 %>% sqrt %>% .^2 %>% as.character

To me this is more readable and this extends to use cases beyond the dataframe. Does the python language have support for something similar?


Source: (StackOverflow)

Macros in python

in my project I have to repeat often such part of code:

class SimplePhysicObject(Object):
    def __init__(self):
        super(Object, self).__init__('SimplePhysicObject')

But instead of SimplePhysicObject there is new string each time. Are there any ways to write some macro to make this work easier? Something like:

DoTemplate(NewObject)
==>
class NewObject(Object):
    def __init__(self):
        super(Object, self).__init__('NewObject')

UPD Sorry, Object is my own class declared before in code


Source: (StackOverflow)