473,789 Members | 2,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

looking for way to include many times some .py code from anotherpython code

Hi,
I'm looking for some easy way to do something like include in c or PHP.
Imagine I would like to have:
cat somefile.py
a = 222
b = 111
c = 9
cat somefile2.py
self.xxx = a
self.zzz = b
self.c = c
self.d = d
cat anotherfile.py

def a():
include somefile
postprocess(a)

def b():
include somefile
postprocess(a, b, c)

class klass():
def __init__(self, a, b, c, d):
include somefile2

I know about module imports and reloads, but am not sure if this is the right
way to go. Mainly, I want to assign to multiple object instances some self bound
variables. Their values will be different, so I can't use global variables.

Martin
Jul 18 '05 #1
22 2287
Martin MOKREJ© wrote:
Hi,
I'm looking for some easy way to do something like include in c or PHP.
Imagine I would like to have: ....

I know about module imports and reloads, but am not sure if this is the
right way to go. Mainly, I want to assign to multiple object instances
some self bound variables. Their values will be different, so I can't
use global variables.


Someone will, no doubt, find a way to code this. I suggest you are
fighting the language here -- learn to use it instead. Decide what
you really want to do, not how you want to do it. Then try to figure
out how to accomplish your real goal in the normal flow of the language.
-Scott David Daniels
Sc***********@A cm.Org
Jul 18 '05 #2
Martin MOKREJ© wrote:
Hi,
I'm looking for some easy way to do something like include in c or PHP.
Imagine I would like to have:
cat somefile.py
a = 222
b = 111
c = 9
cat somefile2.py
self.xxx = a
self.zzz = b
self.c = c
self.d = d
cat anotherfile.py

def a():
include somefile
postprocess(a)

def b():
include somefile
postprocess(a, b, c)

class klass():
def __init__(self, a, b, c, d):
include somefile2
You can do this with module-level variables and a base class for klass:

cat somefile.py
a = 222
b = 111
c = 9
cat somefile2.py
class base:
def __init__(self, a, b, c, d):
self.xxx = a
self.zzz = b
self.c = c
self.d = d
cat anotherfile.py
import somefile, somefile2

def a():
postprocess(som efile.a)

def b():
postprocess(som efile.a, somefile.b, somefile.c)

class klass(somefile2 .base):
def __init__(self, a, b, c, d):
somefile2.base. __init__(self, a, b, c, d)

Kent


I know about module imports and reloads, but am not sure if this is the
right
way to go. Mainly, I want to assign to multiple object instances some
self bound
variables. Their values will be different, so I can't use global variables.

Martin

Jul 18 '05 #3
Scott David Daniels wrote:
Martin MOKREJ© wrote:
Hi,
I'm looking for some easy way to do something like include in c or PHP.
Imagine I would like to have: ....

I know about module imports and reloads, but am not sure if this is
the right way to go. Mainly, I want to assign to multiple object
instances some self bound variables. Their values will be different,
so I can't

> use global variables.


Someone will, no doubt, find a way to code this. I suggest you are
fighting the language here -- learn to use it instead. Decide what
you really want to do, not how you want to do it. Then try to figure
out how to accomplish your real goal in the normal flow of the language.


See my post on Mar 2 about "automating assignment of class variables".
I got no answers, maybe I wasn't clear enough ... :(

I need to define lots of variables. The variable names are often identical.
The problem is that if I put such a code into a function ... no, I'm not going
to pass anything to a function to get it returned back. I just want to get
lots of variables assigned, that all. If I put them into module, it get's
exectued only once unless I do reload. And I'd have to use:
"from some import *", because mainly I'm interrested in assigning to self:
self.x = "blah"
self.y = "uhm"

I'm newbie, sure.

M.
Jul 18 '05 #4
> See my post on Mar 2 about "automating assignment of class variables".
I got no answers, maybe I wasn't clear enough ... :(
Seems so - I for example didn't understand it.

I need to define lots of variables. The variable names are often
identical. The problem is that if I put such a code into a function ...
no, I'm not going to pass anything to a function to get it returned back.
I just want to get lots of variables assigned, that all. If I put them
into module, it get's exectued only once unless I do reload. And I'd have
to use: "from some import *", because mainly I'm interrested in assigning
to self: self.x = "blah"
self.y = "uhm"


Okay, I try and guess: From your two posts I infer that you want to set
variables in instances. But you've got lots of these and you don't want to
write code like this:

class Foo:
def __init__(self, a, b, .....):
self.a = a
self.b = b
....
If that is what you want, then this might help you: Put all the values in a
dictionary - like this:

my_vals = {"a": 1, "b" : 2, ....}

There are plenty of other ways to create such a dictionary, but I won't
digress on that here.

Now in your class, you pass than dict to your constructor and then simply
update the instance's __dict__ so that the keys-value-pairs in my_vals
become attributes:

class Foo:

def __init__(self, my_vals):
self.__dict__.u pdate(my_vals)

foo = Foo(my_vals)

print foo.a

-> 1

Hope this helps,
--
Regards,

Diez B. Roggisch
Jul 18 '05 #5
Kent Johnson wrote:
Martin MOKREJ© wrote:
Hi,
I'm looking for some easy way to do something like include in c or PHP.
Imagine I would like to have:
cat somefile.py
a = 222
b = 111
c = 9
cat somefile2.py
self.xxx = a
self.zzz = b
self.c = c
self.d = d
cat anotherfile.py

def a():
include somefile
postprocess(a)

def b():
include somefile
postprocess(a, b, c)

class klass():
def __init__(self, a, b, c, d):
include somefile2

You can do this with module-level variables and a base class for klass:

cat somefile.py
a = 222
b = 111
c = 9
cat somefile2.py
class base:
def __init__(self, a, b, c, d):
self.xxx = a
self.zzz = b
self.c = c
self.d = d
cat anotherfile.py
import somefile, somefile2

def a():
postprocess(som efile.a)

def b():
postprocess(som efile.a, somefile.b, somefile.c)

class klass(somefile2 .base):
def __init__(self, a, b, c, d):
somefile2.base. __init__(self, a, b, c, d)


Oh, I've picked up not the best example. I wanted to set the variables
not under __init__, but under some other method. So this is actually
what I really wanted.

class klass(somefile2 .base):
def __init__():
pass

def set_them(self, a, b, c, d):
somefile2.base. __init__(self, a, b, c, d)

Thanks!
Martin
Jul 18 '05 #6
Diez B. Roggisch wrote:
See my post on Mar 2 about "automating assignment of class variables".
I got no answers, maybe I wasn't clear enough ... :(

Seems so - I for example didn't understand it.

I need to define lots of variables. The variable names are often
identical. The problem is that if I put such a code into a function ...
no, I'm not going to pass anything to a function to get it returned back.
I just want to get lots of variables assigned, that all. If I put them
into module, it get's exectued only once unless I do reload. And I'd have
to use: "from some import *", because mainly I'm interrested in assigning
to self: self.x = "blah"
self.y = "uhm"

Okay, I try and guess: From your two posts I infer that you want to set
variables in instances. But you've got lots of these and you don't want to
write code like this:

class Foo:
def __init__(self, a, b, .....):
self.a = a
self.b = b
....
If that is what you want, then this might help you: Put all the values in a


Good guess! ;)
dictionary - like this:

my_vals = {"a": 1, "b" : 2, ....}

There are plenty of other ways to create such a dictionary, but I won't
digress on that here.

Now in your class, you pass than dict to your constructor and then simply
update the instance's __dict__ so that the keys-value-pairs in my_vals
become attributes:

class Foo:

def __init__(self, my_vals):
self.__dict__.u pdate(my_vals)

foo = Foo(my_vals)

print foo.a

-> 1

Hope this helps,


Sure. Thanks! Would you prefer exactly for this method over the method posted by Kent Johnson
few minutes ago?

Am I so deperately fighting the language? No-one here on the list needs to set hundreds
variables at once somewhere in their code? I still don't get why:

"include somefile.py" would be that non-pythonic so that it's not available, but
I have already two choices how to proceed anyway. Thanks. ;)

Now have to figure out how to assign them easily into the XML tree.

martin
Jul 18 '05 #7
Martin MOKREJ© wrote:
Oh, I've picked up not the best example. I wanted to set the variables
not under __init__, but under some other method. So this is actually
what I really wanted.

class klass(somefile2 .base):
def __init__():
pass

def set_them(self, a, b, c, d):
somefile2.base. __init__(self, a, b, c, d)


In that case you can define set_them() directly in the base class and omit set_them from klass:

class base:
def set_them(self, a, b, c, d):
self.a = a
# etc

I'm guessing here, but if a, b, c, d are the same variables defined in your original somefile.py,
and the intent is to make them attributes of the klass instance, you could do something like this
and avoid listing all the arguments to klass.set_them( ):

## somefile.py
a = 222
b = 111
c = 9

def set_them(obj):
obj.a = a
obj.b = b
obj.c = c

## anotherfile.py

import somefile
class klass:
def set_them(self):
somefile.set_th em(self)

Kent
Jul 18 '05 #8
Martin MOKREJŠ wrote:
Am I so deperately fighting the language? No-one here on the list
needs to set hundreds variables at once somewhere in their code? I
still don't get why:


I've certainly never needed to set hundreds of variables at once in my
code. I might have hundreds of values, but if so they go into a list or a
dictionary or some other suitable data structure.

There isn't any point setting hundreds of variables unless you also
have hundreds of different expressions accessing hundreds of variables. If
they are all accessed in a similar way then this indicates they shouldn't
be separate variables. If they are all accessed differently then you have
some very complex code and hard to maintain code which can probably be
simplified.

Can you give a use case where you think 'hundreds of variables' would be
needed?
Jul 18 '05 #9
Martin MOKREJŠ wrote:
Diez B. Roggisch wrote:
See my post on Mar 2 about "automating assignment of class variables".
I got no answers, maybe I wasn't clear enough ... :(
Seems so - I for example didn't understand it.

I need to define lots of variables. The variable names are often
identical. The problem is that if I put such a code into a function ...
no, I'm not going to pass anything to a function to get it returned
back.
I just want to get lots of variables assigned, that all. If I put them
into module, it get's exectued only once unless I do reload. And I'd
have
to use: "from some import *", because mainly I'm interrested in
assigning
to self: self.x = "blah"
self.y = "uhm"


Okay, I try and guess: From your two posts I infer that you want to set
variables in instances. But you've got lots of these and you don't
want to
write code like this:

class Foo:
def __init__(self, a, b, .....):
self.a = a
self.b = b
....
If that is what you want, then this might help you: Put all the values
in a

Good guess! ;)
dictionary - like this:

my_vals = {"a": 1, "b" : 2, ....}

There are plenty of other ways to create such a dictionary, but I won't
digress on that here.

Now in your class, you pass than dict to your constructor and then simply
update the instance's __dict__ so that the keys-value-pairs in my_vals
become attributes:

class Foo:

def __init__(self, my_vals):
self.__dict__.u pdate(my_vals)

foo = Foo(my_vals)

print foo.a

-> 1

Hope this helps,

Sure. Thanks! Would you prefer exactly for this method over the method
posted by Kent Johnson
few minutes ago?

Am I so deperately fighting the language? No-one here on the list needs
to set hundreds
variables at once somewhere in their code? I still don't get why:

Well, consider that you haven't actually made any kind of a case for
using variables!

If the names of these things are dynamic then why would you want to put
them in an object's namespace, when you could just as easily include

self.insDict = {}

in your __init__() method and then simply update self.insDict whenever
you want.

If you "set hundreds variables", and their names aren't predictable,
then presumably you will have to go through similar contortions to
access them.

So it is generally simpler just to use a dictionary to hold the values
whose names aren't known in advance. Techniques for inserting names into
namespaces are known (as you have discovered), but when a beginner wants
to know about them it's usually considered a sign of "fighting the
language".

So, think about this a while and then tell us exactly *why* it's so
important that these values are stored in variables.

"include somefile.py" would be that non-pythonic so that it's not
available, but
I have already two choices how to proceed anyway. Thanks. ;)

Now have to figure out how to assign them easily into the XML tree.

martin


You do know there are lots of Python libraries that support XML, right?

regards
Steve

Jul 18 '05 #10

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

Similar topics

5
1631
by: Abby Lee | last post by:
My code does what I want (works unless there is a lot of volume...works for this month cause not a lot of items marked paid yet...) but the page times out for last month because there is just so many items. Is there a better way to do this? my page gets all distinct Oganizations. within each organization it gets each distinct Fund within each fund it gets each distinct Program then it lists each item that is paid for the month being...
22
2909
by: Long | last post by:
Problem: to insert the content of a file in an HTML document at a specific location. One possible way is to add a WebCharm tag like this: <%@charm:text 20 0 my_include_file.txt %> When the HTML template is processed by a WebCharm-aware web server, the content of my_include_file.txt is inserted at the tag location. This work very much like the SSI #include tag. However, you have more control
3
3111
by: .Net Sports | last post by:
I want to include some asp if statements inside a do until loop, predicated on the recordset going until EOF. I do not have any < % %> delimiters inside the include file. The below doesnt show the character I want to display: <% do until rs.eof response.write rs("position") <!-- #include file="inc_.asp" --> loop %>
28
3892
by: Ramesh | last post by:
Hi, I am currently maintaining a legacy code with a very very large code base. I am facing problems with C/C++ files having a lot of un-necessary #includes. On an average every C/C++ file has around 150+ .h files included. I find 75% of the files unnecessary and could be removed. Considering the fact that I have a huge code base, I can't manually fix it. Are there any tools that would report un wanted .h files?
8
2501
by: gumi | last post by:
Hi, I am looking for code for a alarm clock program that pops up a messege to be used as part of my VB.Net class project. Any help is very much appreciated. Thanks
13
3116
by: Alan Silver | last post by:
Hello, MSDN (amongst other places) is full of helpful advice on ways to do data access, but they all seem geared to wards enterprise applications. Maybe I'm in a minority, but I don't have those sorts of clients. Mine are all small businesses whose sites will never reach those sorts of scales. I deal with businesses whose sites get maybe a few hundred visitors per day (some not even that much) and get no more than ten orders per day....
8
2307
by: Eric_Dexter | last post by:
I was looking for a simple way to load a simple python program from another python program. I tried os.system(cabel) The file name is cabel.py a csound instrument editor.. The error I am getting is
0
2000
by: AMDRIT | last post by:
I am looking for better concrete examples, as I am a bit dense, on design patterns that facilitate my goals. I have been out to the code project, planet source code, and microsoft's patterns and practices site and it just isn't sinking in all that clearly to me. Currently we have code in production and it all works well, however it is not the way we want it. We know that we can implement a better design plan to improve performance,...
7
10269
by: guido | last post by:
Hi, I'm looking for a container class that can map whole ranges of keys to objects - something like std::map, but not only for individual values for the key, but for whole ranges. Example: I want to be able to tell the container to return object a for every given key between 0 and 10, object c for every key between 11 and 500000 and object c for every key between 500001 and 599999, without having to
0
9663
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10404
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6761
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.