mirror of
https://github.com/jlengrand/Programming_Tips.git
synced 2026-03-10 00:31:21 +00:00
1.4 KiB
1.4 KiB
PYTHON
Get variable name as a string:
blah = 1
blah_name = [ k for k , v in locals (). iteritems () if v is blah ][ 0 ]
WARNING : Not very clever however, as variables in Python may have more that one name ! Use with caution!
Replace a part of a table [Numpy]:
small_BW=where((L[temp[ptr-1]]==ptr),1,0)
a=BW[posi_0:posi_1,posi_2:posi_3]
a[:,:]=small_BW.copy()
Reshape table without size information:
Put -1 as second argument, the info will automatically be processed.
array.reshape(3,-1)
Concatenate 2 tables [Numpy]:
c=numpy.concatenate((a,b),axis=0)
Cast data in table to int:
Ndarray.astype(int)
Write in the end of a file :
Use add mode :
file.open(file_name, 'a')
Test if variable exists:
try:
print variable
except NameError:
print "Error!"
Check if variable is one of several choices:
if a in [b, c,d]:
blabla
Multi instanciation in Python:
a = b = c = 1+1
WARNING: Those three variables are the same object!
Print function name :
import sys
def tutut():
"""
Dum function displaying its name!
"""
print sys._getframe().f_code.co_name
if __name__ == '__main__':
tutut()
And here is the result
$ python tutut.py
tutut