473,320 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Reloading after from adder import * ????

I created a module with the DictAdder subclass as follows:

class DictAdder(Adder):
def add(self, x, y):
new={}
for k in x.keys(): new[k]=x[k]
for k in y.keys(): new[k]=y[k]
return new

At the interactive prompt I then said: from Adder import *

After defining an instance of DictAdder as x=DictAdder() and trying to
add two dictionaries, I notice a typo in DictAdder. If I then go back
and make a change to the DictAdder subclass I have to go through many
steps to add two dictionaries (get the revision into active memory):
1. import Adder
2. Reload(Adder)
3. from Adder import *
4. x=DictAdder()

Question: Is there an easier and quicker way to get DictAdder into
active memory?????
Thanks,
Seymour

Nov 19 '06 #1
3 1515
Seymour wrote:
I created a module with the DictAdder subclass as follows:

class DictAdder(Adder):
def add(self, x, y):
new={}
for k in x.keys(): new[k]=x[k]
for k in y.keys(): new[k]=y[k]
return new

At the interactive prompt I then said: from Adder import *

After defining an instance of DictAdder as x=DictAdder() and trying to
add two dictionaries, I notice a typo in DictAdder. If I then go back
and make a change to the DictAdder subclass I have to go through many
steps to add two dictionaries (get the revision into active memory):
1. import Adder
2. Reload(Adder)
3. from Adder import *
4. x=DictAdder()

Question: Is there an easier and quicker way to get DictAdder into
active memory?????
This is one of the occasions where semicolons can come in handy in
Python:

import adder; reload(adder); from adder import *

Then depending on your OS and what add-on line editing kit (if any) you
are using, it should only a very small number of keystrokes to retrieve
that and execute it.

If you meant "is there a single command?", the answer is no, not AFAIK.

Having said that, you seem to have a strange idea about what classes
are for -- "self" is not used; having such a class "method" seems
pointless. All you need to do what you are doing is a simple function,
whose body can be simply expressed in terms of dict methods:

def funnyfunc(x, y):
"""This is called a docstring; try to say what the func is doing
for you"""
new = x.copy()
new.update(y)
return new

Of course that is probably not what you *want* to be doing; but you
might need to divulge what the Adder class is doing to get more help.

Hints: unlike some other languages,

(1) You don't need to have 1 module file per class; for several small
related classes (e.g. a main class and a handful of subclasses) it
makes a lot of sense to put it all in one module.

(2) To do most simple things, you don't even need to write a class at
all. For example, you can write a few hundred lines of clear
intelligible Python with very little must-recite-this-spell overhead to
do quite complicated data manipulations using only (a) built-in
functions (b) the methods of built-in types like strings, lists and
dictionaries (c) functions and classes in built-in modules (d) ditto
3rd-party modules. In Python classes usually are things that map onto
something in the real world or at least your model of that; you write
them when you need to; classes are not artificial hoops you must jump
through to do the equivalent of:
print 2 + 2
You may end up using classes to emulate some other programming
constructs, but not yet.
Eventually you can write those 3rd party modules which are chock-full
of classes and other goodies, using them as a guide.

OK, relax, rant over :-)

HTH,
John

Nov 19 '06 #2
John,
Thanks for the reply. Just as a little further background, I am using
Windows XP and Komodo and just trying to learn about classes - how they
work, what they are used for, etc. I have been studying "Learning
Python (2nd Ed)" by Mark Lutz and Davis Ascher and have been typing in
some of the exercises from Chapt 23, Advanced Class Topics (I tend to
learn better by re-typing them in). What I posted was only a partial
snippet of the full solution for the Inheritance code solution given on
page 558 of the book, but really was not relevant to my question -
sorry. I just made some typos and was wondering if there was an easier
way to clear the Python namespace at the interactive prompt rather than
shutting Komodo down and restarting (really brute force). I finally
figured out the four step solution that I posted but was still
wondering if there was yet an easier way.

FYI, below is the full solution to the Inheritance class exercise in
the book. Python is a hobby for me and I am not sure how of if I will
ever use this stuff, but would like to understand it better. That
said, if you have any follow up study source recommendations, I would
be interested.
Seymour

class Adder:
def add(self, x, y):
print 'not implemented!'
def __init__(self, start=[]):
self.data = start
def __add__(self, other): # Or in subclasses?
return self.add(self.data, other) # Or return type?

class ListAdder(Adder):
def add(self, x, y):
return x + y

class DictAdder(Adder):
def add(self, x, y):
new = {}
for k in x.keys(): new[k] = x[k]
for k in y.keys(): new[k] = y[k]
return new


John Machin wrote:
Seymour wrote:
I created a module with the DictAdder subclass as follows:

class DictAdder(Adder):
def add(self, x, y):
new={}
for k in x.keys(): new[k]=x[k]
for k in y.keys(): new[k]=y[k]
return new

At the interactive prompt I then said: from Adder import *

After defining an instance of DictAdder as x=DictAdder() and trying to
add two dictionaries, I notice a typo in DictAdder. If I then go back
and make a change to the DictAdder subclass I have to go through many
steps to add two dictionaries (get the revision into active memory):
1. import Adder
2. Reload(Adder)
3. from Adder import *
4. x=DictAdder()

Question: Is there an easier and quicker way to get DictAdder into
active memory?????

This is one of the occasions where semicolons can come in handy in
Python:

import adder; reload(adder); from adder import *

Then depending on your OS and what add-on line editing kit (if any) you
are using, it should only a very small number of keystrokes to retrieve
that and execute it.

If you meant "is there a single command?", the answer is no, not AFAIK.

Having said that, you seem to have a strange idea about what classes
are for -- "self" is not used; having such a class "method" seems
pointless. All you need to do what you are doing is a simple function,
whose body can be simply expressed in terms of dict methods:

def funnyfunc(x, y):
"""This is called a docstring; try to say what the func is doing
for you"""
new = x.copy()
new.update(y)
return new

Of course that is probably not what you *want* to be doing; but you
might need to divulge what the Adder class is doing to get more help.

Hints: unlike some other languages,

(1) You don't need to have 1 module file per class; for several small
related classes (e.g. a main class and a handful of subclasses) it
makes a lot of sense to put it all in one module.

(2) To do most simple things, you don't even need to write a class at
all. For example, you can write a few hundred lines of clear
intelligible Python with very little must-recite-this-spell overhead to
do quite complicated data manipulations using only (a) built-in
functions (b) the methods of built-in types like strings, lists and
dictionaries (c) functions and classes in built-in modules (d) ditto
3rd-party modules. In Python classes usually are things that map onto
something in the real world or at least your model of that; you write
them when you need to; classes are not artificial hoops you must jump
through to do the equivalent of:
print 2 + 2
You may end up using classes to emulate some other programming
constructs, but not yet.
Eventually you can write those 3rd party modules which are chock-full
of classes and other goodies, using them as a guide.

OK, relax, rant over :-)

HTH,
John
Nov 19 '06 #3
Seymour wrote:
I just made some typos and was wondering if there was an easier
way to clear the Python namespace at the interactive prompt rather than
shutting Komodo down and restarting (really brute force).
most IDE's have a "reset interactive mode" command (it's ctrl-F6 in
IDLE, for example).

another approach is to put the code in a script file, and run that file.
scripts always run in a clean namespace.

yet another approach is to stop using wildcard import ("from import *").
if you refer to the things in Adder via the Adder namespace, you can
just do

reload(Adder)

to fetch the new version.

("from import *" is generally considered a "only use if you know what
you're doing" construct in Python, so if it causes trouble for you, you
shouldn't have used it in the first place, per definition ;-)

</F>

Nov 19 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Andy Jewell | last post by:
Does anyone know of a way to dynamically reload all the imported modules of a client module? I'm writing a program that I have broken down into quite a few submodules, and the 'configuration'...
0
by: OKB (not okblacke) | last post by:
I'm fooling around with some MUD server code, and I want to add a "reload" command that will let MUD wizards reload the server modules, so that changes to the MUD parser and such can be effected...
2
by: ben moretti | last post by:
hi i'm learning python, and one area i'd use it for is data management in scientific computing. in the case i've tried i want to reformat a data file from a normalised list to a matrix with some...
31
by: mark | last post by:
Hello- i am trying to make the function addbitwise more efficient. the code below takes an array of binary numbers (of size 5) and performs bitwise addition. it looks ugly and it is not elegant...
4
by: aine_canby | last post by:
I'm using python.exe to execute my modules. I have a music.py module which contains my classes and a main.py module which uses these classes. In python.exe, I call "import main" to execute my...
1
by: anil.pundoor | last post by:
hi all, i have following code if <condition> from SIPT.xml_param_mapping import MESSAGE_PARAMETER_MAPPING else: from SIPT.msg_param_mapping import MESSAGE_PARAMETER_MAPPING
9
by: andrewfelch | last post by:
Hello all, I'm using the metaclass trick for automatic reloading of class member functions, found at: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/160164 My problem is that if I 1)...
2
by: johnny | last post by:
from people.models import * Now I make changes to the models.py. How do I reload this module in Python Shell?
1
by: lukasz.f24 | last post by:
Hello, I came across annoying problem during my fun with mod_python. I turned out that mod_python load package only onca and don't care about any changes to it. Obviously it makes sense on...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.