Re: TypeError: an integer is required

2009-11-30 Thread Nobody
On Sun, 22 Nov 2009 18:29:25 +, MRAB wrote:

 os.open(C://Users//lutfi//Documents//te//log.txt , a )

 open(C://Users//lutfi//Documents//te//log.txt , a )

Backslashes need to be doubled; forward slashes don't.


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


TypeError: an integer is required

2009-11-22 Thread Lutfi Oduncuoglu
Hello,

I am a newbie on oython and I am taking the error at subject my code is
below, I am trying to develop a qgis plugin and lines begin with # is the
thing that I tried. Thus sys.stdout gives the type error. When I comment
that line it turns an error like below. What may be the problem? thanks for
help:)

...\ReadData.py, line 128, in run
print %d %s %(k, attr.toString())
IOError: [Errno 9] Bad file descriptor




# Import the PyQt and QGIS libraries
from PyQt4 import QtCore, QtGui

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from os import *
from qgis.gui import *
import sys
import pdb
# Initialize Qt resources from file resources.py
import resources
# Import the code for the dialog
from ReadDataDialog import ReadDataDialog

class ReadData:

  def __init__(self, iface):
# Save reference to the QGIS interface
self.iface = iface

  def initGui(self):
# Create action that will start plugin configuration
self.action = QAction(QIcon(:/plugins/readdata/icon.png), \
Read shp for calculations, self.iface.mainWindow())
# connect the action to the run method
QObject.connect(self.action, SIGNAL(triggered()), self.run)

# Add toolbar button and menu item
self.iface.addToolBarIcon(self.action)
self.iface.addPluginToMenu(Read shp for calculations, self.action)

  def unload(self):
# Remove the plugin menu item and icon
self.iface.removePluginMenu(Read shp for calculations,self.action)
self.iface.removeToolBarIcon(self.action)

  # run method that performs all the real work
  def run(self):

#fileName =
QFileDialog.getOpenFileName(None,QString.fromLocal8Bit(Select a file:),,
*.shp *.gml)

#if fileName.isNull():

#  QMessageBox.information(None, Cancel, File selection canceled)




#else:

#  print fileName


vlayer =
QgsVectorLayer(C:\\Users\\lutfi\\Documents\\te\\deneme2\\ownership.shp,
hebe, ogr)
print vlayer.source()
print vlayer.featureCount()
QgsMapLayerRegistry.instance().addMapLayer(vlayer)
QMessageBox.information(self.iface.mainWindow(), info, file:
+str(vlayer.source())+ is added.)





if not vlayer.isValid():
  print Couldn't open the layer
  pdb.set_trace()

else:

#QMessageBox.information(None, Cancel, File selection canceled)

  provider = vlayer.dataProvider()
  feat = QgsFeature()
  allAttrs = provider.attributeIndexes()
  provider.select(allAttrs)

  while provider.nextFeature(feat):
geom = feat.geometry()
import sys
import os
#
win32api.SetFileAttributes('C://Users//lutfi//Documents//te//log.txt',
win32con.FILE_ATTRIBUTE_NORMAL)
#sys.stdout = open('C://Users//lutfi//Documents//te//log.txt',
777 )
print geom
#QMessageBox.information(None, Cancel, File selection canceled)
print Feature ID %d:  % feat.id()
if geom.type() == QGis.Point:
  x = geom.asPoint()
  print Point:  + str(x)

elif geom.type() == QGis.Line:
  x = geom.asPolyline()
  print Line: %d points % len(x)

elif geom.type() == QGis.Polygon:
  x = geom.asPolygon()
  numPts = 0
  for ring in x:
numPts += len(ring)
print Polygon: %d rings with %d points % (len(x), numPts)
else:
  print Unknown

attrs = feat.attributeMap()

for (k,attr) in attrs.iteritems():
  sys.stdout =
os.open(C://Users//lutfi//Documents//te//log.txt , a )
  print %d %s %(k, attr.toString())
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: TypeError: an integer is required

2009-11-22 Thread MRAB

Lutfi Oduncuoglu wrote:

Hello,

I am a newbie on oython and I am taking the error at subject my code is 
below, I am trying to develop a qgis plugin and lines begin with # is 
the thing that I tried. Thus sys.stdout gives the type error. When I 
comment that line it turns an error like below. What may be the problem? 
thanks for help:)


...\ReadData.py, line 128, in run
print %d %s %(k, attr.toString())
IOError: [Errno 9] Bad file descriptor



[snip]


for (k,attr) in attrs.iteritems():
  sys.stdout = 
os.open(C://Users//lutfi//Documents//te//log.txt , a )

  print %d %s %(k, attr.toString())
 

I think the problem is that you're binding a low-level file id to
sys.stdout instead of a file object. Try:

   sys.stdout = 
open(C://Users//lutfi//Documents//te//log.txt , a )


Actually, changing sys.stdout just to use print a single string is a bad
idea. Try this instead:

  log_file = 
open(C://Users//lutfi//Documents//te//log.txt , a )

  print  log_file, %d %s %(k, attr.toString())
  log_file.close()
--
http://mail.python.org/mailman/listinfo/python-list


Re: TypeError: an integer is required

2009-11-22 Thread Dave Angel



Lutfi Oduncuoglu wrote:

Hello,

I am a newbie on oython and I am taking the error at subject my code is
below, I am trying to develop a qgis plugin and lines begin with # is the
thing that I tried. Thus sys.stdout gives the type error. When I comment
that line it turns an error like below. What may be the problem? thanks for
help:)

...\ReadData.py, line 128, in run
print %d %s %(k, attr.toString())
IOError: [Errno 9] Bad file descriptor

  
(Comment #2 is critical, but I didn't notice it right away.  Remove that 
line   from os import *  and use import os  instead.  )


This error is caused by binding the wrong kind of open to stdout.  
os.open() returns a file descriptor (integer), while stdout is expected 
to be a file object.


As for your previous error an integer is required it'd certainly be 
nice if you showed us the error message.  You say it was on a line 
beginning with # but there are many of those, none of them near the 
present error.


Taking a wild guess, I see another open for sys.stdout:

sys.stdout = open('C://Users//lutfi//Documents//te//log.txt', 777 )

But that line causes a file() argument 2 must be string, not int  
error.  You want w or a or something like that, rather than a 
mysterious 777 for that.


Just randomly jumping around in your code, consider what happens if you 
have the following line:


print %d %s %(k, attr)

and k is a string.  Then you'd get:   %d format: a number is required, 
not str


Well, enough guessing.  You need to give a complete traceback, as you 
did for the bad file descriptor


And you'd also need to specify Python version, as the messages might be 
different between your version and the 2.6 that I'm trying it with.


Other comments:

1) Are you coming from a java background?  In Python you do not need to 
put everything in a class definition.


2) the   from  import *   form is discouraged, and you use it 
several times.  I know some GUI packages specify it, and there you're 
best off by following their conventions.  But for modules like os, 
you're asking for trouble.  In this case, the built-in open() function 
that you need is being hidden by the os.open() call, which is not the 
one you wanted.  So you'll get syntax errors and funny runtime errors, 
just because you're calling different functions that what you think you are.


3) You have import within other defs.  This is unnecessary if the module 
has already been imported (such as sys), and is considered bad form in 
almost all cases.
There are only two common cases where an import might not appear right 
at the top of the module:
  A) when you want to conditionally import a module, based on some 
runtime choice.  Example would be to import a Unix version or a Windows 
version of some module.
   B) When a module will only be used in some unlikely case, such as in 
error handling.
   C) when a module is very slow to import, and you need to speed up 
startup time.


4) Simplified example code would do two things:   
   A) easier for us to follow, especially those of us that don't happen 
to use the same libraries that you do.
   B) by simplifying it, you can frequently find the problem yourself, 
which is a great way to learn


5) If you have two different error messages, but only have one version 
of the code, make both tracebacks complete, and make sure we know what's 
changed between the two versions.



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