[Tutor] Unit-testing advice

2010-03-08 Thread mhw
Dear All,

Quick UT question. I am working on a CSV processing script. I want to write 
some tests. The tests will need to read in, manipulate and write out some CSV 
files. 

My instinct is to manually construct some small data files, and consider them 
as part of the test suite. The other option would be to programatically 
construct them (? As a set-up).

Which is better? 

Thanks,

Matt


Sent from my BlackBerry® wireless device
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unit-testing advice

2010-03-08 Thread C.T. Matsumoto

m...@doctors.net.uk wrote:

Dear All,

Quick UT question. I am working on a CSV processing script. I want to write some tests. The tests will need to read in, manipulate and write out some CSV files. 


My instinct is to manually construct some small data files, and consider them 
as part of the test suite. The other option would be to programatically 
construct them (? As a set-up).

Which is better? 


Thanks,

Matt


Sent from my BlackBerry® wireless device
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


I guess this depends on what part of your program you are testing.

If you are testing small 'nuts and bolts' part of the script I usually 
have the test data written into the test as global variables.


As your functionality becomes more complete, you probably need to switch 
to importing a test data file.


I agree I'd manually construct some data files. But if you want to test 
directory/file creation you could programaticaly create a test directory 
in /tmp/ to test on.


T
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Passing nested structures to fcntl.ioctl

2010-03-08 Thread spir
On Mon, 8 Mar 2010 11:57:35 +0530 (IST)
"Noufal Ibrahim"  wrote:

> Hello everyone,
> I have some code that's calling fcntl.ioctl. I need to pass a nested
> structure as the 3rd argument of the function. Something like this
> 
> typedef struct coordinates{
>   int x;
>   int y;
> } coordinates;
> 
> typedef struct point{
>   int amp;
>   coordinates* coord;
> } point;
> 
>How would I do this? I need to allocate a point structure and then a
> coordinate structure and link them up together via the coord member of
> point, fill them up with values and pass it to the fcntl.ioctl. The
> actual IOCTL implementation will do the indirection to read out the
> data.
> 
>I can't use ctypes since I'm working on an embedded system with an ARM
> core that doesn't have ctypes. The struct module doesn't seem to help
> due to the indirection.
> 
>Any ideas? I'm personally planning to write a small C extension that
> creates the structure based on a specification in memory and then
> passes that to the IOCTL call directly but that looks like some work. I
> was also told to take a look at Cython which basically seems to be an
> easy way of doing what I'm planning. Is there a quicker/easier way?
> 
> Thanks.

If your point is to implement a type (point) in C, for performance certainly, 
and reuse it from a dynamic language, then I'm not sure python is the best 
choice (*). Python programmers code in python e basta. C-python bindings are 
far from beeing simplistic.
http://wiki.cython.org/WrappingCorCpp


Denis


(*) Lua instead is well-known for this kind of stuff; especially on particuliar 
hardware like ARM. Lua's own implementation is 100% plain ANSI C standard and 
Lua-C bindings are probably as straightforward as they can be.
http://lua-users.org/wiki/BindingCodeToLua
-- 


la vita e estrany

spir.wikidot.com

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Read XML records one by one

2010-03-08 Thread Hichiro
-- 
Best regards,
Vinh NV
CNPM K50 DHBKHN
Y!: Vinh.dhbk
Sky : Vinh.dhbk
84 976 314 988
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Read XML records one by one

2010-03-08 Thread Hichiro
Hi all!
I'm trying to read one by one record in XML file to find out its tag and
attribute for schema matching. But I haven't done yet. So, could you help
me?!
Thanks so much! :)



-- 
Best regards,
Vinh NV
CNPM K50 DHBKHN
Y!: Vinh.dhbk
Sky : Vinh.dhbk
84 976 314 988
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Printing time without "if" statement

2010-03-08 Thread spir
On Mon, 8 Mar 2010 18:03:12 +1100
Steven D'Aprano  wrote:

> On Mon, 8 Mar 2010 03:38:49 pm Elisha Rosensweig wrote:
> > Hi,
> >
> > I have an event-based simulator written in Python (of course). It
> > takes a while to run, and I want to have messages printed every so
> > often to the screen, indicating the simulation time that has passed.
> > Currently, every event that occurs follows the following code
> > snippet:
> [...]
> > This seems stupid to me - why should I have to check each round if
> > the time has been passed? I was thinking that there might be a
> > simpler way, using threading, but I have zero experience with
> > threading, so need your help. The idea was to have a simple thread
> > that waited X time (CPU cycles, say) and then read the "current_time"
> > variable and printed it.
> >
> > Any simple (simpler?) solutions to this?
> 
> That's a brilliant idea. I think I will steal it for my own code :)
> 
> (Why didn't I think of it myself? Probably because I almost never use 
> threads...)
> 
> Anyway, here's something that should help:
> 
> 
> import time
> import threading
> 
> class Clock(threading.Thread):
> def __init__(self, *args, **kwargs):
> super(Clock, self).__init__(*args, **kwargs)
> self.finished = False
> def start(self):
> print "Clock %s started at %s" % (self.getName(), time.ctime())
> super(Clock, self).start()
> def run(self):
> while 1:
> if self.finished:
> break
> print "Clock %s still alive at %s" % (
> self.getName(), time.ctime())
> time.sleep(2)
> print "Clock %s quit at %s" % (self.getName(), time.ctime())
> def quit(self):
> print "Clock %s asked to quit at %s" % (
> self.getName(), time.ctime())
> self.finished = True
> 
> def do_work():
> clock = Clock(name="clock-1")
> clock.start()
> # Start processing something hard.
> for i in xrange(8):
> print "Processing %d..." % i
> # Simulate real work with a sleep.
> time.sleep(0.75)
> clock.quit()
> 
> 
> 
> And in action:
> 
> >>> do_work()
> Clock clock-1 started at Mon Mar  8 17:40:42 2010
> Processing 0...
> Clock clock-1 still alive at Mon Mar  8 17:40:43 2010
> Processing 1...
> Processing 2...
> Clock clock-1 still alive at Mon Mar  8 17:40:45 2010
> Processing 3...
> Processing 4...
> Processing 5...
> Clock clock-1 still alive at Mon Mar  8 17:40:47 2010
> Processing 6...
> Processing 7...
> Clock clock-1 still alive at Mon Mar  8 17:40:49 2010
> Clock clock-1 asked to quit at Mon Mar  8 17:40:49 2010
> >>> Clock clock-1 quit at Mon Mar  8 17:40:51 2010
> 
> >>>
> 
> 
> There's a bit of a display artifact in the interactive interpreter, when 
> the final quit message is printed: the interpreter doesn't notice it 
> needs to redraw the prompt. But other than that, it should be fine.

Hello,

really interesting, indeed. I also have no idea about threading in python. But 
as timer is concerned, I would prefere something like:

import time
import whatever_to_ensure_run_in_separate_thread
class Timer(whatever_to_ensure_run_in_separate_thread):
UNIT = 1/100# µs
def __init__(self, delay, action, cyclic=False):
self.delay = delay
self.action = action
self.cyclic = cyclic
self.goon = True
def run(self):
if self.cyclic:
while self.goon:
self.cycle()
else:
self.cycle
def cycle(self):
self.wait()
self.action()
def wait(self):
delay.sleep(self.delay * self.UNIT)

Or even better, launch a signal in main thread -- which is supposed to be an 
event cycle. This is how things work in the field of automation:

class Timer(whatever_to_ensure_run_in_separate_thread):
UNIT = 1/100# µs
def __init__(self, delay, signal, resetDelay=1, cyclic=False):
self.delay = delay
self.signal = signal
self.resetDelay = resetDelay
self.cyclic = cyclic
self.goon = True
def run(self):
if self.cyclic:
while self.goon:
self.cycle()
else:
self.cycle
def cycle(self):
self.wait(self.delay)
self.signal = True
self.wait(self.resetDelay)
self.signal = False
def wait(self, delay):
delay.sleep(delay * self.UNIT)

# main cycle:
t1 = Timer(...)
...
if t1_signal:
do_t1_action

(Actually, in python, the signal may be the index of a Timer.signals boolean 
array, incremented with each new instance.)


Denis
-- 


la vita e estrany

spir

[Tutor] Communicate between a thread and the main program

2010-03-08 Thread Plato P.B.
Hi all,
I have created a script in which i need to implement the communication
between the main program and a thread.
The thread looks for any newly created files in a particular directory. It
will be stored in a variable in the thread function. I want to get that name
from the main program.
How can i do it?

Thanks in Advance. :D
-- 






Rgds..
Plato P.B.
obscurant1st.biz/blog

B'Lore  ( +919844882641)
TsR I.( +919995317984)
TsR II.   ( +919037036661)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Read XML records one by one

2010-03-08 Thread Stefan Behnel

Hichiro, 08.03.2010 10:48:

I'm trying to read one by one record in XML file to find out its tag and
attribute for schema matching. But I haven't done yet. So, could you help
me?!


You were not very specific about your data, neither did you provide enough 
information about your use case to understand what you want to achieve.


Therefore, I can't give you a real solution. Here's one you can adapt:

import xml.etree.ElementTree as ET

for _, element in ET.iterparse("the_file.xml"):
 print element.tag, element.attrib

Stefan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] WSGI / Apache

2010-03-08 Thread Benno Lang
On 5 March 2010 04:37, Giorgio  wrote:
> Hi,
>
> as you all probably know i'm using the Google App Engine platform for my
> python code.
>
> As I learn python i try to understand more and more how GAE works. Today
> i've noticed that all applications on GAE are running on WSGI. A quick
> Google search told me that WSGI is a new standard for web applications,
> developed by python.
>
> So, I decided to try it. With Apache.
>
> I found that Apache has a dedicated mod, mod_wsgi to handle this type of
> requests. But, as I always try to be "platform/daemon indipended" i've
> looked for other solutions. I found WSGIREF that as wsgi.org states
> "includes a threaded HTTP server, a CGI server (for running any WSGI
> application as a CGI script)".
>
> So it seems that wsgiref, that is actually included in python, can "convert"
> my apps to work with a standard CGI interface (in other words, working with
> EVERY server that supports CGI not only with apache and his dedicated mod).
> Can you confirm this?

WSGI is based on CGI, so I imagine that it's not too difficult to have
a wrapper that converts between the protocols. I haven't tried
wsgiref, though.

> Do you have any idea i should use mod_wsgi instead of wsgiref + mod_cgi?

>From your application's perspective, if it talks WSGI it shouldn't
make any difference. But if you're using GAE, what good will it do to
have a local Apache server anyway?

Cheers,
benno
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Communicate between a thread and the main program

2010-03-08 Thread Dave Angel

Plato P.B. wrote:

Hi all,
I have created a script in which i need to implement the communication
between the main program and a thread.
The thread looks for any newly created files in a particular directory. It
will be stored in a variable in the thread function. I want to get that name
from the main program.
How can i do it?

Thanks in Advance. :D
  
Don't store it in "a variable in the thread function," but in an 
instance attribute of the thread object.


Then the main program simply checks the object's attribute.  Since it 
launched the thread(s), it should know its (their) instances.  This way, 
the solution scales up as you add more threads, with different 
functionality.


DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] variable inheritance

2010-03-08 Thread spir
Hello,

Say I have a Tree type that may be based on 2 kinds of Node-s. Both 
conceptually and practically, a tree itself is a node, namely the top/root one. 
But a tree also has some additional tree-level behaviour, which is independant 
of the kind of node. Say, the node type performs all the underlying tree 
mechanics. Thus the Tree type looks like:

class Tree(_Node):
def __init__(self, args):
_Node.__init__(self)
...
... tree-level methods ...

The issue is the actual kind of node is not only initialised through 
"_Node.__init__(self)": it must first feature as super class in "class 
Tree(Node)". (Otherwise I get an error about wrong type of self.)
I tried to play with __new__ in Tree and Node types, but this error seems 
unavoidable. Maybe I did not do it right. I can indeed have __new__ return an 
instance of either Node type, but then for any reason it "forgets" it is also a 
tree (so that calling any tree method fails).
I there a way to do this right?

Workaround ideas:
* Make the root node an attribute of tree: unsatisfying.
* Create a common tree type and 2 specialised ones: 
class TreeN(Tree,NodeN)
def __init__(self, args):
_NodeN.__init__(self)
Tree.__init__(self, args)
# ... e basta ...


Denis
-- 


la vita e estrany

spir.wikidot.com

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Really learn programming

2010-03-08 Thread Wayne Werner
As I was reading my favorite webcomics, Abstruse
Goose had
a link to this interesting site about programming in 21 days (or not).

http://norvig.com/21-days.html

I thought it was rather interesting
(and it also recommends Python ;)

enjoy,
-Wayne

-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn’t. - Primo Levi
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Use ZODB or Durus to store Python-objects?

2010-03-08 Thread Karjer Jdfjdf
I'm using pickle more often in my programs and now I store them as files.

I want to store them in ZODB or Durus to prevent losing sight of where I store 
what pickled object.

Which one is the best one to use? 
I've never worked with either one, so I would like to have some opinions. I 
want to achieve the following:

* synchronisation of (ZODB/Durus) data on multiple servers (ie with rsync) to 
prevent data-loss
* which is the most easy to use (for a python-starter)?
* which is the most stabile to use?
* performance with large datasets?
* is it possible to synchronize changes done on objects (ie dictionaries) by 
multiple client-processes?






  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] variable inheritance

2010-03-08 Thread Hugo Arts
On Mon, Mar 8, 2010 at 1:57 PM, spir  wrote:
> Hello,
>
> Say I have a Tree type that may be based on 2 kinds of Node-s. Both 
> conceptually and practically, a tree itself is a node, namely the top/root 
> one. But a tree also has some additional tree-level behaviour, which is 
> independant of the kind of node. Say, the node type performs all the 
> underlying tree mechanics. Thus the Tree type looks like:
>
> class Tree(_Node):
>    def __init__(self, args):
>        _Node.__init__(self)
>        ...
>    ... tree-level methods ...
>
> The issue is the actual kind of node is not only initialised through 
> "_Node.__init__(self)": it must first feature as super class in "class 
> Tree(Node)". (Otherwise I get an error about wrong type of self.)
> I tried to play with __new__ in Tree and Node types, but this error seems 
> unavoidable. Maybe I did not do it right. I can indeed have __new__ return an 
> instance of either Node type, but then for any reason it "forgets" it is also 
> a tree (so that calling any tree method fails).
> I there a way to do this right?
>
> Workaround ideas:
> * Make the root node an attribute of tree: unsatisfying.
> * Create a common tree type and 2 specialised ones:
> class TreeN(Tree,NodeN)
>    def __init__(self, args):
>        _NodeN.__init__(self)
>        Tree.__init__(self, args)
> # ... e basta ...
>

Design-wise, there are two ways of looking at the problem:

* A tree *is* a node, containing some amount of sub-trees/nodes (they
are the same thing)
* A tree is a data structure that has one top-level node, which in
turn contains several child-nodes, which can in turn contain
child-nodes, etc. etc.

In the first case, you really shouldn't make a separate Tree type.
They are the same thing, they should have the same behavior. This also
allows great flexibility with extracting subtrees, and has quite
elegant recursive implementations.

In the second case, the Tree is something separate from it's root
Node, and so making Tree inherit from Node really doesn't make any
sense. Use containment in this case. This case is also well suited to
iterative implementations.

What you're trying to do is a kind of mixture between the two, having
the root Node as a separate type. I would try to avoid it. If you
really must, though, making a Tree class for every node type is your
best bet. Use a class factory to prevent this from getting cumbersome:

def make_treetype(nodetype):
class Tree(nodetype):
def __init__(self):
nodetype.__init__(self)
# etc etc
return Tree

There might also be a metaclass solution, but I'm not an expert on
that. Metaclasses tend to melt my brain.

Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] matching words from a text to keys in a dictionary

2010-03-08 Thread Karjer Jdfjdf
I want to compare words in a text to a dictionary with values attached to the 
words.

The dictionary  looks like:
{ word1: [1,2,3] word2: [2,3,4,a,b ] ... }

I'm trying to find a way to achieve this, but I'm having trouble getting 
corrects results.

If I do the def below, nothing is matched.

def searchWord(text, dictionary):
    text = text.split()
    for word in text:
    print word
    if word in dictionary:
    value = dictionary[str(word)]
    else:
    value = None
    return w

If I try another way, I keep getting errors:

def searchWord(text, dictionary):
    for key in dictionary:
    value = dictionary[key]
    if re.search(key, text):
    w = value
    else:
    w = None
    return w


TypeError: list indices must be integers, not str

I'm probably overlooking something and suspect that I'm doing it the slow way, 
so any advice is welcome. 



  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Making Regular Expressions readable

2010-03-08 Thread Stephen Nelson-Smith
Hi,

I've written this today:

#!/usr/bin/env python
import re

pattern = 
r'(?P^(-|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(,
[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})*){1})
(?P(\S*)) (?P(\S*))
(?P(\[[^\]]+\]))
(?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)
(?P(\S*)) (?P(\S*))
(?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)
(?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)(
)?(?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)'

regex = re.compile(pattern)

lines = 0
no_cookies = 0

for line in open('/home/stephen/scratch/feb-100.txt'):
  lines +=1
  line = line.strip()
  match = regex.match(line)

  if match:
data = match.groupdict()
if data['SiteIntelligenceCookie'] == '':
  no_cookies +=1
  else:
print "Couldn't match ", line

print "I analysed %s lines." % (lines,)
print "There were %s lines with missing Site Intelligence cookies." %
(no_cookies,)

It works fine, but it looks pretty unreadable and unmaintainable to
anyone who hasn't spent all day writing regular expressions.

I remember reading about verbose regular expressions.  Would these help?

How could I make the above more maintainable?

S.

-- 
Stephen Nelson-Smith
Technical Director
Atalanta Systems Ltd
www.atalanta-systems.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] matching words from a text to keys in a dictionary

2010-03-08 Thread Andre Engels
On Mon, Mar 8, 2010 at 5:05 PM, Karjer Jdfjdf  wrote:

>  I want to compare words in a text to a dictionary with values attached to
> the words.
>
> The dictionary  looks like:
> { word1: [1,2,3] word2: [2,3,4,a,b ] ... }
>

Please give the actual dictionary, not something that it 'looks like' - an
actual dictionary would never 'look like' this: it has commas between the
elements, and quotes around anything that is a word.


> I'm trying to find a way to achieve this, but I'm having trouble getting
> corrects results.
>
> If I do the def below, nothing is matched.
>
> def searchWord(text, dictionary):
> text = text.split()
> for word in text:
> print word
> if word in dictionary:
> value = dictionary[str(word)]
> else:
> value = None
> return w
>
> If I try another way, I keep getting errors:
>
> def searchWord(text, dictionary):
> for key in dictionary:
> value = dictionary[key]
> if re.search(key, text):
> w = value
> else:
> w = None
> return w
>
>
> TypeError: list indices must be integers, not str
>

That's quite a clear statement: If this is indeed caused by the function you
show here, then the only explanation is that 'dictionary' is not a
dictionary at all, but a list.


-- 
André Engels, andreeng...@gmail.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: [XML-SIG] Read XML records one by one

2010-03-08 Thread Hichiro
On Mon, Mar 8, 2010 at 5:40 PM, Stefan Behnel  wrote:

> Hichiro, 08.03.2010 10:44:
>
>> I'm trying to read one by one record in XML file to find out its tag and
>>
>> attribute for schema matching. But I haven't done yet. So, could you help
>> me?!
>>
>
> Hi,
>
> since it appears that you cross-posted this to the python-tutor list, I'll
> answer over there.
>
> Stefan
>



-- 
Best regards,
Vinh NV
CNPM K50 DHBKHN
Y!: Vinh.dhbk
Sky : Vinh.dhbk
84 976 314 988
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Read XML records one by one

2010-03-08 Thread Glen Zangirolami
Another alternative to parsing XML is beautiful soup.

Website:
http://www.crummy.com/software/BeautifulSoup/

Documentation for parsing xml:
http://www.crummy.com/software/BeautifulSoup/documentation.html#Parsing%20XML

sample code:
from BeautifulSoup import BeautifulStoneSoup
xml = "Contents 1Contents 2Contents 3"
soup = BeautifulStoneSoup(xml)
print soup.prettify()
# 
#  
#   Contents 1
#   
#Contents 2
#   
#  
#  
#   Contents 3
#  
# 



On Mon, Mar 8, 2010 at 3:48 AM, Hichiro  wrote:

>
> Hi all!
> I'm trying to read one by one record in XML file to find out its tag and
> attribute for schema matching. But I haven't done yet. So, could you help
> me?!
> Thanks so much! :)
>
>
>
>
> --
> Best regards,
> Vinh NV
> CNPM K50 DHBKHN
> Y!: Vinh.dhbk
> Sky : Vinh.dhbk
> 84 976 314 988
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Communicate between a thread and the main program

2010-03-08 Thread Glen Zangirolami
I think you can use Queue to communicate between threads.

"The Queue module implements multi-producer, multi-consumer queues. It is
especially useful in threaded programming when information must be exchanged
safely between multiple threads"

http://docs.python.org/library/queue.html


On Mon, Mar 8, 2010 at 4:43 AM, Plato P.B.  wrote:

>
> Hi all,
> I have created a script in which i need to implement the communication
> between the main program and a thread.
> The thread looks for any newly created files in a particular directory. It
> will be stored in a variable in the thread function. I want to get that name
> from the main program.
> How can i do it?
>
> Thanks in Advance. :D
> --
>
>
>
>
>
>
> Rgds..
> Plato P.B.
> obscurant1st.biz/blog
>
> B'Lore  ( +919844882641)
> TsR I.( +919995317984)
> TsR II.   ( +919037036661)
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] matching words from a text to keys in a dictionary

2010-03-08 Thread spir
On Mon, 8 Mar 2010 08:05:54 -0800 (PST)
Karjer Jdfjdf  wrote:

> I want to compare words in a text to a dictionary with values attached to the 
> words.
> 
> The dictionary  looks like:
> { word1: [1,2,3] word2: [2,3,4,a,b ] ... }

And how does your source text look like? (we have half of the data)
And please print (part of) your dict out (instead of providing erroneous data).

> I'm trying to find a way to achieve this, but I'm having trouble getting 
> corrects results.
> 
> If I do the def below, nothing is matched.
> 
> def searchWord(text, dictionary):
>     text = text.split()
>     for word in text:
>     print word

print word, word in dictionary  # would be useful for debug

>     if word in dictionary:
>     value = dictionary[str(word)]

word is already a string -- or should be ;-)

>     else:
>     value = None
>     return w

What is w? This is your main issue, I guess...

[...]


Denis
-- 


la vita e estrany

spir.wikidot.com

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] matching words from a text to keys in a dictionary

2010-03-08 Thread Karjer Jdfjdf
>>  I want to compare words in a text to a dictionary with values attached to
>> the words.
>>
>> The dictionary  looks like:
>> { word1: [1,2,3] word2: [2,3,4,a,b ] ... }
>>
>
>Please give the actual dictionary, not something that it 'looks like' - an
>actual dictionary would never 'look like' this: it has commas between the
>elements, and quotes around anything that is a word.
>

Sorry, I was a bit quick typing it, forgetting the proper format. The 
dictionary looks something like this (simplified):

{ 'chinese': ['china', '17', '3'], 'vietnamese': ['vietnam', '89'] ... }



A text might be something like this and should get 2 matches:
 'A vietnamese farmer ate a chinese noodle soup, but it was not made in china'


>
>> I'm trying to find a way to achieve this, but I'm having trouble getting
>> corrects results.
>>
>> If I do the def below, nothing is matched.
>>
>> def searchWord(text, dictionary):
>> text = text.split()
>> for word in text:
>> print word
>> if word in dictionary:
>> value = dictionary[str(word)]
>> else:
>> value = None
>> return w
>>
>> If I try another way, I keep getting errors:
>>
>> def searchWord(text, dictionary):
>> for key in dictionary:
>> value = dictionary[key]
>> if re.search(key, text):
>> w = value
>> else:
>> w = None
>> return w
>>
>>
>> TypeError: list indices must be integers, not str
>>
>
>That's quite a clear statement: If this is indeed caused by the function you
>show here, then the only explanation is that 'dictionary' is not a
>dictionary at all, but a list.

Actually the values are lists.



  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Resetting the namespace

2010-03-08 Thread Patrick Sabin
I found this piece of code, which completely puzzles me, in the pdb 
module of the trunk:


class Pdb(bdb.Bdb, cmd.Cmd):
...
def _runscript(self, filename):
# The script has to run in __main__ namespace (or imports from
# __main__ will break).
#
# So we clear up the __main__ and set several special variables
# (this gets rid of pdb's globals and cleans old variables on
# restarts).
import __main__
__main__.__dict__.clear()
__main__.__dict__.update({"__name__": "__main__",
  "__file__": filename,
  "__builtins__": __builtins__,
 })

The intention of this code is to reset the namespace, before executing a 
script.


When I try to do something similar, i.e. __main__.__dict__.clear(), I 
loose the __builtins__ variable in the namespace, e.g.:


>>> import __main__
>>> print __builtins__

>>> __main__.__dict__.clear()
>>> print __builtins__
Traceback (most recent call last):
  File "", line 1, in 
NameError: name '__builtins__' is not defined

But for some reason the code mentioned above actually works in the case 
of pdb. Any ideas why?


- Patrick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Making Regular Expressions readable

2010-03-08 Thread spir
On Mon, 8 Mar 2010 16:12:35 +
Stephen Nelson-Smith  wrote:

> Hi,
> 
> I've written this today:
> 
> #!/usr/bin/env python
> import re
> 
> pattern = 
> r'(?P^(-|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(,
> [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})*){1})
> (?P(\S*)) (?P(\S*))
> (?P(\[[^\]]+\]))
> (?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)
> (?P(\S*)) (?P(\S*))
> (?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)
> (?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)(
> )?(?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)'
[...]
> It works fine, but it looks pretty unreadable and unmaintainable to
> anyone who hasn't spent all day writing regular expressions.

;-)

> I remember reading about verbose regular expressions.  Would these help?

Yes, read the doc and tutorials about python regexes, you'll find some useful 
features.

> How could I make the above more maintainable?

Maybe you can be interested in the following (seems to me it definitely fits 
your case): I wrote a kind of "SuperPattern" ;-) extension that allows writing, 
testing and composing sub-regexes.
I used {name} since this is illegal in a normal regex. Intended use was 
something like:

import re ; Pattern = re.compile
person = Pattern(r"""...""")
email = Pattern(r"""...""")
phone = Pattern(r"""...""")

entry = SuperPattern(scope, r"""{person}:\s+{email}\s*--\s*{phone}""")


Don't remember the details, but it was a very simple type to write (meta-using 
re, indeed). The only point is that the constructor must be passed the scope 
(typically locals(), else use globals() by default) where to find the 
sub-patterns. Each regex pattern in fact has a .pattern attr (not sure of the 
name) that actually holds its format string: so composing a superpattern is 
just replacing a subpattern's name by its format.

> S.
> 

Denis
-- 


la vita e estrany

spir.wikidot.com

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Making Regular Expressions readable

2010-03-08 Thread member thudfoo
On Mon, Mar 8, 2010 at 9:34 AM, spir  wrote:
> On Mon, 8 Mar 2010 16:12:35 +
> Stephen Nelson-Smith  wrote:
>
>> Hi,
>>
>> I've written this today:
>>
>> #!/usr/bin/env python
>> import re
>>
>> pattern = 
>> r'(?P^(-|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(,
>> [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})*){1})
>> (?P(\S*)) (?P(\S*))
>> (?P(\[[^\]]+\]))
>> (?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)
>> (?P(\S*)) (?P(\S*))
>> (?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)
>> (?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)(
>> )?(?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)'
> [...]
>> It works fine, but it looks pretty unreadable and unmaintainable to
>> anyone who hasn't spent all day writing regular expressions.

You might want to consider Plex:
www.cosc.canterbury.ac.nz/greg.ewing/python/Plex/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] matching words from a text to keys in a dictionary

2010-03-08 Thread Karjer Jdfjdf
I brew this up. It works, but I think it will be slow with a long text and a 
big dictionary

def searchWord(text, dictionary):
    '''search for terms in dictionary(key) and retrieve value(keywords)'''
    text = text.split()
    w = []
    for word in text:
    if word in dictionary:
    print word
    l = dictionary[str(word)]
    for i in l:
    w = w + [i]   
    else:
    print "can't find anything"

    return w


dict1 = { 'had': ['1', '2'], 'little': ['a'] }
text = 'Mary had a little lamb'

w = searchWord(text, dict1)
print w





  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Really learn programming

2010-03-08 Thread Alan Gauld


"Wayne Werner"  wrote


a link to this interesting site about programming in 21 days (or not).

http://norvig.com/21-days.html

I thought it was rather interesting


Indeed, although I disagree with some of it. (Including some of his 
"Answers")


And it focusses on programming only, which is a bit like focussing on 
bricklaying

when considering the building of bridges.
You can only build part of a structure if all you can do is lay bricks... 
To engineer
a complete solution takes many other skills too. Software Engineering is 
more

than programming.

But if you only need to build a garden shed or dog kennel then you don't
need to go on a Civil Engineering course or even a bricklayers 
apprenticeship.
I don't think the "Learn Z in 21 days" books are aimed at professionals, 
they are

aimed at amateurs who usually only need to build kennels!

So yes professional grade programming takes a lifetime to learn (not least
because it is constantly changing!) but an amateur can meet all their needs
much more quickly and have a lot of fun in doing so.

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] variable inheritance

2010-03-08 Thread Alan Gauld

"spir"  wrote


Say I have a Tree type that may be based on 2 kinds of Node-s.
Both conceptually and practically, a tree itself is a node, namely the 
top/root one.


Thats one way to do it...

But a tree also has some additional tree-level behaviour, which is 
independant

of the kind of node.


In which case its possibly not a node because it may be breaking the
Liskov Substitution Principle. In that case a Tree has its own identity
and behaviour and attributes, one of which is a heirarchy of nodes.

So you must decide first of all; Is a tree really a node - ie is it fully
interchangable with a node?

Say, the node type performs all the underlying tree mechanics. Thus the 
Tree type looks like:


class Tree(_Node):
   def __init__(self, args):
   _Node.__init__(self)
   ...
   ... tree-level methods ...

The issue is the actual kind of node is not only initialised
through "_Node.__init__(self)": it must first feature as super class
in "class Tree(Node)".


Surely the different types of node are all subclasses of the
(possibly abstract) Node class? ie you have 3 subclasses
of Node?


I tried to play with __new__ in Tree and Node types, but
this error seems unavoidable. Maybe I did not do it right.


I'm not totally sure what you did to get the error?


I can indeed have __new__ return an instance of either
Node type, but then for any reason it "forgets" it is also
a tree (so that calling any tree method fails).
I there a way to do this right?


Provided the tree is not dependant on the node types
there should not be a problem. The Tree treats all nodes
as Nodes. Polymorphism should do the rest.


Workaround ideas:
* Make the root node an attribute of tree: unsatisfying.


Why is it unsatisfying? Its a perfectly legitimate model.
It is the one used by most GUI frameworks for the widget
containment tree


* Create a common tree type and 2 specialised ones:
class TreeN(Tree,NodeN)
   def __init__(self, args):
   _NodeN.__init__(self)
   Tree.__init__(self, args)
# ... e basta ...


Or a Common Node type and 3 subclasses, Tree,, Node1 and Node2

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Really learn programming

2010-03-08 Thread Albert-Jan Roskam
This very funny cartoon is about learning C++ in 21 days, but it could be about 
any language: http://high5.net/comic/AG/ars_longa_vita_brevis.PNG


Cheers!!

Albert-Jan



~~

In the face of ambiguity, refuse the temptation to guess.

~~

--- On Mon, 3/8/10, Alan Gauld  wrote:

From: Alan Gauld 
Subject: Re: [Tutor] Really learn programming
To: tutor@python.org
Date: Monday, March 8, 2010, 7:43 PM


"Wayne Werner"  wrote

> a link to this interesting site about programming in 21 days (or not).
> 
> http://norvig.com/21-days.html
> 
> I thought it was rather interesting

Indeed, although I disagree with some of it. (Including some of his "Answers")

And it focusses on programming only, which is a bit like focussing on 
bricklaying
when considering the building of bridges.
You can only build part of a structure if all you can do is lay bricks... To 
engineer
a complete solution takes many other skills too. Software Engineering is more
than programming.

But if you only need to build a garden shed or dog kennel then you don't
need to go on a Civil Engineering course or even a bricklayers apprenticeship.
I don't think the "Learn Z in 21 days" books are aimed at professionals, they 
are
aimed at amateurs who usually only need to build kennels!

So yes professional grade programming takes a lifetime to learn (not least
because it is constantly changing!) but an amateur can meet all their needs
much more quickly and have a lot of fun in doing so.

-- Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Really learn programming

2010-03-08 Thread Jeff Johnson

Wayne Werner wrote:
As I was reading my favorite webcomics, Abstruse Goose 
 had a link to this interesting site about 
programming in 21 days (or not).


http://norvig.com/21-days.html

I thought it was rather interesting
(and it also recommends Python ;)

enjoy,
-Wayne

I have been programming for at over 30 years in many languages.  When I 
 wanted to learn Python I saw such books.  The problem is that you 
can't learn a language "well" in 21 days; and it's even more difficult 
if you are not already a programmer in some language.  For the novice 
programmers on this list I (me) would expect to become proficient in 
Python in about one year.  Like I said, I have been programming for over 
30 years in RPG, COBOL, Basic, FoxPro mainly.  So keep at it and don't 
expect to be an expert in 21 days.  Having said that, the 21 day 
tutorials have great merit and can help you.


YMMV

--
Jeff

Jeff Johnson
j...@dcsoftware.com
Phoenix Python User Group - sunpigg...@googlegroups.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] for _ in

2010-03-08 Thread Joson
Hi all,
source code as below puzzles me:

def make_sparse_labeled_tensor(ndim, labels=None,
   initial=None, accumulate=None,
   normalize=False):
if labels is None: labels = [OrderedSet() for _ in xrange(ndim)]
..

It's a library named "divisi". "OrderedSet" is a string set defined in this
lib. "ndim" is integer.  What's the meaning of "for _ in" ?

Joson
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor