I can never get around to writing this blog post. It's an ambitious one, with great examples an schematics
planned. Anyway, maybe someone will still find the beginnings of it useful.
This is a writeup in layman's terms about how memory is being allocated and managed in most modern programming languages. Its target audience is users of Crystal programming language, but the parts specific to that language will be on purple background. Expect some omissions and inaccuracies for the purpose of explanation.
Crystal Programming Language does not yet support Windows, but there's an awesome work-in-progress fork that already allows compiling some Crystal programs and getting native Windows executables. None of that work is mine, I just rehashed their instructions and added good news that one can already make proper video games for Windows.
Note that, even though there are multiple mentions of Windows Subsystem for Linux here, it is used only for one of the build steps. The resulting binaries run natively on Windows, no strings attached.
You need a POSIX-compatible system that Crystal officially supports (like Linux) and also Windows. If you are already on Windows 10, the obvious choice is Bash on Ubuntu on Windows. If on a different OS, use a virtual machine with Windows — VirtualBox is a good choice.
- |# Download the latest release of Nim into the "nim-master" folder git clone -b master --depth 1 git://github.com/nim-lang/nim nim-master/
# Download the latest release of Nim's prepared C sources, for bootstrapping git clone -b master --depth 1 git://github.com/nim-lang/csources csources/
# Build C sources sh build.sh
# This concludes the first step of bootstrapping, don't need C sources anymore rm -rf csources
# Use the executable built from C sources to compile the build tool bin/nim c koch
# Compile Nim in release mode, using the Nim compiler we already have ./koch boot -d:release
# Add the 'bin' folder to PATH - exportPATH="nim-master/bin:$PATH"script:
# Run 'tests/all.nim'. Feel free to change this, but it needs to be a program# that returns a non-zero status code in case of failure. The testing facilities# in Nim's standard library do this. - nim compile --verbosity:0 --run tests/all
ksshaskpass is a KDE-based passphrase dialog for use with OpenSSH. It is intended to be called by the ssh-add(1) program and not invoked directly. It allows ssh-add(1) to obtain a passphrase from a user
So this utility is useful if you want to store the passphrases to your SSH keys in the KDE Wallet (there are plenty of instructions on how to do that).
But all it really does is receive a string as an argument, tries to query KWallet for a password saved under this key string (or asks for this password in a dialog and stores it), then writes it to stdout. This means that ksshaskpass can be used to store any kind of password in KWallet for use in your Bash scripting.
Sometimes in Python you may need to diagnose some format strings. Maybe they are supposed to be provided by the user and you need to know which keys to fill it with. Another prominent example is translation software: format string placeholders are definitely a thing there, and you may want to do something special about them.
First, let's get the old-style format strings out of the way. I haven't seen any introspection capabilities for them in the standard library, so instead this neat workaround can be used: a format string is formatted by a dict-like object, which really just stores every key that it gets asked for.
The problem of intersection of two rectangles is very easy to solve. But difference of two rectangles is something considered much less frequently.
To clarify, we will be looking only at rectangles that are parallel to the 2D axes.
Obviously, when one rectangle chomps away a part of another, typically it can no longer be represented as just one rectangle. The illustration shows these different cases. The solution described here represents the difference as a sum of multiple rectangles. It does not try to represent it in as few rectangles as possible, because there are multiple ways to do that and no objective way to pick one.
This example is about Flask-Admin, which is a library that adds a smart automatic CRUD panel according to your data models (typically SQLAlchemy models).
If you have a simple website or want to separate your normal user authentication system from the admin login, or just want a quick temporary solution for securing your admin panel, here is how you can do it using simple HTTP authentication:
If you are the sole user of your computer, you may want to just enable autologin without asking for password. But most often this is not an option. So when you turn on your computer, you wait for the login screen to appear, then enter the password, then wait for the auto-start programs to complete loading (or cope with the slowness).
Instead, you can have your system automatically log into your account and let the startup process take place, but also lock the screen, so the password still needs to be entered. The difference is, you can turn on your computer, then walk away somewhere, and be sure that it's 100% ready for work when you come back. Even without walking away, the time you take to enter your password is used for startup, so it's still quicker.
I realize that suspending may be an even better solution, but it's not always an option.
Let's say we got some bytestring through a socket, but it was cut off in the middle of a UTF-8 character.
We can simulate this:
bs="приклад".encode('utf-8')[:-1]# last byte was lostprint(bs)#< b'\xd0\xbf\xd1\x80\xd0\xb8\xd0\xba\xd0\xbb\xd0\xb0\xd0'
bs.decode('utf-8')# UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 12: unexpected end of data
So, is this string completely undecodable? Can't we just get "прикла"?
Don't worry, I have a solution. Works with 3.x and 2.7!
importstrutils,algorithm,futurevarnames="Brian Chris Pamela Michael Tammy Michelle Roy Timothy".split()# Find shortest and longest nameblock:proc `<`(a,b:string):bool=a.len<b.lenechomin(names)," and ",max(names) #< Roy and Michelle
# Sort names by lengthnames.sort((a,b)=>cmp(a.len,b.len))echonames#< @[Roy, Brian, Chris, Tammy, Pamela, Michael, Timothy, Michelle]
Let's say you have a function that returns a seq of N bytes, such as this one.
proc urandom*(size:Natural):seq[uint8]## Returns a ``seq`` of random integers ``0 <= n < 256`` provided by## the operating system's cryptographic source## ## POSIX: Reads and returns `size` bytes from the file ``/dev/urandom``.
It's a useful function, and all is well. But what if you want to give the result of urandom(32) to a function that takes an array of 32 bytes? Converting a seq to an array of fixed size is surprisingly complicated and dirty (but I may be missing something).
Long story short, you can use static[T] to provide an alternative generic function that returns arrays of fixed size.
C compilers, like GCC, provide various "builtin" compiler-specific extensions (functions). Sure, using them may be looked down upon, but sometimes they can give you access to hardware-optimized operations that significantly boost performance. You can always provide a slower fallback.
Let's take as an example the operation "find how many bytes a number would take up". I needed this to implement a function that returns a random number in a specific range, using an entropy source that just gives bytes, such as /dev/urandom.