Re: [Tutor] Need Some Help

2007-08-27 Thread Michael
Hi

I have just taught myself some Python and among my resources were...
http://www.ibiblio.org/obp/thinkCSpy/   - I worked my way through over 
half of this online and downloadable manual
http://www.livewires.org.uk/python/index.html - This course is good, 
I am currently working through this but it is a good idea to have a bit 
of an idea about Python first, hence the previous site.
http://www.freenetpages.co.uk/hp/alan.gauld/   - This looks good, I had 
been meaning to get to it but there are only so many hours...

After that there are heaps of sites with tutorials and downloadable 
programmes for all sorts of specific uses of Python, just do a search.

Michael

chinni wrote:
> Hi All,
>
> I am new to python.i need some help about python.i want to learn 
> python so plz guide me from where can i start.so,that i can learn and 
> under stand quickly.
>
> -- 
> Best Regards,
> M.Srikanth Kumar,
>
> 
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>   

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


[Tutor] validation

2007-08-27 Thread Michael
Hi

I am fairly new to Python and I wish to validate input. Such as wanting 
to check and make sure that an integer is entered and the program not 
crashing when a naughty user enters a character instead. I have been 
trying to use the Type() function but I cannot work out how to check the 
return value? Caomparing it to 'int' or 'str' isn't working, or should I 
be using the isinstance property? I want to use raw_input and check that 
it is a number. Can someone point me in the right direction?

Thanks

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


Re: [Tutor] validation

2007-08-27 Thread Shantanoo Mahajan

On 27-Aug-07, at 2:20 PM, Michael wrote:

> Hi
>
> I am fairly new to Python and I wish to validate input. Such as  
> wanting
> to check and make sure that an integer is entered and the program not
> crashing when a naughty user enters a character instead. I have been
> trying to use the Type() function but I cannot work out how to  
> check the
> return value? Caomparing it to 'int' or 'str' isn't working, or  
> should I
> be using the isinstance property? I want to use raw_input and check  
> that
> it is a number. Can someone point me in the right direction?
>
> Thanks
>
> Michael

http://docs.python.org/tut/node10.html

Check section 8.3. 'Exception Handling'. You may use the example
without any modifications.

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


Re: [Tutor] validation

2007-08-27 Thread John Fouhy
On 27/08/07, Michael <[EMAIL PROTECTED]> wrote:
> I am fairly new to Python and I wish to validate input. Such as wanting
> to check and make sure that an integer is entered and the program not
> crashing when a naughty user enters a character instead. I have been
> trying to use the Type() function but I cannot work out how to check the
> return value? Caomparing it to 'int' or 'str' isn't working, or should I
> be using the isinstance property? I want to use raw_input and check that
> it is a number. Can someone point me in the right direction?

Hi Michael,

raw_input() will give you a string.  Python does not do automatic type
conversions, like some languages do.  To convert string to int, you
can use int().  e.g.:

  user_input = raw_input()
  num = int(user_input)

If you call int() on something that is not a valid integer string,
this will throw an exception -- TypeError, I think.  So the pythonic
way to check is to catch the exception:

while True:
  user_input = raw_input()
  try:
num = int(user_input)
break
  except TypeError:
print 'Oops, try again!'

HTH!

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


Re: [Tutor] validation

2007-08-27 Thread Alan Gauld

"Michael" <[EMAIL PROTECTED]> wrote

> to check and make sure that an integer is entered and the program 
> not
> crashing when a naughty user enters a character instead.

John F has already pointed you to the use of try/except for this,
however...

> trying to use the Type() function but I cannot work out how to check 
> the
> return value? Caomparing it to 'int' or 'str' isn't working,

The easiest way is to compare to another type:

x = 42
if type(x) == type(int()):

or even

if type(x) == type(2):

Or you can use the types module:

if type(x) == types.IntType

But for your purposes the Python idiom of its 'better to ask
forgiveness than permission' applies.


-- 
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


Re: [Tutor] How to put an event into the Tcl/Tk event queue?

2007-08-27 Thread Dan Knierim
Hello Mr. Gauld,

Your second guess about the scenario is right: I want to automate tests of 
Tcl/Tk GUIs.
I know about the GUI test automation tools like WATSUP, PyWinAuto, winGuiAuto 
etc., and will use one if necessary.
But test automation is usually easier at the lowest possible level for the test 
target (as per your suggestion to test the back-end functions directly).
In this case, my test target is the Tcl/Tk GUI itself.

The Tcl/Tk functions I mentioned (Tcl_QueueEvent etc.) are listed in my copy of 
the Tcl/Tk Man pages (downloadable from www.tcl.tk/man).
I believe they are C or C++ functions.

Thanks for the explanation of what Tkinter does and doesn't cover.
Is there another Python module that does include Python wrappers for Tcl/Tk C 
functions?

My first glance at send() and event_generate() gave me the idea they were for 
other purposes.  
Your suggestion triggered a second glance; maybe event_generate can do what I 
need.
I'll study it some more.  

Thanks for the clues
- Dan K.


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

> > I'd like to simulate user input to TkInter applications from
> > another Python script, by inserting events in the Tcl event queue.
>
> There are two scenarios where I'd expect that to be needed:
> 1) To roboticise an existing app, particularly if you don;t have 
> source code access.
> 2) testing a GUI.
>
> Other than that it's usually easier to go in at the level below the 
> GUI and
> call the underlying commands directly. Is that a possibility here?
>
> > Tcl/Tk has a couple functions for this (Tk_QueueWindowEvent
> > and Tcl_QueueEvent).
>
> These were new to me and indeed don't appear in either of
> my Tcl refrence books (Ousterhout and O'Reilly Nutshell)
> Are they recent additions?
>
> > Is there a Python interface for either of those?  I can't find 
> > any...
>
> Neither can I.
>
> > My second choice would be to use Tcl_CreateEventSource,
> > but I don't see a Python interface for that, either.
>
> Nope, me neither. No references in my books and no
> Python/Tkinter equivalents. In fact my Tcl prompt doesn't
> recognise any of the 3 commands you cite. Are these
> actuially Tcl/Tk interpreter commands or C interface
> functions? Only interpreter commands are reproduced
> in Tkinter.
>
> > I'd rather not work through the actual GUI interface if I can avoid 
> > it.
>
> Why do you need to work through the GUI events?
> Normally the GUI is there to connect humans to the back end code.
> If an app needs access to the back end code it can usually call the
> functions directly using more conventional IPC mechanisms.
>
> There are a couple of higher level methods that might be of use?
>
> send(app, cmd, *args)
>
> event_generate(sequence, option=...)
>
> Dunno if they will help.
>
> -- 
> 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


Re: [Tutor] validation

2007-08-27 Thread Kent Johnson
Alan Gauld wrote:
> "Michael" <[EMAIL PROTECTED]> wrote
>> trying to use the Type() function but I cannot work out how to check 
>> the
>> return value? Caomparing it to 'int' or 'str' isn't working,
> 
> The easiest way is to compare to another type:
> 
> x = 42
> if type(x) == type(int()):
> 
> or even
> 
> if type(x) == type(2):

For the built-in types, since Python 2.2 the familiar name (int, str, 
float, list, dict, set) *is* the type and you can compare to that 
directly, e.g.:

In [13]: type(3)==int
Out[13]: True
In [14]: type([]) == list
Out[14]: True

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


Re: [Tutor] validation

2007-08-27 Thread Alan Gauld

"Kent Johnson" <[EMAIL PROTECTED]> wrote

>> if type(x) == type(int()):
>>
> For the built-in types, since Python 2.2 the familiar name (int, 
> str,
> float, list, dict, set) *is* the type and you can compare to that
> directly, e.g.:
>
> In [13]: type(3)==int
> Out[13]: True

I knew I should be able to use int but I tried to be too clever
and used type(int) which of course returns 'type type'. So I
used the default int() constructor which returns zero...

For some reason I never thought of simply comparing type()
to int... doh!

Thanks Kent,

Alan g. 


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


Re: [Tutor] How to put an event into the Tcl/Tk event queue?

2007-08-27 Thread Alan Gauld
"Dan Knierim" <[EMAIL PROTECTED]> wrote in

> The Tcl/Tk functions I mentioned (Tcl_QueueEvent etc.) are
> listed in my copy of the Tcl/Tk Man pages (downloadable
> from www.tcl.tk/man).
> I believe they are C or C++ functions.

That's what I suspected, the names look like the C functions.

> Is there another Python module that does include Python
> wrappers for Tcl/Tk C functions?

Unfortunately I don't know of anything.

> Your suggestion triggered a second glance; maybe event_generate can 
> do what I need.
> I'll study it some more.

I hope it works, the documentation in the Nutshell book
certainly suggested that, under X windows at least, any
app could send to any other app within the same X
environment...

Alan G 


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


[Tutor] Detecting sequences in lists

2007-08-27 Thread Tino Dai
Hi Everybody,

   Thank you so much for the information on sets. I think that that has
certain uses, but in my case I can't find a way. I have been thinking about
sequences in a list. Let me give you an example:

tset = [ 1,2,4,0,0,1,2,4,4]

What I want to do is detect the 1,2,4 sequence and perhaps how many.

What I have tried is
[1,2,4] in tset

and also

tset.count([1,2,4])

Is there a method or a function that does this in python, or am I left with
DIY?

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


Re: [Tutor] Detecting sequences in lists

2007-08-27 Thread Lutz Horn
Hi,

DIY is easy.

On Mon, 27 Aug 2007 09:20:53 -0400, "Tino Dai" <[EMAIL PROTECTED]> said:
> What I want to do is detect the 1,2,4 sequence and perhaps how many.

>>> tset = [ 1,2,4,0,0,1,2,4,4]
>>> s = [1, 2, 4]
>>> c = 0
>>> for i in range(len(tset) - len(s)):
... if tset[i:i+len(s)] == s:
... c = c + 1
... print "found at %d" % i
... 
found at 0
found at 5
>>> print c
2

Regards
Lutz

-- 


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


Re: [Tutor] Detecting sequences in lists

2007-08-27 Thread Kent Johnson
Tino Dai wrote:
> Hi Everybody,
> 
>Thank you so much for the information on sets. I think that that 
> has certain uses, but in my case I can't find a way. I have been 
> thinking about sequences in a list. Let me give you an example:
> 
> tset = [ 1,2,4,0,0,1,2,4,4]
> 
> What I want to do is detect the 1,2,4 sequence and perhaps how many.

There is nothing built-in for this. Here is a solution that uses index() 
to quickly find potential starting points. It is based on this post to 
c.l.py and the followup:
http://groups.google.com/group/comp.lang.python/msg/a03abee619ec54ef?hl=en&;

This is likely to be much faster than Lutz' solution tset is long, 
though you would have to test to be sure.

def subPositions(alist, innerlist):
 if innerlist == []:
 return
 first, start = innerlist[0], 0
 while 1:
 try:
 p = alist.index(first, start)
 except ValueError:
 return
 if alist[p: p + len(innerlist)] == innerlist:
 yield p
 start = p+1

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


[Tutor] Equivalent of && in Python?

2007-08-27 Thread wormwood_3
I have a script that reads some local system information, performs some 
calculations, and then launches some terminal windows:

# Spawn the 4 terminals, with needed positions and sizes, then exit
commands.getoutput("%s --geometry=%dx%d+%d+%d --working-directory=%s" % \
(terminal, t1width, t1height, t1posx, t1posy, workingdir))
commands.getoutput("%s --geometry=%dx%d+%d+%d --working-directory=%s" % \
(terminal, t2width, t2height, t2posx, t2posy, workingdir))
commands.getoutput("%s --geometry=%dx%d+%d+%d --working-directory=%s" % \
(terminal, t3width, t3height, t3posx, t3posy, workingdir))
commands.getoutput("%s --geometry=%dx%d+%d+%d --working-directory=%s" % \
(terminal, t4width, t4height, t4posx, t4posy, workingdir))


The oddity: When I call this script, sometimes all four terminals launch, one 
right after another, which is the desired behaviour. At other times, one will 
launch, and ONLY after I close it will the second launch, and so on until the 
fourth. I do not understand how this is happening. I thought each line in a 
script which does anything has to be done before the next one is executed, but 
I may be way off on this. 

If this were in a bash script, I could add " &&" after each line, but what to 
do in a Python script?

-Sam


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


Re: [Tutor] Equivalent of && in Python?

2007-08-27 Thread Eric Brunson
wormwood_3 wrote:
> I have a script that reads some local system information, performs some 
> calculations, and then launches some terminal windows:
>
> # Spawn the 4 terminals, with needed positions and sizes, then exit
> commands.getoutput("%s --geometry=%dx%d+%d+%d --working-directory=%s" % \
> (terminal, t1width, t1height, t1posx, t1posy, workingdir))
> commands.getoutput("%s --geometry=%dx%d+%d+%d --working-directory=%s" % \
> (terminal, t2width, t2height, t2posx, t2posy, workingdir))
> commands.getoutput("%s --geometry=%dx%d+%d+%d --working-directory=%s" % \
> (terminal, t3width, t3height, t3posx, t3posy, workingdir))
> commands.getoutput("%s --geometry=%dx%d+%d+%d --working-directory=%s" % \
> (terminal, t4width, t4height, t4posx, t4posy, workingdir))
>
>
> The oddity: When I call this script, sometimes all four terminals launch, one 
> right after another, which is the desired behaviour. 

That is what's actually odd.  Just reading the script without being well 
versed in the intricacies of the command module, I would expect them to 
be run sequentially.

> At other times, one will launch, and ONLY after I close it will the second 
> launch, and so on until the fourth. I do not understand how this is 
> happening. I thought each line in a script which does anything has to be done 
> before the next one is executed, but I may be way off on this. 
>
> If this were in a bash script, I could add " &&" after each line, but what to 
> do in a Python script?
>   

Actually, I think you mean a single ampersand.  "&&" is condition 
execution of the next command and is done synchronously.

I would say try putting an ampersand after the command in getoutput(), 
have you tried that?  If that doesn't work, then make sure getoutput() 
is using a shell, since the shell is the mechanism through which the 
ampersand works, or else read the docs for getoutput() to see if there's 
some way to get it in the background.  If none of those solutions work, 
you could spawn a thread for each command, or finally, use the 
subprocess module, which I know can be instructed to use a subshell.

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

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


[Tutor] user in put

2007-08-27 Thread Latasha Marks
Need help get a user to in put his or her favortie food the the program should 
the n print oue the name of the new food by joining the original food names 
together
   
  code:
  favortie_food= raw_input("What is your favortie food?")
What is your favortie food?   hot dog
>>> print favortie_food
   hot dog
>>> favortie_food= raw_input("What is your favortie food?")
What is your favortie food? pizza
>>> print  favortie_food.replace("hot dog", "pizza)

SyntaxError: EOL while scanning single-quoted string
>>> print  favortie_food.replace("hot dog", "pizza")
 pizza

   
-
Yahoo! oneSearch: Finally,  mobile search that gives answers, not web links. ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] validation

2007-08-27 Thread Terry Carroll
On Mon, 27 Aug 2007, Kent Johnson wrote:

> For the built-in types, since Python 2.2 the familiar name (int, str, 
> float, list, dict, set) *is* the type and you can compare to that 
> directly, e.g.:
> 
> In [13]: type(3)==int
> Out[13]: True
> In [14]: type([]) == list
> Out[14]: True

That is so cool.  I never knew that.

Last night, I coded a routine that could accept either 1) a string, or 2) 
a list or tuple of such strings or 3) a list or tuple of lists or tuples 
of such strings.

I ended up writing a short isListOrTuple function that went something 
like this:

def isListOrTuple(thing):
  result = False
  if isinstance(thing, list): result = True
  if isinstance(thing, tuple): result = True
  return result

Then I used 

  if isListOrTuple(param):
 stuff
  else:
 other stuff

How much cleanar it would have been to just write:

  if type(param) in [list, tuple]:
 stuff
  else:
 other stuff

Thanks, Kent.

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


Re: [Tutor] validation

2007-08-27 Thread Kent Johnson
Terry Carroll wrote:

> I ended up writing a short isListOrTuple function that went something 
> like this:
> 
> def isListOrTuple(thing):
>   result = False
>   if isinstance(thing, list): result = True
>   if isinstance(thing, tuple): result = True
>   return result

isinstance can take a tuple of types as its second argument so this 
could be written

def isListOrTuple(thing):
   return isinstance(thing, (list, tuple))

> How much cleanar it would have been to just write:
> 
>   if type(param) in [list, tuple]:
>  stuff
>   else:
>  other stuff

Note that
   isinstance(thing, (list, tuple))

and
   type(thing) in [list, tuple]

are not equivalent. The first will be true for objects whose type is a 
subclass of list and tuple while the second will not.

In [2]: class Foo(list): pass
...:
In [3]: f=Foo()
In [4]: type(f)
Out[4]: 
In [5]: type(f) in [list, tuple]
Out[5]: False
In [6]: isinstance(f, list)
Out[6]: True

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


[Tutor] Question about installing 2.51

2007-08-27 Thread Dick Moores
XP, Python 2.5

I just downloaded python-2.5.1.msi from python.org. During the 
installation process (which I aborted), I was told, "This Update will 
replace your existing Python25 installation".

What exactly does this mean? What will happen, for example, to all my 
scripts that are in E:\Python25\dev? Or to the packages I've put into 
E:\Python25\Lib\site-packages?

Thanks,

Dick Moores

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


Re: [Tutor] Question about installing 2.51

2007-08-27 Thread Kent Johnson
Dick Moores wrote:
> XP, Python 2.5
> 
> I just downloaded python-2.5.1.msi from python.org. During the 
> installation process (which I aborted), I was told, "This Update will 
> replace your existing Python25 installation".
> 
> What exactly does this mean? What will happen, for example, to all my 
> scripts that are in E:\Python25\dev? Or to the packages I've put into 
> E:\Python25\Lib\site-packages?

In general minor releases (x.y.z) install over any existing x.y.w 
release preserving stuff you have installed into x.y.z. In particular 
site-packages will be preserved. Not sure about \dev since AFAIK that is 
not part of the standard distribution.

You could just copy E:\Python25 to be safe before you do the install.

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


Re: [Tutor] user in put

2007-08-27 Thread Dave Kuhlman
On Mon, Aug 27, 2007 at 11:02:59AM -0700, Latasha Marks wrote:

> Need help get a user to in put his or her favortie food the the
> program should the n print oue the name of the new food by
> joining the original food names together

If what you want is to enable your user to enter several foods
(strings), then concatenate them together, try something like the
following:

In [33]: foods = []
In [34]: food = raw_input('What is your favorite food?')
What is your favorite food?peaches
In [35]: foods.append(food)
In [36]: food = raw_input('What is your favorite food?')
What is your favorite food?nectarines
In [37]: foods.append(food)
In [38]: food = raw_input('What is your favorite food?')
What is your favorite food?cantaloupe
In [39]: foods.append(food)
In [40]: ', '.join(foods)
Out[40]: 'peaches, nectarines, cantaloupe'

Note that we append each food to a list, then do string.join(). 
That's faster than doing multiple string concatenations.  In this
case there are not enough strings to make a difference.  But, it's
a good habit to get into.

Dave

-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about installing 2.51

2007-08-27 Thread Alan Gauld

"Dick Moores" <[EMAIL PROTECTED]> wrote

> installation process (which I aborted), I was told, "This Update 
> will
> replace your existing Python25 installation".
>
> What exactly does this mean? What will happen, for example, to all 
> my
> scripts that are in E:\Python25\dev? Or to the packages I've put 
> into
> E:\Python25\Lib\site-packages?

Probably all will be well. However I generally think its a bad idea to 
keep
your scripts inside the Python folder structure since it makes it 
harder
to share them over multiple Python versions and of course there is the
small  risk that an upgrade like this might destroy them (if for 
example
Python acquired a dev sub folder structure!)

I always create a separate folder structure for my python code and 
point
python at it using the PYTHONPATH env variable (or sys.path).

But that may not be a generally held view...

Alan G. 


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


Re: [Tutor] validation

2007-08-27 Thread Terry Carroll
On Mon, 27 Aug 2007, Kent Johnson wrote:

> isinstance can take a tuple of types as its second argument
> 
> Note that
>isinstance(thing, (list, tuple))
> 
> and
>type(thing) in [list, tuple]
> 
> are not equivalent. The first will be true for objects whose type is a 
> subclass of list and tuple while the second will not.

Thanks.  I totally missed that isinstance's second parameter can be a 
tuple.  That's a much better approach.

Wow, that makes two things I've learned today.  My brain is now full.


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


Re: [Tutor] Question about installing 2.51

2007-08-27 Thread Dick Moores
At 02:00 PM 8/27/2007, Alan Gauld wrote:

>"Dick Moores" <[EMAIL PROTECTED]> wrote
>
> > installation process (which I aborted), I was told, "This Update
> > will
> > replace your existing Python25 installation".
> >
> > What exactly does this mean? What will happen, for example, to all
> > my
> > scripts that are in E:\Python25\dev? Or to the packages I've put
> > into
> > E:\Python25\Lib\site-packages?
>
>Probably all will be well. However I generally think its a bad idea to
>keep
>your scripts inside the Python folder structure since it makes it
>harder
>to share them over multiple Python versions and of course there is the
>small  risk that an upgrade like this might destroy them (if for
>example
>Python acquired a dev sub folder structure!)
>
>I always create a separate folder structure for my python code and
>point
>python at it using the PYTHONPATH env variable (or sys.path).
>
>But that may not be a generally held view...

Thanks very much, Alan and Kent. I took your advice. Backed up 
Python25, and created a PythonWork folder outside of Python25, for my 
stuff. Don't know why I didn't do the latter long before! The 
installation went fine.

Dick


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


[Tutor] tagging pieces of information

2007-08-27 Thread Che M
Hi, I am curious about ways in Python to approach the idea of "tagging" 
pieces of information much in the way that one can tag favorite websites 
like on the site Del.icio.us.  I'm not sure if tagging is the best term for 
this (due to confusion with HTML tags), but the idea would be a way to 
assign one or more words to stored data such that later one might search by 
those words in order to retrieve the data.  That data might be a chunk of 
text, a graph, image, whatever...the point would be to be able to search 
later by tags name.  I know the prorgram GyrFalcon uses tags and is written 
in Python.  And of course Flickr and many other things.

I don't know if there are any preexisting Python structures which would help 
with this or if it has to be done by scratch, or if it is easy or difficult. 
  I also don't know what are good ideas for ways to save the tags, whether 
in a text file, in a database (if so, comma separated in one cell?), or some 
other means, and how to associate them with the data chunk they refer to, 
and lastly how to search for them.

Some starting points in the right direction, jargon words to search for, 
etc., would be very helpful.  Thanks.

_
Learn.Laugh.Share. Reallivemoms is right place! 
http://www.reallivemoms.com?ocid=TXT_TAGHM&loc=us

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


Re: [Tutor] tagging pieces of information

2007-08-27 Thread John Fouhy
On 28/08/07, Che M <[EMAIL PROTECTED]> wrote:
> Hi, I am curious about ways in Python to approach the idea of "tagging"
> pieces of information much in the way that one can tag favorite websites
> like on the site Del.icio.us.  I'm not sure if tagging is the best term for
> this (due to confusion with HTML tags), but the idea would be a way to
> assign one or more words to stored data such that later one might search by
> those words in order to retrieve the data.  That data might be a chunk of
> text, a graph, image, whatever...the point would be to be able to search
> later by tags name.  I know the prorgram GyrFalcon uses tags and is written
> in Python.  And of course Flickr and many other things.

A simple way to do this in-memory would be to use a dict: keys are
tags and values are sets (or lists) of objects.  You might need to
maintain an inverse structure too, mapping object to list/set of tags.

You could use a database (sqlite comes with python 2.5).  I'm not sure
what the "best practice" strucutre would be, but maybe you could have
a table with two columns: "object ID" and "tag".  "object ID" would be
some kind of identifier for your tagged objects.  You could then:

Find tags for an object:
 select tag from tagTable where objectID = ?

Find objects matching a tag:
 select objectID from tagTable where tag = ?

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


Re: [Tutor] tagging pieces of information

2007-08-27 Thread Ian Witham
You may be able to use a dictionary to store your data chunks. eg:

>>> tagged_items = {('spam, swordfights'): 'ITEM A',
... ('swordfights', 'custard'): 'ITEM B'}
>>> [tagged_items[tags] for tags in tagged_items if 'spam' in tags]
['ITEM A']
>>> [tagged_items[tags] for tags in tagged_items if 'swordfights' in tags]
['ITEM A', 'ITEM B']

or..
>>> for tags in tagged_items:
... if 'custard' in tags:
... print tagged_items[tags]
...
ITEM B

Ian.

On 8/28/07, Che M <[EMAIL PROTECTED]> wrote:
>
> Hi, I am curious about ways in Python to approach the idea of "tagging"
> pieces of information much in the way that one can tag favorite websites
> like on the site Del.icio.us.  I'm not sure if tagging is the best term
> for
> this (due to confusion with HTML tags), but the idea would be a way to
> assign one or more words to stored data such that later one might search
> by
> those words in order to retrieve the data.  That data might be a chunk of
> text, a graph, image, whatever...the point would be to be able to search
> later by tags name.  I know the prorgram GyrFalcon uses tags and is
> written
> in Python.  And of course Flickr and many other things.
>
> I don't know if there are any preexisting Python structures which would
> help
> with this or if it has to be done by scratch, or if it is easy or
> difficult.
>   I also don't know what are good ideas for ways to save the tags, whether
> in a text file, in a database (if so, comma separated in one cell?), or
> some
> other means, and how to associate them with the data chunk they refer to,
> and lastly how to search for them.
>
> Some starting points in the right direction, jargon words to search for,
> etc., would be very helpful.  Thanks.
>
> _
> Learn.Laugh.Share. Reallivemoms is right place!
> http://www.reallivemoms.com?ocid=TXT_TAGHM&loc=us
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] tagging pieces of information

2007-08-27 Thread Eric Abrahamsen
On Aug 28, 2007, at 11:07 AM, Che M wrote:

> I don't know if there are any preexisting Python structures which  
> would help
> with this or if it has to be done by scratch, or if it is easy or  
> difficult.
>   I also don't know what are good ideas for ways to save the tags,  
> whether
> in a text file, in a database (if so, comma separated in one  
> cell?), or some
> other means, and how to associate them with the data chunk they  
> refer to,
> and lastly how to search for them.

My first thought would be to create a class for your 'data chunks',  
and then make 'tags' a class attribute that is created empty on  
initialization. The basic code would look like:

class Data_chunk(object):
 def __init__(self):
 self.tags = set()

Then every time you create a new data chunk like so:

my_data = Data_chunk()

You can add, remove and search for tags using the set methods (I made  
it a set because that seemed appropriate to a tagging feature, you  
could use a list or something else):

my_data.tags.add('dogs')
my_data.tags.add('cats')
if 'dogs' in my_data.tags:
print "It's about dogs"
my_data.tags.remove('dogs')
print my_data.tags

The pickle module is usually simplest and most convenient for long- 
term data storage.

Enjoy!

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