473,289 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Macro like functionality for shorthand variable names

I have python code in a class method translated from C++ that looks
sort of like this:
>> self.dydt[1] = self.a * (self.b * self.y[0] - self.y[1])

To make this more readable in C++ I had made macros to achieve this:
#define du (dydt[1])
#define u (y[1])
#define V (y[0])

du = a * (b * V - u);
I realize the value of not having macros in Python. They've tripped
me up more than once in C++. My question is:
Is there any way to write a shorterhand more readable version of the
python code above? I'm doing several calculations one after the other
and some of the lines are quite long.
Jun 27 '08 #1
3 2873
On 6 Jun., 23:13, Tilman Kispersky <tilman...@gmail.comwrote:
I have python code in a class method translated from C++ that looks
sort of like this:
> self.dydt[1] = self.a * (self.b * self.y[0] - self.y[1])

To make this more readable in C++ I had made macros to achieve this:
#define du (dydt[1])
#define u (y[1])
#define V (y[0])

du = a * (b * V - u);

I realize the value of not having macros in Python. They've tripped
me up more than once in C++. My question is:
Is there any way to write a shorterhand more readable version of the
python code above? I'm doing several calculations one after the other
and some of the lines are quite long.
There is no entirely generic way but you can define a function that
produces a string from an object that contains assignments and then
use exec:

def tovars(obj):
return ";".join("%s=%s"%(n,v) for (n,v) in obj.__dict__.items())

# example

class A:pass
>>a = A()
a.x = 0
a.y = 1
exec tovars(a)
y
1
>>x
0

In your own case the tovars) function might be defined as:

def tovars(obj):
assign = []
assign.append("du = %s"%obj.dydt[1])
assign.append("u = %s"%obj.y[1])
assign.append("V = %s"%obj.y[0])
return ";".assign

Jun 27 '08 #2
Tilman Kispersky wrote:
I have python code in a class method translated from C++ that looks
sort of like this:
>>> self.dydt[1] = self.a * (self.b * self.y[0] - self.y[1])


To make this more readable in C++ I had made macros to achieve this:
#define du (dydt[1])
#define u (y[1])
#define V (y[0])

du = a * (b * V - u);
I realize the value of not having macros in Python. They've tripped
me up more than once in C++. My question is:
Is there any way to write a shorterhand more readable version of the
python code above? I'm doing several calculations one after the other
and some of the lines are quite long.
Names in Python point to objects so you can bind several names to the same location.
>>> self.dydt[1] = self.a * (self.b * self.y[0] - self.y[1])
The following will work.

a = self.a
b = self.b
u = self.y[0]
V = self.y[1]

self.dytd[1] = a * (b * V - u)

In addition to making the lines shorter, I'm pretty sure they will be slightly
faster because lookups on the local variables is slightly faster than on the
instance variables (self.?).

Note: I think your parenthesis are incorrect in the equation above. If they
aren't then no parenthesis are necessary since multiplication takes precedence
over subtraction.

Hope this helps.

-Larry
Jun 27 '08 #3
On Jun 7, 6:13*am, Tilman Kispersky <tilman...@gmail.comwrote:
I have python code in a class method translated from C++ that looks
sort of like this:
>*self.dydt[1] = self.a * (self.b * self.y[0] - self.y[1])

To make this more readable in C++ I had made macros to achieve this:
#define du (dydt[1])
#define u (y[1])
#define V (y[0])

du = a * (b * V - u);

I realize the value of not having macros in Python. *They've tripped
me up more than once in C++. *My question is:
Is there any way to write a shorterhand more readable version of the
python code above? *I'm doing several calculations one after the other
and some of the lines are quite long.
First, you can use 's' rather than 'self'.

Then, you can make 'du', 'u' and 'V' properties of your class like
this:
class MyClass(object):
... add this at the end of your class definition ...
def _getu(self): return self.y[1]
def _setu(self, u): self.y[1] = u
u = property(_getu, _setu)
... and similarly for du, V

Using these two tricks, your code line would be:
s.du = s.a * (s.b * s.V - s.u)

--
Paul Hankin
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Andrew Dalke | last post by:
Here's a proposed Q&A for the FAQ based on a couple recent threads. Appropriate comments appreciated X.Y: Why doesn't Python have macros like in Lisp or Scheme? Before answering that, a...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
34
by: SeeBelow | last post by:
I see the value of a class when two or more instances will be created, but Python programmers regularly use a class when there will only be one instance. What is the benefit of this? It has a...
3
by: arut | last post by:
I would like to know when a const should be used and when a #define is necessary in a program using a constant. What are the pros and cons?
7
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead....
20
by: Jim Ford | last post by:
I understand that some compilers define a symbol that can be used anywhere in the code in order to find out, at run time, the name of the file where the code corresponding to the current execution...
17
by: Russell Shaw | last post by:
Hi, How do i make an if/then/else macro act as a function so that the whole thing looks like the return value? I tried this lame attempt for starters: #define A_FROM_B(b) \ ( \ if(b < 10)...
6
by: Marcus Kwok | last post by:
I am designing a GUI (my question is not about GUIs) and I have named my variables using a regular naming scheme. However, in order to simplify the code using these variables, I have created an...
36
by: sh.vipin | last post by:
how to make large macro paste the code as it is Problem Explanation '-- For example in the program below /* a.c - starts here */ #define DECL_VARS() \ unsigned int a0;\ unsigned int a1;\...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.