Re: Strings and Lists
Hi, I not sure what sorts of operations you plan to do. But if you intend to use fixed length arrays or even carrying out repetetive operations. You should probably look at numeric http://numeric.scipy.org/ On 18 Apr 2005 04:42:17 -0700, Tom Longridge <[EMAIL PROTECTED]> wrote: > My current Python project involves lots repeatating code blocks, > mainly centred around a binary string of data. It's a genetic > algorithm in which there are lots of strings (the chromosomes) which > get mixed, mutated and compared a lot. > > Given Python's great list processing abilities and the relative > inefficiencies some string operations, I was considering using a list > of True and False values rather than a binary string. > > I somehow doubt there would be a clear-cut answer to this, but from > this description, does anyone have any reason to think that one way > would be much more efficient than the other? (I realise the best way > would be to do both and `timeit` to see which is faster, but it's a > sizeable program and if anybody considers it a no-brainer I'd much > rather know now!) > > Any advice would be gladly recieved. > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://blogs.applibase.net/sidharth -- http://mail.python.org/mailman/listinfo/python-list
Re: How to check whether a list have specific value exist or not?
Hi Prabha, if 3 in [1, 2, 3, 4]: print "yes" Python is an amazing language if you understand that it is actually quite a bit different from php. The python tutorial is pretty good, I suggest you go through it. On Apr 9, 2005 3:07 PM, Michael Spencer <[EMAIL PROTECTED]> wrote: > praba kar wrote: > > Dear All > > In Php we can find in_array() function > > which function is mainly useful to check > > whether a specific value is exist in the array > > or not. > > > >But In python In cannot find any function > > like that. I want to check a list have specific > > value or not. So If any one know regarding this > > mail me > > > > with regards > > PRabahar > > > > > > > > Yahoo! India Matrimony: Find your life partner online > > Go to: http://yahoo.shaadi.com/india-matrimony > > No need to ask this list, ask the built-in one: > > >>> dir(list) > ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', > '__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__', > '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', > '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', > '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', > '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__str__', 'append', > 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] > > A minute of two experimenting, would then lead you to: > > >>> l = [1,2,3,4,5] > >>> l.index(3) > 2 > >>> > > Note that all indexing in Python is 0-based > > Michael > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://blogs.applibase.net/sidharth -- http://mail.python.org/mailman/listinfo/python-list
Re: check instace already running...
I haven't tested this. There is probably a better way of doing this looking at process information. I use a lock file to mark that the program is already running. The problem is that for an abrupt shutdown the file might not be removed. import atexit if os.path.exists(lockfile): print "there is an instance already running" else: file(lockfile, "w").close() atexit.register(lambda:os.remove(lockfile)) //Your code here On Apr 9, 2005 2:32 PM, Sidharth Kuruvila <[EMAIL PROTECTED]> wrote: > I haven't tested this. There is probably a better way of doing this > looking at process information. I use a lock file to mark that the > program is already running. The problem is that for an abrupt shutdown > the file might not be removed. > > import atexit > if os.path.exists(lockfile): > print "there is an instance already running" > else: > file(lockfile, "w").close() > atexit.register(lambda:os.remove(lockfile)) > > //Your code here > > On Apr 9, 2005 2:01 PM, Fabio Pliger <[EMAIL PROTECTED]> wrote: > > Hi, > > is it possibile, in python, to check for an already running instance of an > > application? > > My problem is that, if my program i running and the user relaunch it, i > > don't want to open a new instance and have to instances of the same program > > running togheter... > > Can someone help me on this? > > Fabio P. > > > > -- > > http://mail.python.org/mailman/listinfo/python-list > > > > -- > http://blogs.applibase.net/sidharth > -- http://blogs.applibase.net/sidharth -- http://mail.python.org/mailman/listinfo/python-list
Re: Declaring variables from a list
What I gave was a bad solution. Something that works right now, but probably shouldn't be done. On Apr 9, 2005 3:37 AM, Inyeol Lee <[EMAIL PROTECTED]> wrote: > On Sat, Apr 09, 2005 at 03:15:01AM +0530, Sidharth Kuruvila wrote: > > Python has a builtin function called locals which returns the local > > context as a dictionary > > > > >>> locals = locals() > > >>> locals["a"] = 5 > > >>> a > > 5 > > >>> locals["a"] = "changed" > > >>> a > > 'changed' > > >From Python lib reference: > > """ > locals() > ... > Warning: The contents of this dictionary should not be > modified; changes may not affect the values of local variables used > by the interpreter. > """ > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://blogs.applibase.net/sidharth -- http://mail.python.org/mailman/listinfo/python-list
Re: Declaring variables from a list
Python has a builtin function called locals which returns the local context as a dictionary >>> locals = locals() >>> locals["a"] = 5 >>> a 5 >>> locals["a"] = "changed" >>> a 'changed' On 8 Apr 2005 13:55:39 -0700, Cactus <[EMAIL PROTECTED]> wrote: > Hi, > > If I got a list is it possible to declare a variable from the items in that > list? > > Code Sample: > Blob = ['Var1', 'Var2', 'vAR3'] > i = 5 > for listitems in Blob: > i += 1 > listitems = i > > print Var1 > 6 > print Var2 > 7 > print vAR3 > 8 > > Something like that? This doesn't work (obviously) but is there a way to do > this? > > TIA, > Cacti > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://blogs.applibase.net/sidharth -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception Handling
Have a look at the chapter on exceptions in the python tutorial its pretty good. http://docs.python.org/tut/node10.html On 8 Apr 2005 14:29:40 -0700, SuperJared <[EMAIL PROTECTED]> wrote: > I'm new to Python, well versed in PHP and a bit of Perl. > > I've written a simple backup utility that FTPs from one server to > another. I'd like to implement exception handling on the FTP should > someting go wrong. > > This is basically what I have so far: > > try: >ftp = FTP(ftp_host, ftp_user, ftp_pass) >ftp.storbinary('STOR ' + filename, pointer) >ftp.close() > except: ># do something with an error message here...? > > I'd like to use 'all_errors' but I don't know how! Will I have to > create a handler for each other exception? > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://blogs.applibase.net/sidharth -- http://mail.python.org/mailman/listinfo/python-list
Re: compound strip() string problem
The time module has a function called 'strftime' which can retyrn the time in the the format you want to. So you really don't need to parse the string returned by asctime the way you are doing. On Apr 8, 2005 6:01 PM, Dylan Wilson <[EMAIL PROTECTED]> wrote: > Hi, > I'm new to python and i have a string problem. > My problem is this > -- > >>>import time > >>>time = time.asctime() > >>>time > 'Fri Apr 08 22:14:14 2005' > >>>ti = time[0:13] > >>>me = time[14:16] > >>>time = ti + me > >>>time > 'Fri Apr 08 2214' > -- > Now i need to compond that string remove the whitespace if you will.Well > i read up on strip(), lstrip() and rstrip() and all i could deduce was > that they striped the whitespace from the start and/or end of the > string.But I tried that anyway and failed.Is there an easier way than i > did it below? I'm sorry it's ugly and tedious. > -- > #!/bin/bash/env python > > import os, time > > #Figure out what os this is > platform = os.name > > #Create string with date, time and extension for our pcap file > ext = '.out' > time = time.asctime() > ti = time[0:13] # > me = time[14:16] # > time = ti + me #There has to be a better way to do this? > fo = time[0:3] # > rm = time[4:7]# > at = time[11:18] > time = fo + rm + at + ext > > #Get rid of yukkies > del ti, me, ext, fo, rm, at > > #create command string > flag = '-w' > wincommand = 'c:/progra~1/ethereal/tethereal' > lincommand = '/usr/sbin/./tethereal' > > #run tethereal and send the output to a pcap file DDDMMMHHMM.out > > if platform == 'nt': > os.system('%s %s %s' % (wincommand, flag, time)) > > if platform == 'posix': > os.system('%s %s %s' % (lincommand, flag, time)) > -- > Thanks, > Dylan > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://blogs.applibase.net/sidharth -- http://mail.python.org/mailman/listinfo/python-list
Re: how can I extract all urls in a string by using re.findall() ?
Reading the documentation on re might be helpfull here :-P findall returns a tuple of all the groups in each match. You might find finditer usefull. for m in re.finditer(url, html) : print m.group() or you could replace all your paranthesis with the non-grouping version. That is, all brackets (...) with (?:...) On Apr 7, 2005 7:35 AM, could ildg <[EMAIL PROTECTED]> wrote: > I want to retrieve all urls in a string. When I use re.fiandall, I get > a list of tuples. > My code is like below: > > [code] > url=unicode(r"((http|ftp)://)?[\d]+\.)+){3}[\d]+(/[\w./]+)?)|([a-z]\w*((\.\w+)+){2,})([/][\w.~]*)*)") > m=re.findall(url,html) > for i in m: >print i > [/code] > > html is a variable of string type which contains many urls in it. > the code will print many tuples, and each tuple seems not to represent > a url. e.g, one of them is as below: > > (u'http://', u'http', u'image.zhongsou.com/image/netchina.gif', u'', > u'', u'', u'', u'image.zhongsou.com', u'.com', u'.com', > u'/netchina.gif') > > Why is there two "http" in it? and why are there so many ampty strings > in the tupe above? It's obviously not a url. How can I get the urls > correctly? > > Thanks in advance. > -- > 鹦鹉聪明绝顶、搞笑之极,是人类的好朋友。 > 直到有一天,我才发觉,我是鹦鹉。 > 我是翻墙的鹦鹉。 > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://blogs.applibase.net/sidharth -- http://mail.python.org/mailman/listinfo/python-list