Re: [Tutor] Re.findall()

2012-04-12 Thread Andreas Perstinger
On Thu, 12 Apr 2012 09:06:53 -0700 
Michael Lewis  wrote:

> Here's the "pattern" portion that I don't understand:
> 
> re.findall("[^A-Z]+[A-Z]{3}([a-z])[A-Z]{3}[^A-Z]+"
> 

You have 5 different parts here:
1) [^A-Z]+ - this matches one or more non-uppercase characters.
The brackets [] describe a set of wanted characters. A-Z would match
any uppercase character, but the caret ^ at the first position inside
the brackets means to inverse the set (i.e., match any character
not in the set). + means to match at least one of the character(s)
described before.
2) [A-Z]{3} - this matches exactly three uppercase characters.
With the braces {} you can define how many characters should match: {3}
matches exactly 3, {3,} matches at least 3, {,3} matches up to three
and {3,6} matches 3 to 6.
3) ([a-z]) - this matches exactly one lowercase character.
The parens () are used to save the character for later use. (using the
group()/groups()-methods, see the docs).
4) [A-Z]{3} - again matches exactly three uppercase characters.
5) [^A-Z]+ - again matches at least one non-uppercase character.

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


Re: [Tutor] python connect() function

2012-04-12 Thread Pierre Barthelemy
sorry, gobject comes from the PyGTK package.



On Thu, Apr 12, 2012 at 6:32 PM, Alan Gauld wrote:

> On 12/04/12 16:42, Pierre Barthelemy wrote:
>
>> Thanks for the fast reply. I went a bit deeper into the code and i found
>> where my problem was. The connect function comes from gobject, and the
>> gobject.connect function accepts extra arguments that are passed to the
>> connected function. So i can solve my problem by:
>>
>> data.connect('new_data_point', analysis_client.update_plot,
>> specific_arguments)
>>
>
> Glad you got it working but what is gobject?
>
> It isn't part of the Python standard library that I recognize
> so presumably you are using some kind of add-in Framework
> or library?
>
> Any questions to the list need to specify what you are
> using, if it's not in the standard library we will be
> pretty much in the dark and unlikely to be able to
> help until you tell us.
>
> --
> Alan G
> 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] python connect() function

2012-04-12 Thread Alan Gauld

On 12/04/12 16:42, Pierre Barthelemy wrote:

Thanks for the fast reply. I went a bit deeper into the code and i found
where my problem was. The connect function comes from gobject, and the
gobject.connect function accepts extra arguments that are passed to the
connected function. So i can solve my problem by:

data.connect('new_data_point', analysis_client.update_plot, specific_arguments)


Glad you got it working but what is gobject?

It isn't part of the Python standard library that I recognize
so presumably you are using some kind of add-in Framework
or library?

Any questions to the list need to specify what you are
using, if it's not in the standard library we will be
pretty much in the dark and unlikely to be able to
help until you tell us.

--
Alan G
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] Re.findall()

2012-04-12 Thread Alan Gauld

On 12/04/12 15:47, Steven D'Aprano wrote:


Regular expressions are like super-charged wildcards. In the DOS or
Windows command.com or cmd.exe shell, you can use wildcards * and ? to
match any characters, or a single character. In Linux and Macintosh
shells, you have the same thing only even more so.


Just being picky but from Windows 2000 onwards you can use full regex in 
the "DOS" command shell too. You need to toggle a registry option I 
believe but "help cmd" will tell all.


File and command completion are likewise available if desired.
The Windows command line has most of the stuff Bash has. It's the 
scripting language that still sucks! (And for that there

is WSH or Power Shell)

--
Alan G
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] should we use multithreading for multiple clients in socket class

2012-04-12 Thread Alan Gauld

On 12/04/12 12:38, Surya K wrote:

I am learning networking and just wrote a primitive client server program..

...

So, I tried to run another client (same code but from different file)
but found that its now working.


I assume that should be *not working*?
A more detailed description of what you did and the result would help 
diagnose the issue.



why its happening, should I use multithreading so that I can handle two
clients simultaneously??


It's not strictly necessary if there are no resource conflicts such as 
two attempts to write to the same file/data structure. But in the 
general case multi-threading a server (or forking a separate

process) is a good idea.

However, there is some implicit "threading" takes place in the socket 
library insofar as each session is given a separate temporary port to 
communicate over once the connection has been accepted so unless the 
connections are using conflicting resource it should still work.


See the network programming topic in my tutorial for an example and 
screenshots of two clients talking to one server...



--
Alan G
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] Re.findall()

2012-04-12 Thread Michael Lewis
>
> mjole...@gmail.com wrote:
> > Hi everyone,
> >
> > I am having trouble understanding re.findall(). I've read through the
> > documentation and looked at at some examples online, but I still don't
> have
> > a clear picture.
> >
> > I am going through pythonchallenge.com and I am on challenge 3. I've
> see.
> > The answer to the problem, but I don't understand the "pattern" portion
> of
> > re.findall().
>
>
> What part don't you understand? Do you understand what a so-called regular
> expression is?
>

Here's the "pattern" portion that I don't understand:

re.findall("[^A-Z]+[A-Z]{3}([a-z])[A-Z]{3}[^A-Z]+"

What is going on here? The ^ means to apply this pattern from what I read.

The first pattern and last pattern are in opposite order, meaning that it
goes from [^A-Z]+[A-Z]{3} to [A-Z]{3}[^A-Z]+ - why does the pattern
flip-flop? The first pattern starts as [^A-Z] but the last pattern end with
[^A-Z]. Why do I need the "+" signs? Also, why does this ([a-z]) need the
parenthesis?

>
> Regular expressions are like super-charged wildcards. In the DOS or Windows
> command.com or cmd.exe shell, you can use wildcards * and ? to match any
> characters, or a single character. In Linux and Macintosh shells, you have
> the
> same thing only even more so.
>

I am familiar with wildcards, but I've mostly used * and nothing else.

>
> Regular expressions are a mini programming language for wildcards. For
> example, 'a.*z' is a pattern that matches any string starting with the
> letter
> 'a' and ending with the letter 'z'.
>
> Here's a more complicated example:
>
> 'm[aeiou]{1,2}n'
>
> This regular expression pattern, or regex, matches the letter 'm',
> followed by
> 1 or 2 vowels ('a', 'e', 'i', 'o', or 'u') in a row, followed by 'n'. So it
> will match "moon" or "mean" or "moan" or "man", but not "mpg" or
> "marvelous"
> or "men".
>
> You can learn more about regexes here:
>
> http://docs.python.org/library/re.html
> http://docs.python.org/howto/regex.html
>
>
> --
> Steven
>
> mjole...@gmail.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] write list to columns

2012-04-12 Thread Alan Gauld

On 12/04/12 15:32, Mark Lawrence wrote:


That's going to be quite awkward to do. Files like to be written one
complete line at a time. The normal approach would be to build up the
table structure in memory and then write the entire table out in one go.




Built-in zip? Or have I missed something?


The OP specifically said he wanted to write out one *column* at a time 
to the file. Then go back and write the next column etc. zip would be 
one way to build the structure in memory prior to printing, as I suggested.


--
Alan G
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] python connect() function

2012-04-12 Thread Pierre Barthelemy
Thanks for the fast reply. I went a bit deeper into the code and i found
where my problem was. The connect function comes from gobject, and the
gobject.connect function accepts extra arguments that are passed to the
connected function. So i can solve my problem by:

data.connect('new_data_point', analysis_client.update_plot, specific_arguments)

Bests,

Pierre

On Thu, Apr 12, 2012 at 9:21 AM, Peter Otten <__pete...@web.de> wrote:

> Pierre Barthelemy wrote:
>
> > I have a question about event handling and the use of the connect
> > function. I have a data object, that contains a series of signals, one
> > being "new_data_point" .
> >
> > When i want to plot the data, i also connect the "new_data_point" event
> to
> > the function "analysis_client.update_plot". I therefore run the line:
> >
> > data.connect('new_data_point', analysis_client.update_plot)
> >
> > In this case, everytime the data object is updated, i also update the
> > plot.
> >
> >
> > I would like to adapt this code to allow to have several plots, connected
> > to several data objects. I would therefore need to specify, when i
> connect
> > the "update_plot" function, which plots needs to be updated.
> > Is it possible to specify arguments to be used by the connected function
> > when the event occurs ? For instance, is there a way to write something
> > like:
> >
> > data.connect('new_data_point',
> > analysis_client.update_plot(specific_plot_instance))
> >
> > So that when the event occurs, the corresponding plot (and only this one)
> > is updated ?
>
> Like Walter I have no idea what framework or library you are talking about.
> The generic answer is yes: Basically you have to make a new function that
> calls the method with a specifc plot instance.
>
> def update_specific_plot():
>analysis_client.update_plot(specific_plot_instance)
> data.connect("new_data_point", update_specific_plot)
>
> A more elegant and more general version of the above would create the
> helper
> function on the fly using functools.partial():
>
> from functools import partial
>
> data.connect("new_data_point",
>partial(analysis_client.update_plot, specific_plot_instance))
>
>
> ___
> 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] Re.findall()

2012-04-12 Thread Mark Lawrence

On 12/04/2012 14:53, mjole...@gmail.com wrote:

Hi everyone,

I am having trouble understanding re.findall(). I've read through the 
documentation and looked at at some examples online, but I still don't have a 
clear picture.

I am going through pythonchallenge.com and I am on challenge 3. I've see. The answer to 
the problem, but I don't understand the "pattern" portion of re.findall().

Thanks!

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



Please give us an example of the code that you've tried, what you expect 
to see and what the actual output from your program was.  If an 
exception occurred please cut and paste the entire exception traceback 
into this thread.


--
Cheers.

Mark Lawrence.

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


Re: [Tutor] Re.findall()

2012-04-12 Thread Steven D'Aprano

mjole...@gmail.com wrote:

Hi everyone,

I am having trouble understanding re.findall(). I've read through the
documentation and looked at at some examples online, but I still don't have
a clear picture.

I am going through pythonchallenge.com and I am on challenge 3. I've see.
The answer to the problem, but I don't understand the "pattern" portion of
re.findall().



What part don't you understand? Do you understand what a so-called regular 
expression is?


Regular expressions are like super-charged wildcards. In the DOS or Windows 
command.com or cmd.exe shell, you can use wildcards * and ? to match any 
characters, or a single character. In Linux and Macintosh shells, you have the 
same thing only even more so.


Regular expressions are a mini programming language for wildcards. For 
example, 'a.*z' is a pattern that matches any string starting with the letter 
'a' and ending with the letter 'z'.


Here's a more complicated example:

'm[aeiou]{1,2}n'

This regular expression pattern, or regex, matches the letter 'm', followed by 
1 or 2 vowels ('a', 'e', 'i', 'o', or 'u') in a row, followed by 'n'. So it 
will match "moon" or "mean" or "moan" or "man", but not "mpg" or "marvelous" 
or "men".


You can learn more about regexes here:

http://docs.python.org/library/re.html
http://docs.python.org/howto/regex.html


--
Steven

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


Re: [Tutor] user created lists

2012-04-12 Thread Mark Lawrence

On 12/04/2012 05:42, john moore wrote:

Hello Pyhton World,


Hello Hojn Rooem and welcome :)



I'm new at this and was wondering how I create a number of user specified lists?


Why? Tell us what you're trying to achieve and we may well come up with 
a better solution.




Example:

"How many list would you like to create?"
User inputs 5
creates five lists,
list1 []
list2 []
list3 []
list4 []
list5 []

I can create one with append, but I don't know how to loop it to create five 
different named list..
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor




--
Cheers.

Mark Lawrence.

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


Re: [Tutor] write list to columns

2012-04-12 Thread Mark Lawrence

On 12/04/2012 08:48, Alan Gauld wrote:

On 12/04/12 03:31, questions anon wrote:

I am trying to simply write a list of values to txt file in one column.
I would then like to add another list in a second column.


That's going to be quite awkward to do. Files like to be written one
complete line at a time. The normal approach would be to build up the
table structure in memory and then write the entire table out in one go.
If it won't fit in memory use a simple database like SQLite to build the
table then print that out row by row.



Built-in zip? Or have I missed something?




Somehow they only appear as one long row.


You need to write newlines but you probably also want to use string
formatting to control the amount of space required so that your columns
line up.

HTH



--
Cheers.

Mark Lawrence.

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


[Tutor] Re.findall()

2012-04-12 Thread mjolewis
Hi everyone,

I am having trouble understanding re.findall(). I've read through the 
documentation and looked at at some examples online, but I still don't have a 
clear picture. 

I am going through pythonchallenge.com and I am on challenge 3. I've see. The 
answer to the problem, but I don't understand the "pattern" portion of 
re.findall(). 

Thanks!

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


[Tutor] should we use multithreading for multiple clients in socket class

2012-04-12 Thread Surya K

I am learning networking and just wrote a primitive client server program..
server:
from time import ctimefrom socket import *
HOST = ""PORT = 21567BUFSIZ = 1024ADDR = (HOST, PORT)
tcpSerSoc = socket(AF_INET, SOCK_STREAM)tcpSerSoc.bind(ADDR)tcpSerSoc.listen(5)
while True:print "waiting for connection.."tcpCliSoc, addr = 
tcpSerSoc.accept()print "# Connected from:", addrwhile True:
data = tcpCliSoc.recv(BUFSIZ)if not data:break
tcpCliSoc.send( "[%s] %s" % (ctime(), data ))
tcpCliSoc.clock()tcpSerSoc.close()
Client:
from socket import *
HOST = 'localhost'PORT = 21567BUFSIZ = 1024ADDR = (HOST, PORT)
tcpCliSoc = socket(AF_INET, SOCK_STREAM)tcpCliSoc.connect(ADDR)
while True:data = raw_input(">")if not data:break
tcpCliSoc.send(data)data = tcpCliSoc.recv(BUFSIZ)if not data:
breakprint datatcpCliSoc.close()
This client sends a string, and receives the same string with current time...

So, I tried to run another client (same code but from different file) but found 
that its now working.
why its happening, should I use multithreading so that I can handle two clients 
simultaneously??



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


Re: [Tutor] write list to columns

2012-04-12 Thread Alan Gauld

On 12/04/12 06:33, questions anon wrote:

Perfect, thank you.
Is there a way I could easily/quickly add headings to each column?


Headings are no different to any other line of text...



with open('output.txt', 'w') as f:

  write your headings here

for record in records:
f.write(output_format %(record))
f.write('\n')



--
Alan G
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] user created lists

2012-04-12 Thread Alan Gauld

On 12/04/12 08:25, Christian Witts wrote:


I recommend that you bite the bullet and use a dedicated dictionary or list
to hold your five lists from the very begining:

You could use globals() then instead of var(),  although it's a
do-it-at-your-own-risk situation then if you overwrite built-ins and
other functions in the namespace.  Creating your own dictionary with the
'variable name' as the key, would be the nicer solution instead.


Trying to create variables on the fly is usually a bad idea, although it 
seems to appeal to beginners a lot. The problem is: how does the rest of 
the pre-existing code know about these new variable names so as to 
access them.


If you keep the new values in a collection they can be access via a 
loop. But randomly named variables in your code require all sorts of
extra effort to create the name and deference the namespace each time 
you want to access them.


Anonymous values are best kept in anonymous structures IMHO.

--
Alan G
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] write list to columns

2012-04-12 Thread Alan Gauld

On 12/04/12 03:31, questions anon wrote:

I am trying to simply write a list of values to txt file in one column.
I would then like to add another list in a second column.


That's going to be quite awkward to do. Files like to be written one 
complete line at a time. The normal approach would be to build up the 
table structure in memory and then write the entire table out in one go.
If it won't fit in memory use a simple database like SQLite to build the 
table then print that out row by row.




Somehow they only appear as one long row.


You need to write newlines but you probably also want to use string 
formatting to control the amount of space required so that your columns 
line up.


HTH
--
Alan G
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] user created lists

2012-04-12 Thread Christian Witts

On 2012/04/12 08:59 AM, Peter Otten wrote:

Christian Witts wrote:


On 2012/04/12 06:42 AM, john moore wrote:

Hello Pyhton World,

I'm new at this and was wondering how I create a number of user specified
lists?

Example:

"How many list would you like to create?"
User inputs 5
creates five lists,
list1 []
list2 []
list3 []
list4 []
list5 []

I can create one with append, but I don't know how to loop it to create
five different named list..



You can use vars() to create the variables on the fly. vars() is just a
dictionary containing the variable name as the key, and the data as the
value so you can do `vars()['list1'] = []` and it's easy enough to
create them en masse

# Set the start to 1, and add 1 to what the user inputted
# as range/xrange doesn't include the top number
for i in xrange(1, user_input + 1):
  vars()['list%s' % i] = []


This will stop working once you move your code into a function:


def f():

... vars()["list1"] = ["a", "b"]
... print list1
...

f()

Traceback (most recent call last):
   File "", line 1, in
   File "", line 3, in f
NameError: global name 'list1' is not defined

I recommend that you bite the bullet and use a dedicated dictionary or list
to hold your five lists from the very begining:
You could use globals() then instead of var(),  although it's a 
do-it-at-your-own-risk situation then if you overwrite built-ins and 
other functions in the namespace.  Creating your own dictionary with the 
'variable name' as the key, would be the nicer solution instead.


--

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


Re: [Tutor] python connect() function

2012-04-12 Thread Peter Otten
Pierre Barthelemy wrote:

> I have a question about event handling and the use of the connect
> function. I have a data object, that contains a series of signals, one
> being "new_data_point" .
> 
> When i want to plot the data, i also connect the "new_data_point" event to
> the function "analysis_client.update_plot". I therefore run the line:
> 
> data.connect('new_data_point', analysis_client.update_plot)
> 
> In this case, everytime the data object is updated, i also update the
> plot.
> 
> 
> I would like to adapt this code to allow to have several plots, connected
> to several data objects. I would therefore need to specify, when i connect
> the "update_plot" function, which plots needs to be updated.
> Is it possible to specify arguments to be used by the connected function
> when the event occurs ? For instance, is there a way to write something
> like:
> 
> data.connect('new_data_point',
> analysis_client.update_plot(specific_plot_instance))
> 
> So that when the event occurs, the corresponding plot (and only this one)
> is updated ?

Like Walter I have no idea what framework or library you are talking about. 
The generic answer is yes: Basically you have to make a new function that 
calls the method with a specifc plot instance.

def update_specific_plot():
analysis_client.update_plot(specific_plot_instance)
data.connect("new_data_point", update_specific_plot)

A more elegant and more general version of the above would create the helper 
function on the fly using functools.partial():

from functools import partial

data.connect("new_data_point",
partial(analysis_client.update_plot, specific_plot_instance))


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


Re: [Tutor] user created lists

2012-04-12 Thread Peter Otten
Christian Witts wrote:

> On 2012/04/12 06:42 AM, john moore wrote:
>> Hello Pyhton World,
>>
>> I'm new at this and was wondering how I create a number of user specified
>> lists?
>>
>> Example:
>>
>> "How many list would you like to create?"
>> User inputs 5
>> creates five lists,
>> list1 []
>> list2 []
>> list3 []
>> list4 []
>> list5 []
>>
>> I can create one with append, but I don't know how to loop it to create
>> five different named list..

> You can use vars() to create the variables on the fly. vars() is just a
> dictionary containing the variable name as the key, and the data as the
> value so you can do `vars()['list1'] = []` and it's easy enough to
> create them en masse
> 
> # Set the start to 1, and add 1 to what the user inputted
> # as range/xrange doesn't include the top number
> for i in xrange(1, user_input + 1):
>  vars()['list%s' % i] = []

This will stop working once you move your code into a function:

>>> def f():
... vars()["list1"] = ["a", "b"]
... print list1
... 
>>> f()
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 3, in f
NameError: global name 'list1' is not defined

I recommend that you bite the bullet and use a dedicated dictionary or list 
to hold your five lists from the very begining:

>>> num_lists = int(raw_input("How many lists? "))
How many lists? 5
>>> list_of_lists = [[] for i in range(num_lists)]

You then have to access the first list as "list_of_list[0]" instead of 
"list1" and so on:

>>> list_of_lists[0].append(1)
>>> list_of_lists[4].append(2)
>>> list_of_lists
[[1], [], [], [], [2]]


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