[Tutor] video watermark

2010-06-04 Thread Igor Nemilentsev
Hi everyone.
Can someone suggest a python module so that it would be able to do a video
processing and adding watermark in video?

-- 
If it's not loud, it doesn't work!
-- Blank Reg, from Max Headroom

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


Re: [Tutor] Misc question about scoping

2010-06-04 Thread Alan Gauld

Steven D'Aprano st...@pearwood.info wrote


isThumbnail = True if size == thumbnail else False


That is technically correct, you could do that. That's a good 
example of
the syntax of the `if` expression, but it's a bad example of where 
to

use it:


In the sense that an equality test will always give a real
boolean value as a result I agree. But in the more generic
case where we use an expressiioon as a boolean it can
be usful. Specifically if the test involves boolean
operators Python does not return True/False but the
values of the operands. In that case using the if/else
form yields a  real boolean result.

eg

flag = True if  (smoeValue or another) else False

is different to

flag = someValue or another

Which was why I thought it worth pointing out that the if/else
could be used.


with all due respect to Alan who suggested it. It really is an
unnecessarily complicated and verbose way of doing a
simple assignment,


In the case of an equality test I agree.

--
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] sockets, servers, clients, broadcasts...?

2010-06-04 Thread spir
On Thu, 3 Jun 2010 18:03:34 -0400
Alex Hall mehg...@gmail.com wrote:

 Hi all,
 I am a CS major, so I have had the required networking class. I get
 the principles of networking, sockets, and packets, but I have never
 had to actually implement any such principles in any program. Now I
 have this Battleship game (not a school assignment, just a summer
 project) that I am trying to make playable over the internet, since I
 have not written the AI and playing Battleship against oneself is
 rather boring.
 
 Right now I am just trying to figure out how to implement something
 like the following:
 *you start the program and select online game
 *you select server or client (say you choose server)
 *somehow, your instance of the program starts up a server that
 broadcasts something; your enemy has selected client, and is now
 (SOMEHOW) listening for the signal your server is broadcasting
 *the signal is picked up, and, SOMEHOW, you and your opponent connect
 and can start sending and receiving data.
 
 First, how does the client know where to look for the server? I am not
 above popping up the server's ip and making the client type it in, but
 a better solution would be great.
 How do I make it so that the client can find the server correctly? The
 above IP is one thing, but are ports important here? Not a big deal if
 they are, but I am not sure. Does someone have an example of this
 process?

I'm far to be a specialist in this field, so this is just reasoning by watching 
your requirements.

First, such a game seems to match peer-to-peer relation. This would be 
different if you implemented complicated game logic, like in the case of an AI 
playing.
For peers to connect, a general solution is them to register on a dedicated 
public interface (the same can be used to publish a server's IP, indeed). Then, 
your app first reads data there to know which (address,port) pair(s) are 
available.
But since you seem to be still exploring data exchange issues, you'd better 
concentrate on this basic mechanism first, choose and try one of numerous 
possible solutions, then only address higher-level problems. In the meanwhile, 
just put IPs and ports in a config file or even hardcode them as constants.

About client-server, as said above, this model does not really seem to match 
your use case, I guess. But this is still an interesting alternative. I would 
set my own post as server and players as clients. So, when playing myself, I 
would be a local client. This indeed allows two other guys playing using your 
post as server (and you maybe watching the game ;-).
But this does not make much sense if the server does not have any relevant info 
to deliver (eg playing hints).


Denis


vit esse estrany ☣

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


[Tutor] BerkeleyTIP Join June Global Free SW HW Culture Mtgs via VOIP or in Berkeley

2010-06-04 Thread giovanni_re
Hi Python Tutor people  :) 

Who leads this group?

I put together a global meeting (via voip) that 
1) has an interest in Python,
2) and that Python learners, users  developers might be interested in.

BerkeleyTIP is a meeting about All Free SW HW  Culture.
It is Educational, Productive,  Social.
For Learning about, Sharing,  Producing, All Free SW HW  Culture.
TIP ==  Talks, Installfest, Project  Programming Party
http://sites.google.com/site/berkeleytip

I put these meetings together in my nonexistent free time, as a
volunteer contribution to the various communities.  When i can make the
time, I send out one announcement per month with the monthly videos to
various free SW HW  Culture groups.  

Would you all would like to have me send your list the monthly
announcements about the global via VOIP  Free SW, HW  Culture meetings,
BerkeleyTIP global?  

Please let me know. :)

Below is the June announcement, so you know what they are like.

We have a python video this month, too:
Scientific data visualization using Mayavi2, Gael Varoquaux,
Python4ScienceUCB


Best wishes :)


=
You're invited to join in with the friendly people at the BerkeleyTIP
global meeting  -  newbie to Ph.D. -  everyone is invited.

Get a headset  join using VOIP online, or come to Berkeley.

1st step: Join the mailing list:
http://groups.google.com/group/BerkTIPGlobal

Watch the videos.  Discuss them on VOIP.
8 great videos/talks this month - see below.

Starting off year 3 of BerkeleyTIP  :)

Join with us at the LOCATION TO BE DETERMINED
 at the University of California at Berkeley,
or join from your home via VOIP,
or send this email locally, create a local meeting,  join via VOIP:
  Tip: a wifi cafe is a great place to meet. :)

JUNE 5  20 AT UCB MEETING LOCATIONS TO BE DETERMINED.
PLEASE VIEW THE BTIP WEBSITE  MAILING LIST PAGES FOR THE LATEST
DETIALS.
http://sites.google.com/site/berkeleytip
http://groups.google.com/group/BerkTIPGlobal


BerkeleyTIP  -  Educational, Productive, Social
For Learning about, Sharing,  Producing, All Free SW HW  Culture.
TIP ==  Talks, Installfest, Project  Programming Party
http://sites.google.com/site/berkeleytip

=  CONTENTS:  1) 2010 JUNE VIDEOS;  2) 2010 JUNE MEETING DAYS,
TIMES, LOCATIONS;  3) LOCAL MEETING AT U. C. Berkeley LOCATION TO BE
DETERMINED;  4) HOT TOPICS;  5) PLEASE  RSVP  PROBABILISTICALLY, THANKS 
:) ;  6) INSTALLFEST;  SPECIAL: FREE 7th Annual BERKELEY WORLD MUSIC
FESTIVAL June 5 Sat;  7) ARRIVING FIRST AT THE MEETING: MAKE A
BerkeleyTIP SIGN;  8) IRC: #berkeleytip  on  irc.freenode.net; 9) VOIP
FOR GLOBAL MEETING;  10) VOLUNTEERING, TO DOs;  11) MAILING LISTS:
BerkeleyTIP-Global, LocalBerkeley, Announce;  12) ANYTHING I FORGOT TO
MENTION?;  13) FOR FORWARDING


===
=  1)  2010 JUNE VIDEOS
Super Computing for Business, Brian Modra, CLUG
State of the Linux Union 2010, Jim Zemlin, Linux Foundation
Journaled Soft-Updates, Dr. Kirk McKusick, BSDCan 2010
Scientific data visualization using Mayavi2, Gael Varoquaux,
Python4ScienceUCB
Bringing OLPC to children in Afghanistan, Carol Ruth Silver, OLPC-SF
Text-to-Speech in Ubuntu with Kttsd Kmouth Festival, blip.tv
Rabbi Rabbs, the UnixRabbi, leads a group of Unix geeks, Comedy, UUASC,
BSD, 2003
The Great Debate - Are We Alone?, Geoff Marcy and Dan Werthimer, s...@uc
Berkeley 

Thanks to all the speakers, organizations,  videographers. :)
[Please alert the speakers that their talks are scheduled for June for
BTIP (if you are with the group that recorded their talk), because I may
not have time to do that.  Thanks. :)  ]

URLs for video download  full details:
http://sites.google.com/site/berkeleytip/talk-videos

Download  watch these talks before the BTIP meetings. Discuss at the
meeting.

Email the mailing list, tell us what videos you'll watch  want to
discuss:
http://groups.google.com/group/BerkTIPGlobal

Know any other video sources? - please email me.
_Your_ group should video record  post online your meeting's talks!


=  2)  2010 JUNE MEETING DAYS, TIMES, LOCATIONS
In person meetings on 1st Saturday  3rd Sunday, every month.
June  5  20, 12N-3P USA-Pacific time, Saturday, Sunday

Online only meeting using VOIP:
June 14  29, 5-6P   USA-Pacific time, Monday, Tuesday

Mark your calendars.

 5 Sat 12N-3P PDST = 3-6P EDST = 19-22 UTC
14 Mon   5-6P PDST = 8-9P EDST =  0- 1 UTC Tues 15
20 Sun 12N-3P PDST = 3-6P EDST = 19-22 UTC
29 Tues  5-6P PDST = 8-9P EDST =  0- 1 UTC Wed 30

USA-PacificDaylightSavingsTime is -7 hours UTC, due to daylight savings
currently. Times listed above should be double checked by you for
accuracy.


=  3)  LOCAL MEETING AT  U. C. BERKELEY  -  LOCATION TO BE
DETERMINED
http://sites.google.com/site/berkeleytip/directions
RSVP please.  See below.  It greatly helps my planning.  But, _do_ come
if you forgot to RSVP.

THE JUNE 5  20 ON CAMPUS MEETING LOCATIONS ARE CURRENTLY TO BE
DETERMINED.  HOPEFULLY KNOW ON FRIDAY JUNE 4.  PLEASE 

Re: [Tutor] Misc question about scoping

2010-06-04 Thread Dave Angel

Alan Gauld wrote:

snip

flag = True if  (smoeValue or another) else False

is different to

flag = someValue or another

Which was why I thought it worth pointing out that the if/else
could be used.



I'd prefer the form:

  flag = not not (someValue or another)

if I needed real True or False result.


DaveA

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


Re: [Tutor] Misc question about scoping

2010-06-04 Thread Tino Dai
 That is technically correct, you could do that. That's a good example of
 the syntax of the `if` expression, but it's a bad example of where to
 use it:

 (1) it only works in Python 2.5 or better; and

 (2) experienced Python programmers will laugh at you :)

 with all due respect to Alan who suggested it. It really is an
 unnecessarily complicated and verbose way of doing a simple assignment,
 nearly as bad as writing this:

 if len(mystring) == 0:
    n = 0
 else:
    n = len(mystring)


 instead of just

 n = len(mystring)


Hey Everybody,

    Thank you for the rich discussion and making me a better
python programmer! Between the new topics that I have learned in
Python and Django from documentation, experimentation, and this list,
I think my IQ has gone up a couple of points!

    I'm at a point where I can do most things in Python (maybe) ,
now I'm looking to do them succinctly and elegantly. For instance, I
had about 10 - 15 lines of code to do this before with a bunch of
loops and if blocks, I distilled the product down to this:

   answerDict=dict(map(lambda x: (str(x[1]),x[0]),map(lambda x: \
      x.values(),Answer.objects.filter(fk_questionSet=1). \
      filter(fk_question=1).values('widgetAnswer').order_by(). \
      annotate(widgetCount=Count('widgetAnswer')

So instead of my python code doing the leg work, I have the Django
ORM and that underlying DB do the work. Also, I leveraged lambda
functions and maps to coerce the data in to the right format. Pretty
cool IMHO. And I turn it over to the group to see if there is any
improvements or gotchas that I missed.

Thanks and Thanks in advance,
Tino
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Misc question about scoping

2010-06-04 Thread Hugo Arts
On Fri, Jun 4, 2010 at 1:23 PM, Dave Angel da...@ieee.org wrote:

 I'd prefer the form:

  flag = not not (someValue or another)


That's a construct you might commonly find in languages like C, but I
don't think it's very pythonic. If you want to convert your result to
a bool, be explicit about it:

flag = bool(some_value or another)

I'd agree that the if/else construct is redundant if you just want a
True/False result, but the double not is a kind of implicit type
conversion that is easily avoided.

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


Re: [Tutor] Misc question about scoping

2010-06-04 Thread Hugo Arts
On Fri, Jun 4, 2010 at 2:46 PM, Tino Dai obe...@gmail.com wrote:

     I'm at a point where I can do most things in Python (maybe) ,
 now I'm looking to do them succinctly and elegantly. For instance, I
 had about 10 - 15 lines of code to do this before with a bunch of
 loops and if blocks, I distilled the product down to this:

    answerDict=dict(map(lambda x: (str(x[1]),x[0]),map(lambda x: \
       x.values(),Answer.objects.filter(fk_questionSet=1). \
       filter(fk_question=1).values('widgetAnswer').order_by(). \
       annotate(widgetCount=Count('widgetAnswer')


I have a distinct feeling that you would simply love a language like lisp.

 So instead of my python code doing the leg work, I have the Django
 ORM and that underlying DB do the work. Also, I leveraged lambda
 functions and maps to coerce the data in to the right format. Pretty
 cool IMHO. And I turn it over to the group to see if there is any
 improvements or gotchas that I missed.


The code is succinct, and it may very well be called elegant in some
sense of the word. I might call it clever, which in the python
community is not generally meant as a compliment. Readability counts,
you see, and I find that piece of code nigh impossible to read. I
would suggest changing the map calls into generator expressions, and
using a few temporary variables for clarity. That should keep most of
the brevity but increase legibility:

answers = Answer.objects.filter(fk_questionSet=1,
fk_question=1).values('widgetAnswer')
answers = answers.order_by().annotate(widgetCount=Count('widgetAnswer'))
values = (x.values() for x in answers)
answerDict = dict((str(v[1]), v[0]) for v in values)

Disclaimer: i'm not particularly experienced with django, so this
piece of code is quite possibly not the most efficient way to do this,
and may even be incorrect (e.g. I assumed the filter calls could be
collapsed like in sqlAlchemy). However, the rest of my advice should
still apply.

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


Re: [Tutor] video watermark

2010-06-04 Thread Jan Jansen

Am 04.06.2010 09:21, schrieb Igor Nemilentsev:

Hi everyone.
Can someone suggest a python module so that it would be able to do a video
processing and adding watermark in video?

   
Once I had to blur the part of the viewing area of a screencast which 
showed some confident CAD model. First, I exported the video as sequence 
of images with virtualdub. The result was something like 3000 *.bmp 
files on my harddrive. Then I created a little python script using PIL 
(http://www.pythonware.com/products/pil/). That script processed the 
files file by file, so no worries about huge movie files and memory. 
Finally, I've merged the new images with virtual dub. It all worked very 
well.


Otherwise I'm not aware of any video processing library in Python.

Also, there're filters for virtualdub to add watermarks to a movie. That 
might be the easiest way to get your results.


Cheers,

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


Re: [Tutor] Misc question about scoping

2010-06-04 Thread Tino Dai
 I have a distinct feeling that you would simply love a language like lisp.

LOL, it's actually on the list of things to do. And hear that one will become a
better programmer once they learn LISP.

 The code is succinct, and it may very well be called elegant in some
 sense of the word. I might call it clever, which in the python
 community is not generally meant as a compliment. Readability counts,
 you see, and I find that piece of code nigh impossible to read. I
 would suggest changing the map calls into generator expressions, and
 using a few temporary variables for clarity. That should keep most of
 the brevity but increase legibility:

 answers = Answer.objects.filter(fk_questionSet=1,
 fk_question=1).values('widgetAnswer')
 answers = answers.order_by().annotate(widgetCount=Count('widgetAnswer'))
 values = (x.values() for x in answers)
 answerDict = dict((str(v[1]), v[0]) for v in values)

I am always fighting the battle - more compact vs more readable :) . A couple of
questions that I have: Why generators over map? Is it somehow more efficient
under the covers or is it for readability purposes? I noticed that you
took out the
lambda function too (not much good without a map/filter/reduce) to replace
with the generator function. Also could you give me some instances
where a generator
would be used in a real situation? I have already read the stuff on
doc.python.org about
generators.

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


Re: [Tutor] Misc question about scoping

2010-06-04 Thread Emile van Sebille

On 6/4/2010 5:46 AM Tino Dai said...

 I'm at a point where I can do most things in Python (maybe) ,
now I'm looking to do them succinctly and elegantly. For instance, I
had about 10 - 15 lines of code to do this before with a bunch of
loops and if blocks, I distilled the product down to this:

answerDict=dict(map(lambda x: (str(x[1]),x[0]),map(lambda x: \
   x.values(),Answer.objects.filter(fk_questionSet=1). \
   filter(fk_question=1).values('widgetAnswer').order_by(). \
   annotate(widgetCount=Count('widgetAnswer')



The first time there's a suspected problem with this code, you'll 
probably end up with a similar refactored set of 10-15 lines.  I'm sure 
because I've got code like that scattered throughout my codebase and 
that's what I end up doing.  The difference is that I rattle off the 
one-liners as part of the original coding effort, and only break it out 
when there's a need to -- I'm not striving to compact things into 
one-liners.


BTW, doesn't

   dict(map(lambda x: (str(x[1]),x[0]),map(lambda x:x.values()

simply map the values of a dict back into a dict?

Emile






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


[Tutor] treeTraversal, nested return?

2010-06-04 Thread jjcrump

All,

any observations might be helpful. For the display of database contents I 
have the following problem:


Database querys will return data to me in tuples of the following sort:
each tuple has a unique id, a parent id, and a type.

a parent id of 0 indicates that the element has no parent.
not all data elements have children
image elements have no children and must have a parent

So the contents of the db is a tree of sorts:

(1, 0, work)
(555, 1, image)
(556, 1, work)
(557, 556, image)
(558, 556, work)
(559, 558, image)
(560, 558, work)
(561, 556, image)
(562, 556, image)
(563, 1, work)
(564, 563, work)
(565, 1, work)

I have a function that will traverse this tree, summoning lists of tuples 
at each leaf. Well and good; it took a lot of trial and error, but it was 
a fine education in tree traversal.


def makeHeirarchy(id):
id = parentPath(id)[-1]
rootimages = getImages(id[0])
rootworks = getWorks(id[0])
heirarchy = treeTraversal(rootworks, [id, rootimages])
return heirarchy

## which calls this recursive function:

def treeTraversal(listIn,result):
for x in listIn:
if not getWorks(x[0]):
result.append(x)
result.append(getImages(x[0]))
else:
result.append(x)
result.append(getImages(x[0]))
treeTraversal(getWorks(x[0]), result)
return result

My problem is that this returns the tuples to me in a flat list.
I've been trying to work out how I can get it to return some sort of 
nested structure: nested lists, dictionaries, or html thus:


ul
li1,0,data/li
ul
li555, 1, image/li
li556, 1, datali
ul
li557, 556, image/li
li561, 556, image/li
li562, 556, image/li
li558, 556, data/li
ul
li559, 558, image/li
li560, 558, image/li
/ul
/ul
li563, 1, data/li
ul
li564, 563, data/li
/ul
li565, 1, data/li
/ul
/ul

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


Re: [Tutor] From SQL Blobs to Python Buffers to Actual Files

2010-06-04 Thread GoodPotatoes
When I write the blob/binary data to a file and give it its original file name 
customer1.rtf, I am expecting the application to read the binary data into 
text,excel,word documents, or whatever application created the file originally.

Example:
when I write blob1 to customer1.rtf, I get 
xÚµVKsÛ6î93ù{ÈÁîÈ2%Û±cŸlgœdÒvLJ²\J¨ as my first line.

To get the blob in Python, I use the following:

row=cursor.execute(select top 1 * from FOO..DocUploads).fetchone()
#row[2] is the original file name, row[3] is the binary data

The blob, when selected in python is a buffer:

 row[3]
read-only buffer for 0x010D7820, size 1496, offset 0 at 0x010B5EE0

When I print row[3] I get data like xڵVKsÛ6î93ù{ÈÁîÈ2%۱
When I print str(row[3]) I get the same data.





From: Steven D'Aprano st...@pearwood.info
To: tutor@python.org
Sent: Thu, June 3, 2010 9:18:44 PM
Subject: Re: [Tutor] From SQL Blobs to Python Buffers to Actual Files

On Fri, 4 Jun 2010 10:57:07 am GoodPotatoes wrote:

 I have been given a legacy database, and need to read the binaryfiles
 out to a disk.  The table has columns filename and binaryFile,
 where the binaryFile is a BLOB

 My python script so far is:

 import pyodbc
 cnxn=pyodbc.Connection(DSN=sybasedatabase)
 cursor=cnxn.cursor()
 p=cursor.execute(select top 1 * from FOO..Table).fetchone()
 #p contains ('UsersOldFile.rtf', read-only buffer for 0x010E1540,
 size 1496, offset 0 at 0x010C04E0)

 #I tried to write this out to the disk as:
 myfile=open(p[0],'wb')
 myfile.write(p[1])
 myfile.close()

 #but all I get is gibberish.  Is there another way to handle the
 buffer, or something else I'm missing?

What do you mean gibberish? I would expect a binary blob to look 
exactly like random binary characters, in other words, gibberish, so 
what makes you think this is not working perfectly?

What do you get if you print the binary blob, and what do you expect to 
get?



-- 
Steven D'Aprano
___
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] treeTraversal, nested return?

2010-06-04 Thread Matthew Wood
In general, there's 2 solutions to your unflattening problem.

A) The general solution: Keep track of the depth you're at, and add
tabs/spaces ('\t' * depth) as necessary.

B) The xml solution: Use (for example) the dom.minidom module, and create a
new ul node and populate it with children, at each level.  Then, have the
xml do the printing for you.

nitpick
For what it's worth, I may have a slight nit-pick of your final output.  I
don't believe you can technically have a ul child of a ul node.  I'm not
absolutely sure on that, but my quick google searching has appeared to
confirm.  If that turns out to be true, you'll need to change the structure
of your output.

ul
li
1,0,data
ul
li555,1,image/li
...
/ul
/li
/ul

All of that is, of course, null and void if you define your own xml scheme.


/nitpick


--

I enjoy haiku
but sometimes they don't make sense;
refrigerator?


On Fri, Jun 4, 2010 at 11:15 AM, jjcr...@uw.edu wrote:

 All,

 any observations might be helpful. For the display of database contents I
 have the following problem:

 Database querys will return data to me in tuples of the following sort:
 each tuple has a unique id, a parent id, and a type.

 a parent id of 0 indicates that the element has no parent.
 not all data elements have children
 image elements have no children and must have a parent

 So the contents of the db is a tree of sorts:

 (1, 0, work)
 (555, 1, image)
 (556, 1, work)
 (557, 556, image)
 (558, 556, work)
 (559, 558, image)
 (560, 558, work)
 (561, 556, image)
 (562, 556, image)
 (563, 1, work)
 (564, 563, work)
 (565, 1, work)

 I have a function that will traverse this tree, summoning lists of tuples
 at each leaf. Well and good; it took a lot of trial and error, but it was a
 fine education in tree traversal.

 def makeHeirarchy(id):
id = parentPath(id)[-1]
rootimages = getImages(id[0])
rootworks = getWorks(id[0])
heirarchy = treeTraversal(rootworks, [id, rootimages])
return heirarchy

 ## which calls this recursive function:

 def treeTraversal(listIn,result):
for x in listIn:
if not getWorks(x[0]):
result.append(x)
result.append(getImages(x[0]))
else:
result.append(x)
result.append(getImages(x[0]))
treeTraversal(getWorks(x[0]), result)
return result

 My problem is that this returns the tuples to me in a flat list.
 I've been trying to work out how I can get it to return some sort of nested
 structure: nested lists, dictionaries, or html thus:

 ul
 li1,0,data/li
ul
li555, 1, image/li
li556, 1, datali
ul
li557, 556, image/li
li561, 556, image/li
li562, 556, image/li
li558, 556, data/li
ul
li559, 558, image/li
li560, 558, image/li
/ul
/ul
li563, 1, data/li
ul
li564, 563, data/li
/ul
li565, 1, data/li
/ul
 /ul

 ___
 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] sockets, servers, clients, broadcasts...?

2010-06-04 Thread Alex Hall
Thanks for the suggestions, everyone. For now, more as a way to
introduce myself to all this, I am going to have my program
automatically start a server and pop up that server's ip. Then, if you
want, you can type in an ip and connect to the server at that ip
(ports are hard-coded at ). That way everyone is a server and a
client, and either you connect to someone else as a client or they
connect to you as a client. It appears, though, that I need a loop to
have a server make any sense at all, to handle incoming data and
connection requests. Obviously, starting this loop will then put the
rest of my program on hold. Am I safe to use basic threading to stick
the server's loop into a new thread? That is, will the rest of my
program be able to talk to the server / will the server be able to
talk to my program from its new home in its separate thread, and might
I run into strange problems because of process scheduling and blocking
(or not blocking)?

On 6/4/10, spir denis.s...@gmail.com wrote:
 On Thu, 3 Jun 2010 18:03:34 -0400
 Alex Hall mehg...@gmail.com wrote:

 Hi all,
 I am a CS major, so I have had the required networking class. I get
 the principles of networking, sockets, and packets, but I have never
 had to actually implement any such principles in any program. Now I
 have this Battleship game (not a school assignment, just a summer
 project) that I am trying to make playable over the internet, since I
 have not written the AI and playing Battleship against oneself is
 rather boring.

 Right now I am just trying to figure out how to implement something
 like the following:
 *you start the program and select online game
 *you select server or client (say you choose server)
 *somehow, your instance of the program starts up a server that
 broadcasts something; your enemy has selected client, and is now
 (SOMEHOW) listening for the signal your server is broadcasting
 *the signal is picked up, and, SOMEHOW, you and your opponent connect
 and can start sending and receiving data.

 First, how does the client know where to look for the server? I am not
 above popping up the server's ip and making the client type it in, but
 a better solution would be great.
 How do I make it so that the client can find the server correctly? The
 above IP is one thing, but are ports important here? Not a big deal if
 they are, but I am not sure. Does someone have an example of this
 process?

 I'm far to be a specialist in this field, so this is just reasoning by
 watching your requirements.

 First, such a game seems to match peer-to-peer relation. This would be
 different if you implemented complicated game logic, like in the case of an
 AI playing.
 For peers to connect, a general solution is them to register on a dedicated
 public interface (the same can be used to publish a server's IP, indeed).
 Then, your app first reads data there to know which (address,port) pair(s)
 are available.
 But since you seem to be still exploring data exchange issues, you'd better
 concentrate on this basic mechanism first, choose and try one of numerous
 possible solutions, then only address higher-level problems. In the
 meanwhile, just put IPs and ports in a config file or even hardcode them as
 constants.

 About client-server, as said above, this model does not really seem to match
 your use case, I guess. But this is still an interesting alternative. I
 would set my own post as server and players as clients. So, when playing
 myself, I would be a local client. This indeed allows two other guys playing
 using your post as server (and you maybe watching the game ;-).
 But this does not make much sense if the server does not have any relevant
 info to deliver (eg playing hints).


 Denis
 

 vit esse estrany ☣

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



-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Misc question about scoping

2010-06-04 Thread ALAN GAULD


  flag = True if  (someValue or another) else False
 
 I'd prefer the form:

flag = not not (someValue or another)

Eek! no I'd just cast to a bool:

flag = bool(someValue or another)

but the if/else form has the Pythonic virtue of explicitness.

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


Re: [Tutor] sockets, servers, clients, broadcasts...?

2010-06-04 Thread Alan Gauld


Alex Hall mehg...@gmail.com wrote

connect to you as a client. It appears, though, that I need a loop 
to

have a server make any sense at all, to handle incoming data and
connection requests.


You need to listen then process incoming messages then listen some 
more, so yes you will need a loop. Probaqbly an infinite one:


while True:
listen()
if someQuitMessage:
   break
process()
rest of my program on hold. Am I safe to use basic threading to 
stick

the server's loop into a new thread?


For your game you don't need to do that. A single thread should 
suffice.
But usually a server's listen loop is the main thread and the 
processing

goes into the sub threads.


program be able to talk to the server / will the server be able to
talk to my program from its new home in its separate thread,


Look at the example in my tutorial. It doesn't use threads but
services two separate clients without lockup. (Albeit it doesn't
care about context...)

--
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] Misc question about scoping

2010-06-04 Thread ALAN GAULD
 Are you arguing that the if/else form is better than simply casting 
 to bool because it is more explicit? 

No, I'm simply offering a reason to use if/else in the general sense.

In practice, for this specific case, I'd go with the bool(expr) form.

 Alan Gauld
Author of the Learn To Program website
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] Misc question about scoping

2010-06-04 Thread Hugo Arts
On Fri, Jun 4, 2010 at 10:02 PM, ALAN GAULD alan.ga...@btinternet.com wrote:

 No, I'm simply offering a reason to use if/else in the general sense.

 In practice, for this specific case, I'd go with the bool(expr) form.


Ah, then it seems we are in agreement. Sorry for the misunderstanding
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Misc question about scoping

2010-06-04 Thread Dave Angel

Hugo Arts wrote:

On Fri, Jun 4, 2010 at 1:23 PM, Dave Angel da...@ieee.org wrote:
  

I'd prefer the form:

 flag =ot not (someValue or another)




That's a construct you might commonly find in languages like C, but I
don't think it's very pythonic. If you want to convert your result to
a bool, be explicit about it:

flag =ool(some_value or another)

I'd agree that the if/else construct is redundant if you just want a
True/False result, but the double not is a kind of implicit type
conversion that is easily avoided.

Hugo

  
I'd certainly agree that bool() is better than not not.  And I admit a C 
background.


DaveA

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


Re: [Tutor] Misc question about scoping

2010-06-04 Thread Hugo Arts
On Fri, Jun 4, 2010 at 6:26 PM, Tino Dai obe...@gmail.com wrote:

 LOL, it's actually on the list of things to do. And hear that one will become 
 a
 better programmer once they learn LISP.


I most certainly did. There are very few languages as conceptually
pure as this one. If you ever get it on the top of your list, check
out these links. [1] is the venerable book structure and
interpretation of computer programs, commonly regarded as one of the
best introductory programming books ever written. It teaches scheme.
[2] is a set of excellent lectures of said book, by its writers. They
are very good teachers.

[1] http://mitpress.mit.edu/sicp/
[2] http://groups.csail.mit.edu/mac/classes/6.001/abelson-sussman-lectures/


 I am always fighting the battle - more compact vs more readable :) . A couple 
 of
 questions that I have: Why generators over map? Is it somehow more efficient
 under the covers or is it for readability purposes? I noticed that you
 took out the
 lambda function too (not much good without a map/filter/reduce) to replace
 with the generator function. Also could you give me some instances
 where a generator
 would be used in a real situation? I have already read the stuff on
 doc.python.org about
 generators.


generator expressions have essentially the same performance, but are
less memory hungry, since the results do not have to be kept into
memory. There is a function called imap in the itertools module that
has that same benefit though, so it's not inherent to map at all.
Think of the list comprehension as a more readable/concise alternative
to map and filter, while generator expressions correspond to the
imap/ifilter functions. Readability is the key issue here. The
generator expression doesn't need a function, so the use of the lambda
keyword is avoided, making it a lot shorter and easier on the eyes. It
also reads more naturally.

generator expressions are basically just a shortcut to generators
though, which are even more powerful. Take, for example, a simple
generator that yields the fibonacci sequence:

def fib():
a = b = 1
while True:
yield a
a, b = b, a + b

 fib()
generator object at 0x014B35F8
 it = fib()
 for i in range(10): print it.next(),

1 1 2 3 5 8 13 21 34 55


It's both more efficient and clearer than a function that simply
calculates a list and returns the results. Plus it can actually go on
infinitely, and you just take as many results from it as you need
(also look at itertools for that, particularly takewhile). In practice
it's used mostly for writing __iter__ methods, which is vastly simpler
than a separate iterator class with the next method and everything.
Just write __iter__ as a generator that yields your sequence, and
you're done.

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


Re: [Tutor] Misc question about scoping

2010-06-04 Thread spir
On Fri, 4 Jun 2010 12:26:20 -0400
Tino Dai obe...@gmail.com wrote:

 Also could you give me some instances
 where a generator
 would be used in a real situation? I have already read the stuff on
 doc.python.org about
 generators.

Sure, generally speaking in the programming world, documentation misses the 
first and principle step: *purpose* :-) Why is that stuff intended for?

My point of view is as follows (I don't mean it's _the_ answer):
Generators are useful when not all potential values need be generated (or 
possibly not all). Meaning you perform some task until a condition is met, so 
you don't need all possible map values. A mapping or list comprehension instead 
always creates them all.

A particuliar case where a generator is necessary is the one of an open, 
unlimited,  series, defined by eg a formula, such as cubes. Such a potentially 
infinite series is only broken by a loop break:

def cubes(first):
n = first
while True:
yield n ** 3
n += 1

for cube in cubes(1):
if cube  999:
break
else:
print(cube),

Denis


vit esse estrany ☣

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


Re: [Tutor] treeTraversal, nested return?

2010-06-04 Thread spir
On Fri, 4 Jun 2010 10:15:26 -0700 (PDT)
jjcr...@uw.edu wrote:

 All,
 
 any observations might be helpful. For the display of database contents I 
 have the following problem:
 
 Database querys will return data to me in tuples of the following sort:
 each tuple has a unique id, a parent id, and a type.
 
 a parent id of 0 indicates that the element has no parent.
 not all data elements have children
 image elements have no children and must have a parent
 
 So the contents of the db is a tree of sorts:
 
 (1, 0, work)
 (555, 1, image)
 (556, 1, work)
 (557, 556, image)
 (558, 556, work)
 (559, 558, image)
 (560, 558, work)
 (561, 556, image)
 (562, 556, image)
 (563, 1, work)
 (564, 563, work)
 (565, 1, work)
 
 I have a function that will traverse this tree, summoning lists of tuples 
 at each leaf. Well and good; it took a lot of trial and error, but it was 
 a fine education in tree traversal.
 
 def makeHeirarchy(id):
  id = parentPath(id)[-1]
  rootimages = getImages(id[0])
  rootworks = getWorks(id[0])
  heirarchy = treeTraversal(rootworks, [id, rootimages])
  return heirarchy
 
 ## which calls this recursive function:
 
 def treeTraversal(listIn,result):
  for x in listIn:
  if not getWorks(x[0]):
  result.append(x)
  result.append(getImages(x[0]))
  else:
  result.append(x)
  result.append(getImages(x[0]))
  treeTraversal(getWorks(x[0]), result)
  return result
 
 My problem is that this returns the tuples to me in a flat list.
 I've been trying to work out how I can get it to return some sort of 
 nested structure: nested lists, dictionaries, or html thus:

For an indented output, you're simply forgetting to inform the recursive 
function aobut current indent level. Example (note: a nested sequence is not a 
tree: it has no root):

SPACE , WIDTH, NODE, NL =   , 4 , node , '\n'

def indentOutput(s, level=0):
# fake root
if level == 0:
text = NODE
else:
text = 
# children
level += 1
offset = level * SPACE * WIDTH
for element in s:
if isinstance(element, (tuple,list)):
text += %s%s%s%s %(NL,offset,NODE , 
indentOutput(element, level))
else:
text += %s%s%s %(NL,offset,element)
return text

s = (1,(2,3),4,(5,(6,7),8),9)
print indentOutput(s)

node
1
node
2
3
4
node
5
node
6
7
8
9


Denis


vit esse estrany ☣

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


Re: [Tutor] sockets, servers, clients, broadcasts...?

2010-06-04 Thread Alex Hall
On 6/4/10, Alan Gauld alan.ga...@btinternet.com wrote:

 Alex Hall mehg...@gmail.com wrote

 connect to you as a client. It appears, though, that I need a loop
 to
 have a server make any sense at all, to handle incoming data and
 connection requests.

 You need to listen then process incoming messages then listen some
 more, so yes you will need a loop. Probaqbly an infinite one:

 while True:
  listen()
  if someQuitMessage:
 break
  process()
 rest of my program on hold. Am I safe to use basic threading to
 stick
 the server's loop into a new thread?

 For your game you don't need to do that. A single thread should
 suffice.
The idea is to have every instance of the game start up a server (if
it is configured to do so by the user). So when a client connects to
you, you and the client exchange data (ship positions, board size, and
so on), then each move by you sends results to the client and each
move by the client sends results to you. For example, the client fires
at square A1. As soon as that happens, a toople something like
(shell,a1)
is sent to you, the server. You then see if that was a hit or a miss
on one of your ships and inform the user accordingly. Currently, the
game is based on an accelerator table inside a wx Frame, but I think
what I will do is put the calls to server.send() as required in each
function called by the accelerator table. I am not sure how I would
insert this into the server's loop; as it stands, it seems much more
intuitive to have the server spinning its wheels, waiting for things
to happen. Again, I am very new to all this, so maybe there is a
better way, but I do not know how I would put all the game logic into
a huge while loop. Oh, I am using the toomi library, which provides
basic client and server classes and seems to do what I need, at least
for now.
 But usually a server's listen loop is the main thread and the
 processing
 goes into the sub threads.

 program be able to talk to the server / will the server be able to
 talk to my program from its new home in its separate thread,

 Look at the example in my tutorial. It doesn't use threads but
 services two separate clients without lockup. (Albeit it doesn't
 care about context...)
What is the link for the tutorial? Also, maybe a bit off topic, what
is a context? I dealt with them a lot in Android programming last
semester, but I still do not get them, and I thought they were just an
Android/Java thing.

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



-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] From SQL Blobs to Python Buffers to Actual Files

2010-06-04 Thread Steven D'Aprano
On Sat, 5 Jun 2010 03:51:02 am GoodPotatoes wrote:
 When I write the blob/binary data to a file and give it its original
 file name customer1.rtf, I am expecting the application to read the
 binary data into text,excel,word documents, or whatever application
 created the file originally.

 Example:
 when I write blob1 to customer1.rtf, I get
 xÚµVKsÛ6î93ù{ÈÁîÈ2%Û±cŸlgœdÒvLJ²\J¨ as my first line.

Does that look like Rich Text Format to you? RTF looks like:

{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard
This is some {\b bold} text.\par}

It's a database blob. Writing it to a file called customer1.rtf won't 
magically turn it into RTF any more than writing it to a file 
called puppy.jpg will magically turn it into a picture of a puppy.

Blobs are database-specific binary data, not RTF. The database could be 
doing *anything* to it -- it could be compressing it, or encoding it in 
some way, who knows? You have to convert the data back into RTF before 
writing it. This almost certainly isn't a Python problem, but a 
database problem. Consult your database documentation, and good luck.



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


Re: [Tutor] sockets, servers, clients, broadcasts...?

2010-06-04 Thread ALAN GAULD


 Look at the example in my tutorial. It doesn't use threads but
What is the link for the tutorial? 

In my sig.
Go to the V2 version and look for the Network Programming topic.

 what is a context? I dealt with them a lot in Android programming 

Nothing to do with Android contexts I'm simply using the 
English meaning. In other words my example processes each 
request independent of the client, there is no concept of session 
state. It basically servers up a sequience of numbers on a firs 
come first served basis, it doesn't remember the last number 
served per client and give the next number it just serves the 
next number to whoever asks for one.

In your case you only have one client per server - albeit with 
two servers - so each server only has to deal with a single 
client's request so again no need to keep session state 
(the context), you just look at the model and act accordingly.

HTH,

Alan G
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] Misc question about scoping

2010-06-04 Thread Alan Gauld

Hugo Arts hugo.yo...@gmail.com wrote


[1] http://mitpress.mit.edu/sicp/
[2] 
http://groups.csail.mit.edu/mac/classes/6.001/abelson-sussman-lectures/




And I'd add the superb How to Design Programs:

http://www.htdp.org/

It teaches Scheme programming using a recipe approach
that creates a standard stricture for a function. It then extends
that structure as the examples build in complexity but always
keeping the basic theme.

This book and SICP are two of the best for making you rethink all
you thought you knew about program structure and design!

If you really want to bend your brain in Lisp (Scheme) try
The Little Schemer and its follow up the Seasoned Schemer
It took me 3 attempts to really get to grips with the first and
I'm on my second attempt at the second!

What all these books will do is give you a rational approach
to problem solving for programming that will often work when
more 'conventional' approaches don't. The performance of the
resulting code may not be optimal but it will often give you
the key breakthrough that you can then rewrite more
conventionally into good and fast code. It also often leads to
much more elegant solutions. Good for when you have
something that works but looks a mess... rethink it for
Lisp and see what's different.

--
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] treeTraversal, nested return?

2010-06-04 Thread jjcrump

Matthew,

sorry for responding to you instead of the list

Thanks for trying to tutor the dunderheaded. It is exactly the general 
solution I wish to learn. But how, in the context of my treeTraversal 
function, does one keep track of the depth you're at? Tabs are all very 
well, but what I want is truly a nested structure, and as you point out in 
nitpick that's not what I was showing in my html (though deprecated, 
browsers still render lists like the one I showed).


The simplest and most graphic example would be a nested list of lists, but 
for the life of me I can't work out how to generate that in this context.


Jon



On Fri, 4 Jun 2010, Matthew Wood wrote:


In general, there's 2 solutions to your unflattening problem.
A) The general solution: Keep track of the depth you're at, and add 

tabs/spaces ('\t' * depth) as necessary.



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


[Tutor] Lisphon

2010-06-04 Thread spir
On Fri, 4 Jun 2010 22:33:19 +0200
Hugo Arts hugo.yo...@gmail.com wrote:

 On Fri, Jun 4, 2010 at 6:26 PM, Tino Dai obe...@gmail.com wrote:
 
  LOL, it's actually on the list of things to do. And hear that one will 
  become a
  better programmer once they learn LISP.
 
 
 I most certainly did. There are very few languages as conceptually
 pure as this one. If you ever get it on the top of your list, check
 out these links. [1] is the venerable book structure and
 interpretation of computer programs, commonly regarded as one of the
 best introductory programming books ever written. It teaches scheme.
 [2] is a set of excellent lectures of said book, by its writers. They
 are very good teachers.
 
 [1] http://mitpress.mit.edu/sicp/
 [2] http://groups.csail.mit.edu/mac/classes/6.001/abelson-sussman-lectures/

Thanks for the pointer, Hugo, did not know these lectures. If you do not know 
lisperati, you *must* follow the pointer, if only for the home page: 
http://lisperati.com/.
Also, history of Lisp: http://www-formal.stanford.edu/jmc/history/lisp/lisp.html


Denis


vit esse estrany ☣

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