I`m using a function within a script which produces a number of variables.
The function is used at different places in the script.
It produces variables a,b,c,d,e etc
def function(input):
a,b,c,d,e = 1,2,3,4,5
return a,b,c,d,e
I call the function as follows:
a,b,c,d,e = function(input)
This way all the variables a,b,c,d,e etc... need to be repeated every time I want to use the function. Is there a way to prevent this repetition?
For example I tried to make a list of the variables and then zip them with the output of the function and then connect them but that is not working.
listofvariables = [a,b,c,d,e]
outputfunction = function(input)
gezipt = zip(listofvariables,outputfunction)
for itm in gezipt:
itm[0] = itm[1]
That way when I change the function I would have to rewrite the list with the variables once and not every time the function is used. but that doesnt work.
The answer to your question, of whether you can programmatically set variables is mostly no. There probably are ways you could make it work, but they'd be fragile or hard to understand. Generally speaking, treating variable names as part of your program's data is a bad idea.
If you have lots of related data items, you should generally be using a data structure to group them all together, rather than using separate variables for each one. Your example is dealing with a batch of five variables, and at several different points keeps them in a tuple. That tuple is probably what you want, though you could use a list if you need to modify the values, or maybe a dictionary if you wanted to be able to index them by key (e.g. {"a": 1, "b": 2}). For other more complicated situations, you might need to program up your own container for the data, such as a class. You might even use several different containers for different sub-sets of your data, and then keep those separate containers in a further container (resulting in a nested data structure).
Unfortunately, that's about as detailed as we can get without more details about your circumstances. If you need more specific help, you should probably ask another question with more details about your actual application.
Related
I am writing a program that receives data over time and I am looking for different patterns in the data.
I have to save data for different processes that I create in the program for future calculations,
I want to save the data to an Oracle DB (that has some support for storing objects).
the information that I want to relate to a new process has the following structure:
list of logic expressions:
stage 1 ->(a*b*c)+(d*e)+..(can have more conditions)
stage 2 ->(f*a*c)+(a*b)+..
stage 3 ->(g*h*i)+(j*k)+..
each letter: a,b, c,d etc represent a logic function that has different parameters related to it, I need to save these parameters for future usage of each logic function.
the * represents logical AND
the + represents logical OR
The question is how to implement it?
I can create an object for each letter, e.g. for "a" (which can be a function or a condition that needs to be check etc) and save the data of this object to the oracle DB.
A numerator can be given to each process to identify it, however I am not sure how to identify each one of the logic functions (e.g. "a") because I need later to assemble the data from the database back to the original process that I am handling (example stage 1).
Regarding linked lists, not sure if to use them in my program to represent the structure of each logic in each stage e.g. a->b->c->(new OR expression)->d->e. or maybe there is a better solution? I can also save this information as a string and try to do parsing later
e.g. string command="stage 1 ->(a*b*c)+(d*e)"
in case that I will be using linked list, I am not sure how to save the structure of the lists to the database.
for the external structure, stage1,stage2, stage3.. etc not sure also if to use linked lists and how to save them to a database.
I would appreciate some advice on how to build it.
Thanks!
Let's build this from the bottom up. You want to refrain from writing your own linked list structures, if possible.
A stage consists of one or more products that are summed.
The products are pointers to functions or function objects, let's call them functors.
Each group of functions objects could be a std::vector<function_object>. These group would have the results multiplied together; which can be handled in a loop.
A stage is one or more of the above vectors. This can be expressed as:
std::vector< std::vector<function_object> >
You can add another dimension for the stages:
std::vector< std::vector< std::vector<function_object> > >
If you prefer to use linked list, replace std::vector with std::list.
Edit 1: Function IDs not objects
Most databases have a difficult time storing code for a function. So, you'll have to use function identifiers instead.
A function identifier is a number associated with a function. This association will be in your code and not in the data. The easiest implementation is to use an array of function objects or pointers. Use the function identifier as an index into the array, the retrieve the functor.
A more robust method is to use a table of <function_id, functor>. This structure allows for the records to be in any order, and the records can be deleted without damaging the code. With the vector, slots must never be removed.
struct Table_Entry
{
unsigned int function_id;
Function_Pointer p_function;
const char * function_name;
};
Table Entry function_associations[] =
{
{5, logic_function_1, "Logic Function 1"},
//...
};
When should I use an Erlang record instead of a tuple? Or, visa-versa, when is a Erlang record unnecessary? I am relatively new to Erlang and I am not sure if I am using records and tuples properly. I understand from what I have read that records are essentially stored as tuples behind the scenes.
I typically use records for pieces of data that are going to be passed around the application or persisted somewhere. I use tuples things like the return value of a function, params of a function, and for things that are specific to the body of a function.
Am I using records and tuples correctly? Is there documentation outlining when one type should be used over another?
It is a style question. But do note:
Tuples of large arity are hard to get correct and you will easily swap values. A record names each field making swaps less likely.
You can easily match on a record for a subset of all fields.
A record always needs the same arity. As such they are bad for emulating sum types.
Records are not shared over modules which leads to lots of .hrl files with include statements if they are used between modules. This breaks the abstraction.
Records can be kept module-local to make it harder for others to use the record. This improves modularity.
I need to store a list vectors of different types, each to be referenced by a string identifier. For now, I'm using std::map with std::string as the key and boost::any as it's value (example implementation posted here).
I've come unstuck when trying to run a method on all the stored vector, e.g.:
std::map<std::string, boost::any>::iterator it;
for (it = map_.begin(); it != map_.end(); ++it) {
it->second.reserve(100); // FAIL: refers to boost::any not std::vector
}
My questions:
Is it possible to cast boost::any to an arbitrary vector type so I can execute its methods?
Is there a better way to map vectors of arbitrary types and retrieve then later on with the correct type?
At present, I'm toying with an alternative implementation which replaces boost::any with a pointer to a base container class as suggested in this answer. This opens up a whole new can of worms with other issues I need to work out. I'm happy to go down this route if necessary but I'm still interested to know if I can make it work with boost::any, of if there are other better solutions.
P.S. I'm a C++ n00b novice (and have been spoilt silly by Python's dynamic typing for far too long), so I may well be going about this the wrong way. Harsh criticism (ideally followed by suggestions) is very welcome.
The big picture:
As pointed out in comments, this may well be an XY problem so here's an overview of what I'm trying to achieve.
I'm writing a task scheduler for a simulation framework that manages the execution of tasks; each task is an elemental operation on a set of data vectors. For example, if task_A is defined in the model to be an operation on "x"(double), "y"(double), "scale"(int) then what we're effectively trying to emulate is the execution of task_A(double x[i], double y[i], int scale[i]) for all values of i.
Every task (function) operate on different subsets of data so these functions share a common function signature and only have access to data via specific APIs e.g. get_int("scale") and set_double("x", 0.2).
In a previous incarnation of the framework (written in C), tasks were scheduled statically and the framework generated code based on a given model to run the simulation. The ordering of tasks is based on a dependency graph extracted from the model definition.
We're now attempting to create a common runtime for all models with a run-time scheduler that executes tasks as their dependencies are met. The move from generating model-specific code to a generic one has brought about all sorts of pain. Essentially, I need to be able to generically handle heterogenous vectors and access them by "name" (and perhaps type_info), hence the above question.
I'm open to suggestions. Any suggestion.
Looking through the added detail, my immediate reaction would be to separate the data out into a number of separate maps, with the type as a template parameter. For example, you'd replace get_int("scale") with get<int>("scale") and set_double("x", 0.2) with set<double>("x", 0.2);
Alternatively, using std::map, you could pretty easily change that (for one example) to something like doubles["x"] = 0.2; or int scale_factor = ints["scale"]; (though you may need to be a bit wary with the latter -- if you try to retrieve a nonexistent value, it'll create it with default initialization rather than signaling an error).
Either way, you end up with a number of separate collections, each of which is homogeneous, instead of trying to put a number of collections of different types together into one big collection.
If you really do need to put those together into a single overall collection, I'd think hard about just using a struct, so it would become something like vals.doubles["x"] = 0.2; or int scale_factor = vals.ints["scale"];
At least offhand, I don't see this losing much of anything, and by retaining static typing throughout, it certainly seems to fit better with how C++ is intended to work.
Usually, entities and components or other parts of the game code in data-driven design will have names that get checked if you want to find out which object you're dealing with exactly.
void Player::Interact(Entity *myEntity)
{
if(myEntity->isNearEnough(this) && myEntity->GetFamilyName() == "guard")
{
static_cast<Guard*>(myEntity)->Say("No mention of arrows and knees here");
}
}
If you ignore the possibility that this might be premature optimization, it's pretty clear that looking up entities would be a lot faster if their "name" was a simple 32 bit value instead of an actual string.
Computing hashes out of the string names is one possible option. I haven't actually tried it, but with a range of 32bit and a good hashing function the risk of collision should be minimal.
The question is this: Obviously we need some way to convert in-code (or in some kind of external file) string-names to those integers, since the person working on these named objects will still want to refer to the object as "guard" instead of "0x2315f21a".
Assuming we're using C++ and want to replace all strings that appear in the code, can this even be achieved with language-built in features or do we have to build an external tool that manually looks through all files and exchanges the values?
Jason Gregory wrote this on his book :
At Naughty Dog, we used a variant of the CRC-32 algorithm to hash our strings, and we didn't encounter a single collision in over two years of development on Uncharted: Drake's Fortune.
So you may want to look into that.
And about the build step you mentioned, he also talked about it. They basically encapsulate the strings that need to be hashed in something like:
_ID("string literal")
And use an external tool at build time to hash all the occurrences. This way you avoid any runtime costs.
This is what enums are for. I wouldn't dare to decide which resource is best for the topic, but there are plenty to choose from: https://www.google.com/search?q=c%2B%2B+enum
I'd say go with enums!
But if you already have a lot of code already using strings, well, either just keep it that way (simple and usually enough fast on a PC anyway) or hash it using some kind of CRC or MD5 into an integer.
This is basically solved by adding an indirection on top of a hash map.
Say you want to convert strings to integers:
Write a class wraps both an array and a hashmap. I call these classes dictionaries.
The array contains the strings.
The hash map's key is the string (shared pointers or stable arrays where raw pointers are safe work as well)
The hash map's value is the index into the array the string is located, which is also the opaque handle it returns to calling code.
When adding a new string to the system, it is searched for already existing in the hashmap, returns the handle if present.
If the handle is not present, add the string to the array, the index is the handle.
Set the string and the handle in the map, and return the handle.
Notes/Caveats:
This strategy makes getting the string back from the handle run in constant time (it is merely an array deference).
handle identifiers are first come first serve, but if you serialize the strings instead of the values it won't matter.
Operator[] overloads for both the key and the value are fairly simple (registering new strings, or getting the string back), but wrapping the handle with a user-defined class (wrapping an integer) adds a lot of much needed type safety, and also avoids ambiguity if you want the key and the values to be the same types (overloaded[]'s wont compile and etc)
You have to store the strings in RAM, which can be a problem.
What's the best way to store the following message into a data structure for easy access?
"A=abc,B=156,F=3,G=1,H=10,G=2,H=20,G=3,H=30,X=23.50,Y=xyz"
The above consists of key/value pairs of the following:
A=abc
B=156
F=3
G=1
H=10
G=2
H=20
G=3
H=30
X=23.50
Y=xyz
The tricky part is the keys F, G and H. F indicates the number of items in a group whose item consists of G and H.
For example if F=3, there are three items in this group:
Item 1: G=1, H=10
Item 2: G=2, H=20
Item 3: G=3, H=30
In the above example, each item consists of two key/pair values: G and H. I would like the data structure to be flexible such that it can handle if the item increases its key/pair values. As much as possible, I would like to maintain the order it appears in the string.
UPDATE: I would like to store the key/value pairs as strings even though the value often appears as float or other data type, like a map.
May not be what you're looking for, but I'd simply recommend using QuickFIX (quickfixengine.org), which is a very high quality C++ FIX library. It has the type "FIX::Message" which does everything you're looking for, I believe.
I work with FIX a lot in Python an Perl, and I tend to use a dictionary or hash. Your keys should be unique within the message. For C++, you could look at std::map or STL extension std::hash_map.
If you have a subset of FIX messages you have to support (most exchanges usually use 10-20 types), you can roll your own classes to parse messages into. If you're trying to be more generic, I would suggest creating something like a FIXChunk class. The entirety of the message could be stored in this class, organized into keys and their values, as well as lists of repeating groups. Each of the repeating groups would itself be a FIXChunk.
A simple solution, but you could use a std::multimap<std::string,std::string> to store the data. That allows you to have multiple keys with the same value.
In my experience, fix messages are usually stored either in their original form (as a stream of bytes) or as a complex data structure providing a full APIs that can handle their intricacies. After all, a fix message can sometimes represent a tree of data.
The problem with the latter solution is that the transition is expensive in terms of computation cost in high-speed trading systems. If you are building a trading system, you may prefer to lazily calculate the parts of the fix message than you need, which is admittedly easier said than done.
I am not familiar with efficient open-source implementations; companies like the one I work for usually have proprietary implementations.