Create a file if it doesn’t exist

If you don’t need atomicity you can use os module: UPDATE: As Cory Klein mentioned, on Mac OS for using os.mknod() you need a root permissions, so if you are Mac OS user, you may use open() instead of os.mknod()

List changes unexpectedly after assignment. Why is this and how can I prevent it?

With new_list = my_list, you don’t actually have two lists. The assignment just copies the reference to the list, not the actual list, so both new_list and my_list refer to the same list after the assignment. To actually copy the list, you have various possibilities: You can use the builtin list.copy() method (available since Python 3.3):new_list = old_list.copy() You can slice it:new_list … Read more

How can I see the differences between two branches?

You want to use git diff. Where <commit> is your branch name, the hash of a commit or a shorthand symbolic reference For instance git diff abc123…def567 or git diff HEAD..origin/master That will produce the diff between the tips of the two branches. If you’d prefer to find the diff from their common ancestor to test, you can use three dots instead of two: … Read more

Is it possible to make abstract classes in Python?

Use the abc module to create abstract classes. Use the abstractmethod decorator to declare a method abstract, and declare a class abstract using one of three ways, depending upon your Python version. In Python 3.4 and above, you can inherit from ABC. In earlier versions of Python, you need to specify your class’s metaclass as ABCMeta. Specifying the metaclass has different … Read more

typedef struct vs struct definitions [duplicate]

The common idiom is using both: They are different definitions. To make the discussion clearer I will split the sentence: In the first line you are defining the identifier S within the struct name space (not in the C++ sense). You can use it and define variables or function arguments of the newly defined type by defining … Read more

Git fetch remote branch

Update: Using Git Switch All of the information written below was accurate, but a new command, git switch has been added that simplifies the effort. If daves_branch exists on the remote repository, but not on your local branch, you can simply type: Since you do not have the branch locally, this will automatically make switch look on the remote repo. It … Read more

How to get the CUDA version?

As Jared mentions in a comment, from the command line: (or /usr/local/cuda/bin/nvcc –version) gives the CUDA compiler version (which matches the toolkit version). From application code, you can query the runtime API version with or the driver API version with As Daniel points out, deviceQuery is an SDK sample app that queries the above, along with … Read more