Re: [Tutor] factorial of anumber

2012-02-04 Thread Blockheads Oi Oi

On 04/02/2012 10:11, Debashish Saha wrote:

_PROGRAM TO FIND FACTORIAL OF A NUMBER(I HAVE WRITTEN IT ON GEDIT)_
x=1
n=input('enter a positive integer no:')
for i in range(1,1+n):
 x=x*i
print x


_ERROR:_

enter a positive integer
no:---
EOFError  Traceback (most recent call last)
C:\Python27\lib\site-packages\IPython\utils\py3compat.pyc in
execfile(fname, glob, loc)
 166 else:
 167 filename = fname
--> 168 exec compile(scripttext, filename, 'exec') in glob, loc
 169 else:
 170 def execfile(fname, *where):

C:\Users\as\mnb.py in ()
   1 x=1
> 2 n=input('enter a positive integer no:')
   3 for i in range(1,1+n):
   4 x=x*i
   5 print x

EOFError: EOF when reading a line

*QUESTION*:
HOW TO ASK INPUT FROM USER THEN?


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


It works fine for me.

>>> enter a positive integer no:>>> 6
720

And please DO NOT USE CAPITAL LETTERS or people may stop answering your 
questions.


--
Cheers.

Mark Lawrence.

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


Re: [Tutor] Importing libraries

2012-02-04 Thread Blockheads Oi Oi

On 04/02/2012 05:37, Michael Lewis wrote:

Why don't I have to import str or list to access their attributes like I
do with the math or random or any other library?

--
Michael J. Lewis
mjole...@gmail.com 
415.815.7257



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


They're built in to Python so...

PythonWin 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit 
(Intel)] on win32.
Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin' 
for further copyright information.

>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'pywin']
>>> help(__builtins__)
Help on built-in module __builtin__:

NAME
__builtin__ - Built-in functions, exceptions, and other objects.

FILE
(built-in)

DESCRIPTION
Noteworthy: None is the `nil' object; Ellipsis represents `...' in 
slices.


CLASSES
object
basestring
str
str
unicode
buffer
etc.

--
Cheers.

Mark Lawrence.

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


Re: [Tutor] (no subject)

2012-02-03 Thread Blockheads Oi Oi

On 03/02/2012 20:32, Debashish Saha wrote:

what is the basic difference between numpy and pylab?



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


http://catb.org/esr/faqs/smart-questions.html

--
Cheers.

Mark Lawrence.

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


Re: [Tutor] ASCII Conversion

2012-01-30 Thread Blockheads Oi Oi

On 31/01/2012 05:33, Christian Witts wrote:

On 2012/01/31 06:50 AM, Michael Lewis wrote:

I am trying to do a simple test but am not sure how to get around
ASCII conversion of characters. I want to pass in y have the function
test to see if y is an integer and print out a value if that integer
satisfies the if statement. However, if I pass in a string, it's
converted to ASCII and will still satisfy the if statement and print
out value. How do I ensure that a string is caught as a ValueError
instead of being converted?

def TestY(y):
try:
y = int(y)
except ValueError:
pass
if y < -1 or y > 1:
value = 82
print value
else:
pass

--
Michael J. Lewis
mjole...@gmail.com 
415.815.7257



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

If you just want to test if `y` is an integer you can do so with
`type(y) == int`, and to get the ASCII value of a character you can use
`ord` like `ord('a') == 97`. And how to avoid your ValueError with a bad
conversion, do your type checking before hand.

Hope that helps.
--

Christian Witts
Python Developer
//



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


The test of y would not normally be written as it is, comparisons can be 
chained see http://docs.python.org/reference/expressions.html#not-in.
Also Python tends to use EAFP rather than LBYL see 
http://docs.python.org/glossary.html.


--
Cheers.

Mark Lawrence.

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


Re: [Tutor] compile time calculator

2012-01-27 Thread Blockheads Oi Oi

On 27/01/2012 15:46, Surya K wrote:

Hi,

I want to calculate compile time for my puzzles. Although I read about
timeit(), I didn't really understand how it should be applied it.
So, can anyone write a function for me please!!

I am looking for a function which should solve my puzzle and also show
compile time. (Later, I should be able to delete that function before
submitting my code)

The following way is what I am exactly looking at:

define a compile time function such that it should take all my puzzle
code and provide compile time for it.

(I think timeit() should be called at each statement, which is not
flexible, don't know exactly.)

can anyone write a program for me? please...


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


Nobody will write code for you unless you show that you've made an 
attempt yourself.  Perhaps you're looking for the standard profile 
module?  Or trying optimization prematurely?  Or what?  Please give more 
information about the precise nature of your problem and you will get 
many very useful answers.


--
Cheers.

Mark Lawrence.

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


Re: [Tutor] Pythonic way of concatenation of elements in an array

2012-01-27 Thread Blockheads Oi Oi

On 27/01/2012 06:44, Andre' Walker-Loud wrote:

Hi Steven,


(5) When assembling strings from substrings, never use repeated concatenation 
using + as that can be EXTREMELY slow. Use str.join to build the string in one 
assignment, instead of multiple assignments.

Your code shown above is *very* inefficient and will be PAINFULLY slow if m is 
very large. To understand why, you should read this article:

http://www.joelonsoftware.com/articles/fog000319.html

In this case, you can replace your snippet with this:

result = '-'.join(str(item) for item in m[1:])


This was an interesting article.  I have only had one programming class, and 
that was 15 years ago or so, so these are not issues I am aware of.

I often find myself joining strings (and have mostly used + to do it).  An 
alternate method I use is, for eg.


print('here is a string %s which has many variables %s %s %s I have to sort 
out' %(s1,s2,s3,s4))


where the various strings (s1 - s4) have been determined elsewhere, perhaps in 
a loop.

Is this method any better at combining strings than the +?  My first guess 
would be no, but that is really just a guess.


Thanks,

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



You might like to take a look here 
http://wiki.python.org/moin/PythonSpeed/PerformanceTips#String_Concatenation

--
Cheers.

Mark Lawrence.

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


Re: [Tutor] OverflowError in lucky numbers script

2012-01-22 Thread Blockheads Oi Oi

On 23/01/2012 06:15, Shreesh bhat wrote:

Calculating the table is fast.
I think either my luckiness test (where i find the sum of all digits and
sum of squares of all digits of a large number)
or generating numbers is slow.



Don't think, know :) Tools like the profile or timeit modules are there 
for a purpose so use them.


Cheers.

Mark Lawrence.


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


Re: [Tutor] PyVISA

2011-04-01 Thread Blockheads Oi Oi

On 01/04/2011 20:10, Alan Gauld wrote:


"Mark R Rivet"  wrote


Well I tried the setup file and here is what I get:
C:\Users\Rivetmr\PyVisa_1.3\PyVISA-1.3>Python setup.py install
Traceback (most recent call last):
File "setup.py", line 60, in 
home_dir = os.environ['HOME']


Windows doesn't by default define a HOME environment variable.
Are you sure your module is not Unix specific?

If not then the easiest solution is probably to just define a value for
HOME... and see if it works!

HTH,

Alan G.

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



I think someone has already said this, but just to be sure there's a 
Windows executable here 
http://sourceforge.net/projects/pyvisa/files/PyVISA/1.3/


HTH.

Mark L.

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


Re: [Tutor] Run application from MS-DOS with sys.argv

2011-04-01 Thread Blockheads Oi Oi

On 01/04/2011 18:15, Susana Iraiis Delgado Rodriguez wrote:

Hello!

I'm going to answer to the question I posted in this list.
After a search trouhg web and analize the code, I just had to add a
try/except statement. Now I get what I need. Here is the information:

directorio = sys.argv[1]
extension = sys.argv[2]
csv_salida = sys.argv[3]
#Here I add the line:
try:
  if len(sys.argv) == 4:
  
  
else:
 print "Tus argumentos no son correctos"
and here:
except IndexError:
 print "Tus argumentos no son correctos"



You appear to be mixing two ways of doing the same thing.  Either check 
the number of entries in sys.argv or catch the index error, don't do 
both.  Read this, it probably explains things better than I can 
http://mail.python.org/pipermail/python-list/2003-May/203039.html




2011/3/29 Susana Iraiis Delgado Rodriguez mailto:susana.delgad...@utzmg.edu.mx>>

Hello List:

I developed a script to walk through a specific directory in my PC
and look for files with the same extension (.shp). I want the user
to enter from MS-DOS and write the the directory, file extension and
csv filename.
My script reads the arguments from Windows console, but when I
opened the txt file and csv file I noticed that isn't walking
through all the root I wrote in MS-DOS. This is my module:

import os, csv, time, socket, sys
from osgeo import ogr,gdal,osr
#This should be the order for the arguments('csv_args.py [root]
[file_extension] [csv filename]')
#The user is typing python csv_args.py C:\ .shp csv.csv
directorio = sys.argv[1]
extension = sys.argv[2]
csv_salida = sys.argv[3]
if len(sys.argv) == 4:
 print 'Iniciando...'
 gdal.AllRegister()
 file_list = []
 folders = None
 for root, folders, files in os.walk(directorio):
 file_list.extend(os.path.join(root,fi) for fi in files if
fi.endswith(extension))
 f = open(csv_salida, 'wb')
 log = open ('errores.txt','w')
 writer = csv.writer(f)
 ruta = 'Ruta'
 archivo = 'archivo'
 x_min = 'x_min'
 x_max = 'x_max'
 y_min = 'y_min'
 y_max = 'y_max'
 geometria = 'geometria'
 num_elem = 'num_elem'
 prj = '.prj'
 proyeccion = 'proyeccion'
 fecha = 'fecha_modificacion'
 maq = 'maquina_host'
 usu = 'usuario'
 campos =

[ruta,archivo,x_min,x_max,y_min,y_max,geometria,num_elem,prj,proyeccion,fecha,maq,usu]
 writer.writerow(campos)
 for row, filepath in enumerate(file_list, start=1):
 (ruta, filename) = os.path.split(filepath)
 shapeData = ogr.Open(filepath)
 shp = 'Error al abrir el archivo' +filepath
 if shapeData is None:
 print shp
 log.write(shp+"\n")
 else:
 layer = shapeData.GetLayer()
 feature = layer.GetNextFeature()
 x_y = layer.GetExtent()
 x1 = x_y[0]
 x2 = x_y[1]
 y1 = x_y[2]
 y2 = x_y[3]
 defn = layer.GetLayerDefn()
 geo = defn.GetGeomType()
 cuenta = layer.GetFeatureCount()
 proy = layer.GetSpatialRef()
 prjtext = ''+str(proy)+''
 n = os.path.splitext(filepath)
 p = n[0]+'.prj'
 shx = n[0]+'.shx'
 dbf = n[0]+'.dbf'
 filepath = ''+filepath+''
 filename = ''+filename+''
 t = time.strftime("%m/%d/%Y %I:%M:%S
%p",time.localtime(os.path.getmtime(filepath)))
 modificacion = ''+t+''
 usuario = os.environ.get("USERNAME")
 user = ''+usuario+''
 host = socket.gethostname()
 maquina = ''+host+''
 if os.path.exists(shx):
 print 'El archivo ' +shx +' existe'
 else:
 og.write('No existe el archivo ' +shx+"\n")
 if os.path.exists(dbf):
 print 'El archivo ' +dbf +' existe'
 else:
 log.write('No existe el archivo ' +dbf+"\n")
 if os.path.exists(p):
 aRow= [ filepath, filename, x1, x2, y1, y2, geo,
cuenta, 1, prjtext, modificacion, maquina, user]
 writer.writerow(aRow)
 else:
 aRow1= [ filepath, filename, x1, x2, y1, y2, geo,
cuenta, 0, prjtext, modificacion, maquina, user]
 writer.writerow(aRow1)
 log.close()
 f.close()
 print "El archivo esta listo"
else:
 print "Tus argumentos no son correctos"

___
Tutor maillist  -  Tutor@python.org
To 

Re: [Tutor] PyVISA GPIB

2011-04-01 Thread Blockheads Oi Oi

On 01/04/2011 18:00, Donald Bedsole wrote:

Hi Mark,

On Fri, Apr 1, 2011 at 11:42 AM,  wrote:

  I would like to control electronic instruments with PyVISA. I have downloaded 
PyVISA and unpacked the files into the Python27/lib/site-packages dir and in 
the IDLE
GUI I run "import visa' for a quick check and I get this error:

import visa

Traceback (most recent call last):
  File "", line 1, in
import visa
ImportError: No module named visa

I'm scratching my head. Help

Mark R Rivet, Genesis Software Consulting
ASCT(Computer Technologies), BSIT/SE(Software Engineering)
Electrical Engineering Technician
Member IEEE, Computer Society


Do or do not; there is no try.


Could this be the problem?


No.  The problem above is at a higher level.  This is what you get at a 
lower level trying to import the code.  At least I think so :)


c:\Users\Mark\Cashflow\Python>python -V
Python 2.7.1

c:\Users\Mark\Cashflow\Python>python -c "import visa"
Traceback (most recent call last):
  File "", line 1, in 
  File "c:\python27\lib\site-packages\visa.py", line 1, in 
from pyvisa.visa import *
  File "c:\python27\lib\site-packages\pyvisa\visa.py", line 231, in 


resource_manager = ResourceManager()
  File "c:\python27\lib\site-packages\pyvisa\vpp43.py", line 105, in 
__new__

it.init(*args, **kwds)
  File "c:\python27\lib\site-packages\pyvisa\visa.py", line 227, in init
self.session = self.vi = vpp43.open_default_resource_manager()
  File "c:\python27\lib\site-packages\pyvisa\vpp43.py", line 758, in 
open_default_resource_manager

visa_library().viOpenDefaultRM(byref(session))
  File "c:\python27\lib\site-packages\pyvisa\vpp43.py", line 175, in 
__call__

self.load_library()
  File "c:\python27\lib\site-packages\pyvisa\vpp43.py", line 141, in 
load_library

self.__lib   = windll.visa32
  File "c:\python27\lib\ctypes\__init__.py", line 423, in __getattr__
dll = self._dlltype(name)
  File "c:\python27\lib\ctypes\__init__.py", line 353, in __init__
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 126] The specified module could not be found



PyVISA doesn’t implement VISA itself. Instead, PyVISA provides
bindings to the VISA library (a DLL or
“shared object” file). This library is usually shipped with your GPIB
interface or software like LabVIEW. Alternatively, you can download it
from your favourite equipment vendor (National Instruments, Agilent,
etc).

quote from this document:

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


Cheers.

Mark L.


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


Re: [Tutor] Data frame packages

2011-03-31 Thread Blockheads Oi Oi

On 31/03/2011 09:38, Ben Hunter wrote:

Is anybody out there familiar with data frame modules for python that
will allow me to read a CSV in a similar way that R does? pydataframe
and DataFrame have both befuddled me. One requires a special stripe of R
that I don't think is available on windows and the other is either very
buggy or I've put it in the wrong directory / installed incorrectly.
Sorry for the vague question - just taking the pulse. I haven't seen any
chatter about this on this mailing list.




What are you trying to achieve?  Can you simply read the data with the 
standard library csv module and manipulate it to your needs?What 
makes you say that the code is buggy, have you examples of what you 
tried and where it was wrong?  Did you install with easy_install or run 
setup.py?




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


Regards.

Mark L.


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


Re: [Tutor] String formatting question.

2011-03-29 Thread Blockheads Oi Oi

On 29/03/2011 20:41, Prasad, Ramit wrote:

Is there a difference (or preference) between using the following?
"%s %d" % (var,num)
VERSUS
"{0} {1}".format(var,num)


Ramit

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



From Python 2.7.1 docs at 
http://docs.python.org/tutorial/inputoutput.html "Since str.format() is 
quite new, a lot of Python code still uses the % operator. However, 
because this old style of formatting will eventually be removed from the 
language, str.format() should generally be used.".


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


Re: [Tutor] Read arguments from command line

2011-03-28 Thread Blockheads Oi Oi

On 28/03/2011 18:24, Susana Iraiis Delgado Rodriguez wrote:

Hello everyone!

I want to run a python script which reads arguments from command line. I
searched in web and the module sys.argv came up. But I don't understand
how it works. I need to know how to pass arguments from command line and
make the python script works from MS-DOS instead of run my program from
the python console.

Is there any documentation?



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
See this from a well known Python tutorial 
http://www.faqs.org/docs/diveintopython/kgp_commandline.html


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


Re: [Tutor] how to join two text files ?

2011-03-27 Thread Blockheads Oi Oi

On 26/03/2011 21:08, Mateusz K wrote:

Hello,

I have many text files, where data is delimited by space.
Each file contain three colums: coordinate x, coordinate y and value for
this location.

I would like to join this data to get one big file which will contain
columns:
coordinate x, coordinate y, value1,value2,..., value_n and save it to
another text file.

I wrote script, but there's some stupid error (like "\n" character although
I have added .rstrip() command).

Coul You tell me what is most convenient way to join this data
(maybe by using csv module?)?

=
Best regards,
Mateusz

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


Maybe use split like this,
'x y 123.456\n'.split()
['x', 'y', '123.456']
then store the x and y coords in a dict with a list of values, perhaps 
sort according to your needs, write output to file.


HTH.

Mark L.

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


Re: [Tutor] if value not in dictionary, do a?

2011-03-25 Thread Blockheads Oi Oi

On 25/03/2011 16:11, Robert Sjoblom wrote:


I received your email just as I left my computer, and didn't want to
type out an answer on my phone (because smartphones aren't smart
enough for that yet), and my first reaction was "why?" followed by "I
need my program to continue even if I get the errors at this part, so
why?" I soon realized that catching all errors would mean never seeing
the errors you might not foresee. I know that I could get either
ValueError or KeyError, but there could be something else somewhere
else in the code that could send an error (unlikely as it is) down
that path and with a blank except I would never actually see that
happen. Am I somewhat close to the real reason why?



If you accidentally create an infinite loop you can't break out of it 
with Ctrl-C or whatever if you're catching all exceptions :)




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