Dynamic %%writefile in Jupyter - jupyter

I have a variable that holds the contents of a file I'd like to write in Jupyter using %%writefile. However, only the cell magic line accepts variables, e.g. %%writefile $FILE_NAME. Is there a way to make the contents dynamic, too, i.e. something like this?
%%writefile some.file
${variable_that_holds_contents_of_file}
The cell magic %save some.file variable does it in a similar way, but it always creates a .py file and that's not what I want in my use case. It's a JSON file.

My experience is similar to yours, %%writefile will not interpret variables.
If you want a handy one liner for printing a variable to a file you can use print:
somejson = """{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}"""
print(somejson, file=open('output.json', 'a'))

Related

VS Code Spell Checker

I enabled the VS Code extension, "Code Spell Checker," and it works great. However, I wanted to include words from my custom dictionary file so the words in it aren't flagged as incorrect. I tried the following in my settings.json:
"cSpell.customUserDictionaries": [
"name": "Custom",
"description": "These are words from my custom dictionary.",
"path": "C:\\Users\\Joe\\Documents\\Custom.txt",
"addWords": false
],
But the words in Custom.txt are still marked as incorrect.
How can I configure Code Spell Checker so that it's able to load all the words in Custom.txt and ignore them?
According to their package.json that configuration is expecting a typeof array of objects, so the following should work:
"cSpell.customUserDictionaries": [
{
"name": "Custom",
"description": "My desc",
"path": "C:\\Users\\Joe\\Documents\\Custom.txt",
"addWords": false
}
],
And per their description:
File Format: Each line in the file is considered a dictionary entry

Passing multiple json as a payload for a request in Gatling

sample json payload:
'{
"Stub1": "XXXXX",
"Stub2": "XXXXX-3047-4ed3-b73b-83fbcc0c2aa9",
"Code": "CodeX",
"people": [
{
"ID": "XXXXX-6425-EA11-A94A-A08CFDCA6C02"
"customer": {
"Id": 173,
"Account": 275,
"AFile": "tel"
},
"products": [
{
"product": 1,
"type": "A",
"stub1": "XXXXX-42E1-4A13-8190-20C2DE39C0A5",
"Stub2": "XXXXX-FC4F-41AB-92E7-A408E7F4C632",
"stub3": "XXXXX-A2B4-4ADF-96C5-8F3CDCF5821D",
"Stub4": "XXXXX-1948-4B3C-987F-B5EC4D6C2824"
},
{
"product": 2,
"type": "B",
"stub1": "XXXXX-42E1-4A13-8190-20C2DE39C0A5",
"Stub2": "XXXXX-FC4F-41AB-92E7-A408E7F4C632",
"stub3": "XXXXX-A2B4-4ADF-96C5-8F3CDCF5821D",
"Stub4": "XXXXX-1948-4B3C-987F-B5EC4D6C2824"
}
]
}
]
}'
I am working on a POST call. Is there any way to feed multiple json files as a payload in Gatling. I am using body(RawFileBody("file.json")) as json here.
This works fine for a single json file. I want to check response for multiple json files. Is there any way we can parametrize this and get response against multiple json files.
As far as I can see, there's a couple of ways you could do this.
Use a JSON feeder (https://gatling.io/docs/current/session/feeder#json-feeders). This would need your multiple JSON files to be in a single file, with the root element being a JSON array. Essentially you'd put the JSON objects you have inside an array inside a single JSON file
Create a Scala Iterator and have the names of the JSON files you're going to use in it. e.g:
val fileNames = Iterator("file1.json", "file2.json)
// and later, in your scenario
body(RawFileBody(fileNames.next())
Note that this method cannot be used across users, as the iterator will initialize separately for each user. You'd have to use repeat or something similar to send multiple files as a single user.
You could do something similar by maintaining the file names as a list inside Gatling's session variable, but this session would still not be shared between different users you inject into your scenario.

Explorer/Context Menus. Is it possible to change folder context depending on a file inside the folder?

So I currently have context menus based off the folder in VScode.
I am wondering if I can do context based on whether the file exists inside the folder. An example being if temp.exe doesn't exist in the folder don't show the context?
Can't seem to find anywhere in the documentation.
Thanks,
Trent
This is for a Vscode extension. I've only tried the ability to get context based on filetype.
"menus": {
"explorer/context":[
{
"when": "explorerResourceIsFolder",
"command": "extension.runApp",
"group": "vm#1"
},
{
"when":"explorerResourceIsFolder",
"command": "extension.provisionApp",
"group": "vm#2"
}
]
}
This in operator for when clauses looks like it is what you were looking for. From the v1.49 Release Notes:
We have added a new in operator for when clauses. This new
operator allows for a dynamic lookup of a context key's value within
another context key's value. For example, if you wanted to add a
context menu command to folders that contain a certain type of file
(or something that can't be statically known), you can now use the
in operator to achieve it.
vscode.executeCommand('setContext', 'ext:supportedFolders', [ 'test', 'foo', 'bar' ]);
// Note in this case (using an object), the value doesn't matter, it is based on the existence of the key in the object
vscode.executeCommand('setContext', 'ext:supportedFolders', { 'test': true, 'foo': 'anything', 'bar': false });
// Note, this assumes you have already defined a command called ext.doSpecial
"menus": {
"explorer/context": [
{
"command": "ext.doSpecial",
"when": "explorerResourceIsFolder && resourceFilename in ext:supportedFolders"
}
]
}
In that example, we are taking the value of resourceFilename (which
is the name of the folder in this case) and checking for its existence
in the value of ext:supportedFolders. If it exists the menu will be
shown.

Create Entities and training phrases for values in functions for google action

I have created a trivia game using the SDK, it takes user input and then compares it to a value in my DB to see if its correct.
At the moment, I am just passing a raw input variable through my conversation, this means that it regularly fails when it mishears the user since the exact string which was picked up is rarely == to the value in the DB.
Specifically I would like it to only pick up numbers, and for example realise that it must extract '10' , from a speech input of 'my answer is 10'.
{
"actions": [
{
"description": "Default Welcome Intent",
"name": "MAIN",
"fulfillment": {
"conversationName": "welcome"
},
"intent": {
"name": "actions.intent.MAIN"
}
},
{
"description": "response",
"name": "Raw input",
"fulfillment": {
"conversationName": "rawInput"
},
"intent": {
"name": "raw.input",
"parameters": [{
"name": "number",
"type": "org.schema.type.Number"
}],
"trigger": {
"queryPatterns":[
"$org.schema.type.Number:number is the answer",
"$org.schema.type.Number:number",
"My answer is $org.schema.type.Number:number"
]
}
}
}
],
"conversations": {
"welcome": {
"name": "welcome",
"url": "https://us-central1-triviagame",
"fulfillmentApiVersion": 2
},
"rawInput": {
"name": "rawInput",
"url": "https://us-central1-triviagame",
"fulfillmentApiVersion": 2
}
}
}
app.intent('actions.intent.MAIN', (conv) => {
conv.data.answers = answersArr;
conv.data.questions = questionsArr;
conv.data.counter = answersArr.length;
var thisQuestion = conv.data.questions;
conv.ask((conv.data.answers)[0]));
});
app.intent('raw.input', (conv, input) => {
if(input == ((conv.data.answers)[0])){
conv.ask(nextQuestion());
}
app.intent('actions.intent.TEXT', (conv,input) => {
//verifying if input and db value are equal
// at the moment input is equal to 'my number is 10' (for example) instead of '10'
//therefore the string verification never works
conv.ask(nextQuestion());
});
In a previous project i used the dialogflow UI and I used this #system.entities number parameter along with creating some training phrases so it understands different speech patterns.
This input parameter I am passing through my conv , is only a raw string where I'd like it to be filtered using some sort of entity schema.
How do I create the same effect of training phrases/entities using the JSON file?
You can't do this using just the Action SDK. You need a Natural Language Processing system (such as Dialogflow) to handle this as well. The Action SDK, by itself, will do speech-to-text, and will use the actions.json configuration to help shape how to interpret the text. But it will only return the entire text from the user - it will not try to determine how it might match an Intent, nor what parameters may exist in it.
To do that, you need an NLP/NLU system. You don't need to use Dialogflow, but you will need something that does the parsing. Trying to do it with simple pattern matching or regular expressions will lead to nightmares - find a good system to do it.
If you want to stick to things you can edit yourself, Dialogflow does allow you to download its configuration files (they're just JSON), edit them, and update or replace the configuration through the UI or an API.

How to insert current date time in vscode?

Does anyone know of a way that I can insert the current date & time in a visual studio code by snippets?
I have looked docs but did not get any information about it.
I want to create a snippets like this:
title: theFileTitle
date: 2016-08-05 09:44:16
As of Jan 2018 (release 1.20) you can use these new snippet environment variables.
Your example above would look like this:
"File Header": {
"prefix": "header",
"description": "Output a file header with the file name and date",
"body": [
"title: $TM_FILENAME",
"date: $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND",
]
}
Type head, press ctrl+space and it should show snippet menu.
I have created an extension for you that allows to insert formatted date and/or time string - Insert Date String.
Installation
Open Command Palette by pressing F1, type ext install + press Enter and then look for Insert Date String extension.
Usage
To insert current date and/or time at the cursor position you can:
Press ⇧+⌘+I (OS X) or Ctrl+Shift+I (Windows and Linux), or open Command Palette by pressing F1 and type Insert DateTime then press Enter.
Configuration
By default you don't have to set anything. But if you want to change the datetime format, look for insertDateString.format option in user settings.
// Date format to be used.
"insertDateString.format": "YYYY-MM-DD hh:mm:ss",
You can specify any valid ISO 8601 format. There are some examples in readme.
Snippet
Unfortunately you can't use anything more than tab stops or variables in snippets so you'll have to enter the title and date/time manually.
You can define snippets for specific languages. To open a snippet file for editing, open User Snippets under File > Preferences (Code > Preferences on Mac OS X) and select the language for which the snippets should appear.
Following example is for Plain Text files.
After opening a snippet file for Plain Text, add following definition:
{
"File header": {
"prefix": "header",
"body": [
"title: ${title:Enter title}",
"date: ${date:Insert datetime string (⇧⌘I or Ctrl+Shift+I)}"
]
}
}
Now you can open a new plaintext file, enter header and press Tab. Enter your title and use Insert DateTime command to insert current date and/or time.
Idea for a more customizable solution
One could write an extension for inserting such headers. This way some sort of templates with several predefined variables (e.g. date, filename, configurable username/email, etc.) might be used.
Hope this helps!!
if you don't want create a snippet, there is a simple way, using keybindings.
open keybindings.json (Preferences: Open Keyboard Shortcuts (JSON)), and add fellow code to your keybindings.json
[
{
"key": "cmd+k t",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "$CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND"
}
}
]
that's all.
now you can use cmd+k t to insert current data time while typing.
You can just use the following variables in your snippets:
$CURRENT_YEAR
$CURRENT_YEAR_SHORT
$CURRENT_MONTH
$CURRENT_DATE
$CURRENT_HOUR
$CURRENT_MINUTE
$CURRENT_SECOND
Links to the official VSCode docs:
date and time in snippets
user defined snippets
The env variable TM_FILENAME will fill the title with the file name automatically.
For example:
"title: ${1:$TM_FILENAME_BASE}"
You could also use a tool outside of Code, like for example Texter.
I have configured it to replace [t with [%ds %t], which gives me [9/11/2017 16:30] while I type, regardless of application.
It might be a bit of an overkill but you can check Org Mode extension which has this functionality and more:
You can use this snippet for Vscode, I used to refer to this in my code files.
"file description": {
"prefix": "template",
"body": [
"\"\"\"",
"# _* coding: utf8 *_",
"",
"filename: $TM_FILENAME",
"",
"#author: sounishnath",
"createdAt: $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND",
"\"\"\"",
"",
""
],
"description": "file description"
}
See https://stackoverflow.com/a/74082031/836330 for how to insert the Intl.DateTimeFormat timeStamp with options with a keybinding:
Or see Command Variable extension. It uses the Intl.DateTimeFormat format and can be used in a keybinding like so:
{
"key": "alt+d",
"when": "editorTextFocus",
"command": "extension.commandvariable.dateTimeInEditor",
"args": {
"locale": "en-US",
"options": {
"year": "numeric",
"month": "long",
"weekday": "long",
"day": "2-digit",
"hour12": false,
"hour": "2-digit",
"minute": "2-digit",
"second": "2-digit"
},
"template": "${month} ${day}, (${weekday}), ${year} - ${hour}:${minute}::${second}"
}
},
to produce
March 25, (Wednesday), 2020 - 21:16::49
and many other timestamp versions. See the possibilities at Intl.DateTimeFormat