Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
Have a great weekend guys ✌️🍺

Happy coding :)
Slices are far more efficient than Arrays in Go. Assigning one array to another copies all the elements while in Slice it refers to the underlying Array.

There are major differences between the ways arrays work in Go and C. In Go,

- Arrays are values. Assigning one array to another copies all the elements.
- In particular, if you pass an array to a function, it will receive a copy of the array, not a pointer to it.
- The size of an array is part of its type. The types [10]int and [20]int are distinct.

*NOTE:* The value property can be useful but also expensive; if you want C-like behavior and efficiency, you can pass a pointer to the array.

Read more about arrays and slices here:

- https://golang.org/doc/effective_go.html#arrays

#go #arrays #slices
A very great article about Go styling and how to code readble, maintainable code in Go (it can be used in other languages too):

- https://dave.cheney.net/practical-go/presentations/qcon-china.html#_always_document_public_symbols

#golang #go #maintainable #readble_code
Let's talk a little bit about WaitGroup in sync library. First things first, the documentation is here:
- https://golang.org/pkg/sync/#WaitGroup

A WaitGroup waits for a collection of goroutines to finish. The main goroutine calls Add to set the number of goroutines to wait for. Then each of the goroutines runs and calls Done when finished. At the same time, Wait can be used to block until all goroutines have finished.

With an example it will be crystal clear. Let's say we have a script that fetches URLs concurrently and we want to wait using WaitGroup until all the fetches are complete:

package main

import (
"sync"
)

type httpPkg struct{}

func (httpPkg) Get(url string) {}

var http httpPkg

func main() {
var wg sync.WaitGroup
var urls = []string{
"http://www.golang.org/",
"http://www.google.com/",
"http://www.somestupidname.com/",
}
for _, url := range urls {
// Increment the WaitGroup counter.
wg.Add(1)
// Launch a goroutine to fetch the URL.
go func(url string) {
// Decrement the counter when the goroutine completes.
defer wg.Done()
// Fetch the URL.
http.Get(url)
}(url)
}
// Wait for all HTTP fetches to complete.
wg.Wait()
}


First we create a WaitGroup called wg and using range loop through the URLs on each iterration we use Add to increment WaitGroup counter/queue and run an anonymous function that gets a URL. When func i
s done getting the URL, defer will be run and Done decrement the counter in WaitGroup. Finally we use Wait to actually wait for all http calls to get completed.

NOTE: anonymous function is preceded with go keyword that runs the function concurrently.

#golang #go #sync #WaitGroup #Done #Add #Wait
How to add nested documents in Marshmallow python?

books = fields.List(fields.Dict(
keys=fields.String(validate=OneOf(('title', 'author', 'publication_date'))),
values=fields.String(required=True)))


#python #data_class #marshmallow #fields #list #OneOf
Run a specific unit test in pytest:

pytest tests/test_user.py -k 'TestClassName and test_method_name'


#python #unitest #pytest
In Dockerfile`s some people in the community use `alpine base image in order to reduce docker image size. apk is its package management tool that can be used to install OS packages. So for example if
you want to install network tools (like ping) you need to install netcat-openbsd:

apk add netcat-openbsd


You can squash your image size even further by some tips. When you install a package, linux distros first download the package and put it in a cache folder. In Alpine it is located in /var/cache/apk.
To tell the OS to delete the cache after installation you can provide --no-cache option to it:

apk add --no-cache netcat-openbsd


There are some package like g++ or git that is needed on installation of some other packages. After installation those packages is useless and just increase image size. You can remove those packages b
y using --virtual command:

apk add --no-cache --virtual .build-deps g++ \
&& # do you stuff here \
&& apk del .build-deps


Great job guys! You have reduced your alpine docker images so much :)

#docker #linux #alpine #apk #virtual #no_cache #apk_del #apk_add
In marshmallow you can have a schema field which can be filled with a method output. Let's see with an example:

class AuthorSchema(Schema):
id = fields.Int(dump_only=True)
first = fields.Str()
last = fields.Str()
formatted_name = fields.Method("format_name", dump_only=True)

def format_name(self, author):
return "{}, {}".format(author.last, author.first)


As you see formatted_name field is filled by a method called format_name. Examples are endless, you can calculate average score based on the given scores for instance.

#python #marshmallow #fields #fields_method
GJSON is a Go package that provides a fast and simple way to get values from a json document. It has features such as one line retrieval, dot notation paths, iteration, and parsing json lines.

Las mere:
https://github.com/tidwall/gjson

#golang #go #gjson
A father before he died said to his son: “this is a watch your grandfather gave me, and is more than 200 years old. But before I give it to you, go to the watch shop on the first street, and tell him I want to sell it, and see how much he offers you”. He went, and then came back to his father, and said, "the watchmaker offered 5 dollars because it's old”. He said to him : “go to the coffee shop”. He went and then came back, and said: “He offered $5 father”. “Go to the museum and show that watch”. He went then came back, and said to his father “They offered me a million dollars for this piece”. The father said: “I wanted to let you know that the right place values you in right way. Don't find yourself in the wrong place and get angry if you are not valued. Those that know your value are those who appreciate you, don't stay in a place where nobody sees your value".

Know your worth.
pdsh: a high performance, parallel remote shell utility. Pdsh is a multithreaded remote shell client which executes commands on multiple remote hosts in parallel. Pdsh can use several different remote shell services, including standard "rsh", Kerberos IV, and ssh.

To see more about it head over to:
- https://github.com/grondo/pdsh

#linux #shell #pdsh #shell
Hello @all,
I'm looking for an Angular +6 developer with a very good salary! In case there is a person who is looking for a challenging job, please send a PV to me.
In order to expand a CIDR in python you can use ipaddress module as below:

import ipaddress
available_ips = [str(ip) for ip in ipaddress.IPv4Network('192.0.2.0/28')]


#python #ipaddress #ipv4 #IPv4Network
شرکتی در ایران نیازمند برنامه نویس فرانت اند هست. لطفاً در صورتی که تمایل به همکاری دارید به بنده پیام بدید:
@alirezastack

#شغل
How to recursively rename filenames?

find . -name '*txt' -exec bash -c ' mv $0 ${0/brand-/category-}' {} \;


The above command renames txt files starting with brand- to category-.

#linux #bash #find #rename #batch_rename
دوستان برای کار روی یه پروژه کوچیک یکی از همکاران نیاز به نیروی فرانت داره. اگر کسی هست لطفاً پیام بده.

#job #project #offer