473,472 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Unable to see os.environ['COLUMNS']

Not sure what's going on here and hoping for some insight:

tim@rubbish:~$ echo $COLUMNS
129
tim@rubbish:~$ python2.5
Python 2.5.2 (r252:60911, May 28 2008, 08:35:32)
[GCC 4.2.4 (Debian 4.2.4-1)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>import os
os.environ.get('COLUMNS')
'COLUMNS' in os.environ
False

I can coerce it by using

tim@rubbish:~$ COLUMNS=$COLUMNS python2.5
Python 2.5.2 (r252:60911, May 28 2008, 08:35:32)
[GCC 4.2.4 (Debian 4.2.4-1)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>import os
'COLUMNS' in os.environ
True

However, this seems hokey to me.

FWIW, this is in Bash on Debian.

What's the best way to read what seems to be a pseudo-environment
variable?

-tkc


Sep 15 '08 #1
3 3895
On 2008-09-13, Tim Chase <py*********@tim.thechases.comwrote:
Not sure what's going on here and hoping for some insight:

tim@rubbish:~$ echo $COLUMNS
129
tim@rubbish:~$ python2.5
Python 2.5.2 (r252:60911, May 28 2008, 08:35:32)
[GCC 4.2.4 (Debian 4.2.4-1)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>import os
>>os.environ.get('COLUMNS')
>> 'COLUMNS' in os.environ
False

I can coerce it by using

tim@rubbish:~$ COLUMNS=$COLUMNS python2.5
Python 2.5.2 (r252:60911, May 28 2008, 08:35:32)
[GCC 4.2.4 (Debian 4.2.4-1)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>import os
>>'COLUMNS' in os.environ
True

However, this seems hokey to me.

FWIW, this is in Bash on Debian.
In bash (and other descendants of the Bourne shell), there are
two types of environment variables: 1) local variables that are
not passed on to child processes and 2) exported variables that
_are_ passed on to children.

By default, when a variable is created it is local and will not
be inherited by sub-processes.
What's the best way to read what seems to be a
pseudo-environment variable?
You can't. You need to export the variable in the parent shell
before it exec's the child:

$ echo $COLUMNS
80

$ python -c "import os; print os.environ['COLUMNS']"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python2.5/UserDict.py", line 22, in __getitem__
raise KeyError(key)
KeyError: 'COLUMNS'

$ export COLUMNS

$ python -c "import os; print os.environ['COLUMNS']"
80
Now, on to the question you're about to ask:

Q: How do I find out how big my terminal is from a Python
program?

A: You use the TIOCGWINSZ ioctl call on the terminal's file
descriptor:
>>import sys,fcntl,termios,struct
data = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, '1234')
struct.unpack('hh',data)
(24, 80)

There's a more detailed explanation here (including an
explanation of what the third parameter to ioctl() does, and
how you detect changes in the window size):

http://mail.python.org/pipermail/pyt...ry/365710.html

There's also chance that you'd be better off just using ncurses or
newt for screen management, but that's another post.

--
Grant


Sep 15 '08 #2
>What's the best way to read what seems to be a
>pseudo-environment variable?

You can't. You need to export the variable in the parent shell
before it exec's the child:

$ export COLUMNS

$ python -c "import os; print os.environ['COLUMNS']"
80
This works well, and also manages to keep up to date across runs
as window-size changes. More importantly, it makes sense (minus
the "why doesn't bash automatically export COLUMNS to subshells"
question, but a little investigation shows I can use "set -a" or
"export COLUMNS" in my .bashrc and everything works).
Now, on to the question you're about to ask:

Q: How do I find out how big my terminal is from a Python
program?
You must be one of the folks working with the Python
time-machine. :) (okay, so the intent of my question was pretty
obvious)
A: You use the TIOCGWINSZ ioctl call on the terminal's file
descriptor:
>>>import sys,fcntl,termios,struct
data = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, '1234')
struct.unpack('hh',data)
(24, 80)

There's a more detailed explanation here (including an
explanation of what the third parameter to ioctl() does, and
how you detect changes in the window size):

http://mail.python.org/pipermail/pyt...ry/365710.html
Thanks, I'll read up on that, as well as investigate ncurses options.
There's also chance that you'd be better off just using ncurses or
newt for screen management, but that's another post.
The screen-width is merely for a little output formatting to
determine how many items can fit across. However, given the
opacity of the ioctl() call, it might not hurt to look into using
curses.

Thanks for the pointers,

-tkc

Sep 15 '08 #3
Thomas Bellman wrote:
Tim Chase <py*********@tim.thechases.comwrites:
>>$ export COLUMNS

$ python -c "import os; print os.environ['COLUMNS']"
80
>This works well, and also manages to keep up to date across runs
as window-size changes.

Now try this:

$ export COLUMNS
$ python -c "import time, os; time.sleep(60); print os.environ['COLUMNS']"

and change the window width during that sleep.
Yes, I did try something similar in my experimenting:

$ export COLUMNS
$ python
>>import os
print os.environ['COLUMNS']
80
>># resize to some other width
print os.environ['COLUMNS']
80

However, for my particular use-case, it's merely for output
formatting of a short-running process (akin to "svn status"
output). If you resize it in the middle of the sub-second
operation, you deserve what you get :)

It is disappointing that something so cross-platform as "what's
my output width" isn't built-in, requiring jumping through hoops,
in ways that aren't cross-platform. The ioctl() method worked on
my *nix-like boxes, as did the ncurses versions. However on
Win32, neither worked:

C:\Temp>python
Python 2.4.3 (#69, Mar 29 2006, 17:35:34)
[MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license"
for more information.
>># try the Curses version:
...
>>import curses
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "c:\Program Files\Python24\lib\curses\__init__.py",
line 15, in ?
from _curses import *
ImportError: No module named _curses
>># try the ioctl version:
...
>>import fcntl
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: No module named fcntl
>>import termios
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: No module named termios
>>>
So for cross-platform'ness on Win32, you wander over here:

http://code.activestate.com/recipes/440694/

Abstracting all this mess in a cross-platform sort of way would
be a nice "batteries included" tool to have. Based on the other
thread that Grant directed me to and the comments in the Win32
page, I'm not the first to have hoped for such an built-in.

Fortunately, for now I'm mostly focused on the *nix side of
things and have enough to get it working for now. Thanks to
those who gave their input.

-tkc



Sep 15 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: SL_McManus | last post by:
Hi All; I am fairly new to Perl. I have a file with close to 3000 lines that I would like to split out in a certain way. I would like to put the record type starting in column 1 for 2 spaces,...
3
by: Shawn | last post by:
Hi guys, I'm trying to compose a query that will select all columns from a table, but without any duplicates. E.g. table name is 'tblShipment' columns are: fldUnique(pk) | fldShipNo |...
2
by: Jim | last post by:
All of a sudden I am unable to create tables or add columns to existing tables if I specify CHARACTER greater than 254. If I try I get the following error: DB21034E The command was processed as...
6
by: John Ruiz | last post by:
Greetings, I originally posted this to microsoft.public.dotnet.framework.aspnet.datagridcontrol two weeks ago, but no one was able to answer. I am unable to dynamically add columns to a...
2
by: Ing. Rajesh Kumar | last post by:
Hi everybody I have a problem reading the content of a cell. When i use AutogenerateColumns = True or when i use <asp:BoundColumn DataField="COLUMN1" HeaderText="COLUMN1"/> then i can simply read...
3
by: Imran Aziz | last post by:
Hello All, I am getting the following error on our production server, and I dont get the same error on the development box. Unable to cast object of type 'System.Byte' to type 'System.String'. ...
4
by: Stropher | last post by:
I have the following: this.dataGridViewBill.DataSource = tblResult; //hide the following columns this.dataGridViewBill.Columns.Visible = false; //email this.dataGridViewBill.Columns.Visible =...
0
by: geeteshss | last post by:
the present problem is that i am unable to display data in datagrid....... but the data is visible in database..below is the code what should i do...earlier i could view it also below this code is...
0
by: sattu | last post by:
hi friends, i have one class and i have created two properties and filled this property through dataset but when i created object o this class to access this property to fill textbox i...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.