Showing posts with label tip. Show all posts
Showing posts with label tip. Show all posts

Friday, November 4, 2016

TIP: recover file from old Git branch

In cleaning up my source code I realize I was too... aggresive in my house cleaning. I deleted a file I needed.  No biggie, just take a look at HEAD or HEAD^... Hey, that didn't work!

TIP: use git rev-list to search and find the most recent commit that affected (deleted) a path. (Source: Charles Bailey on SO)

$ git rev-list -n 1 HEAD -- docker/torta/torta-task.json
0c400d323aff484cfb2bdc18dcdb7813de93a658

Yay!  That was really easy. Now all I need to do is check out from that Git hash and I'm done:

$ git checkout 0c400d323aff484cfb2bdc18dcdb7813de93a658 !$
error: pathspec 'docker/torta/torta-task.json' did not match any file(s) known to git.

Err?  Hmm. Oh: the 0c4 branch contains the "delete" command. If I want to get the file contents, I'll get it from the branch just before that one:

$ git checkout 0c400d323aff484cfb2bdc18dcdb7813de93a658^ docker/torta/torta-task.json
$ ls -l docker/torta/torta-task.json
-rw-r--r--  1 johnm  staff  1445 Nov  4 13:06 docker/torta/torta-task.json

Yay, it worked!  Thanks, interwebs!

Wednesday, October 12, 2016

TIP: more info in Python's IPDB debugger

In ipdb the “dunder exception” variable has more information after your program crashes:

pdb> pp __exception__

(,
 ClientError...)

Friday, September 9, 2016

TIP: fix bizarre Docker problems

docker rm -fv $(docker ps -aq)

Sometimes testing produces strange Docker-related problems.  The above command kills all containers, including the stopped ones.  The "-v" also destroys each container's private storage volumes.  This is great because Docker likes to run differently different times you run it -- because it has local state.  Local state is evil!  Run the above command every now and then so Docker starts from a clean, state-less, state.

In Docker, stopping a container is different from killing it.  The former stops the running container/process, but leaves the private volume.  The latter zaps the volume.

Thanks Sam!

Friday, September 2, 2016

TIP: auto-fix your Python code!

The script autopep8 will automatically fix and reindent your Python code!

The following shows a "diff" of what PEP8 changes should be made, and then we apply the changes:

$ autopep8 -d collect.py
@@ -29,5 +29,5 @@

-if __name__=='__main__':
+if __name__ == '__main__':
     main()

$ autopep8 -i collect.py

Friday, July 29, 2016

TIP: running 2N+1 Gunicorn workers

If you read, people tend to say that for web services, run 2N+1 workers, where N is the number of CPUs.

It turns out the Intarwebs are wrong about exactly how to do this. Here's my solution based on "epicbrew"'s work on Stack Overflow:

gunicorn --workers=$((2 * $(getconf _NPROCESSORS_ONLN) + 1)) wsgi:application
The cool thing is that it works on both macOS and Linux!

Tuesday, July 12, 2016

TIP: edit files with Git merge conflicts

subl $(git diff --name-only --diff-filter=U)

This is even easier if you have a Git alias:

subl $(git unmerged)

In both cases it uses the Sublime text editor to open any file that has a Git merge conflict.

Wednesday, July 6, 2016

TIP: "git unmerged" to list unmerged files with conflicts

in your ~/.gitconfig file:

[alias]
        unmerged = diff --name-only --diff-filter=U

Monday, July 4, 2016

TIP: Git Grep searches your project really fast

Searching inside a large project of source code can be a challenge

In my codebase there's lots of "delete" methods, but only a few "_delete_" methods. If I search for "delete", I'll get zillions of false positives. By using the super-fast Git Grep and also "\W" for matching non-word-characters, I can find all references of the target string "_delete_":

$ git grep '\W_delete_\W'
app/looks_services.py:390:        order._delete_()
app/models.py:2634:    def _delete_(self, **kwargs):

Friday, August 28, 2015

TIP: language-aware Git gives you contextual searches

TIP: if Git is setup correctly, Git knows we're working with Python, so commands are clearer.

setup: echo '*.py filter=python' >> .gitattributes

Example: find the usage/definition of formatted_tax:

$ git grep -p formatted_tax

app/models.py=2083=class Order(CreatedMixin):
app/models.py:2454:            'tax': self.formatted_tax,

The second line says the symbol was found on line 2454.  The first line shows the context, the symbol was found inside the class Order! Very useful

related: 'git grep' and Language-Aware Diffs