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

2010-06-03 Thread Steven D'Aprano
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',  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] From SQL Blobs to Python Buffers to Actual Files

2010-06-03 Thread GoodPotatoes
Environment:
Sybase/ODBC/Windows/Python2.5

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', )

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


  ___
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-03 Thread Steven D'Aprano
On Fri, 4 Jun 2010 03:10:41 am Alan Gauld wrote:
> "Tino Dai"  wrote
>
> >Is there a way to express this:
> >isThumbnail = False
> >if size == "thumbnail":
> >isThumbnail = True
> >
> > like this:
> > [ isThumbnail = True if size == "thumbnail" isThumbnail =
> > False ]
>
> Bob showed one way, you could also do:
>
> 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:

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


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


Re: [Tutor] Fw: Misc question about scoping

2010-06-03 Thread Steven D'Aprano
On Fri, 4 Jun 2010 09:00:13 am ALAN GAULD quoted Tino Dai who wrote:

> "Tino Dai"  wrote
[...]
> >> I have code that is unpythonic in many places. It works, but
> >> it's ugly. One of those unpythonic places is I'm initializing some
> >> variable such as a list,dict, or whatever outside of if/while/for
> >> in block to use later. So, I'm cleaning those places up.
> >>
> >>  For example, my present code looks like:
> >>  L = [] # Needed or L doesn't exist outside of for loop
> >> below for o in a:
> >> if o.someAttribute > someConstant:
> >>L.append(o.someOtherAttribute)
> >>
> >>   later in code 
> >>  


There's nothing unpythonic about that code. For loops existed in Python 
since the very earliest days, which is *at least* 1991, while list 
comprehensions are a newcomer.

But if you insist on a list comprehension, you can re-write the above 
for-loop as:

L = [o.someOtherAttribute for o in a if o.someAttribute > someConstant]



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


Re: [Tutor] parse text file

2010-06-03 Thread Steven D'Aprano
On Fri, 4 Jun 2010 12:45:52 am Colin Talbert wrote:

> I thought when you did a for uline in input_file each single line
> would go into memory independently, not the entire file.

for line in file:

reads one line at a time, but file.read() tries to read everything in 
one go. However, it should fail with MemoryError, not just stop 
silently.

> I'm pretty sure that this is not your code, because you can't call
> len() on a bz2 file. If you try, you get an error:
>
> You are so correct.  I'd been trying numerous things to read in this
> file and had deleted the code that I meant to put here and so wrote
> this from memory incorrectly.  The code that I wrote should have
> been:
>
> import bz2
> input_file = bz2.BZ2File(r'C:\temp\planet-latest.osm.bz2','rb')
> str=input_file.read()
> len(str)
>
> Which indeed does return only 90.


Unfortunately, I can't download your bz2 file myself to test it, but I 
think I *may* have found the problem. It looks like the current bz2 
module only supports files written as a single stream, and not multiple 
stream files. This is why the BZ2File class has no "append" mode. See 
this bug report:

http://bugs.python.org/issue1625

My hypothesis is that your bz2 file consists of either multiple streams, 
or multiple bz2 files concatenated together, and the BZ2File class 
stops reading after the first.

I can test my hypothesis:

>>> bz2.BZ2File('a.bz2', 'w').write('this is the first chunk of text')
>>> bz2.BZ2File('b.bz2', 'w').write('this is the second chunk of text')
>>> bz2.BZ2File('c.bz2', 'w').write('this is the third chunk of text')
>>> # concatenate the files
... d = file('concate.bz2', 'w')
>>> for name in "abc":
... f = file('%c.bz2' % name, 'rb')
... d.write(f.read())
...
>>> d.close()
>>>
>>> bz2.BZ2File('concate.bz2', 'r').read()
'this is the first chunk of text'

And sure enough, BZ2File only sees the first chunk of text!

But if I open it in a stand-alone bz2 utility (I use the Linux 
application Ark), I can see all three chunks of text. So I think we 
have a successful test of the hypothesis.


Assuming this is the problem you are having, you have a number of 
possible solutions:

(1) Re-create the bz2 file from a single stream.

(2) Use another application to expand the bz2 file and then read 
directly from that, skipping BZ2File altogether.

(3) Upgrade to Python 2.7 or 3.2, and hope the patch is applied.

(4) Backport the patch to your version of Python and apply it yourself.

(5) Write your own bz2 utility.

Not really a very appetising series of choices there, I must admit. 
Probably (1) or (2) are the least worst.



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


[Tutor] Fw: Misc question about scoping

2010-06-03 Thread ALAN GAULD
Fowarding to the list.
Remember to use Reply ALL when replying.

 
Alan G.


"Tino Dai"  wrote
>>>
>>>
>>>   Is there a way to express this:
   isThumbnail = False
   if size == "thumbnail":
   isThumbnail = True

like this:
[ isThumbnail = True if size == "thumbnail" isThumbnail = False ]

>>>
>>>Bob showed one way, you could also do:
>>>
>>isThumbnail = True if size == "thumbnail" else False
>>>
>>>
>>>
>>>and the scoping extending to one level above without resorting to the
global keyword?

>>>
>>>Not sure what this has to do with scoping or the use of global?
>>global is only needed inside a function if you are modifying a
>>value defined outside the function.
>>>
>>If the assignment is inside a function and the definition of
>>isThumbnail is outside then you need to use global.
>>>
>>HTH,
>>>
>>>
>> 
>>Hi Bob, Alan, and Joel,
>>
>> I may have used scoping in an incorrect way. To clear things up, let me 
>> try to explain my general issue:
>>
>> I have code that is unpythonic in many places. It works, but it's ugly. 
>> One of those unpythonic places is I'm initializing some variable such as a 
>> list,dict, or whatever outside of if/while/for in block to use later. So, 
>> I'm cleaning those places up. 
>>
>>  For example, my present code looks like:
>>  L = [] # Needed or L doesn't exist outside of for loop below
>>  for o in a:
>> if o.someAttribute > someConstant:
>>L.append(o.someOtherAttribute)

>>   later in code 
>>  
>>
>>  Would like my code to resemble (don't know if this code works):
>>  L = [o.someOtherAttribute if o.someAttribute > someConstant else pass 
>> for o in a]

>>   later in code 
>>  
>>
>> Does that make more sense?
>>
>>TIA,
>>Tino 
>>On Thu, Jun 3, 2010 at 1:10 PM, Alan Gauld  wrote:
>>
>Here is some actual code that I working on right now:
>
>First iteration:
>>answerDict={}
>for key in qas[0].fk_question.q_WidgetChoice.all():
>answerDict[str(key.widgetChoice)]=0
>
>Second iteration:
>answerDict=dict([(str(key.widgetChoice),0) for key in 
>qas[0].fk_question.q_widgetChoice.all()])
>
>Third iteration:
>answerDict=dict(map(lambda x:  \
>  (str(x.widgetChoice),0),qas[0].fk_question.q_WidgetChoice.all()))
>
>The third iteration is what I'm looking for.
>
>-Tino
>On Thu, Jun 3, 2010 at 1:39 PM, Tino Dai  wrote:
>___
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-03 Thread Alan Gauld


"Alex Hall"  wrote


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


You can use IP broadcasts to do this but its not very
network friendly. The usual approach is for the IP address/port
to be published in some way - thats how email, ftp, the web etc
all work. The ports are standardised(25 for SMTP, 80 for WWW etc)
but the IP address needs to be published. I'd certainly start with 
that

approach and add broadcast and auro-discovery as a feature later.

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.


I'd get it working using a fixed IP first, wiorry about broadcasting
after the basics are there.

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?


Yes, you need IP address plus port number. Best done in a config file
or similar. For peer to peer you can have a list of addresses and
poll them till you get a response but if you are using DHCP you will
need a range and broadcast starts to be better. For now I'd stick with
each process displaing the IP address and the users typing them in.


they are, but I am not sure. Does someone have an example of this
process?


Any internet protocol, incluiding SMTP email or FTP...

For a more peer to peer approach think about IM clients.
Netmeeting etc

from a programming perspective, in any language. For now, it is all 
a

frustrating and confusing tangle of Socket objects and low-level
calls,


Get the basics first. You still need to work out your command
protocol to play the game. That will be challenging enough to start
with.

HTH,

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


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


Re: [Tutor] Misc question about scoping

2010-06-03 Thread Emile van Sebille

On 6/3/2010 8:50 AM Tino Dai said...

Hi All,

 Is there a way to express this:
 isThumbnail = False
 if size == "thumbnail":
 isThumbnail = True

  like this:
  [ isThumbnail = True if size == "thumbnail" isThumbnail = False ]
  and the scoping extending to one level above without resorting to the
global keyword?



If by 'extending one level above' you mean 'outside the current scope', 
then yes if isThumbnail is mutable.  ie, something like:


isThumbnail = [False]

# create scope

def inner(size):
isThumbnail[0] = bool(size == "thumbnail")


inner('not thumbnail')
print isThumbnail


inner('thumbnail')
print isThumbnail

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


Re: [Tutor] parse text file

2010-06-03 Thread Sander Sweers
On 3 June 2010 21:02, Colin Talbert  wrote:

> I couldn't find any example of it in use and wasn't having any luck getting
> it to work based on the documentation.


Good examples of the bz2 module can be found at [1].

greets
Sander

[1] http://www.doughellmann.com/PyMOTW/bz2/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2010-06-03 Thread Alex Hall
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?

Thanks. I know I have emailed this list (and other lists) before about
this, but my peer-to-peer has not even started to look promising yet,
since this is my first time ever doing anything remotely like this
from a programming perspective, in any language. For now, it is all a
frustrating and confusing tangle of Socket objects and low-level
calls, but hopefully things will start to clear up over the next few
days.

-- 
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] parse text file

2010-06-03 Thread Vincent Davis
On Thu, Jun 3, 2010 at 1:02 PM, Colin Talbert  wrote:

>
> Dave,
> I think you are probably right about using decompressor.  I
> couldn't find any example of it in use and wasn't having any luck getting it
> to work based on the documentation.  Maybe I should try harder on this
> front.
>

Is it possible write a python script to transfer this to a hdf5 file?  Would
this help?
Thanks
Vincent


> Colin Talbert
> GIS Specialist
> US Geological Survey - Fort Collins Science Center
> 2150 Centre Ave. Bldg. C
> Fort Collins, CO 80526
>
> (970) 226-9425
> talbe...@usgs.gov
>
>
>
>  From: Dave Angel  To:
> Colin Talbert 
> Cc: Steven D'Aprano , tutor@python.org Date: 06/03/2010
> 12:36 PM Subject: Re: [Tutor] parse text file
> --
>
>
>
> Colin Talbert wrote:
> > 
> > You are so correct.  I'd been trying numerous things to read in this file
>
> > and had deleted the code that I meant to put here and so wrote this from
> > memory incorrectly.  The code that I wrote should have been:
> >
> > import bz2
> > input_file = bz2.BZ2File(r'C:\temp\planet-latest.osm.bz2','rb')
> > str=input_file.read()
> > len(str)
> >
> > Which indeed does return only 90.
> >
> > Which is also the number returned when you sum the length of all the
> lines
> > returned in a for line in file with:
> >
> >
> > import bz2
> > input_file = bz2.BZ2File(r'C:\temp\planet-latest.osm.bz2','rb')
> > lengthz = 0
> > for uline in input_file:
> > lengthz = lengthz + len(uline)
> >
> > print lengthz
> >
> > 
> >
> >
> Seems to me for such a large file you'd have to use
> bz2.BZ2Decompressor.  I have no experience with it, but its purpose is
> for sequential decompression -- decompression where not all the data is
> simultaneously available in memory.
>
> DaveA
>
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
  *Vincent Davis
720-301-3003 *
vinc...@vincentdavis.net
 my blog  |
LinkedIn
___
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-03 Thread spir
On Thu, 3 Jun 2010 11:50:42 -0400
Tino Dai  wrote:

> Hi All,
> 
> Is there a way to express this:
> isThumbnail = False
> if size == "thumbnail":
> isThumbnail = True
> 
>  like this:
>  [ isThumbnail = True if size == "thumbnail" isThumbnail = False ]
>  and the scoping extending to one level above without resorting to the
> global keyword?

If you are not in a function, then your question does not make sense for 
python. If you are in a function, then isThumbnail must have been defined 
before and you must use global, yes.
Thus, maybe no need for complicated expression:

isThumbnail = False;
def f(size):
  global isThumbnail
  if size == "thumbnail":
isThumbnail = True



> Thanks,
> Tino



-- 


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] parse text file

2010-06-03 Thread Colin Talbert
Dave,
I think you are probably right about using decompressor.  I 
couldn't find any example of it in use and wasn't having any luck getting 
it to work based on the documentation.  Maybe I should try harder on this 
front.

Colin Talbert
GIS Specialist
US Geological Survey - Fort Collins Science Center
2150 Centre Ave. Bldg. C
Fort Collins, CO 80526

(970) 226-9425
talbe...@usgs.gov




From:
Dave Angel 
To:
Colin Talbert 
Cc:
Steven D'Aprano , tutor@python.org
Date:
06/03/2010 12:36 PM
Subject:
Re: [Tutor] parse text file



Colin Talbert wrote:
> 
> You are so correct.  I'd been trying numerous things to read in this 
file 
> and had deleted the code that I meant to put here and so wrote this from 

> memory incorrectly.  The code that I wrote should have been:
>
> import bz2
> input_file = bz2.BZ2File(r'C:\temp\planet-latest.osm.bz2','rb')
> str=input_file.read()
> len(str)
>
> Which indeed does return only 90.
>
> Which is also the number returned when you sum the length of all the 
lines 
> returned in a for line in file with:
>
>
> import bz2
> input_file = bz2.BZ2File(r'C:\temp\planet-latest.osm.bz2','rb')
> lengthz = 0
> for uline in input_file:
> lengthz = lengthz + len(uline)
>
> print lengthz
>
> 
> 
>
Seems to me for such a large file you'd have to use 
bz2.BZ2Decompressor.  I have no experience with it, but its purpose is 
for sequential decompression -- decompression where not all the data is 
simultaneously available in memory.

DaveA



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


Re: [Tutor] parse text file

2010-06-03 Thread Dave Angel

Colin Talbert wrote:


You are so correct.  I'd been trying numerous things to read in this file 
and had deleted the code that I meant to put here and so wrote this from 
memory incorrectly.  The code that I wrote should have been:


import bz2
input_file = bz2.BZ2File(r'C:\temp\planet-latest.osm.bz2','rb')
str=input_file.read()
len(str)

Which indeed does return only 90.

Which is also the number returned when you sum the length of all the lines 
returned in a for line in file with:



import bz2
input_file = bz2.BZ2File(r'C:\temp\planet-latest.osm.bz2','rb')
lengthz = 0
for uline in input_file:
lengthz = lengthz + len(uline)

print lengthz


  

Seems to me for such a large file you'd have to use 
bz2.BZ2Decompressor.  I have no experience with it, but its purpose is 
for sequential decompression -- decompression where not all the data is 
simultaneously available in memory.


DaveA

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


Re: [Tutor] parse text file

2010-06-03 Thread Alan Gauld


"Colin Talbert"  wrote

I thought when you did a for uline in input_file each single line 
would go

into memory independently, not the entire file.


Thats true but your code snippet showed you using read()
which reads the whole file...

I'm pretty sure that this is not your code, because you can't call 
len()

on a bz2 file. If you try, you get an error:

You are so correct.  I'd been trying numerous things to read in this 
file
and had deleted the code that I meant to put here and so wrote this 
from

memory incorrectly.  The code that I wrote should have been:

import bz2
input_file = bz2.BZ2File(r'C:\temp\planet-latest.osm.bz2','rb')
str=input_file.read()
len(str)


This again usees read() which reads the whole file.

Which is also the number returned when you sum the length of all the 
lines

returned in a for line in file with:

import bz2
input_file = bz2.BZ2File(r'C:\temp\planet-latest.osm.bz2','rb')
lengthz = 0
for uline in input_file:
   lengthz = lengthz + len(uline)


I'm not sure how

for line in file

will work for binary files. It may read the whole thing since
the concept of lines really only applies to text. So it may
be the same result as using read()

Try looping using read(n) where n is some buffer size
(1024 might be a good value?).

HTH,

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


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


Re: [Tutor] Misc question about scoping

2010-06-03 Thread Alan Gauld

"Tino Dai"  wrote


   Is there a way to express this:
   isThumbnail = False
   if size == "thumbnail":
   isThumbnail = True

like this:
[ isThumbnail = True if size == "thumbnail" isThumbnail = 
False ]


Bob showed one way, you could also do:

isThumbnail = True if size == "thumbnail" else False

and the scoping extending to one level above without resorting 
to the

global keyword?


Not sure what this has to do with scoping or the use of global?
global is only needed inside a function if you are modifying a
value defined outside the function.

If the assignment is inside a function and the definition of
isThumbnail is outside then you need to use global.

HTH,

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


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


Re: [Tutor] Misc question about scoping

2010-06-03 Thread bob gailer

On 6/3/2010 11:50 AM, Tino Dai wrote:

Hi All,

Is there a way to express this:
isThumbnail = False
if size == "thumbnail":
isThumbnail = True



How I do that is:

isThumbnail = size == "thumbnail":


 like this:
 [ isThumbnail = True if size == "thumbnail" isThumbnail = False ]
 and the scoping extending to one level above without resorting to 
the global keyword?


I have no idea what you mean by that. Please provide context.


--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] OOP clarification needed

2010-06-03 Thread Lie Ryan
On 06/03/10 01:37, Jim Byrnes wrote:
> 
>>> some older procedural languages I always end up becoming confused by
>>> the large number of built in methods.
>>
>> C is one of the simplest procedural languages around
>> and yet it comes with a huge library of functions (several
>> hundred in some cases). The size of the library should be easier
>> to manage using OOP than with older function/procedure based
>> libraries, because the functions are not just logically grouped
>> in the documentation but in the code too.
> 
> I don't know C, I was thinking more along the lines of Basic or Rexx.I
> could sit down and read through a list of keywords and built in
> functions and it would be compact enough that I would have a good idea
> of what was available.  I can't seem to do that with the OO languages,
> but of course  I am older now also.

When I learned Visual Basic, I definitely remembered the thousands of
pages of documentation for thousands of functions. Python is probably
isn't much different in the size of the number of functions included in
the stdlib, but IMHO help() and pydoc aids much better in navigating the
docs compared to context sensitive help.

Python's keywords are just:

help> keywords

Here is a list of the Python keywords.  Enter any keyword to get more help.

and elifif  print
as  elseimport  raise
assert  except  in  return
break   execis  try
class   finally lambda  while
continuefor not with
def fromor  yield
del global  pass

I don't think there are many non-esoteric languages with significantly
less keywords than python.

and the builtin functions:

abs  all  any  apply  basestring  bin  bool  buffer  bytearray  bytes
callable  chr  classmethod  cmp  coerce  compile  complex  delattr  dict
 dir  divmod  enumerate  eval  execfile  exit  file  filter  float
format  frozenset  getattr  globals  hasattr  hash  help  hex  id  input
 int  intern  isinstance  issubclass  iter  len  list  locals  long  map
 max  min  next  object  oct  open  ord  pow  print  property  quit
range  raw_input  reduce  reload  repr  reversed  round  set  setattr
slice  sorted  staticmethod  str  sum  super  tuple  type  unichr
unicode  vars  xrange  zip

and unlike some languages, the list of python's built-ins actually gets
smaller with py3k (84 items in 2.6.4 and 71 items in 3.1.2, excluding
Exceptions)

I never actually sit down and read through all the built-in function's
documentation; I just skim through this list, make a mental note of what
they appears to be doing from their name, and only read their
documentation as the need to use them arises.

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


[Tutor] Misc question about scoping

2010-06-03 Thread Tino Dai
Hi All,

Is there a way to express this:
isThumbnail = False
if size == "thumbnail":
isThumbnail = True

 like this:
 [ isThumbnail = True if size == "thumbnail" isThumbnail = False ]
 and the scoping extending to one level above without resorting to the
global keyword?

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


Re: [Tutor] parse text file

2010-06-03 Thread Colin Talbert
Hello Steven,
Thanks for the reply.  Also this is my first post to tu...@python 
so I'll reply all in the future.


However, a file of that size changes things drastically. You can't 
expect to necessarily be able to read the entire 9.2 gigabyte BZ2 file 
into memory at once, let along the unpacked 131 GB text file, EVEN if 
your computer has more than 9.2 GB of memory. So your tests need to 
take this into account.

I thought when you did a for uline in input_file each single line would go 
into memory independently, not the entire file.



I'm pretty sure that this is not your code, because you can't call len() 
on a bz2 file. If you try, you get an error:

You are so correct.  I'd been trying numerous things to read in this file 
and had deleted the code that I meant to put here and so wrote this from 
memory incorrectly.  The code that I wrote should have been:

import bz2
input_file = bz2.BZ2File(r'C:\temp\planet-latest.osm.bz2','rb')
str=input_file.read()
len(str)

Which indeed does return only 90.

Which is also the number returned when you sum the length of all the lines 
returned in a for line in file with:


import bz2
input_file = bz2.BZ2File(r'C:\temp\planet-latest.osm.bz2','rb')
lengthz = 0
for uline in input_file:
lengthz = lengthz + len(uline)

print lengthz


Thanks again for you help and sorry for the bad code in the previous 
submittal.


Colin Talbert
GIS Specialist
US Geological Survey - Fort Collins Science Center
2150 Centre Ave. Bldg. C
Fort Collins, CO 80526

(970) 226-9425
talbe...@usgs.gov




From:
Steven D'Aprano 
To:
tutor@python.org
Date:
06/02/2010 03:42 PM
Subject:
Re: [Tutor] parse text file
Sent by:
tutor-bounces+talbertc=usgs@python.org



Hi Colin,

I'm taking the liberty of replying to your message back to the list, as 
others hopefully may be able to make constructive comments. When 
replying, please ensure that you reply to the tutor mailing list rather 
than then individual.


On Thu, 3 Jun 2010 12:20:10 am Colin Talbert wrote:

> > Without seeing your text file, and the code you use to read the text
> > file, there's no way of telling what is going on, but I can guess
> > the most likely causes:
>
> Since the file is 9.2 gig it wouldn't make sense to send it to you. 

And I am very glad you didn't try *smiles*

However, a file of that size changes things drastically. You can't 
expect to necessarily be able to read the entire 9.2 gigabyte BZ2 file 
into memory at once, let along the unpacked 131 GB text file, EVEN if 
your computer has more than 9.2 GB of memory. So your tests need to 
take this into account.

> > (2) There's a bug in your code so that you stop reading after
> > 900,000 bytes.
> The code is simple enough that I'm pretty sure there is not a
> bug in it.
>
> import bz2
> input_file =
> bz2.BZ2File(r'C:\temp\planet-latest.osm.bz2','rb') print
> len(input_file)
>
> returns 90

I'm pretty sure that this is not your code, because you can't call len() 
on a bz2 file. If you try, you get an error:


>>> x = bz2.BZ2File('test.bz2', 'w')  # create a temporary file
>>> x.write("some data")
>>> x.close()
>>> input_file = bz2.BZ2File('test.bz2', 'r')  # open it
>>> print len(input_file)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: object of type 'bz2.BZ2File' has no len()


So whatever your code actually is, I'm fairly sure it isn't what you say 
here.



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