IO Redirection to Console
Hello, I am trying to port the following C++ code for IO redirection to console. // C++ (from Synfig) void redirectIOToConsole() { int hConHandle; HANDLE lStdHandle; CONSOLE_SCREEN_BUFFER_INFO coninfo; FILE *fp; // allocate console if( GetStdHandle(STD_OUTPUT_HANDLE) != INVALID_HANDLE_VALUE ) AllocConsole(); // set the screen buffer to be big enough to let us scroll text GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo); coninfo.dwSize.Y = MAX_LINES; SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize); //redirect unbuffered STDOUT to the console lStdHandle = GetStdHandle(STD_OUTPUT_HANDLE); hConHandle = _open_osfhandle((intptr_t) lStdHandle, _O_TEXT); fp = _fdopen( hConHandle, "w" ); *stdout = *fp; setvbuf( stdout, NULL, _IONBF, 0 ); // redirect unbuffered STDIN to the console lStdHandle = GetStdHandle(STD_INPUT_HANDLE); hConHandle = _open_osfhandle((intptr_t) lStdHandle, _O_TEXT); fp = _fdopen( hConHandle, "r" ); *stdin = *fp; setvbuf( stdin, NULL, _IONBF, 0 ); // redirect unbuffered STDERR to the console lStdHandle = GetStdHandle(STD_ERROR_HANDLE); hConHandle = _open_osfhandle((intptr_t) lStdHandle, _O_TEXT); fp = _fdopen( hConHandle, "w" ); *stderr = *fp; setvbuf( stderr, NULL, _IONBF, 0 ); // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog // point to console as well ios::sync_with_stdio(); } My Python port: from ctypes import windll, create_string_buffer,Structure, byref from ctypes.wintypes import DWORD,SHORT, WORD import os import msvcrt import sys STD_INPUT_HANDLE = -10 STD_OUTPUT_HANDLE = -11 STD_ERROR_HANDLE = -12 INVALID_HANDLE_VALUE = DWORD(-1).value MAX_LINES = 500 def consoleOptionEnabled(argv): value = False if ("--console" in argv) or ("-c" in argv): value = True return value def redirectIOToConsole(): class CONSOLE_SCREEN_BUFFER_INFO(Structure): _fields_ = [("dwSize", COORD), ("dwCursorPosition", COORD), ("wAttributes", WORD), ("srWindow", SMALL_RECT), ("dwMaximumWindowSize", DWORD)] coninfo = CONSOLE_SCREEN_BUFFER_INFO() # allocate console if(windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE) != INVALID_HANDLE_VALUE): windll.kernel32.AllocConsole() # set the screen buffer to be big enough to let us scroll text windll.kernel32.GetConsoleScreenBufferInfo(windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE), byref(coninfo)) coninfo.dwSize.Y = MAX_LINES windll.kernel32.SetConsoleScreenBufferSize(windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize) #redirect unbuffered STDOUT to the console lStdHandle = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE) hConHandle = msvcrt.open_osfhandle(lStdHandle, os.O_TEXT) fp = os.fdopen( hConHandle, "w" ) sys.stdout = fp setvbuf( stdout, NULL, _IONBF, 0 ) # redirect unbuffered STDIN to the console lStdHandle = windll.kernel32.GetStdHandle(STD_INPUT_HANDLE) hConHandle = msvcrt.open_osfhandle(lStdHandle, os.O_TEXT) fp = os.fdopen( hConHandle, "r" ) sys.stdin = fp setvbuf( stdin, NULL, _IONBF, 0 ) #redirect unbuffered STDERR to the console lStdHandle = windll.kernel32.GetStdHandle(STD_ERROR_HANDLE) hConHandle = msvcrt.open_osfhandle(lStdHandle, os.O_TEXT) fp = os.fdopen( hConHandle, "w" ) sys.stderr = fp setvbuf( stderr, NULL, _IONBF, 0 ) # make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog # point to console as well Is there a better way to handling IO redirection to console in Python? Thanks. Austin -- https://mail.python.org/mailman/listinfo/python-list
Python IO Redirection to Console
Hello, I am trying to redirect the IO (stdout, stdin and stderr) to the console. Is there a Python module for this? Thanks. Regards -- https://mail.python.org/mailman/listinfo/python-list
list comparison vs integer comparison, which is more efficient?
Hi, I am currently implementing the LTE physical layer in Python (ver 2.7.7). For the qpsk, 16qam and 64qam modulation I would like to know which is more efficient to use, between an integer comparison and a list comparison: Integer comparison: bit_pair as an integer value before comparison # QPSK - TS 36.211 V12.2.0, section 7.1.2, Table 7.1.2-1 def mp_qpsk(self): r = [] for i in range(self.nbits/2): bit_pair = (self.sbits[i*2] << 1) | self.sbits[i*2+1] if bit_pair == 0: r.append(complex(1/math.sqrt(2),1/math.sqrt(2))) elif bit_pair == 1: r.append(complex(1/math.sqrt(2),-1/math.sqrt(2))) elif bit_pair == 2: r.append(complex(-1/math.sqrt(2),1/math.sqrt(2))) elif bit_pair == 3: r.append(complex(-1/math.sqrt(2),-1/math.sqrt(2))) return r List comparison: bit_pair as a list before comparison # QPSK - TS 36.211 V12.2.0, section 7.1.2, Table 7.1.2-1 def mp_qpsk(self): r = [] for i in range(self.nbits/2): bit_pair = self.sbits[i*2:i*2+2] if bit_pair == [0,0]: r.append(complex(1/math.sqrt(2),1/math.sqrt(2))) elif bit_pair == [0,1]: r.append(complex(1/math.sqrt(2),-1/math.sqrt(2))) elif bit_pair == [1,0]: r.append(complex(-1/math.sqrt(2),1/math.sqrt(2))) elif bit_pair == [1,1]: r.append(complex(-1/math.sqrt(2),-1/math.sqrt(2))) return r Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: list comparison vs integer comparison, which is more efficient?
On Sunday, January 4, 2015 8:12:10 AM UTC+1, Terry Reedy wrote: > On 1/3/2015 6:19 PM, austin aigbe wrote: > > > I am currently implementing the LTE physical layer in Python (ver 2.7.7). > > For the qpsk, 16qam and 64qam modulation I would like to know which is more > > efficient to use, between an integer comparison and a list comparison: > > > > Integer comparison: bit_pair as an integer value before comparison > > > > # QPSK - TS 36.211 V12.2.0, section 7.1.2, Table 7.1.2-1 > > def mp_qpsk(self): > > r = [] > > for i in range(self.nbits/2): > > bit_pair = (self.sbits[i*2] << 1) | self.sbits[i*2+1] > > if bit_pair == 0: > > r.append(complex(1/math.sqrt(2),1/math.sqrt(2))) > > elif bit_pair == 1: > > r.append(complex(1/math.sqrt(2),-1/math.sqrt(2))) > > elif bit_pair == 2: > > r.append(complex(-1/math.sqrt(2),1/math.sqrt(2))) > > elif bit_pair == 3: > > r.append(complex(-1/math.sqrt(2),-1/math.sqrt(2))) > > return r > > > > List comparison: bit_pair as a list before comparison > > > > # QPSK - TS 36.211 V12.2.0, section 7.1.2, Table 7.1.2-1 > > def mp_qpsk(self): > > r = [] > > for i in range(self.nbits/2): > > bit_pair = self.sbits[i*2:i*2+2] > > if bit_pair == [0,0]: > > r.append() > > elif bit_pair == [0,1]: > > r.append(complex(1/math.sqrt(2),-1/math.sqrt(2))) > > elif bit_pair == [1,0]: > > r.append(complex(-1/math.sqrt(2),1/math.sqrt(2))) > > elif bit_pair == [1,1]: > > r.append(complex(-1/math.sqrt(2),-1/math.sqrt(2))) > > return r > > Wrong question. If you are worried about efficiency, factor out all > repeated calculation of constants and eliminate the multiple comparisons. > > sbits = self.sbits > a = 1.0 / math.sqrt(2) > b = -a > points = (complex(a,a), complex(a,b), complex(b,a), complex(b,b)) > complex(math.sqrt(2),1/math.sqrt(2)) > def mp_qpsk(self): > r = [points[sbits[i]*2 + sbits[i+1]] > for i in range(0, self.nbits, 2)] > return r > > -- > Terry Jan Reedy Cool. Thanks a lot. -- https://mail.python.org/mailman/listinfo/python-list
Re: list comparison vs integer comparison, which is more efficient?
On Sunday, January 4, 2015 12:20:26 PM UTC+1, austin aigbe wrote: > On Sunday, January 4, 2015 8:12:10 AM UTC+1, Terry Reedy wrote: > > On 1/3/2015 6:19 PM, austin aigbe wrote: > > > > > I am currently implementing the LTE physical layer in Python (ver 2.7.7). > > > For the qpsk, 16qam and 64qam modulation I would like to know which is > > > more efficient to use, between an integer comparison and a list > > > comparison: > > > > > > Integer comparison: bit_pair as an integer value before comparison > > > > > > # QPSK - TS 36.211 V12.2.0, section 7.1.2, Table 7.1.2-1 > > > def mp_qpsk(self): > > > r = [] > > > for i in range(self.nbits/2): > > > bit_pair = (self.sbits[i*2] << 1) | self.sbits[i*2+1] > > > if bit_pair == 0: > > > r.append(complex(1/math.sqrt(2),1/math.sqrt(2))) > > > elif bit_pair == 1: > > > r.append(complex(1/math.sqrt(2),-1/math.sqrt(2))) > > > elif bit_pair == 2: > > > r.append(complex(-1/math.sqrt(2),1/math.sqrt(2))) > > > elif bit_pair == 3: > > > r.append(complex(-1/math.sqrt(2),-1/math.sqrt(2))) > > > return r > > > > > > List comparison: bit_pair as a list before comparison > > > > > > # QPSK - TS 36.211 V12.2.0, section 7.1.2, Table 7.1.2-1 > > > def mp_qpsk(self): > > > r = [] > > > for i in range(self.nbits/2): > > > bit_pair = self.sbits[i*2:i*2+2] > > > if bit_pair == [0,0]: > > > r.append() > > > elif bit_pair == [0,1]: > > > r.append(complex(1/math.sqrt(2),-1/math.sqrt(2))) > > > elif bit_pair == [1,0]: > > > r.append(complex(-1/math.sqrt(2),1/math.sqrt(2))) > > > elif bit_pair == [1,1]: > > > r.append(complex(-1/math.sqrt(2),-1/math.sqrt(2))) > > > return r > > > > Wrong question. If you are worried about efficiency, factor out all > > repeated calculation of constants and eliminate the multiple comparisons. > > > > sbits = self.sbits > > a = 1.0 / math.sqrt(2) > > b = -a > > points = (complex(a,a), complex(a,b), complex(b,a), complex(b,b)) > > complex(math.sqrt(2),1/math.sqrt(2)) > > def mp_qpsk(self): > > r = [points[sbits[i]*2 + sbits[i+1]] > > for i in range(0, self.nbits, 2)] > > return r > > > > -- > > Terry Jan Reedy > > Cool. Thanks a lot. Hi Terry, No difference between the int and list comparison in terms of the number of calls(24) and time (0.004s). Main part is the repeated call to sqrt(). However, it took a shorter time (0.004s) with 24 function calls than your code (0.005s) which took just 13 function calls to execute. Why is this? Integer comparison profile result: >>> p = pstats.Stats('lte_phy_mod.txt') >>> p.strip_dirs().sort_stats(-1).print_stats() Sun Jan 04 12:36:32 2015lte_phy_mod.txt 24 function calls in 0.004 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 10.0040.0040.0040.004 lte_phy_layer.py:16() 10.0000.0000.0000.000 lte_phy_layer.py:20(Scrambling) 10.0000.0000.0000.000 lte_phy_layer.py:276(LayerMapping) 10.0000.0000.0000.000 lte_phy_layer.py:278(Precoding) 10.0000.0000.0000.000 lte_phy_layer.py:280(ResourceElementMapping) 10.0000.0000.0000.000 lte_phy_layer.py:282(OFDMSignalGenerator) 10.0000.0000.0000.000 lte_phy_layer.py:65(Modulation) 10.0000.0000.0000.000 lte_phy_layer.py:71(__init__) 10.0000.0000.0000.000 lte_phy_layer.py:87(mp_qpsk) 10.0000.0000.0000.000 {len} 80.0000.0000.0000.000 {math.sqrt} 40.0000.0000.0000.000 {method 'append' of 'list' objects} 10.0000.0000.0000.000 {method 'disable' of '_lsprof.Profiler' objects} 10.0000.0000.0000.000 {range} >>> List comparison: >>> import pstats >>> p = pstats.Stats('lte_phy_mod2.txt') >>> p.strip_dirs().sort_stats(-1).print_stats() Sun Jan 04 12:57:24 2015lte_phy_mod2.txt 24 function calls in 0.004 seconds Ordered by: stan