TIL: NuShell Structured Data Shell, Async Python Performance Reality, and Go Programming Fundamentals
Today I learned about NuShell's structured approach to shell computing, why async Python isn't always faster, comprehensive Go language resources, and effective techniques for asking technical questions.
Today’s discoveries challenged conventional wisdom about async programming while exploring innovative shell design and comprehensive language learning resources.
# Working with JSON directlyhttp get https://api.github.com/repos/nushell/nushell | get stargazers_count
# CSV processingopen data.csv | where salary > 50000|select name age department
# File system as structured datals | where size > 1mb | sort-by modified | reverse
# System informationsys | get host.name
# Convert between formatsopen data.json | to csv | save data.csv
open config.toml | to json | save config.json
# Advanced filtering and groupingopen sales.csv
| where date >= 2023-01-01
| group-by region
| each {|group|{ region: ($group.0),
total_sales: ($group.1 | get amount | math sum),
avg_sale: ($group.1 | get amount | math avg)}}
# File operations work consistently across platformsls **/*.rs | where size > 10kb | get name
# Network operations with structured outputport 8080| get state # Check if port is openwhich python | get path # Find command location# Environment variables as structured data$env| where name=~ PATH | get value
# Mix NuShell with external commandsdocker ps | from ssv | where IMAGE=~ nginx
# Transform and pipe to traditional toolsls | where name=~ ".log"| get name | lines | xargs tail -f
# Use traditional commands when needed^ls -la /usr/bin | lines | length # ^ prefix runs external command
importsysimportasyncioimportthreadingdefmeasure_memory_usage():"""Compare memory usage of different approaches"""# Thread-based approachdefthread_worker():time.sleep(1)threads=[threading.Thread(target=thread_worker)for_inrange(1000)]thread_memory=sys.getsizeof(threads)+sum(sys.getsizeof(t)fortinthreads)# Async approachasyncdefasync_worker():awaitasyncio.sleep(1)tasks=[async_worker()for_inrange(1000)]task_memory=sys.getsizeof(tasks)+sum(sys.getsizeof(t)fortintasks)print(f"1000 threads: ~{thread_memory} bytes")print(f"1000 async tasks: ~{task_memory} bytes")# Async tasks typically use much less memorymeasure_memory_usage()
// Define behavior through interfacestypeWriterinterface{Write([]byte)(int,error)}typeLoggerinterface{Log(messagestring)}// Concrete implementationstypeFileLoggerstruct{filenamestring}func(f*FileLogger)Log(messagestring){// Write to filefmt.Printf("File: %s\n",message)}typeConsoleLoggerstruct{}func(c*ConsoleLogger)Log(messagestring){fmt.Printf("Console: %s\n",message)}// Function that works with any LoggerfuncdoWork(loggerLogger){logger.Log("Starting work")// ... do work ...logger.Log("Work completed")}funcmain(){fileLogger:=&FileLogger{filename:"app.log"}consoleLogger:=&ConsoleLogger{}doWork(fileLogger)doWork(consoleLogger)}
import("errors""fmt")// Custom error typestypeValidationErrorstruct{FieldstringValueinterface{}Reasonstring}func(v*ValidationError)Error()string{returnfmt.Sprintf("validation failed for field '%s' with value '%v': %s",v.Field,v.Value,v.Reason)}// Function with multiple return valuesfuncvalidateUser(namestring,ageint)(*User,error){ifname==""{returnnil,&ValidationError{Field:"name",Value:name,Reason:"name cannot be empty",}}ifage<0||age>150{returnnil,&ValidationError{Field:"age",Value:age,Reason:"age must be between 0 and 150",}}return&User{Name:name,Age:age},nil}// Error handling in actionfuncmain(){user,err:=validateUser("",25)iferr!=nil{varvalidationErr*ValidationErroriferrors.As(err,&validationErr){fmt.Printf("Validation error: %s\n",validationErr.Error())}else{fmt.Printf("Unknown error: %v\n",err)}return}fmt.Printf("Valid user: %+v\n",user)}
These discoveries highlight the importance of understanding the true characteristics of tools and techniques rather than accepting conventional wisdom, while also providing practical frameworks for effective learning and communication.