473,802 Members | 2,031 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
22 2291
Martin MOKREJŠ wrote:
Steve Holden wrote:

[...]
I will be *very* surprised if you can't get a much better (i.e. easier
and more efficient) solution by stepping back from the programming

Hmm, I'm not convinced, but I'll put few more words here then. ;)
details for a moment and explaining what it is you are actually trying
to achieve in user-space.

Can you describe the problem you are trying to solve, rather than the
solution you are hoping to adopt?

User inputs data through html forms, data has to be quality-checked
and strored into mysql. Later, data is read from mysql and presented
through web. There's nothing special in it, just that the tables
describe a lot of specific experimental parameters. Tables do reflect
type of informations, so they group the data into logical units - so
tablename reflects the data contained.

In general, there are two types of data, hence my X and Y objects.
The underlaying data at some time point go into sql tables. Before that
happens, the data == variable contents are checked that they contain
expected values (in some cases enumerated values, in some cases integers,
sometime chars and only about 6 blobs). I spent a year developing the
database schema and php code, the schema is nearly optimal. I got bored
by the php code, as it was partly developed by a lazy guy (lazier than
I'm).
I went fot python - to have better error handling, have not only web app,
but reusable code for standalone application (html forms can be replaced
by any tcl/tk widget for M$ Windows). Sql transaction I have added to
the php code, but anyway it sucks to work with it further.

My idea is to check some of the values while instantiating, as I get it for
free (assigning either to a default value or raising an exception when
variable is empty). In most cases this is not enough, and I have to type in
the allowed values.
1. In case of enumerated types, I hope to find a tool
able to read sql files and able to extract column definitions. In this
particular case, the program would dynamically read allowed ENUM values,
so whenever sql table is altered, the program will recognize new value
allowed.
2. In most other cases, the values are simply some kind of string, and
.find() et al. will suffice.
3. In case data was read from mysql, I can verify that foreign keys refer
to what they should refer.

OK, I get the data written to mysql. I can fetch it back, and want to dump
it into xml and present on web/(local gui).

I have the claases corresponding to all tables as superclasses of X and Y
as necessary. I went to ask on this list how to assign the variables easily
because parts of the code are almost identical. I believe this has been
answered quite well.

I believe the approach using classes corresponding to every single table
is right, when using them as superclasses for those two, practically
used objects: X and Y.

To print the output on web or any gui, I think I'll use the xml output
and just parse it. I need xml anyway for testing, and definitely want
to be able to construct the html/GUI output from the xml input - again,
for testing. So the objects will more or less exist only to get the
necessary checks done for those myriads of variables, which must be
evaluated in current context. I'd get crazy if I'd store things into
bsbdb -- I'm not going to remember that a[0] is table1, a[1] is table2,
a[0][0] is the primary key called blah, a[0][22] is allowed to be equal
only to "foo" or "bar" ... and that if a[2][4] is defined (actually
number),
the number is the key to search in c[key]. Simply, that's for what I use
mysql
I don't want to invent the database schema in bsddb in python. ;)
It's simply data, it must be read into variables in some objects, those
object are groupped into just two superobjects. The superobjects define
check-methods, define how to dump the it's data into xml, how
to write (in which order) the values into mysql.

I'm sorry not to send in the sql schema + the code, but this is my phd
thesis. ;)

I'm very glad there's so many people interrested to help - not only - me.
Thanks! Now I'm really looking forward how would you rework this thing.
It's simple, easy, it's just sometime tedious as having 250 columns in
20 tables
simply makes you bored to type the code, after while.

The only think where I think I need help is, how to dump easily into xml
say object
X, having variables a, b, c, where c is a ref. to object B, containing
variables p, q, r.
B = obj()
setattr(B, p, 44)
setattr(B, q, "sdjahd")
setattr(B, r, "qew")
X = obj()
setattr(X, a, 1)
setattr(X, a, 2)
setattr(X, a, B)
print do_magick(X)
<X>
<a>1</a>
<b>2</b>
<B>
<p>44</p>
<q>sdjahd</q>
<r>qew</r>
</B>
</X>

I still don't really see why you have to store this thing as objects,
but I appreciate that you can only give limited information and still
retain the validity of a thesis.

My own usual approach in such situations is to use metadata to describe
the structure and required processing of the data, as it's often much
easier to write small amounts of relatively flexible data-driven code
that it is to hard-wire all the logic around specific structures and data.

However you appear to have chosen your approach, so having made your bed
you must now proceed to lie on it! :-) Good luck with the thesis.

regards
Steve

Jul 18 '05 #21
>
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 once (and only once) needed hundreds of variables in a program. It
was to simplify creation of unit tests, not for production use. The
variable names and data (representing a graph with named nodes) was
stored in a text file, I read that file and used setattr() to create
each variable. This was in a module that did nothing else, and was
imported by unit test code that benefited from the names when setting
up easily readable test cases.

Background if you're new to Python: importing a module *executes* it;
for most modules the only important stuff executing is the class and
def statements. However, you can execute more stuff when (rarely)
necessary -- reading a file in my case. This is very different than,
say, C or C++, which has separate include and execute steps.

In general, you want to either use the built-in lists and dicts, or
create classes/objects to represent hundreds of things.

Brian.
Jul 18 '05 #22
Steve Holden wrote:
Martin MOKREJŠ wrote:
Steve Holden wrote:
[...]
I will be *very* surprised if you can't get a much better (i.e.
easier and more efficient) solution by stepping back from the
programming


Hmm, I'm not convinced, but I'll put few more words here then. ;)
details for a moment and explaining what it is you are actually
trying to achieve in user-space.

Can you describe the problem you are trying to solve, rather than the
solution you are hoping to adopt?


User inputs data through html forms, data has to be quality-checked
and strored into mysql. Later, data is read from mysql and presented
through web. There's nothing special in it, just that the tables
describe a lot of specific experimental parameters. Tables do reflect
type of informations, so they group the data into logical units - so
tablename reflects the data contained.

In general, there are two types of data, hence my X and Y objects.
The underlaying data at some time point go into sql tables. Before that
happens, the data == variable contents are checked that they contain
expected values (in some cases enumerated values, in some cases integers,
sometime chars and only about 6 blobs). I spent a year developing the
database schema and php code, the schema is nearly optimal. I got bored
by the php code, as it was partly developed by a lazy guy (lazier than
I'm).
I went fot python - to have better error handling, have not only web app,
but reusable code for standalone application (html forms can be replaced
by any tcl/tk widget for M$ Windows). Sql transaction I have added to
the php code, but anyway it sucks to work with it further.

My idea is to check some of the values while instantiating, as I get
it for
free (assigning either to a default value or raising an exception when
variable is empty). In most cases this is not enough, and I have to
type in
the allowed values.
1. In case of enumerated types, I hope to find a tool
able to read sql files and able to extract column definitions. In this
particular case, the program would dynamically read allowed ENUM values,
so whenever sql table is altered, the program will recognize new value
allowed.
2. In most other cases, the values are simply some kind of string, and
.find() et al. will suffice.
3. In case data was read from mysql, I can verify that foreign keys refer
to what they should refer.

OK, I get the data written to mysql. I can fetch it back, and want to
dump
it into xml and present on web/(local gui).

I have the claases corresponding to all tables as superclasses of X and Y
as necessary. I went to ask on this list how to assign the variables
easily
because parts of the code are almost identical. I believe this has been
answered quite well.

I believe the approach using classes corresponding to every single table
is right, when using them as superclasses for those two, practically
used objects: X and Y.

To print the output on web or any gui, I think I'll use the xml output
and just parse it. I need xml anyway for testing, and definitely want
to be able to construct the html/GUI output from the xml input - again,
for testing. So the objects will more or less exist only to get the
necessary checks done for those myriads of variables, which must be
evaluated in current context. I'd get crazy if I'd store things into
bsbdb -- I'm not going to remember that a[0] is table1, a[1] is table2,
a[0][0] is the primary key called blah, a[0][22] is allowed to be
equal only to "foo" or "bar" ... and that if a[2][4] is defined
(actually number),
the number is the key to search in c[key]. Simply, that's for what I
use mysql
I don't want to invent the database schema in bsddb in python. ;)
It's simply data, it must be read into variables in some objects, those
object are groupped into just two superobjects. The superobjects define
check-methods, define how to dump the it's data into xml, how
to write (in which order) the values into mysql.

I'm sorry not to send in the sql schema + the code, but this is my phd
thesis. ;)

I'm very glad there's so many people interrested to help - not only - me.
Thanks! Now I'm really looking forward how would you rework this thing.
It's simple, easy, it's just sometime tedious as having 250 columns in
20 tables
simply makes you bored to type the code, after while.

The only think where I think I need help is, how to dump easily into
xml say object
X, having variables a, b, c, where c is a ref. to object B, containing
variables p, q, r.
> B = obj()
> setattr(B, p, 44)
> setattr(B, q, "sdjahd")
> setattr(B, r, "qew")
> X = obj()
> setattr(X, a, 1)
> setattr(X, a, 2)
> setattr(X, a, B)

> print do_magick(X)

<X>
<a>1</a>
<b>2</b>
<B>
<p>44</p>
<q>sdjahd</q>
<r>qew</r>
</B>
</X>
>

I still don't really see why you have to store this thing as objects,
but I appreciate that you can only give limited information and still
retain the validity of a thesis.


The project is not published yet. When it is, I'll make it free. I'm a biologist,
and most biologists care only about the content of the database, not about
*any* technical details. It's very interresting project for them/me,
and I'm the only one who cares about technical details.
My own usual approach in such situations is to use metadata to describe
the structure and required processing of the data, as it's often much
easier to write small amounts of relatively flexible data-driven code
that it is to hard-wire all the logic around specific structures and data.


Can you give me some example? What are the "metadata"? Sure I want to learn
something and I don't rely on almost anything. But I simply thought that
the object at least group together common methods, common variables.
Anyway when reading or writing to a single sql table, I have to have handy
which coulmns to expect. Supergrouping into superobject gives me way to
define order, in which I have to interact with set of mysql tables.
Whats' wrong here? ;)

M.
Jul 18 '05 #23

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
3118
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
2309
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
2002
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
10279
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
9699
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
10304
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9114
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
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
6838
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
5494
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...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2966
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.