printing table on the command line
Hello, I'm using the MySQLdb library in python to interface with a mysql database I've created. I have written a command line app which runs from the command line. I have 10 fields and hence, have found that each record spreads over one line. What is the best way to print a table of a database like this? Perhaps using tab spacing? Should I only print the first 8 characters of each field, and allow the user to expand an individual record? If so, how do I do this? I'm only new with python, but %8s doesn't seem to do anything and with print it just prints the number 8 before the string... I want to line up all the fields under the necessary headings, here is the structure of the program: for x in headings: print '%8s\t' % (x), print '\n' # a few new lines for x in records: # entire list of all records for i in x: # search individual record print '%8s\t' % (i), print '\n' This may not be 100% exact, but I'm just trying to simplify it so I don't need to paste the entire program here, note that records is a list of all records in the table, and i is obviously a list of fields for each individual record. Headings is just a tupple of strings such as "name", "email" etc, just the name of the fields. So all I want to do is print a nicely formatted table to the screen on the console, with tab spacing, I've got 10 fields per record, so will I have to just chop off some characters? Any examples of how to do this would be great, as I'm blind and it's a bit difficult to check the spacing. Thank you very much, Dan -- http://mail.python.org/mailman/listinfo/python-list
Re: working with laptop battery
On Sun, Feb 14, 2010 at 03:22:11AM +0100, Daniel Fetchinson wrote: > /proc/acpi/battery/BAT0/info > /proc/acpi/battery/BAT0/state Had a quick look, but that path doesn't seem to exist, I'll look harder on the weekend when I put the script together, because it has to be somewhere. Thanks, Dan -- http://mail.python.org/mailman/listinfo/python-list
Re: working with laptop battery
I'm not sure I have those files, but I'll look a little harder this weekend when I put together the script. Thanks for your help, Dan On Sat, Feb 13, 2010 at 08:23:28PM -0600, Tim Chase wrote: > Daniel Dalton wrote: > >On Sat, Feb 13, 2010 at 05:26:02PM -0800, Chris Rebert wrote: > >>It's probably gonna depend on which OS you're running. Which would be...? > > > >Sorry, forgot to mention this. I'm running debian linux. > > You should be able to read/poll the various files in > > /proc/acpi/battery/BAT*/* > > for whatever battery information you need. Each BAT* directory > contains information about one of the batteries in the system (it's > possible, albeit rare, to have more than one). So you might have > some script that runs every $INTERVAL that looks something like > > from glob import glob > for fname in glob('/proc/acpi/battery/BAT*/*'): > f = file(fname) > for line in f: > do_something(line) > f.close() > > On my Debian laptop (Gateway Solo 1200, OEM battery circa 2001), the > contents look something like > > t...@rubbish:/proc/acpi/battery/BAT0$ cat alarm > alarm: unsupported > t...@rubbish:/proc/acpi/battery/BAT0$ cat info > present: yes > design capacity: 4016 mAh > last full capacity: 4011 mAh > battery technology: rechargeable > design voltage: 9600 mV > design capacity warning: 602 mAh > design capacity low: 401 mAh > capacity granularity 1: 201 mAh > capacity granularity 2: 3409 mAh > model number:QT08 > serial number: SANYOQT08 > battery type:NiMH > OEM info:SANYO > t...@rubbish:/proc/acpi/battery/BAT0$ cat state > present: yes > capacity state: ok > charging state: charged > present rate:unknown > remaining capacity: 4011 mAh > present voltage: 9600 mV > > > > -tkc > -- http://mail.python.org/mailman/listinfo/python-list
Re: working with laptop battery
On Sat, Feb 13, 2010 at 09:19:59PM -0500, Chris Colbert wrote: >You'll need acpi installed: >In [6]: import subprocess Thanks for that code, I'll try putting something together this weekend. Dan -- http://mail.python.org/mailman/listinfo/python-list
Re: working with laptop battery
On Sat, Feb 13, 2010 at 05:26:02PM -0800, Chris Rebert wrote: > It's probably gonna depend on which OS you're running. Which would be...? Sorry, forgot to mention this. I'm running debian linux. Thanks, Dan -- http://mail.python.org/mailman/listinfo/python-list
working with laptop battery
Hi, I'm constantly working in the command line and need to write a program to give me alerts on my battery. Can someone please tell me what module I should use to access battery information? Looking for something that perhaps makes use of acpi so I can get estimated time left as well as a percentage. Thanks very much for any help, Dan -- http://mail.python.org/mailman/listinfo/python-list
Best way to conduct a google search
Hi, I need to do the following in my program: 1. Conduct a google search, supplying the variable "text" to the search. Is there a google api or something similar I should use? 2. I then need to be able to get the url, of the page, or the html content, so I can dump it to text. Thanks, Dan signature.asc Description: Digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: python and vc numbers
> Can you make do with the tempfile module? Or you'd need to identify > from an external process which console is locked? Perhaps, I wrote a small hack: - Manually set environment variable TTYNUMBER in .bash_profile - Then use this in the script, to establish what tty I'm working with. Thanks -- Cheers, Dan http://members.iinet.net.au/~ddalton/ signature.asc Description: Digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: python and vc numbers
On Mon, Nov 30, 2009 at 02:26:14AM -0800, Chris Rebert wrote: > Also, in my quickie newbie experimentation with `screen`, each screen > "window" seems to get a unique tty#. Admittedly I am running OS X Correct (Which creates the problem) > Perhaps if you could explain your problem in greater detail? Sure, well, first I am running screen in a console. Under screen I open many windows, but in my .screenrc file, after 15 minutes screen runs screen lock which is the equivalent of /usr/local/bin/lock /usr/local/bin/lock is my python script, basically it checks to see if file /tmp/.vlock.run exists and if it does, will not run vlock again, but if it doesn't then passes onto vlock. When vlock returns a clean exit, eg. the user unlocks the term, my program removes that statefile and exits nicely. (the purpose of this is so screen doesn't lock the system hundreds of times, asking for a password hundreds of times, or when using -na it doesn't create a bunch of useless blank "lock" windows in my screen session. This works well, but it is only useful for one tty, because if you lock tty1 then it blocks tty2 etc. I know I could have a bunch of different scripts using different files, but this just gets to complicated to manage, so the logical solution is to append vc number to the filename: /tmp/.vlock.run.1 /tmp/.vlock.run.2 etc So we can identify which consoles have been locked, and which haven't. The problem lies with the fact, I can't find a reliable way to determine the current console number with python or any bash tool. When I say console number, I mean the actual console number, not screen window or device it is sending to or whatever. I am totally blind, and therefore use a package called brltty, and this package has the ability to show me what number console I'm in, and even under screen always works reliably and consistently. So anyone know of a better solution, now I have described the issue in great detail? Thanks very much for all the help. Cheers, Dan signature.asc Description: Digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: python and vc numbers
On Mon, Nov 30, 2009 at 07:20:59PM +1100, Daniel Dalton wrote: > That did the trick, thanks, after I append > [-2] Further testing under screen says otherwise -- it seems to give me the tty number, not the virtual console number. Is there any way to figure out what virtual console I'm am in so a certain command ran under screen process A isn't confused with a command ran under screen process B? Thanks Dan signature.asc Description: Digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: python and vc numbers
On Mon, Nov 30, 2009 at 02:21:54PM +1300, Gregory Ewing wrote: > >I use to figure out what tty my program was invoked from? > > Here's one way: > > % python > Python 2.5 (r25:51908, Apr 8 2007, 22:22:18) > [GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin > Type "help", "copyright", "credits" or "license" for more information. > >>> import os > >>> os.popen("tty").read() That did the trick, thanks, after I append [-2] It works great. Thanks Dan signature.asc Description: Digital signature -- http://mail.python.org/mailman/listinfo/python-list
python and vc numbers
Hi, I have a very simple problem, but I can't work out the answer. How do I return the current tty number in python? eg. what function/module should I use to figure out what tty my program was invoked from? Thanks -- Cheers, Dan http://members.iinet.net.au/~ddalton/ signature.asc Description: Digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: python and web pages
On Thu, Nov 19, 2009 at 09:43:50AM +0100, Diez B. Roggisch wrote: > >elinks, links, links2 and do the following. > >1. Open a certain web page and find the first text box on the page, and > >put this text into the form. > >2. Press the submit button, and wait for the result page to load. > >3. Click on the 15th link down the page. > > > >So, how do I do this, can anyone point me to some docs or modules that > >may help out here? > > Use the module "mechanize". It will mimic a browser, and allows you > to navigate to pages, fill in forms & submit them. Well, what seemed a very daunting task at first, proved to be quite easy, after a little bit of research. Thanks very much, I've written my script, and it works correctly, using the mechanize module. Thanks for everyone's help -- Cheers, Dan http://members.iinet.net.au/~ddalton/ signature.asc Description: Digital signature -- http://mail.python.org/mailman/listinfo/python-list
python and web pages
Hi, Here is my situation: I'm using the command line, as in, I'm not starting gnome or kde (I'm on linux.) I have a string of text attached to a variable,. So I need to use one of the browsers on linux, that run under the command line, eg. lynx, elinks, links, links2 and do the following. 1. Open a certain web page and find the first text box on the page, and put this text into the form. 2. Press the submit button, and wait for the result page to load. 3. Click on the 15th link down the page. So, how do I do this, can anyone point me to some docs or modules that may help out here? Thank you, -- Cheers, Dan http://members.iinet.net.au/~ddalton/ -- http://mail.python.org/mailman/listinfo/python-list
Re: storing variable value
Yes. I used a file, thanks. On Sat, Apr 11, 2009 at 09:30:40AM -0400, Dave Angel wrote: > > > Daniel Dalton wrote: >> Hi! >> >> I'm writing a program to provide me with battery warnings when my >> battery hits certain levels. It just checks the current level and does >> something. I plan to call it from a a cron job. But If the cron runs >> every minute, warnings every minute would be rather annoying. so is >> there a way to make the script check if it has already ran before? >> eg. can I write to a variable on one run of the program, and on the next >> read that value that was written? >> >> Thx! >> >> Daniel. >> >> > To put it simply, you want to store a value from one run of the script, > that persists till the next run of the same script. > > The most portable way to to that is to write a file. And since all you > care about is the time of writing, the file can be zero length. So > simply check for the file, determine it's creation time, and decide if > it's too recent to want to run again. If the file doesn't exist, or is > "old", then write the file and notify the user. > > > It's still messy to leave this bogus file around, so think carefully > about where to put it. If the source directory is writable, it's > reasonble to me to simply put it there. That way, if the program gets > deleted, it'll probably get deleted at the same time. Failing that, you > could put it in a TEMP directory. And perhaps the sneakiest place to > put it, if you're on Windows, is in the "run once on startup" directory. > Make it a do-nothing batch file, and Windows will remove it next time the > user restarts the system. > > > -- > http://mail.python.org/mailman/listinfo/python-list signature.asc Description: Digital signature -- http://mail.python.org/mailman/listinfo/python-list
storing variable value
Hi! I'm writing a program to provide me with battery warnings when my battery hits certain levels. It just checks the current level and does something. I plan to call it from a a cron job. But If the cron runs every minute, warnings every minute would be rather annoying. so is there a way to make the script check if it has already ran before? eg. can I write to a variable on one run of the program, and on the next read that value that was written? Thx! Daniel. signature.asc Description: Digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: What's the difference between generating a value and returning a value?
On Mon, Mar 23, 2009 at 03:12:19PM -0700, grocery_stocker wrote: > So what's the difference between generating a value and returning a > value? Well when you return, you would use the "return" keyword, I would imagine... I guess generating could mean many things, you can generate a value by operating on something, but I guess this does imply returning a value, because the function returning the value is generating a new value... But, I know for sure that when you return a value it is the information the function returns to the caller after operating on either no, or some arguments. Daniel. -- http://mail.python.org/mailman/listinfo/python-list
Re: searching strings
On Mon, Mar 09, 2009 at 06:24:24PM -0700, Chris Rebert wrote: > Normalize the case of the strings: > > i.lower() in j.lower() Too easy, thanks very much! Cheers, Daniel. -- http://mail.python.org/mailman/listinfo/python-list
searching strings
Hi, I'm writing a program where I search a variable (path), and see if it contains the whole string of variable name so: if name in path: else: One question about this, how can I make it do exactly what it's doing now, except ignore case? eg. if I do this: i="A" j="ab" i in j should return true... How do I do this? Thanks, Daniel. -- http://mail.python.org/mailman/listinfo/python-list
Re: NEWB: dividing numbers
Hi, On Mon, Mar 09, 2009 at 12:08:16AM +0100, Lo wrote: > I just tried python first time. > > 2/3 > > the result is zero That's because your dividing an int by an int to an int. The definition of an int is a "whole number". So just use floating point I think it's called, this should work, and does just tested with the python interactive shell: 2.0/3 Cheers, Daniel. -- http://mail.python.org/mailman/listinfo/python-list
Re: help with printing to stdout...
> or like this: > > print '\r'+str(percent), > > Then make sure it gets sent out, like this: > > sys.stdout.flush() Hey! Thanks very much, that did the trick! Thanks to everyone that replied, I discovered converting to str was crutial to actually print anything. :) Cheers, Daniel. -- http://mail.python.org/mailman/listinfo/python-list
help with printing to stdout...
Hi, I've got a program here that prints out a percentage of it's completion. Currently with my implimentation it prints like this: 0% 1% 2% 3% 4% etc taking up lots and lots of lines of output... So, how can I make it write the percentage on the same line eg. while working: print percent every time the line print percent is ran it should delete the old percentage from the screen, replacing it with the new one, so as to only use up one line... Basically I'm just printing a string of text to the screen and every time my print command is ran I would like the old text to be removed and my new text added (talking about the one line of the screen here)... This is a command line program, under linux... Thanks, Daniel. -- http://mail.python.org/mailman/listinfo/python-list