- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如果您要将大量值从 C 输出到 Python 中的字典,是否有比以下更好(更快且更不容易出错)的方法:
return Py_BuildValue("{s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:(i,i,i,i),s:(i,i,i,i),s:(i,i,i,i)}",
"jd\0", spa.jd, //Julian day
"jc\0", spa.jc, //Julian century
"jde\0", spa.jde, //Julian ephemeris day
"jce\0", spa.jce, //Julian ephemeris century
"jme\0", spa.jme, //Julian ephemeris millennium
"l\0", spa.l, //earth heliocentric longitude [degrees]
"b\0", spa.b, //earth heliocentric latitude [degrees]
"r\0", spa.r, //earth radius vector [Astronomical Units, AU]
"theta\0", spa.theta, //geocentric longitude [degrees]
"beta\0", spa.beta, //geocentric latitude [degrees]
"x0\0", spa.x0, //mean elongation (moon-sun) [degrees]
"x1\0", spa.x1, //mean anomaly (sun) [degrees]
"x2\0", spa.x2, //mean anomaly (moon) [degrees]
"x3\0", spa.x3, //argument latitude (moon) [degrees]
"x4\0", spa.x4, //ascending longitude (moon) [degrees]
"del_psi\0", spa.del_psi, //nutation longitude [degrees]
"del_epsilon\0", spa.del_epsilon, //nutation obliquity [degrees]
"epsilon0\0", spa.epsilon0, //ecliptic mean obliquity [arc seconds]
"epsilon\0", spa.epsilon, //ecliptic true obliquity [degrees]
"del_tau\0", spa.del_tau, //aberration correction [degrees]
"lamda\0", spa.lamda, //apparent sun longitude [degrees]
"nu0\0", spa.nu0, //Greenwich mean sidereal time [degrees]
"nu\0", spa.nu, //Greenwich sidereal time [degrees]
"alpha\0", spa.alpha, //geocentric sun right ascension [degrees]
"delta\0", spa.delta, //geocentric sun declination [degrees]
"h\0", spa.h, //observer hour angle [degrees]
"xi\0", spa.xi, //sun equatorial horizontal parallax [degrees]
"del_alpha\0", spa.del_alpha, //sun right ascension parallax [degrees]
"delta_prime\0", spa.delta_prime, //topocentric sun declination [degrees]
"alpha_prime\0", spa.alpha_prime, //topocentric sun right ascension [degrees]
"h_prime\0", spa.h_prime, //topocentric local hour angle [degrees],
"h0_prime\0", spa.h0_prime,
"delta_zero\0", spa.delta_zero,
"e0\0", spa.e0, //topocentric elevation angle (uncorrected) [degrees]
"del_e\0", spa.del_e, //atmospheric refraction correction [degrees]
"e\0", spa.e, //topocentric elevation angle (corrected) [degrees]
"eot\0", spa.eot, //equation of time [minutes]
"srha\0", spa.srha, //sunrise hour angle [degrees]
"ssha\0", spa.ssha, //sunset hour angle [degrees]
"sta\0", spa.sta, //sun transit altitude [degrees]
"zenith\0", spa.zenith, //topocentric zenith angle [degrees]
"azimuth180\0", spa.azimuth180, //topocentric azimuth angle (westward from south) [-180 to 180 degrees]
"azimuth\0", spa.azimuth, //topocentric azimuth angle (eastward from north) [ 0 to 360 degrees]
"incidence\0", spa.incidence, //surface incidence angle [degrees]
"_suntransit\0", spa.suntransit, //local sun transit time (or solar noon) [fractional hour]
"_sunrise\0", spa.sunrise, //local sunrise time (+/- 30 seconds) [fractional hour]
"_sunset\0", spa.sunset, //local sunset time (+/- 30 seconds) [fractional hour]
"sunrise\0", sunrise_hour, sunrise_min, sunrise_sec, sunrise_microsec,
"sunset\0", sunset_hour, sunset_min, sunset_sec, sunset_microsec,
"noon\0", transit_hour, transit_min, transit_sec, transit_microsec
);
最佳答案
我同意@Martin v. Löwis 关于使用 C 预处理器及其宏功能至少减轻一些设置和维护您正在做的事情的负担。如果您正确定义了这些宏,您可以安排将所有定义信息放在单个头文件中的一个位置,避免重复自己。
基本上,您需要关于每个项目或键值对的两条信息,这些信息将进入您正在构建的字典中。一个是放在 Py_BuildValue()
的格式字符串参数中的内容,第二个是键的来源和它的关联值。
您可以通过定义然后重新定义任务所需的宏来提取这两组信息中的每一个。对于您的示例,可以创建以下头文件。请注意,根据 #include
d 时定义的是 FORMAT
还是 FIELDS
,如何定义两组不同的宏中的一组。
// builddict.h -- for defining Py_BuildValue() arguments
// define apppropriate macros for current usage
#ifdef FORMAT
#define SPA_FIELD_LAST(FIELD) "s:d"
#define SPA_FIELD(FIELD) SPA_FIELD_LAST(FIELD)", "
#define TIME_FIELD_LAST(NAME) "s:(i,i,i,i)"
#define TIME_FIELD(NAME) TIME_FIELD_LAST(NAME)", "
#define TIME_KEY_FIELD_LAST(KEY,NAME) "s:(i,i,i,i)"
#define TIME_KEY_FIELD(KEY,NAME) TIME_KEY_FIELD_LAST(KEY,NAME)", "
#undef FORMAT
#elif defined FIELDS
#define SPA_FIELD_LAST(FIELD) #FIELD, spa.FIELD
#define SPA_FIELD(FIELD) SPA_FIELD_LAST(FIELD),
#define TIME_FIELD_LAST(NAME) #NAME, NAME##_hour, NAME##_min, NAME##_sec, NAME##_microsec
#define TIME_FIELD(NAME) TIME_FIELD_LAST(NAME),
#define TIME_KEY_FIELD_LAST(KEY,NAME) #KEY, NAME##_hour, NAME##_min, NAME##_sec, NAME##_microsec
#define TIME_KEY_FIELD(KEY,NAME) TIME_KEY_FIELD_LAST(KEY,NAME),
#undef FIELDS
#else
#error neither FORMAT nor FIELDS usage macros are defined
#endif
SPA_FIELD(jd) // Julian day
SPA_FIELD(jc) // Julian century
SPA_FIELD(jde) // Julian ephemeris day
SPA_FIELD(jce) // Julian ephemeris century
SPA_FIELD(jme) // Julian ephemeris millennium
SPA_FIELD(l) // earth heliocentric longitude [degrees]
SPA_FIELD(b) // earth heliocentric latitude [degrees]
SPA_FIELD(r) // earth radius vector [Astronomical Units) AU]
SPA_FIELD(theta) // geocentric longitude [degrees]
SPA_FIELD(beta) // geocentric latitude [degrees]
SPA_FIELD(x0) // mean elongation (moon-sun) [degrees]
SPA_FIELD(x1) // mean anomaly (sun) [degrees]
SPA_FIELD(x2) // mean anomaly (moon) [degrees]
SPA_FIELD(x3) // argument latitude (moon) [degrees]
SPA_FIELD(x4) // ascending longitude (moon) [degrees]
SPA_FIELD(del_psi) // nutation longitude [degrees]
SPA_FIELD(del_epsilon) // nutation obliquity [degrees]
SPA_FIELD(epsilon0) // ecliptic mean obliquity [arc seconds]
SPA_FIELD(epsilon) // ecliptic true obliquity [degrees]
SPA_FIELD(del_tau) // aberration correction [degrees]
SPA_FIELD(lamda) // apparent sun longitude [degrees]
SPA_FIELD(nu0) // Greenwich mean sidereal time [degrees]
SPA_FIELD(nu) // Greenwich sidereal time [degrees]
SPA_FIELD(alpha) // geocentric sun right ascension [degrees]
SPA_FIELD(delta) // geocentric sun declination [degrees]
SPA_FIELD(h) // observer hour angle [degrees]
SPA_FIELD(xi) // sun equatorial horizontal parallax [degrees]
SPA_FIELD(del_alpha) // sun right ascension parallax [degrees]
SPA_FIELD(delta_prime) // topocentric sun declination [degrees]
SPA_FIELD(alpha_prime) // topocentric sun right ascension [degrees]
SPA_FIELD(h_prime) // topocentric local hour angle [degrees])
SPA_FIELD(h0_prime)
SPA_FIELD(delta_zero)
SPA_FIELD(e0) // topocentric elevation angle (uncorrected) [degrees]
SPA_FIELD(del_e) // atmospheric refraction correction [degrees]
SPA_FIELD(e) // topocentric elevation angle (corrected) [degrees]
SPA_FIELD(eot) // equation of time [minutes]
SPA_FIELD(srha) // sunrise hour angle [degrees]
SPA_FIELD(ssha) // sunset hour angle [degrees]
SPA_FIELD(sta) // sun transit altitude [degrees]
SPA_FIELD(zenith) // topocentric zenith angle [degrees]
SPA_FIELD(azimuth180) // topocentric azimuth angle (westward from south) [-180 to 180 degrees]
SPA_FIELD(azimuth) // topocentric azimuth angle (eastward from north) [ 0 to 360 degrees]
SPA_FIELD(incidence) // surface incidence angle [degrees]
SPA_FIELD(suntransit) // local sun transit time (or solar noon) [fractional hour]
SPA_FIELD(sunrise) // local sunrise time (+/- 30 seconds) [fractional hour]
SPA_FIELD(sunset) // local sunset time (+/- 30 seconds) [fractional hour]
TIME_FIELD(sunrise)
TIME_FIELD(sunset)
TIME_KEY_FIELD_LAST(noon, transit) // must use a xxx_LAST macro on last one
// clean up to prevent warnings about redefining macros
#undef SPA_FIELD_LAST
#undef SPA_FIELD
#undef TIME_FIELD_LAST
#undef TIME_FIELD
#undef TIME_KEY_FIELD_LAST
#undef TIME_KEY_FIELD
一旦您完成所有设置,您的 build_dict()
函数将变得相当简短并且独立于字典的实际内容:
// build format string using header
char format_string[] = "{"
#define FORMAT
#include "builddict.h"
"}";
// use header again to build list of fields
PyObject* build_dict(SPA spa)
{
return Py_BuildValue(format_string,
#define FIELDS
#include "builddict.h"
);
}
虽然这不能完全自动化该过程,但可以提供很大帮助。可能有其他可用的文本处理或 C 接口(interface)工具(或者您可以编写自己的工具)来进一步帮助您创建这个单一的头文件,因为它采用非常统一的格式。
关于 python /C : Parse all values at once for return to Python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4424206/
询问 unrelated question我有这样的代码: public boolean equals(Object obj) { if (this == obj) retur
在我之前的一个问题中 js: Multiple return in Ternary Operator我询问了有关使用三元运算符返回多个参数的问题。但是现在参数IsActveUser boolean(t
假设我有一个带有 return 的 if 语句。从效率的角度来看,我应该使用 if(A > B): return A+1 return A-1 或 if(A > B): return
例如考虑以下代码: int main(int argc,char *argv[]) { int *p,*q; p = (int *)malloc(sizeof(int)*10); q
PyCharm 对这段代码发出警告,说最后一个返回是不可访问的: def foo(): with open(...): return 1 return 0 如果 ope
我想实现这样的目标: 如果在返回 Json 的方法中抛出异常,则返回 new Json(new { success = false, error = "unknown"}); 但如果方法返回 View
它是多余的,但我正在学习 JS,我想知道它是如何工作的。 直接从模块返回函数 let func1 = function () { let test = function () {
我不明白我应该使用什么。我有两页 - intro.jsp(1) 和 booksList.jsp(2)。我为每一页创建了一个 Controller 类。第一页有打开第二页的按钮:
我最近在 Joomla 组件(Kunena,更准确地说是 Kunena)中看到这段代码,那么使用 $this->return VS 简单的 return 语句有什么区别. 我已经用谷歌搜索了代码,但没
我的类实现了 IEnumerable。并且可以编译这两种方式来编写 GetEnumerator 方法: public IEnumerator GetEnumerator() { yield r
我只是在编码,我想到了一个简单的想法(显然是问题),如果我有一个像这样的函数: int fun1(int p){ return(p); } 我有一个这样的函数: int fun1(int p){
这个问题在这里已经有了答案: What does the comma operator do in JavaScript? (5 个答案) 关闭 9 年前。 function makeArray
假设我写了一个 for 循环,它将输出所有数字 1 到 x: x=4 for number in xrange(1,x+1): print number, #Output: 1 2 3 4 现
我最近在这个 Apache Axis tutorial example. 中看到了下面的一段代码 int main() { int status = AXIS2_SUCCESS; ax
function a(){ return{ bb:"a" } } and function a(){ return { bb:"a" } } 这两个代码有什么区别吗,如果有请
function a() { return 1; } function b() { return(1); } 我在 Chrome 的控制台中测试了上面的代码,都返回了 1。 function c()
考虑这三个函数: def my_func1(): print "Hello World" return None def my_func2(): print "Hello World"
这可能是一个愚蠢的问题,但我正在努力,如果有一种简明的方法来测试函数的返回结果,如果它不满足条件,则返回该值(即,传递它)。。现在来回答一个可能的问题,是的,我正在寻找的类似于例外提供的东西。然而,作
我正在测试一个函数,并尝试使用 return 来做什么,并在 PowerShell 5.1 和 PwSh 7.1 中偶然发现了一个奇怪的问题,即 return cmdlet似乎不适合在团体中工作: P
这个问题已经有答案了: Return in generator together with yield (2 个回答) Why can't I use yield with return? (5 个回
我是一名优秀的程序员,十分优秀!