python globals dictionary - global

I want to merge a dictionary , recieved from another module as a function argument to the globals dictionary of the current module. Any idea how this can be done ?
module - test.py
def setdict(indict):
somedict = dict(globals(), **indict)
what i want is, the resultant dictionary somedict is to be set as the globals dictionary of the current module (test) . somedict was created by merging globals() of the current module and the recieved dictionary indict.

globals() returns the current module's global dictionary, which you can then modify. Your function would like:
def setdict(indict):
globals().update(indict)
If there are name clashes, the indict dictionary will win.

Related

Get object handle given the name as a string (MATLAB)

Being given the name of a variable as a string (in my case the name of an existing Simulink.Parameter variable in the workspace selected by the user as a design variable for optimization), I would like to be able to access the properties of the object such as Simulink.Parameter.Min, Simulink.Parameter.Max, Simulink.Parameter.Value without using eval(). So far I am employing the (very ugly) solution
varnames = {'var1','var2'}; % Simulink.Parameter objects existing in workspace
objects = cell(length(varnames),1);
for i = 1:length(varnames)
eval(['objects{i}=', varnames{i}, ';']) % Store objects in a cell array
end
Ideally, this would look like:
objects = get_object_handles_from_string(varnames);
value_1 = object{1}.Value(:);
Otherwise a method returning the variable name given the object handle would also be acceptable.
Methods that I found not to be working but might be useful otherwise:
whos finds variable names and properties in the current workspace but no handles.
inputname returns the variable name of an explicit function input as a string but does not work for cell arrays of objects (see this question).
str2func returns a function handle with a string as input but does not enable access to attributes.
findobj returns objects given an array of objects to iterate over which I do not have. Might there be a method returning all workspace variable handles as an array?
Thanks!
This is exactly what eval is for. Yes, you should avoid using eval, but if you want to have a user type in stuff to be evaluated, you need eval. Or evalin if you want to evaluate it in the base or caller workspace rather than the current workspace.
There are no such thing as "object handles" (except graphics objects, but that is not what you are talking about here). There are variables owning arrays of data, that is it.
If you don't trust your users, don't use eval. They could type in anything, including clear all or !\rm -rf /* (or whatever the Windows equivalent is to erase the disk).
In this case, and presuming there is a limited set of variables that the user can specify, do
var1 = 1;
var2 = 2;
varnames = {'var1','var2'}; % Simulink.Parameter objects existing in workspace
objects = cell(size(varnames));
for i = 1:numel(varnames)
objects{i} = get_variable_value(varnames{i}) % Store objects in a cell array
end
function val = get_variable_value(name)
switch name
case 'var1'
val = evalin('caller',var1);
case 'var2'
val = evalin('caller',var2);
otherwise
error('Illegal variable name')
end

How to get defined variables in Swift?

Does swift have a way to access all global and scope variables that have been defined such as the get_defined_variables function in php? I am trying to target a global variable in a function by having its key name match the string sent to the function.
I ended up solving by holding data in a dictionary and matching the method I wanted to run on the value in the dictionary with an enum. Thanks for the replies.

Is it possible to edit the value of a public variable from another module?

Can you edit the value of a public variable from another module or does that variable have to be open.
My understanding of public/open in swift 3 is:
Public classes can be made in another module but only open classes can be subclassed in another module.
Public functions can be called in another module but only open functions can be overwritten in another module.
But I am unsure about about variables.
You most definitely can! Here is a great way you can accomplish it:
In the module that contains the variable that you wish to manipulate:
// Must be declared globally
public var someValue = "hi"
In a different module that you wish to manipulate the variable from:
// Initialize the module that holds the variable you want to manipulate
var myModule = SomeModule()
// Manipulate the variable (someValue)
myModule.someValue = "bye"
The variable someValue will now have a value of "bye"

Modifying content of immutable array in Swift does not work

Silly beginner Swift question:
I am expecting the following 3 lines of code to work in the playground:
let items = ["Apple", "Orange", "Pear"]
items[1] = "Banana" // error here
items
Now the error
error: '#lvalue $T5' is not identical to 'String'
items[1] = "Banana"
My understanding that updating content of immutable array is possible in Swift.
I use XCODE 6.1.1
Any idea what is going on here?
Thanks
Based on this thread this was possible in previous releases:
Why in immutable array in swift value can be changed?
When you write let, you define an immutable variable. Use var instead; this lets you define a variable that is mutable.
as stated before me- use a var array if you wish to change your array.
a bit below the question you posted a link and an answer was given inside:
Array in Swift has been completely redesigned to have full value
semantics like Dictionary and String have always had in Swift. This
resolves various mutability problems – now a 'let' array is completely
immutable, and a 'var' array is completely mutable – composes properly
with Dictionary and String, and solves other deeper problems. Value
semantics may be surprising if you are used to NSArray or C arrays: a
copy of the array now produces a full and independent copy of all of
the elements using an efficient lazy copy implementation. This is a
major change for Array, and there are still some performance issues to
be addressed. Please see the Swift Programming Language for more
information. (17192555)
The use of the let keyword declares a Constant.
By definition, a constant cannot be modified.
You want to use the var keyword to declare a Variable,
hence things that will/may vary.
From the apple Swift documentation:
Constants and Variables
Constants and variables associate a name (such as
maximumNumberOfLoginAttempts or welcomeMessage) with a value of a
particular type (such as the number 10 or the string "Hello"). The
value of a constant cannot be changed once it is set, whereas a
variable can be set to a different value in the future.
Declaring Constants and Variables
Constants and variables must be declared before they are used. You
declare constants with the let keyword and variables with the var
keyword. Here’s an example of how constants and variables can be used
to track the number of login attempts a user has made:
let maximumNumberOfLoginAttempts = 10
var currentLoginAttempt = 0

Excel VBA: Extract custom class from dictionary

I am having a problem with VBA Excel 2010. I've made a custom Class called 'Enclosure', created an instance of this and added it to a dictonary.
I can then loop through the keys to ensure it has been added.
I'm having trouble then extracting my enclosure class. Here's my code for the extraction sub:
Sub AddEnclosureItem(sItemToAdd As String, ByRef rdEnclosures As Scripting.Dictionary, dDebug As Boolean)
Dim TempEnclosure As Enclosure ' hold enclosure we pull
TempEnclosure = rdEnclosures(1)
End Sub
When I try to compile I get the 'Object Variable or With block variable not set'
Any idea's on how to proceed? I've read somewhere you can declare a dictionary stating the items are of a certain object but I can't get it to work in 2010. That's all I can guess at. OR a way to cast the enclosure item as it comes out.
When you assign an object to a variable, you have to use the Set keyword. The opposite of Set is Let, which is used to assign a value (not an object) to a variable. The Let keyword is optional and almost nobody uses it anymore.
The reason there's a Set and Let is because most objects have a default property - meaning that if you reference an option without a property, it will return the value stored in the default property. The Value property is a common default property. This
x = Range("A1")
is the same as this
x = Range("A1").Value
is the same as this
Let x = Range("A1").Value
Any is acceptable (although I prefer the second one). That's great if x is a Double or String, but if x is a Range object variable, you need use Set
Set x = Range("A1")
If you omit the Set keyword, VBA assumes you wanted Let and tries to assign the (default) Value property to x. That gives the 'Object variable or with block not set' error because it's trying to assign a Double or String to a Range object variable.
Your custom class module probably doesn't have a default value, so none of this should matter. But it does. Even if there is no default value, you have to use Set to reference the object.