Re: Python newbie question re Strings and integers

2008-09-22 Thread Bruno Desthuilliers

rmac a écrit :

Ah!  Arghh!!! You are so correct on the usage of the ':'

Python syntax is a little different from what I am used to.


I don't know what you're used to, but chances are that more than the 
syntax differs !-)


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


Re: Python newbie question re Strings and integers

2008-09-20 Thread Bruno Desthuilliers

rmac a écrit :


the following code attempts to extract a symbol name from a string:
extensionStart = int(filename.rfind('.'))


rfind returns an int, so passing it to the int type constructor is useless.


filenameStart = int(filename.rfind('/'))


idem


#print 'Extension Start - ' + str(extensionStart)


The print statement isn't restricted to string objects. And FWIW, you 
can use string formating. The above should be either


  print Extension start - , extensionStart

or

  print Extension start - %s % extensionStart

As a side not, the naming convention in Python is to use all_lower for 
identifiers.



#print 'FileName Start - ' + str(filenameStart)


idem.


currentSymbol=filename[int(filenameStart),int(extensionStart)]


Two more useless int constructor calls.


Uncommenting the print statements clearly show the values to be
integers


If by 'the values', you mean objects bound to identifiers 
'filenameStart' and 'extensionStart', I fail to see how they could be 
anything else...



(and without the str casts actually provide int+string
errors)


Indeed. What should be the semantic of expression
  'answer is ' + 42

???


However, executing this code results in...
opening - /Users/rmac/Documents/Sandbox/data/MarketData/AA.csv
Traceback (most recent call last):
  File rHistFileToDB_Equities.py, line 25, in module
currentSymbol=filename[int(filenameStart),int(extensionStart)]
TypeError: string indices must be integers


the expression:

   int(filenameStart),int(extensionStart)

1/ takes the object currently bound to identifier 'filenameStart' and 
pass it to the int type constructor


2/ takes the object currently bound to identifier 'extensionStart' and 
pass it to the int type constructor


3/ build a tuple (actually, a pair of ints) from the results of the two 
above expressions




Then you try to use this tuple as an index on a string. A tuple is not a 
legal type for string (or any other sequence) subscripting.


The complete syntax for string subscripting is described in the 
FineManual. To make a long story short, it's :


string[start:end:step]

where end and step are optionals.

IOW, the correct syntax here is:
filename[filenameStart:extensionStart]


Now, while this is the correct *syntax*, it's not the correct *idiom*. 
There's a module named os.path, which provides the needed service. I 
leave it up to you to read the relevant part of the documentation...

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


Python newbie question re Strings and integers

2008-09-18 Thread rmac


the following code attempts to extract a symbol name from a string:
extensionStart = int(filename.rfind('.'))
filenameStart = int(filename.rfind('/'))
#print 'Extension Start - ' + str(extensionStart)
#print 'FileName Start - ' + str(filenameStart)
currentSymbol=filename[int(filenameStart),int(extensionStart)]

Uncommenting the print statements clearly show the values to be
integers (and without the str casts actually provide int+string
errors)

However, executing this code results in...
opening - /Users/rmac/Documents/Sandbox/data/MarketData/AA.csv
Traceback (most recent call last):
  File rHistFileToDB_Equities.py, line 25, in module
currentSymbol=filename[int(filenameStart),int(extensionStart)]
TypeError: string indices must be integers

Running Python 2.5.2_5 on OSX
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python newbie question re Strings and integers

2008-09-18 Thread Christian Heimes

rmac wrote:


the following code attempts to extract a symbol name from a string:
extensionStart = int(filename.rfind('.'))
filenameStart = int(filename.rfind('/'))
#print 'Extension Start - ' + str(extensionStart)
#print 'FileName Start - ' + str(filenameStart)
currentSymbol=filename[int(filenameStart),int(extensionStart)]

Uncommenting the print statements clearly show the values to be
integers (and without the str casts actually provide int+string
errors)

However, executing this code results in...
opening - /Users/rmac/Documents/Sandbox/data/MarketData/AA.csv
Traceback (most recent call last):
  File rHistFileToDB_Equities.py, line 25, in module
currentSymbol=filename[int(filenameStart),int(extensionStart)]
TypeError: string indices must be integers


You are using , inside filename[]. The splicing syntax is start:end, not 
start,end.


You are better off with using the appropriate APIs from the os.path module.

http://docs.python.org/lib/module-os.path.html

import os.path
filename = os.path.basename(path)
prefix, extension = os.path.splitext(filename)

Christian

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


Re: Python newbie question re Strings and integers

2008-09-18 Thread Miki
     currentSymbol=filename[int(filenameStart),int(extensionStart)]
Should be
currentSymbol=filename[int(filenameStart):int(extensionStart)]
(change , to :)

You don't need to convert to int all the time, rfind will return an
integer.

Also you can use os.path for this

from os.path import basename, splitext
currentSymbol = splitext(basename(filename))[0]

HTH,
--
Miki [EMAIL PROTECTED]
http://pythonwise.blogspot.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python newbie question re Strings and integers

2008-09-18 Thread rmac

Ah!  Arghh!!! You are so correct on the usage of the ':'

Python syntax is a little different from what I am used to.

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