def contains(dict, key):
return key in dict
dict={}
dict['a']=1
dict['b']=2
dict['c']=3
print(contains(dict, 'a')) # True
print(contains(dict, 'd')) # False
Search This Blog
Python: test if a dictionary contains a specific key
Python: delete a item from a list
- remove the first matching element:
if e in list:
list.remove(e) - remove all occurrences:
while e in list:
list.remove(e) - EAFP style: remove the first occurrence:
try:
list.remove(e)
except ValueError:
# not in the list
pass - EAFP style: remove all occurrences:
while True:
try:
list.remove(e)
except ValueError:
break
EAFP
Easier to ask for forgiveness than permission. This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements. The technique contrasts with the LBYL style common to many other languages such as C.
Python: del or assign None?
>>> x=1
>>> print x
1
>>> del x
>>> print x
Traceback (most recent call last):
File "", line 1, in
NameError: name 'x' is not defined
>>> x=1
>>> print x
1
>>> x=None
>>> print x
None
see also
Compile and install GNU-COBOL (formerly Open COBOL) on Mac OS X Yosemite
- Make sure you have xcode installed.
- Compile and install GNU MP Bignum Library.
- Compile and install Berkeley DB.
- Download GNU COBOL
- Compile and install:
./configure CFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib --prefix=/usr/local make sudo make install
Compile and install Berkeley DB on Mac OS X Yosemite
- Make sure you have xcode installed.
- Download Berkeley DB from Oracle.
- Compile and install:
unzip db-6.1.19.zip cd db-6.1.19/build_unix ../dist/configure --disable-replication --prefix=/usr/local make sudo make install
- To check where the files are located, run
find /usr/local | grep db
you should see something like below:/usr/local/lib/libdb-6.1.a /usr/local/lib/libdb-6.1.dylib /usr/local/lib/libdb-6.1.la /usr/local/lib/libdb-6.dylib /usr/local/lib/libdb.a /usr/local/lib/libdb.dylib /usr/local/include/db.h /usr/local/include/db_cxx.h
Compile and install GNU MP Bignum Library on Mac OS X Yosemite
- Make sure you have xcode installed.
- Excute the following commands to download, compile and install gmp 6.0.0 into /usr/local.
cd /tmp; wget https://gmplib.org/download/gmp/gmp-6.0.0a.tar.bz2; tar jxvf gmp-6.0.0a.tar.bz2; cd gmp-6.0.0 ./configure --prefix=/usr/local make make check sudo make install
- To check where the files are installed, run
find /usr/local | grep gmp
and you should see something like below:/usr/local/include/gmp.h /usr/local/include/gmpxx.h /usr/local/lib/libgmp.10.dylib /usr/local/lib/libgmp.a /usr/local/lib/libgmp.dylib /usr/local/lib/libgmp.la /usr/local/lib/libgmpxx.4.dylib /usr/local/lib/libgmpxx.a /usr/local/lib/libgmpxx.dylib /usr/local/lib/libgmpxx.la /usr/local/share/info/gmp.info /usr/local/share/info/gmp.info-1 /usr/local/share/info/gmp.info-2
see also
synchronized method in python
Java code
class AClass {
private int count =0;
public synchronized inc(){
count++;
}
public synchronized dec(){
count--;
}
}
Python code
import threading
class AClass(object):
def __init__(self):
self.__count = 0;
self.__lock = threading.RLock()
def inc(self):
with self.__lock:
self.__count += 1
def dec(self):
with self.__lock:
self.__count -= 1
see also
synchronized static method in python
Java code
class AClass {
private static int _count = 0;
public static synchronized inc(){
_count ++;
}
public static synchronized dec(){
_count --;
}
}
Python code
import threading
class AClass(object):
__count = 0
__lock = threading.RLock()
@classmethod
def inc(cls):
with cls.__lock:
cls.__count += 1
@classmethod
def dec(cls):
with cls.__lock:
cls.__count -= 1
Use xargs to process files in parallel
find ./ -type f -iname '*.nii' | xargs -n 1 --max-procs 4 gzip
Generate index.html using tree command
- To install:
sudo apt-get install tree
- To use:
tree -H <base> -T <title> -o <output.html>
for example:tree -H /maven -T "Mediaflux Maven Repository" -o index.html
X11 forwarding over ssh
ssh -XYC user@server xtermThe options are:
- X: enables x11 forwarding
- Y: enables trusted x11 forwarding
- C: compresses the communication
- xterm: can be substituted with other commands that are available on the server
X11 forwarding over multiple ssh hops
Suppose you do not have direct ssh access to server2. You can access server2 via server1 over ssh. (localhost => server1 => server2)- In terminal 1:
ssh server1 -L2200:server2:22
- In terminal 2:
ssh localhost -XYC -p2200 xterm
It will run xterm on server2 and displays back to localhost.
Subscribe to:
Posts (Atom)