programming

Python temporary file

August 2, 2020
python, programming, libs

tags Python Programming Python’s tempfile standard library is pretty neat when we need to create a temporary file and/or directories. Instead of having a much of code like this: import pathlib def create_file(filename): if pathlib.Path(filename).exists(): # handle path removal/rewrite/backup pass with open(filename, "w") as f: # write to file pass We can ask Python to take care of handling this type of tasks for us. tempfile handles most of the OS API, so we can focus on writting the logic instead. ...

Python Async

July 30, 2020
python, programming

tags Python Programming Important keywords # Asynchronous IO (async IO) Language-agnostic paradigm (model) coroutine A Python (sort of generator function) async/await Python keywords used to defined a coroutine asyncio Python package that provides an API for running/managing coroutines Coroutine # A coroutine allows a function to pause before returning or indirectly call another coroutine for some time, for example: import asyncio import time async def count(n): print(f"n is {n}") await asyncio. ...

Use case template

June 23, 2020
programming, methodology, cs

tags Computer Science Programming Methodologies Software Engineering The following is a requirements template developed by Alistair Cockburn. This template may or may not have all or more fields related to the requirements itself. Recommended reading: Basic Use Case Template USE CASE #N Goal in context Scope Level <one of: Summary, Primary task, Subfunction> Preconditions Success end condition Failed end condition Primary actor <a role name for the primary actor, or description> Trigger <the action upon the system that starts the use case, may be time event> Priority <how critical to your system/organization> Frequency Usage example: ...

Go interfaces

June 22, 2020
go, programming, cs

tags Go Programming Programming Code snippets The following implements a DataModel, like Django’s Model class which requires Validate and Save methods be implemented by whoever uses that Interface. The problem is the Model. m object will be nil, because Model cannot access the User due to the embedded Model struct within the User struct. package main import "fmt" type DataModel interface { Validate() Save() } type Model struct { DataModel } func (m *Model) Validate(){ fmt. ...

List Foreign key constrants defined in a database

June 21, 2020
sql, programming, database

tags Programming Code snippets Postgres Postgres View foreign key constrants defined in all databases select kcu.table_schema || '.' ||kcu.table_name as foreign_table, '>-' as rel, rel_tco.table_schema || '.' || rel_tco.table_name as primary_table, string_agg(kcu.column_name, ', ') as fk_columns, kcu.constraint_name from information_schema.table_constraints tco join information_schema.key_column_usage kcu on tco.constraint_schema = kcu.constraint_schema and tco.constraint_name = kcu.constraint_name join information_schema.referential_constraints rco on tco.constraint_schema = rco.constraint_schema and tco.constraint_name = rco.constraint_name join information_schema.table_constraints rel_tco on rco.unique_constraint_schema = rel_tco. ...

Go reading struct tag

June 21, 2020
go, programming

tags Go Programming Programming Code snippets The following code reads a specific struct Tag by name. import ( "reflect" "strings" "fmt" ) type Profile struct { Id int `validate:"numeric" json:"id"` First string `validate:"required" json:"first"` Last string `validate:"required" json:"last"` Birth string `validate:"required" json:"birth"` Sex string `validate:"required" json:"sex"` MaritalStatus string `validate:"required" json:"marital_status"` Children int `validate:"numeric" json:"children"` } func GetStructFieldValidators(data interface{}) map[string][]string { v := reflect.ValueOf(data) t := reflect.TypeOf(data) validators := make(map[string][]string) for i := 0; i < v. ...

Go defer

June 19, 2020
go, programming, cs

tags Go Programming Programming import "fmt" func trace(name string) string { fmt.Println("Entering ", name) return name } func un(name string) string { fmt.Println("Exiting", name) return name } func a() { defer un(trace("a")) fmt.Println("Hello, world") } func b(){ defer un(trace("b")) fmt.Println("Before i < 10 loop") for i:=0 ; i < 10; i++{ defer fmt.Println("i =", i) } fmt.Println("After i < 10 loop") } func main(){ a() b() } Entering a Hello, world Exiting a Entering b Before i < 10 loop After i < 10 loop i = 9 i = 8 i = 7 i = 6 i = 5 i = 4 i = 3 i = 2 i = 1 i = 0 Exiting b

Code snippets

May 31, 2020
snippets, programming, code

Backlinks # Go reading struct tag Go interfaces List Foreign key constrants defined in a database tags Programming Python Python run HTTP server locally # 1 python -m http.server 8000 --bind 127.0.0.1