Re: Objects in Python

2012-08-22 Thread shaun
Here is some code: 
//This is the object I want to create:
#!/usr/bin/python
import cx_Oracle
import sys
import time
import datetime


class batchParam:

def __init__(self,array):
self.array=array


def breakuparray(self):
for row in self.array:
mer = row[0].ljust(25, ' ')
merc = row[1].ljust(13, ' ')
mertype = row[2]
merloc = row[3]
mercount = row[4]
mersec = row[5]
acq = row[6]



def returnBatch(self):
self.breakuparray()
return "\x01001\x0251.%s%s%s%s%s%s%s%s\x03"  % (mer, merc, 
mertype, merloc, mercount, mersec, acq);


//Here is the script I want to run the 
object in:


#!/usr/bin/python
import cx_Oracle
import sys
import time
import datetime
sys.path.append("C:\\Documents and Settings\\swiseman\\Desktop")
from batchParam import batchParam

term = sys.argv[1]
batch = sys.argv[2]

con = cx_Oracle.connect('databaseInfo')


cur = con.cursor()
cur.execute("SELECT * FROM SOME_TABLE))

results = cur.fetchall()

batchParam(results)
Batch=batchParam.returnBatch

print Batch

cur.close()

//

Thanks,
Shaun
-- 
http://mail.python.org/mailman/listinfo/python-list


Need help fixing this error please:NameError: global name is not defined

2012-09-06 Thread shaun
Hi all,

 I have a class which I create an object from in a different script but when 
its run I get an error at the last part of this method:

CODE///

def databasebatchcall(self,tid, bid):

con=cx_Oracle.connect('user/[email protected]/ODB4TEST.COMPANY.COM')
cur = con.cursor()
cur.execute("SELECT * FROM name)
results = cur.fetchall()
//

>From this last line I get the following error which I don't understand I'm 
>very new to python and have no idea about this any help would be appreciated


//

File "/home/dcroke/mdcFDACStringCall.py", line 21, in fetchbatchdata
results = cur.fetchall()
NameError: global name 'cur' is not defined

//////


Thanks all, 
Shaun
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need help fixing this error please:NameError: global name is not defined

2012-09-06 Thread shaun
Sorry guys here is the full code for the class:

#!/usr/bin/python
# Echo client program
import cx_Oracle
import socket
import pprint
from struct import *
import sys
from binascii import *
import time
import datetime


class StringCall:
results=[]
def databasebatchcall(self,termid, batchid):
con = 
cx_Oracle.connect('user/[email protected]/ODB4TEST.COMPANY.IE')
cur = con.cursor()
cur.execute("SELECT * from name)
results = cur.fetchall()


def fetchbatchdata(self,results):

for row in results:
mer = row[0].ljust(25, ' ')
mercity = row[1].ljust(13, ' ')
mertype = row[2]
merloc = row[3]
mercount = row[4]
mersec = row[5]
acq = row[6]
btime = row[7].strftime('%d%m')
bmerch = str(row[8]).rjust(12, '0')
termcur = row[9]
acqbank = str(row[10]).rjust(24, '0')
termtype = row[11]
termsoftver = row[12]
merbatch = str(row[13]).rjust(3, '0')
reccount = str(row[14]).rjust(9, '0')
amounttotal = str(row[15]).rjust(16, '0')
cashback = str(row[16]).rjust(16, '0')
deposit = str(row[17]).rjust(16, '0')

def createbatchstrings(self):
BatchHeaderPacket = 
"\x01000\x0251.520%s0%s06060001%s%s%s%s0003 \x03" % (btime, bmerch, 
termcur, acqbank, termtype, termsoftver);
ParameterPacket = 
"\x01001\x0251.530401%s%sIE%s%s%s0%s%s0%s\x03" % 
(mer, mercity, mertype, merloc, termid, mercount, mersec, acq);
TrailerPacket = 
"\x01003\x0251.550%s00%s%s%s%s%s000\x03" % (btime, merbatch, reccount, 
amounttotal, cashback, deposit);
cur.close()

def returnbatchheader(self):
return BatchHeaderPacket
def returnparameterpacket(self):
return ParameterPacket
def returntrailerpacket(self):
return TrailerPacket



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


Re: Need help fixing this error please:NameError: global name is not defined

2012-09-06 Thread shaun
This is the code in the script im calling:


batchObject=StringCall()
batchObject.databasebatchcall(termid, batchid)
batchObject.fetchbatchdata()
batchObject.createbatchstrings()

BatchHeaderPacket =batchObject.returnbatchheader()
ParameterPacket =batchObject.returnparameterpacket()
TrailerPacket =batchObject.returntrailerpacket()

print BatchHeaderPacket
print ParameterPacket
print TrailerPacket
-- 
http://mail.python.org/mailman/listinfo/python-list


Setting up a class

2012-09-06 Thread shaun
Hi all,

So I'm trying to to OO a script which is currently in place on work. It 
connects to the database and makes multiple strings and sends them to a server.

But I'm having major problems since I am new to python I keep trying to do it 
as I would do it in Java but classes seem to be very different. I was wondering 
could someone answer a few questions? 

1) Is there anything I should know about passing in variables from another 
script to the class?

2) When I'm passing variables back to the script they seem to come back blank 
as if I haven't done it correctly (I declare the empty variable at the top of 
the class, I use the information I get from the database to fill it and I send 
it back) Is there anything I'm not doing right with this.

3)When I want to use a method from a class in another class method it never 
seems to work for me, I have a feeling this is to do with "self" but im not too 
sure??

Any help would be appreciated.
Thanks,
Shaun
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need help fixing this error please:NameError: global name is not defined

2012-09-06 Thread shaun
Hi Chris,

 I'm changing it into multiple classes because the script is going to get 
much larger its more for maintainability reasons rather than functionality 
reasons.

Thanks so much man it was the "self" fix you stated above. I woe you a pint of 
Guinness :D

Thanks again,
Shaun
-- 
http://mail.python.org/mailman/listinfo/python-list


overload builtin operator

2005-08-25 Thread Shaun
Hi,

I'm trying to overload the divide operator in python for basic arithmetic.
eg. 10/2 ... no classes involved.

I am attempting to redefine operator.__div__ as follows:

 # my divide function
 def safediv(a,b):
 return ...

 # reassign buildin __div__
 import operator
 operator.__div__ = safediv

The operator.__dict__ seems to be updated OK but the '/' operator still  
calls buildin __div__

Does anyone know if this is possible and if I'm going along the correct  
path with my attempts above?
Is it possible to do this using a C extention?

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


Re: overload builtin operator

2005-08-26 Thread Shaun
Thanks for your replies, obviously this isn't a simple thing to do so
I'll take a different tack.

The exact problem I am trying to solve here is to avoid the
ZeroDivisionError in division.
I have c++ code which delegates to python to calculate expressions on
table cells. The values of the table cell are arbitary numbers and the
expressions to be calculated are fairly simple python arithmetic and
math functions.

The problem being that some users want an expression like '(100/x)+ 3'
where x=0 to return 3. So that dividing a number by zero results in 0.

Apart from parsing the expression string myself and checking for divide
by zero I can't find another way to solve the problem.

Hopefully someone out there has some ideas.
Thanks,
Shaun.

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


Noobie Question: Using strings and paths in mkdir (os.mkdir("/test/"a))

2005-04-22 Thread Shaun
Hello!

I can't seem to get paths and variables working together:

import os
a = 'books'
os.chdir( '/test')
os.mkdir("/test/"a)

the last line does not seem to work.  os.mkdir(a) makes the directory
books, but i want this directory as a subdirectory of test.

I also tried: os.mkdir("/test/",a), and trying to make b = 'test' and
then os.mkdir(a b).

Does someone have any ideas or a link they can give me, I looked under
strings in the python tutorial and library manual but I guess not in
the right spot.

Thanks for your help!

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


Dictionary with Lists

2009-10-03 Thread Shaun
Hi,

I'm trying to create a dictionary with lists as the value for each
key.  I was looking for the most elegant way of doing it... I thought
this would work:

testDict = {}
...
testDict [1] = testDict.get (1, []).append ("Test0")  # 1 does not
exist, create empty array
print testDict
testDict [1] = testDict.get (1, []).append ("Test1")
print testDict

(Obviously I wouldn't normally write code like this.. but this is how
it would unfold in a loop)
However, the first printout gives {1: None}  instead of the desired
{1: ['test']}.  What's wrong with this syntax?

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


Re: Dictionary with Lists

2009-10-04 Thread Shaun
Okay that makes sense.  I was assuming that list.append returned the
new list.

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


subprocess module: execution of standard binaries without shell?

2009-02-26 Thread Visco Shaun
hi all

while getting used to with subprocess module i failed in executuing a)
but succeeded in running b). Can anyone explain me why as i am providing
absolute path? Is this has to do anything with shared library.. which
must be accessed based on system variables?


a) pipe = subprocess.Popen("/bin/ls /", stdout=subprocess.PIPE,
close_fds=True)
==>OSError: [Errno 2] No such file or directory

b) pipe = subprocess.Popen("/bin/ls /", stdout=subprocess.PIPE,
close_fds=True, shell=True)

-- 
Thanks & Regards
visco

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


to get name of file opened

2009-03-28 Thread Visco Shaun
Hi

Is there any way to get the name of the file opened from the file object
'f' which i get through the code 

f = os.fdopen(os.open("trial', os.O_WRONLY|os.O_CREAT), "w")

The situation will be like i can access only the above variable 'f'.
f.name is having '' instead of filename 'trial'

Or if not possible can anyone suggest a solution where my requirements
are
a) i need file access through os module
b) i need file object and not file descriptor as its more easy to use

-- 
Thanks & Regards
visco

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


difference between os.fdopen and builtin open

2009-03-28 Thread Visco Shaun
Hi

I was wondering the difference between os.fdopen()(or os.open() not
considering the difference in args) and builtin open(). Can anyone help
me?
-- 
Thanks & Regards
visco

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


Re: to get name of file opened

2009-03-28 Thread Visco Shaun
First of all
Thanks Dave for the reply
On Sat, 2009-03-28 at 09:51 -0500, Dave Angel wrote:
> First question is why you need os.open(), and not the open() function.  
> I'll guess that you need some of the access modes (e.g. for file 
> sharing) that you get from the low level functions.  So assuming that:
of course access was an issue.. but i opted for it because in the
document it is mentioned that os.open(besides sharing b/w processes) is
low level resembling unix system call, and i thought may be efficiency
is more, even though interpreted by the same python interpreter...
But i never found any article regarding any efficiency difference
between those..
Can u comment on this
> I don't believe there's any way to use a fd ("file descriptor") to 
> retrieve the file name that was perhaps passed to open.  There are ways 
> to spelunk inside Windows, but they're not advisable. And I don't know 
> what Unix might offer there.
> 
> So by the time fdopen() is invoked, the name is already gone.
> 
> Here's what I'd do.  Create your own open function that has the 
> parameters of os.open(), but that will return an object derived from the 
> file object.  Your derived object can have its own filename, but close() 
> will know what to do.
Well this is what i am trying now, though i wished if there was any
suitable method or attribute built within python
> 
> Alternatively, you could encapsulate the line you showed, and just zap 
> the name attribute of the existing file object.right when it's being 
> returned by fdopen()
The name attribute appears to be read only as i tried that already
though through the interpreter rather than in code


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


how to know the importing file name from an imported file?

2009-04-17 Thread Visco Shaun
Hi

Is there a way to know the name of the script(say A), which is importing
a module(say B), from B?
ie in above situation i should be able to get name 'A' through some way
in B, when A contains an 'import B' statement.
-- 
Thanks & Regards
visco

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


Any adv. in importing a module and some objects in the same module, into the same file?

2009-04-18 Thread Visco Shaun
http://docs.python.org/library/logging.html

While going thr' the above link i came across import statements 
"import logging
import logging.handlers"

What is the use of second import as the first import will be
enough(AFAIK) to access anything intended by the second import?
Is there any kind of advantage?
-- 
Thanks & Regards
visco

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


"TypeError: 'int' object is not callable"

2009-06-01 Thread Visco Shaun
when I was executing the below code I got "TypeError: 'int' object is
not callable" exception. Why is it so?

if type(c) == type(ERROR):

c can be a string or an integer representing an error
-- 
Thanks & Regards
visco

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


different behaviour for user defined exception with attribute args

2009-09-29 Thread Visco Shaun
Hi all

For an exception defined as below

class OptionError(Exception):
def __init__(self, args):
self.args = args
def __str__(self):
return repr(self.v)

an iteration is happening when the exception is raised

Meanwhile for almost the same structured exception replacing the
attribute 'args' with say 'value' it is not a probs.

class OptionError(Exception):
def __init__(self, args):
self.value = args
def __str__(self):
return repr(self.value)

This was frustrating because for a st. OptionError('Error') for
exception 1 output will be

OptionError: ('E', 'r', 'r', 'o', 'r') 

Meanwhile for exception 2 output will be 

OptionError: 'Error'

which is desired..Why this behaviour?

Regards
Visco

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