All Questions

Tagged with
Filter by
Sorted by
Tagged with
127 votes
4 answers
195k views

Jinja2 inline comments

How can I put comments inside Jinja2 argument list declaration ? Everything I have tried gives an error: jinja2.exceptions.TemplateSyntaxError: unexpected char u'#' {{ Switch('var', [('1', 'foo')...
kimstik's user avatar
  • 1,729
71 votes
2 answers
19k views

*args, **kwargs in jinja2 macros

How are extra args & kwargs handled for a Jinja2 macro? The documentation isn't exactly clear offhand. For example, this is clearly wrong: {% macro example_1(one, two, **kwargs) %} do macro ...
blueblank's user avatar
  • 4,814
37 votes
8 answers
9k views

What can you do with Lisp macros that you can't do with first-class functions?

I think I understand Lisp macros and their role in the compilation phase. But in Python, you can pass a function into another function def f(filename, g): try: ...
Joshua Fox's user avatar
  • 19.1k
29 votes
5 answers
30k views

How to run python macros in LibreOffice?

When I go to Tools -> Macros -> Organize Macros -> Python I get this dialog: It is not possible to create new Python macros. Apparently LibreOffice has no Python editor so I have to write the macros ...
Martin Drozdik's user avatar
21 votes
16 answers
16k views

Python Macros: Use Cases?

If Python had a macro facility similar to Lisp/Scheme (something like MetaPython), how would you use it? If you are a Lisp/Scheme programmer, what sorts of things do you use macros for (other than ...
Rick Copeland's user avatar
20 votes
6 answers
18k views

Macros in django templates

In jinja I can create macros and call it in my template like this: {% macro create_list(some_list) %} <ul> {% for item in some_list %} <li>{{ item }}</li> {% endfor %} &...
Dima  Kudosh's user avatar
  • 7,216
20 votes
9 answers
5k views

Pythonic macro syntax

I've been working on an alternative compiler front-end for Python where all syntax is parsed via macros. I'm finally to the point with its development that I can start work on a superset of the ...
Serafina Brocious's user avatar
16 votes
3 answers
15k views

Save a function definition in ipython

When using ipython I often want to save specific functions I have defined during my session, e.g.: In [1]: def func1(): ...: pass ...: In [2]: %save func1.py func1 func1 is neither a string ...
cmh's user avatar
  • 10.7k
16 votes
6 answers
4k views

Creating LaTeX math macros within Sphinx

I'm writing some mathematical code in Python and using Sphinx to produce the documentation. I know that Sphinx can handle LaTeX code in Python docstrings; see https://www.sphinx-doc.org/en/master/...
araichev's user avatar
  • 778
14 votes
2 answers
30k views

what language do I need to write macros in LIbre Office Calc? [closed]

I've written a bunch of VBA code for various things in Excel. I'm looking at migrating to libreOffice. Under Tool->Macros->Organize Macros: the two choices are LibreOffice Basic and Python. Should ...
DBWeinstein's user avatar
  • 9,057
14 votes
4 answers
26k views

C Preprocessor Macro equivalent for Python

I use to define macros (not just constants) in C like #define loop(i,a,b) for(i=a; i<b; ++i) #define long_f(a,b,c) (a*0.123 + a*b*5.6 - 0.235*c + 7.23*c - 5*a*a + 1.5) Is there a way of doing ...
Carlos Pinzón's user avatar
11 votes
2 answers
3k views

How to write an ipython alias which executes in python instead of shell?

We can define an alias in ipython with the %alias magic function, like this: >>> d NameError: name 'd' is not defined >>> %alias d date >>> d Fri May 15 00:12:20 AEST 2015 ...
wim's user avatar
  • 349k
8 votes
1 answer
723 views

How is the at sign (@) used in the c source code for numpy?

I've been perusing some of the source code for numpy and I noticed that a lot of the c source code uses the construction @variablename@. For example, in the file "npy_math_complex.c.src" (located ...
FormerPhysicist's user avatar
8 votes
1 answer
4k views

OpenOffice Python macros: Where can I find useful documentation?

I'm trying to make a macro for OpenOffice Calc that will toggle the background color of cells containing a user-specified value. Since I don't know OpenOffice Basic and have no desire to learn it, I'd ...
Scott Severance's user avatar
7 votes
2 answers
3k views

PyCharm: Run `black -S` on region

We are not ready to automatically format the whole source code with black. But from time to time I would like to execute black -S on a region via PyCharm. There is a hint in the docs how to run black (...
guettli's user avatar
  • 27.2k
7 votes
1 answer
896 views

try..catch macro wrapper equivalent in cython

I'm wrapping a large number of C++ functions that can raise an exception if the underlying socket connection is lost. While I have figured out how to wrap my "get connection" function to re-establish ...
jdi's user avatar
  • 91.6k
6 votes
2 answers
9k views

Jinja2: saying 'Render this macro inside another macro or template'

I have a number of macros I'd like to render within a common container macro or template. With pseudo-code: Macro1 Macro2 Macro3 Container In a template: "render macro1 inside of Container" e.g. ...
blueblank's user avatar
  • 4,814
6 votes
1 answer
8k views

How to run a MS Access macro from python

I'm trying to run an existing Microsoft Access Macro from a python script. What I currently have is import win32api,time from win32com.client import Dispatch strDbName = 'Exit.mdb' objAccess = ...
paldives's user avatar
  • 125
6 votes
2 answers
4k views

entering context managers in __enter__

with context managers defined as functions, it's easy to programmatically enter a separate (or recursive) context manager from within one, like so: @contextmanager def enter(times): if times: ...
ben w's user avatar
  • 2,536
5 votes
2 answers
917 views

C++ macro in scope of if statement not compiling

I have some code which is supposed to be a thread-safe python/c++ api. I am using the macros Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS, which expand to create save thread state and create a lock....
errolflynn's user avatar
5 votes
1 answer
2k views

Generating Python code with Hy macros

I am trying to generate some python code from Hy. How is that done better? I have tried several approaches. One is with a macro: (defmacro make-vars [data] (setv res '()) (for [element data] ...
Srv19's user avatar
  • 3,538
5 votes
2 answers
3k views

How to render a Jinja2 macro from a view callable?

I have some macros defined that are called from several templates. For example, the Product page has a Review section that uses the macros defined in 'helpers/review.jinja2' to print each review. The ...
Marco Bruggmann's user avatar
4 votes
7 answers
8k views

Is there a way to convert code to a string and vice versa in Python?

The original question was: Is there a way to declare macros in Python as they are declared in C: #define OBJWITHSIZE(_x) (sizeof _x)/(sizeof _x[0]) Here's what I'm trying to find out: Is there a ...
user avatar
4 votes
3 answers
8k views

How do I use side mouse buttons with Pynput?

I am using the Python module Pynput to make a macro that will press one of my side buttons. Does anyone know what the side buttons are called in Pynput? For example: from pynput.mouse import Button, ...
Kamepoh2's user avatar
4 votes
1 answer
3k views

Python Jinja2 call to macro results in (undesirable) newline

My JINJA2 template is like below. {% macro print_if_john(name) -%} {% if name == 'John' -%} Hi John {%- endif %} {%- endmacro %} Hello World! {{print_if_john('Foo')}} {{print_if_john('Foo2')}} {{...
Rags Rachamadugu's user avatar
4 votes
2 answers
2k views

How to configure PyCharm to develop LibreOffice Python macros?

I've installed Python 3.5.1 as default in Win7(x64) for all my projects in Python. I use PyCharm 5.0.5 community edition for develop Python scripts and its default settings has "Default Project ...
Trimax's user avatar
  • 2,433
4 votes
2 answers
890 views

Pythonic way to intercept calls to a function?

For testing things that query the environment (e.g., os.getenv, sys.version, etc.), it's often more convenient to make the queries lie than to actually fake up the environment. Here's a context ...
Wang's user avatar
  • 3,287
4 votes
1 answer
1k views

SWIG with preprocessor macro from boost preprocessor

I'm utilizing the enum with ToString implementation that was suggested here: How to convert an enum type variable to a string? It utilizes and works fine as far as I can tell. My issues arise when ...
Matthias Kauer's user avatar
4 votes
3 answers
108 views

Rearrange order of for-loops with setting

I'm working with arrays having the following kind of structure/entries (for a masters project in quantum info games); The 1st column entries {0,1}, 2nd col {0,1}, 3rd col {0,2**(d-1)} , last col {0,d-...
balletpiraat's user avatar
4 votes
0 answers
2k views

Flask/Jinja2: Macro calling macro with context

I have a template that contains some macros: {% macro input(id, editable=current_user.is_active) %} ... some content ... {% endmacro %} {% macro the_form() %} {{input('id1')}} {{input('id2', ...
Tom's user avatar
  • 7,510
3 votes
6 answers
2k views

Partial evaluation for parsing

I'm working on a macro system for Python (as discussed here) and one of the things I've been considering are units of measure. Although units of measure could be implemented without macros or via ...
Serafina Brocious's user avatar
3 votes
2 answers
4k views

AIRFLOW: use .replace() or relativedelta() in jinja template for {{ds}}

My goal is to return 1st day of previous month based on airflow macro variable {{ds}} and use it e.g. in HiveOperator. E.g. For ds = 2020-05-09 I expect to return: 2020-04-01 The solution I found and ...
Mateusz Konopelski's user avatar
3 votes
2 answers
10k views

Copy .csv files into a .xlsx workbook using python

I have done extensive researches, however have not been able to come up with the right solution for my needs. I have python script which generates multiple .csv files. Each .csv file only has data ...
stu6000's user avatar
  • 33
3 votes
1 answer
3k views

Python one button macro for games

Here is my Python 2.7.13 script that is basically a so called "one key macro" for a video game. Based on information captured from a part of the game screen it presses the right key combinations ...
Norbert's user avatar
  • 39
3 votes
2 answers
2k views

LibreOffice - How to create a file dialog via python macro?

I'd like to know if it's possible to create a standard file dialog to save a pdf via a python macro. I've tried to write some code based on this outdated documentation: wiki.openoffice.org but ...
p2or's user avatar
  • 339
3 votes
3 answers
128 views

Is it possible to create named functions in Python similar to C macro?

Sorry for the confusing title. I would like to do the following: (Similar to defstruct in Lisp) def mkstruct(structname, field_dict): # create a function called "structname" and get/set ...
dividebyzero's user avatar
  • 1,253
3 votes
2 answers
633 views

Python macros on Mac using Komodo

I'm using Komodo IDE, version 6.1.1, build 61234, platform macosx-x86. I'm also using Python 3.2 and Python 2.7. I've just moved to using (my first) Mac. OS version 10.6.? On my Windows machines I ...
debragail's user avatar
  • 149
3 votes
1 answer
2k views

Python Jinja2 macro whitespace issues

This is kind of extension to my other question Python Jinja2 call to macro results in (undesirable) newline. My python program is import jinja2 template_env = jinja2.Environment(trim_blocks=True, ...
Rags Rachamadugu's user avatar
3 votes
1 answer
342 views

Quickly import custom SPSS commands from Python

I have written a neat Python module that contains some custom SPSS functions for my coworkers to use. However, to use the functionality, they first need to type BEGIN PROGRAM. Import module etc... ...
Sander's user avatar
  • 409
3 votes
1 answer
2k views

How to use m4 with Python and dealing with indendentation and whitespace

Is there a good general strategy for using m4 with Python? Specifically, Python's whitespace requirements make using m4 somewhat awkward. For example, the following program: def foo(): pushdef(`...
wyer33's user avatar
  • 6,210
3 votes
0 answers
2k views

Jinja 2 block inside macro?

I have base template that contains macro like this {%- macro local_vars(d) -%} {%- block local_vars %} {% endblock -%} {%- endmacro -%} So I would like to override this block in the child template ...
Alen's user avatar
  • 55
3 votes
1 answer
671 views

How can I leave whitespaces in nestedExpr pyparsing

I've a wiki text like that data = """ {{hello}} {{hello world}} {{hello much { }} {{a {{b}}}} {{a td { } {{inner}} }} "&...
Ahmed Thabet's user avatar
2 votes
6 answers
723 views

Can you translate this debugging macro from C++ to python?

I use this very helpful macro when developing in C++: #define DD(a) std::cout << #a " = [ " << a << " ]" << std::endl;std::cout.flush(); Could you help me implement the same ...
static_rtti's user avatar
  • 54.6k
2 votes
5 answers
5k views

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 ...
Max Frai's user avatar
  • 63.2k
2 votes
2 answers
1k views

How to return focus to editor in VS Code macro sending Python text to Debug Console?

I have tried to key-bind a macro to send python text to the Debug Console and return focus to the editor in Visual Studio Code. This is what I have tried: Installed the vscode-python extension ...
bers's user avatar
  • 5,160
2 votes
1 answer
3k views

In Python, how can I include (not import) one file within another file, macro style, without changing namespace?

First I do know about 'import'. When I try 'import' it doesn't work. What I'm trying to do is split a single module into two parts, one of which is editable by a group and the other of which is not. ...
Brian's user avatar
  • 923
2 votes
3 answers
3k views

Controlling a browser from Python

I am looking for a way to control a browser from Python, i.e. fill out form fields and submit them, possibly call JS functions. I've looked around a bit, but as far as I could see PyWebKitGtk only ...
noio's user avatar
  • 5,804
2 votes
2 answers
219 views

Python Equivalent Of SCAR

I currently use a small program named 'SCAR' to build up small macro's when I have repetive tasks and I'm feeling lazy. 'SCAR' is designed to find colors on a screen in order to perform tasks. The ...
RandomPhobia's user avatar
  • 4,722
2 votes
1 answer
2k views

Passing variable in airflow macros

Please help me with {{ execution_date.subtract(hours = 5) }} Here instead of hardcoding 5, I want to pass it as a variable. I am not able to find a way to pass a variable and finally use the value of ...
Arpit Pruthi's user avatar
2 votes
2 answers
1k views

Strip prefix from all variable names in SPSS

I have a similar question as asked here (Strip suffix from all variable names in SPSS) and the answers there already helped a lot but there is still one question remaining. I have a dataset in which ...
Beate's user avatar
  • 23

1
2 3 4 5