Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > [Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces)

Reply
Thread Tools

[Help] [Newbie] Require help migrating from Perl to Python 2.7 (namespaces)

 
 
Peter Otten
Guest
Posts: n/a
 
      12-23-2012
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Thanks to all your answers, I have read a lot about namespaces, but still
> there's something I do not understood. I have tried your example but as I
> expected:
>
> line 13, in HandoverSQLCursor
> curs.execute("SELECT * FROM lager")
> AttributeError: 'builtin_function_or_method' object has no attribute
> 'execute'


You have assigned a built-in function to the curs variable. Example:

>>> curs = open
>>> curs.execute

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute
'execute'

You can find out the name of the actual function with

>>> print curs.__name__

open

PS: This question is only loosely related to your previous question. You
should have started a new thread.




 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      12-23-2012
In article <(E-Mail Removed)>,
(E-Mail Removed) wrote:

> Thanks to all your answers, I have read a lot about namespaces, but still
> there's something I do not understood. I have tried your example but as I
> expected:
>
> line 13, in HandoverSQLCursor
> curs.execute("SELECT * FROM lager")
> AttributeError: 'builtin_function_or_method' object has no attribute
> 'execute'
>
> I will try my best to write tomorrow a sample as detailed as possible.
>
> Good evening


Polishing up my crystal ball, I'm going to guess you're using one of the
Python Database APIs (http://www.python.org/dev/peps/pep-0249/). And
that at some point, you tried to generate a cursor by doing:

curs = connection.cursor

instead of

curs = connection.cursor()

This left you with curs being (a reference to) the cursor function
itself, rather than what the function returns when it's called.

You own the oracle 20 minutes spent reading ESR's classic essay, "How To
Ask Questions The Smart Way" (http://tinyurl.com/cabqnop).
 
Reply With Quote
 
 
 
 
prilisauer@googlemail.com
Guest
Posts: n/a
 
      12-23-2012
Okay, I try to publish this sample, and yes it's not a working piece of code, but I try to "draw" my problem that way. As you will see, I load modules, create cursor,... in the main.py. In the lower section you see, that the modules should execute sqls. In case It could occur that two queries occur at the same time. PS: IT IS NOT A QUESTION ABOUT SQL, etc. I do not understand, how I could handle the part which is marked with Problemsection1 and Problemsection2

I hope really found the right "wording". thank to all your help.


main.py
import HomeLog # LogHandler
import HomeSocketServer # Threaded TCP Socket Server
import HomeDatastore # SQLite DB
import HomeDaliServer # Connects to USB Device
import HomeScheduler # Advanced scheduler functions


# Attach Loghandler
Loghandler = HomeLog.Logging()
# Attach SocketServer
HomeSocketServer.HomeSocketServerStart()

# Attach Scheduler
HomeSched = HomeScheduler.HomeScheduler()
HomeSched.SchedulerStart()
HomeSched.SchedulerJobs()

# Attach Datastore
Datastore=HomeDatastore.HomeDBStore()
Datastore=Datastore.Startup()

#Attach Dali Driver
Dali=HomeDaliServer.Startup()
# This is a "Sample" that builds 2byte Cmd and transmits it on bus
PowerOnLamp1=Dali.send(0,0,1,80)

################################################## #############
HomeDaliServer.py
.........
def send (self,DaliAdress,RequestType,Request,RequestValue) :
# Problemsection1:
# Here it's getting Interesting
# We're at the HomeDaliServer, and now I want to use QuerySqlite() in the file HomeDatastore.py

################################################## #############
HomeScheduler.py
# Problemsection2:
# If new workerthread is started, Informations must be queried using QuerySlite() and also update data


################################################## #############
HomeDatastore.py
def QuerySqlite():
#doing something here..
# returning Data

################################################## #############

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      12-24-2012
On 12/23/2012 4:32 AM, (E-Mail Removed) wrote:
> By the way, I think I have found the correct "wording". for my
> understood, the "handover" of objects to imported modules doesn't
> work because, e.g. trying to hand-over an SQLite connection into a
> imported module, can't work because the "attributes" are not
> transfered.


I have not followed this thread, and do not know the context of your
statement, or the code that did not work, but if you hand a Python
object to an imported module, and something within the module can access
the object, then all of its attributes are also accessible, the same as
from the original module.

--
Terry Jan Reedy

 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      12-24-2012
On Sun, 23 Dec 2012 13:42:14 -0800 (PST), (E-Mail Removed)
declaimed the following in gmane.comp.python.general:

> Okay, I try to publish this sample, and yes it's not a working piece of code, but I try to "draw" my problem that way. As you will see, I load modules, create cursor,... in the main.py. In the lower section you see, that the modules should execute sqls. In case It could occur that two queries occur at the same time. PS: IT IS NOT A QUESTION ABOUT SQL, etc. I do not understand, how I could handle the part which is marked with Problemsection1 and Problemsection2


> #Attach Dali Driver
> Dali=HomeDaliServer.Startup()
> # This is a "Sample" that builds 2byte Cmd and transmits it on bus
> PowerOnLamp1=Dali.send(0,0,1,80)
>
> ################################################## #############
> HomeDaliServer.py
> ........
> def send (self,DaliAdress,RequestType,Request,RequestValue) :


You've left out all the important stuff...

Python uses indentation to delimit code... The above is fully to the
left, which implies it is a MODULE level function, NOT a method in a
CLASS.

You also do not show us the "Startup()" code, so we can't tell if
that is a class or not.

You do not include a traceback of the error...
--
Wulfraed Dennis Lee Bieber AF6VN
(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
Thomas Bach
Guest
Posts: n/a
 
      12-24-2012
Hi there,

On Sun, Dec 23, 2012 at 01:42:14PM -0800, (E-Mail Removed) wrote:
> […] In the lower section you see, that the modules should execute
> sqls. In case It could occur that two queries occur at the same
> time. PS: IT IS NOT A QUESTION ABOUT SQL, etc. I do not understand,
> how I could handle the part which is marked with Problemsection1 and
> Problemsection2


I actually do not understand the problem you are stating. But, did you
have a look at SQLAlchemy? If you are coping a lot with SQL it really
makes your life much easier and they also provide the necessary
mechanisms for threading.

> import HomeLog # LogHandler


Modules should be written all lower-case separated by underscores.

> # Attach Loghandler
> Loghandler = HomeLog.Logging()


Have a look at the logging module and tutorial. I don't know what is
in HomeLog.Logging, but this doesn't seem right.

Variables defined at module level should be written all upper-case.

> # Attach SocketServer
> HomeSocketServer.HomeSocketServerStart()
>
> # Attach Scheduler
> HomeSched = HomeScheduler.HomeScheduler()
> […]
> # This is a "Sample" that builds 2byte Cmd and transmits it on bus
> PowerOnLamp1=Dali.send(0,0,1,80)


You do all this on the module level? These things should go into
functions with proper names or at least into a

if __name__ == '__main__':
pass


> ################################################## #############
> HomeDaliServer.py
> ........
> def send (self,DaliAdress,RequestType,Request,RequestValue) :
> # Problemsection1:
> # Here it's getting Interesting
> # We're at the HomeDaliServer, and now I want to use QuerySqlite()
> in the file HomeDatastore.py


So, where's the problem?

# make sure not to introduce cyclic dependence here!
import home_data_store

def send (connection,DaliAdress,RequestType,Request,Request Value):
results = home_data_store.query_sqlite(connection, …)
return results


> ################################################## #############
> HomeScheduler.py
> # Problemsection2:
> # If new workerthread is started, Informations must be queried using
> QuerySlite() and also update data


So, here's a first sketch (untested):

def query():
data = do_something()
return data

def update(data):
do_something_with(data)


> HomeDatastore.py
> def QuerySqlite():
> #doing something here..
> # returning Data


Have you read the Python tutorial by the way?

Regards,
Thomas.
 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      12-24-2012
On 12/23/2012 04:42 PM, (E-Mail Removed) wrote:
> Okay, I try to publish this sample, and yes it's not a working piece of code, but I try to "draw" my problem that way. As you will see, I load modules, create cursor,... in the main.py. In the lower section you see, that the modules should execute sqls. In case It could occur that two queries occur at the same time. PS: IT IS NOT A QUESTION ABOUT SQL, etc. I do not understand, how I could handle the part which is marked with Problemsection1 and Problemsection2

You're miles from being ready to worry about "the same time." Don't
start threading till you can get a simple multi-module program understood.
> I hope really found the right "wording". thank to all your help.
>
>
> main.py


For some reason you capitalize all those filenames, so you're stuck with
unpythonic module names. If you want your code readable, use lowercase
for module name, and Capitalized for class name.

> import HomeLog # LogHandler
> import HomeSocketServer # Threaded TCP Socket Server
> import HomeDatastore # SQLite DB
> import HomeDaliServer # Connects to USB Device
> import HomeScheduler # Advanced scheduler functions
>
>
> # Attach Loghandler
> Loghandler = HomeLog.Logging()
> # Attach SocketServer
> HomeSocketServer.HomeSocketServerStart()
>
> # Attach Scheduler
> HomeSched = HomeScheduler.HomeScheduler()
> HomeSched.SchedulerStart()
> HomeSched.SchedulerJobs()
>
> # Attach Datastore
> Datastore=HomeDatastore.HomeDBStore()
> Datastore=Datastore.Startup()
>
> #Attach Dali Driver
> Dali=HomeDaliServer.Startup()
> # This is a "Sample" that builds 2byte Cmd and transmits it on bus
> PowerOnLamp1=Dali.send(0,0,1,80)
>
> ################################################## #############
> HomeDaliServer.py

import HomeDatastore
> ........
> def send (self,DaliAdress,RequestType,Request,RequestValue) :

Nobody's going to be able to understand your code if you persist in
using self in unpythonic ways. It's used as the first argument of a
class method. Period.
> # Problemsection1:
> # Here it's getting Interesting
> # We're at the HomeDaliServer, and now I want to use QuerySqlite() in the file HomeDatastore.py

So call it:
firstarg = whatever * RequestType
secondarg = something different + RequestValue
result = HomeDatastore.QuerySqlite(firstarg, secondarg)
> ################################################## #############
> HomeScheduler.py

Where are your import statements? No module automatically sees imports
that were done elsewhere. Import what you need in a module.
> # Problemsection2:
> # If new workerthread is started, Informations must be queried using QuerySlite() and also update data

I don't see any 'update data' function anywhere, but if you want to call
QuerySqlite, you need to call it:
thisresult = HomeDatastore.QuerySqlite(him, her, theother)
>
> ################################################## #############
> HomeDatastore.py
> def QuerySqlite():

You presumably mean
def QuerySqlite(firstparam, secondparam):

since a function with no arguments is going to be stuck trying to use
globals, and that's not a good habit to get into.
> #doing something here..

#doing something with those parameters, and only those parameters
> # returning Data
>
> ################################################## #############
>



--

DaveA

 
Reply With Quote
 
prilisauer@googlemail.com
Guest
Posts: n/a
 
      12-24-2012
Hello Dave,

Thank you, for your help, I'll try my best.

To all others, PLEASE be pleasant with my nescience, I'll tried to describe not a specific error at my Program. I'll tried to get rid of that missing link this sample is only theoretic, but the code really exists and is over 1000 lines long.

I understood how to transmit data to a class, but I do not understood how that class could access an SQL object, that is allready opened at a other class without getting into troubles with sqlite.

For my understood, the newsgroup isn't a place only to solve concrete problems. I think you're not only a "helpdesk" , If I'm at the wrong group to get some Ideas how to solve my "issues"


Ps.: DaveA I don't know how to say it, but I treasure your great work here, giving such detailed good answers.

PPs.: I know, that my codingstyle isn't that great, I've haven't programmed the last two years. You're welcome to guess what I've worked 8 years long. you will laugh till you fall of your keyboard

PPPs.: I' will use that day to check out the PEP's and correct my coding style, and naming.
 
Reply With Quote
 
prilisauer@googlemail.com
Guest
Posts: n/a
 
      12-24-2012
Hello Dave,

Thank you, for your help, I'll try my best.

To all others, PLEASE be pleasant with my nescience, I'll tried to describe not a specific error at my Program. I'll tried to get rid of that missing link this sample is only theoretic, but the code really exists and is over 1000 lines long.

I understood how to transmit data to a class, but I do not understood how that class could access an SQL object, that is allready opened at a other class without getting into troubles with sqlite.

For my understood, the newsgroup isn't a place only to solve concrete problems. I think you're not only a "helpdesk" , If I'm at the wrong group to get some Ideas how to solve my "issues"


Ps.: DaveA I don't know how to say it, but I treasure your great work here, giving such detailed good answers.

PPs.: I know, that my codingstyle isn't that great, I've haven't programmed the last two years. You're welcome to guess what I've worked 8 years long. you will laugh till you fall of your keyboard

PPPs.: I' will use that day to check out the PEP's and correct my coding style, and naming.
 
Reply With Quote
 
Cameron Simpson
Guest
Posts: n/a
 
      12-24-2012
On 24Dec2012 00:23, (E-Mail Removed) <(E-Mail Removed)> wrote:
| To all others, PLEASE be pleasant with my nescience, I'll tried to
| describe not a specific error at my Program.

If you don't describe specific errors, you won't get specific advice.

If you're after stylistic and technique advice, please offer real,
specific, _running_ code and say "I'm doing this X way, should I?"
or something like that.

People here like specific questions. Even theoretical questions can be
illustrated with specific examples. It makes things explicit, and people
can point at concrete things as good or bad.

| I'll tried to get rid of
| that missing link this sample is only theoretic, but the code really
| exists and is over 1000 lines long.
|
| I understood how to transmit data to a class, but I do not understood
| how that class could access an SQL object, that is allready opened at
| a other class without getting into troubles with sqlite.

The "SQL object" is just more data. Pass it to the class instance like
any other argument.

| For my understood, the newsgroup isn't a place only to solve concrete
| problems. I think you're not only a "helpdesk" , If I'm at the wrong
| group to get some Ideas how to solve my "issues"

If you're after theoretic advice, please ask it in the context of real
working example code. It removes a lot of vagueness and ambiguity.
Especially if you are having trouble with English "wording": code
examples that run are much easier to discuss.

Cheers,
--
Cameron Simpson <(E-Mail Removed)>

The US government can't make a penny for a penny. How can we make RFID
tags for a penny?
- overhead by WIRED at the Intelligent Printing conference Oct2006
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: migrating from python 2.4 to python 2.6 Mark Lawrence Python 0 07-09-2012 11:05 AM
Tabnanny errors when Migrating Python 2.4 code to 2.5 kyosohma@gmail.com Python 3 01-04-2008 10:28 PM
advice required re migrating php app to python and most likely zope Ken Guest Python 2 01-06-2006 10:59 AM
migrating c code to perl chetan Perl Misc 3 01-13-2004 06:35 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments