TestDriven.io Profile Banner
TestDriven.io Profile
TestDriven.io

@testdrivenio

35,310
Followers
163
Following
1,726
Media
23,510
Statuses

Teaching practical programming through real-world applications. Tweets by @MikeHerman and @JanGiacomelli .

Denver, CO
Joined December 2017
Don't wanna be here? Send us removal request.
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Use how and when to use decorators ๐Ÿ‘‡
Tweet media one
12
127
854
@testdrivenio
TestDriven.io
3 years
Python clean code tip: Use enums to group related constants ๐Ÿ‘‡
Tweet media one
6
129
833
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Check the quality of your code inside your CI pipeline Use: - black - code formatting - isort - optimize imports - bandit - check for security vulnerabilities - safety - check for security vulnerabilities of dependencies Github Actions Example ๐Ÿ‘‡
Tweet media one
8
185
818
@testdrivenio
TestDriven.io
2 years
Python Clean Code Tip: Avoid using too many attributes on a single object. Try to cluster them to improve cohesion, reduce coupling, and improve readability ๐Ÿ‘‡
Tweet media one
7
119
716
@testdrivenio
TestDriven.io
3 years
Python tip: To generate a random password you should use the secrets module For example, you can generate a random password of length 21 containing letters, number, and special characters๐Ÿ‘‡
Tweet media one
14
91
657
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Use dataclasses when only storing attributes inside your class instances to reduce the amount of boilerplate code ๐Ÿ‘‡
Tweet media one
8
108
666
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Don't use flags in functions Flags are variables passed to functions, which the function uses to determine its behavior. They are considered bad because functions should only perform one task. -> split your function into smaller functions.
Tweet media one
11
87
504
@testdrivenio
TestDriven.io
2 years
Python Clean Code Tip: Check the quality of your code inside your CI pipeline. - flake8 - style guide enforcer - black - code formatting - isort - optimize imports - bandit - check for security vulnerabilities - safety - check for security vulnerabilities of dependencies ๐Ÿ‘‡
Tweet media one
7
101
499
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Use a list comprehension to filter lists instead of for loop ๐Ÿ‘‡
Tweet media one
9
75
487
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Avoid using too many attributes on a single object. Try to cluster them to improve cohesion, reduce coupling, and improve readability ๐Ÿ‘‡
Tweet media one
9
84
476
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Keep your arguments at a minimum Ideally, your functions should only have one to two arguments. If you need to provide more arguments to the function, you can create a config object which you pass to the function or split it into multiple functions.
Tweet media one
19
86
468
@testdrivenio
TestDriven.io
2 years
Python clean code tip: Use a dictionary to remove duplicates from a list and maintain order ๐Ÿ‘‡
Tweet media one
23
63
462
@testdrivenio
TestDriven.io
3 years
Python clean code tip: Use dependency injection to simplify testing ๐Ÿ‘‡
Tweet media one
3
66
425
@testdrivenio
TestDriven.io
2 years
Python Clean Code Tip: Avoid setting attributes of your objects outside of the constructor. Instead, implement methods that map to real-world concepts. Why? To ensure attributes exist and are easily discoverable. ๐Ÿ‘‡
Tweet media one
2
65
417
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Avoid setting attributes of your objects outside of the constructor. Rather implement methods that map to real-world concepts ๐Ÿ‘‡
Tweet media one
7
72
415
@testdrivenio
TestDriven.io
2 years
Python tip: You can merge dictionaries into a third one in one line by simply using **. Since Python 3.9, you can also use | operator.
Tweet media one
4
56
411
@testdrivenio
TestDriven.io
3 years
Python clean code tip: Avoid doing things only inside a single environment - e.g. production ๐Ÿ‘‡
Tweet media one
3
67
402
@testdrivenio
TestDriven.io
3 years
Python clean code tip: Use protocol to define interface required by your function/method instead of using real objects This way your function/method defines what it needs ๐Ÿ‘‡
Tweet media one
8
60
395
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Use dictionary to implement strategy pattern instead of long and complex if statements Hint: Dict keys can be tuples so you can use multiple parameters to select a function/method ๐Ÿ‘‡
Tweet media one
9
72
406
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Use max to find an element with maximal value inside iterable. You can provide custom function as a key argument to find an element with maximal value for the provided function ๐Ÿ‘‡
Tweet media one
6
64
379
@testdrivenio
TestDriven.io
2 years
Python clean code tip: Keep your API endpoints simple and fast - move all the unnecessary logic to background tasks Why? - prevent API timeouts - enable retries - simplify testing (no need for the API to run) ๐Ÿ‘‡
Tweet media one
8
78
370
@testdrivenio
TestDriven.io
2 years
Python clean code tip: Use enums to group related constants. Why? Autocomplete Static type checking ๐Ÿ‘‡
Tweet media one
4
73
356
@testdrivenio
TestDriven.io
2 years
Python clean code tip: Donโ€™t use random to generate random numbers when you need them to be cryptographically strong Use secrets instead ๐Ÿ‘‡
Tweet media one
7
65
354
@testdrivenio
TestDriven.io
2 years
Python clean code tip: Use contract testing when you want to verify the same behavior for different implementations. ๐Ÿ‘‡
Tweet media one
1
58
353
@testdrivenio
TestDriven.io
2 years
Python clean code tip: Use HTTPStatus for status codes instead of magic numbers ๐Ÿ‘‡
Tweet media one
11
62
351
@testdrivenio
TestDriven.io
2 years
Python Clean Code Tip: Use dataclasses when only storing attributes inside your class instances to reduce the amount of boilerplate code. ๐Ÿ‘‡
Tweet media one
3
66
342
@testdrivenio
TestDriven.io
2 years
Python Clean Code Tip: Avoid empty except blocks -> try-except-pass. They lead to hard-to-find bugs. ๐Ÿ‘‡
Tweet media one
4
52
338
@testdrivenio
TestDriven.io
2 years
Python clean code tip: Don't separate parts of your code with comments Introduce new methods instead ๐Ÿ‘‡
Tweet media one
5
80
345
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Specify the most general type for inputs and the most specific type for outputs. ๐Ÿ‘‡
Tweet media one
8
56
343
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Avoid using floats when you need precise results. Use Decimal instead. e.g. prices ๐Ÿ‘‡
Tweet media one
4
57
345
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Use any to check if at least one element in iterable meets the criteria instead of for loop ๐Ÿ‘‡
Tweet media one
6
45
340
@testdrivenio
TestDriven.io
2 years
Python tip (>=3.9): You can use .removeprefix() to remove the prefix from a string e.g. remove filename prefix ๐Ÿ‘‡
Tweet media one
7
46
339
@testdrivenio
TestDriven.io
2 years
Python Clean Code Tip: Avoid storing things like secret keys, passwords, connection strings, and API keys inside your code. Instead, use a secrets management solution like AWS Secrets Manager or Vault ๐Ÿ‘‡
Tweet media one
5
71
329
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Use singledispatch/singledispatchmethod to register different handlers based on input data type instead of IF statements with isinstance checks Hint: Useful for use cases such as reacting to different event types ๐Ÿ‘‡
Tweet media one
10
71
337
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Avoid long chains of collaborations to avoid high coupling and breaking encapsulation ๐Ÿ‘‡
Tweet media one
9
57
335
@testdrivenio
TestDriven.io
2 years
Python clean code tip: Use Protocol to define the interface required by your function/method instead of using real objects. This way, your function/method defines what it needs. ๐Ÿ‘‡
Tweet media one
1
43
325
@testdrivenio
TestDriven.io
2 years
Python Clean Code Tip: Avoid using the variable/parameter type inside your variable/parameter name. Use type hints instead. ๐Ÿ‘‡
Tweet media one
4
64
327
@testdrivenio
TestDriven.io
3 years
10 Free FastAPI resources to take your skill to the next level ๐Ÿงต ๐Ÿ‘‡
2
83
333
@testdrivenio
TestDriven.io
2 years
Python Clean Code Tip: Use preconditions to ensure the integrity of your objects. ๐Ÿ‘‡
Tweet media one
13
61
321
@testdrivenio
TestDriven.io
2 years
Python clean code tip: Don't return a tuple of two objects of the same type from your methods Rather introduce new data structure to return more complex data Why? To prevent misusage because of switched position - access values by name ๐Ÿ‘‡
Tweet media one
8
47
324
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Use HTTPStatus from http (it's inside the standard library) to avoid "magic" numbers for statuses inside your code ๐Ÿ‘‡
Tweet media one
3
55
321
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Use the defaultdict to automatically handle cases where the key does not exist yet instead of using IF statement ๐Ÿ‘‡
Tweet media one
3
67
317
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Use automatic code formatter such as black to format your code ๐Ÿ‘‡
Tweet media one
6
47
313
@testdrivenio
TestDriven.io
2 years
Python Clean Code Tip: When your module becomes too big you can restructure it to a package while keeping all the imports from the module as they were. ๐Ÿ‘‡
Tweet media one
8
53
311
@testdrivenio
TestDriven.io
2 years
Python tip: You can use dictionary to implement switch-like behaviour ๐Ÿ‘‡
Tweet media one
6
47
307
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Don't add redundant context Do not add unnecessary data to variable names, especially if you're working with classes.
Tweet media one
6
40
306
@testdrivenio
TestDriven.io
1 year
Python clean code tip: Use enums to group related constants. Why? - Autocomplete - Static type checking ๐Ÿ‘‡
Tweet media one
3
48
299
@testdrivenio
TestDriven.io
2 years
Python clean code tip: Use a dictionary to easily switch between different actions instead of IF Very useful when dealing with enum-like values Why? - smaller specialized methods for processing - one simple path - no branching - simple to extend ๐Ÿ‘‡
Tweet media one
3
55
306
@testdrivenio
TestDriven.io
2 years
FastAPI: You can protect API endpoints with API key ๐Ÿ‘‡
Tweet media one
3
49
306
@testdrivenio
TestDriven.io
19 days
Python clean code tip: Use enums to group related constants. Example ๐Ÿ‘‡
Tweet media one
3
37
308
@testdrivenio
TestDriven.io
2 years
Python clean code tip: Use .exception() instead of .error() to get traceback logged together with the exception ๐Ÿ‘‡
Tweet media one
4
47
301
@testdrivenio
TestDriven.io
1 year
Python clean code tip: Use dependency injection to simplify testing - avoid the need for patching Useful when: - replace DB implementation of store with in-memory one for tests (faster tests) - replace 3rd party integrations with in-memory ones (faster/more reliable tests) ๐Ÿ‘‡
Tweet media one
6
49
298
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: When your module becomes too big you can restructure it to package while keeping all the imports from the module as they were ๐Ÿ‘‡
Tweet media one
5
40
304
@testdrivenio
TestDriven.io
2 years
Python clean code tip: Avoid checking if the key already exists in a dictionary before using it by using defaultdict ๐Ÿ‘‡
Tweet media one
4
50
297
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Use all to check whether all elements in an iterable meet the criteria ๐Ÿ‘‡
Tweet media one
3
49
299
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Use NullObjects instead of None Hint: None/null doesn't exist in the real world ๐Ÿ‘‡
Tweet media one
11
55
296
@testdrivenio
TestDriven.io
2 years
Python Clean Code Tip: Use min to find an element with minimal value inside an iterable. You can provide a custom function as a key argument to serve as a key for the min comparison ๐Ÿ‘‡
Tweet media one
5
50
296
@testdrivenio
TestDriven.io
5 months
Python Clean Code Tip: Use retries when calling 3rd party API They are not responsive/down/unavailable much more frequently than anticipated ๐Ÿ‘‡
Tweet media one
7
62
298
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Avoid double negatives - like not is not valid ๐Ÿ‘‡
Tweet media one
3
48
291
@testdrivenio
TestDriven.io
2 years
Python clean code tip: Use singledispatch instead of is instance ๐Ÿ‘‡
Tweet media one
3
51
295
@testdrivenio
TestDriven.io
1 year
Python Tip: Check the quality of your code inside your CI pipeline. - flake8 - style guide enforcer - black - code formatting - isort - optimize imports - bandit - check for security vulnerabilities - safety - check for security vulnerabilities of dependencies ๐Ÿ‘‡
Tweet media one
6
59
295
@testdrivenio
TestDriven.io
2 years
Python clean code tip: Avoid doing things in only one environment (e.g., production because of rate limits of external API) Rather use interface and dummy implementation together with mapping ๐Ÿ‘‡
Tweet media one
8
65
292
@testdrivenio
TestDriven.io
1 year
Python Clean Code Tip: Avoid using too many attributes on a single object. Try to cluster them to improve cohesion, reduce coupling, and improve readability ๐Ÿ‘‡
Tweet media one
3
62
292
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Favor real objects over primitive types such as dictionaries ๐Ÿ‘‡
Tweet media one
11
40
281
@testdrivenio
TestDriven.io
2 years
Python clean code tip: Always use paginated queries Use "last evaluated record" approach to paginate instead of offset This way you limit database load per single query ๐Ÿ‘‡
Tweet media one
3
44
282
@testdrivenio
TestDriven.io
3 years
Python tip: Use bandit to check your code for known security vulnerabilities such as eval() ๐Ÿ‘‡
Tweet media one
3
56
283
@testdrivenio
TestDriven.io
1 year
Python clean code tip: Use singledispatch instead of isinstance ๐Ÿ‘‡
Tweet media one
13
49
276
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Use secrets module to generate cryptographically strong random strings such as one-time passwords and access tokens ๐Ÿ‘‡
Tweet media one
5
50
281
@testdrivenio
TestDriven.io
3 years
Python tip: Implement method on objects - they should tell you what you need to know. Don't leak logic to other places. ๐Ÿ‘‡
Tweet media one
4
49
276
@testdrivenio
TestDriven.io
1 year
Docker Best Practices for Python Developers Looks at several best practices to make your Dockerfiles and images cleaner, leaner, and more secure. by @amal_ytics #Docker #Python @Docker
6
72
281
@testdrivenio
TestDriven.io
1 year
Python Clean Code Tip: Avoid setting attributes of your objects outside of the constructor. Instead, implement methods that map to real-world concepts. Why? To ensure attributes exist and are easily discoverable. Also, to encapsulate knowledge in a single place. ๐Ÿ‘‡
Tweet media one
2
55
278
@testdrivenio
TestDriven.io
2 years
Python Clean Code Tip: Use sum to sum the values of all elements inside an iterable instead of a for loop. Why? Don't re-invent the wheel! sum is much faster ๐Ÿ‘‡
Tweet media one
4
59
272
@testdrivenio
TestDriven.io
11 months
FastAPI tip: You can easily add WebSockets to your app with @app .websocket(). ๐Ÿ‘‡
Tweet media one
5
59
273
@testdrivenio
TestDriven.io
2 years
Python clean code tip: Prefer null/empty/dummy object over null Why? - avoid IF pollution - null doesn't exist in the real world ๐Ÿ‘‡
Tweet media one
5
57
266
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Avoid using variable/parameter type inside variable/parameter name Use type hints instead ๐Ÿ‘‡
Tweet media one
6
41
275
@testdrivenio
TestDriven.io
2 years
Python clean code tip: Define abstract class attributes for your mixins ๐Ÿ‘‡
Tweet media one
4
47
271
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Use docstrings to document usage of your modules/classes/functions ๐Ÿ‘‡
Tweet media one
7
40
268
@testdrivenio
TestDriven.io
2 years
FastAPI tip: You can use strawberry to build GraphQL API with FastAPI ๐Ÿ‘‡
Tweet media one
3
42
270
@testdrivenio
TestDriven.io
11 months
Docker best practice: Use multistage builds to reduce the size of the production image. ๐Ÿ‘‡
Tweet media one
1
60
267
@testdrivenio
TestDriven.io
2 years
Python clean code tip: Don't allow CORS for all domains - be explicit ๐Ÿ‘‡
Tweet media one
2
46
266
@testdrivenio
TestDriven.io
2 years
Python Clean Code Tip: Use operator overloading to enable the usage of operators such as +, -, /, *, ... on your instances. ๐Ÿ‘‡
Tweet media one
1
43
265
@testdrivenio
TestDriven.io
3 years
Did you know? You can enforce user to use only keyword arguments when calling your function An example๐Ÿ‘‡
Tweet media one
4
49
265
@testdrivenio
TestDriven.io
3 years
Django tip: Use Flower to monitor your Celery ๐Ÿ‘‡
Tweet media one
3
51
257
@testdrivenio
TestDriven.io
21 days
Python clean test tip: The tests we write should cover: - all happy paths - edge/corner/boundary cases - negative test cases - security and illegal issues ๐Ÿ‘‡
Tweet media one
2
45
259
@testdrivenio
TestDriven.io
1 year
Python clean code tip: Use contract testing when you want to verify the same behavior for different implementations Useful when: - You want to improve the current implementation and switch between old/new using a feature flag - You want an in-memory test double ๐Ÿ‘‡
Tweet media one
2
53
248
@testdrivenio
TestDriven.io
5 months
Python Clean Code Tip: Use dependency injection - inject objects that interact with the outer world (e.g., DB, 3rd-party APIs, ...) Pros: - easier & faster testing of business logic -> easily replace dependencies with mocks/stubs/in-memory implementations ๐Ÿ‘‡
Tweet media one
1
56
251
@testdrivenio
TestDriven.io
2 years
Python clean test tip: A test should not depend on the state of any other tests or external services. ๐Ÿ‘‡
Tweet media one
0
48
247
@testdrivenio
TestDriven.io
1 year
Docker best practice: Limit CPU and memory for your containers to prevent crippling the rest of the containers on the machine. ๐Ÿ‘‡
Tweet media one
0
59
250
@testdrivenio
TestDriven.io
3 years
Python Clean Code Tip: Learn how and when to use context managers ๐Ÿ‘‡
Tweet media one
2
34
248
@testdrivenio
TestDriven.io
1 year
Python Clean Code Tip: Avoid storing things like secret keys, passwords, connection strings, and API keys inside your code. Instead, use a secrets management solution like AWS Secrets Manager or Vault. This prevents secrets leakage via version control ๐Ÿ‘‡
Tweet media one
7
61
246
@testdrivenio
TestDriven.io
2 years
Python clean code tip: Use enumerate when you need to iterate over iterable and have an index at the same time ๐Ÿ‘‡
Tweet media one
2
29
248
@testdrivenio
TestDriven.io
2 years
Python tip: Did you know you can generate a complete HTML page with a yearly calendar using Python?
Tweet media one
Tweet media two
6
61
245
@testdrivenio
TestDriven.io
3 years
Python clean code tip: Use contract test when you want to verify the same behavior for different implementations ๐Ÿ‘‡
Tweet media one
1
36
236
@testdrivenio
TestDriven.io
2 years
Python Clean Code Tip: Use __post_init__ hook when working with dataclasses to apply preconditions to ensure the integrity of your objects. ๐Ÿ‘‡
Tweet media one
8
45
231
@testdrivenio
TestDriven.io
3 years
Can't decide between Flask and Django? ๐ŸงตThis one is for you๐Ÿ‘‡
13
58
235
@testdrivenio
TestDriven.io
2 years
Python tip: You can use a dictionary to remove duplicates from a list while preserving the order of elements Dictionary preserve insertion order ๐Ÿ‘‡
Tweet media one
6
49
232
@testdrivenio
TestDriven.io
1 year
Parallelism, Concurrency, and AsyncIO in Python - by example Looks at how to speed up CPU-bound and IO-bound operations with multiprocessing, threading, and AsyncIO and when you should use each. by @amal_ytics #Python
1
54
236
@testdrivenio
TestDriven.io
2 years
Python clean code tip: Avoid naive datetime objects - use timezone-aware ones instead Use them always in UTC and only present them to users in their local time zone ๐Ÿ‘‡
Tweet media one
4
46
233
@testdrivenio
TestDriven.io
3 years
Python tip: You can split a string on all non-word characters by using re.split non-word characters - characters that are not letters, numbers or _ An example๐Ÿ‘‡
Tweet media one
3
42
235
@testdrivenio
TestDriven.io
2 years
BREAKING: Django will be used inside navigation system in the next Mars Rover.
7
35
229