[Tutor] code works in windows command but not ubuntu terminal

2014-01-24 Thread Tobias Quezada
hello community,i am a newbie to python and program in general.
the script below works in python 2.7.3 on windows but not in the python 2.7.3 
ubuntu terminal.

>>>fp=open("prez.dat","r")>>>x=fp.read>>>(print(x)***i used fp for file 
>>>pointer.I am using windows 7 and it works but on ubuntu 12.04 LTS i get this 
>>>syntax error:
Traceback (most recent call last):  File "", line 1, in IOError: 
[Errno 2] No such file or directory: 'prez.dat'

Any thoughts?Thanx___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] code works in windows command but not ubuntu terminal

2014-01-24 Thread Keith Winston
The file would appear to not be on your search path, that is, in any
directory in which Python is expecting to find it. Either move it to a
directory on your path, or change your path to include it's location.
The easiest way to find out what your path is, that I know, is

import sys
sys.path

Good luck! Warning, I'm still a beginner myself, I might be mistaken
about something... If you don't understand what I'm talking about, try
to be very specific about what you do understand, it'll help people
formulate clear responses.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Iterator vs. iterable cheatsheet, was Re: iter class

2014-01-24 Thread Peter Otten

+--+++
|  | __iter__   | __next__   |
+--+++
| iterable | return an iterator | not available  |
+--+++
| iterator | return self| return next item   |
|  || or raise StopIteration |
+--+++

iter(x) is x --> True:  x is an iterator
 False: x is an iterable
 raises Exception: x is neither iterator nor iterable

Once next(it) raises a StopIteration on a well-behaved iterator it must 
continue to raise `StopIteration`s on subsequent next(it) calls.

There's an odd outlier that I probably shouldn't tell you about: 
classes with a __getitem__() method behave like iterators. An eventual 
IndexError exception is propagated as StopIteration:

>>> class A:
... def __getitem__(self, index):
... if index > 2:
... raise IndexError
... return index * index
... 
>>> for item in A():
... print(item)
... 
0
1
4


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


Re: [Tutor] code works in windows command but not ubuntu terminal

2014-01-24 Thread Keith Winston
I should have mentioned, the other possibility is that the file does
not, in fact, exist, but I assume you put it out there somewhere?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] code works in windows command but not ubuntu terminal

2014-01-24 Thread Peter Otten
Tobias Quezada wrote:

> hello community,i am a newbie to python and program in general.
> the script below works in python 2.7.3 on windows but not in the python
> 2.7.3 ubuntu terminal.
> 
fp=open("prez.dat","r")>>>x=fp.read>>>(print(x)***i used fp for file
pointer.I am using windows 7 and it works but on ubuntu 12.04 LTS i get
this syntax error:
> Traceback (most recent call last):  File "", line 1, in
> IOError: [Errno 2] No such file or directory: 'prez.dat'
 
That was probably

> fp=open("prez.dat","r")
> x=fp.read
> print(x)


> i used fp for file pointer.I am using windows 7 and it works but on ubuntu
> 12.04 LTS i get this syntax error:

> Traceback (most recent call last):  File "", line 1, in
> IOError: [Errno 2] No such file or directory: 'prez.dat'
> Any thoughts?Thanx

Please reread the error message. This is not a SyntaxError. Python is trying 
to tell you that a file called "prez.dat" doesn't exist in the current 
working directory. However, once you create the file (or provide the 
complete path if the file exists but is located in another directory) you 
will run into the next problem:

> x=fp.read

This will bind x to the fp.read method. To call the method the parentheses 
are mandatory:

x = fp.read()

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


Re: [Tutor] code works in windows command but not ubuntu terminal

2014-01-24 Thread Steven D'Aprano
Hi Tobias, and welcome.

On Thu, Jan 23, 2014 at 07:34:18PM -0700, Tobias Quezada wrote:
> hello community,i am a newbie to python and program in general.
> the script below works in python 2.7.3 on windows but not in the python 2.7.3 
> ubuntu terminal.
> 
> >>> fp=open("prez.dat","r")
> >>> x=fp.read
> >>> (print(x)
>
> ***i used fp for file pointer.I am using windows 7 and it works

Are you sure this is the *exact* same code you use on Windows 7? Because 
it won't read the file, it will fail with a SyntaxError due to the 
missing close bracket after the print. 

Fixing that problem reveals another problem: rather than read the 
file, it will just print something like 
this:



That's because you need round brackets (parentheses) to actually call 
the read method, otherwise you just get the method itself.

Finally we get to the error you have have reported:

> but on ubuntu 12.04 LTS i get this syntax error:
> Traceback (most recent call last):  
> File "", line 1, in 
> IOError: [Errno 2] No such file or directory: 'prez.dat'

That's not a syntax error. The problem is not with your code, but with 
the location of the file. There is no file called "prez.dat" in the 
current directory.

Possibly you have just forgotten to create the file, or you have 
forgotten that Ubuntu (like all Linux operating systems) are case 
sensitive and "prez.dat" will not match "Prez.DAT", or the file is in 
the wrong directory. You may need to use the cd command to go into the 
directory before starting Python.

Does this help solve your problem? Please feel free to ask if you have 
any further questions.


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


Re: [Tutor] code works in windows command but not ubuntu terminal

2014-01-24 Thread Steven D'Aprano
On Fri, Jan 24, 2014 at 04:22:20AM -0500, Keith Winston wrote:
> The file would appear to not be on your search path, that is, in any
> directory in which Python is expecting to find it.

Python does not use a search path for the open() function, only for 
imports. With open(), it uses a simple rule:

- absolute paths will look only in that exact location;

- relative paths are always relative to the current working directory.

Do you know the difference between absolute and relative paths?


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


Re: [Tutor] code works in windows command but not ubuntu terminal

2014-01-24 Thread maxin...@gmail.com
hello community,i am a newbie to python and program in general.
the script below works in python 2.7.3 on windows but not in the python 2.7.3 
ubuntu terminal.

>>>fp=open("prez.dat","r")>>>x=fp.read>>>(print(x)***i used fp for file 
>>>pointer.I am using windows 7 and it works but on ubuntu 12.04 LTS i get this 
>>>syntax error:
Traceback (most recent call last):  File "", line 1, in IOError: 
[Errno 2] No such file or directory: 'prez.dat'

Any thoughts?Thanx
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] code works in windows command but not ubuntu terminal

2014-01-24 Thread Alan Gauld

On 24/01/14 02:34, Tobias Quezada wrote:


 >>>fp=open("prez.dat","r")
 >>>x=fp.read
 >>>(print(x)



/IOError: [Errno 2] No such file or directory: 'prez.dat'/


Python can't see your file. You can check what python
is seeing by importing os and using listdir():

import os
os.listdir(',')  # . is the current directory

If your file is not listed then that's your problem.
Or it could be a permissions problem, but I can't
recall if that's a different error message, I suspect
so.

hth
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

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


Re: [Tutor] Iterator vs. iterable cheatsheet, was Re: iter class

2014-01-24 Thread spir

On 01/24/2014 10:22 AM, Peter Otten wrote:


There's an odd outlier that I probably shouldn't tell you about [...]


I guess there is a whole class of outliers; not really sure how to classify 
them. This is the case of defining a wrapper or "proxy" type, for a underlying 
data structure which is iterable, typically a builtin collection. This was 
evoked (but not specifically termed as "wrapper" or such) in previous message of 
the orginal thread. In that case, __iter__ would neither return self (it is not 
an iterator), nore a hand-baked iterator, but the builtin (or already defined) 
one of the underlying iterable. Two example, rather artificial (but code works):


First, say you have an array of angles, which for the user are measured in 
degrees but internally use radians. (A better example may be of internal 
machine-friendly RGB colors and external user-friendly HSL colors [not HSV! 
g...].) At the interface, there is conversion. Iteration actually is 
iterating on the internal array, thus just uses iter().


import math ; TAU = 2 * math.pi
class Angles:
def __init__ (self):
self.angles = list()
def put (self, angle):
self.angles.append(angle * TAU / 360)
def __getitem__ (self, i):
return self.angles[i] * 360 / TAU
def __iter__ (self):
return iter(self.angles)

angles = Angles()
angles.put(100) ; angles.put(200) ; angles.put(300)
print(angles[1])
for a in angles: print(a)

Second, we build an associative array (for few entries) as a plain association 
list à la Lisp, but implemented as a pair of lists instead as a list of pairs 
(this saves an intermediate notion of Pair). Iterating is here on the pair of 
list, zipped (in the python sense) together:


class AssList:
def __init__ (self):
self.keys, self.vals = list(), list()
def put (self, key, val):
self.keys.append(key)
self.vals.append(val)
def __getitem__ (self, i):
return self.keys[i], self.vals[i]
def __iter__ (self):
return iter(zip(self.keys, self.vals))

al = AssList()
al.put(1,'a') ; al.put(2,'b') ; al.put(3,'c')
print(al[1])
for k,v in al: print(k,v)

Side question: what is the point of __iter__ on iterators? Take a 'for' loop 
like:
for x in xs: f(x)
In the case where xs is not an iterator (no __next__), python calls iter(xs), 
which IIUC may call xs.__iter__() unless it is a builtin. But if xs is an 
iterator (__next__ is there), then Python uses it directly, thus what is the 
point of __iter__ there? In any case, python must check whether xs is an 
iterator (__next__). So there is no sense in calling __iter__ on an iterator. 
Logically, this would lead to an infinite recursion (calling __iter__ again and 
again). But python does it anyway (and stops after the first call, indeed):


class Iter:
def __init__ (self): self.n = 0
def __next__ (self):
if self.n == 10: raise StopIteration
self.n += 1
return self.n
def __iter__ (self):
print("*** __iter__ ***") # *
return self
it = Iter()
for i in it: print(i, end=" ")
print()

huh?

The only theoretical case I can find is iterators which do implement the 
protocol (__next__) but are not to be used (!), instead delegate to another 
iterator. Then, why do they bear __next__ at all? why are they iterators at all?


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


Re: [Tutor] Iterator vs. iterable cheatsheet, was Re: iter class

2014-01-24 Thread Peter Otten
spir wrote:

> On 01/24/2014 10:22 AM, Peter Otten wrote:
>>
>> There's an odd outlier that I probably shouldn't tell you about [...]
> 
> I guess there is a whole class of outliers; not really sure how to
> classify them. 

I think you are focusing on the details too much. In

> class Angles:
>  def __getitem__ (self, i):
>  return self.angles[i] * 360 / TAU
>  def __iter__ (self):
>  return iter(self.angles)

iter(self.angles) is just an implementation detail. The important point is 
that __iter__() returns a new iterator on each invocation, thus making 
Angles an "iterable" according to the naming scheme I was trying to 
establish. The __getitem__() method can be ignored as it is used for element 
lookup only, not for iteration.


> Side question: what is the point of __iter__ on iterators? Take a 'for'
> loop like: for x in xs: f(x)
> In the case where xs is not an iterator (no __next__), python calls
> iter(xs), which IIUC may call xs.__iter__() unless it is a builtin. But if
> xs is an iterator (__next__ is there), then Python uses it directly, thus
> what is the point of __iter__ there? In any case, python must check
> whether xs is an iterator (__next__). So there is no sense in calling
> __iter__ on an iterator. Logically, this would lead to an infinite
> recursion (calling __iter__ again and again). But python does it anyway
> (and stops after the first call, indeed):

There is no infinite recursion. The for loop is currently implemented as

# expect an iterable
# handle iterators through an idempotent iter()
tmp = iter(xs)
while True:
try:
x = next(tmp)
except StopIteration:
break
# use x

If I understand you correctly you suggest the following:

# expect an iterator
# fall back to getting an iterator through iter()
try:
tmp = xs.__next__
except AttributeError:
tmp = iter(xs).__next__
while True:
try:
x = tmp()
except StopIteration:
break

How is that simpler?

> The only theoretical case I can find is iterators which do implement the
> protocol (__next__) but are not to be used (!), instead delegate to
> another iterator. 

Again: the important differentiation is between iterator and iterable, not 
how the iterator is implemented.

> Then, why do they bear __next__ at all? why are they
> iterators at all?

Python allows you to do things that make no sense from the point of view of 
a human being. You can also implement an integer type with a negative abs():

>>> class Int(int):
... def __abs__(self): return self
... 
>>> abs(Int(-1))
-1

My advice: don't do it unless you have a good reason.

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


Re: [Tutor] code works in windows command but not ubuntu terminal

2014-01-24 Thread Keith Winston
On Fri, Jan 24, 2014 at 4:50 AM, Steven D'Aprano  wrote:
> Python does not use a search path for the open() function, only for
> imports. With open(), it uses a simple rule:
>
> - absolute paths will look only in that exact location;
>
> - relative paths are always relative to the current working directory.
>
> Do you know the difference between absolute and relative paths?

Ah! I was just running into this... I did not know that. So there's no
way to get it to search a path (other than coding some string
concatenation of path names or something, of course) to open a file?
Well, at least that clears things up for me, I've stumbled over this a
few times and didn't understand. Thanks.


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


Re: [Tutor] Iterator vs. iterable cheatsheet, was Re: iter class

2014-01-24 Thread spir

On 01/24/2014 06:44 PM, Peter Otten wrote:

There is no infinite recursion. The for loop is currently implemented as

# expect an iterable
# handle iterators through an idempotent iter()
tmp = iter(xs)


# here you must check that tmp actually implements the iterator protocol,
# else raise an error


while True:
 try:
 x = next(tmp)
 except StopIteration:
 break
 # use x

If I understand you correctly you suggest the following:

# expect an iterator
# fall back to getting an iterator through iter()
try:
 tmp = xs.__next__
except AttributeError:
 tmp = iter(xs).__next__
while True:
 try:
 x = tmp()
 except StopIteration:
 break

How is that simpler?


see above

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


Re: [Tutor] code works in windows command but not ubuntu terminal

2014-01-24 Thread Danny Yoo
> Ah! I was just running into this... I did not know that. So there's no
> way to get it to search a path (other than coding some string
> concatenation of path names or something, of course) to open a file?

Potentially distutils.spawn.find_executable might apply,

http://docs.python.org/2/distutils/apiref.html#module-distutils.spawn

but this it is not intended for finding data files either.


If you really need something like this, you'll probably be writing
your own path searching routines for this, as to my knowledge this is
not provided by the standard library.

(Probably for a good reason: sometimes searching for a data resource
without knowing exactly where it should be can be a susceptible vector
for attacks.  That is, you might consider this a convenience vs.
safety thing.  Directory search can be _expensive_ in certain contexts
as well, such as on a networked file system, for example.)

---

In a professional context: there are libraries in place where you say
explicitly which resources your file depends on at installation time,
and the library manages the placement and finding of those resources
at runtime.  For an example, see the distutils.package_data option:

http://docs.python.org/2/distutils/setupscript.html#installing-package-data
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] How to correct decimal addition.

2014-01-24 Thread Leon S
Here is what I'm trying to do, accept a price of gas, but I want to add the
.009 to the price, so that people do not have to type the full amount.
 Example, 3.49 /gallon would return 3.499 /gallon.

This is what I have tried and the results of it.

def gas_price(price):
   price == raw_input("What is the price of gas?")  return price + .09
  3.49=> 3.4898


It reduces the number and then adds many decimal points after.


Thanks for any help, I am sure this is an easy one for someone.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-24 Thread Norman Khine
maybe this would be of help

https://stackoverflow.com/questions/455612/python-limiting-floats-to-two-decimal-points


On Fri, Jan 24, 2014 at 5:57 PM, Leon S  wrote:

> Here is what I'm trying to do, accept a price of gas, but I want to add
> the .009 to the price, so that people do not have to type the full amount.
>  Example, 3.49 /gallon would return 3.499 /gallon.
>
> This is what I have tried and the results of it.
>
> def gas_price(price):
>price == raw_input("What is the price of gas?")  return price + .09   
> 3.49=> 3.4898
>
>
> It reduces the number and then adds many decimal points after.
>
>
> Thanks for any help, I am sure this is an easy one for someone.
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
>


-- 
%>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for
c in ",adym,*)&uzq^zqf" ] )
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-24 Thread wesley chun
hi leon,

you made a good start and ran into something that i know doesn't seem right
to you. however, before we attack the issue, there are some basic problems
with the code you need to correct first. below are a few explanations and
perhaps workarounds:

   1. you're passing in price *and* asking the user for it... pick one, not
   both
   2. i'll assume you did *not* mean to pass it in for the rest of this
   response
   3. the == should be a single = otherwise you should get a NameErrorexception
   4. the return price + .09 should be indented on the next line
   5. the operation price + .09 should fail with a TypeError exception
   6. the value .09 is incorrect for the solution you're describing...
   choose another
   7. assuming you fix all of the problems above and run it, you'll still
   get the output you stated
   8. however, if you print the value, it'll show correctly
   9. if you use Python 2.7.x+ or 3.1.x+, it'll show correctly
   10. reference link 1 to review:
   http://docs.python.org/tutorial/floatingpoint
   11. reference link 2 to review: http://bugs.python.org/issue1580

hope this helps!
--wesley


On Fri, Jan 24, 2014 at 9:57 AM, Leon S  wrote:

> Here is what I'm trying to do, accept a price of gas, but I want to add
> the .009 to the price, so that people do not have to type the full amount.
>  Example, 3.49 /gallon would return 3.499 /gallon.
>
> This is what I have tried and the results of it.
>
> def gas_price(price):
>price == raw_input("What is the price of gas?")  return price + .09   
> 3.49=> 3.4898
>
>
> It reduces the number and then adds many decimal points after.
>
>
> Thanks for any help, I am sure this is an easy one for someone.
>
>


-- 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"A computer never does what you want... only what you tell it."
+wesley chun  : wescpy at gmail :
@wescpy
Python training & consulting : http://CyberwebConsulting.com
"Core Python" books : http://CorePython.com
Python blog: http://wescpy.blogspot.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-24 Thread Dave Angel
 Leon S  Wrote in message:
> _

(please post in plain text in this text mailing list.  Html messes
 up formatting and causes some newsreaders grief.)
..
Leon said:
Here is what I'm trying to do, accept a price of gas, but I want
 to add the .009 to the price, so that people do not have to type
 the full amount.  Example, 3.49 /gallon would return 3.499
 /gallon.

This is what I have tried and the results of it.

def gas_price(price):  
   price == raw_input("What is the price of gas?")  return price + .09
   3.49
=> 3.4898

It reduces the number and then adds many decimal points after.

Thanks for any help, I am sure this is an easy one for someone
..

That stackoverflow link has lots of good information and some
 disinformation. 

First the code you show won't run, even after fixing the
 formatting.  Please use copy/paste. But making some assumptions, 
 I'm guessing you're adding two float objects and wondering why
 the result is off when printed. 

float is a binary floating point object and cannot represent
 precisely decimal float values like three point forty nine. You
 get quantization errors by definition,  and must deal with them
 somehow before the user sees them. You can fix that either by not
 using binary (perhaps the decimal module? ) or by rounding when
 you convert to string before printing. 

Next time, post in text format, include all your code (no print or
 equivalent in what you posted ), and tell us what version of
 python you're using. 



> 


-- 
DaveA

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


Re: [Tutor] code works in windows command but not ubuntu terminal

2014-01-24 Thread Steven D'Aprano
On Fri, Jan 24, 2014 at 04:31:49PM -0500, Keith Winston wrote:
> On Fri, Jan 24, 2014 at 4:50 AM, Steven D'Aprano  wrote:
> > Python does not use a search path for the open() function, only for
> > imports. With open(), it uses a simple rule:
> >
> > - absolute paths will look only in that exact location;
> >
> > - relative paths are always relative to the current working directory.
> >
> > Do you know the difference between absolute and relative paths?
> 
> Ah! I was just running into this... I did not know that. So there's no
> way to get it to search a path (other than coding some string
> concatenation of path names or something, of course) to open a file?

Of course there is -- you just have to program it yourself!

First, decide whether the filename is absolute or relative. Absolute 
filenames should not search the path. Then, if it is relative, loop over 
your "open path list" like this:


for prefix in open_path_list:
location = os.path.join(prefix, filename)
try:
f = open(location)
except IOError:
pass
else:
break


However, there's more to it than this. For starters, you need to decide 
on the exact behaviour. Clearly, "file not found" errors should move on 
to try the next prefix in the path list. But what about permission 
denied errors? 



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


Re: [Tutor] Iterator vs. iterable cheatsheet, was Re: iter class

2014-01-24 Thread eryksun
On Fri, Jan 24, 2014 at 8:50 AM, spir  wrote:
>
> xs is an iterator (__next__ is there), then Python uses it directly, thus
> what is the point of __iter__ there? In any case, python must check whether

Python doesn't check whether a type is already an iterator. It's
simpler to require that iterators implement __iter__, like any other
non-sequence iterable. This technically allows an iterator to return a
new iterator when __iter__ is called:

class C:
def __iter__(self): return D()
def __next__(self): return 'C'

class D:
def __iter__(self): return C()
def __next__(self): return 'D'

it1 = iter(C())
it2 = iter(it1)

>>> next(it1)
'D'
>>> next(it2)
'C'

That said, it's supposed to return self.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] code works in windows command but not ubuntu terminal

2014-01-24 Thread bob gailer

On 1/24/2014 4:47 AM, Steven D'Aprano wrote:

Hi Tobias, and welcome.

On Thu, Jan 23, 2014 at 07:34:18PM -0700, Tobias Quezada wrote:

hello community,i am a newbie to python and program in general.
the script below works in python 2.7.3 on windows but not in the python 2.7.3 
ubuntu terminal.


fp=open("prez.dat","r")
x=fp.read
(print(x)

***i used fp for file pointer.I am using windows 7 and it works

Are you sure this is the *exact* same code you use on Windows 7? Because
it won't read the file, it will fail with a SyntaxError due to the
missing close bracket after the print.
This is Python 2.7.3, not 3.x. The error is due to a ( in front of the 
print statement.


And please call () parends and [] brackets, and{} braces. Saves a lot of 
confusion.


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


Re: [Tutor] code works in windows command but not ubuntu terminal

2014-01-24 Thread Steven D'Aprano
On Fri, Jan 24, 2014 at 10:28:09PM -0500, bob gailer wrote:

> And please call () parends and [] brackets, and{} braces. Saves a lot of 
> confusion.

If you think that parentheses are spelt with a "d", you're certainly 
confused :-)

They're all brackets. Often the type of bracket doesn't matter, but when 
it does, adjectives do a perfectly fine job at distinguishing one from 
the other: round brackets, square brackets, and curly brackets are 
well-known and in common use all over the Commonwealth, and have been 
established since the mid 1700s.

As a sop to Americans, who I understand are easily confused by ordinary 
English *wink*, the Unicode consortium describes () as parentheses:

py> unicodedata.name("(")
'LEFT PARENTHESIS'

but [] and {} are described as brackets:

py> unicodedata.name("[")
'LEFT SQUARE BRACKET'
py> unicodedata.name("{")
'LEFT CURLY BRACKET'

As are angle brackets:

py> unicodedata.lookup("LEFT ANGLE BRACKET")
'〈'
py> unicodedata.lookup("RIGHT ANGLE BRACKET")
'〉'

HTML uses ASCII less-than and greater-than signs as angle brackets.

Physicists even have a pun about them, with "bra-ket" notation for 
quantum state:

http://en.wikipedia.org/wiki/Bra-ket_notation

There are a number of other types of brackets with more specialised 
uses, or common in Asian texts. See http://en.wikipedia.org/wiki/Bracket

By the way, the word "bracket" itself is derived from the French and 
Spanish words for "codpiece". That's not relevant to anything, I just 
thought I'd mention it.


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


Re: [Tutor] code works in windows command but not ubuntu terminal

2014-01-24 Thread eryksun
On Fri, Jan 24, 2014 at 8:38 PM, Steven D'Aprano  wrote:
>
> However, there's more to it than this. For starters, you need to decide
> on the exact behaviour. Clearly, "file not found" errors should move on
> to try the next prefix in the path list. But what about permission
> denied errors?

Prior to 3.3, I/O operations raise an IOError, except functions in the
os module raise an OSError. Inspect the exception `errno` attribute
for the error code. Named error constants are available in the errno
module.

3.3 makes IOError an alias for OSError and maps several of the more
common error codes to subclasses that have descriptive names. Here are
some examples where 3.3 OSError returns a sublcass:

>>> for code in [errno.EPERM, errno.ENOENT,
...  errno.EACCES, errno.EISDIR]:
... OSError(code, os.strerror(code))
...
PermissionError(1, 'Operation not permitted')
FileNotFoundError(2, 'No such file or directory')
PermissionError(13, 'Permission denied')
IsADirectoryError(21, 'Is a directory')
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Iterator vs. iterable cheatsheet, was Re: iter class

2014-01-24 Thread spir

On 01/25/2014 04:13 AM, eryksun wrote:

On Fri, Jan 24, 2014 at 8:50 AM, spir  wrote:


xs is an iterator (__next__ is there), then Python uses it directly, thus
what is the point of __iter__ there? In any case, python must check whether


Python doesn't check whether a type is already an iterator. It's
simpler to require that iterators implement __iter__, like any other
non-sequence iterable. This technically allows an iterator to return a
new iterator when __iter__ is called:

 class C:
 def __iter__(self): return D()
 def __next__(self): return 'C'

 class D:
 def __iter__(self): return C()
 def __next__(self): return 'D'

 it1 = iter(C())
 it2 = iter(it1)

 >>> next(it1)
 'D'
 >>> next(it2)
 'C'

That said, it's supposed to return self.


All right, thank you, Peter & Eryksun.

d

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