for循环在python中将多个文件写为latex输出

时间:2017-03-13 12:20:40

标签: python latex

嘿,我是新手学习python和LaTeX的经济学课程,我查询了如何在不重复代码的情况下在多个tex文件中编写LaTex输出的for循环。

#include <TlHelp32.h>

#define BOOL_TO_ERR(b) ((b) ? NOERROR : GetLastError())

ULONG RunNonElevated(PCWSTR lpApplicationName, PWSTR lpCommandLine)
{
    HANDLE hToken;

    ULONG err = BOOL_TO_ERR(OpenProcessToken(NtCurrentProcess(), TOKEN_QUERY|TOKEN_ADJUST_PRIVILEGES, &hToken));

    if (err != NOERROR)
    {
        return err;
    }

    DWORD cb, rcb;

    union {
        TOKEN_ELEVATION_TYPE tet;
        TOKEN_LINKED_TOKEN tlt;
    };

    TOKEN_STATISTICS ts;
    LUID AuthenticationId = {};

    BOOL bSearchToken = FALSE, bFound = FALSE;

    err = BOOL_TO_ERR(GetTokenInformation(hToken, TokenElevationType, &tet, sizeof(tet), &rcb));

    if (err == NOERROR)
    {
        if (tet == TokenElevationTypeFull)
        {
            err = BOOL_TO_ERR(GetTokenInformation(hToken, TokenLinkedToken, &tlt, sizeof(tlt), &rcb));

            if (err == NOERROR)
            {
                err = BOOL_TO_ERR(GetTokenInformation(tlt.LinkedToken, TokenStatistics, &ts, sizeof(ts), &rcb));

                CloseHandle(tlt.LinkedToken);

                if (bSearchToken = (err == NOERROR))
                {
                    AuthenticationId.LowPart = ts.AuthenticationId.LowPart;
                    AuthenticationId.HighPart = ts.AuthenticationId.HighPart;

                    TOKEN_PRIVILEGES tp = {
                        1, { { { SE_DEBUG_PRIVILEGE } , SE_PRIVILEGE_ENABLED } }
                    };

                    AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL);
                }
            }
        }
    }

    CloseHandle(hToken);

    STARTUPINFO si = { sizeof (si) };
    PROCESS_INFORMATION pi;

    if (bSearchToken)
    {
        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

        if (hSnapshot != INVALID_HANDLE_VALUE)
        {
            PROCESSENTRY32W pe = { sizeof(pe) };

            static volatile UCHAR guz;

            PVOID stack = alloca(guz);

            cb = 0, rcb = FIELD_OFFSET(TOKEN_PRIVILEGES, Privileges[SE_MAX_WELL_KNOWN_PRIVILEGE]);

            union {
                PVOID buf;
                PTOKEN_PRIVILEGES ptp;
            };

            BOOL fHavePrivs = FALSE;

            if (Process32FirstW(hSnapshot, &pe))
            {
                do 
                {
                    if (HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pe.th32ProcessID))
                    {
                        if (OpenProcessToken(hProcess, TOKEN_QUERY|TOKEN_DUPLICATE, &hToken))
                        {

                            if (!fHavePrivs) do 
                            {
                                if (cb < rcb)
                                {
                                    cb = RtlPointerToOffset(buf = alloca(rcb - cb), stack);
                                }

                                if (GetTokenInformation(hToken, TokenPrivileges, buf, cb, &rcb))
                                {
                                    if (ULONG PrivilegeCount = ptp->PrivilegeCount)
                                    {
                                        int n = 3;
                                        BOOL fAdjust = FALSE;

                                        PLUID_AND_ATTRIBUTES Privileges = ptp->Privileges;
                                        do 
                                        {
                                            switch (Privileges->Luid.LowPart)
                                            {
                                            case SE_ASSIGNPRIMARYTOKEN_PRIVILEGE:
                                            case SE_INCREASE_QUOTA_PRIVILEGE:
                                            case SE_DEBUG_PRIVILEGE:
                                                if (!(Privileges->Attributes & SE_PRIVILEGE_ENABLED))
                                                {
                                                    Privileges->Attributes |= SE_PRIVILEGE_ENABLED;
                                                    fAdjust = TRUE;
                                                }

                                                if (!--n)
                                                {
                                                    if (DuplicateTokenEx(hToken, 
                                                        TOKEN_ADJUST_PRIVILEGES|TOKEN_IMPERSONATE, 
                                                        0, SecurityImpersonation, TokenImpersonation, 
                                                        &tlt.LinkedToken))
                                                    {
                                                        if (fAdjust)
                                                        {
                                                            AdjustTokenPrivileges(tlt.LinkedToken, FALSE, ptp, rcb, NULL, NULL);
                                                        }
                                                        fHavePrivs = SetThreadToken(0, tlt.LinkedToken);
                                                        CloseHandle(tlt.LinkedToken);
                                                    }
                                                    goto __1;
                                                }
                                            }
                                        } while (Privileges++, --PrivilegeCount);
                                    }
                                    break;
                                }

                            } while (GetLastError() == ERROR_INSUFFICIENT_BUFFER);

__1:
                            if (fHavePrivs &&
                                GetTokenInformation(hToken, TokenStatistics, &ts, sizeof(ts), &rcb) &&
                                ts.AuthenticationId.LowPart == AuthenticationId.LowPart &&
                                ts.AuthenticationId.HighPart == AuthenticationId.HighPart)
                            {
                                bFound = DuplicateTokenEx(hToken, 
                                    TOKEN_QUERY|TOKEN_DUPLICATE|TOKEN_ASSIGN_PRIMARY, 
                                    0, SecurityImpersonation, TokenPrimary, &tlt.LinkedToken);
                            }
                            CloseHandle(hToken);
                        }
                        CloseHandle(hProcess);
                    }
                } while (!bFound && Process32NextW(hSnapshot, &pe));
            }
            CloseHandle(hSnapshot);

            if (bFound)
            {
                err = BOOL_TO_ERR(CreateProcessAsUserW(tlt.LinkedToken, lpApplicationName, lpCommandLine, 
                    NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi));

                CloseHandle(tlt.LinkedToken);

                if (err == NOERROR)
                {
                    CloseHandle(pi.hThread);
                    CloseHandle(pi.hProcess);
                }
            }
        }
    }
    else if (err == NOERROR)
    {
        if ((err = BOOL_TO_ERR(CreateProcessW(lpApplicationName, lpCommandLine,
            NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))) == NOERROR)
        {
            CloseHandle(pi.hThread);
            CloseHandle(pi.hProcess);
        }
    }

    return err;
}

上面的代码给出了 AttributeError:&#39; str&#39;对象没有属性&#39;摘要&#39;

结果列表包含我运行的回归的摘要。因此每个res都是OLS回归的摘要。我想通过for循环将摘要转换为LATeX代码,而不是像

那样编写它
result= ['res','res1','res2','res3']
for r in result:
   f = open(r +'.tex', 'w')
   latex= r.summary().as_latex()
   f.write(latex)
   f.close()'

依旧...... 有什么方法可以通过for循环来实现吗?

1 个答案:

答案 0 :(得分:0)

您说“结果列表包含我已运行的回归的摘要”,但您的代码

result= ['res','res1','res2','res3']

显然有result作为字符串列表。 这会给出错误:“AttributeError:'str'对象没有属性'summary'。”

如果你有四个变量,那么

result = [res, res1, res2, res3]

会给你一个列表或许你的回归。

您似乎想要遍历结果列表和文件名列表。

这样的东西
result = [res, res1, res2, res3]
filename = ['res', 'res1', 'res2', 'res3']

你可以这样做

for (res, name) in zip(result, filename):
   f = open(name +'.tex', 'w')
   latex= res.summary().as_latex()
   f.write(latex)
   f.close()

甚至更好

for (res, name) in zip(result, filename):
   with f = open(name +'.tex', 'w')
       latex = res.summary().as_latex()
       f.write(latex)