anil maran | 1 Jul 09:13 2006
Picon

static variables - lock/semaphore

hi
i have a program that is run on a website, and as it
is run from a website, it can be called more than once
and hence could end up corrupting db/data
is there a way to use some sort of semaphore or lock
so that it is not accessed simultaneously
the problem is it can be done with static variables in
C but how is it done in python
thanks a lot

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Alan Gauld | 1 Jul 15:06 2006
Picon

Re: static variables - lock/semaphore

> i have a program that is run on a website, and as it
> is run from a website, it can be called more than once
> and hence could end up corrupting db/data
> is there a way to use some sort of semaphore or lock
> so that it is not accessed simultaneously

There are ways of doing this in Python but if you are using 
a relational database for the data its usually easier to 
apply row level locking at the database level.

> the problem is it can be done with static variables in
> C but how is it done in python

It could be done with a global variable in Python in the 
same way but there are other solutions based around
threads which have explicit locks etc. For a web 
application using Python threads would probably be 
a good bet.

But you may find the whole problem simplified by adopting a 
web framework such as TurboGears which will take care of 
a lot of these kinds of issues for you - but with a bit of a 
learning curve. But if you have several web apps to build 
I'd seriously consider this route.

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

_______________________________________________
(Continue reading)

Liviu Antoniu | 1 Jul 16:03 2006
Picon

python debugger

Hi, guys,

I have a small  problem with python's debugger. I don't know how can I debugg a script through this debugger.
Can you tell me?
Thank you very much

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Kent Johnson | 1 Jul 16:30 2006
Picon

Re: static variables - lock/semaphore

anil maran wrote:
> hi
> i have a program that is run on a website, and as it
> is run from a website, it can be called more than once
> and hence could end up corrupting db/data
> is there a way to use some sort of semaphore or lock
> so that it is not accessed simultaneously
> the problem is it can be done with static variables in
> C but how is it done in python

If you are using a database for storage you should learn how to use the 
transactional features of the database. They are designed to let you 
control what happens when multiple users access the database at the same 
time.

To protect access to other kinds of data look at threading.Lock and RLock.

Kent

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Danny Yoo | 1 Jul 19:18 2006
Picon

Re: saving output in a text file (fwd)

[forwarding to Tutor]

---------- Forwarded message ----------
Date: Sat, 01 Jul 2006 13:58:47 +0400
From: Hafsa raza <hafsaraza <at> hotmail.com>
To: dyoo <at> hkn.eecs.berkeley.edu
Subject: Re: [Tutor] saving output in a text file

Thank you for the quick reply. Let me put my question in a more
clearer way for you.

If we want to create a text file and write some text into it we use the
following command in Python:

myfile = open("test.txt","w")<?xml:namespace prefix = o ns =
"urn:schemas-microsoft-com:office:office" />

myfile.write("hello world")

But what if instead of writing the text 'hello world', i want to write
the output returned by a regular expression pattern, into the text file,
how would we specify that in the write command.

Regards,

Hafsa

________________________________________________________________________________
From:  Danny Yoo <dyoo <at> hkn.eecs.berkeley.edu>
To:  Hafsa raza <hafsaraza <at> hotmail.com>
CC:  tutor <at> python.org
Subject:  Re: [Tutor] saving output in a text file
Date:  Fri, 30 Jun 2006 15:04:41 -0700 (PDT)
>    def double(s):
>        "double: string -> string
>        doubles up the input string s."
>        return s + s

Gaah.  Typos.  My apologies.  Here's a correction to double()

     def double(s):
         """double: string -> string
         Doubles up the input string s.  For example, double("abc")
         should return "abcabc".
         """
         return s + s

>Would you be able to write a program that takes "hello world",
runs
>it through double(), and writes out "hello worldhelloworld" to
disk?

I meant to say that the expected content of the file should be:

      "hello worldhello world"

My apologies; I rushed that message too quickly.

________________________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar MSN Toolbar Get it now!
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Danny Yoo | 1 Jul 19:40 2006
Picon

Re: saving output in a text file (fwd)

> If we want to create a text file and write some text into it we use the 
> following command in Python:
>
> myfile = open("test.txt","w")<?xml:namespace prefix = o ns =
> "urn:schemas-microsoft-com:office:office" />
>
> myfile.write("hello world")
>
> But what if instead of writing the text 'hello world', i want to write 
> the output returned by a regular expression pattern, into the text file, 
> how would we specify that in the write command.

Just as a disclaimer: I am doing my utmost not to give out The Answer 
here.  So if it sounds like I'm not being very direct, that's precisely 
because you have a good intuition.  *grin* I'm trying to poke at the 
source of the problem, and not the immediate side effects.

You still haven't tackled my initial question about:

>     def double(s):
>         """double: string -> string
>         Doubles up the input string s.  For example, double("abc")
>         should return "abcabc".
>         """
>         return s + s
>
>
> Would you be able to write a program that takes "hello world", runs it 
> through double(), and writes out "hello worldhello world" to disk?

Can you try this mini-problem first?  What you've done doesn't touch on 
the question: would you know how to use double() as a part of the write()?

Note that this is just a variation of your original question, replacing 
the regex part with the use of a helper function.  It seems like this 
should be even easier than the problem you're having now.

Here's why I'm asking this: if you're getting stuck at this point too, 
then I'll assume that the problem is that you are unfamiliar with using 
helper functions and function composition.  Then the group as a whole can 
help you with that, since that's a fundamental programming skill that 
you'll want to develop.

But if you can do the mini-exercise, then the focus shifts to the use of 
regular expressions, and things become much easier, since it'll mean that 
you're not familiar with the regex API.
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

anil maran | 2 Jul 09:18 2006
Picon

help regarding string

please help me format this string from 
input : 2006-06-16 16:23:27.703000 to
output: 2006-06-16 16:23:27

or 2006-06-16 4:23:27 PM

thanks a lot 

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Dave S | 2 Jul 12:09 2006
Picon

can't import module

Hi all,

I wrote some script a while back but can no longer get it to run, since then I 
have upgraded my system from breezy to dapper (kubuntu) I now cannot import a 
module I wrote. I just know its got to be simple and am somewhat embarrassed 
that I cannot see the problem :(

dave <at> dave-comp:~/my_files/my_gg/gg1.4/get_data$ ./live_datad.py
Traceback (most recent call last):
  File "./live_datad.py", line 15, in ?
    from logger import log
ImportError: No module named logger

OK that's my problem 

dave <at> dave-comp:~/my_files/my_gg/gg1.4/get_data$ echo $PYTHONPATH
/home/dave/my_files/my_gg/gg1.4/configs:/home/dave/my_files/my_gg/gg1.4/logs:/home/dave/my_files/my_gg/gg1.4/get_data:/home/dave/my_files/my_gg/gg1.4/gg_utils:/home/dave/my_files/my_gg/gg1.4/ipc:/home/dave/my_files/my_gg/gg1.4/process_data:/home/dave/my_files/my_gg/gg1.4/common_utils
dave <at> dave-comp:~/my_files/my_gg/gg1.4/get_data$
dave <at> dave-comp:~/my_files/my_gg/gg1.4/get_data$ 
ls -al /home/dave/my_files/my_gg/gg1.4/gg_utils

total 92
drwxr-xr-x  2 dave dave 4096 2006-03-27 13:03 .
drwxr-xr-x 13 dave dave 4096 2006-06-07 22:12 ..
-rwxr-xr-x  1 dave dave 1301 2005-01-31 20:35 archive_read.py
-rw-r--r--  1 dave dave 1612 2005-02-06 10:18 archive_read.pyc
-rw-r--r--  1 dave dave 1612 2005-02-06 12:44 archive_read.pyo
-rwxr-xr-x  1 dave dave 2630 2005-02-04 22:54 convert_data.py
-rw-r--r--  1 dave dave 2631 2005-02-06 10:18 convert_data.pyc
-rw-r--r--  1 dave dave 2631 2005-02-06 12:44 convert_data.pyo
-rw-r--r--  1 dave dave 1146 2005-01-31 20:35 cookie_string.py
-rw-r--r--  1 dave dave 1315 2005-11-20 15:21 cookie_string.pyc
-rw-r--r--  1 dave dave 1420 2005-02-06 12:44 cookie_string.pyo
-rwxr-xr-x  1 dave dave 1531 2005-02-04 12:34 dump_key.py
-rw-r--r--  1 dave dave 1470 2005-02-04 12:34 dump_key.pyc
-rw-r--r--  1 dave dave 1470 2005-02-06 12:44 dump_key.pyo
-rwxr-xr-x  1 dave dave 1906 2005-01-31 20:35 html_strip.py
-rw-r--r--  1 dave dave 2069 2005-11-20 15:13 html_strip.pyc
-rw-r--r--  1 dave dave 2380 2005-02-06 12:44 html_strip.pyo
-rwxr--r--  1 dave dave 2646 2005-01-31 20:35 logger.py
-rw-r--r--  1 dave dave 3212 2005-11-20 15:13 logger.pyc
-rw-r--r--  1 dave dave 3546 2005-02-06 12:44 logger.pyo
-rwxr-xr-x  1 dave dave 1873 2005-01-31 20:35 tail_log.py
-rw-r--r--  1 dave dave 1990 2005-02-06 10:18 tail_log.pyc
-rw-r--r--  1 dave dave 1990 2005-02-06 12:44 tail_log.pyo
dave <at> dave-comp:~/my_files/my_gg/gg1.4/get_data$
dave <at> dave-comp:~/my_files/my_gg/gg1.4/get_data$    

so logger.py is in PYTHONPATH so why can't live_data.d see it ?

Any ideas .. 

Dave

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Kent Johnson | 2 Jul 12:20 2006
Picon

Re: help regarding string

anil maran wrote:
> please help me format this string from 
> input : 2006-06-16 16:23:27.703000 to
> output: 2006-06-16 16:23:27
> 
> or 2006-06-16 4:23:27 PM

str.split() can extract everything up to the first period:
In [4]: '2006-06-16 16:23:27.703000'.split('.')[0]
Out[4]: '2006-06-16 16:23:27'

Kent

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Kent Johnson | 2 Jul 12:29 2006
Picon

Re: can't import module

Dave S wrote:
>  > dave <at> dave-comp:~/my_files/my_gg/gg1.4/get_data$ ./live_datad.py
> Traceback (most recent call last):
>   File "./live_datad.py", line 15, in ?
>     from logger import log
> ImportError: No module named logger
> 
> dave <at> dave-comp:~/my_files/my_gg/gg1.4/get_data$ echo $PYTHONPATH
> /home/dave/my_files/my_gg/gg1.4/configs:/home/dave/my_files/my_gg/gg1.4/logs:/home/dave/my_files/my_gg/gg1.4/get_data:/home/dave/my_files/my_gg/gg1.4/gg_utils:/home/dave/my_files/my_gg/gg1.4/ipc:/home/dave/my_files/my_gg/gg1.4/process_data:/home/dave/my_files/my_gg/gg1.4/common_utils

I don't know why your import is failing, but from your PYTHONPATH it 
looks like you should consider making all the gg1.4 directories into 
packages, then you would have imports such as
from gg_utils.logger import log

This would simplify PYTHONPATH and I think reflect more accurately what 
you are doing.

Kent

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor


Gmane