frodocoder

Since
9 Snippets
  • Writing JSON to a file in Python

    import json
    import os
    import uuid
    
    filename = str(uuid.uuid4()) # create random file name
    
    # JSON data to be written
    jsonDict ={
        "firstname" : "John",
        "lastname" : "Doe",
        "age" : 30,
        "phonenumber" : "9976770500"
    }
    
    # write JSON to file
    with open(filename, "w") as f:
        json.dump(jsonDict, f)
    
    # read JSON string from file
    f = open(filename, "r")
    print(f.read())
    f.close()
    os.remove(filename)
    
    # Output:
    # {"firstname": "John", "lastname": "Doe", "age": 30, "phonenumber": "9976770500"}

    You can write JSON to a file using the json.dump() function from the JSON module combined with file handling in Python. In thisexample, we open a file in writing mode. If the file doesn't exist, it will be created. The json.dump() function converts the Python dictionary into a JSON string, which is then saved in the file.

  • Read JSON file using Python

    import json
    import uuid
     
    jsonString = '{"firstname": "John", "lastname": "Doe"}'
    
    # Create random file name and write json to file
    filename = str(uuid.uuid4())
    
    with open(filename, "w") as f:
        f.write(jsonString)
    
    # Parse JSON string
    # Deserializes json string into dict
    print("Parse JSON string")
    jsonDict = json.loads(jsonString)
    
    # Iterating through the json
    for key, value in jsonDict.items():
        print(key, value)
    
    print()
    #----------------------------------------------------#
    
    print("Read JSON file")
    f = open (filename, "r")
     
    # Reading from file
    jsonDict = json.loads(f.read())
     
    # Iterating through the json
    for key, value in jsonDict.items():
        print(key, value)
     
    # Closing file
    f.close()
    
    # Output
    # Parse JSON string
    # firstname John
    # lastname Doe
    #
    # Read JSON file
    # firstname John
    # lastname Doe

    This exampleillustrates how to read from both a string and a JSON file. Initially, we have a JSON string stored in the variable 'jsonString'. We convert this JSON string into a Python dictionary using json.loads() method, which is then stored in the variable 'jsonDict'. Next, we read a JSON string stored in a file using json.loads(). To achieve this, we first convert the JSON file into a string using file handling, similar to the previous example. Then, we convert it into a string using the read() function. The subsequent steps mirror those followed earlier, utilizing the json.loads() method.

  • Read a file line by line in Python

    import os
    import uuid
    
    filename = str(uuid.uuid4()) # create random file name
    wLines = ["First line\n", "Second line\n", "Third line\n"]
    
    # writing lines to file
    f = open(filename, 'w')
    f.writelines(wLines)
    f.close()
    
    #--------------------------------------------------------#
    
    print("-----Read lines using 'readlines' method-----")
    f = open(filename, 'r')
    rLines = f.readlines()
    
    for lineNumber, line in enumerate(rLines, 1):
    	print("Line {}: {}".format(lineNumber, line.strip()))
    
    #--------------------------------------------------------#
    
    print("-----Read lines using 'readline' method-----")
    f = open(filename, 'r')
    lineNumber = 1
     
    while True:
        lineNumber += 1
     
        # Get next line from file
        line = f.readline()
     
        # if line is empty
        # end of file is reached
        if not line:
            break
        print("Line {}: {}".format(lineNumber, line.strip()))
     
    f.close()
    
    #--------------------------------------------------------#
    
    print("-----Read lines via file object iteration-----")
    f = open(filename, 'r')
    
    for lineNumber, line in enumerate(f, 1):
        print("Line {}: {}".format(lineNumber, line.strip()))
    
    #--------------------------------------------------------#
    
    print("-----Read lines via file contex manager-----")
    with open(filename, "r") as f:
        for lineNumber, line in enumerate(f, 1):
            print("Line {}: {}".format(lineNumber, line.strip()))
    
    os.remove(filename) # remove file
    
    # Output example:
    
    # Read lines using 'readlines' method
    # Line 1: First line
    # Line 2: Second line
    # Line 3: Third line

    Here's an example of reading from a file line by line in Python.

  • Generate random hash or token in Go

    package main
    
    import (
    	"crypto/rand"
    	"crypto/sha256"
    	"encoding/hex"
    	"fmt"
    )
    
    func GenerateRandomHash(n int) (string, error) {
    	b := make([]byte, n)
    	_, err := rand.Read(b)
    
    	// Note that err == nil only if we read len(b) bytes.
    	if err != nil {
    		return "", err
    	}
    
    	hasher := sha256.New()
    	hasher.Write(b)
    	sha := hex.EncodeToString(hasher.Sum(nil))
    
    	return sha, nil
    }
    
    func main() {
    	hash, err := GenerateRandomHash(512)
    
    	if err != nil {
    		panic(err)
    	}
    
    	fmt.Println(hash)
    }
    
    // go run main.go 
    // 34c0fb393623843e56719b5d9d66385a55b4b4d3393187b7b1a76aee46c421c5

    Here is an example of randomly generating a hash or token in Go.

  • SHA256 hashes in Go

    package main
    
    import (
        "crypto/sha256"
        "fmt"
    )
    
    func main() {
        str := "some string to calculate hash"
        hash := sha256.New()
    
        hash.Write([]byte(str))
        sum := hash.Sum(nil) // the argument can be used to append to an existing byte slice
    
        fmt.Println(str)
        fmt.Printf("%x\n", sum)
    }
    
    // go run main.go 
    // some string to calculate hash
    // 7aad383b9ad516fa67057adc283ce2cf71858aff317a5e267adebfdbd5dda5fd

    SHA256 hashes are commonly used to generate short identifiers for binary or text data. This example shows how to calculate SHA256 hashes for string in Go.

  • Atomic counters in Go

    package main
    
    import (
    	"fmt"
    	"sync"
    	"sync/atomic"
    )
    
    func main() {
    	var atomicCounter, nonAtomicCounter uint64
    	wg := sync.WaitGroup{}
    
    	for i := 0; i < 50; i++ {
    		wg.Add(1)
    
    		go func() { // run 50 goroutines, each goroutine increases the counter 1000 times
    			for c := 0; c < 1000; c++ {
    				atomic.AddUint64(&atomicCounter, 1) // increase atomic counter
    				nonAtomicCounter++ // increase non-atomic counter
    			}
    
    			wg.Done()
    		}()
    	}
    
    	wg.Wait() // wait until all goroutines finish
    	
    	fmt.Println("atomic counter value:", atomicCounter)
    	fmt.Println("non atomic counter value:", nonAtomicCounter)
    }
    
    // go run counters.go 
    // atomic counter value: 50000
    // non atomic counter value: 30648

    An example of using the sync/atomic package for atomic counters accessed by goroutines.

  • Example of timeouts in Go

    package main
    
    import (
    	"fmt"
    	"time"
    )
    
    func main() {
    	first := make(chan string, 1)
    
    	go func() {
    		time.Sleep(2 * time.Second) // wait 2 seconds before send value to channel
    		first <- "first result"     // send value to the channel
    	}()
    
    	select {
    	case result := <-first:
    		fmt.Println(result)
    	case <-time.After(time.Second): // timeout occurs before the value is read from the first channel
    		fmt.Println("first timeout")
    	}
    
    	second := make(chan string, 1)
    
    	go func() {
    		time.Sleep(time.Second)   // wait 1 second before send value to the channel
    		second <- "second result" // send value to the channel
    	}()
    
    	select {
    	case result := <-second: // the value from the channel is read before the timeout expires
    		fmt.Println(result)
    	case <-time.After(2 * time.Second):
    		fmt.Println("second timeout")
    	}
    }
    
    // $ go run timeouts.go 
    // first timeout
    // second result

    Timeouts are important for programs that connect to external resources or need to limit their execution time.

  • Example of using Go tickers

    package main
    
    import (
    	"fmt"
    	"time"
    )
    
    func main() {
    	ticker := time.NewTicker(time.Second)
    	fmt.Println("ticker is started")
    
    	done := make(chan struct{})
    
    	go func() {
    		for {
    			select {
    			case <-done:
    				return
    			case t := <-ticker.C:
    				fmt.Println("tick", t)
    			}
    		}
    	}()
    
    	time.Sleep(5 * time.Second)
    
    	ticker.Stop()
    	fmt.Println("ticker is stopped")
    
    	time.Sleep(5 * time.Second)
    	done <- struct{}{}
    }
    
    // go run tickers.go 
    // ticker is started
    // tick 2024-02-09 23:40:25.809616086 +0700 +07 m=+1.000091237
    // tick 2024-02-09 23:40:26.809742343 +0700 +07 m=+2.000152938
    // tick 2024-02-09 23:40:27.809804037 +0700 +07 m=+3.000214622
    // tick 2024-02-09 23:40:28.809852444 +0700 +07 m=+4.000263029
    // ticker is stopped
    // tick 2024-02-09 23:40:29.809908932 +0700 +07 m=+5.000319557
    

    Tickers allow you to repeat actions at certain intervals. A Ticker holds a channel that delivers "ticks" of a clock at intervals. Tickers can be stopped in the same way as timers. When the ticker is stopped, it will no longer be able to accept values ​​into its channel.

  • Example of using Go timers

    package main
    
    import (
    	"fmt"
    	"time"
    )
    
    func main() {
    	firstTimer := time.NewTimer(3 * time.Second)
    	value := <-firstTimer.C // value is a current time
    
    	fmt.Printf("first timer expired: %v\n", value) // the line printed 3 seconds after running code
    
    	secondTimer := time.NewTimer(2 * time.Second)
    
    	go func() {
    		<-secondTimer.C
    		fmt.Println("second timer expired") // the line is not printed because the second timer is stopped before expiration
    	}()
    
    	stop := secondTimer.Stop() // stop second timer before it expired
    
    	if stop {
    		fmt.Println("second timer is stopped")
    	}
    }
    
    // $ go run timers.go
    // first timer expired: 2024-02-09 22:32:57.570649221 +0700 +07 m=+3.002054417
    // second timer is stopped

    Timers allow you to execute one event in the future. You tell the timer how long you want to wait and it provides a channel to be notified at that time. The first timer will wait for 3 seconds. <-firstTimer.C blocks the timer C channel until a message (current time) is sent indicating that the timer has expired. If you just want to wait, you can use time.Sleep. One reason a timer can be useful is that you can cancel the timer before it expires. The first timer expires 3s after the program starts, but the second is stopped before it expires.