Re: [Tutor] Equivalent of PHP's __FUNCTION__ ?

2004-12-02 Thread Kent Johnson
Here is one way to do it, based on this Python Cookbook recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66062 def debug(msg): import sys frame = sys._getframe(1) name = frame.f_code.co_name line_number = frame.f_lineno filename = frame.f_code.co_filename retu

Re: [Tutor] Equivalent of PHP's __FUNCTION__ ?

2004-12-02 Thread Kent Johnson
Kent Johnson wrote: Here is one way to do it, based on this Python Cookbook recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66062 Use of sys._getframe() is generally considered something of a skanky hack. (Though it is often done anyway.) Here is another solution that only uses

Re: [Tutor] unittest.makeSuite question

2004-12-02 Thread Kent Johnson
makeSuite() creates a TestSuite object that consolidates all the test methods of the class named by the first argument. Test methods are identified by having names starting with the second argument. 'test' is the default value so you could omit it; in fact the most recent documentation does omit

Re: [Tutor] Creating & Handling lots of objects

2004-12-03 Thread Kent Johnson
If you want to be able to access the objects by name, you can put them in a dict. For example, MyObjects={} l=["a","b","c"] for i in l: MyObjects[i] = MyClass(i) Then you can refer to MyObjects["a"] If all the operations on the objects are done to all objects in a batch, putting them in a

Re: [Tutor] Global presets ?

2004-12-04 Thread Kent Johnson
You are on the right track. Put your common definitions in a configuration module like this: # Config.py arch_data_dir='/home/dave/mygg/gg1.3/arch_data' data_dir='/home/dave/mygg/gg1.3/data' Then in client code, import Config. When you use the names defined in Config you have to prefix them with

Re: [Tutor] Simple RPN calculator

2004-12-04 Thread Kent Johnson
I didn't get the attachment, can you try again? Thanks, Kent Just Incase wrote: Hi Tutors, I am new to programming and so don't know "anything" much, yet. I am having problem with implementing a simple RPN calculator in python. I have tried editing some other programs I have been referenced to

Re: [Tutor] How to select particular lines from a text

2004-12-04 Thread Kent Johnson
kumar, Here is a solution for you. The makeSections() function will iterate through blocks in the file and return each one in turn to the caller. makeSections() is a generator function - the use of yield makes it one. That means that it returns an iterator that can be used in a for loop. Each ti

Re: [Tutor] Broblem with exiting a Tkinter app

2004-12-04 Thread Kent Johnson
Mark Kels wrote: Hi all , I got 2 questions for you guys. The fist question: I wrote small Tkinter app while laerning about the Radiobutton widget, and I added a "Quit" button, like this: bb=Button(root, text="Quit", fg="BLUE", command=root.quit).pack() When I pressed the button the app crashed and

Re: [Tutor] eval and exec

2004-12-04 Thread Kent Johnson
Marilyn Davis wrote: Thank you. You guys are great. I was trying to eval("import %s" % something). exec("import %s" % something) works just fine and now I understand why. But, why is this so extremely dangerous? The danger is in exec'ing code whose source is not trusted. Using exec to import a

Re: [Tutor] Simple RPN calculator

2004-12-05 Thread Kent Johnson
RPN reverses the order of operator and operand, it doesn't reverse the whole string. So in Polish Notation 2 + 3 is +23 and (2 + 3) - 1 is -+231; in RPN they become 23+ and 23+1- Kent Brian van den Broek wrote: By analogy, I had always assumed that Polish Arithmetic would read (2 + 3) - 1 and 2 +

Re: [Tutor] unicode help (COM)

2004-12-05 Thread Kent Johnson
Rene Bourgoin wrote: Thanks for the responses. i'm a non-programmer and was learning/playing with some pyhton COM . I'm trying to get my resluts from an excel spreadsheet to be saved or printed or stored as a python string. when i run this the results are in unicode. What problem are the unic

Re: [Tutor] psyco 1.3 is out, with support for Python 2.4

2004-12-06 Thread Kent Johnson
Dick Moores wrote: Here's what I learned from Kent about installing psyco (for Windows): Download psyco from http://psyco.sourceforge.net. Unzip the zip file. Copy the folder psyco-1.3/psyco into Python24/Lib/site-packages. (Create site-packages if you don't already have it.) Should be good to go

Re: [Tutor] Python 2.3.5 out in January??

2004-12-07 Thread Kent Johnson
I think the idea is to back-port bugfixes from 2.4 to 2.3. Then that is the end of the line for 2.3. Kent Dick Moores wrote: Just saw this on comp.lang.python.announce. I don't understand this. Why is a new version of 2.3 being worked on after 2.4 has been released? I

Re: [Tutor] String matching?

2004-12-07 Thread Kent Johnson
Regular expressions are a bit tricky to understand but well worth the trouble - they are a powerful tool. The Regex HOW-TO is one place to start: http://www.amk.ca/python/howto/regex/ Of course, Jamie Zawinsky famously said, "Some people, when confronted with a problem, think 'I know, I'll use r

Re: [Tutor] Printing two elements in a list

2004-12-07 Thread Kent Johnson
kumar s wrote: Dear group, I have two lists names x and seq. I am trying to find element of x in element of seq. I find them. However, I want to print element in seq that contains element of x and also the next element in seq. So I tried this piece of code and get and error that str and int c

Re: [Tutor] Printing two elements in a list

2004-12-07 Thread Kent Johnson
kumar, Looking at the quantity and structure of your data I think the search you are doing is going to be pretty slow - you will be doing 4504 * 398169 = 1,793,353,176 string searches. Where does the seq data come from? Could you consolidate the pairs of lines into a single record? If you do tha

Re: [Tutor] String matching?

2004-12-07 Thread Kent Johnson
Liam Clarke wrote: And thanks for the re, hopefully I won't have to use it, but it gives me a starting point to poke the re module from. BTW Python comes with a nice tool for experimenting with regular expressions. Try running Python23\Tools\Scripts\redemo.py Kent

Re: [Tutor] Import Statement Failing With IDLE?

2004-12-07 Thread Kent Johnson
I don't know why you are getting different results, but I do know that you aren't supposed to put the whole psyco-1.3 folder inside site-packages. Just put the psyco folder (from inside psyco-1.3) into site-packages. Also the os.chdir() doesn't have any effect on import. You could compare sys.pa

Re: [Tutor] Removing/Handing large blocks of text

2004-12-08 Thread Kent Johnson
I would use a loop with a flag to indicate whether you are in the block or not. If the tags are always and on a line by themselves you don't need an re: lines = [] appending = True f = open('foobar.txt', 'r') for line in f: if appending: lines.append(line) if line.strip() == '':

Re: [Tutor] "TypeError: 'int' object is not callable"??

2004-12-08 Thread Kent Johnson
A callable is something that can be called with functional notation. It can be a function, a class, or in some cases a class instance. In general, any object that has a __call__() special method is callable. The callable() built-in tells you if an object is callable, though you can also just try

Re: [Tutor] Re: Could I have used time or datetime modules here?

2004-12-09 Thread Kent Johnson
Liam Clarke wrote: Hi Brian and Dick, I for one have learnt a lot from this combined poke around the workings of datetime. The datetime.year thing never occurred to me, yet it was so obvious when I saw it being used. I give you, my python alarm clock timing mechanism, final version! http://www.ra

Re: [Tutor] MemoryError

2004-12-09 Thread Kent Johnson
Liam, I'm sorry to hear you so discouraged. It sourds like your program has gotten too big for you to clearly understand what it is doing. An earlier poster suggested that you break your program up into functions. This is a very good idea, especially if you do it from the start. A very powerful

Re: [Tutor] sorting a list of dictionaries

2004-12-09 Thread Kent Johnson
If you can use Python 2.4 it is very simple using the new key= parameter to sort and operator.itemgetter: >>> import operator >>> ds = [{'name':'foo.txt','size':35}, {'name':'bar.txt','size':36}] >>> ds.sort(key=operator.itemgetter('name')) >>> ds [{'name': 'bar.txt', 'size': 36}, {'name': 'foo.t

Re: [Tutor] sorting a list of dictionaries

2004-12-09 Thread Kent Johnson
Using sort() with a user compare function is not recommended when you care about performance. The problem is that the sort function has to call back into Python code for every compare, of which there are many. The decorate - sort - undecorate idiom is the preferred way to do this in Python < 2.4

Re: [Tutor] MemoryError

2004-12-09 Thread Kent Johnson
Liam, Here's a nifty re trick for you. The sub() method can take a function as the replacement parameter. Instead of replacing with a fixed string, the function is called with the match object. Whatever string the function returns, is substituted for the match. So you can simplify your code a bit

Re: [Tutor] PDF and Python

2004-12-09 Thread Kent Johnson
The reportlab toolkit is frequently recommended, though I haven't tried it myself. http://www.reportlab.org/ Kent Jason Child wrote: Hey there. Does anyone know of a way to output PDFs with python? I have some data that I have processed from a series of textfiles that I would like to provide PDF f

Re: [Tutor] MemoryError

2004-12-10 Thread Kent Johnson
Well, that's a regex only a mother could love. :-) I can see why you were happy to find the (?P) form of grouping. You should compile textObj and urlObj outside of getVals. You could just pull the four lines that create textObj and urlObj outside of the function (into global scope). You just hav

Re: [Tutor] os.listdir fn

2004-12-10 Thread Kent Johnson
Have you looked at the glob module? >>> import glob >>> glob.glob('src/*.properties') ['src\\jdbc.properties', 'src\\jdbc_local.properties', 'src\\jdbc_qa.properties', 'src\\jdbc_test.properties', 'src\\log4j.jnlp.properties', 'src\\log4j.properties', 'src\\version.properties'] Kent Nandan wrote

Re: [Tutor] os.listdir fn

2004-12-11 Thread Kent Johnson
Are you sure this does what you want? You compute 'scripts' and 'filenames' and throw them away. I think this function will return the base name of all the files *and folders* in ./icon.scripts/opname. If you are trying to return the base name of every file or directory whose extension is '.scrip

Re: [Tutor] Re: Could I have used time or datetime modules here?

2004-12-11 Thread Kent Johnson
Liam Clarke wrote: As far as I know, you'll either have to - run Python 2.3 or - run Python 2.3 until they release a new version of Pygame, Yes, any Python addon that uses .pyd or .dll files has to be recompiled for Python2.4. Pygame has many .pyd files so you have to use Python 2.3. Here is a th

Re: [Tutor] function that returns a fn

2004-12-11 Thread Kent Johnson
Yes, it really is that simple. :-) A common example is a function that makes a function which adds a constant to its argument: >>> def makeadder(n): ... def adder(x): ... return x + n ... return adder ... Make a function that adds 3 to its argument...note there is no special syntax for the

Re: [Tutor] using a function as a dictionary value?

2004-12-13 Thread Kent Johnson
Liam Clarke wrote: Hey Justin, Tricky one this.. as far as I know, and I'm a beginner myself, a dictionary stores a reference to the function, not the actual function. Yes. In fact this is a good way to think about all variables in Python. A variable stores a reference to a value, not the value it

Re: [Tutor] Problems with unsigned integers

2004-12-13 Thread Kent Johnson
It seems that ntohl doesn't understand about unsigned values, at least on Win32: Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from struct import pack, unpack >>> pack('L', -1) '\xff\xff\xff\xf

Re: [Tutor] cgi with system calls

2004-12-13 Thread Kent Johnson
Nik wrote: hi, I'm trying to write a python cgi script that can control certain processes on my server, but I'm having some trouble. The script is; #!/usr/bin/python import cgitb; cgitb.enable() print "Content-type: text/plain\n\n" You may need explicit \r\n here, I'm not sure: print "Content-typ

Re: [Tutor] Regexp Not Matching on Numbers?

2004-12-14 Thread Kent Johnson
I'm not too sure what you are trying to do here, but the re in your code matches the names in your example data: >>> import re >>> name = 'partners80_access_log.1102723200' >>> re.compile(r"([\w]+)").match( name ).groups() ('partners80_access_log',) One thing that may be tripping you up is that r

Re: [Tutor] Regexp Not Matching on Numbers?

2004-12-14 Thread Kent Johnson
Max Noel wrote: On Dec 14, 2004, at 18:15, Gooch, John wrote: So far I have tried the following regular expressions: "\d+" "\d*" "\W+" "\W*" "[1-9]+" and more... I think you have to escape the backslashes. or use raw strings like r"\d+" which is what is in the sample code... Kent _

Re: [Tutor] check_range

2004-12-14 Thread Kent Johnson
You misunderstand what range() does. It returns a list of numbers starting with the lower one and up to but not including the upper one: >>> range(5) [0, 1, 2, 3, 4] >>> range(5, 10) [5, 6, 7, 8, 9] To test for a number in a range you can use 10 < n < 90: >>> x = 1 >>> 10 < x < 90 False >>> x = 1

Re: [Tutor] check_range

2004-12-15 Thread Kent Johnson
Brian van den Broek wrote: DogWalker said unto the world upon 2004-12-15 00:32: "Brian van den Broek" <[EMAIL PROTECTED]> said: I have a some style suggestions for you, too. Try it this way: def check_in_range(value): in_range = False if 9 < value < 90: in_range = True

Re: [Tutor] dbcp module

2004-12-15 Thread Kent Johnson
Googling for 'python dbcp' turns up this recipe, is this what you are looking for? http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81189 Kent Rene Bourgoin wrote: Anyone know where I can download the dbcp module for Python ___ No banners. N

Re: [Tutor] A simpler mousetrap

2004-12-16 Thread Kent Johnson
As Wolfram pointed out, os.path.splitext() is a good way to do this. It is also more robust than the other solutions because it does the right thing if there is no extension on the original file name. I just want to say that your first solution can be written much more simply as x=a[:a.rfind('.')

[Tutor] Multiple returns in a function

2004-12-16 Thread Kent Johnson
Brian van den Broek wrote: Kent Johnson said unto the world upon 2004-12-15 20:16: > I would write > def is_leap_year(year): > try: > datetime.date(year, 2, 29) > return True > except ValueError: > return False Not an adherent of the

Re: [Tutor] am I missing another simpler structure?

2004-12-16 Thread Kent Johnson
It's probably worth pointing out that these two functions are not entirely equivalent: def t1(): if condition: return True return False def t2(): return condition because 'condition' does not have to evaluate to a boolean value, it can be any Python value. Here is a simple example where

Re: [Tutor] Python structure advice ?

2004-12-16 Thread Kent Johnson
Dave S wrote: Dave S wrote: The 'remembering where is was' seems a continuous stumbling block for me. I have though of coding each module as a class but this seems like a cheat. I could declare copious globals, this seems messy, I could define each module as a thread & get them talking via queue

Re: [Tutor] "TypeError: 'int' object is not callable"??

2004-12-17 Thread Kent Johnson
Jacob S. wrote: Ha! That's what I was looking for! The builtin apply function! The only way I could send the *args to the function was through a list, and function calls see a list as one argument. The apply argument doesn't! Thanks Bob. apply() is deprecated; it has been replaced by 'extended call

Re: [Tutor] dbcp module

2004-12-17 Thread Kent Johnson
. http://jakarta.apache.org/ --- On Wed 12/15, Kent Johnson < [EMAIL PROTECTED] > wrote: From: Kent Johnson [mailto: [EMAIL PROTECTED] To: Cc: [EMAIL PROTECTED] Date: Wed, 15 Dec 2004 20:19:22 -0500 Subject: Re: [Tutor] dbcp module Googling for 'python dbcp' turns up this

Re: [Tutor] Python structure advice ?

2004-12-17 Thread Kent Johnson
Dave S wrote: Separate modules is good. Separate directories for anything other than big programs (say 20 or more files?) is more hassle than its worth. The files are better kept in a single directory IMHO. The exception being modules designed for reuse... It just makes life simpler! Ive tried t

Re: [Tutor] "TypeError: 'int' object is not callable"??

2004-12-17 Thread Kent Johnson
Jacob S. wrote: Hey, could you give an example? I'll try... Here is range with three explicit arguments >>> range(1, 10, 2) [1, 3, 5, 7, 9] Here is range with the arguments supplied in a list; it does the same thing >>> args = [1, 10, 2] >>> range(*args) [1, 3, 5, 7, 9] Here is an example with z

Re: [Tutor] "TypeError: 'int' object is not callable"??

2004-12-17 Thread Kent Johnson
Jacob S. wrote: Thank you! Wait, though. How do I do this? def differentnoofvars(*args,**kwargs): ## By the way, is it **kwargs or **kwds? Call it what you like, it's an ordinary function parameter. kwds is commonly used but you can use kwargs. print kwargs another(kwargs) Should be anoth

Re: [Tutor] dbcp module

2004-12-17 Thread Kent Johnson
The recipe you cite has the pp() function and an example of its use. It sounds like that is what you want. Kent Rene Bourgoin wrote: Ive been learning to interact with databases using python and i was looking for ways to return a SELECT query result in a plain format. what i mean by plain form

Re: [Tutor] least squares

2004-12-17 Thread Kent Johnson
Have you tried contacting the author of the Scientific package? His email address is on the main web page. Kent mdcooper wrote: Hi Danny, Thanks for the reply - I was purposely vague just to see what people would ask for. The code uses LinearAlgebra.py from Numeric and LeastSquares.py from Sci

Re: [Tutor] maximum value in a Numeric array

2004-12-10 Thread Kent Johnson
Are you using numarray? If so, there appears to be a max method of an array, so you can try b = array([[1,2],[3,4]]) b.max() Note that your method of finding the max row, then finding the max in the row, will not in general give the correct result. Sequences are compared lexicographically - the f

Re: [Tutor] am I missing another simpler structure?

2004-12-15 Thread Kent Johnson
Brian van den Broek wrote: I've begun to wonder if I am overlooking a improvement similar to that in DogWalker's suggestion. As an example of the sort of thing I have been doing: import datetime def is_leap_year(year): '''-> boolean Returns True or False as year is, or is not, a leap yea

Re: [Tutor] question about function inside of function

2010-01-10 Thread Kent Johnson
On Sat, Jan 9, 2010 at 8:03 AM, spir wrote: > Do you realize the inner func will be redefined before each call? Meaning in > your case n calls x n outer loops x n inner loops. >   def f() ... > is actually a kind of masked assignment >   f = function()... That's true, but it is pretty inexpensi

Re: [Tutor] Question on "import foobar" vs "from foobar import *"

2010-01-10 Thread Kent Johnson
On Sat, Jan 9, 2010 at 3:50 AM, spir wrote: > Lie Ryan dixit: > >> only use "from module import *" if the >> module was designed for such use > > In most cases, this translates to: the imported module defines __names__, > which holds the list of names (of the objects) to be exported. Check it. >

Re: [Tutor] composing one test suite from two test cases

2010-01-11 Thread Kent Johnson
On Sun, Jan 10, 2010 at 10:44 PM, Tom Roche wrote: > > How to create a single unittest test suite class that runs all methods > from multiple TestCase classes? Why I ask: > > I'm trying to relearn TDD and learn Python by coding a simple app. > Currently the app has 2 simple functional classes, Pid

Re: [Tutor] what is the equivalent function to strtok() in c++

2010-01-11 Thread Kent Johnson
On Mon, Jan 11, 2010 at 2:33 AM, sudhir prasad wrote: > hi, > what is the equivalent function to strtok() in c++, > what i need to do is to divide a line into different strings and store them > in different lists,and write them in to another file To split on a single character or a fixed sequence

Re: [Tutor] Tips

2010-01-11 Thread Kent Johnson
On Mon, Jan 11, 2010 at 12:33 AM, VacStudent wrote: > I would like to get a python book, how and where to get one? Amazon.com? Or lots of free resources online. Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options:

Re: [Tutor] composing one test suite from two test cases

2010-01-11 Thread Kent Johnson
On Mon, Jan 11, 2010 at 8:53 AM, Tom Roche wrote: > Kent Johnson Mon, 11 Jan 2010 06:42:39 -0500 >> However I don't recommend this style of organizing tests. I prefer >> using nose for test discovery, it saves the work of creating all the >> aggregating suites. >

Re: [Tutor] Numpy unexpected result: subtraction of cell values

2010-01-11 Thread Kent Johnson
On Mon, Jan 11, 2010 at 2:02 PM, Carnell, James E wrote: >> I want to subtract the Red Value in an array cell from a neighboring >> Red Value cell. >> >> >>> pictArray[39][4]    #pixel at 39 4 >> array([150, 140, 120], dtype=unint8) >> >> >>> pictArray[39][5]    #p

Re: [Tutor] samples on sort method of sequence object.

2010-01-12 Thread Kent Johnson
On Tue, Jan 12, 2010 at 9:39 AM, Make Twilight wrote: >  I can understand how to use parameters of cmp and reverse,except the > key parameter... >  Would anyone give me an example of using sort method with key parameter? http://personalpages.tds.net/~kent37/kk/7.html Kent ___

Re: [Tutor] samples on sort method of sequence object.

2010-01-13 Thread Kent Johnson
On Wed, Jan 13, 2010 at 2:21 PM, Stefan Behnel wrote: > Hugo Arts, 13.01.2010 15:25: >> >> Here is my solution for the general case: >> >> from itertools import groupby >> def alphanum_key(string): >>    t = [] >>    for isdigit, group in groupby(string, str.isdigit): >>        group = ''.join(gro

Re: [Tutor] Keeping a list of attributes of a certain type

2010-01-14 Thread Kent Johnson
On Wed, Jan 13, 2010 at 9:24 PM, Guilherme P. de Freitas wrote: > Hi everybody, > > Here is my problem. I have two classes, 'Body' and 'Member', and some > attributes of 'Body' can be of type 'Member', but some may not. The > precise attributes that 'Body' has depend from instance to instance, > a

Re: [Tutor] Keeping a list of attributes of a certain type

2010-01-14 Thread Kent Johnson
On Wed, Jan 13, 2010 at 11:15 PM, Guilherme P. de Freitas wrote: > Ok, I got something that seems to work for me. Any comments are welcome. > > > class Member(object): >    def __init__(self): >        pass > > > class Body(object): >    def __init__(self): >        self.members = [] > >    def __

Re: [Tutor] Searching in a file

2010-01-15 Thread Kent Johnson
On Fri, Jan 15, 2010 at 4:24 AM, Paul Melvin wrote: > Hi, > > Thanks very much to all your suggestions, I am looking into the suggestions > of Hugo and Alan. > > The file is not very big, only 700KB (~2 lines), which I think should be > fine to be loaded into memory? > > I have two further que

Re: [Tutor] Keeping a list of attributes of a certain type

2010-01-15 Thread Kent Johnson
On Fri, Jan 15, 2010 at 3:43 PM, Guilherme P. de Freitas wrote: > Ok, I checked the answers you all gave me, and the suggestions seem to be 2: > > a. Compute the list of members on the fly, with a list comprehension > that looks up stuff on self.__dict___; > > b. Stay with my solution, but substit

Re: [Tutor] smtp project

2010-01-17 Thread Kent Johnson
On Sun, Jan 17, 2010 at 3:45 PM, Kirk Z Bailey wrote: > I am writing a script that will send an email message. This will run in a > windows XP box. The box does not have a smtp server, so the script must > crete not merely a smtp client to talk to a MTA, it must BE one for the > duration of sendin

Re: [Tutor] smtp project

2010-01-18 Thread Kent Johnson
smtplib to talk to the remote MTA on port 25? - The std lib contains a simple SMTP server in smtpd.py - Lamson is a more robust SMTP server http://lamsonproject.org/ - Twisted has SMTP support http://twistedmatrix.com/trac/wiki/TwistedMail Kent PS Please Reply All to reply on list. >

Re: [Tutor] Python workspace - IDE and version control

2010-01-19 Thread Kent Johnson
On Mon, Jan 18, 2010 at 4:17 PM, Alan Gauld wrote: > I use plain old RCS for version control because its just me working on the > code. Wow. You should take a look at Mercurial. It is so easy to set up a Mercurial repository for a local project - just hg init # create a repository hg st # show w

Re: [Tutor] Python workspace - IDE and version control

2010-01-19 Thread Kent Johnson
On Tue, Jan 19, 2010 at 9:12 AM, Andreas Kostyrka wrote: > The cool part about git that I've not yet replicated with hg is git add -p > which allows you to seperate out > different changes in the same file. Sounds like the record and crecord extensions come close, anyway: http://mercurial.seleni

Re: [Tutor] keeping track of the present line in a file

2010-01-21 Thread Kent Johnson
On Thu, Jan 21, 2010 at 1:57 AM, sudhir prasad wrote: > hi, > is there any other way to keep track of line number in a file other than > fileinput.filelineno() With enumerate(): for line_number, line in enumerate(open('myfile.txt')): # etc Kent ___

Re: [Tutor] Still searching in html files

2010-01-21 Thread Kent Johnson
On Thu, Jan 21, 2010 at 4:03 AM, Paul Melvin wrote: > The code is at http://python.codepad.org/S1ul2bh7 and the bit I would like > some advice on is how to get the sorted data and where to put it. Generally the code seems a bit disorganized, consider breaking it into functions. There is a fair a

Re: [Tutor] combinatorics problem: assembling array

2010-01-21 Thread Kent Johnson
On Thu, Jan 21, 2010 at 4:06 AM, markus kossner wrote: > Dear Pythonics, > I have a rather algorithmic problem that obviously made a knot in my brain: > > Assume we have to build up all the arrays that are possible if we have a > nested array > containing an array of integers that are allowed for

Re: [Tutor] combinatorics problem: assembling array

2010-01-21 Thread Kent Johnson
On Thu, Jan 21, 2010 at 8:07 AM, Kent Johnson wrote: > On Thu, Jan 21, 2010 at 4:06 AM, markus kossner wrote: >> Dear Pythonics, >> I have a rather algorithmic problem that obviously made a knot in my brain: >> >> Assume we have to build up all the arrays that are poss

Re: [Tutor] The magic parentheses

2010-01-24 Thread Kent Johnson
On Sun, Jan 24, 2010 at 12:08 PM, Alan Gauld wrote: > > "Hugo Arts" wrote > > print "this is {0}".format("formatted") >> >> this is formatted > > Caveat: > this style only works in Python 3.0 upwards  (or maybe in 2.6/2.7?) It's in 2.6 http://docs.python.org/library/stdtypes.html#str.for

Re: [Tutor] length of a string? Advice saught

2010-01-28 Thread Kent Johnson
On Wed, Jan 27, 2010 at 6:57 PM, Alan Gauld wrote: > read() functions usually have an optional buffersize parameter > set to a "reasonable" size. If you try to read more than that it > will be truncated. This is explained in the read() documentation > for files. ?? files have a buffer size that s

Re: [Tutor] multiply and sum two lists with list comprehension?

2010-01-28 Thread Kent Johnson
On Thu, Jan 28, 2010 at 1:22 AM, Muhammad Ali wrote: > > Hi, > > I am multipliying two lists so that each of list As elements get multiplied > to the corresponding list Bs. Then I am summing the product. > > For example, A= [1, 2, 3] and B=[2, 2, 2] so that I get [2, 4, 6] after > multiplication a

Re: [Tutor] hash value input

2010-01-29 Thread Kent Johnson
On Fri, Jan 29, 2010 at 8:03 AM, spir wrote: > I recently discovered that Lua uses the data's address (read: id) as input to > the hash func. This allows Lua tables (a kind of more versatile associative > array) to use _anything_ as key, since the id is guaranteed not to change, > per definiti

Re: [Tutor] hash value input

2010-01-29 Thread Kent Johnson
On Fri, Jan 29, 2010 at 1:20 PM, Rich Lovely wrote: > I've played with this a little.  The following class was quite handy for this: > > class BrokenHash(object): >    def __init__(self, hashval): >        self.hashval = hashval >    def __hash__(self): >        return self.hashval > > It basical

Re: [Tutor] hash value input

2010-01-30 Thread Kent Johnson
On Sat, Jan 30, 2010 at 4:56 AM, spir wrote: > I'm surprised of this, for this should create as many indexes (in the > underlying array actually holding the values) as there are integer keys. With > possibly huge holes in the array. Actually, there will certainly be a > predefined number of in

Re: [Tutor] parse text file

2010-02-01 Thread Kent Johnson
On Mon, Feb 1, 2010 at 6:29 AM, Norman Khine wrote: > thanks, what about the whitespace problem? \s* will match any amount of whitespace includin newlines. Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: htt

Re: [Tutor] parse text file

2010-02-02 Thread Kent Johnson
On Tue, Feb 2, 2010 at 4:16 AM, Norman Khine wrote: > here are the changes: > > import re > file=open('producers_google_map_code.txt', 'r') > data =  repr( file.read().decode('utf-8') ) Why do you use repr() here? > get_record = re.compile(r"""openInfoWindowHtml\(.*?\\ticon: myIcon\\n""") > get

Re: [Tutor] how to pass data to aother function from a class?

2010-02-02 Thread Kent Johnson
On Tue, Feb 2, 2010 at 8:04 AM, Zheng Jiekai wrote: > I'm beginning my python learning. My python version is 3.1 > > I‘v never learnt OOP before. > So I'm confused by the HTMLParser > > Here's the code: from html.parser import HTMLParser class parser(HTMLParser): > def handle_data(self,

Re: [Tutor] parse text file

2010-02-02 Thread Kent Johnson
On Tue, Feb 2, 2010 at 9:33 AM, Norman Khine wrote: > On Tue, Feb 2, 2010 at 1:27 PM, Kent Johnson wrote: >> On Tue, Feb 2, 2010 at 4:16 AM, Norman Khine wrote: >> >>> here are the changes: >>> >>> import re >>> file=open('producer

Re: [Tutor] parse text file

2010-02-02 Thread Kent Johnson
On Tue, Feb 2, 2010 at 1:39 PM, Norman Khine wrote: > On Tue, Feb 2, 2010 at 4:19 PM, Kent Johnson wrote: >> On Tue, Feb 2, 2010 at 9:33 AM, Norman Khine wrote: >>> On Tue, Feb 2, 2010 at 1:27 PM, Kent Johnson wrote: >>>> On Tue, Feb 2, 2010 at 4:16 AM, Norman

Re: [Tutor] parse text file

2010-02-02 Thread Kent Johnson
On Tue, Feb 2, 2010 at 4:56 PM, Norman Khine wrote: > On Tue, Feb 2, 2010 at 10:11 PM, Kent Johnson wrote: >> Try this version: >> >> data = file.read() >> >> get_records = re.compile(r"""openInfoWindowHtml\(.*?\ticon: >> myIcon

Re: [Tutor] help with strings

2010-02-03 Thread Kent Johnson
On Wed, Feb 3, 2010 at 8:19 AM, NISA BALAKRISHNAN wrote: > hi > > I am very new to python. > I have a string for example : 123B     new Project > i want to separate 123B as a single string and new  project as another > string . > how can i do that. > i tried using partition but couldnt do it str.

Re: [Tutor] language aid

2010-02-04 Thread Kent Johnson
On Thu, Feb 4, 2010 at 5:43 AM, Owain Clarke wrote: > My question is, that if I proceed like this I will end up with a single list > of potentially several hundred strings of the form "frword:engword". In > terms of performance, is this a reasonable way to do it, or will the program > increasingl

Re: [Tutor] correcting an Active State Recipe for conversion to ordinal

2010-02-04 Thread Kent Johnson
On Thu, Feb 4, 2010 at 12:11 PM, Serdar Tumgoren wrote: > I just noticed, however, that in the comments section of the > ActiveState recipe that someone is getting incorrect results for > certain numbers (11 and 12, specifically). > > But when I use the code on my own machine it still works fine.

Re: [Tutor] correcting an Active State Recipe for conversion to ordinal

2010-02-04 Thread Kent Johnson
On Thu, Feb 4, 2010 at 1:09 PM, Kent Johnson wrote: > On Thu, Feb 4, 2010 at 12:11 PM, Serdar Tumgoren wrote: >> Here's the link to the recipe: >> >> http://code.activestate.com/recipes/576888/ > > Perhaps the code on activestate is not a correct copy of what yo

Re: [Tutor] Help with cursors please

2010-02-04 Thread Kent Johnson
On Thu, Feb 4, 2010 at 12:47 PM, Alan Harris-Reid wrote: > Hi, > > I have a SQLite cursor which I want to traverse more than once, eg... > for row in MyCursor: >   method1(row) >  then later... > for row in MyCursor: >   method2(row) >  Method2 is never run, I guess because the pointer is at the b

Re: [Tutor] correcting an Active State Recipe for conversion to ordinal

2010-02-04 Thread Kent Johnson
On Thu, Feb 4, 2010 at 1:21 PM, Serdar Tumgoren wrote: >> Perhaps the code on activestate is not a correct copy of what you are >> running? The conditional at line 23 extends all the way to line 35 - >> the end of the function - so if value % 100/10 == 1 no more code is >> executed and None is ret

Re: [Tutor] Variable: From input and output

2010-02-04 Thread Kent Johnson
On Thu, Feb 4, 2010 at 1:19 PM, ssiverling wrote: > So I have been working on this example for a little while.  I looked for the > answer before posting.  I tried to use two raw inputs, then use > sys.stdout.write, to add them together.  However I think I might need to use > a variable.  Any help

Re: [Tutor] Variable: From input and output

2010-02-05 Thread Kent Johnson
On Thu, Feb 4, 2010 at 5:39 PM, ssiverling wrote: > > > I uploaded a file.  I know it's not very impressive. Where did you upload it? For short programs you can just include them in your email. Also, please don't top-post, and please subscribe to the list. Kent >

Re: [Tutor] if item.find(extension)!= -1: -- what's the -1?

2010-02-06 Thread Kent Johnson
On Sat, Feb 6, 2010 at 11:35 AM, David wrote: > Hello again, > > in Knowlton's 2008 book "Python: Create, Modify, Reuse" the author makes > frequent use of the term -1 in his code, but doesn't tell to what aim. For > want of search terms Google is not helpful. Could someone please enlighten > me b

Re: [Tutor] rstrip in list?

2010-02-09 Thread Kent Johnson
On Tue, Feb 9, 2010 at 10:28 AM, Ken G. wrote: > I printed out some random numbers to a list and use 'print mylist' and > they came out like this: > > ['102\n', '231\n', '463\n', '487\n', '555\n', '961\n'] How are you generating this list? You should be able to create it without the \n. That woul

Re: [Tutor] rstrip in list?

2010-02-09 Thread Kent Johnson
On Tue, Feb 9, 2010 at 11:09 AM, Ken G. wrote: > > Kent Johnson wrote: > > On Tue, Feb 9, 2010 at 10:28 AM, Ken G. wrote: > > > I printed out some random numbers to a datafile and use 'print mylist' and > they came out like this: > > ['102\n

Re: [Tutor] NameError: global name 'celsius' is not defined (actually, solved)

2010-02-10 Thread Kent Johnson
On Tue, Feb 9, 2010 at 10:00 PM, David wrote: > Hi guys, > > I just wrote this message, but after restarting ipython all worked fine. > How is it to be explained that I first had a namespace error which, after a > restart (and not merely a new "run Sande_celsius-main.py"), went away? I > mean, sur

Re: [Tutor] "Error :Attempt to overwrite cell" while using xlwt to create excel sheets

2010-02-10 Thread Kent Johnson
On Wed, Feb 10, 2010 at 6:47 AM, nikunj badjatya wrote: > I commented out the "raise Exception" statement in Row.py library > module. > Here's the (line no. 150 ) of Row.py which i have edited: > >   def insert_cell(self, col_index, cell_obj): >         if col_index in self.__cells: >            

Re: [Tutor] string to list

2010-02-10 Thread Kent Johnson
On Wed, Feb 10, 2010 at 6:26 AM, Owain Clarke wrote: > Please excuse the obviousness of my question (if it is), but I have searched > the documentation for how to generate a list e.g. [(1,2), (3,4)] from a > string "[(1,2), (3,4)]". I wonder if someone could point me in the right > direction. Pyt

Re: [Tutor] string to list

2010-02-10 Thread Kent Johnson
On Wed, Feb 10, 2010 at 8:32 AM, Owain Clarke wrote: > My son was doing a statistics project in which he had to sort some data by > either one of two sets of numbers, representing armspan and height of a > group of children - a boring and painstaking job.  I came across this piece > of code:- > >

  1   2   3   4   5   6   7   8   9   10   >