Search Results

Search found 3 results on 1 pages for 'philipjkim'.

Page 1/1 | 1 

  • Better ways to print out column names when using cx_Oracle

    - by philipjkim
    Found an example using cx_Oracle, this example shows all the information of Cursor.description. import cx_Oracle from pprint import pprint connection = cx_Oracle.Connection("%s/%s@%s" % (dbuser, dbpasswd, oracle_sid)) cursor = cx_Oracle.Cursor(connection) sql = "SELECT * FROM your_table" cursor.execute(sql) data = cursor.fetchall() print "(name, type_code, display_size, internal_size, precision, scale, null_ok)" pprint(cursor.description) pprint(data) cursor.close() connection.close() What I wanted to see was the list of Cursor.description[0](name), so I changed the code: import cx_Oracle import pprint connection = cx_Oracle.Connection("%s/%s@%s" % (dbuser, dbpasswd, oracle_sid)) cursor = cx_Oracle.Cursor(connection) sql = "SELECT * FROM your_table" cursor.execute(sql) data = cursor.fetchall() col_names = [] for i in range(0, len(cursor.description)): col_names.append(cursor.description[i][0]) pp = pprint.PrettyPrinter(width=1024) pp.pprint(col_names) pp.pprint(data) cursor.close() connection.close() I think there will be better ways to print out the names of columns. Please get me alternatives to the Python beginner. :-)

    Read the article

  • Problems with sys.stdout.write() with time.sleep() in a function

    - by philipjkim
    What I wanted is printing out 5 dots that a dot printed per a second using time.sleep(), but the result was 5 dots were printed at once after 5 seconds delay. Tried both print and sys.stdout.write, same result. Thanks for any advices. import time import sys def wait_for(n): """Wait for {n} seconds. {n} should be an integer greater than 0.""" if not isinstance(n, int): print 'n in wait_for(n) should be an integer.' return elif n < 1: print 'n in wait_for(n) should be greater than 0.' return for i in range(0, n): sys.stdout.write('.') time.sleep(1) sys.stdout.write('\n') def main(): wait_for(5) # FIXME: doesn't work as expected if __name__ == '__main__': try: main() except KeyboardInterrupt: print '\nAborted.'

    Read the article

  • Can EventMachine recognize all threads are completed?

    - by philipjkim
    I'm an EM newbie and writing two codes to compare synchronous and asynchronous IO. I'm using Ruby 1.8.7. The example for sync IO is: def pause_then_print(str) sleep 2 puts str end 5.times { |i| pause_then_print(i) } puts "Done" This works as expected, taking 10+ seconds until termination. On the other hand, the example for async IO is: require 'rubygems' require 'eventmachine' def pause_then_print(str) Thread.new do EM.run do sleep 2 puts str end end end EventMachine.run do EM.add_timer(2.5) do puts "Done" EM.stop_event_loop end EM.defer(proc do 5.times { |i| pause_then_print(i) } end) end 5 numbers are shown in 2.x seconds. Now I explicitly wrote code that EM event loop to be stopped after 2.5 seconds. But what I want is that the program terminates right after printing out 5 numbers. For doing that, I think EventMachine should recognize all 5 threads are done, and then stop the event loop. How can I do that? Also, please correct the async IO example if it can be more natural and expressive. Thanks in advance.

    Read the article

1