Re: Please provide a better explanation of tuples and dictionaries

2013-01-30 Thread rusi
On Jan 30, 7:55 am, "Daniel W. Rouse Jr."
 wrote:
> Or, can an anyone provide an example of
> more than a three-line example of a tuple or dictionary?

Have you seen this byt the creator of python -- GvR?
http://www.python.org/doc/essays/graphs.html

> I have recently started learning Python (2.7.3) but need a better
> explanation of how to use tuples and dictionaries.

This is an important question: To start off you need to digest whats
the diff between value oriented and object oriented. Since this is
more to do with paradigm than with a specific language you may read
for example:
http://www.haskell.org/haskellwiki/The_Monad.Reader/Issue3/Functional_Programming_vs_Object_Oriented_Programming

In the python data structure world:
value | object
tuple | list
XX| dictionary
frozen-set | set


>
> I am currently using "Learning Python" by Mark Lutz and David Ascher,
> published by O'Reilly (ISBN 1-56592-464-9)--but I find the explanations
> insufficient and the number of examples to be sparse. I do understand some
> ANSI C programming in addition to Python (and the book often wanders off
> into a comparison of C and Python in its numerous footnotes), but I need a
> better real-world example of how tuples and dictionaries are being used in
> actual Python code.
>
> Any recommendations of a better book that doesn't try to write such compact
> and clever code for a learning book?

> The purpose of my learning Python in this case is not for enterprise level
> or web-based application level testing at this point. I initially intend to
> use it for Software QA Test Automation purposes.
>
> Thanks in advance for any replies.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ways to apply while learning....

2013-01-30 Thread Ritchie Flick
It's perfectly normal that you need to look things up, even the most
seasoned programmer has to look up something at some point.

Finding small projects is often difficult, because many projects grow to
such an extent, that they're simply to difficult to grasp for a beginner
and even for an experienced programmer if the code documentation sucks.

I would suggets reading some python code on http://ww.github.com
Learning through reading other peoples code also helps in learning a
language.


On Wed, Jan 30, 2013 at 12:09 AM, David Hutto wrote:

> On Tue, Jan 29, 2013 at 5:57 PM,   wrote:
> > Hello,
> > I am learning programming as a spare time hobby and learning python
> through codecademy.
> >
> > Today I have downloaded and installed aptana, and found out that
> although I have been progressing for some time now but I do not remember
> how to code and I have to look everything up.
>
> When using different languages to mean client needs,this will be a
> necessity.
>
> >
> > I want to know what is the best way to learn python and some small
> projects that I can make using console
>
> you might need to utilize subrocess, but many ahve their preference.
> (I know there is a long way to develop something for the desktop)
> Do you mean command line app, or with a GUI?
> >
> > Thank you.
> > ps: I am coming from vb6 paradigm.
> > --
> > http://mail.python.org/mailman/listinfo/python-list
>
>
>
> --
> Best Regards,
> David Hutto
> CEO: http://www.hitwebdevelopment.com
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Ritchie Flick
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fonts & Tinker

2013-01-30 Thread Angel
THis one workd fine:

.option_add('*Font', "Heveltica 14") 

Thanks!

Á.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reading data from 2 different files and writing to a single file

2013-01-30 Thread inshu chauhan
On Mon, Jan 28, 2013 at 6:05 PM, Dennis Lee Bieber wrote:

> On Mon, 28 Jan 2013 14:31:31 +0100, inshu chauhan
>  declaimed the following in
> gmane.comp.python.general:
>
> > In the code below I am trying to read 2 files f1 and f2 , extract some
> data
> > from them and then trying to write them into a single file that is 'nf'.
> >
> > import cv
> > f1 = open(r"Z:\modules\Feature_Vectors_300.arff")
> > f2 = open(r"Z:\modules\Feature_Vectors_300_Pclass.arff")
> > nf = open(r"Z:\modules\trial.arff", "w")
> >
> >
> > for l in f1:
> > sp = l.split(",")
>
> If you are going to be splitting on commas, you might want to read
> up on the csv (comma separate values) module
>

The  csv module has many fuctions but not of much use to me and it makes my
programme slower

>
> >
> > if len(sp)!= 12:
> > continue
> > else:
>
> Given the apparent block structure, you could drop the
> continue/else, and more cleanly just use
>

Yeah, Thats Right

>
> if len(sp) == 12:
> > ix = sp[0].strip()
> > iy = sp[1].strip()
> > print ix, iy
> >
> >for s in f2:
>
> It's been mentioned that the indentation is wrong here
>

I dont know why the indentation is wrong ?

>
> > st = s.split(",")
> >
> csv module again
>
> > if len(st)!= 11:
> > continue
> > else:
>
> I'm tempted to repeat the comment on reversing the conditional BUT
>
> > clas = st[10].strip()
> >
> >  print ix, iy, clas
> >  print >> nf, ix, iy, clas
> >
> The indentation of the print statements is not aligned with the
> previous assignment -- the effect is the same however as everything
> under the else is executed anyway.
>
> But as has also been mentioned, ignoring indentation, the apparent
> algorithm you have here is going to process every line of f2 for the
> first line of f1 -- and then for later lines in f1 it will find f2 is at
> the end of file, and do nothing. If it is supposed to process every line
> of f2 for each line of f1, you'll need to rewind f2.
>

For that I added 'Break' statement as suggested by Chris in above mails.

>
> If you mean to match one line of f1 with one line of f2, you do not
> want nested loops. But now you have to define the behavior if one of the
> two files is correct length and the other is not? Do you skip both or
> read the next line from the wrong length file? And how will you handle
> files with different numbers of records.
>

Yes , actually my Prog was like this :
for l in f1:
sp = l.split(",")

if len(sp)!= 12:
continue
else:
ix = sp[0].strip()
iy = sp[1].strip()


for s in f2:
st = s.split(",")

if len(st)!= 11:
continue
else:
clas = st[10].strip()

print ix, iy, clas
print >> nf, ix, iy, clas
break


f1.close()
f2.close()
nf.close()

I actually dont want nested loops but cant find another way to achieve what
I want, But for these files I am sure that they have equal lengths, thats
why I am taking the risk of using nested loops.. Can you suggest any
different way to go around this problem , which could be flexible and
non-errorneous ?


>
>
>
> --
> Wulfraed Dennis Lee Bieber AF6VN
> wlfr...@ix.netcom.comHTTP://wlfraed.home.netcom.com/
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reading data from 2 different files and writing to a single file

2013-01-30 Thread Chris Angelico
On Wed, Jan 30, 2013 at 9:43 PM, inshu chauhan  wrote:
> I actually dont want nested loops but cant find another way to achieve what
> I want, But for these files I am sure that they have equal lengths, thats
> why I am taking the risk of using nested loops.. Can you suggest any
> different way to go around this problem , which could be flexible and
> non-errorneous ?

Easy. Just trim off the header row(s), if you know how many to expect,
and then use zip() as Dave mentioned earlier.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Sorting a hierarchical table (SQL)

2013-01-30 Thread Frank Millman

Hi all

This is not really a python question, but I am hoping that some of you 
can offer some suggestions.


I have a SQL table with hierarchical data. There are two models that can 
be used to represent this - Adjacency Lists and Nested Sets. Here is a 
link to an article that discusses and compares the two approaches -


http://explainextended.com/2009/09/24/adjacency-list-vs-nested-sets-postgresql/

A feature of the Nested Sets model is that a SELECT returns the rows by 
following the links in the structure - root first, followed by its first 
child, followed by *its* first child, until the bottom is reached, then 
any siblings, then up one level to the next child, and so on, until the 
entire tree is exhausted.


I am looking for a way to emulate this behaviour using Adjacency Lists. 
It is not that easy.


The article above shows a way of doing this using an Array. 
Unfortunately that is a PostgreSQL feature not available in all 
databases, so I want to avoid that. Here is the best I have come up with.


For each row, I know the parent id, I know the level (depth in the 
tree), and I know the sequence number - every row has a sequence number 
that is unique within any group of siblings within the tree, always 
starting from zero.


I create a string to be used as a sort key, consisting of the parent's 
sort key, a comma, and the row's sequence number. So the root has a key 
of '0', the first child has '0,0', its first child has '0,0,0', etc.


If there could never be more than 10 siblings, that would work, but if 
it goes over 10, the next key would contain the substring '10', which 
sorts earlier than '2', which would be incorrect.


Therefore, on the assumption that there will never be more that 1 
siblings, I zero-fill each element to a length of 4, so the first key is 
'', the next one is ',', then ',,', etc.


All this is done in SQL, as part of a complicated SELECT statement.

It works, and it would be unusual to have a tree with a depth of more 
than 4 or 5, so I can live with it.


However, it is not pretty. I wondered if anyone can suggest a more 
elegant solution.


Thanks

Frank Millman

--
http://mail.python.org/mailman/listinfo/python-list


pyrudp

2013-01-30 Thread Jorge Alberto Diaz Orozco
can someone give me a link to download pyrudp.
I tried here http://code.google.com/p/pyrudp/ but didn´t worked.

if someone can give me another idea it will be great to. 
I´m traying to make a reliable udp connection

help will be really appreciated



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reading data from 2 different files and writing to a single file

2013-01-30 Thread Dave Angel

On 01/30/2013 05:43 AM, inshu chauhan wrote:

On Mon, Jan 28, 2013 at 6:05 PM, Dennis Lee Bieber wrote:


On Mon, 28 Jan 2013 14:31:31 +0100, inshu chauhan
 declaimed the following in
gmane.comp.python.general:


In the code below I am trying to read 2 files f1 and f2 , extract some

data

from them and then trying to write them into a single file that is 'nf'.

import cv
f1 = open(r"Z:\modules\Feature_Vectors_300.arff")
f2 = open(r"Z:\modules\Feature_Vectors_300_Pclass.arff")
nf = open(r"Z:\modules\trial.arff", "w")


for l in f1:
 sp = l.split(",")


 If you are going to be splitting on commas, you might want to read
up on the csv (comma separate values) module



The  csv module has many fuctions but not of much use to me and it makes my
programme slower





 if len(sp)!= 12:
 continue
 else:


 Given the apparent block structure, you could drop the
continue/else, and more cleanly just use



Yeah, Thats Right



 if len(sp) == 12:

 ix = sp[0].strip()
 iy = sp[1].strip()
 print ix, iy

for s in f2:


 It's been mentioned that the indentation is wrong here



I dont know why the indentation is wrong ?


Your for statement is not lined up with the print that precedes it.  If 
your code were really that way, you'd be getting an indentation error. 
So we assume it's because your email editor is mangling the code.  Post 
in text email, not in html.







 st = s.split(",")


 csv module again


 if len(st)!= 11:
 continue
 else:


 I'm tempted to repeat the comment on reversing the conditional BUT


 clas = st[10].strip()

  print ix, iy, clas
  print >> nf, ix, iy, clas


 The indentation of the print statements is not aligned with the
previous assignment -- the effect is the same however as everything
under the else is executed anyway.

 But as has also been mentioned, ignoring indentation, the apparent
algorithm you have here is going to process every line of f2 for the
first line of f1 -- and then for later lines in f1 it will find f2 is at
the end of file, and do nothing. If it is supposed to process every line
of f2 for each line of f1, you'll need to rewind f2.



For that I added 'Break' statement as suggested by Chris in above mails.



 If you mean to match one line of f1 with one line of f2, you do not
want nested loops. But now you have to define the behavior if one of the
two files is correct length and the other is not? Do you skip both or
read the next line from the wrong length file? And how will you handle
files with different numbers of records.



Yes , actually my Prog was like this :
for l in f1:
 sp = l.split(",")

 if len(sp)!= 12:
 continue
 else:
 ix = sp[0].strip()
 iy = sp[1].strip()


for s in f2:


This is not nested, it's back at the left margin.  Or it could be 
posting wrong because you're still posting in html, instead of plain 
text email.




 st = s.split(",")

 if len(st)!= 11:
 continue
 else:
 clas = st[10].strip()

 print ix, iy, clas
 print >> nf, ix, iy, clas
 break


f1.close()
f2.close()
nf.close()

I actually dont want nested loops but cant find another way to achieve what
I want, But for these files I am sure that they have equal lengths, thats
why I am taking the risk of using nested loops.


You have that backwards.  Because you say you can assume they're the 
same length, you don't need the flexibility (and unreadability) of the 
nested approach.  The zip approach works great, and nested is unnecessary.


. Can you suggest any

different way to go around this problem , which could be flexible and
non-errorneous ?





--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Re: GeoBases: data services and visualization

2013-01-30 Thread guil . genet
Le mardi 29 janvier 2013 22:25:39 UTC+1, Alex Prengere a écrit :
> This new project provides tools to play with geographical data. It also works 
> with non-geographical data, except for map visualizations :). There are 
> embedded data sources in the project, but you can easily play with your own 
> data in addition to the available ones. Files containing data about airports, 
> train stations, countries, ... are loaded, then you can: - performs various 
> types of queries ( find this key, or find keys with this property) - fuzzy 
> searches based on string distance ( find things roughly named like this) - 
> geographical searches ( find things next to this place) - get results on a 
> map, or export it as csv data, or as a Python object This is entirely written 
> in Python. The core part is a Python package, but there is a command line 
> tool as well! For tutorials and documentation, check out 
> http://opentraveldata.github.com/geobases/

Preum's
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: GeoBases: data services and visualization

2013-01-30 Thread guil . genet
Le mercredi 30 janvier 2013 14:28:46 UTC+1, guil@gmail.com a écrit :
> Le mardi 29 janvier 2013 22:25:39 UTC+1, Alex Prengere a écrit : > This new 
> project provides tools to play with geographical data. It also works with 
> non-geographical data, except for map visualizations :). There are embedded 
> data sources in the project, but you can easily play with your own data in 
> addition to the available ones. Files containing data about airports, train 
> stations, countries, ... are loaded, then you can: - performs various types 
> of queries ( find this key, or find keys with this property) - fuzzy searches 
> based on string distance ( find things roughly named like this) - 
> geographical searches ( find things next to this place) - get results on a 
> map, or export it as csv data, or as a Python object This is entirely written 
> in Python. The core part is a Python package, but there is a command line 
> tool as well! For tutorials and documentation, check out 
> http://opentraveldata.github.com/geobases/ Preum's

First
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pyrudp

2013-01-30 Thread Rodrick Brown
On Jan 30, 2013, at 8:12 AM, Jorge Alberto Diaz Orozco
 wrote:

> can someone give me a link to download pyrudp.
> I tried here http://code.google.com/p/pyrudp/ but didn´t worked.
>
> if someone can give me another idea it will be great to.
> I´m traying to make a reliable udp connection

What about the native socket call to SOCK_DGRAM?

Here is a simple example to read messages of a udp socket.

import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 5005

sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))

while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print "received message:", data

>
> help will be really appreciated
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reading data from 2 different files and writing to a single file

2013-01-30 Thread inshu chauhan
On Wed, Jan 30, 2013 at 2:23 PM, Dave Angel  wrote:

> On 01/30/2013 05:43 AM, inshu chauhan wrote:
>
>> On Mon, Jan 28, 2013 at 6:05 PM, Dennis Lee Bieber > >wrote:
>>
>>  On Mon, 28 Jan 2013 14:31:31 +0100, inshu chauhan
>>>  declaimed the following in
>>> gmane.comp.python.general:
>>>
>>>  In the code below I am trying to read 2 files f1 and f2 , extract some

>>> data
>>>
 from them and then trying to write them into a single file that is 'nf'.

 import cv
 f1 = open(r"Z:\modules\Feature_**Vectors_300.arff")
 f2 = open(r"Z:\modules\Feature_**Vectors_300_Pclass.arff")
 nf = open(r"Z:\modules\trial.arff", "w")


 for l in f1:
  sp = l.split(",")

>>>
>>>  If you are going to be splitting on commas, you might want to
>>> read
>>> up on the csv (comma separate values) module
>>>
>>>
>> The  csv module has many fuctions but not of much use to me and it makes
>> my
>> programme slower
>>
>>
>>>
  if len(sp)!= 12:
  continue
  else:

>>>
>>>  Given the apparent block structure, you could drop the
>>> continue/else, and more cleanly just use
>>>
>>>
>> Yeah, Thats Right
>>
>>
>>>  if len(sp) == 12:
>>>
  ix = sp[0].strip()
  iy = sp[1].strip()
  print ix, iy

 for s in f2:

>>>
>>>  It's been mentioned that the indentation is wrong here
>>>
>>>
>> I dont know why the indentation is wrong ?
>>
>
> Your for statement is not lined up with the print that precedes it.  If
> your code were really that way, you'd be getting an indentation error. So
> we assume it's because your email editor is mangling the code.  Post in
> text email, not in html.
>
>
>
>>
>>>   st = s.split(",")

   csv module again
>>>
>>>   if len(st)!= 11:
  continue
  else:

>>>
>>>  I'm tempted to repeat the comment on reversing the conditional
>>> BUT
>>>
>>>   clas = st[10].strip()

   print ix, iy, clas
   print >> nf, ix, iy, clas

   The indentation of the print statements is not aligned with
>>> the
>>> previous assignment -- the effect is the same however as everything
>>> under the else is executed anyway.
>>>
>>>  But as has also been mentioned, ignoring indentation, the
>>> apparent
>>> algorithm you have here is going to process every line of f2 for the
>>> first line of f1 -- and then for later lines in f1 it will find f2 is at
>>> the end of file, and do nothing. If it is supposed to process every line
>>> of f2 for each line of f1, you'll need to rewind f2.
>>>
>>>
>> For that I added 'Break' statement as suggested by Chris in above mails.
>>
>>
>>>  If you mean to match one line of f1 with one line of f2, you do
>>> not
>>> want nested loops. But now you have to define the behavior if one of the
>>> two files is correct length and the other is not? Do you skip both or
>>> read the next line from the wrong length file? And how will you handle
>>> files with different numbers of records.
>>>
>>>
>> Yes , actually my Prog was like this :
>> for l in f1:
>>  sp = l.split(",")
>>
>>  if len(sp)!= 12:
>>  continue
>>  else:
>>  ix = sp[0].strip()
>>  iy = sp[1].strip()
>>
>>
>> for s in f2:
>>
>
> This is not nested, it's back at the left margin.  Or it could be posting
> wrong because you're still posting in html, instead of plain text email.




> Yes My Initial code was not nested at the same time of no use too,  I am
> trying to use zip() now :) :) nyways..
>
>
>   st = s.split(",")
>>
>>  if len(st)!= 11:
>>  continue
>>  else:
>>  clas = st[10].strip()
>>
>>  print ix, iy, clas
>>  print >> nf, ix, iy, clas
>>  break
>>
>>
>> f1.close()
>> f2.close()
>> nf.close()
>>
>> I actually dont want nested loops but cant find another way to achieve
>> what
>> I want, But for these files I am sure that they have equal lengths, thats
>> why I am taking the risk of using nested loops.
>>
>
> You have that backwards.  Because you say you can assume they're the same
> length, you don't need the flexibility (and unreadability) of the nested
> approach.  The zip approach works great, and nested is unnecessary.
>
>
> . Can you suggest any
>
>> different way to go around this problem , which could be flexible and
>> non-errorneous ?
>>
>>
>>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: security quirk

2013-01-30 Thread Martin Musatov
On Jan 29, 8:55 pm, RichD  wrote:
> I read Wall Street Journal, and occasionally check






00commentBegin 01comment
02commentEnd 03 04




();

Open
middle
Close
Open
middle
Close
Open
middle
Close
%%






































> articles on their Web site.  It's mostly free, with some items
> available to subscribers only.  It seems random, which ones
> they block, about 20%.
>
> Anywho, sometimes I use their search utility, the usual author
> or title search, and it blocks, then I look it up on Google, and
> link from there, and it loads!  ok, Web gurus, what's going on?
>
> --
> Rich

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: security quirk

2013-01-30 Thread Gandalf Parker
RichD  contributed wisdom to  news:b968c6c6-5aa9-
4584-bd7a-5b097f17c...@pu9g2000pbc.googlegroups.com:

> Web gurus, what's going on?
> 

That is the fault of the site itself.
If they are going to block access to users then they should also block 
access to the automated spiders that hit the site to collect data.
-- 
http://mail.python.org/mailman/listinfo/python-list


looping versus comprehension

2013-01-30 Thread Robin Becker
An email in reportlab-us...@reportlab.com claimed that the following loop in a 
charting module was taking a long time



I use ReportLab 2.6 but I also found the problem in ReportLab daily from 
01/29/2013 in /src/reportlab/graphics/charts/lineplots.py:
276  # Iterate over data columns.
277  if self.joinedLines:
278  points = []
279  for xy in row:
280  points += [xy[0], xy[1]]

If I use a list comprehension instead, the plot is generated within seconds or 
minutes:
278  points = [[xy[0], xy[1]] for xy in row]


however, when I tried an experiment in python 2.7 using the script below I find 
that the looping algorithms perform better. A naive loop using list += list 
would appear to be an O(n**2) operation, but python seems to be doing better 
than that. Also why does the append version fail so dismally. Is my test coded 
wrongly or is pre-allocation of the list making this better than expected?


C:\code\tests>tpoints 86000 86
 START n=86000 
existing algorithm took 0.08 seconds
existing algorithm using list took 0.12 seconds
existing algorithm using list assuming length 2 took 0.12 seconds
map(list,row) took 0.16 seconds
[list(xy) for xy in row] took 0.28 seconds
[[xy[0],xy[1]] for xy in row] took 0.22 seconds
append algorithm took 0.19 seconds
 END   n=86000 


 START n=86 
existing algorithm took 0.86 seconds
existing algorithm using list took 1.33 seconds
existing algorithm using list assuming length 2 took 1.25 seconds
map(list,row) took 3.44 seconds
[list(xy) for xy in row] took 3.03 seconds
[[xy[0],xy[1]] for xy in row] took 2.70 seconds
append algorithm took 2.48 seconds
 END   n=86 

#
import sys, time
def main(n):
print 20*'#','START n=%s'%n,20*'#'
row = [(i,i+1) for i in xrange(2*n)]
print 'existing algorithm',
t0 = time.time()
points = []
for xy in row:
points += [xy[0],xy[1]]
t1 = time.time()
print 'took %.2f seconds' % (t1-t0)

print 'existing algorithm using list',
t0 = time.time()
points = []
for xy in row:
points += list(xy[:2])
t1 = time.time()
print 'took %.2f seconds' % (t1-t0)

print 'existing algorithm using list assuming length 2',
t0 = time.time()
points = []
for xy in row:
points += list(xy)
t1 = time.time()
print 'took %.2f seconds' % (t1-t0)

print 'map(list,row)',
t0 = time.time()
points = map(list,row)
t1 = time.time()
print 'took %.2f seconds' % (t1-t0)

print '[list(xy) for xy in row]',
t0 = time.time()
points = [list(xy) for xy in row]
t1 = time.time()
print 'took %.2f seconds' % (t1-t0)

print '[[xy[0],xy[1]] for xy in row]',
t0 = time.time()
points = [[xy[0],xy[1]] for xy in row]
t1 = time.time()
print 'took %.2f seconds' % (t1-t0)

print 'append algorithm',
t0 = time.time()
points = [].append
for xy in row:
points([xy[0],xy[1]])
points = points.__self__
t1 = time.time()
print 'took %.2f seconds' % (t1-t0)

print 20*'#','END   n=%s'%n,20*'#','\n\n'

if __name__=='__main__':
if len(sys.argv)==1:
N = [86000]
else:
N = map(int,sys.argv[1:])
for n in N:
main(n)
#
--
Robin Becker

--
http://mail.python.org/mailman/listinfo/python-list


Re: looping versus comprehension

2013-01-30 Thread Chris Angelico
On Thu, Jan 31, 2013 at 1:58 AM, Robin Becker  wrote:
> however, when I tried an experiment in python 2.7 using the script below I
> find that the looping algorithms perform better. A naive loop using list +=
> list would appear to be an O(n**2) operation, but python seems to be doing
> better than that. Also why does the append version fail so dismally. Is my
> test coded wrongly or is pre-allocation of the list making this better than
> expected?

First off, are you aware that your first three blocks of code and your
last four produce different results? The first ones flatten the list,
the others simply convert tuples to lists. With n = 3:

>>> points = []
>>> for xy in row:
points += [xy[0],xy[1]]
>>> points
[0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6]
>>> map(list,row)
[[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6]]

Once that's sorted out, then timings can be played with. But it's
worth noting that list appending is not going to be O(N*N), because
it's going to allow room for expansion.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pyrudp

2013-01-30 Thread Jorge Alberto Diaz Orozco
I´ve tried it but it´s not reliable. Datagrams can arive disorganised or just 
not arive.
Some programmers said I most use TCP, but I need to use UDP.
that´s why I need pyrudp, but I can not find it.

On Jan 30, 2013, at 8:12 AM, Jorge Alberto Diaz Orozco

What about the native socket call to SOCK_DGRAM?

Here is a simple example to read messages of a udp socket.

import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 5005

sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))

while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print "received message:", data
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pyrudp

2013-01-30 Thread Michael Torrie
On 01/30/2013 10:02 AM, Jorge Alberto Diaz Orozco wrote:
> I´ve tried it but it´s not reliable. Datagrams can arive disorganised or just 
> not arive.
> Some programmers said I most use TCP, but I need to use UDP.
> that´s why I need pyrudp, but I can not find it.

Excuse me for chuckling, but your complaint that UDP packets can "arive
disorganised or just not arive" describes exactly what UDP does by
design! If you need to use UDP then you will have to live with this.  I
always consider UDP to stand for "UNRELIABLE datagram packets."  They
can and do come in any order, or not at all.  That's exactly the
expected behavior of UDP.

If you want to build a reliable connection on top of UDP you'll have to
invent some sort of tcp-like protocol that uses UDP packets.  An example
of a real all that does this is openvpn.  The Bell Labs Plan 9 RUDP
protocol is probably what you were shooting for by trying to use the
non-existent pyrudp code.  But I fear you'll have to implement it
yourself.  pyrudp is an empty project on Google Code.  The owner
intended to develop some code but never did.

-- 
http://mail.python.org/mailman/listinfo/python-list


how to use subprocess to execute an exe with args and an output

2013-01-30 Thread noydb
I am looking for some guidance on using subprocess to execute an EXE with 
arguments and an output.  The below code works in that it returns a 0 exit 
code, but no output file is created.  I have tried a few different versions of 
this code (used Popen instead, some stderr/stdout), but no luck.  Can anyone 
offer an explanation or suggestion?  (GPSBabel is freeware)
Python 2.7 on windows7 64-bit

import subprocess
subprocess.call([r"C:\Program Files (x86)\GPSBabel\gpsbabel.exe",
"-i", "gdb", "-f", r"C:\Temp\GDBdata\testgps28.gdb",
"-o", "gpx", r"C:\Temp\gpx\test28output.gpx"])

If I use this below, I get a returncode of 1, exit code of 0.
import subprocess
x = subprocess.Popen([r"C:\Program Files (x86)\GPSBabel\gpsbabel.exe",
"-i", "gdb", "-f", r"C:\Temp\GDBdata\testgps28.gdb",
"-o", "gpx", r"C:\Temp\gpx\test28output.gpx",
"shell=True", "stdout=subprocess.PIPE", 
"stderr=subprocess.PIPE"])

x.wait()
print x.returncode

Thanks in advance for any help
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to use subprocess to execute an exe with args and an output

2013-01-30 Thread noydb
To add on, I need to eventually write a script that takes input, feeds it into 
this exe, and takes the output file to perform more 'tools'/manipulations on 
it.  Is call or Popen called for, why?  Or maybe some other module???
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to use subprocess to execute an exe with args and an output

2013-01-30 Thread Chris Rebert
On Wed, Jan 30, 2013 at 9:15 AM, noydb  wrote:
> I am looking for some guidance on using subprocess to execute an EXE with 
> arguments and an output.  The below code works in that it returns a 0 exit 
> code, but no output file is created.  I have tried a few different versions 
> of this code (used Popen instead, some stderr/stdout), but no luck.  Can 
> anyone offer an explanation or suggestion?  (GPSBabel is freeware)
> Python 2.7 on windows7 64-bit
>
> import subprocess
> subprocess.call([r"C:\Program Files (x86)\GPSBabel\gpsbabel.exe",
> "-i", "gdb", "-f", r"C:\Temp\GDBdata\testgps28.gdb",
> "-o", "gpx", r"C:\Temp\gpx\test28output.gpx"])

If my cursory reading of GPSBabel's documentation is right, you're
missing a "-F" before the output filepath. Try:

subprocess.call([
r"C:\Program Files (x86)\GPSBabel\gpsbabel.exe",
"-i", "gdb",
"-f", r"C:\Temp\GDBdata\testgps28.gdb",
"-o", "gpx",
"-F", r"C:\Temp\gpx\test28output.gpx",
])

Regards,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: looping versus comprehension

2013-01-30 Thread Robin Becker

On 30/01/2013 15:49, Chris Angelico wrote:

On Thu, Jan 31, 2013 at 1:58 AM, Robin Becker  wrote:

however, when I tried an experiment in python 2.7 using the script below I
find that the looping algorithms perform better. A naive loop using list +=
list would appear to be an O(n**2) operation, but python seems to be doing
better than that. Also why does the append version fail so dismally. Is my
test coded wrongly or is pre-allocation of the list making this better than
expected?


First off, are you aware that your first three blocks of code and your
last four produce different results? The first ones flatten the list,
the others simply convert tuples to lists. With n = 3:


points = []
for xy in row:

 points += [xy[0],xy[1]]

points

[0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6]

map(list,row)

[[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6]]

Once that's sorted out, then timings can be played with. But it's
worth noting that list appending is not going to be O(N*N), because
it's going to allow room for expansion.

ChrisA



No I missed that :( the list is a flattened one.

That'll teach me not to copy the code from the users without checking. Now you 
point it out it's clear that his code is doing something different. Presumably 
it's not drawing the same thing at all :) no wonder it got much faster.

--
Robin Becker
--
http://mail.python.org/mailman/listinfo/python-list


Re: pyrudp

2013-01-30 Thread Rob Day
Have you seen http://pyraknet.slowchop.com/? It appears to do a similar thing.

On 30 January 2013 17:02, Jorge Alberto Diaz Orozco
 wrote:
> I´ve tried it but it´s not reliable. Datagrams can arive disorganised or just 
> not arive.
> Some programmers said I most use TCP, but I need to use UDP.
> that´s why I need pyrudp, but I can not find it.
>
> On Jan 30, 2013, at 8:12 AM, Jorge Alberto Diaz Orozco
>
> What about the native socket call to SOCK_DGRAM?
>
> Here is a simple example to read messages of a udp socket.
>
> import socket
> UDP_IP = "127.0.0.1"
> UDP_PORT = 5005
>
> sock = socket.socket(socket.AF_INET, # Internet
> socket.SOCK_DGRAM) # UDP
> sock.bind((UDP_IP, UDP_PORT))
>
> while True:
> data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
> print "received message:", data
> --
> http://mail.python.org/mailman/listinfo/python-list



-- 
Robert K. Day
robert@merton.oxon.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to use subprocess to execute an exe with args and an output

2013-01-30 Thread MRAB

On 2013-01-30 17:15, noydb wrote:

I am looking for some guidance on using subprocess to execute an EXE with 
arguments and an output.  The below code works in that it returns a 0 exit 
code, but no output file is created.  I have tried a few different versions of 
this code (used Popen instead, some stderr/stdout), but no luck.  Can anyone 
offer an explanation or suggestion?  (GPSBabel is freeware)
Python 2.7 on windows7 64-bit

import subprocess
subprocess.call([r"C:\Program Files (x86)\GPSBabel\gpsbabel.exe",
 "-i", "gdb", "-f", r"C:\Temp\GDBdata\testgps28.gdb",
 "-o", "gpx", r"C:\Temp\gpx\test28output.gpx"])

If I use this below, I get a returncode of 1, exit code of 0.
import subprocess
x = subprocess.Popen([r"C:\Program Files (x86)\GPSBabel\gpsbabel.exe",
 "-i", "gdb", "-f", r"C:\Temp\GDBdata\testgps28.gdb",
 "-o", "gpx", r"C:\Temp\gpx\test28output.gpx",
 "shell=True", "stdout=subprocess.PIPE", 
"stderr=subprocess.PIPE"])

x.wait()
print x.returncode

Thanks in advance for any help


The second example is incorrect. The parts starting from "shell" are
supposed to be further arguments for Popen itself, not something passed
to "gpsbabel.exe":

x = subprocess.Popen([r"C:\Program Files (x86)\GPSBabel\gpsbabel.exe",
 "-i", "gdb", "-f", r"C:\Temp\GDBdata\testgps28.gdb",
 "-o", "gpx", r"C:\Temp\gpx\test28output.gpx"],
 shell=True, stdout=subprocess.PIPE,
 stderr=subprocess.PIPE)

--
http://mail.python.org/mailman/listinfo/python-list


Re: how to use subprocess to execute an exe with args and an output

2013-01-30 Thread noydb
oh DUH!  that was it, just missing the -F.  Thank-you!!

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: security quirk

2013-01-30 Thread RichD
On Jan 30, Gandalf  Parker 
wrote:
> > Web gurus, what's going on?
>
> That is the fault of the site itself.
> If they are going to block access to users then they should also block
> access to the automated spiders that hit the site to collect data.

well yeah, but what's going on, under the hood?
How does it get confused?  How could this
happen?  I'm looking for some insight, regarding a
hypothetical programmimg glitch -


--
Rich
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: security quirk

2013-01-30 Thread Joel Goldstick
On Wed, Jan 30, 2013 at 2:39 PM, RichD  wrote:

> On Jan 30, Gandalf  Parker 
> wrote:
> > > Web gurus, what's going on?
> >
> > That is the fault of the site itself.
> > If they are going to block access to users then they should also block
> > access to the automated spiders that hit the site to collect data.
>
> well yeah, but what's going on, under the hood?
> How does it get confused?  How could this
> happen?  I'm looking for some insight, regarding a
> hypothetical programmimg glitch -
>
>
> --
> Rich
> --
>
> As was pointed out, this really is off topic for this group.  You might
try googling.  The NYTimes makes articles available by adding a parameter
to the tail of the url I believe


-- 
Joel Goldstick
http://joelgoldstick.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pyrudp

2013-01-30 Thread Jorge Alberto Diaz Orozco
I want to use a reliable UDP connection like you say, a TCP like connection but 
over UDP. thaks for your recomendation, if I get good results I promise to 
share them.

- Original Message -
From: "Michael Torrie" 
To: python-list@python.org
Sent: Wednesday, January 30, 2013 9:16:15 AM
Subject: Re: pyrudp

Excuse me for chuckling, but your complaint that UDP packets can "arive
disorganised or just not arive" describes exactly what UDP does by
design! If you need to use UDP then you will have to live with this.  I
always consider UDP to stand for "UNRELIABLE datagram packets."  They
can and do come in any order, or not at all.  That's exactly the
expected behavior of UDP.

If you want to build a reliable connection on top of UDP you'll have to
invent some sort of tcp-like protocol that uses UDP packets.  An example
of a real all that does this is openvpn.  The Bell Labs Plan 9 RUDP
protocol is probably what you were shooting for by trying to use the
non-existent pyrudp code.  But I fear you'll have to implement it
yourself.  pyrudp is an empty project on Google Code.  The owner
intended to develop some code but never did.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to use subprocess to execute an exe with args and an output

2013-01-30 Thread Terry Reedy

On 1/30/2013 1:11 PM, MRAB wrote:

On 2013-01-30 17:15, noydb wrote:

I am looking for some guidance on using subprocess to execute an EXE
with arguments and an output.  The below code works in that it returns
a 0 exit code, but no output file is created.  I have tried a few
different versions of this code (used Popen instead, some
stderr/stdout), but no luck.  Can anyone offer an explanation or
suggestion?  (GPSBabel is freeware)
Python 2.7 on windows7 64-bit

import subprocess
subprocess.call([r"C:\Program Files (x86)\GPSBabel\gpsbabel.exe",
 "-i", "gdb", "-f", r"C:\Temp\GDBdata\testgps28.gdb",
 "-o", "gpx", r"C:\Temp\gpx\test28output.gpx"])

If I use this below, I get a returncode of 1, exit code of 0.
import subprocess
x = subprocess.Popen([r"C:\Program Files (x86)\GPSBabel\gpsbabel.exe",
 "-i", "gdb", "-f", r"C:\Temp\GDBdata\testgps28.gdb",
 "-o", "gpx", r"C:\Temp\gpx\test28output.gpx",
 "shell=True", "stdout=subprocess.PIPE",
"stderr=subprocess.PIPE"])

x.wait()
print x.returncode

Thanks in advance for any help


The second example is incorrect. The parts starting from "shell" are
supposed to be further arguments for Popen itself, not something passed
to "gpsbabel.exe":

x = subprocess.Popen([r"C:\Program Files (x86)\GPSBabel\gpsbabel.exe",
  "-i", "gdb", "-f", r"C:\Temp\GDBdata\testgps28.gdb",
  "-o", "gpx", r"C:\Temp\gpx\test28output.gpx"],
  shell=True, stdout=subprocess.PIPE,
  stderr=subprocess.PIPE)



and it is apparently best to not use shell=True unless actually needed 
for shell processing, which I do not think is the case here.


--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


Re: security quirk

2013-01-30 Thread Big Bad Bob

On 01/29/13 20:55, RichD so wittily quipped:

I read Wall Street Journal, and occasionally check
articles on their Web site.  It's mostly free, with some items
available to subscribers only.  It seems random, which ones
they block, about 20%.

Anywho, sometimes I use their search utility, the usual author
or title search, and it blocks, then I look it up on Google, and
link from there, and it loads!  ok, Web gurus, what's going on?


in my last post, I quoted an article from 'The Register' where they talk 
about how Facebook (literally) "broke" that feature.


[this works in a LOT of places, but sometimes you have to enable cookies 
or javascript to actually see the content]


--
http://mail.python.org/mailman/listinfo/python-list


derived class name in python 2.6/2.7

2013-01-30 Thread Sells, Fred
This is simple, but I just cannot find it after quite a bit of searching

I have this basic design

class  A:
def __init__(self):
print 'I am an instance of ', self.__class__.name

class B(A):
pass


X = B
I would like this to print "I am an instance of B"  but I keep getting A.  Can 
someone help me out here.

-- 
http://mail.python.org/mailman/listinfo/python-list


A new script which creates Python 3.3 venvs with Distribute and pip installed in them

2013-01-30 Thread Vinay Sajip
Python 3.3 includes a script, pyvenv, which is used to create virtual 
environments. However, Distribute and pip are not installed in such 
environments - because, though they are popular, they are third-party packages 
- not part of Python.

The Python 3.3 venv machinery allows customisation of virtual environments 
fairly readily. To demonstrate how to do this, and to provide at the same time 
a script which might be useful to people, I've created a script, pyvenvex.py, at

https://gist.github.com/4673395

which extends the pyvenv script to not only create virtual environments, but to 
also install Distribute and pip into them. The script needs Python 3.3, and one 
way to use it is:

1. Download the script to a directory in your path, and (on Posix platforms) 
make it executable.
2. Add a shebang line at the top of your script, pointing to your Python 3.3 
interpreter (Posix, and also Windows if you have the PEP 397 launcher which is 
part of Python 3.3 on Windows).
3. Run the pyvenvex script to create your virtual environments, in place of 
pyvenv, when you want Distribute and pip to be installed for you (this is how 
virtualenv sets up environments it creates). You can run the script with -h to 
see the command line options available, which are a superset of the pyvenv 
script.

Regards,

Vinay Sajip
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: derived class name in python 2.6/2.7

2013-01-30 Thread Oscar Benjamin
On 30 January 2013 20:05, Sells, Fred  wrote:
> This is simple, but I just cannot find it after quite a bit of searching
>
> I have this basic design
>
> class  A:
> def __init__(self):
> print 'I am an instance of ', self.__class__.name

Did you mean to use __name__ instead of name?

>
> class B(A):
> pass
>
> X = B

I assume that this is
X = B()

> I would like this to print "I am an instance of B"  but I keep getting A.  
> Can someone help me out here.

If you make those two changes then it should do what you want.


Oscar
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: derived class name in python 2.6/2.7

2013-01-30 Thread Dave Angel

On 01/30/2013 03:05 PM, Sells, Fred wrote:

This is simple, but I just cannot find it after quite a bit of searching

I have this basic design

class  A:
def __init__(self):
print 'I am an instance of ', self.__class__.name

class B(A):
pass


X = B
I would like this to print "I am an instance of B"  but I keep getting A.  Can 
someone help me out here.



Why would creating an alias for class B execute the initializer for 
either class?


perhaps you meant:

x = B()

BTW, since you're on 2.x python, you should derive A from object. 
Otherwise it's an old-style class.


--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Re: A new script which creates Python 3.3 venvs with Distribute and pip installed in them

2013-01-30 Thread Ian Kelly
On Wed, Jan 30, 2013 at 1:09 PM, Vinay Sajip  wrote:
> Python 3.3 includes a script, pyvenv, which is used to create virtual 
> environments. However, Distribute and pip are not installed in such 
> environments - because, though they are popular, they are third-party 
> packages - not part of Python.
>
> The Python 3.3 venv machinery allows customisation of virtual environments 
> fairly readily. To demonstrate how to do this, and to provide at the same 
> time a script which might be useful to people, I've created a script, 
> pyvenvex.py, at
>
> https://gist.github.com/4673395
>
> which extends the pyvenv script to not only create virtual environments, but 
> to also install Distribute and pip into them. The script needs Python 3.3, 
> and one way to use it is:
>
> 1. Download the script to a directory in your path, and (on Posix platforms) 
> make it executable.
> 2. Add a shebang line at the top of your script, pointing to your Python 3.3 
> interpreter (Posix, and also Windows if you have the PEP 397 launcher which 
> is part of Python 3.3 on Windows).
> 3. Run the pyvenvex script to create your virtual environments, in place of 
> pyvenv, when you want Distribute and pip to be installed for you (this is how 
> virtualenv sets up environments it creates). You can run the script with -h 
> to see the command line options available, which are a superset of the pyvenv 
> script.

I have a shell script for this:

#!/bin/sh
python3 -m venv $1
cd $1
. bin/activate
wget http://python-distribute.org/distribute_setup.py
python distribute_setup.py
bin/easy_install pip
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pyrudp

2013-01-30 Thread garabik-news-2005-05
Jorge Alberto Diaz Orozco  wrote:
> I want to use a reliable UDP connection like you say, a TCP like
> connection but over UDP. thaks for your recomendation, if I get good
> results I promise to share them.
>

utalk (long since disappeared from the surface of the internet) did have
such an implementation, and I once wrote a half-cooked python
implementation of its semi-reliable protocol over UDP.

Nowadays, I'd recommend using openvpn, or perhaps investigate the
possibility of PPP over UDP, or maybe look into SCTP.

-- 
 ---
| Radovan Garabík http://kassiopeia.juls.savba.sk/~garabik/ |
| __..--^^^--..__garabik @ kassiopeia.juls.savba.sk |
 ---
Antivirus alert: file .signature infected by signature virus.
Hi! I'm a signature virus! Copy me into your signature file to help me spread!
-- 
http://mail.python.org/mailman/listinfo/python-list


help

2013-01-30 Thread aramildaern
Hi everyone! I don't mean to intrude, but ive heard great things about this 
group and ive stumped myself with my python code.

heres my code:
#! /usr/bin/python

import sys

global labelList 
labelList= dict()

global counter
counter = 0

def execute(line):
if line.find("::print") != -1:
stripPrint = line.find("::print")
startPrint = line.find('"', stripPrint)
stopPrint = line.find('"', startPrint + 1)
printSection = line[startPrint + 1 : stopPrint]
print(printSection)

if line.find("::label") != -1:
stripLabel = line.find("::label")
startLabel = line.find(' ', stripLabel)
stopLabel = line.find('--', startLabel + 1)
label = line[startLabel + 1 : stopLabel]
line.strip("\r\n")
labelList[label] = counter

if len(sys.argv) < 2:
print("error: no input files")
print("compilation terminated")

else:
fileName = sys.argv[1]
jadeFile = open(fileName, 'r')

for line in jadeFile:
counter = counter + 1
execute(line)

jadeFile.close()

i = 0

while i < len(labelList):
print(labelList.keys()[i], ":", labelList.values()[i])
i = i + 1


and its giving me a bunch of errors thanks for the help in advance!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help

2013-01-30 Thread Ian Kelly
On Wed, Jan 30, 2013 at 2:16 PM,   wrote:
> Hi everyone! I don't mean to intrude, but ive heard great things about this 
> group and ive stumped myself with my python code.
>
> heres my code:

It would be helpful if you would also include the error that you get
when trying to run the code.

> global labelList
> labelList= dict()
>
> global counter
> counter = 0

That said, these lines stick out as being obviously wrong.  The global
statement is used within the context of a function to declare that a
name used within that function has global scope rather than local
scope.  There is no meaning to using the statement at the module level
like this -- Python already knows the name is global, because it's not
being used within a function -- and I'd be surprised if this didn't
result in a SyntaxError.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help

2013-01-30 Thread Joel Goldstick
On Wed, Jan 30, 2013 at 4:16 PM,  wrote:

> Hi everyone! I don't mean to intrude, but ive heard great things about
> this group and ive stumped myself with my python code.
>

No intrusion.  That is what the list is for.

Two points:

You should use a descriptive subject line -- "Help" isn't really
descriptive.
You should run your code, cut and paste the traceback you get showing the
errors.  This is extremely useful for people to help you with, and as you
program, you will learn how useful it can be to find problems on your own.

So, do that, and come back with that info

>
> heres my code:
> #! /usr/bin/python
>
> import sys
>
> global labelList
> labelList= dict()
>
> global counter
> counter = 0
>
> def execute(line):
> if line.find("::print") != -1:
> stripPrint = line.find("::print")
> startPrint = line.find('"', stripPrint)
> stopPrint = line.find('"', startPrint + 1)
> printSection = line[startPrint + 1 : stopPrint]
> print(printSection)
>
> if line.find("::label") != -1:
> stripLabel = line.find("::label")
> startLabel = line.find(' ', stripLabel)
> stopLabel = line.find('--', startLabel + 1)
> label = line[startLabel + 1 : stopLabel]
> line.strip("\r\n")
> labelList[label] = counter
>
> if len(sys.argv) < 2:
> print("error: no input files")
> print("compilation terminated")
>
> else:
> fileName = sys.argv[1]
> jadeFile = open(fileName, 'r')
>
> for line in jadeFile:
> counter = counter + 1
> execute(line)
>
> jadeFile.close()
>
> i = 0
>
> while i < len(labelList):
> print(labelList.keys()[i], ":", labelList.values()[i])
> i = i + 1
>
>
> and its giving me a bunch of errors thanks for the help in advance!
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Joel Goldstick
http://joelgoldstick.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pyrudp

2013-01-30 Thread Dave Angel

On 01/30/2013 02:55 PM, Jorge Alberto Diaz Orozco wrote:

I want to use a reliable UDP connection like you say, a TCP like connection but 
over UDP. thaks for your recomendation, if I get good results I promise to 
share them.



It's nice to "want" but what is your actual condition/problem?  Are you 
trying to re-invent the wheel, implement a senior project, or are you 
trying to work around some administrative or technical restriction 
against tcp?  What's the use case?


Do you have control over both ends?  Are you writing both ends of the 
imitation tcp you're writing?


One of the car magazines published a road test of the Mercedes GT 
(garbage truck).  It had plenty of power, but its top speed was 6 mph, 
and it leaned horribly around the pylon obstacle course.  Not what I'd 
take to the race track.


Point is that a definitive specification of requirements will be much 
more useful than a simple wanna.


--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Re: A new script which creates Python 3.3 venvs with Distribute and pip installed in them

2013-01-30 Thread Vinay Sajip
Ian Kelly  gmail.com> writes:

> 
> I have a shell script for this:
> 

Sure - there's a similar one at

https://gist.github.com/4591655

The main purpose of the script was to illustrate how to subclass 
venv.EnvBuilder,
and I've added it as an example to the 3.3 and in-development documentation:

http://docs.python.org/3/library/venv.html#an-example-of-extending-envbuilder

Doing it in Python means that it runs cross-platform, offers a few benefits
such as command line help, or the option to install Distribute but not pip.

Regards,

Vinay Sajip

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help

2013-01-30 Thread Chris Angelico
On Thu, Jan 31, 2013 at 8:16 AM,   wrote:
> Hi everyone! I don't mean to intrude, but ive heard great things about this 
> group and ive stumped myself with my python code.

Hi! As others have said, this is no intrusion, but it'd help a lot if
you posted your errors and used a more useful subject line.
Additionally, when you post, can you give some example lines from the
file? This part of your code is impossible for us to simulate:

> fileName = sys.argv[1]
> jadeFile = open(fileName, 'r')
>
> for line in jadeFile:
> counter = counter + 1
> execute(line)
>
> jadeFile.close()

But this much I can suggest:

> i = 0
>
> while i < len(labelList):
> print(labelList.keys()[i], ":", labelList.values()[i])
> i = i + 1

Instead of iterating with a counter, you can iterate with a Python 'for' loop.

for label,count in labelList.items():
print(label,":",count)

It's that simple!

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pyrudp

2013-01-30 Thread Chris Angelico
On Thu, Jan 31, 2013 at 6:55 AM, Jorge Alberto Diaz Orozco
 wrote:
> I want to use a reliable UDP connection like you say, a TCP like connection 
> but over UDP. thaks for your recomendation, if I get good results I promise 
> to share them.

To get something reliable over UDP, you're going to need to
acknowledge everything you're sent, and if you don't hear back an
acknowledgement, re-send. Basically reimplement TCP, or parts of it.
Why do you need to use UDP?

I've used UDP for a number of purposes, but usually in a "fire and
forget" system. For instance, my latest use of it was a peer-to-peer
self-healing network; each node would broadcast a periodic UDP packet
saying "Hi, I'm here, and here's my current status", and each node
would keep track of the timestamp when it last received such a packet
from each known IP address. If the time-since-last-received exceeds
three broadcast intervals, the node is considered to be dead. But for
this to work, I have to not care about individual packet loss; there
is no data in the packet that won't be repeated in the next one. This
is a reliable *system* built on UDP.

Can you explain your goals and restrictions? Might help us figure out
how to advise.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: security quirk

2013-01-30 Thread Auric__
Martin Musatov wrote:

> On Jan 29, 8:55 pm, RichD  wrote:
>> I read Wall Street Journal, and occasionally check
> 
[snip]
> 
> 

Ignoring the big ol' unneccessary crosspost... What the fuck?

-- 
Oooh, I just learned a new euphemism.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pyrudp

2013-01-30 Thread Jorge Alberto Diaz Orozco
I have restrictions in my system that does not allow me to use TCP, so I want 
to make a pipe over UDP imitating TCP behavior.
I have control over both endpoints, and I´m writing both of them.
I just don´t want to re-invent the wheel and I´m looking for a reliable UDP 
sockets implementation for Python so I can start from there.

_

It's nice to "want" but what is your actual condition/problem?  Are you 
trying to re-invent the wheel, implement a senior project, or are you 
trying to work around some administrative or technical restriction 
against tcp?  What's the use case?

Do you have control over both ends?  Are you writing both ends of the 
imitation tcp you're writing?

One of the car magazines published a road test of the Mercedes GT 
(garbage truck).  It had plenty of power, but its top speed was 6 mph, 
and it leaned horribly around the pylon obstacle course.  Not what I'd 
take to the race track.

Point is that a definitive specification of requirements will be much 
more useful than a simple wanna.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pyrudp

2013-01-30 Thread Chris Angelico
On Thu, Jan 31, 2013 at 11:04 AM, Jorge Alberto Diaz Orozco
 wrote:
> I have restrictions in my system that does not allow me to use TCP, so I want 
> to make a pipe over UDP imitating TCP behavior.
> I have control over both endpoints, and I´m writing both of them.
> I just don´t want to re-invent the wheel and I´m looking for a reliable UDP 
> sockets implementation for Python so I can start from there.

Then... I think the place to start is here:

http://www.ietf.org/rfc/rfc793.txt

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


confusion with decorators

2013-01-30 Thread Jason Swails
Hello,

I was having some trouble understanding decorators and inheritance and all
that.  This is what I was trying to do:

# untested
class A(object):
   def _protector_decorator(fcn):
  def newfcn(self, *args, **kwargs):
 return fcn(self, *args, **kwargs)
  return newfcn

   @_protector_decorator
   def my_method(self, *args, **kwargs):
  """ do something here """

class B(A):
   def _protector_decorator(fcn):
  def newfcn(self, *args, **kwargs):
 raise MyException('I do not want B to be able to access the
protected functions')
  return newfcn

The goal of all that was to be able to change the behavior of my_method
inside class B simply by redefining the decorator. Basically, what I want
is B.my_method() to be decorated by B._protector_decorator, but in the code
I'm running it's decorated by A._protector_decorator.

I presume this is because once the decorator is applied to my_method in
class A, A.my_method is immediately bound to the new, 'decorated' function,
which is subsequently inherited (and not decorated, obviously), by B.

Am I correct here?  My workaround was to simply copy the method from class
A to class B, after which B._protector_decorator decorated the methods in
B.  While this doesn't make the use of decorators completely pointless (the
decorators actually do something in each class, it's just different), it
does add a bunch of code duplication which I was at one point hopeful to
avoid.

I'm still stumbling around with decorators a little, but this exercise has
made them a lot clearer to me.

Thanks!
Jason
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: confusion with decorators

2013-01-30 Thread Dave Angel

On 01/30/2013 07:34 PM, Jason Swails wrote:

Hello,

I was having some trouble understanding decorators and inheritance and all
that.  This is what I was trying to do:

# untested
class A(object):
def _protector_decorator(fcn):
   def newfcn(self, *args, **kwargs):
  return fcn(self, *args, **kwargs)
   return newfcn

@_protector_decorator
def my_method(self, *args, **kwargs):
   """ do something here """

class B(A):
def _protector_decorator(fcn):
   def newfcn(self, *args, **kwargs):
  raise MyException('I do not want B to be able to access the
protected functions')
   return newfcn

The goal of all that was to be able to change the behavior of my_method
inside class B simply by redefining the decorator. Basically, what I want
is B.my_method() to be decorated by B._protector_decorator, but in the code
I'm running it's decorated by A._protector_decorator.

I presume this is because once the decorator is applied to my_method in
class A, A.my_method is immediately bound to the new, 'decorated' function,
which is subsequently inherited (and not decorated, obviously), by B.

Am I correct here?  My workaround was to simply copy the method from class
A to class B, after which B._protector_decorator decorated the methods in
B.  While this doesn't make the use of decorators completely pointless (the
decorators actually do something in each class, it's just different), it
does add a bunch of code duplication which I was at one point hopeful to
avoid.

I'm still stumbling around with decorators a little, but this exercise has
made them a lot clearer to me.




I'm certainly not the expert on decorators;  I've only used them for 
simple things.  But I think I can clear up one misconception.


The decorator function will execute while *compiling* the class A, and 
the one in class B is unreferenced.


The decorator @_protector_decorator is shorthand for something like
   mymethod = _protector_decorator(mymethod)

So by the time the compiler ends with class A, the mymethod has its 
final value.


(Note, I've not used a decorator that was defined inside a class, so I'm 
probably missing the appropriate A. or self. or cls. overrides.)
But the order of definition is still correct.  A decorator executes 
once, just after a function is completed.




--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Re: security quirk

2013-01-30 Thread alex23
On Jan 31, 5:39 am, RichD  wrote:
> well yeah, but what's going on, under the hood?
> How does it get confused?  How could this
> happen?  I'm looking for some insight, regarding a
> hypothetical programmimg glitch -

As has been stated, this has nothing to do with Python, so please stop
posting your questions here.

However, here's an answer to get you to stop repeating yourself: it's
not uncommon to find that content you're restricted from accessing via
a site's own search is available to you through Google. This has to do
with Google's policy of _requiring_ that pages that it is allowed to
index _must_ be available for view. Any site that allows Google to
index its pages that then blocks you from viewing them will swiftly
find themselves web site-a non gratis in Google search. As most
websites are attention whores, they'll do anything to ensure they
remain within Google's indices.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: security quirk

2013-01-30 Thread Arne Vajhøj

On 1/29/2013 11:55 PM, RichD wrote:

I read Wall Street Journal, and occasionally check
articles on their Web site.  It's mostly free, with some items
available to subscribers only.  It seems random, which ones
they block, about 20%.

Anywho, sometimes I use their search utility, the usual author
or title search, and it blocks, then I look it up on Google, and
link from there, and it loads!  ok, Web gurus, what's going on?


WSJ want their articles to be findable from Google.

So they open up for Google indexing them.

If they require any type of registration to see an article,
then Google will remove the link.

So therefore WSJ (and many other web sites!) gives more access
if you come from Google than if not.

Arne


--
http://mail.python.org/mailman/listinfo/python-list


Re: help

2013-01-30 Thread Steven D'Aprano
aramilda...@gmail.com wrote:

> its giving me a bunch of errors thanks for the help in advance!

Yay! Guessing games! I love guessing games! Let me see if I can guess the
errors you have...

/usr/bin/python: bad interpreter: No such file or directory

You haven't actually got Python installed, or at least not where you think
it is.

Am I close? If not, I recommend that you actually show us the errors you are
getting.



-- 
Steven

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help

2013-01-30 Thread Dave Angel

On 01/30/2013 04:16 PM, aramilda...@gmail.com wrote:

Hi everyone! I don't mean to intrude, but ive heard great things about this 
group and ive stumped myself with my python code.

heres my code:
#! /usr/bin/python

import sys

global labelList
labelList= dict()

global counter
counter = 0

def execute(line):
 if line.find("::print") != -1:
 stripPrint = line.find("::print")
 startPrint = line.find('"', stripPrint)
 stopPrint = line.find('"', startPrint + 1)
 printSection = line[startPrint + 1 : stopPrint]
 print(printSection)

 if line.find("::label") != -1:
 stripLabel = line.find("::label")
 startLabel = line.find(' ', stripLabel)
 stopLabel = line.find('--', startLabel + 1)
 label = line[startLabel + 1 : stopLabel]
 line.strip("\r\n")
 labelList[label] = counter

if len(sys.argv) < 2:
 print("error: no input files")
 print("compilation terminated")

else:
 fileName = sys.argv[1]
 jadeFile = open(fileName, 'r')

 for line in jadeFile:
 counter = counter + 1
 execute(line)

 jadeFile.close()

 i = 0

 while i < len(labelList):
 print(labelList.keys()[i], ":", labelList.values()[i])
 i = i + 1


and its giving me a bunch of errors thanks for the help in advance!



davea@think2:~/temppython$ python aram.py
error: no input files
compilation terminated


These messages are triggered by sys.argv being less than 2.  Cure is to 
pass some string as the first argument on the command line.


Fixed that:

davea@think2:~/temppython$ ./aram.py  myfile.txt
Traceback (most recent call last):
  File "./aram.py", line 33, in 
jadeFile = open(fileName, 'r')
IOError: [Errno 2] No such file or directory: 'myfile.txt'

(notice that I pasted the full traceback into this message.)
Problem is that the program is treating that parameter as a filename, 
and I don't have a file by that filename.


davea@think2:~/temppython$ ./aram.py  aram.py
) != -1:
)
('stripLabel = line.find("::label")', ':', 20)
('!= -1:', ':', 19)

Worked perfectly.  Of course, nobody has said what it's supposed to do. 
 So anything that doesn't display an error must be okay.


How about describing what version of Python this is intended for, how 
you ran it, and what errors you're getting.  Don't paraphrase, don't 
summarize (lots of errors ???!), just copy/paste.


And if it's appropriate, show a sample data file for it to open, not 
attached, but pasted into your email message.


--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Re: pyrudp

2013-01-30 Thread Chris Angelico
On Thu, Jan 31, 2013 at 3:26 PM,   wrote:
> Now, the good news is that because UDP-based protocols all run in user memory 
> space (as opposed to TCP that runs privileged in kernel space) it is 
> relatively straightforward for non-privledged users to write and test UDP 
> transport schemes and this has become a fairly standard CS exercise at the 
> graduate level.  If I were in your shoes, I'd start Googling for the papers 
> published on protocols like HURRICANE, ATAU, or even just the general subject 
> of UDP transport protocols.


I'd still include reading up on TCP. The RFC has a good bit about why
things are the way they are; when you're designing a protocol that
does similar things, it's worth getting an understanding of what your
predecessors did. Either you'll get some ideas ("yeah, that's how I'll
do it!") or you'll decide you can do better, but it's still worth a
read.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: looping versus comprehension

2013-01-30 Thread Steven D'Aprano
On Thu, 31 Jan 2013 02:49:31 +1100, Chris Angelico wrote:

> it's worth
> noting that list appending is not going to be O(N*N), because it's going
> to allow room for expansion.

This is true for list.append, which is amortized constant time. But it is 
not true for list addition, alist + blist, which is O(N**2) and hence 
gets really, really slow:

steve@runes:~$ python -m timeit "L = []" "for i in xrange(1000): L = L + [1]"
100 loops, best of 3: 3.08 msec per loop
steve@runes:~$ python -m timeit "L = []" "for i in xrange(5000): L = L + [1]"
10 loops, best of 3: 71 msec per loop
steve@runes:~$ python -m timeit "L = []" "for i in xrange(25000): L = L + [1]"
10 loops, best of 3: 2.06 sec per loop


Notice that as the number of list additions goes up by a factor of 5, 
the time taken goes up by a factor of 25.

-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pyrudp

2013-01-30 Thread wrw
On Jan 30, 2013, at 7:14 PM, Chris Angelico  wrote:

> On Thu, Jan 31, 2013 at 11:04 AM, Jorge Alberto Diaz Orozco
>  wrote:
>> I have restrictions in my system that does not allow me to use TCP, so I 
>> want to make a pipe over UDP imitating TCP behavior.
>> I have control over both endpoints, and I´m writing both of them.
>> I just don´t want to re-invent the wheel and I´m looking for a reliable UDP 
>> sockets implementation for Python so I can start from there.
> 
> Then... I think the place to start is here:
> 
> http://www.ietf.org/rfc/rfc793.txt
> 
> ChrisA
> -- 
> http://mail.python.org/mailman/listinfo/python-list

I think you really ought to think about this long and hard.  Although TCP 
started as a fairly simple-minded protocol designed to guarantee reliable 
delivery of packets in the order in which they were transmitted, it has evolved 
considerably over the years.  It now incorporates concepts of "fairness" and a 
very sophisticated rate control system that is constantly probing available 
network bandwidth to be sure it isn't over driving or hogging the network 
connection.  It would be easy to say: "I really don't need all that - all I'm 
doing is X." - but in reality you do, and getting reliable delivery over UDP 
really does require it all.

Now, the good news is that because UDP-based protocols all run in user memory 
space (as opposed to TCP that runs privileged in kernel space) it is relatively 
straightforward for non-privledged users to write and test UDP transport 
schemes and this has become a fairly standard CS exercise at the graduate 
level.  If I were in your shoes, I'd start Googling for the papers published on 
protocols like HURRICANE, ATAU, or even just the general subject of UDP 
transport protocols.  Two places you might start are:

  http://www.ogf.org/documents/GFD.55.pdf 

and

  http://www.ornl.gov/~webworks/cppr/y2001/rpt/121150.pdf

Most, if not all of these UDP schemes are in the public domain, have been 
written in high-level languages, and could be translated into python.

-Bill
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: confusion with decorators

2013-01-30 Thread Steven D'Aprano
On Wed, 30 Jan 2013 19:34:03 -0500, Jason Swails wrote:

> Hello,
> 
> I was having some trouble understanding decorators and inheritance and 
> all that.  This is what I was trying to do:
> 
> # untested
> class A(object):
>def _protector_decorator(fcn):
>   def newfcn(self, *args, **kwargs):
>  return fcn(self, *args, **kwargs)
>   return newfcn

Well, that surely isn't going to work, because it always decorates the 
same function, the global "fcn". 

You probably want to add an extra parameter to the newfcn definition:

def newfcn(self, fcn, *args, **kwargs):


Also, I trust you realise that this is a pointless decorator that doesn't 
do anything useful? It just adds an extra layer of indirection, without 
adding any functionality.


>   @_protector_decorator
>   def my_method(self, *args, **kwargs):
>  """ do something here """
>
> class B(A):
>   def _protector_decorator(fcn):
>   def newfcn(self, *args, **kwargs):
>  raise MyException('I do not want B to be able to access the
> protected functions')
>   return newfcn


That's not going to work, because B's _protector_decorator never gets 
called. True, it overrides A's _protector_decorator, but too late. A has 
already used it to decorate the methods, and B does not override those 
methods, so A's version are inherited.

But even if it could work, it relies on class B protecting class A from 
B. All B needs do to overcome the protection is ... *not* define the 
magic decorator.


> The goal of all that was to be able to change the behavior of my_method
> inside class B simply by redefining the decorator. Basically, what I
> want is B.my_method() to be decorated by B._protector_decorator, but in
> the code I'm running it's decorated by A._protector_decorator.

Yes. Remember that you don't have a B.my_method, so B merely inherits 
A.my_method.


> I presume this is because once the decorator is applied to my_method in
> class A, A.my_method is immediately bound to the new, 'decorated'
> function, which is subsequently inherited (and not decorated,
> obviously), by B.

Correct.

> Am I correct here?  My workaround was to simply copy the method from
> class A to class B, after which B._protector_decorator decorated the
> methods in B.

That's not a work-around, that's an anti-pattern.

Why is B inheriting from A if you don't want it to be able to use A's 
methods? That's completely crazy, if you don't mind me saying so. If you 
don't want B to access A's methods, simply don't inherit from A.

I really don't understand what you are trying to accomplish here. 
Possibly Java.

http://dirtsimple.org/2004/12/python-is-not-java.html
http://dirtsimple.org/2004/12/java-is-not-python-either.html


But you can accomplish something close to what you are after like this:


import functools

def decorate(func):
@functools.wraps(func)
def inner(self, *args, **kwargs):
protector = getattr(self, '_protect', None)
if protector is not None:
protector()
return func(self, *args, **kwargs)
return inner


class A(object):
@decorate
def mymethod(self):
"""Do something useful."""


class B(A):
def _protect(self):
raise RuntimeError("I'm sorry Dave, I'm afraid I cannot do that.")



Try studying that to see how it works, and then try studying it to 
realise how pointless it is, since it too relies on class B protecting 
class A from B.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list