Question
#
How do you estimate the size of a project roughly?
Answer
#
When I am asked to provide a rough estimate of the required effort on a project with a lot of requirements or user stories, I first want to make sure that my estimate is in the right order of magnitude. That means that I want to estimate a project that takes 1-10 weeks to be in that range, but not estimate less than a week or more than 10 weeks. Similarly, a project that takes one or more years should not estimated as a job of a few weeks.
My orders of magnitude are as follows:
- 1 day
- 1 week (5 days)
- 1 month (20 days)
- 1 quarter (60 days)
- 1 half-year (120/125 days)
- 1 year (250 days)
As such, when estimating a task, I will say that the task will either take 1 day, 1 week or 1 month. In general, any task that is estimated at 1 month long (or above) needs to be broken down into sub-tasks as it indicates that the task is hiding a lot of complexity.
With this kind of approach, one can estimate that a developer can do approximately 250 small tasks (1 day), 50 medium tasks (5 days) or 13 large tasks (20 days) per year.
Start by creating an estimate of the overall project without thinking about any of the underlying tasks. This is done to have a quick idea of the scale of the project.
Then list and estimate the tasks that will need to be accomplished to complete the project. You might be forgetting a few, but it is fine at that moment. Think mostly of the most important tasks and also the riskiest.
If the sum of the efforts you estimated is in the same order of your original project estimate, then you are done. If not, then you need to investigate and explain what led you to either over or under estimate. Did you forget to estimate some tasks? Did you ignore some tasks when you did your initial estimate? What were the assumptions you made that were right/wrong? Once you are satisfied with your explanation, you are done with estimating.
Problem
#
I use VS Code and I’d like to insert templates into my documents. How do I do this?
Solution
#
In VS Code, the concept of templates is called
snippets. It is fairly easy to
create a snippet, so I won’t go into the details. Here’s an example of a snippet I use to insert the date and time quickly into my documents.
{
"Datetime": {
"scope": "",
"prefix": "dt",
"body": [
"$CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND"
],
"description": "Date time"
}
}
With this snippet, I only have to type dt and then press TAB and dt gets replaced by the current date and time.
I use snippets to create the template of my questions and problems articles. My current approach is to open a non-existent file by calling code /path/to/new/file and VS Code opens the editor with this file. I can then write the content of this file, which will not automatically save until I manually save. Once I’m happy with the content of my article, I manually save, which means that in the future, any edit I make and then have the editor lose focus will be automatically saved and commit to git (my current workflow).
One of the things I don’t particularly like with the current implementation of the snippets system is that the body needs to be a list of strings, where each item represents a new line. It is possible to create a single entry and use \n and \ to format the string, but that is not a clean approach. Those are limitations of jsonc. If it was possible to link to a file, then it would be “easy” to create a clean template.
References
#
Question
#
How can I read more books?
Answer
#
Make it a priority to read books. Replace some of the existing habits you have with reading books instead.
If you like to spend time reading reddit or hackernews or watching videos on youtube, replace that with reading a book. Of course, it’s easier said than done. The best way I’ve been able to transition from those activities to reading books was to progress slowly. Don’t replace reading hackernews one day by reading a book instead. Instead, simply spend 5, 10, 15 minutes reading a book, and then the remainder of the time on reading hackernews. Spend the same amount of time every day reading a book for a week. When a week has passed, increase that amount of time slightly (e.g., go from 5 minutes per day to 10 minutes per day).
As you increase the amount of time you read, you will find it easier and easier to spend time reading instead of doing other activities you might consider harmful or less valuable.
Question
#
How can you tell when you’ve reached the top of your job and that you won’t find smarter people?
Answer
#
When people that challenge you bring arguments which you’ve already considered. When the suggestions of others are stale or not intriguing. When you don’t find excitement in your work. When you aren’t challenged anymore. When the problems you are facing are not solved elegantly by others. When the problems you are facing have become so niche that few people on Earth may be able to discuss them or help you solve them.
References
#
Problem
#
My
click CLI is slow, even just to show the help. How do I make it go faster?
Solution
#
In most cases, the reason your click CLI is slow is that you have large imports at the top of the files where you have declared your commands.
The typical pattern is as follows:
cli.py
from train import train
from predict import predict
@click.group()
def cli():
pass
cli.add_command(predict)
cli.add_command(train)
train.py
import click
import pandas as pd
import torch
@click.command()
def train():
pass
predict.py
import click
import pandas as pd
import torch
@click.command()
def predict():
pass
Notice that in both these files we import pandas and torch, which can account for a large chunk of script execution time simply due to importing them. You can verify that by simply running python -X importtime train.py 2>tuna.log and using
tuna (run tuna tuna.log) to inspect the results and convince yourself.
The suggested pattern is to move the imports inside of the function itself, as such:
train.py
import click
@click.command()
def train():
import pandas as pd
import torch
pass
predict.py
import click
@click.command()
def predict():
import pandas as pd
import torch
pass
This will shave off a large amount of time spent importing those packages (pandas and torch). They will only be loaded when you need to run the command itself, not every time you invoke the CLI.
Another pattern which is more complicated is to move the logic of the functions in separate files. This is done to avoid the common mistake that will happen over time that developers will add more logic in those command files, adding imports at the top of the file and slowing the CLI again. By moving the complete implementation to a separate file, you can have the imports at the top of the file and it is not possible to make this mistake again.
train.py
import click
@click.command()
def train():
from train_implementation import train
train()
train_implementation.py
import pandas as pd
import torch
def train():
# Implementation is now here
pass
Problem
#
Some imports in my python code are slow. How can I figure out which ones are the source of slowness?
Solution
#
Python offers a really useful functionality you can use that will list how long each import took. By passing the -X importtime argument to your python command when you execute your script it will print out both the cumulative time (including nested imports) and self time (excluding nested imports) of each import.
python -X importtime your-script.py
Running python -X importtime my-script.py on an empty script returns the following (on Windows 7, Python 3.7.5)
import time: self [us] | cumulative | imported package
import time: 52 | 52 | zipimport
import time: 367 | 367 | _frozen_importlib_external
import time: 55 | 55 | _codecs
import time: 530 | 585 | codecs
import time: 520 | 520 | encodings.aliases
import time: 1107 | 2210 | encodings
import time: 328 | 328 | encodings.utf_8
import time: 39 | 39 | _signal
import time: 357 | 357 | encodings.latin_1
import time: 34 | 34 | _abc
import time: 312 | 345 | abc
import time: 474 | 819 | io
import time: 113 | 113 | _stat
import time: 264 | 377 | stat
import time: 269 | 269 | genericpath
import time: 794 | 1062 | ntpath
import time: 871 | 871 | _collections_abc
import time: 915 | 3223 | os
import time: 490 | 490 | _sitebuiltins
import time: 57 | 57 | _locale
import time: 934 | 991 | _bootlocale
import time: 421 | 421 | encodings.cp1252
import time: 472 | 472 | types
import time: 394 | 394 | warnings
import time: 440 | 834 | importlib
import time: 263 | 263 | importlib.machinery
import time: 554 | 816 | importlib.abc
import time: 64 | 64 | _operator
import time: 792 | 856 | operator
import time: 343 | 343 | keyword
import time: 43 | 43 | _heapq
import time: 405 | 447 | heapq
import time: 85 | 85 | itertools
import time: 328 | 328 | reprlib
import time: 69 | 69 | _collections
import time: 1460 | 3585 | collections
import time: 44 | 44 | _functools
import time: 596 | 640 | functools
import time: 784 | 5008 | contextlib
import time: 651 | 7308 | importlib.util
import time: 1095 | 1095 | pywin32_bootstrap
import time: 231 | 231 | sitecustomize
import time: 10744 | 24972 | site
For a script with a simple import argparse, I get the following output:
import time: self [us] | cumulative | imported package
import time: 70 | 70 | zipimport
import time: 341 | 341 | _frozen_importlib_external
import time: 54 | 54 | _codecs
import time: 457 | 511 | codecs
import time: 456 | 456 | encodings.aliases
import time: 1030 | 1997 | encodings
import time: 215 | 215 | encodings.utf_8
import time: 38 | 38 | _signal
import time: 268 | 268 | encodings.latin_1
import time: 33 | 33 | _abc
import time: 398 | 431 | abc
import time: 311 | 741 | io
import time: 87 | 87 | _stat
import time: 271 | 357 | stat
import time: 196 | 196 | genericpath
import time: 416 | 612 | ntpath
import time: 714 | 714 | _collections_abc
import time: 610 | 2292 | os
import time: 229 | 229 | _sitebuiltins
import time: 48 | 48 | _locale
import time: 246 | 293 | _bootlocale
import time: 217 | 217 | encodings.cp1252
import time: 488 | 488 | types
import time: 279 | 279 | warnings
import time: 461 | 740 | importlib
import time: 269 | 269 | importlib.machinery
import time: 557 | 825 | importlib.abc
import time: 63 | 63 | _operator
import time: 808 | 871 | operator
import time: 336 | 336 | keyword
import time: 41 | 41 | _heapq
import time: 336 | 376 | heapq
import time: 69 | 69 | itertools
import time: 341 | 341 | reprlib
import time: 70 | 70 | _collections
import time: 1136 | 3197 | collections
import time: 69 | 69 | _functools
import time: 642 | 710 | functools
import time: 801 | 4708 | contextlib
import time: 688 | 6959 | importlib.util
import time: 934 | 934 | pywin32_bootstrap
import time: 224 | 224 | sitecustomize
import time: 9323 | 20954 | site
import time: 698 | 698 | enum
import time: 55 | 55 | _sre
import time: 417 | 417 | sre_constants
import time: 372 | 789 | sre_parse
import time: 443 | 1286 | sre_compile
import time: 323 | 323 | copyreg
import time: 716 | 3021 | re
import time: 725 | 725 | locale
import time: 948 | 1673 | gettext
import time: 986 | 5678 | argparse
The package are listed in order that they are resolved. In argparse case, os and sys were already loaded, so it first loads re, then gettext. Once both are loaded, argparse has finished loading.
The way the cumulative column is computed is to take all the prior self that are a level higher than the package you’re looking at. For example (if we take the io package):
import time: 33 | 33 | _abc
import time: 398 | 431 | abc
import time: 311 | 741 | io
311 + 398 + 33 = 742
We can see here that the numbers are not necessarily equal to one another, this might be due to precision used to do the computation while the rendering of numbers is rounded.
Note that the load time of a package may be different depending on which script you load because dependencies of the package may have already been loaded in some cases, while in others it may have to load them.
Looking at text might be your thing, but if you’re more visual, there’s a tool called
tuna which will consume this output and create an icicle plot you can look at to find which imports are the slowest/longest.
References
#
Question
#
How can you tell if you are a low performer?
Answer
#
I always prefer to compare myself against my prior self and not against others. Thus, I would consider myself a low performer if my throughput is lower than what it has been on average in the past. This may happen for many reasons, amongst them it would be because I’m learning something new, so I’m spending a good chunk of my time on learning and less on executing. It might be because I’m trying different ideas to find the best one because I’m working on something I’ve never worked before.
It’s generally easy for a programmer to tell whether he’s been more or less productive than the prior week. It is mostly based on feelings, where you feel good when you are productive and less good when you’re not making any progress or facing issues.
If you think and feel that you are performing poorly, start recording more thoroughly what you are working on. Identify when you start and finish working on a task, and when you get blocked, write down why. After a few weeks, look at what you wrote and assess what might cause you to feel that you are a low performer. Is it because you’re working on a task you are not good at? Is it because of a lack of motivation on the task you’ve been assigned to?
With more information in hand to determine why you feel that you are a low performer, you will be able to devise a plan so that you can once again feel like a high performer.
Problem
#
I have two test files with the same name and pytest complains. How do I make it work without changing the test filenames?
Example directory structure
/path/to/project/tests
├── a/
│ └── test_a.py
└── b/
└── test_a.py
Error message:
import file mismatch:
imported module 'test_a' has this __file__ attribute:
/path/to/project/tests/a/test_a.py
which is not the same as the test file we want to collect:
/path/to/project/tests/b/test_a.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
Solution
#
Add a __init__.py to each directories with tests files that have the same name. Technically, you only need to have a __init__.py file in one of the two directories, so that one is in a package while the other one is in a different one. Adding it in both simply prevents this issue from occurring again if you were to add a third file test_a.py.
/path/to/project/tests
├── a/
│ ├── __init__.py
│ └── test_a.py
└── b/
├── __init__.py
└── test_a.py
Problem
#
I use pytest with coverage and I want to see the files that have no coverage.
Solution
#
It appears that pytest and pytest-cov will not list someof the files that are under namespace packages, while it will work fine for files in regular packages (see
PEP 420 on the topic of implicit namespace packages).
To fix this problem, one solution is to add __init__.py files in all of your directories in order to create regular packages.
If you are using PyCharm Professional, you can simply run your test with coverage. This will allow you to identify all the files that have currently no coverage as they will appear with coverage = 0%.
References
#
Question
#
What should be defined to make a user demo walkthrough successful?
Answer
#
You need to define what you want to learn from the demo walkthrough: where does the user ask questions? where does he stay stuck? what is easy/hard for him to do? what does he think about when he goes through the demo? what is/isn’t working? what frustrates him? where does the user want to have more guidance?
The user doing the walkthrough should be as close as possible to the ideal user otherwise you may get feedback that is biased on their own experience. A user with too much knowledge compared to your target user will be able to do many things your target user may need help with and they may assume a lot of things because they know about them. On the other hand, a user with too little knowledge will require help in many places where the target user is expected to have knowledge, which may make the demo walkthrough slower than desired.
The walkthrough should have a clear scenario. You may only give an initial setup to the user and a desired goal and let them figure everything out by themselves. You may also go with a more directed approach, where you tell them what to do and you see if the instructions are clear enough to accomplish the steps. The first approach is interesting because it allows you to observe variability in how to solve a problem.