Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

checking 'type' programmatically

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


mrkafk at gmail

Nov 20, 2009, 2:10 AM

Post #1 of 3 (181 views)
Permalink
checking 'type' programmatically

Disclaimer: this is for exploring and debugging only. Really.

I can check type or __class__ in the interactive interpreter:

Python 2.6.2 (r262:71600, Jun 16 2009, 16:49:04)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>>
p=subprocess.Popen(['/bin/ls'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
>>> p
<subprocess.Popen object at 0xb7f2010c>
>>> (so, se) = p.communicate()
>>> so
'abc.txt\nbak\nbox\nbuild\ndead.letter\nDesktop\nhrs\nmbox\nmmultbench\nmmultbench.c\npyinstaller\nscreenlog.0\nshutdown\ntaddm_import.log\nv2\nvm\nworkspace\n'
>>> se
''
>>> so.__class__
<type 'str'>
>>> type(so)
<type 'str'>
>>> type(se)
<type 'str'>

But when I do smth like this in code that is ran non-interactively (as
normal program):

req.write('stderr type %s<br>' % type(se))
req.write('stderr class %s<br>' % str(se.__class__))

then I get empty output. WTF?

How do I get the type or __class__ into some object that I can display?


Why do that: e.g. if documentation is incomplete, e.g. documentation on
Popen.communicate() says "communicate() returns a tuple (stdoutdata,
stderrdata)" but doesn't say what is the class of stdoutdata and
stderrdata (a file object to read? a string?).

Regards,
mk



--
http://mail.python.org/mailman/listinfo/python-list


exarkun at twistedmatrix

Nov 20, 2009, 7:01 AM

Post #2 of 3 (177 views)
Permalink
Re: checking 'type' programmatically [In reply to]

On 10:10 am, mrkafk [at] gmail wrote:
>
>Disclaimer: this is for exploring and debugging only. Really.
>
>I can check type or __class__ in the interactive interpreter:
>
>Python 2.6.2 (r262:71600, Jun 16 2009, 16:49:04)
>[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
>Type "help", "copyright", "credits" or "license" for more information.
> >>> import subprocess
> >>>
>p=subprocess.Popen(['/bin/ls'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
> >>> p
><subprocess.Popen object at 0xb7f2010c>
> >>> (so, se) = p.communicate()
> >>> so
>'abc.txt\nbak\nbox\nbuild\ndead.letter\nDesktop\nhrs\nmbox\nmmultbench\nmmultbench.c\npyinstaller\nscreenlog.0\nshutdown\ntaddm_import.log\nv2\nvm\nworkspace\n'
> >>> se
>''
> >>> so.__class__
><type 'str'>
> >>> type(so)
><type 'str'>
> >>> type(se)
><type 'str'>
>
>But when I do smth like this in code that is ran non-interactively (as
>normal program):
>
>req.write('stderr type %s<br>' % type(se))
>req.write('stderr class %s<br>' % str(se.__class__))
>
>then I get empty output. WTF?
>
>How do I get the type or __class__ into some object that I can display?

Hooray for HTML.

You asked a browser to render "stderr type <type 'str'><br>". This
isn't valid HTML, so pretty much any behavior goes. In this case, the
browser seems to be discarding the entire <type 'str'> - not too
suprising, as it has some features in common with an html tag.

Try properly quoting your output (perhaps by generating your html with a
real html generation library).

Jean-Paul
--
http://mail.python.org/mailman/listinfo/python-list


billy.earney at gmail

Nov 20, 2009, 7:06 AM

Post #3 of 3 (179 views)
Permalink
RE: checking 'type' programmatically [In reply to]

Try looking at the function 'isinstance', so for example

if isinstance(obj, str):
print "object is a string.."
elif isinstance(obj, int):
print "object is an integer.."

-----Original Message-----
From: python-list-bounces+billy.earney=gmail.com [at] python
[mailto:python-list-bounces+billy.earney=gmail.com [at] python] On Behalf Of
mk
Sent: Friday, November 20, 2009 4:10 AM
To: python-list [at] python
Subject: checking 'type' programmatically


Disclaimer: this is for exploring and debugging only. Really.

I can check type or __class__ in the interactive interpreter:

Python 2.6.2 (r262:71600, Jun 16 2009, 16:49:04)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>>
p=subprocess.Popen(['/bin/ls'],stdout=subprocess.PIPE,stderr=subprocess.PIPE
)
>>> p
<subprocess.Popen object at 0xb7f2010c>
>>> (so, se) = p.communicate()
>>> so
'abc.txt\nbak\nbox\nbuild\ndead.letter\nDesktop\nhrs\nmbox\nmmultbench\nmmul
tbench.c\npyinstaller\nscreenlog.0\nshutdown\ntaddm_import.log\nv2\nvm\nwork
space\n'
>>> se
''
>>> so.__class__
<type 'str'>
>>> type(so)
<type 'str'>
>>> type(se)
<type 'str'>

But when I do smth like this in code that is ran non-interactively (as
normal program):

req.write('stderr type %s<br>' % type(se))
req.write('stderr class %s<br>' % str(se.__class__))

then I get empty output. WTF?

How do I get the type or __class__ into some object that I can display?


Why do that: e.g. if documentation is incomplete, e.g. documentation on
Popen.communicate() says "communicate() returns a tuple (stdoutdata,
stderrdata)" but doesn't say what is the class of stdoutdata and
stderrdata (a file object to read? a string?).

Regards,
mk



--
http://mail.python.org/mailman/listinfo/python-list

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.