Re: ['a', 'b'][True] results 'b' But how?

2007-07-05 Thread Robert Bauck Hamar
kath wrote:

 Hi,
 
 Can any one please tell me how is the following code is working?
 ['a','b'] is a list of string

Yes.

 and [True] is list of boolean value. 

No. It's the subscription operator applied to the list of strings.
a = ['a', 'b']
a[True]
may be clearer.

 How is it making effect?

 int(True)
1
 int(False)
0
 isinstance(True, int)
True
 bool.__bases__
(type 'int',)

 code Python24
 
 ['a','b] [True]
 'b'
 ['a','b'] [False]
 'a'
 ['a','b']['some_string' == r'some_string']
 'b'
 ['a','b']['some_string' == r'somestring']
 'a'
 
 code

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


Re: Newbie question: how to get started?

2007-06-17 Thread Robert Bauck Hamar
ed wrote:

 I should also mention that I know C/C++, Perl, Javascript, the basics
 of mySQL, and HTML/CSS.  If anyone has tried to enter python from these
 angles, I'd be grateful to hear from you.

What's C/C++?

Well, I knew C, C++, Perl, Java, SQL, and HTML before learning Python. And
some more languages. If you really know all these languages, Python should
be a no-brainer, and a practical language in use.

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


Re: Re printing on same line.

2007-06-15 Thread Robert Bauck Hamar
Jerry Hill wrote:

 On 6/15/07, HMS Surprise [EMAIL PROTECTED] wrote:
 I want to print a count down timer on the same line. I tried

 print '\r', timeLeft,

 which just appends to the same line.
 
 Sounds to me like whatever you're printing to doesn't do what you
 expect when it encounters a carriage return (\r).  Is your program
 running in a terminal?  Both the windows cmd.exe shell and bash under
 linux seem to do the right thing when encountering a '\r'.

Actually, bash has nothing to do with how the terminal handles \r. The job
of the shell (bash, ksh, csh, sh ...) is to execute your script when you
type its name.

Outputting a \r might or might not move the cursor to the beginning of the
line. It's completely system specific, and even on the same OS, it depends
on the capabilities of the actual terminal the programs run on, and on some
terminal emulators it might depend on configuration settings.

If you need to explore the capabilities of the terminal, curses will be a
good place to start.

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


Re: Is there any way to catch expections when call python method in C++

2007-06-13 Thread Robert Bauck Hamar
Allen wrote:

 I use try catch, but cannot catch the execeptions of execution python
 method.
 
 PYCALL_API void PyCall(const char * pszModule, const char * pszFunc,
 void * pArg)
 {
 if (pszModule == NULL || pszFunc == NULL)
 {
 return;
 }
 
 Py_Initialize();
 
 PyObject * pModule = NULL;
 PyObject * pFunc   = NULL;
 
 try {
 
 pModule = PyImport_ImportModule(pszModule);
 pFunc   = PyObject_GetAttrString(pModule, pszFunc);
 
 PyEval_CallObject(pFunc, (PyObject*)pArg);
 } catch (...) {
fprintf(stderr, Error: call python method failed);
 }
 
 Py_Finalize();
 }
 
 Can I catch it from C++?

No. CPython is written in C, not C++, and C has no concept of exceptions.
Exceptions in Python is usually indicated by return value in the
interpreter, and has no mapping to the C++ exception model. You should
never let C++ exceptions propagate into the python functions either.
PyImport_ImportModule will return NULL if an exception occured, and so will
also PyObject_GetAttrString and PyEval_CallObject do.

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


Re: stdout/err and C extentions

2007-06-12 Thread Robert Bauck Hamar
hg wrote:

 Hi,
 
 I have the following
 
 * C extention  - redir.c
 
 
 #include Python.h
 
 PyObject * test_redir_test(PyObject *self) {
   fprintf(stdout, Hello from an extention!\n);
   Py_INCREF(Py_None);
   return Py_None;
 }
 
[...]
 *** python test script: test.py:
 import sys
 
 
 class My_Stdout:
 def write(self, p_string):
 l_file = open('res.txt','a')
 l_file.write(p_string)
 l_file.close
 
 
 sys.stdout = My_Stdout()
 
 print 'toto'
 import test_redir
 
 
 
 test_redir.test()
 
 
 
  Question:
  
 print 'toto' does go to res.txt while Hello from an extention!\n goes
 to the console.
 
 Any clue ?

There is no portable way to change the location of stdout during execution
of a program. If you want to print with whatever is sys.stdout from an
extension module, you should call sys.stdout's write method dynamically
from C.

-- 
Robert Bauck Hamar

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


Re: stdout/err and C extentions

2007-06-12 Thread Robert Bauck Hamar
hg wrote:

 Robert Bauck Hamar wrote:
 
 hg wrote:
 
 Hi,
 
 I have the following
 
 * C extention  - redir.c
 
 
 #include Python.h
 
 PyObject * test_redir_test(PyObject *self) {
   fprintf(stdout, Hello from an extention!\n);
   Py_INCREF(Py_None);
   return Py_None;
 }
 
 [...]
 *** python test script: test.py:
 import sys
 
 
 class My_Stdout:
 def write(self, p_string):
 l_file = open('res.txt','a')
 l_file.write(p_string)
 l_file.close
 
 
 sys.stdout = My_Stdout()
 
 print 'toto'
 import test_redir
 
 
 
 test_redir.test()
 
 
 
  Question:
  
 print 'toto' does go to res.txt while Hello from an extention!\n
 goes to the console.
 
 Any clue ?
 
 There is no portable way to change the location of stdout during
 execution of a program. If you want to print with whatever is sys.stdout
 from an extension module, you should call sys.stdout's write method
 dynamically from C.
 
 
 Robert, thanks,
 
 I understand that sys.stdout and stdout of an extention are two different
 entities ... correct ?

Yes. Python's sys.stdout and C's stdout are both objects wrapping the call
os.write(1, string) in Python and write(1, string, strlen(string)) in
C, or some other function on platforms using other mechanisms for output.

In that way sys.stdout in Python and stdout in C will be portable to many
more systems.

-- 
Robert Bauck Hamar

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


Re: newb: Join two string variables

2006-12-05 Thread Robert Bauck Hamar
johnny wrote:

Please don't top post. Arrange your answer so that your comments follow what
you comment.

 In my code, I have the following:
 
 p = posixpath.basename(e).strip

make this:
p = posixpath.basename(e).strip()

 filename = download_dir+p
 
 I am getting the following error:
 
  filename = download_dir+p
 TypeError: cannot concatenate 'str' and 'builtin_function_or_method'
 objects
 

Which is correct. Because you forgot to _call_ strip, you just stored (a
reference to) strip itself in p, and not its return value.

(rest of message deleted, because I don't comment on it.)

-- 
Robert Bauck Hamar
Der er to regler for suksess:
1. Fortell aldri alt du vet.
   - Roger H. Lincoln
-- 
http://mail.python.org/mailman/listinfo/python-list