How can I have increasing named variables? - variables

I want a bunch of buttons, using QtGui to all have their own unique values, but when looping to create a grid of them, the button variable is overwritten.
I was trying to get something that would have each button have its own variable, like grid_btn01, grid_btn02, and so on.
Ideally, it would be like this
for x in range(gridx):
grid_btn + str(x) = GridBtn(self, x, y, btn_id)
But of course, this doesn't work.

consider using a python dictionary
,also i'm not familiar with Qt but double check what is the return value of this function maybe btn_id is the variable you should store
buttons = {}
for x in range(gridx):
buttons[x] = GridBtn(self,x,y,btn_id)

What you're asking may technicallybe possible in Python, but it's definitely the wrong approach.
Use a list instead:
grid_btns = []
for x in range(gridx):
y = ...
grid_btns.append(GridBtn(self, x, y, btn_id))

Related

why are objects clipping behind each other?

I'm making a script that sorts the depth for my objects by prioritizing the y variable, but then afterwards checks to see if the objects that are touching each other have a higher depth the further to the right they are, but for some reason the last part isn't working.
Here's the code:
ds_grid_sort(_dg,1,true);
_yy = 0;
repeat _inst_num
{
_inst = _dg[# 0, _yy];
with _inst
{
with other
{
if (x > _inst.x and y = _inst.y)
{
_inst.depth = depth + building_space;
}
}
}
_yy++;
}
I've identified that the problem is that nothing comes out as true when the game checks the y = _inst.y part of the _inst statement, but that doesn't make any sense seeing how they're all at the same y coordinate. Could someone please tell me what I'm doing wrong?
As Steven mentioned, it's good practice to use double equal signs for comparisons (y == _inst.y) and a single equals sign for assignments (_yy = 0;), but GML doesn't care if you use a single equals sign for comparison, so it won't be causing your issue. Though it does matter in pretty much every other language besides GML.
From what I understand, the issue seems to be your use of other. When you use the code with other, it doesn't iterate through all other objects, it only grabs one instance. You can test this by running this code and seeing how many debug messages it shows:
...
with other
{
show_debug_message("X: "+string(x)+"; Y: "+string(y));
...
You could use with all. That will iterate through all objects or with object, where object is either an object or parent object. That will iterate through all instances of that object. However, neither of these functions check whether the objects overlap (it's just going to iterate over all of them), so you'll have to check for collisions. You could do something like this:
...
with all
{
if place_meeting(x, y, other)
{
if (x > _inst.x and y = _inst.y)
{
_inst.depth = depth + building_space;
}
}
...
I don't know what the rest of your code looks like, but there might be an easier way to achieve your goal. Is it possible to initially set the depth based on both the x and y variables? Something such as depth = -x-y;? For people not as familiar with GameMaker, objects with a smaller depth value are drawn above objects with higher depth values; that is why I propose setting the depth to be -x-y. Below is what a view of that grid would look like (first row and column are x and y variables; the other numbers would be the depth of an object at that position):
Having one equation that everything operates on will also make it so that if you have anything moving (such as a player), you can easily and efficiently update their depth to be able to display them correctly relative to all the other objects.
I think it should be y == _inst.y.
But I'm not sure as GML tends to accept such formatting.
It's a better practise to use == to check if they're equal when using conditions.

Arrays with attributes in Julia

I am making my first steps in julia, and I would like to reproduce something I achieved with numpy.
I would like to write a new array-like type which is essentially an vector of elements of arbitrary type, and, to keep the example simple, an scalar attribute such as the sampling frequency fs.
I started with something like
type TimeSeries{T} <: DenseVector{T,}
data::Vector{T}
fs::Float64
end
Ideally, I would like:
1) all methods that take a Vector{T} as argument to take on TimeSeries{T}.
e.g.:
ts = TimeSeries([1,2,3,1,543,1,24,5], 12.01)
median(ts)
2) that indexing a TimeSeries always returns a TimeSeries:
ts[1:3]
3) built-in functions that return a Vector to return a TimeSeries:
ts * 2
ts + [1,2,3,1,543,1,24,5]
I have started by implementing size, getindex and so on, but I definitely do not see how it could be possible to match points 2 and 3.
numpy has a quite comprehensive way to doing this: http://docs.scipy.org/doc/numpy/user/basics.subclassing.html. R also seems to allow linking attributes attr()<- to arrays.
Do you have any idea about the best strategy to implement this sort of "array with attributes".
Maybe I'm not understanding, why is for say point 3 it not sufficient to do
(*)(ts::TimeSeries, n) = TimeSeries(ts.data*n, ts.fs)
(+)(ts::TimeSeries, n) = TimeSeries(ts.data+n, ts.fs)
As for point 2
Base.getindex(ts::TimeSeries, r::Range) = TimeSeries(ts.data[r], ts.fs)
Or are you asking for some easier way where you delegate all these operations to the internal vector? You can clever things like
for op in (:(+), :(*))
#eval $(op)(ts::TimeSeries, x) = TimeSeries($(op)(ts.data,x), ts.fs)
end

Are Gadfly plots currently composable?

Is there currently a way to add plot elements together in Gadfly.jl?
For example, in R if I have another function that returns a ggplot and I want to add a title to it, I'd do the following:
p <- makeMyPlot()
p + ggtitle("Now it has a title")
Is there currently a Gadfly equivalent? If not, is this on Gadfly's roadmap?
There is add_plot_element(), which can add stuff to an existing layer:
xs = [0:0.1:pi]
l = layer(x=xs, y=sin(xs))
add_plot_element(l, Guide.title("Now it has a title"))
You can then plot the layer using plot(l), and invoke either draw or display to actually show something. Further down, there's a bunch of overloads that work on a Plot directly:
p = plot(x=xs, y=sin(xs))
add_plot_element(p, Guide.title("Now it has a title"))
display(p)
I can't find either of these functions in the documentation, but fortunately the source is comprehensible enough. One of the many joys of Julia =)

Python 2.7 Tkinter, OOP, and Callbacks

I'm just starting to piece together a simple utility for turning on a projector and selecting some presets on it. I was using it from a command line but then thought it would be a good excuse to start learning Tkinter. I'm struggling with the OOP design, since clearly you can do this with functions and so on. As I started adding more features and buttons, it seemed kind of crazy to have a particular callback to a one off function for each button. How do engineers deal with this?
If you have simillar buttons with simillar functions you can use loop (for) to create that buttons and you can use one function with different argument. You have to use lambda function to call function with arguments.
(not complet) example:
def my_func(a, b):
print a, b
Button("Hello", command=lambda arg1="abc",arg2=123:my_func(arg1, arg2)).pack()
Button("World", command=lambda arg1="xyz",arg2=987:my_func(arg1, arg2)).pack()
You can even use list to keep arguments for all buttons.
def my_func(a, b):
print a, b
buttons = (
# title, x, y, function name, function arguments)
("Hello", 0, 0, my_func, ("abc", 123)),
("World", 0, 1, my_func, ("xyz", 987)),
)
for btn in buttons:
title, x, y, func_name, func_args = btn
temp = Button(title, command=lambda func=func_name, args=func_args:func(*args) )
temp.grid(row=y, column=x)

Blender draw list via script, Add Mesh: Extra Object

I am trying to "visualize" a list of numbers from a .csv file.
I could do that by writing a blender script and thats what I already did.
But I would like to know how the Add Mesh: Extra Object - Math Function could help me (or other functions/addons/anything).
I would like to have a 2D Line (x=+1, y=0, z= value from list) that I would rotate afterwards.
In other words can I convert a list of numbers into a function ?
Sorry if that is a stupid question.
Thanks for the help.
Where The Extra Object - Math Function addon uses minU maxU and stepU as user input values that are used to calculate the value of u before evaluating the function to generate the mesh, you want to let the user input an int value and use it as an index into an array -
AvailZOptions = [1.3, 2.6, 5.2, 10.4]
lineEnd = [xvalue, yvalue, AvailZOptions[OptZUserChoice]]
Another option would be to get the zValue from a function -
def CalcZValue:
if OptZUserChoice = 1:
return 1.3
elif OptZUserChoice = 2:
return 2.6 * cos(x)
lineEnd = [xvalue, yvalue, CalcZValue]