{"id":17133747,"url":"https://github.com/slavikdev/til","last_synced_at":"2026-01-05T21:07:45.776Z","repository":{"id":142155562,"uuid":"240293421","full_name":"slavikdev/til","owner":"slavikdev","description":"Today I Learned 💡","archived":false,"fork":false,"pushed_at":"2021-09-13T21:40:25.000Z","size":12,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-29T11:33:30.262Z","etag":null,"topics":["til","tips","tips-and-tricks","today-i-learned","writing"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/slavikdev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-13T15:27:08.000Z","updated_at":"2021-09-13T21:40:28.000Z","dependencies_parsed_at":null,"dependency_job_id":"ab868646-f15a-465f-b708-dc7c013a6e39","html_url":"https://github.com/slavikdev/til","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slavikdev%2Ftil","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slavikdev%2Ftil/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slavikdev%2Ftil/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slavikdev%2Ftil/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/slavikdev","download_url":"https://codeload.github.com/slavikdev/til/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245217789,"owners_count":20579297,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["til","tips","tips-and-tricks","today-i-learned","writing"],"created_at":"2024-10-14T19:42:57.584Z","updated_at":"2026-01-05T21:07:40.744Z","avatar_url":"https://github.com/slavikdev.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# TIL: Today I Learned 💡\n\nThis is a living collection of new findings, coding tips and gotchas that I found in my everyday work.\n\n## Data Science\n\n#### Create train/split datasets with a mask\n\nThis code splits the dataset in 80% for training and 20% for testing:\n\n```python\nmask = np.random.rand(len(df)) \u003c 0.8\ntrain = data_set[mask]\ntest = data_set[~mask]\n```\n\n## Python\n\n#### Reverse a string\n\nWhen you pass step size `-1` it reverses the string:\n\n```python\nmystring[::-1]\n```\n\nand you can reverse every word in a string, maintaining the word order:\n\n```python\n' '.join(s[::-1] for s in strs.split())\n```\n\n#### Create array/matrix with prefilled values\n\nCreates 3x3 matrix where all elements equal to -1:\n\n```python\n[[-1]*3]*3\n```\n\n```bash\n[[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]]\n```\n\n_This approach should be avoided, because it creates list of references to the same element!_\nInstead a list comprehension should be used:\n\n```python\n[[-1]*3 for _ in range(3)]\n```\n\n\n## Golang\n\n#### defer call order\n\nSince I normally use only one `defer` statement, I never thought of the execution order. Actually it’s LIFO:\n\n```go\ndefer print(2)\ndefer print(1)\n```\n\nSo that will actually be printed as `12`, even though the `2` statement is first. More practical case, if you want to close and then remove a file:\n```go\ndefer os.Remove(f.Name())\ndefer f.Close()\n```\n\n## C#\n\n#### Convert object to a template method type\n\nIf you want to convert `object` value to a type, specified in a template method, there’s a way to do it without `if-s` and `case-es`:\n\n```csharp\nprivate T GetValue\u003cT\u003e(object objVal)\n{\n   var converter = TypeDescriptor.GetConverter(typeof(T));\n   if (converter == null)\n   {\n      return default(T);\n   }\n   return (T)converter.ConvertFrom(objVal);\n}\n```\n\n## TypeScript\n\n#### Type casting to an object with methods\n\nIf you have a class with attributes and methods and you want to type cast a JSON object to an instance of the class, then `const obj = json as MyClass` will only assign attributes, i.e. later calling something like `obj.myMethod()` will cause a no method error. To create a proper object you need to use `Object.assign`:\n\n```typescript\nconst obj = Object.assign(new MyClass(), json as MyClass)\nobj.myMethod() // this will work now\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslavikdev%2Ftil","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fslavikdev%2Ftil","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslavikdev%2Ftil/lists"}