Wednesday, January 22, 2014

comp.lang.python - 26 new messages in 11 topics - digest

comp.lang.python
http://groups.google.com/group/comp.lang.python?hl=en

comp.lang.python@googlegroups.com

Today's topics:

* which data structure to use? - 7 messages, 4 authors
http://groups.google.com/group/comp.lang.python/t/368af4950533eeb0?hl=en
* Flushing out data from Popen buffer - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/f5b445d917b4dd24?hl=en
* Can post a code but afraid of plagiarism - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/bb644bb09fed1442?hl=en
* import file without .py into another module - 7 messages, 4 authors
http://groups.google.com/group/comp.lang.python/t/11627bfd38b84845?hl=en
* use class in class - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/7c013c5946298783?hl=en
* Early retirement project? - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/61d421ed38839cfa?hl=en
* ANN: rom 0.25.0 - Redis object mapper for Python - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/2a3c6aade0cea15f?hl=en
* Diving in to Python - Best resources? - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/8df3a0e5ab12c63a?hl=en
* Add followers at time of import - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/c3de77ae37502292?hl=en
* autoconf tools and python3 3m 3dm - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/760f1993767c2e90?hl=en
* Running pywin32-218.win32-py2.7.exe (win32 package for Python) results in
error "The file exists" - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/025921f89bf5ab56?hl=en

==============================================================================
TOPIC: which data structure to use?
http://groups.google.com/group/comp.lang.python/t/368af4950533eeb0?hl=en
==============================================================================

== 1 of 7 ==
Date: Tues, Jan 21 2014 5:43 am
From: Robert Voigtländer


Am Dienstag, 21. Januar 2014 14:38:34 UTC+1 schrieb Robert Voigtländer:
> > On Tue, Jan 21, 2014 at 03:17:43AM -0800, Robert Voigtl�nder wrote:
>
> >
>
>
>
> > > I have objects like this:
>
> >
>
> > >
>
> >
>
> > > class Node(object):
>
> >
>
> > > def __init__(self, pos, parent, g , h):
>
> >
>
> > > self.pos = pos
>
> >
>
> > > self.parent = parent
>
> >
>
> > > self.g = g
>
> >
>
> > > self.h = h
>
> >
>
> > > self.f = g+h
>
> >
>
> > >
>
> >
>
> > >
>
> >
>
> > > I need to build a "list" of these objects. The amount is unknown.
>
> >
>
> > > On this list I need to regularly
>
> >
>
> > >
>
> >
>
> > > 1. check if a specific item - identified by Node.pos - is in the list.
>
> >
>
> > > 2. find the object with the lowest Node.f attribute and update or remove it
>
> >
>
>
>
>
>
> First thanks to all who responded. Although I only partially understand your answers as I am a Python starter.
>
> What's clear to me is the difference between object and instance of an object. Just didn't explain it well.
>
>
>
> Maybe I give some more info on what I need / want to do. I will also provide a working code example. Should have done this before.
>
>
>
> I would very much appreciate a working example of what you mean. Then I have a chance to understand it. :-)
>
>
>
> I would like to implement a class for a A* pathfinding algorithm. (there are ready libraries out there but I would like to learn it myself) This requires to maintain a list of nodes to be processed and nodes already processed. For new nodes I need to check if they are on one of the lists. I also need to regularly pick the node with the lowest value f from the list.
>
>
>
> Here some working code. For one function I sill need a solution. Any better solution is welcome.
>
>
>
> Thanks
>
> Robert

Sorry - found a bug right after my post.
Here the corrected version.

class Node:
def __init__(self, pos, parent, g , h):
self.pos = pos
self.parent = parent
self.g = g
self.h = h
self.f = g+h


def isinlist(nodeToSeatch):
for item in openlist:
if item.pos == nodeToSeatch: return True
return False


def lowestF():
lowestF = ''
for item in openlist:
if item.f < lowestF:
lowestF = item.f
lowestFItem = item
return lowestFItem

def deleteItemWithPos(pos):
## need this function or different approach
pass

openlist=[]
openlist.append(Node((1,1),None,1,5))
openlist.append(Node((1,2),(1,1),4,6))
openlist.append(Node((1,3),(1,2),9,10))

for item in openlist: print item.pos, item.f

print isinlist((1,1))
print isinlist((1,5))

nextNode = lowestF()
print nextNode.pos, nextNode.f




== 2 of 7 ==
Date: Tues, Jan 21 2014 5:59 am
From: Oscar Benjamin


On Tue, Jan 21, 2014 at 05:38:34AM -0800, Robert Voigtl�nder wrote:
>
> > On Tue, Jan 21, 2014 at 03:17:43AM -0800, Robert Voigtl�nder wrote:
> >
>
> > > I have objects like this:
> >
> > >
> >
> > > class Node(object):
> >
> > > def __init__(self, pos, parent, g , h):
> >
> > > self.pos = pos
> >
> > > self.parent = parent
> >
> > > self.g = g
> >
> > > self.h = h
> >
> > > self.f = g+h
> >
> > >
> >
> > >
> >
> > > I need to build a "list" of these objects. The amount is unknown.
> >
> > > On this list I need to regularly
> >
> > >
> >
> > > 1. check if a specific item - identified by Node.pos - is in the list.
> >
> > > 2. find the object with the lowest Node.f attribute and update or remove it
> >
>
>
> First thanks to all who responded. Although I only partially understand your answers as I am a Python starter.
> What's clear to me is the difference between object and instance of an object. Just didn't explain it well.
>
> Maybe I give some more info on what I need / want to do. I will also provide a working code example. Should have done this before.
>
> I would very much appreciate a working example of what you mean. Then I have a chance to understand it. :-)
>
> I would like to implement a class for a A* pathfinding algorithm. (there are ready libraries out there but I would like to learn it myself) This requires to maintain a list of nodes to be processed and nodes already processed. For new nodes I need to check if they are on one of the lists. I also need to regularly pick the node with the lowest value f from the list.
>
> Here some working code. For one function I sill need a solution. Any better solution is welcome.
>
> Thanks
> Robert
>
> ---------------
> class Node:
> def __init__(self, pos, parent, g , h):
> self.pos = pos
> self.parent = parent
> self.g = g
> self.h = h
> self.f = g+h
>
>
> def isinlist(nodeToSeatch):
> for item in openlist:
> if item.pos == nodeToSeatch: return True
> return False
>
>
> def lowestF():
> lowestF = ''
> for item in openlist:
> if item.f < lowestF: lowestF = item
> return lowestF

The function above is incorrect. I think it should be:

def lowestF():
lowestF = ''
for item in openlist:
if item.f < lowestF:
lowestF = item.f # Note the .f here
return lowestF

>
> def deleteItemWithPos(pos):
> ## need this function or different approach
> pass

def deleteItemWithPos(pos):
for n, item in enumerate(openlist):
if item.pos == pos:
del openlist[n]
return
else:
raise RuntimeError('Not in the list!')


>
> openlist=[]
> openlist.append(Node((1,1),None,1,5))
> openlist.append(Node((1,2),(1,1),4,6))
> openlist.append(Node((1,3),(1,2),9,10))
>
> for item in openlist: print item.pos, item.f
>
> print isinlist((1,1))
> print isinlist((1,5))
>
> nextNode = lowestF()
> print nextNode.pos, nextNode.f

My first suggestion would be to use a dict instead of a list:


class Node:
def __init__(self, pos, parent, g , h):
self.pos = pos
self.parent = parent
self.g = g
self.h = h
self.f = g+h

def isinlist(pos):
return pos in opendict

def lowestF():
return min(opendict.values(), key=lambda x: x.f)

def deleteItemWithPos(pos):
del opendict[pos]

nodes = [
Node((1,1),None,1,5),
Node((1,2),(1,1),4,6),
Node((1,3),(1,2),9,10),
]

opendict = {}
for node in nodes:
opendict[node.pos] = node

for item in opendict.values():
print item.pos, item.f

print isinlist((1,1))
print isinlist((1,5))

nextNode = lowestF()
print nextNode.pos, nextNode.f


The above is more efficient and simpler. It is still O(N) for the lowestF()
function. Changing data structure could make that more efficient.


Oscar




== 3 of 7 ==
Date: Tues, Jan 21 2014 6:09 am
From: Peter Otten <__peter__@web.de>


Robert Voigtländer wrote:

>
>> On Tue, Jan 21, 2014 at 03:17:43AM -0800, Robert Voigtl�nder wrote:
>>
>
>> > I have objects like this:
>>
>> >
>>
>> > class Node(object):
>>
>> > def __init__(self, pos, parent, g , h):
>>
>> > self.pos = pos
>>
>> > self.parent = parent
>>
>> > self.g = g
>>
>> > self.h = h
>>
>> > self.f = g+h
>>
>> >
>>
>> >
>>
>> > I need to build a "list" of these objects. The amount is unknown.
>>
>> > On this list I need to regularly
>>
>> >
>>
>> > 1. check if a specific item - identified by Node.pos - is in the list.
>>
>> > 2. find the object with the lowest Node.f attribute and update or
>> > remove it
>>
>
>
> First thanks to all who responded. Although I only partially understand
> your answers as I am a Python starter. What's clear to me is the
> difference between object and instance of an object. Just didn't explain
> it well.
>
> Maybe I give some more info on what I need / want to do. I will also
> provide a working code example. Should have done this before.
>
> I would very much appreciate a working example of what you mean. Then I
> have a chance to understand it. :-)
>
> I would like to implement a class for a A* pathfinding algorithm. (there
> are ready libraries out there but I would like to learn it myself) This
> requires to maintain a list of nodes to be processed and nodes already
> processed. For new nodes I need to check if they are on one of the lists.
> I also need to regularly pick the node with the lowest value f from the
> list.
>
> Here some working code. For one function I sill need a solution. Any
> better solution is welcome.
>
> Thanks
> Robert
>
> ---------------
> class Node:
> def __init__(self, pos, parent, g , h):
> self.pos = pos
> self.parent = parent
> self.g = g
> self.h = h
> self.f = g+h
>
>
> def isinlist(nodeToSeatch):
> for item in openlist:
> if item.pos == nodeToSeatch: return True
> return False
>
>
> def lowestF():
> lowestF = ''
> for item in openlist:
> if item.f < lowestF: lowestF = item
> return lowestF

def lowestF():
return min(openlist, key=operator.attrgetter("f"))


> def deleteItemWithPos(pos):
> ## need this function or different approach
> pass
>
> openlist=[]
> openlist.append(Node((1,1),None,1,5))
> openlist.append(Node((1,2),(1,1),4,6))
> openlist.append(Node((1,3),(1,2),9,10))
>
> for item in openlist: print item.pos, item.f
>
> print isinlist((1,1))
> print isinlist((1,5))
>
> nextNode = lowestF()
> print nextNode.pos, nextNode.f

Here is an OO implementation of Chris Angelico's suggestion:

import heapq

class Node:
def __init__(self, pos, parent, g , h):
self.pos = pos
self.parent = parent
self.g = g
self.h = h
self.f = g+h
def __str__(self):
return "Node(pos={!r}, f={!r})".format(self.pos, self.f)

class Nodes():
def __init__(self):
self.lookup = {}
self.heap = []
def add(self, node):
self.lookup[node.pos] = node
heapq.heappush(self.heap, (node.f, node))
def __iter__(self):
return iter(self.lookup.values())
def __contains__(self, pos):
return pos in self.lookup
def lowest(self):
return self.heap[0][1]
def pop(self):
f, node = heapq.heappop()
del lookup[node.pos]
return node

nodes = Nodes()
nodes.add(Node((1,1), None, 1, 5))
nodes.add(Node((1,2), (1,1), 4, 6))
nodes.add(Node((1,3), (1,2), 9, 10))

for node in nodes:
print(node)

print((1,1) in nodes)
print((1,5) in nodes)

print(nodes.lowest())









== 4 of 7 ==
Date: Tues, Jan 21 2014 6:19 am
From: Peter Otten <__peter__@web.de>


Peter Otten wrote:

> def pop(self):
> f, node = heapq.heappop()
> del lookup[node.pos]
> return node

That should be

def pop(self):
f, node = heapq.heappop(self.heap)
del self.lookup[node.pos]
return node






== 5 of 7 ==
Date: Tues, Jan 21 2014 7:33 am
From: Robert Voigtländer


Am Dienstag, 21. Januar 2014 15:19:54 UTC+1 schrieb Peter Otten:
> Peter Otten wrote:
>
>
>
> > def pop(self):
>
> > f, node = heapq.heappop()
>
> > del lookup[node.pos]
>
> > return node
>
>
>
> That should be
>
>
>
> def pop(self):
>
> f, node = heapq.heappop(self.heap)
>
> del self.lookup[node.pos]
>
> return node

Hi Peter,

this works great. I will try to find some info about the functionality you used and to understnad what you did.
Maybe you can add a function to remove a node?

Thanks for your support and all tthe swift answers.

Robert




== 6 of 7 ==
Date: Tues, Jan 21 2014 7:37 am
From: Robert Voigtländer



> > > def pop(self):
> > > f, node = heapq.heappop()
> > > del lookup[node.pos]
> > > return node

> > That should be
>
> > def pop(self):
>
> > f, node = heapq.heappop(self.heap)
> > del self.lookup[node.pos]
> > return node
>
> Hi Peter,
> this works great. I will try to find some info about the functionality you used and to understnad what you did.
> Maybe you can add a function to remove a node?
> Thanks for your support and all tthe swift answers.
>
> Robert

Just realized what the pop function is for. I changed it from:
def pop(self):
to
def pop(self,pos):

and it works.

Now I go on with trying to understand it.




== 7 of 7 ==
Date: Tues, Jan 21 2014 10:03 am
From: Mark Lawrence



On 21/01/2014 13:43, Robert Voigtl�nder wrote:

[double spaced google disease snipped]

I'm pleased to see the regular contributors helping out as usual. In
response would you please be kind enough to read and action this
https://wiki.python.org/moin/GoogleGroupsPython to prevent us seeing the
double line spacing that google inserts, thanks.

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence






==============================================================================
TOPIC: Flushing out data from Popen buffer
http://groups.google.com/group/comp.lang.python/t/f5b445d917b4dd24?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Jan 21 2014 5:46 am
From: abc79721


I am working on a python script that reads data by tailing a file and then puts in a different file. The script works in a time bound manner and eventually flushes out the data from the buffer when the ENDTIME is reached. However there has been a mismatch in the source and target file in terms of size.

Following is a snippet:

self.read_size = 2048
self.tail_buffer = 2048

# start the file tail
cmd = '%s -f -o %s %s' % (self.jtailcmd, offset, self.source_journal)
self.logger.debug('[%s] starting FILETail' % self.getName())
try:
self.jtail = popen2.Popen3(cmd, bufsize = self.tail_buffer)
self.jtail.tochild.close()
out = self.jtail.fromchild
outfd = self.jtail.fromchild.fileno()
flags = fcntl.fcntl(outfd, fcntl.F_GETFL)
fcntl.fcntl(outfd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
except:
message = '[%s] error reading file' % self.getName()
self.logger.error(message)
self.logger.error('[%s] %s: %s' % \
(self.getName(), sys.exc_info()[0], sys.exc_info()[1]))
send_alert('AE', message)
self.sleep(60)
self.close_tail()
self.close_ssh()
And then eventually it flushes out the data:

try:
[i, o, e] = select.select([outfd], [], [], 1)
if i:
data = out.read(self.read_size)
else:
data = None
except:
message = '[%s] error reading file' % self.getName()
self.logger.error(message)
self.logger.error('[%s] %s: %s' % \
(self.getName(), sys.exc_info()[0], sys.exc_info()[1]))
send_alert('AE', message)
self.close_tail()
self.close_ssh()
self.sleep(60)
break
if data:
if self.sshcat.poll() != -1:
self.logger.error('[%s] connection error' % self.getName())
self.close_tail()
self.close_ssh()
break
try:
self.sshcat.tochild.writelines(data)
self.sshcat.tochild.flush()
except:
message = '[%s] error writing remote file' % self.getName()
While troubleshooting, I narrowed out the problem to tail_buffer size! By reducing the tail_buffer size , the script worked fine. I cant use subprocess(Python Version is 2.4.3)

I do not want to rely on tail_buffer size. Ideally the script should be independent of it!

Is there a way to flush data from the POPEN buffer ?





==============================================================================
TOPIC: Can post a code but afraid of plagiarism
http://groups.google.com/group/comp.lang.python/t/bb644bb09fed1442?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Jan 21 2014 5:49 am
From: Dan Sommers


On Tue, 21 Jan 2014 10:32:13 +0000, Oscar Benjamin wrote:

> ... When you set assignments the students will usually learn more if
> they work in groups. However at some point you need to try and assess
> how much they've individually learned. I find in practice that it's
> easy to tell when a student has copied someone else without really
> understanding what they're doing though. Of course if they just pay
> someone else to do it for them then there's not much you can do...

I had a programming teacher in high school who encouraged us to work
however we wanted, individually or in groups. There were two
conditions: (1) each student had to turn in a complete assignment, and
(2) he reserved the right to question anyone about anything they turned
in. He observed us working in class enough to know whom to question. I
know that a couple of students were busted early on; I don't know how it
all turned out in the end.

Dan





==============================================================================
TOPIC: import file without .py into another module
http://groups.google.com/group/comp.lang.python/t/11627bfd38b84845?hl=en
==============================================================================

== 1 of 7 ==
Date: Tues, Jan 21 2014 6:44 am
From: kevinbercaw@gmail.com


I have a python script that accepts two arguments:
sys.argv[1] is the full directory path to a config script. The script is python but does not have a .py extension!
sys.argv[2] is the file name of the config script

For example:
mainScript.py ./ a15800


The config script sets variables that I want to be able to use in the main script.

*** contents of "a15800": ***
myVar = "hello"

*** contents of "mainScript.py": ***
def printVars(configModuleName):
myVarName = ("%s.myVar" % configModuleName)
print "myVarName = %s" % myVarName
myVarValue = eval(myVarName)
print "myVarValue = %s" % myVarValue


if __name__ == '__main__':
import sys
import imp
filePath = sys.argv[1]
fileName = sys.argv[2]
configModuleObject = imp.load_source(fileName, filePath)
configModuleName = configModuleObject.__name__
print "configModuleName = %s" % configModuleName
printVars(configModuleName)

*** Output: ***
>mainScript.py ./ a15800
configModuleName = a15800
myVarName = a15800.myVar
Traceback (most recent call last):
File "mainScript.py", line 27, in <module>
printVars(configModuleName)
File "mainScript.py", line 15, in printVars
myVarValue = eval(myVarName)
File "<string>", line 1, in <module>
NameError: name 'a15800' is not defined

*** Question: ***
How do I get the value of the config file variable "myVar"?? It seems it's interpreting the variable name as a string rather than a variable name. I don't see any python function stringToVariable.




== 2 of 7 ==
Date: Tues, Jan 21 2014 7:06 am
From: MRAB


On 2014-01-21 14:44, kevinbercaw@gmail.com wrote:
> I have a python script that accepts two arguments:
> sys.argv[1] is the full directory path to a config script. The script is python but does not have a .py extension!
> sys.argv[2] is the file name of the config script
>
> For example:
> mainScript.py ./ a15800
>
>
> The config script sets variables that I want to be able to use in the main script.
>
> *** contents of "a15800": ***
> myVar = "hello"
>
> *** contents of "mainScript.py": ***
> def printVars(configModuleName):
> myVarName = ("%s.myVar" % configModuleName)
> print "myVarName = %s" % myVarName
> myVarValue = eval(myVarName)
> print "myVarValue = %s" % myVarValue
>
>
> if __name__ == '__main__':
> import sys
> import imp
> filePath = sys.argv[1]
> fileName = sys.argv[2]
> configModuleObject = imp.load_source(fileName, filePath)
> configModuleName = configModuleObject.__name__
> print "configModuleName = %s" % configModuleName
> printVars(configModuleName)
>
> *** Output: ***
>>mainScript.py ./ a15800
> configModuleName = a15800
> myVarName = a15800.myVar
> Traceback (most recent call last):
> File "mainScript.py", line 27, in <module>
> printVars(configModuleName)
> File "mainScript.py", line 15, in printVars
> myVarValue = eval(myVarName)
> File "<string>", line 1, in <module>
> NameError: name 'a15800' is not defined
>
> *** Question: ***
> How do I get the value of the config file variable "myVar"?? It seems it's interpreting the variable name as a string rather than a variable name. I don't see any python function stringToVariable.
>
The line:

configModuleObject = imp.load_source(fileName, filePath)

imports the module and then binds it to the name configModuleObject,
therefore:

print configModuleObject.myVar





== 3 of 7 ==
Date: Tues, Jan 21 2014 7:13 am
From: kevinbercaw@gmail.com


On Tuesday, January 21, 2014 10:06:16 AM UTC-5, MRAB wrote:
> On 2014-01-21 14:44, wrote:
>
> > I have a python script that accepts two arguments:
>
> > sys.argv[1] is the full directory path to a config script. The script is python but does not have a .py extension!
>
> > sys.argv[2] is the file name of the config script
>
> >
>
> > For example:
>
> > mainScript.py ./ a15800
>
> >
>
> >
>
> > The config script sets variables that I want to be able to use in the main script.
>
> >
>
> > *** contents of "a15800": ***
>
> > myVar = "hello"
>
> >
>
> > *** contents of "mainScript.py": ***
>
> > def printVars(configModuleName):
>
> > myVarName = ("%s.myVar" % configModuleName)
>
> > print "myVarName = %s" % myVarName
>
> > myVarValue = eval(myVarName)
>
> > print "myVarValue = %s" % myVarValue
>
> >
>
> >
>
> > if __name__ == '__main__':
>
> > import sys
>
> > import imp
>
> > filePath = sys.argv[1]
>
> > fileName = sys.argv[2]
>
> > configModuleObject = imp.load_source(fileName, filePath)
>
> > configModuleName = configModuleObject.__name__
>
> > print "configModuleName = %s" % configModuleName
>
> > printVars(configModuleName)
>
> >
>
> > *** Output: ***
>
> >>mainScript.py ./ a15800
>
> > configModuleName = a15800
>
> > myVarName = a15800.myVar
>
> > Traceback (most recent call last):
>
> > File "mainScript.py", line 27, in <module>
>
> > printVars(configModuleName)
>
> > File "mainScript.py", line 15, in printVars
>
> > myVarValue = eval(myVarName)
>
> > File "<string>", line 1, in <module>
>
> > NameError: name 'a15800' is not defined
>
> >
>
> > *** Question: ***
>
> > How do I get the value of the config file variable "myVar"?? It seems it's interpreting the variable name as a string rather than a variable name. I don't see any python function stringToVariable.
>
> >
>
> The line:
>
>
>
> configModuleObject = imp.load_source(fileName, filePath)
>
>
>
> imports the module and then binds it to the name configModuleObject,
>
> therefore:
>
>
>
> print configModuleObject.myVar

Yep, I tried that right off as that's how I thought it would work, but it doesn't work.
Traceback (most recent call last):
File "mainScript.py", line 31, in <module>
print configModuleObject.myVar
AttributeError: 'module' object has no attribute 'myVar'




== 4 of 7 ==
Date: Tues, Jan 21 2014 7:27 am
From: kevinbercaw@gmail.com


On Tuesday, January 21, 2014 9:44:13 AM UTC-5, kevin...@gmail.com wrote:
> I have a python script that accepts two arguments:
>
> sys.argv[1] is the full directory path to a config script. The script is python but does not have a .py extension!
>
> sys.argv[2] is the file name of the config script
>
>
>
> For example:
>
> mainScript.py ./ a15800
>
>
>
>
>
> The config script sets variables that I want to be able to use in the main script.
>
>
>
> *** contents of "a15800": ***
>
> myVar = "hello"
>
>
>
> *** contents of "mainScript.py": ***
>
> def printVars(configModuleName):
>
> myVarName = ("%s.myVar" % configModuleName)
>
> print "myVarName = %s" % myVarName
>
> myVarValue = eval(myVarName)
>
> print "myVarValue = %s" % myVarValue
>
>
>
>
>
> if __name__ == '__main__':
>
> import sys
>
> import imp
>
> filePath = sys.argv[1]
>
> fileName = sys.argv[2]
>
> configModuleObject = imp.load_source(fileName, filePath)
>
> configModuleName = configModuleObject.__name__
>
> print "configModuleName = %s" % configModuleName
>
> printVars(configModuleName)
>
>
>
> *** Output: ***
>
> >mainScript.py ./ a15800
>
> configModuleName = a15800
>
> myVarName = a15800.myVar
>
> Traceback (most recent call last):
>
> File "mainScript.py", line 27, in <module>
>
> printVars(configModuleName)
>
> File "mainScript.py", line 15, in printVars
>
> myVarValue = eval(myVarName)
>
> File "<string>", line 1, in <module>
>
> NameError: name 'a15800' is not defined
>
>
>
> *** Question: ***
>
> How do I get the value of the config file variable "myVar"?? It seems it's interpreting the variable name as a string rather than a variable name. I don't see any python function stringToVariable.

FYI - more info from interactive session, query configModuleObject and configModuleName using imp.find_module:
>>> imp.find_module("configModuleObject")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named configModuleObject
>>> imp.find_module("a15800")
(<open file 'a15800.pyc', mode 'rb' at 0x2b503b72d288>, 'a15800.pyc', ('.pyc', 'rb', 2))
>>> imp.find_module(configModuleName)
(<open file 'a15800.pyc', mode 'rb' at 0x2b503b72d300>, 'a15800.pyc', ('.pyc', 'rb', 2))




== 5 of 7 ==
Date: Tues, Jan 21 2014 7:36 am
From: Tim Chase


On 2014-01-21 07:13, kevinbercaw@gmail.com wrote:
>On Tuesday, January 21, 2014 10:06:16 AM UTC-5, MRAB wrote:
>> configModuleObject = imp.load_source(fileName, filePath)
>>
>> imports the module and then binds it to the name
>> configModuleObject,
>>
>> therefore:
>>
>> print configModuleObject.myVar
>
> Yep, I tried that right off as that's how I thought it would work,
> but it doesn't work. Traceback (most recent call last):
> File "mainScript.py", line 31, in <module>
> print configModuleObject.myVar
> AttributeError: 'module' object has no attribute 'myVar'

Check what you're passing for fileName/FilePath:

>>> import imp
>>> with open('demo.txt', 'wb') as f:
... f.write('x = 42\ny = "hello"\n')
...
>>> d = imp.load_source('SomeName', 'demo.txt')
>>> dir(d)
['__builtins__', '__doc__', '__file__', '__name__', '__package__',
'x', 'y']
>>> d.x
42
>>> d.y
'hello'
>>> d.__name__
'SomeName'

-tkc








== 6 of 7 ==
Date: Tues, Jan 21 2014 7:40 am
From: Peter Otten <__peter__@web.de>


kevinbercaw@gmail.com wrote:

>> > How do I get the value of the config file variable "myVar"?? It seems
>> > it's interpreting the variable name as a string rather than a variable
>> > name. I don't see any python function stringToVariable.

>> The line:
>>
>> configModuleObject = imp.load_source(fileName, filePath)

>> imports the module and then binds it to the name configModuleObject,
>>
>> therefore:
>>
>> print configModuleObject.myVar
>
> Yep, I tried that right off as that's how I thought it would work, but it
> doesn't work. Traceback (most recent call last):
> File "mainScript.py", line 31, in <module>
> print configModuleObject.myVar
> AttributeError: 'module' object has no attribute 'myVar'

Try again with

module = imp.load_source("made_up_name", "a15800")
print module.myVar

If the file "a15800" is not in the current working directory, give the
complete path, e. g:

module = imp.load_source("made_up_name", "/path/to/a15800")
print module.myVar

The first arg serves as the module's name which is used for caching to speed
up repeated imports:

>>> import imp
>>> import sys
>>> "made_up_name" in sys.modules
False
>>> module = imp.load_source("made_up_name", "a15800")
>>> module.myVar
'hello'
>>> "made_up_name" in sys.modules
True
>>> module
<module 'made_up_name' from 'a15800c'>






== 7 of 7 ==
Date: Tues, Jan 21 2014 7:50 am
From: kevinbercaw@gmail.com


On Tuesday, January 21, 2014 10:40:09 AM UTC-5, Peter Otten wrote:
> kevin...@gmail.com wrote:
>
>
>
> >> > How do I get the value of the config file variable "myVar"?? It seems
>
> >> > it's interpreting the variable name as a string rather than a variable
>
> >> > name. I don't see any python function stringToVariable.
>
>
>
> >> The line:
>
> >>
>
> >> configModuleObject = imp.load_source(fileName, filePath)
>
>
>
> >> imports the module and then binds it to the name configModuleObject,
>
> >>
>
> >> therefore:
>
> >>
>
> >> print configModuleObject.myVar
>
> >
>
> > Yep, I tried that right off as that's how I thought it would work, but it
>
> > doesn't work. Traceback (most recent call last):
>
> > File "mainScript.py", line 31, in <module>
>
> > print configModuleObject.myVar
>
> > AttributeError: 'module' object has no attribute 'myVar'
>
>
>
> Try again with
>
>
>
> module = imp.load_source("made_up_name", "a15800")
>
> print module.myVar
>
>
>
> If the file "a15800" is not in the current working directory, give the
>
> complete path, e. g:
>
>
>
> module = imp.load_source("made_up_name", "/path/to/a15800")
>
> print module.myVar
>
>
>
> The first arg serves as the module's name which is used for caching to speed
>
> up repeated imports:
>
>
>
> >>> import imp
>
> >>> import sys
>
> >>> "made_up_name" in sys.modules
>
> False
>
> >>> module = imp.load_source("made_up_name", "a15800")
>
> >>> module.myVar
>
> 'hello'
>
> >>> "made_up_name" in sys.modules
>
> True
>
> >>> module
>
> <module 'made_up_name' from 'a15800c'>

Thanks Peter Otten, that worked. I was not able to understand the documentation for imp.load_source correctly. Thanks so much!





==============================================================================
TOPIC: use class in class
http://groups.google.com/group/comp.lang.python/t/7c013c5946298783?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Jan 21 2014 7:24 am
From: Robert Voigtländer


>
> copy/paste of the whole thing. The actual error message could not
>
> have said "node", as there's no such name in the method.
>

You are correct. I copied the error before I renamed node into Node. I have to be more consistent here. :-)
The source for the error was still the same.





==============================================================================
TOPIC: Early retirement project?
http://groups.google.com/group/comp.lang.python/t/61d421ed38839cfa?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Jan 21 2014 7:25 am
From: Grant Edwards


On 2014-01-21, Larry Martell <larry.martell@gmail.com> wrote:
> On Tue, Jan 21, 2014 at 1:30 AM, Devin Jeanpierre
><jeanpierreda@gmail.com> wrote:
>> Congrats on the early retirement! It takes guts to decide to do that. :)
>
> I thought it took money.

One or the other. If you've got money, it doesn't take guts.

--
Grant Edwards grant.b.edwards Yow! I feel better about
at world problems now!
gmail.com




== 2 of 2 ==
Date: Tues, Jan 21 2014 9:34 am
From: Terry Reedy


On 1/21/2014 6:38 AM, Tim Chase wrote:
> On 2014-01-21 00:00, xeysxeys@gmail.com wrote:
>> Well, I retired early, and I guess now I've got some spare time to
>> learn about programming, which always seemed rather mysterious. I
>> am using an old mac as my main computer, and it runs os x 10.4 is
>> this too old? It fills my needs, and I am on a fixed income and
>> can't really afford to buy another. I think python would be a good
>> starter language, based on what I've read on the net.
>
> It's certainly a great way to consume lots of hours :)
>
> Mac OS X 10.4 should come with an older version of Python
> out-of-the-box.

Someone else said that it comes with 2.5. That will be fine for many
purposed. If you do use that, always make any classes you define a
subclass of 'object' if nothing else. In other words,

class MyClass(object): ...
# instead of
class MyClass: ...

In Python 2, the second gives you an 'old-style' or 'classic' class. You
do not need to learn about those. In Python 3, both forms give you
new-style classes, which is what you should learn.

There are a few other obsolete features to avoid, such as using strings
for exceptions.

> The install media should also include XCode if you
> want to download the latest & greatest version of Python and install
> that from source instead.

If you can do that easily, I recommend starting with the latest Python
3, especially if you want to work with non-English (non-ascii) characters.

--
Terry Jan Reedy






==============================================================================
TOPIC: ANN: rom 0.25.0 - Redis object mapper for Python
http://groups.google.com/group/comp.lang.python/t/2a3c6aade0cea15f?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Jan 21 2014 7:44 am
From: Josiah Carlson


Hey everyone,

Big change today: rom now supports fast prefix, suffix, and pattern match
queries over your data. The method is based on the autocomplete process
described in my book, Redis in Action

The "rom" package is a Redis object mapper for Python. It sports an
interface similar to Django's ORM, SQLAlchemy + Elixir, or Appengine's
datastore.

The changelog for recent releases can be seen below my signature.

You can find the package at:
https://www.github.com/josiahcarlson/rom
https://pypi.python.org/pypi/rom

And docs can be found at:
http://pythonhosted.org/rom/

Please CC me on any replies if you have any questions or comments.

Thank you,
- Josiah

#---------------------------------- 0.25.0
-----------------------------------
[changed] version numbers to account for bugfixes vs. feature updates.
[added] columns can now be defined to allow for prefix and/or suffix
queries.
Enabling prefix queries also enables arbitrary pattern matching over
your
data.
[fixed] in some cases, rom would allow the definition of multiple primary
keys, of which only one would ever be used (inconsistently). This will
now
result in an error.
[changed] defaulted to assume Lua is available on Redis, which has been
released for over 15 months at this point. You can disable support via
a call to rom._disable_lua_writes().
[added] the ability to cache and get the key that holds the result of a
query,
which can be used for pagination, etc. See: Query.cached_result()
[warning] using rom versions of 0.23 with 0.25.0 when prefix and suffix
indexes are enabled can result in improper results from prefix, suffix,
and/or pattern queries, and can result in orphan data living in prefix
or
suffix indexes. Upgrade all of your clients!
[changed] temporary keys for queries are now prefixed with the name of the
model over which queries are being executed on. This should effect
basically zero people, but can allow for query cleanup in the off chance
of a failure during execution.
#----------------------------- 0.23 (unreleased)
-----------------------------
[changed] reduced number of round trips for single-filter queries by 1,
thanks
to https://github.com/MickeyKim for the report.
#----------------------------------- 0.22
------------------------------------
[fixed] size estimation for intersection ordering when filtering has now
been
fixed, thank you to https://github.com/MickeyKim for the report and the
change (should improve performance).
[fixed] an issue with some types when trying to update attributes has now
been
fixed, thank you to https://github.com/denisvolokh for the report.
[changed] improved performance for simple numeric range queries of the form
Model.get_by(attr=value) or Model.get_by(attr=(min, max)) by roughly a
factor of 60x or better in some cases. Thank you to
https://github.com/MickeyKim for the report on poor performance.
#----------------------------------- 0.21
------------------------------------
[fixed] upload for rom 0.20 was missing new columns.py, now fixed





==============================================================================
TOPIC: Diving in to Python - Best resources?
http://groups.google.com/group/comp.lang.python/t/8df3a0e5ab12c63a?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Jan 21 2014 8:00 am
From: Rustom Mody


On Tuesday, January 21, 2014 1:04:16 AM UTC+5:30, Matt Watson wrote:
> Getting in the habit of dropping in a google group for any new project -
> everyone tends to be so helpful.

> I work in the automotive sales industry(management) and find myself
> doing so many day to day tasks that could easily be automated. I'm a
> very tech saavy person, but after running in fear from a Javascript
> class in undergrad 8 years ago I haven't ever looked back. I simply
> had no interest because I saw no applications.

> Now that I have a solid career I see SO many applications for
> programming in my industry alone. Automating data
> movement/calculations from websites, spreadsheets, pricing, etc will
> be my primary use.I'm OK saying I didn't retain 1% of what I
> learned in the Javascript class, I've dabbled in HTML, I've tweaked
> code in Excel macros or AutoIt scripts, but I'd classify myself as a
> complete beginner in programming.

It looks like
1. You are familiar with spreadsheets
2. Your work is spreadsheet-like

Why not develop that into a bigger strength?

Most people -- even those using spreadsheets -- dont seem to think of
the spreadsheet macro language/VBA as a programming language but it
is and it may well be all you need. This is written by one of the
biggest names in programming languages today

http://research.microsoft.com/en-us/um/people/simonpj/Papers/excel/excel.pdf




== 2 of 2 ==
Date: Tues, Jan 21 2014 9:57 am
From: Terry Reedy


On 1/21/2014 11:00 AM, Rustom Mody wrote:
> On Tuesday, January 21, 2014 1:04:16 AM UTC+5:30, Matt Watson wrote:
>> Getting in the habit of dropping in a google group for any new project -
>> everyone tends to be so helpful.
>
>> I work in the automotive sales industry(management) and find myself
>> doing so many day to day tasks that could easily be automated. I'm a
>> very tech saavy person, but after running in fear from a Javascript
>> class in undergrad 8 years ago I haven't ever looked back. I simply
>> had no interest because I saw no applications.
>
>> Now that I have a solid career I see SO many applications for
>> programming in my industry alone. Automating data
>> movement/calculations from websites, spreadsheets, pricing, etc will
>> be my primary use.I'm OK saying I didn't retain 1% of what I
>> learned in the Javascript class, I've dabbled in HTML, I've tweaked
>> code in Excel macros or AutoIt scripts, but I'd classify myself as a
>> complete beginner in programming.
>
> It looks like
> 1. You are familiar with spreadsheets
> 2. Your work is spreadsheet-like
>
> Why not develop that into a bigger strength?
>
> Most people -- even those using spreadsheets -- dont seem to think of
> the spreadsheet macro language/VBA as a programming language but it
> is

Definitely. I once worked out how to do nonlinear regression with a
spreadsheet, after doing it with Basic (and later C).

> and it may well be all you need. This is written by one of the
> biggest names in programming languages today
>
> http://research.microsoft.com/en-us/um/people/simonpj/Papers/excel/excel.pdf

--
Terry Jan Reedy






==============================================================================
TOPIC: Add followers at time of import
http://groups.google.com/group/comp.lang.python/t/c3de77ae37502292?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Jan 21 2014 9:52 am
From: emile


Hi Ethan,

How hard would it be to add a follower to a transaction (PO or Sales
Order) at time of import if the account has that follower? IOW, Ron
follows 'hilltop ranch' -- he'd like all activity linked to 'hilltop
ranch' to include him as a follower. Right now, following the account
doesn't notify about changes to documents, only changes to the account
itself.

Ask if that's not clear.

Emile






== 2 of 2 ==
Date: Tues, Jan 21 2014 10:03 am
From: Chris Angelico


On Wed, Jan 22, 2014 at 4:52 AM, emile <emile@fenx.com> wrote:
> Hi Ethan,
>
> How hard would it be to add a follower to a transaction (PO or Sales Order)
> at time of import if the account has that follower? IOW, Ron follows
> 'hilltop ranch' -- he'd like all activity linked to 'hilltop ranch' to
> include him as a follower. Right now, following the account doesn't notify
> about changes to documents, only changes to the account itself.
>
> Ask if that's not clear.

I'm not entirely sure, but I think this might have been meant for
somewhere other than python-list. Welcome to the club.

ChrisA





==============================================================================
TOPIC: autoconf tools and python3 3m 3dm
http://groups.google.com/group/comp.lang.python/t/760f1993767c2e90?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Jan 21 2014 9:55 am
From: Mark Heieis


Hi,

I've been migrating a python2 package+extension to python3. The problem
I'm running into is with ./configure and which version it picks up or
doesn't in this case.

The default is python2 and works just fine as expected.

However, when ./configure PYTHON=python3 is run, the problems occur.
"./configure" picks python3 correctly and propagates that through to all
of the makefiles created. Good so far. It defines all of the appropriate
libs and include with the '3.3' extension.

The yum installed python3, however, is denoted by a suffix 'dm',
yielding an extension of '3.3dm' (Fedora 20)
Building and installing from source, python has a suffix 'm', so the
extension is '3.3m;

In ./configure, PYTHON_VERSION is set by "$PYTHON -c 'import sys;
print(sys.version[:3])'" which reports "3.3". PYTHON_VERSION is then
used as the suffix to find all of the appropriate python directories,
libraries, site-packages and includes. The same result is had if
python3.3m or python3.3dm is used.

Unfortunately, this isn't enough, as the python site packages are
installed with naming ending in 'm' or 'dm', resulting in all of the
makefiles failing because they can't find the libraries, include files,
or site packages.

I've kludged a solution by manually creating soft links in the lib and
include directories which almost solves the problem:

for example in /usr/lib64:

lrwxrwxrwx. 1 root root 19 2013-12-18 15:02 libpython2.7.so ->
libpython2.7.so.1.0
-r-xr-xr-x. 1 root root 1800480 2013-11-12 08:47 libpython2.7.so.1.0
lrwxrwxrwx 1 root root 21 2014-01-20 16:11 libpython3.3dm.so ->
libpython3.3dm.so.1.0
-rwxr-xr-x 1 root root 3057640 2013-11-07 02:03 libpython3.3dm.so.1.0
lrwxrwxrwx 1 root root 20 2014-01-20 16:11 libpython3.3m.so ->
libpython3.3m.so.1.0
-rwxr-xr-x 1 root root 2498992 2013-11-07 02:03 libpython3.3m.so.1.0
lrwxrwxrwx 1 root root 20 2014-01-20 16:37 libpython3.3.so ->
libpython3.3m.so.1.0
-rwxr-xr-x 1 root root 6704 2013-11-07 02:03 libpython3.so

This approach doesn't feel right to me. I don't think pythonX.Y-config
would work either as one would need to know in advance specifically
which one to call and there'd be extra work to extract the full version
info, etc. ("$python3-config --includes" yields
-I/usr/include/python3.3m -I/usr/include/python3.3m)

Has anyone else come up against this? If so, what was the solution?

BTW - anyone know what the 'm' and 'dm' suffixes indicate?

TIA.











==============================================================================
TOPIC: Running pywin32-218.win32-py2.7.exe (win32 package for Python) results
in error "The file exists"
http://groups.google.com/group/comp.lang.python/t/025921f89bf5ab56?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Jan 21 2014 10:02 am
From: Ziv Tepman


I am trying to install the Python module "win32" but whenever I click on
the exe file (pywin32-218.win32-py2.7.exe) I get the message "The file
exists Could not create temporary file", and when I click OK, I get the
message, "pywin32-218.win32-py2.7." I have chosen the appropriate build.
How might I fix this error? Thanks!




==============================================================================

You received this message because you are subscribed to the Google Groups "comp.lang.python"
group.

To post to this group, visit http://groups.google.com/group/comp.lang.python?hl=en

To unsubscribe from this group, send email to comp.lang.python+unsubscribe@googlegroups.com

To change the way you get mail from this group, visit:
http://groups.google.com/group/comp.lang.python/subscribe?hl=en

To report abuse, send email explaining the problem to abuse@googlegroups.com

==============================================================================
Google Groups: http://groups.google.com/?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate