compile built-in function can be used to precompile an expression to a code object; this code object can then be passed to eval. This will speed up the repeated executions of the evaluated code. The 3rd parameter to compile needs to be the string 'eval'.
>>> code = compile('a * b + c', '&l...
>>> variables = {'a': 6, 'b': 7}
>>> eval('a * b', globals=variables)
42
As a plus, with this the code cannot accidentally refer to the names defined outside:
>>> eval('variables')
{'a': 6, 'b': 7}
>>> eval('variables', globals=variables)
Traceback (most ...
You can fetch the current properties of the Annotation by using Reflection to fetch the Method or Field or Class which has an Annotation applied to it, and then fetching the desired properties.
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
String key() default "foo";...
It is possible to create a QtQuick view directly from C++ and to expose to QML C++ defined properties. In the code below the C++ program creates a QtQuick view and exposes to QML the height and width of the view as properties.
main.cpp
#include <QApplication>
#include <QQmlContext>
#...
You can also use regular expressions to split a string. For example,
import re
data = re.split(r'\s+', 'James 94 Samantha 417 Scarlett 74')
print( data )
# Output: ['James', '94', 'Samantha', '417', 'Scarlett', '74']
If you have a vsix file, you can install it by running the file.
Get the vsix file (this is the extension installer)
Run the file.
In the window that opens, confirm the installation.
In Visual studio
go to Tools > Extensions and updates...
In the window that opens go to online
Select Visual Studio Gallery
You can search for an extension on the search box at the upper right corner
Select the extension you want to add
Click on download.
Once download is complete, click...
Using threading & queue:
from socket import socket, AF_INET, SOCK_STREAM
from threading import Thread
from queue import Queue
def echo_server(addr, nworkers):
print('Echo server running at', addr)
# Launch the client workers
q = Queue()
for n in range(nworkers):
...
Delegates may have variant type parameters.
delegate void Action<in T>(T t); // T is an input
delegate T Func<out T>(); // T is an output
delegate T2 Func<in T1, out T2>(); // T1 is an input, T2 is an output
This follows from the Liskov Substitution Principle, w...
Category theory is a modern mathematical theory and a branch of abstract algebra focused on the nature of connectedness and relation. It is useful for giving solid foundations and common language to many highly reusable programming abstractions. Haskell uses Category theory as inspiration for some o...
Using Kestrel you can specify port using next approaches:
Defining ASPNETCORE_URLS environment variable.
Windows
SET ASPNETCORE_URLS=https://0.0.0.0:5001
OS X
export ASPNETCORE_URLS=https://0.0.0.0:5001
Via command line passing --server.urls parameter
dotnet run --server.urls=http...
If you have a string that contains Python literals, such as strings, floats etc, you can use ast.literal_eval to evaluate its value instead of eval. This has the added feature of allowing only certain syntax.
>>> import ast
>>> code = """(1, 2, {'foo': 'bar'})"...
Unity works with hierarchies in order to keep your project organized. You can assign objects a place in the hierarchy using the editor but you can also do this through code.
Parenting
You can set an object's parent with the following methods
var other = GetOtherGameObject();
other.transform.Se...
Sometimes you want to destructure key under a map which might not be present in the map, but you want a default value for the destructured value. You can do that this way:
(def my-map {:a 3 :b 4})
(let [{a :a
b :b
:keys [c d]
:or {a 1
c 2}} my-map]
(println ...
The routing configuration is included in your app/config/config.yml file, by default the app/config/routing.yml file.
From there you can link to the controllers that have annotated routing configuration:
# app/config/routing.yml
app:
resource: "@AppBundle/Controller"
type: ...
Sometimes, we may open a file which we do not have permission to write in Vim without using sudo.
Use this command to save a read-only file edited in Vim.
:w !sudo tee > /dev/null %
Which you could map to :w!! in your .vimrc:
cmap w!! w !sudo tee > /dev/null %
You will be presented a ...
Starter code to create and remove a full page canvas that responds to resize events via javascript.
var canvas; // Global canvas reference
var ctx; // Global 2D context reference
// Creates a canvas
function createCanvas () {
const canvas = document.createElement(&q...
For each environment you need to create a separate appsettings.{EnvironmentName}.json files:
appsettings.Development.json
appsettings.Staging.json
appsettings.Production.json
Then open project.json file and include them into "include" in "publishOptions" section. This lis...
1. URLByDeletingPathExtension:
If the receiver represents the root path, this property contains a copy of the original URL. If the URL has multiple path extensions, only the last one is removed.
2. URLByAppendingPathExtension:
Returns a new URL made by appending a path extension to the original U...