473,811 Members | 3,026 Online
Bytes | Software Development & Data Engineering Community
+ 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.ge t('COLUMNS')
'COLUMNS' in os.environ
False

I can coerce it by using

tim@rubbish:~$ COLUMNS=$COLUMN S 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 3921
On 2008-09-13, Tim Chase <py*********@ti m.thechases.com wrote:
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.ge t('COLUMNS')
>> 'COLUMNS' in os.environ
False

I can coerce it by using

tim@rubbish:~$ COLUMNS=$COLUMN S 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,termi os,struct
data = fcntl.ioctl(sys .stdout.fileno( ), termios.TIOCGWI NSZ, '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,termi os,struct
data = fcntl.ioctl(sys .stdout.fileno( ), termios.TIOCGWI NSZ, '1234')
struct.unpac k('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*********@ti m.thechases.com writes:
>>$ 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\__in it__.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
12845
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, the employer code in column 23 for 29 spaces and employer description in column 53 for 30 spaces. I have tried modifying an existing file with no real success. I haven't found anything that specifically answers my question. Any guidance would be...
3
12713
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 | fldDate | fldValue
2
11291
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 an SQL statement because it was not a valid Command Line Processor command. During SQL processing it returned: SQL0604N The length, precision, or scale attribute for column, distinct type,
6
2436
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 DataGrid in a web user control (.ascx) I am creating. This applies to VS.NET 2003 / Framework 1.1.
2
1329
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 the content of a cell in the OnItemDataBound method using the following code : For j = 0 To DT.Columns.Count - 1 If e.Item.Cells(j).Text = "something" Then Do Something Next Now i changed some columns to template columns as follows :...
3
13014
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'. here is the code that I used to create a table and then add columns to it later, later I populate the rows in the table.
4
22123
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 = false; //mobiltelefon When I run the programm, the columns are still displayed. I am developing with Visual C#2005 Express Edition Beta.
0
1401
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 the code which is in my html page plz help........ private void Page_Load(object sender, System.EventArgs e) { if (!Page.IsPostBack) { BindData(); }
0
3964
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 am geting this problem "Unable to cast object of type 'System.Data.DataColumn' to type 'System.IConvertible"
0
10386
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10398
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10133
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9204
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7669
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5554
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.