I'd like my Fabric tasks to have hyphens (-) instead of underscores (_). For example, database-reset instead of database_reset. However, hyphens aren't permitted as Python function names.
Is it possible in Fabric to create tasks whose names do not exactly match the corresponding Python function?
From the documentation:
#task(alias = 'database-reset')
def database_reset():
...
Cat Plus Plus has the best solution to what you want to do. Tangentially, however, it is technically possible to have Python global variables (a function name is just a global variable) that don't conform to the usual rules by assigning to the globals() dictionary.
def foo_bar():
print "foo-bar"
globals()["foo-bar"] = foo_bar
globals()["foo-bar"]() # prints "foo-bar"
The syntax is not very nice, though, making it quite the hassle.
Related
I have the following example:
def get_max_close(symbol): # Example 1
df = pd.read_csv("../../ml4t/data/{}.csv".format(symbol))
get_max = df["Close"].max()
res = {symbol: float(get_max)}
return res
if __name__ == "__main__":
symbol = ["AAPL", "IBM"] # Example 2
max_close = {"Max": [get_max_close(s) for s in symbol]}
print max_close
My example 1 shows the following issue:
This inspection detects shadowing names defined in outer scopes
Example 2 is the outer scope variable with the same name. I don't want to change the code and I recall reading in one of the PEPs that I can add a _ to this variable to make it work.
I also recall reading that placement of the _ matters (in the same way that camel casing matters in functions).
I believe placing the _ before the variable name (ie. _symbol) is the correct way of achieving this. Placing it after would be exclusively for when I have a variable with the same name as a built in function (ie. dict_).
Am I mixing these two up? And could you provide me the url to the related pep? https://www.python.org/dev/peps/...
There is nothing you need to fix here. It is just a warning from your IDE, not Python.
The IDE is pointing out that the name symbol is used both as a global (in the __name__ == '__main__' block), and as a local in the get_max_close() function. This is fine and not something you need to be concerned about.
The advice is aimed at using names that are provided by Python itself, the built-in names, like list, id, str or all. You can then rename your variable to id_ or all_ and not run the risk of trying to use the built-in as well. That's simply not the case here.
If you used the name _symbol, then by convention you are signalling that the name is considered private to your implementation (e.g. anyone using your code should not rely on the name existing in the same form in future versions).
You could use a different name altogether too, you don't have to use symbol_. I'd just stick to symbol, it is a perfectly valid name for the local, and the IDE is being overzealous here.
The PEP you are thinking of is PEP 8 – Style Guide for Python Code, which in the Naming Styles section states:
_single_leading_underscore: weak "internal use" indicator. E.g. from M import * does not import objects whose name starts with an underscore.
single_trailing_underscore_: used by convention to avoid conflicts with Python keyword, e.g.
Tkinter.Toplevel(master, class_='ClassName')
I searched around but I did not found the answer to this question.
Is there any aesthetic, pythonic way to create temporary variables in an arbitrary scope so I am sure they won't ever appear outside this scope ?
For exemple in OCaml, I can write :
let x = 3 and y = 5 in z = x + y
I'd love to do some stuff like this in Python. For exemple, let's imagine I create a module talker.py with a lot of useful functions, and each of them uses a variable that I don't want to copy again and again.
# talker.py
sentance = ( 'Has provinciae has dictione emergunt sorte aptae'
'iuris navigerum nusquam iuris in natura sorte aptae visitur'
'in verum dictione flumen.' )
def talk() :
print sentance
def talk_loudly() :
print sentance.upper()
def any_other_useful_function :
# do stuff using sentance
Then I want to use all of these wonderful functions in another file, but :
I want to import all these functions at once (without naming each of them)
I don't want to have access to sentance : this variable has done it's job, now I want it to fall in darkness and to be forgotten.
But if I call this module with import talk or from talk import *, this second condition won't be respected.
This is an example of where I could use a "special scope for temporary variables", even if it is not the only situation in which I wish I had this at range.
I thought about using the with ... : statement but I wasn't satisfied with the result.
Any idea is welcomed, but I am looking for the most aesthetic and least wordy manner to proceed.
You can define the __all__ name on your module, to control the names which will be available when you are using from talk import *.
Documented here.
Firstly, sentance isn't temporary. Those functions are relying on its existence. You could del sentance and it would be gone but the functions would throw an error.
A common Python convention is to prefix the name with an underscore. This signals that the variable is 'protected'. It doesn't do anything, but programmers know that they probably shouldn't access it from outside the module (or class).
This question already has answers here:
Why does python use two underscores for certain things? [duplicate]
(7 answers)
Closed 9 years ago.
Hello there i am very new to python (and i am from java background ) my question is does the predefined functions such as length, size or init (constructor of a class) starts with
__function__()
is this standard python syntax that every predefined should starts with this symbol ? ( for example touple.__len__() )? if not then whats the purpose of use this symbol
i am asking this quesion because this symbol confuse me a lot in python.
In python, method names which start and end in a double underscore have special meaning and are, by convention, reserved (there is nothing preventing you from creating your own "special" names, but you should rather not). For example, they are used to implement operator overloading.
You can find a nice introduction in A Guide to Python's Magic Methods. You can also see the Data model - Special method names section of the language reference.
You should also note that methods with an initial double underscore and no final double underscore are treated specially by the interpreter (they are name-mangled so that you get a "private-ish" method). This has nothing to do with methods starting and ending with a double underscore.
Something that begins and ends with a double underscore or often called a dunder, is a magic function or method. Usually, they make objects work with operators or base functions (like len or statements like del, so basically functions or statements of importance).
So, for example:
__add__ is for the + operator
__mul__ is for * operator.
So, if you have two objects of the same type say Foo, then Foo('a') + Foo('b'), would call Foo('a').__add__(Foo('b')).
They behave as reserved words and have some protection. So, for example:
class Foo(object):
def __init__(self):
self.x = 10
def __len__(self):
return "Cheese"
if __name__ == '__main__':
a = Foo()
print a.__len__()
would print out Cheese to the console. However, this would give you a TypeError:
if __name__ == '__main__':
a = Foo()
print len(a)
the __ and _ symbol is for "private" functions
usually you do not use function name with _. functions that start with _ are usually python's and are being used so dont override them or something if you dont know what you are doing
if you want to make a function "private" you use _. e.g : def _myFunc()
I need help with parameteres. Do both of these function definitions do the exact same thing for print_twice?
def print_twice(lol):
print lol
print lol
def print_twice(michael):
print michael
print michael
If yes, then I'm guessing the word used for the parameter doesn't matter, correct?
The word we use for the parameter does matter. It is important that the word you use:
is meaningful and clearly explains what the argument is for,
does not override some variable name from the external scope.
Importance of meaningful arguments' names
The name you use for argument is important, because the names of the arguments, their default values and the function name are the things developers using your function first see, even without the need to look into function documentation (eg. by using help(your_function)). Just use IDLE to define your function and then try to use it - when writing it, the IDLE will show you possible arguments.
So please, give them meaningful names that will make using your function easier and will not require looking into the documentation.
Overriding variables from outer scopes
When it comes to the second point, just look at this example:
def show_elements(elements):
"""Shows elements of the passed argument
"""
for element in elements:
print element
which works ok, but if you replace elements with eg. list, you will override list within this specific scope:
def show_elements(list):
"""Shows elements of the passed argument
"""
for element in list:
print element
and then if you would like to use list eg. for building a list, or converting from other type into list, then you will have problems. list is a builtin and you should not override it. Similar is true also about the other variables from the scopes surrounding the function.
Historically, when Python was resolving variable names by first looking into local scope, then global and builtin scopes, skipping all nonlocal ones (eg. scope from the function in which our function was defined), enclosing scope's variables were passed that way:
def funca():
local_val1 = 'some value1'
local_val2 = 'some value2'
def funcb(local_val1=local_val1):
# local_val1 is accessible here, even though local_val2 is not
...
...
But since the above is no longer true, you will need to take surrounding scopes into account, thus using non-conflicting name is important.
Yes they do. The name of a parameter is irrelevant, although good programming practices mandate that the name should be clear and self-explanatory
That's correct, the name of the parameter doesn't matter.
yes that is correct its just a variable name ...
That is correct. The word used for the parameter in the function definition is only a name, and does not refer to anything external.
Programming is supposed to be logical. So, let's think logically. If you write "a" on a paper twice, you get "a" two times. Same with "b." So you're doing the same function with letters. But what if you reassigned a value to a, each time a went through the function. I mean, what if a was a number, then IMO the closest you could get is something like this:
def func1(a, b):
a = input("Enter a number: ")
b = input("Enter another number: ")
b *= b
a *= a
print func1(a)
print func1(a)
print func1(b)
print func1(b)
Now, when I try to compile this specific code online, I get an error but I think something like this will work for the sake of trying to do what you're doing if done correctly? It's a good experiment, and I would imagine some usage in it.
I wrote a class that lets me pass in a list of variable types, variable names, prompts, and default values. The class creates a wxPython panel, which is displayed in a frame that lets the user set the input values before pressing the calculate button and getting the results back as a plot. I add all of the variables to the class using exec statements. This keeps all of the variables together in one class, and I can refer to them by name.
light = Variables( frame , [ ['f','wavelength','Wavelength (nm)',632.8] ,\
['f','n','Index of Refraction',1.0],])
Inside the class I create and set the variables with statments like:
for variable in self.variable_list:
var_type,var_text_ctrl,var_name = variable
if var_type == 'f' :
exec( 'self.' + var_name + ' = ' + var_text_ctrl.GetValue() )
When I need to use the variables, I can just refer to them by name:
wl = light.wavelength
n = light.n
Then I read on SO that there is rarely a need to use exec in Python. Is there a problem with this approach? Is there a better way to create a class that holds variables that should be grouped together, that you want to be able to edit, and also has the code and wxPython calls for displaying, editing, (and also saving all the variables to a file or reading them back again)?
Curt
You can use the setattr function, which takes three arguments: the object, the name of the attribute, and it's value. For example,
setattr(self, 'wavelength', wavelength_val)
is equivalent to:
self.wavelength = wavelength_val
So you could do something like this:
for variable in self.variable_list:
var_type,var_text_ctrl,var_name = variable
if var_type == 'f' :
setattr(self, var_name, var_text_ctrl.GetValue())
I agree with mipadi's answer, but wanted to add one more answer, since the Original Post asked if there's a problem using exec. I'd like to address that.
Think like a criminal.
If your malicious adversary knew you had code that read:
exec( 'self.' + var_name + ' = ' + var_text_ctrl.GetValue() )
then he or she may try to inject values for var_name and var_text_ctrl that hacks your code.
Imagine if a malicious user could get var_name to be this value:
var_name = """
a = 1 # some bogus assignment to complete "self." statement
import os # malicious code starts here
os.rmdir('/bin') # do some evil
# end it with another var_name
# ("a" alone, on the next line)
a
"""
All of the sudden, the malicious adversary was able to get YOU to exec[ute] code to delete your /bin directory (or whatever evil they want). Now your exec statement roughly reads the equivalent of:
exec ("self.a=1 \n import os \n os.rmdir('/bin') \n\n "
"a" + ' = ' + var_text_ctrl.GetValue() )
Not good!!!
As you can imagine, it's possible to construct all sorts of malicious code injections when exec is used. This puts the burden onto the developer to think of any way that the code can be hacked - and adds unnecessary risk, when a risk-free alternative is available.
For the security conscious, there might be an acceptable alternative. There used to be a module call rexec that allowed "restricted" execution of arbitrary python code. This module was removed from recent python versions. http://pypi.python.org/pypi/RestrictedPython is another implementation by the Zope people that creates a "restricted" environment for arbitrary python code.
The module was removed because it had security issues. Very difficult to provide an environment where any code can be executed in a restricted environment, with all the introspection that Python has.
A better bet is to avoid eval and exec.
A really off-the-wall idea is to use Google App Engine, and let them worry about malicious code.