Re: [Tutor] Filesystem vs Database vs Lucene

2007-03-28 Thread Shitiz Bansal
Thanks Alan for your reply.
I however have a few more concerns.

Memory is not an option not just because of capacity but also because I desire 
persistence.
Also I feel that 500 MB of data in a dictionary would typically cross 1 Gigin 
total used memory.That of course will depend on the Hashing algorithm used and 
the actual data.Anyway this program is just a module of something quite larger 
so...

Shelves seem nice but I would like to be sure on how it is implemented. Would 
it 
seek to a particular point in file and load just the desired data in memory or 
does it unpickle everything and create a fullblown dictionary in memory?...in 
which case the memory problem will resurface.

If this is addressed I would definitely opt for shelve over db/dbm as it would 
be simpler. Also the underlying algorithm being a hash search instead of a 
b-tree search/insert as in most database indexes, this is bound to outperform 
them.

It might be blasphemous to say this here but is there an equivalent c library 
as I am willing to spend an extra amount of time coding for that extra zing in 
performance.

Alan Gauld <[EMAIL PROTECTED]> wrote: "Shitiz Bansal"  wrote

> I need to implement a system which stores Strings(average length 50 
> chars).
> For every input String it would need to tell the user wether that 
> string
> already exists in the system. It would also need to add that input
> String to the system if it did not exist.

Sounds like a job for a dictionary, except...

> It will also be useful to know the last accessed datetime value of 
> that string.

That can be done with a bit of effort.

> The number of strings is in millions and i also need persistence
> so keeping all Strings in memory is not an option.

10 million x 50 chars = 500MB. So if you have a Gig of RAM
and not much else running on the machine memory might still
be a valid option... but if not...

This rules out a normal dictionary, but what about a shelf?
Have a look at the shelve module, it makes a file look a lot like
a dictionary. It should solve your problem. And you can store
either a string or a string/date tuple.

I'm not sure how a shelf would perform compared to a database,
but its a lot simpler to manage.

> Would it be wiser to keep these Strings in an indexed column
> of the DB or would it be better to keep these strings as filenames
> on the filesystem in a folder hiearchy of some sort.

I'd definitely go for the database approach if not using shelve.

> Please also bear in mind the time required to insert the
> strings (for eg. i tried using a database but found the insertion
> time to be very high once i indexed the particular column.

That's common, so I'd suggest not indexing. Its the rebuild of the
index that takes the time. Or if you can break the strings into
categories to reduce the size of the tables that would help.
But that depends on how easy it is to categorise the strings
such that you know where to insert/search.

Also consider using the dbm family of moidules, for simple
data access they often out perform a full SQL database.

HTH,
-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


 
-
Don't be flakey. Get Yahoo! Mail for Mobile and 
always stay connected to friends.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Filesystem vs Database vs Lucene

2007-03-26 Thread Shitiz Bansal
Hi,
I need to implement a system which stores Strings(average length 50 chars).
For every input String it would need to tell the user wether that string 
already exists in the system. It would also need to add that input String to 
the system if it did not exist. It will also be useful to know the last 
accessed datetime value of that string.
The number of strings is in millions and i also need persistence so keeping all 
Strings in memory is not an option.
I was wondering what would be the most efficient(timewise) solution for this 
riddle.
Would it be wiser to keep these Strings in an indexed column of the DB or would 
it be better to keep these strings as filenames on the filesystem in a folder 
hiearchy of some sort. I have no experience of indexers like lucene but i can 
also consider those if they are a better alternative than the other two.
Please also bear in mind the time required to insert the strings(for eg. i 
tried using a database but found the insertion time to be very high once i 
indexed the particular column.
Thanks,
Shitiz

 
-
 Get your own web address.
 Have a HUGE year through Yahoo! Small Business.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Beautiful Soup

2006-12-06 Thread Shitiz Bansal
Hi,
I am using beautiful soup to get links from an html document.
I found that beautiful Soup changes the & in the links to & due to which 
some of the links become unusable.
Is there any way I could stop this behaviour?

Regards,
Shitiz

 
-
Access over 1 million songs - Yahoo! Music Unlimited.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beautiful Soup

2006-11-29 Thread Shitiz Bansal
Thanks, urlparse.urljoin did the trick.
Akash- the problem with directly prefixing url to the link is that the url most 
of the times contains not just the page address but also parameters and 
fragments.

Andreas Kostyrka <[EMAIL PROTECTED]> wrote: * Akash  [061129 20:54]:
> On 11/30/06, Shitiz Bansal  wrote:
> > I am using beautiful soup for extracting links from a web page.
> > Most pages use relative links in their pages which is causing a problem. Is
> > there any library to extract complete links or do i have to parse this
> > myself?
> >
> 
> Beautiful Soup can also extract text which is present on the page. If
> there are no complete links no library can do that for you. But since
> you are reaching a certain web page to extract you already have that
> URL information with you. All you have to do then is to prefix it to
> each extracted URL.
Take a look at urlparse.urljoin from the standard library.

Andreas


 
-
Access over 1 million songs - Yahoo! Music Unlimited.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Beautiful Soup

2006-11-29 Thread Shitiz Bansal
Hi,

I am using beautiful soup for extracting links from a web page. 
Most pages use relative links in their pages which is causing a problem. Is 
there any library to extract complete links or do i have to parse this myself?

Thanks,
Shitiz

Terry Carroll <[EMAIL PROTECTED]> wrote: On Wed, 29 Nov 2006, Alan Gauld wrote:

> But for the Python programmer I'd consider Borland Delphi, based
> on Pascal. Pascal is much more readable and Pythonic than C
> and allows the same level of access to the underlying hardware
> (and assembler when needed).

I'm going to apologize for taking this even further off-topic, but, if 
the OP is looking at Delphi, he should know that Borland provides a
version of Delphi, called Turbo Delphi Explorer, that you can download and 
use for free.  We also offer a priced "Professional" version, but for the 
OP's interests, the Explorer edition will probably work just fine.

 http://www.turboexplorer.com/
 http://www.borland.com/downloads/download_turbo.html

Full disclosure: I say "we" above, because I *am* affiliated with Borland: 
I'm Borland's in-house attorney for intellectual property and product 
matters.  (What's an attorney doing on a Python list?  I was a developer 
before I was an attorney, and still play around a little bit here and 
there, and I usually use Python when I do.)

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


 
-
Cheap Talk? Check out Yahoo! Messenger's low PC-to-Phone call rates.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Convert string to file handle

2006-11-27 Thread Shitiz Bansal
Hi,
i have a string abc.
i need a file handle f  pointing to a file which contains this string.
is there any way i can achieve this without actually writing the contents to a 
file and then opening it? for performance reasons i want to run this entire 
process within the physical memory!

Thanks,
Shitiz

 __
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] testing for modules?

2005-10-25 Thread Shitiz Bansal
try:
   import 
except:
   
   import 


--- Ed Hotchkiss <[EMAIL PROTECTED]> wrote:

> i have a generic script that is using several
> modules on windows and linux
> boxes. i need to have the scripts test if a module
> is installed, and then if
> not - then to install the module. can anyone give me
> a headsup on how to
> test for a module, returning something to indicate
> whether or not it is
> installed, then after that executing file to install
> the module.? just
> somewhere to start would be helpful! thanks in
> advance.
> 
> --
> edward hotchkiss
> > -- 
> http://mail.python.org/mailman/listinfo/python-list


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] File mode r+

2005-09-30 Thread Shitiz Bansal
Hi,
Thats strange. Maybe me being on windows makes the
diference.
Here is the exact IDLE session to clarify:


IDLE 1.1   No Subprocess 
>>> 
>>> file1=open("aa.txt",'r+')
>>> file1.read()
'abcd\nefgh\nijkl\nmnop\nqrst\nuvwx\nyx12\n'
>>> file1.close()
>>> file1=open("aa.txt",'r+')
>>> file1.readline()
'abcd\n'
>>> file1.seek(0,1)
>>> file1.write("1234\b")
>>> file1.close()
>>> file1=open("aa.txt",'r+')
>>> file1.read()
'abcd\n1234\x08\nijkl\nmnop\nqrst\nuvwx\nyx12\n'
>>> 

I find this very very strange.

shitiz.

--- "Michael P. Reilly" <[EMAIL PROTECTED]> wrote:

> On 9/27/05, Shitiz Bansal <[EMAIL PROTECTED]> wrote:
> >
> > Hi,
> > thanx..it works fine. As for the structure of the
> > file...i decided to pad every line with extra
> spaces
> > to make the length of each line constant.That way
> i
> > can replace any line with another without being
> > bothered about the number of characters.
> > However my curiosity got better of me again...:D.
> > i tried to insert a /b character(i had a notion it
> > represents backspace) to see if it would work.
> > It inserted a bit of unicode in the file(Mind you,
> > inserted . Not replaced)
> > original file:
> >
> > 'abcd\nefgh\nijkl\nmnop\nqrst'
> >
> > code::
> > file1=open("aa.txt",'r+')
> > file1.readline()
> > file1.seek(0,1)
> > file1.write("1234\b")
> > file1.close()
> >
> > newfile:
> >
> > 'abcd\n1234\x08\nijkl\nmnop\nqrst'
> >
> > How does that come to pass?
> >
> >
> I didn't get what you got exactly. I got:
> 
> 'abcd\0121234\010ijkl\012mnop\012qrst'
> 
> \012 => \n and
> \010 => \b (\x08)
> 
> I'm not sure why you would have gotten '\x08\n' in
> the same sequence. Maybe
> you should look again. Incidently, when I try to
> print aa.txt, I get:
> abcd
> 123ijkl
> mnop
> qrst
> which is what I would expect.
> -Arcege
> --
> There's so many different worlds,
> So many different suns.
> And we have just one world,
> But we live in different ones.
> 




__ 
Yahoo! Mail - PC Magazine Editors' Choice 2005 
http://mail.yahoo.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] File mode r+

2005-09-27 Thread Shitiz Bansal


--- "Michael P. Reilly" <[EMAIL PROTECTED]> wrote:

> On 9/24/05, Shitiz Bansal <[EMAIL PROTECTED]> wrote:
> >
> > Hi,
> >
> > How do i proceed if i am not sure about the number
> of characters in the
> > fist two lines and want to write in the third
> line.is <http://line.is>there a way to skip two
> lines and write on the third??
> >
> > Also could anyone explain why the readline() did
> not work. according to
> > what i understand it should.
> > *bob <[EMAIL PROTECTED]>* wrote:
> >
> > At 01:11 AM 9/24/2005, Shitiz Bansal wrote:
> > >Hi,
> > >I want to update a textfile using the r+ file
> mode.
> > >contents of file:
> > >
> > >abcd
> > >efgh
> > >ijkl
> > >mnop
> > >qrst
> > >uvwx
> > >yx12
> > >
> > >my scripts is:
> > >
> > >file1=open("aa.txt",'r+')
> >
> > >>Instead of readline, use skip to position the
> file to where you want to
> > >>overwrite it.
> > >>file1.seek(10)
> >
> >
> > >file1.readline()
> > >file1.readline()
> > >file1.write("1234\n")
> > >file1.close()
> > >
> > >This should replace the third line with 1234.
> > >However it does nothing.
> > >
> > >Moreover the script:
> > >
> > >file1=open("aa.txt",'r+')
> > >file1.write("1234\n")
> > >file1.close()
> > >
> > >does replace the first line with 1234.
> > >
> > >could anyone explain what is happening?
> > >
> > >shitiz
> >
> > Hello Shitiz,
> 
> I'm not sure if it is true anymore, I haven't gotten
> to test it. But in the
> olden days of C and standard libraries and UNIX, you
> would open a file for
> reading and writing and before you could switch
> between read() and write()
> you would have to perform a seek(). Usually
> something like:
> file.readline() # read line 1
> file.readline() # read line 2
> file.seek(0, 1) # move 0 bytes from current position
> (no place)
> file.write("1234\n")
> file.close()
>  There may be one problem here. What you write will
> overwrite line 3 or
> only part of line 3... or maybe all of line 3 and
> some of line 4. It is good
> to know how the file is structured before you write
> to it, in case you end
> up with unexpected data. If all your lines are four
> characters plus a
> newline, then you don't have to worry (most of us
> aren't that lucky).
> -Arcege
> --
> There's so many different worlds,
> So many different suns.
> And we have just one world,
> But we live in different ones.
> 




__ 
Yahoo! Mail - PC Magazine Editors' Choice 2005 
http://mail.yahoo.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] File mode r+

2005-09-24 Thread Shitiz Bansal

Hi,
How do i proceed if i am not sure about the number of characters in the fist two lines and want to write in the third line.is there a way to skip two lines and write on the third??
Also could anyone explain why the readline() did not work. according to what i understand it should.
 
shitizbob <[EMAIL PROTECTED]> wrote:

At 01:11 AM 9/24/2005, Shitiz Bansal wrote:>Hi,>I want to update a textfile using the r+ file mode.>contents of file:>>abcd>efgh>ijkl>mnop>qrst>uvwx>yx12>>my scripts is:>>file1=open("aa.txt",'r+')>>Instead of readline, use skip to position the file to where you want to >>overwrite it.>>file1.seek(10)
>file1.readline()>file1.readline()>file1.write("1234\n")>file1.close()>>This should replace the third line with 1234.>However it does nothing.>>Moreover the script:>>file1=open("aa.txt",'r+')>file1.write("1234\n")>file1.close()>>does replace the first line with 1234.>>could anyone explain what is happening?>>shitiz>>__>Do You Yahoo!?>Tired of spam? Yahoo! Mail has the best spam protection around>http://mail.yahoo.com>___>Tutor maillist - Tutor@python.org>http://mail.python.org/mailman/listinfo/tutor
		Yahoo! for Good 
Click here to donate to the Hurricane Katrina relief effort. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] File mode r+

2005-09-24 Thread Shitiz Bansal
Hi,
I want to update a textfile using the r+ file mode.
contents of file:

abcd
efgh
ijkl
mnop
qrst
uvwx
yx12

my scripts is:

file1=open("aa.txt",'r+')
file1.readline()
file1.readline()
file1.write("1234\n")
file1.close()

This should replace the third line with 1234.
However it does nothing.

Moreover the script:

file1=open("aa.txt",'r+')
file1.write("1234\n")
file1.close()

does replace the first line with 1234.

could anyone explain what is happening?

shitiz

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] smtp error

2005-09-06 Thread Shitiz Bansal
does this error make sense to anyone?

Traceback (most recent call last):
  File "C:/Documents and
Settings/Administrator/Desktop/lkp.py", line 46, in
-toplevel-
s.sendmail(msg['From'], i, msg.as_string())
  File "C:\Python24\lib\smtplib.py", line 692, in
sendmail
raise SMTPDataError(code, resp)
SMTPDataError: (550, 'Invalid recipient:
[EMAIL PROTECTED]')



heres the complete code:
# Import smtplib for the actual sending function
import smtplib

# Here are the email pacakge modules we'll need
import base64
import cStringIO
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
COMMASPACE = ', '
import email
# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject']=raw_input("Subject:") 
# me == the sender's email address
# family = the list of all recipients' email addresses
msg['From'] =raw_input("From:")
kk = raw_input("To filename:")
# Guarantees the message ends in a newline
msg.epilogue = ''
file1=raw_input("text measage file:")
fp = open(file1, 'rb')
img = MIMEText(fp.read())
fp.close()
msg.attach(img)
file=raw_input("File to attach:")
fp = open(file, 'rb')
contentType="application/octet-stream"
contentsEncoded=cStringIO.StringIO()
mainType=contentType[:contentType.find("/")]
cte="base64"
base64.encode(fp,contentsEncoded)
subMsg=email.Message.Message()
subMsg.add_header("Content-transfer-encoding",cte)
subMsg.set_payload(contentsEncoded.getvalue())
contentsEncoded.close()

fp.close()
msg.attach(subMsg)
fp=open(kk,'r')
# Send the email via our own SMTP server.
mailadds=fp.readlines()
for i in mailadds:
msg['To']=i
s = smtplib.SMTP()
s.connect()
s.sendmail(msg['From'], i, msg.as_string())
s.close()






__
Click here to donate to the Hurricane Katrina relief effort.
http://store.yahoo.com/redcross-donate3/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Remove a number from a string

2005-08-23 Thread Shitiz Bansal
Hi,
Suppose i have a string '347 liverpool street'.
I want to remove all the numbers coming at the starting of the string.
I can think of a few ways but whats the cleanest way to do it?
 
Shitiz__Do You Yahoo!?Tired of spam?  Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Web Browser in Python

2005-08-03 Thread Shitiz Bansal

Hi,
I actually want to create a web-browser on my own. There are so many changes i would like to see in my browser that i felt that i would be better of creating my own rather than trying to modify the existing ones.As for the size of project, I have been pursuing programming as a hobby for years and really enjoy it.I dont have any specific timeframe or deadline in mind so the size doesnt matter :). 
Cheers,
Shitiz Kent Johnson <[EMAIL PROTECTED]> wrote:
R. Alan Monroe wrote:>>>I need to program a web browser in python.I dont have any idea of how to>>>start, what i do have is time and willingness to learn.Could anyone direct>>>me to some suitable reference? >>>Shitiz > >>How about Grail http://grail.sourceforge.net/ ?> > > Looks neat, but it doesn't run on my box:Grail is pretty much dead, I think. If you look at the checkin mailing list the last activity was in 2001. If you really want to write a web browser it might be a good starting point though.wxPython has an HTML widget that could also be a starting point.I hope you realize that creating a browser on the scale of Firefox or IE is a massive and difficult project. Is that your goal or did you have something more modest in
 mind?Kent___Tutor maillist - Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor
		 Start your day with Yahoo! - make it your home page ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Web Browser in Python

2005-08-03 Thread Shitiz Bansal
Hi,
I need to program a web browser in python.I dont have any idea of how to start, what i do have is time and willingness to learn.Could anyone direct me to some suitable reference?
Shitiz__Do You Yahoo!?Tired of spam?  Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] print command

2005-03-20 Thread Shitiz Bansal
Now this is so basic, i am feeling sheepish asking
about it.
I am outputting to the terminal, how do i use a print
command without making it jump no newline after
execution, which is the default behaviour in python.
To clarify:

print 1
print 2
print 3

I want output to be 

123

whereas by default it is
1
2
3

.

Shitiz 

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to delete a class instance

2005-03-16 Thread Shitiz Bansal

--- Danny Yoo <[EMAIL PROTECTED]> wrote:

> Ok, this makes sense.  Each passenger thread needs
> to know about the
> queue, because that's the place you want the
> passenger to drop out of.
> 
> Lists support a 'remove()' method, so you may be
> able to use it.

Does it operate like queue.remove(self) ?
Because the instance has no way of knowing its
position in the queue.



__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to delete a class instance

2005-03-16 Thread Shitiz Bansal


> In the code you have shown, I don't see any need for
> the queue. Just create the thread and start it. 
> I don't think you have to keep a reference to it.
> I'm not sure, but I think the thread will be 
> garbage collected when it completes. (Can anyone
> else confirm this?)

This does make sense,but how can we confirm that the
instance is not garbage collected before it ends?is
this the default behyaviour?

Shitiz 


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to delete a class instance

2005-03-16 Thread Shitiz Bansal
No, this list is not a linked list.
Since mu original code is rather huge, I am presenting
the relevant snippet.
queue=[]
def genpassenger(num,destination,queue=queue):
for i in range(num):
newpass=passenger(destination)
queue.append(newpass)
newpass.start()
class passenger(threading.Thread):
def __init__

def run:
#do something
delete itself

genpassengers is called interactively by the user
.Also there can be multiple instances of genpassengers
running at the same time.
Now my questions are:
1) Generically , how do i accomplish the delete itself
part
2) If i used a linked list, does automatic memory
handling by python entail that delete itself is
automatically accomplished once i have readjusted the
pointers, since no further reference to the instance
remains.(Of course, if it doesnt i can get a reference
in the external code(in case of a doubly linked
list)).
3)  How about making a destructor function (on lines
of c++) and deleting all the variables of the
class.Does it free all my memory or is there still
some memory being used, though the instance has no
variables stored.

Shitiz


--- Kent Johnson <[EMAIL PROTECTED]> wrote:
> Shitiz Bansal wrote:
> > Hi,
> > How do i delete a class instance in a function
> running
> > within itself?
> > All the instances of the class are stored in a
> list,
> > and they need to be deleted after executing their
> > function.
> > However since the list is dynamic, there is no way
> to
> > know the exact position of the instance within the
> > list.
> 
> You have to do this from the caller, in code that
> has access to the list. There is no general way 
> for an object to find out how it is being referenced
> in external code. Can you show some of the code 
> that creates and uses the list? Does the code that
> calls the class know where the class is in the list?
> 
> Is this the linked list you asked about before that
> is modified from two threads?
> 
> > 
> > I tried del self to no avail.
> 
> del self just removes the link between the name
> 'self' and the object it references.
> 
> Kent
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 



__ 
Do you Yahoo!? 
Yahoo! Small Business - Try our new resources site!
http://smallbusiness.yahoo.com/resources/ 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How to delete a class instance

2005-03-16 Thread Shitiz Bansal
Hi,
How do i delete a class instance in a function running
within itself?
All the instances of the class are stored in a list,
and they need to be deleted after executing their
function.
However since the list is dynamic, there is no way to
know the exact position of the instance within the
list.

I tried del self to no avail.

Shitiz



__ 
Do you Yahoo!? 
Yahoo! Small Business - Try our new resources site!
http://smallbusiness.yahoo.com/resources/ 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


RE: [Tutor] Acessing files in Windows 2000

2005-03-08 Thread Shitiz Bansal
I faced the same problem once.Dont remember the
solution, but it is definitely the slashes which are
causing the problem.I couldnt find a specific rule in
whole of microsoft documentation, so i assume it has
to be hit and try.Try a mix of backslashes n forward
slashes till u get there.
Whats worse, I had found that the rule is different
for different versions of windows.Just proves what we
all know...Windows Suxx.
 
--- John Purser <[EMAIL PROTECTED]> wrote:
> I agree with a previous poster, check your path.  I
> think either the path
> doesn't exist or you don't have permission to get to
> it.
> 
> John 
> 
> -Original Message-
> From: Dave S [mailto:[EMAIL PROTECTED] 
> Sent: Tuesday, March 08, 2005 14:50
> To: [EMAIL PROTECTED]
> Cc: 'Python Tutor'
> Subject: Re: [Tutor] Acessing files in Windows 2000
> 
> John Purser wrote:
> 
> >Try c:\\my documents\\memo.txt
> >
> >
> >John 
> >  
> >
> Unfortunately thats a no go ...
> 
> palm.py
> 
> palm memo.txt to oocalc data.csv convertor, written
> by lentil ;)
> [EMAIL PROTECTED] - enter "palm software" in the title
> or it will be junked
> 
> 09-03-2005  v1.02w Coded for Windows
> 07-03-2005  v1.02  Coded capitalization() + default
> to mon
> 03-03-2005  v1.01  Squished 1/9 decode bug
> 02-03-2005  v1.00  Coded for Linux
> 
> Traceback (most recent call last):
>   File "C:\Documents and
> Settings\Administrator\Desktop\palm.py", line 
> 268, in ?
> palm_conv()
>   File "C:\Documents and
> Settings\Administrator\Desktop\palm.py", line 
> 54, in palm_conv
> memo = open(memo_file, 'r')
> IOError: [Errno 2] No such file or directory:
> 'c:\\my documents\\memo.txt'
>  >>>
> 
> If I try c:\\
> 
> or
> 
> c:\
> 
> it always comes out as c:\\... and fails
> 
> Dave
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 




__ 
Celebrate Yahoo!'s 10th Birthday! 
Yahoo! Netrospective: 100 Moments of the Web 
http://birthday.yahoo.com/netrospective/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Linked List

2005-03-05 Thread Shitiz Bansal
Got ya, it doesnt however help in my case as inside
the for loop also i intend to change the list
elements. 
--- Kent Johnson <[EMAIL PROTECTED]> wrote:

> Shitiz Bansal wrote:
> > I could not understand what you exactly mean.
> > 
> > In order to explain wat my problem is, here is an
> > example code. Its not exactly what I am doing, I
> am
> > using multiple threads and a rather complicated
> code
> > so  try and understand the sense rather than the
> code
> > itself.
> > 
> > 
> >>>>myls=range(50)
> >>>>for i in myls:
> > 
> > print i
> > if i==20:
> > myls.insert(5,5)
> > 
> > The point is, the list(both size and elements) is
> > changing even as it is being operated upon.
> 
> What Jacob is saying is, one common way to deal with
> this is to make a copy of the list and iterate 
> over that while changing the original list. Your
> sample would look like this:
> 
> myls=range(50)
> for i in myls[:]:  # <-- Note: makes a copy of myls
>   print i
>   if i==20:
>   myls.insert(5,5)
> 
> Kent
> 
> > 
> > This particular case goes into infinite loop at
> i=20.
> > 
> > Interestingly, I can freely edit the list yet to
> be
> > traversed, without any ill effects, which would
> not
> > have been possible if a python list was a pure
> > array(due to immutable length of an array in c).
> > 
> > Shitiz
> > 
> > --- "Jacob S." <[EMAIL PROTECTED]> wrote:
> > 
> > 
> >>I'm taking a shot in the dark and answering here.
> >>
> >>Have you tried making a copy of the list,
> iterating
> >>over the copy, and 
> >>changing the original based on the copy?
> >>
> >>Jacob
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 





__ 
Celebrate Yahoo!'s 10th Birthday! 
Yahoo! Netrospective: 100 Moments of the Web 
http://birthday.yahoo.com/netrospective/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Linked List

2005-03-05 Thread Shitiz Bansal
I could not understand what you exactly mean.

In order to explain wat my problem is, here is an
example code. Its not exactly what I am doing, I am
using multiple threads and a rather complicated code
so  try and understand the sense rather than the code
itself.

>>> myls=range(50)
>>> for i in myls:
print i
if i==20:
myls.insert(5,5)

The point is, the list(both size and elements) is
changing even as it is being operated upon.

This particular case goes into infinite loop at i=20.

Interestingly, I can freely edit the list yet to be
traversed, without any ill effects, which would not
have been possible if a python list was a pure
array(due to immutable length of an array in c).

Shitiz

--- "Jacob S." <[EMAIL PROTECTED]> wrote:

> I'm taking a shot in the dark and answering here.
> 
> Have you tried making a copy of the list, iterating
> over the copy, and 
> changing the original based on the copy?
> 
> Jacob
> 
> > Hi,
> > Any body has any idea on how to implement a linked
> > list in python?
> > Ordinary python lists are not quite the same. what
> i
> > need  is a list i can traverse through
> sequentially
> > without bothering about the indices, as another
> > process is continuously editing this list, by
> > inserting and deleting elements even as the list
> is
> > being traversed.
> >
> > Shitiz
> >
> >
> >
> >
> > __
> > Celebrate Yahoo!'s 10th Birthday!
> > Yahoo! Netrospective: 100 Moments of the Web
> > http://birthday.yahoo.com/netrospective/
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> > 
> 
> 


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Linked List

2005-03-04 Thread Shitiz Bansal
Hi,
Any body has any idea on how to implement a linked
list in python?
Ordinary python lists are not quite the same. what i
need  is a list i can traverse through sequentially
without bothering about the indices, as another
process is continuously editing this list, by
inserting and deleting elements even as the list is
being traversed.

Shitiz




__ 
Celebrate Yahoo!'s 10th Birthday! 
Yahoo! Netrospective: 100 Moments of the Web 
http://birthday.yahoo.com/netrospective/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to use threads ?

2005-03-02 Thread Shitiz Bansal
The thread finishes when:
1.The run method finishes.
2.In case of the loop- you may -

>>> import threading
>>> class abc(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.cont_flag=1
def run(self):
while self.cont_flag:
pass

>>> abcd=abc()
>>> abcd

>>> abcd.start()
>>> abcd

>>> abcd.cont_flag=0
>>> abcd


Also, you may 

>>> del abcd
>>> abcd

Traceback (most recent call last):
  File "", line 1, in -toplevel-
abcd
NameError: name 'abcd' is not defined

I am not very sure about the CPU usage of a stopped
thread.Perhaps someone from the group can enlighten
us.

Hope this makes it clear.

Cheers,
Shitiz
--- Mark Kels <[EMAIL PROTECTED]> wrote:

> On Tue, 1 Mar 2005 12:25:37 -0800 (PST), Shitiz
> Bansal
> <[EMAIL PROTECTED]> wrote:
> > 
> > Here is a simple program
> > 
> > class abc(threading.Thread):
> > def __init__(self):
> > threading.Thread.__init__(self)
> > self.x=1 etc. etc.
> > self.cont_flag=1
> > def run(self):
> > while self.cont_flag:
> >print self.x
> >(you can set the self.cont_falg to zero
> > whenever you wish to end the loop(and
> > hence
> > the thread., either
> > here, or even from outside the thread.
> > of course, it isnt necessary that your
> > thread is a loop, it can be a finite
> > function.)
> > 
> Thanks alot !!
> I think I got it ths time.
> The only problem is that when I try to do it my
> thread doesnt closes.
> When does a thread closes ?
> 
> Thanks again. 
> 
> -- 
> 1. The day Microsoft makes something that doesn't
> suck is probably the
> day they start making vacuum cleaners.
> 2. Unix is user friendly - it's just picky about
> it's friends.
> 3. Documentation is like sex: when it is good, it is
> very, very good.
> And when it is bad, it is better than nothing. -
> Dick Brandon
> 





__ 
Celebrate Yahoo!'s 10th Birthday! 
Yahoo! Netrospective: 100 Moments of the Web 
http://birthday.yahoo.com/netrospective/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Re: Online Programming Contest (Python solutions accepted)

2005-03-02 Thread Shitiz Bansal
Hi,
some questions need to be addressed.
First, is execution time a factor, bcos admittedly
python sols r much slowere than c/c++.
second, wats the exact conf of python installed on
machines checking our sols.
i.e. which modules r allowed n which r not.
shitiz
--- Sridhar <[EMAIL PROTECTED]> wrote:

> Hi,
> 
> We, the students of CEG, Anna University [1] are
> organizing an online
> programming contest as part of aBaCus [2] 2005.  The
> contest itself
> will start on 6th March 2005 at 1:00 pm IST [3] and
> will end after 5
> hours.  You have to solve the problems posted at the
> start of the
> contest.  Teams ranking high will be awarded the
> prizes.
> 
> As a special note, inspite of C,C++ and Java we also
> allow Python [4]
> this time.  So we hope a lot of Pythonistas also
> could attend the
> contest for fun. :-)
> 
> For more details about the contest, visit the
> contest page
> 
>  --   http://203.197.138.181/OPC  --
> 
> [1] http://en.wikipedia.org/wiki/Anna_University
> [2] http://www.annauniv.edu/abacus/
> [3] Indian Standard Time (IST) is the time zone for
> India. It is 5
> hours and 30 minutes ahead of GMT/UTC.
> [4] http://www.python.org
> 
> --
> Sridhar Ratna - http://srid.bsdnerds.org
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to use threads ?

2005-03-01 Thread Shitiz Bansal

--- Mark Kels <[EMAIL PROTECTED]> wrote:

> Can anyone give me a very simple example on thread
> programming ?
> I looked at some tutorials but they don't really
> make sense...
> 
> 
> Thanks in advance
> -- 
> 1. The day Microsoft makes something that doesn't
> suck is probably the
> day they start making vacuum cleaners.
> 2. Unix is user friendly - it's just picky about
> it's friends.
> 3. Documentation is like sex: when it is good, it is
> very, very good.
> And when it is bad, it is better than nothing. -
> Dick Brandon
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 3 questions for my port scanner project

2005-02-27 Thread Shitiz Bansal


> 2. I got a while loop which does the port scan
> itself. How can I end
> it while its working ?

using the break statement anywhere inside the loop
will exit the loop.

> 3. For some reason the scan is too slow (2-3 seconds
> for a port). Is
> there a way to make it faster (other port scanner
> work allot faster...

The ports which do not respond are the ones which take
most of the time.You can use the timer object to fix
the time for each port.For eg. if a port does not
respond within .1 sec it can reasonably be expected to
be closed.The exact implementation will depend upon
your code.You can also use threads to ping more than
one port simultaneously.

And I loved your microsoft quote :).

Cheers
> -- 
> 1. The day Microsoft makes something that doesn't
> suck is probably the
> day they start making vacuum cleaners.
> 2. Unix is user friendly - it's just picky about
> it's friends.
> 3. Documentation is like sex: when it is good, it is
> very, very good.
> And when it is bad, it is better than nothing. -
> Dick Brandon
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 




__ 
Do you Yahoo!? 
Read only the mail you want - Yahoo! Mail SpamGuard. 
http://promotions.yahoo.com/new_mail 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] threads

2005-02-25 Thread Shitiz Bansal
I have run into a problem again.
I thought about simulation time concurrency and
figured an inherent problem with the approach.
The main objective of my project is to simulate
traffic  behavious under congested conditions.Hence I
was using semaphores to lock the areas already
occupied by the cars.A car wanting to move ahead
acquires the semaphore right ahead, releases the
semaphore on its old position, and so on...
Now, if i use the one thread approach, or use pseudo
threads like greenlets or tasklets, a deadlock occurs.
A car cant acquire the next semaphore until it is
released by the next car. The next car on the other
hand cant release it because the thread of control is
busy trying to acquire the semaphore inside the old
car's run method.
The way out could be maintaining a database of every
position on the road, in binary format, which would be
more efficient memory wise as well.
Any comments?
Another issue is...since the speed of every car is not
same but follows a random distribution,I was updating
the car positions after every 1/speed seconds.
To be able to apply the above database (or even
semaphoes for that matter)i would need a fixed number
of car positions.Hence I have to continue updating the
position after every 1/speed second. Hence I would
need a HCF(fraction) of all the 1/speeds(which are
actually floats, but approximations can be made). How
do i calculate this HCF??
Remember that I am generating cars even while the
simulation is running, hence calculating this HCF at
the beginning is not going to work. 
Any comments?
--- Shitiz Bansal <[EMAIL PROTECTED]> wrote:

> Hi,
> Thanx everyone for all your suggestions.I used a
> separate thread for every car as I wanted my cars to
> run in real time, to coordinate effectively with
> other
> systems like traffic lights for eg. which I cant
> edit,
> hence which cant use the variable time defined by
> me.
> 
> I figured out two ways to achieve this
> 
> On Linux... ulimit -s 64, limits the stacksize of
> each
> thread to 64Kb, and i was able to generate 8000
> threads.However upgrading my program to support more
> cars was a problem.
> 
> Another way was to spawn 1000 threads, handling abt
> 10
> cars each...this way the diff in real time and my
> program time was limited.
> 
> I tried my hands at Stackless too... but still had
> problems implementing the concept.
> 
> Can anyone guide me on how to spawn simultaneously(
> or
> pseudo simultaneously) running microthreads using
> stackless.
> 
> Here is what i tried..
> 
> ef gencars(num,origin,dest,speed):
> global adjls
> global cars
> global juncls
> for i in range(num):
>  
>  
>  cars.append(car(orig,dest,speed)
>  task=tasklet(cars[i].run())
>  task.setup('Bind using Setup')
> 
> Now this is what i copied from somewhere...i dont
> claim to understand fully what is happening.Here
> car.run() is a process which takes a long time to
> execute.
> 
> What happens on execution isOne car is
> initialised
> and then the program waits for its run method to
> complete before proceeding.
> 
> I also feel that there is no clear documentation on
> stackless.
> 
> Show me the light.
> 
> Shitiz
> 
>  
> --- Hugo González Monteverde
> <[EMAIL PROTECTED]>
> wrote:
> 
> > OK, as ignorant as I may be on threads, I have
> read
> > a bit on Stackless 
> > Python's tasklets. Suppossedly they require much
> > less memory than 
> > python's threads.
> > 
> > Information is at www.stackless.com
> > 
> > Hugo
> > 
> > Shitiz Bansal wrote:
> > > Hi,
> > > 
> > > I am trying to build a traffic network simulator
> > using
> > > python, for my degree project.
> > > 
> > > I need to run at least 5-6000 cars
> > simultaneously.I
> > > wanted to run each car in a separate thread.
> > > However , after about 400 threads i am unable to
> > > create new threads.
> > > 
> > > Here's the code:
> > > 
> > >>>>cars=range(1000)
> > >>>>for i in cars:
> > > 
> > >   cars[i]=cars[i]=car(1,10,2,1,adjls,juncls)
> > > 
> > >   
> > > 
> > >>>>for i in cars:
> > > 
> > >   i.start()
> > > 
> > > Traceback (most recent call last):
> > >   File "", line 2, in -toplevel-
> > > i.start()
> > > error: can't start new thread
> > > 
> > > Is there a way out.Also, are there any tips on
> > > performance issues?
>

Re: [Tutor] threads

2005-02-25 Thread Shitiz Bansal
Hi,
Thanx everyone for all your suggestions.I used a
separate thread for every car as I wanted my cars to
run in real time, to coordinate effectively with other
systems like traffic lights for eg. which I cant edit,
hence which cant use the variable time defined by me.

I figured out two ways to achieve this

On Linux... ulimit -s 64, limits the stacksize of each
thread to 64Kb, and i was able to generate 8000
threads.However upgrading my program to support more
cars was a problem.

Another way was to spawn 1000 threads, handling abt 10
cars each...this way the diff in real time and my
program time was limited.

I tried my hands at Stackless too... but still had
problems implementing the concept.

Can anyone guide me on how to spawn simultaneously( or
pseudo simultaneously) running microthreads using
stackless.

Here is what i tried..

ef gencars(num,origin,dest,speed):
global adjls
global cars
global juncls
for i in range(num):  
 
 cars.append(car(orig,dest,speed)
 task=tasklet(cars[i].run())
 task.setup('Bind using Setup')

Now this is what i copied from somewhere...i dont
claim to understand fully what is happening.Here
car.run() is a process which takes a long time to
execute.

What happens on execution isOne car is initialised
and then the program waits for its run method to
complete before proceeding.

I also feel that there is no clear documentation on
stackless.

Show me the light.

Shitiz

 
--- Hugo González Monteverde <[EMAIL PROTECTED]>
wrote:

> OK, as ignorant as I may be on threads, I have read
> a bit on Stackless 
> Python's tasklets. Suppossedly they require much
> less memory than 
> python's threads.
> 
> Information is at www.stackless.com
> 
> Hugo
> 
> Shitiz Bansal wrote:
> > Hi,
> > 
> > I am trying to build a traffic network simulator
> using
> > python, for my degree project.
> > 
> > I need to run at least 5-6000 cars
> simultaneously.I
> > wanted to run each car in a separate thread.
> > However , after about 400 threads i am unable to
> > create new threads.
> > 
> > Here's the code:
> > 
> >>>>cars=range(1000)
> >>>>for i in cars:
> > 
> > cars[i]=cars[i]=car(1,10,2,1,adjls,juncls)
> > 
> > 
> > 
> >>>>for i in cars:
> > 
> > i.start()
> > 
> > Traceback (most recent call last):
> >   File "", line 2, in -toplevel-
> > i.start()
> > error: can't start new thread
> > 
> > Is there a way out.Also, are there any tips on
> > performance issues?
> > 
> > Here is the class car:
> > 
> > class car(threading.Thread):
> > def
> > __init__(self,carid,speed,dest,orig,adjls,juncls):
> > threading.Thread.__init__(self)
> > self.speed=speed
> > self.finished=0
> > self.carid=carid
> > self.dest=dest
> > self.orig=orig
> > self.adjls=adjls
> > self.juncls=juncls
> >
> >
>
self.shortest=find_shortest_path(adjls,self.dest,self.orig)
> >
> self.calc=findpaths(adjls,self.dest,self.orig)
> >
> >
>
self.stats={'currtrsp':0,'avgspeed':0,'distcov':0,'totaltime':0}
> > def traverse_track(self,p1,p2):
> > counter=0
> > time=0
> > while self.adjls[p1][counter].to!=p2:
> > counter=counter+1
> > self.track=self.adjls[p1][counter]
> > self.pos=0
> > if self.speed>self.track.speed:
> > speed=self.track.speed
> > else:
> > speed=self.speed
> > while self.pos!=self.track.length:
> > if self.track.state.has_key(self.pos):
> >
> self.track.state[self.pos].acquire()
> > else:
> >
> >
>
self.track.state[self.pos]=threading.Semaphore(value=self.track.lanes)
> >
> self.track.state[self.pos].acquire()
> > if self.pos!=0:
> >
> self.track.state[self.pos-1].release()
> > self.pos=self.pos+1
> > sleep(1.0/speed)
> > time=time+1.0/speed
> >
> >
> self.stats['currtrsp']=float(self.track.length)/time
> > if self.stats['avgspeed']:
> >
> >
>
self.stats['avgspeed']=float(self.stats['distcov']+self.track.length)/(self.stats['distcov']/self.stats['avgspeed']+self

Re: [Tutor] threads

2005-02-23 Thread Shitiz Bansal
Hi,
Googling for my problem i found the following page:

http://gnosis.cx/publish/programming/charming_python_b7.html

It doesnt make much sense to me.It seems that the
author is implementing a series of routines rather
than  running them sumultaneously as threads are
supposed to do.Am i missing the point?

Shitiz
--- Shitiz Bansal <[EMAIL PROTECTED]> wrote:

> Hi,
> 
> I am trying to build a traffic network simulator
> using
> python, for my degree project.
> 
> I need to run at least 5-6000 cars simultaneously.I
> wanted to run each car in a separate thread.
> However , after about 400 threads i am unable to
> create new threads.
> 
> Here's the code:
> >>> cars=range(1000)
> >>> for i in cars:
>   cars[i]=cars[i]=car(1,10,2,1,adjls,juncls)
> 
>   
> >>> for i in cars:
>   i.start()
> 
> Traceback (most recent call last):
>   File "", line 2, in -toplevel-
> i.start()
> error: can't start new thread
> 
> Is there a way out.Also, are there any tips on
> performance issues?
> 
> Here is the class car:
> 
> class car(threading.Thread):
> def
> __init__(self,carid,speed,dest,orig,adjls,juncls):
> threading.Thread.__init__(self)
> self.speed=speed
> self.finished=0
> self.carid=carid
> self.dest=dest
> self.orig=orig
> self.adjls=adjls
> self.juncls=juncls
>
>
self.shortest=find_shortest_path(adjls,self.dest,self.orig)
>
> self.calc=findpaths(adjls,self.dest,self.orig)
>
>
self.stats={'currtrsp':0,'avgspeed':0,'distcov':0,'totaltime':0}
> def traverse_track(self,p1,p2):
> counter=0
> time=0
> while self.adjls[p1][counter].to!=p2:
> counter=counter+1
> self.track=self.adjls[p1][counter]
> self.pos=0
> if self.speed>self.track.speed:
> speed=self.track.speed
> else:
> speed=self.speed
> while self.pos!=self.track.length:
> if self.track.state.has_key(self.pos):
> self.track.state[self.pos].acquire()
> else:
>
>
self.track.state[self.pos]=threading.Semaphore(value=self.track.lanes)
> self.track.state[self.pos].acquire()
> if self.pos!=0:
>
> self.track.state[self.pos-1].release()
> self.pos=self.pos+1
> sleep(1.0/speed)
> time=time+1.0/speed
>
> self.stats['currtrsp']=float(self.track.length)/time
> if self.stats['avgspeed']:
>
>
self.stats['avgspeed']=float(self.stats['distcov']+self.track.length)/(self.stats['distcov']/self.stats['avgspeed']+self.track.length/self.stats['currtrsp'])
> else:
>
> self.stats['avgspeed']=self.stats['currtrsp']
>
> self.stats['totaltime']=self.stats['totaltime']+time
> if self.track.stats['avgspeed']:
>
>
self.track.stats['avgspeed']=(self.track.stats['avgspeed']*self.track.stats['traffictotal']+self.stats['currtrsp'])/(self.track.stats['traffictotal']+1)
> else:
>
> self.track.stats['avgspeed']=self.stats['currtrsp']
>
>
self.stats['distcov']=self.stats['distcov']+self.track.length
>
>
self.track.stats['traffictotal']=self.track.stats['traffictotal']+1
> def
> cross_junction(self,juncls,juncid,orig,dest):
> marker=str(orig)+'-'+str(dest)
> if juncls[juncid].free.has_key(marker):
> self.track.state[self.pos].release()
> else:
> while not
> juncls[juncid].signalled['green'].has_key(marker):
> sleep(0.2)
> self.track.state[self.pos-1].release()
> def run(self):
> path=self.shortest
> counter=1
> for i in path[:1]:
> self.traverse_track(i,path[counter])
> if not counter==len(path)-1:
>
>
self.cross_junction(self.juncls,path[counter],i,path[counter+1])
> counter=counter+1
> self.finished=1
> self.track.state[self.pos-1].release()
> 
> 
> 
> __
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam
> protection around 
> http://mail.yahoo.com 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 




__ 
Do you Yahoo!? 
Yahoo! Mail - now with 250MB free storage. Learn more.
http://info.mail.yahoo.com/mail_250
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] threads

2005-02-23 Thread Shitiz Bansal
Hi,

I am trying to build a traffic network simulator using
python, for my degree project.

I need to run at least 5-6000 cars simultaneously.I
wanted to run each car in a separate thread.
However , after about 400 threads i am unable to
create new threads.

Here's the code:
>>> cars=range(1000)
>>> for i in cars:
cars[i]=cars[i]=car(1,10,2,1,adjls,juncls)


>>> for i in cars:
i.start()

Traceback (most recent call last):
  File "", line 2, in -toplevel-
i.start()
error: can't start new thread

Is there a way out.Also, are there any tips on
performance issues?

Here is the class car:

class car(threading.Thread):
def
__init__(self,carid,speed,dest,orig,adjls,juncls):
threading.Thread.__init__(self)
self.speed=speed
self.finished=0
self.carid=carid
self.dest=dest
self.orig=orig
self.adjls=adjls
self.juncls=juncls
   
self.shortest=find_shortest_path(adjls,self.dest,self.orig)
self.calc=findpaths(adjls,self.dest,self.orig)
   
self.stats={'currtrsp':0,'avgspeed':0,'distcov':0,'totaltime':0}
def traverse_track(self,p1,p2):
counter=0
time=0
while self.adjls[p1][counter].to!=p2:
counter=counter+1
self.track=self.adjls[p1][counter]
self.pos=0
if self.speed>self.track.speed:
speed=self.track.speed
else:
speed=self.speed
while self.pos!=self.track.length:
if self.track.state.has_key(self.pos):
self.track.state[self.pos].acquire()
else:
   
self.track.state[self.pos]=threading.Semaphore(value=self.track.lanes)
self.track.state[self.pos].acquire()
if self.pos!=0:
self.track.state[self.pos-1].release()
self.pos=self.pos+1
sleep(1.0/speed)
time=time+1.0/speed
   
self.stats['currtrsp']=float(self.track.length)/time
if self.stats['avgspeed']:
   
self.stats['avgspeed']=float(self.stats['distcov']+self.track.length)/(self.stats['distcov']/self.stats['avgspeed']+self.track.length/self.stats['currtrsp'])
else:
   
self.stats['avgspeed']=self.stats['currtrsp']
   
self.stats['totaltime']=self.stats['totaltime']+time
if self.track.stats['avgspeed']:
   
self.track.stats['avgspeed']=(self.track.stats['avgspeed']*self.track.stats['traffictotal']+self.stats['currtrsp'])/(self.track.stats['traffictotal']+1)
else:
   
self.track.stats['avgspeed']=self.stats['currtrsp']
   
self.stats['distcov']=self.stats['distcov']+self.track.length
   
self.track.stats['traffictotal']=self.track.stats['traffictotal']+1
def cross_junction(self,juncls,juncid,orig,dest):
marker=str(orig)+'-'+str(dest)
if juncls[juncid].free.has_key(marker):
self.track.state[self.pos].release()
else:
while not
juncls[juncid].signalled['green'].has_key(marker):
sleep(0.2)
self.track.state[self.pos-1].release()
def run(self):
path=self.shortest
counter=1
for i in path[:1]:
self.traverse_track(i,path[counter])
if not counter==len(path)-1:
   
self.cross_junction(self.juncls,path[counter],i,path[counter+1])
counter=counter+1
self.finished=1
self.track.state[self.pos-1].release()



__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python sizeof()

2005-02-23 Thread Shitiz Bansal
Hi,
Is there a python equivalent of c's sizeof function.
If not, then what are the typical sizes of the
BoundedSemaphore object in python?

Shitiz



__ 
Do you Yahoo!? 
Yahoo! Sports - Sign up for Fantasy Baseball. 
http://baseball.fantasysports.yahoo.com/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] killing a thread

2005-02-22 Thread Shitiz Bansal
Hi,

Is there a way to terminate a thread from another
thread?

Shitiz

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] manipulating a file

2005-02-07 Thread Shitiz Bansal
I aplogise for a typo...

Please read the command as:
 cat log|grep -v -E [[:alnum]]'{2096,}'>> log.bak

note the missing comma in the previous command.
--- Shitiz Bansal <[EMAIL PROTECTED]> wrote:

> 
> How about 
>cat log|grep -v -E [[:alnum]]'{2096}'>> log.bak
> 
> The issue is - will unix shell command be any more
> efficient than a python script??
> 
> Also i used append because i gathered that the user
> will not want to erase the previous logs. He is free
> to use a single > if he does.
> 
> 
> --- Alan Gauld <[EMAIL PROTECTED]> wrote:
> 
> > > About the efficiency, why do u need python at
> > all...
> > > How abt a simple shell command
> > >cat httpd-access.log>>log.bak
> > > 
> > 
> > Because that would be a copy, well actually an
> > append...
> > 
> > cp httpd-access.log log.bak
> > 
> > would be better!
> > 
> > But the OP wanted to strip out long lines in
> transit
> > not just copy...
> > 
> > Alan G.
> > 
> > 
> > 
> 
> 
> 
>   
> __ 
> Do you Yahoo!? 
> Yahoo! Mail - Find what you need with new enhanced
> search.
> http://info.mail.yahoo.com/mail_250
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] manipulating a file

2005-02-07 Thread Shitiz Bansal

How about 
   cat log|grep -v -E [[:alnum]]'{2096}'>> log.bak

The issue is - will unix shell command be any more
efficient than a python script??

Also i used append because i gathered that the user
will not want to erase the previous logs. He is free
to use a single > if he does.


--- Alan Gauld <[EMAIL PROTECTED]> wrote:

> > About the efficiency, why do u need python at
> all...
> > How abt a simple shell command
> >cat httpd-access.log>>log.bak
> > 
> 
> Because that would be a copy, well actually an
> append...
> 
> cp httpd-access.log log.bak
> 
> would be better!
> 
> But the OP wanted to strip out long lines in transit
> not just copy...
> 
> Alan G.
> 
> 
> 




__ 
Do you Yahoo!? 
Yahoo! Mail - Find what you need with new enhanced search.
http://info.mail.yahoo.com/mail_250
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Iterating over multiple lists- options

2005-02-07 Thread Shitiz Bansal
Hi,
My solution might raise purist's eyebrows but here it
goes...
How about using a try loop every time you read from
the list.

try:
   x=list[someno]
except:
   x=nothing(or whatever)

This goes on till the all lists start returning none.
for shorter lists try throws an index out of range
exception which is caught by except.

Shitiz
--- Tony Cappellini <[EMAIL PROTECTED]> wrote:

> 
> 
> I'm trying to generate an HTML table, from multiple
> lists.
> 
> There are 4 lists total, each of which *may* have a
> different length from 
> the other lists.
> Each list has been stored in a master dictionary.
> 
> 
> North=[Bill, Bob, Sue, Mary]
> South=['Tim', ''Tom', 'Jim', 'John', 'Carl', 'Evan',
> 'Rich']
> etc
> 
> d1={'North':North, 'South':South, 'East':East,
> 'West':West]
> 
> 
> I want to iterate over all the lists a the same
> time, so I can populate an 
> html table.
> This is approximately what the HTML table should
> look like, but the lists 
> can be in any order, top to bottom, and left to
> right.
> 
> South  North East West
> 
> Tim  BillMay  Ellen
> Tom Bob   Mick
> Jim  Sue   Ron
> JohnMary  Keith
> Carl  Joey
> Evan
> Rich
> 
> 
> Looking through my books on Python I've found
> examples for zip() and map() 
> both of which have serious shortcomings
> That being, both of these functions can truncate the
> data, depending on 
> certain conditions
> 
> When iterating over multiple lists, it is fine if
> the mechanism returns an 
> empty string , or None for a non-existing list item.
> I just wont display anything in the HTML table for
> missing items.
> I know how to create the HTML table, statically. The
> problem is being able 
> to fill the table in one pass (preferably), which
> means my program would 
> need to iterate over
> more than one list at the same time.
> 
> Even using the range to generate an index has
> different effects, depending 
> on the order in which the lists are referenced in
> the for loop.
> 
> Are there any other options available for iterating
> over multiple lists ?
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 




__ 
Do you Yahoo!? 
The all-new My Yahoo! - What will yours do?
http://my.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] manipulating a file

2005-02-07 Thread Shitiz Bansal
Hi,
I do see a problem.
The script is fine, the problem lies else where.

Your script is trying to write log.bak to log, it
should b other way round.

i.e
srcfile = open('/var/log/httpd-access.log', 'r')
dstfile = open('/var/log/httpd-access.log.bak', 'w')

hope that fixes it.

About the efficiency, why do u need python at all...
How abt a simple shell command
   cat httpd-access.log>>log.bak

Shitiz

--- Danny Yoo <[EMAIL PROTECTED]> wrote:

> 
> 
> On Mon, 7 Feb 2005, Reed L. O'Brien wrote:
> 
> > I want to read the httpd-access.log and remove any
> oversized log records
> >
> > I quickly tossed this script together.  I manually
> mv-ed log to log.bak
> > and touched a new logfile.
> >
> > running the following with print i uncommented
> does print each line to
> > stdout.  but it doesn't write to the appropriate
> file...
> 
> 
> Hello!
> 
> Let's take a look at the program again:
> 
> ###
> import os
> srcfile = open('/var/log/httpd-access.log.bak', 'r')
> dstfile = open('/var/log/httpd-access.log', 'w')
> while 1:
>  lines = srcfile.readlines()
>  if not lines: break
>  for i in lines:
>  if len(i) < 2086:
>  dstfile.write(i)
> srcfile.close()
> dstfile.close()
> ###
> 
> > a) what am I missing?
> > b) is there a less expensive way to do it?
> 
> Hmmm... I don't see anything offhand that prevents
> httpd-access.log from
> containing the lines you expect.  Do you get any
> error messages, like
> permission problems, when you run the program?
> 
> Can you show us how you are running the program, and
> how you are checking
> that the resulting file is empty?
> 
> 
> Addressing the question on efficiency and expense:
> yes.  The program at
> the moment tries to read all lines into memory at
> once, and this is
> expensive if the file is large.  Let's fix this.
> 
> 
> In recent versions of Python, we can modify
> file-handling code from:
> 
> ###
> lines = somefile.readlines()
> for line in lines:
> ...
> ###
> 
> to this:
> 
> ###
> for line in somefile:
> ...
> ###
> 
> That is, we don't need to extract a list of 'lines'
> out of a file.
> Python allows us to loop directly across a file
> object.  We can find more
> details about this in the documentation on
> "Iterators" (PEP 234):
> 
> http://www.python.org/peps/pep-0234.html
> 
> Iterators are a good thing to know, since Python's
> iterators are deeply
> rooted in the language design.  (Even if it they
> were retroactively
> embedded.  *grin*)
> 
> 
> A few more comments: the while loop appears
> unnecessary, since on the
> second run-through the loop, we'll have already read
> all the lines out of
> the file.  (I am assuming that nothing is writing to
> the backup file at
> the time.)  If the body of a while loop just runs
> once, we don't need a
> loop.
> 
> This simplifies the code down to:
> 
> ###
> srcfile = open('/var/log/httpd-access.log.bak', 'r')
> dstfile = open('/var/log/httpd-access.log', 'w')
> for line in srcfile:
> if len(line) < 2086:
> dstfile.write(line)
> srcfile.close()
> dstfile.close()
> ###
> 
> 
> I don't see anything else here that causes the file
> writing to fail.  If
> you can tell us more information on how you're
> checking the program's
> effectiveness, that may give us some more clues.
> 
> Best of wishes to you!
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 




__ 
Do you Yahoo!? 
Yahoo! Mail - Helps protect you from nasty viruses. 
http://promotions.yahoo.com/new_mail
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] freeze

2005-02-05 Thread Shitiz Bansal
Hi,

Do exe files generated by py2run on linux???i thought
it was only for windows.

Shitiz
--- "Jacob S." <[EMAIL PROTECTED]> wrote:

> My two bits.
> 
> 1) Download py2exe found here
> http://py2exe.sourceforge.net/
> 2) Make a setup file -- intructions can be found
> through above link, I 
> think.
> (Hey that rhymes!)
> 3) Ask if you want my totally cool automation
> technique
> 4) Ask more questions, go on we don't mind.
> 
> HTH,
> Jacob Schmidt
> 
> > Hi,
> >
> > I intend to create compiled python binaries on
> linux.
> > I understand that freeze can be used to do this.
> > But I have few doubts i would like to clarify.
> >
> > 1. In the freeze documentation i found the lines:
> >
> > "One tricky issue: Freeze assumes that the Python
> > interpreter and
> > environment you're using to run Freeze is the same
> one
> > that would be
> > used to run your program, which should also be the
> > same whose sources
> > and installed files you will learn about in the
> next
> > section.  In
> > particular, your PYTHONPATH setting should be the
> same
> > as for running
> > your program locally.  (Tip: if the program
> doesn't
> > run when you type
> > "python hello.py" there's little chance of getting
> the
> > frozen version
> > to run.)"
> >
> > My intention is to create files which can be run
> on
> > Linux systems with python not installed.Do the
> above
> > lines mean that freeze can't do it(which defeats
> the
> > very pupose of the program i guess.).
> >
> > 2. While compiling with freezeit seems that it
> is
> > including all the available modules, even if they
> are
> > not required.Of course using freeze -X is one
> > option,but it being really cumbersome, is there a
> > better option available.
> >
> > In case the above issues do present a problem, is
> > there any alternative to freeze?
> >
> > Shitiz
> >
> >
> >
> > __
> > Do you Yahoo!?
> > The all-new My Yahoo! - Get yours free!
> > http://my.yahoo.com
> >
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> > 
> 
> 





__ 
Do you Yahoo!? 
Yahoo! Mail - You care about security. So do we. 
http://promotions.yahoo.com/new_mail
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] freeze

2005-02-04 Thread Shitiz Bansal
Hi,

I intend to create compiled python binaries on linux.
I understand that freeze can be used to do this.
But I have few doubts i would like to clarify.

1. In the freeze documentation i found the lines:

"One tricky issue: Freeze assumes that the Python
interpreter and
environment you're using to run Freeze is the same one
that would be
used to run your program, which should also be the
same whose sources
and installed files you will learn about in the next
section.  In
particular, your PYTHONPATH setting should be the same
as for running
your program locally.  (Tip: if the program doesn't
run when you type
"python hello.py" there's little chance of getting the
frozen version
to run.)"

My intention is to create files which can be run on
Linux systems with python not installed.Do the above
lines mean that freeze can't do it(which defeats the
very pupose of the program i guess.).

2. While compiling with freezeit seems that it is
including all the available modules, even if they are
not required.Of course using freeze -X is one
option,but it being really cumbersome, is there a
better option available.

In case the above issues do present a problem, is
there any alternative to freeze?

Shitiz



__ 
Do you Yahoo!? 
The all-new My Yahoo! - Get yours free! 
http://my.yahoo.com 
 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Traffic Network simulator

2005-01-31 Thread Shitiz Bansal
Hi,

I need a traffic network simulator(for roads) for my
project work.I would prefer the simulator to be in
python so that i can reprogram/modify it according to
my needs.
does anyone know where i can find something of the
sort or are there any special modules available which
can help me build one?

Shitiz



__ 
Do you Yahoo!? 
All your favorites on one personal page – Try My Yahoo!
http://my.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor